From 677dd45b2ebb4e3fc19f121ac61fd6144f993367 Mon Sep 17 00:00:00 2001 From: IOsetting Date: Sun, 9 Jan 2022 19:27:37 +0800 Subject: [PATCH] feat: rtc module --- demo/rtc/rtc_interrupt.c | 93 ++++++++++++++++++++++++++++++++++++++++ include/fw_hal.h | 1 + include/fw_rtc.h | 79 ++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 demo/rtc/rtc_interrupt.c create mode 100644 include/fw_rtc.h diff --git a/demo/rtc/rtc_interrupt.c b/demo/rtc/rtc_interrupt.c new file mode 100644 index 0000000..82e7439 --- /dev/null +++ b/demo/rtc/rtc_interrupt.c @@ -0,0 +1,93 @@ +// Copyright 2021 IOsetting +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/*** + * Demo: RTC + * Board: STC8H8K64U + */ + +#include "fw_hal.h" + +uint8_t year, month, day, hour, minute, second; + +INTERRUPT(RTC_Routine, EXTI_VectRTC) +{ + SFRX_ON(); + if (RTC_IsSecondInterrupt()) + { + RTC_ClearSecondInterrupt(); + year = YEAR; + month = MONTH; + day = DAY; + hour = HOUR; + minute = MIN; + second = SEC; + } + SFRX_OFF(); +} + +void RTC_Init(void) +{ + // Enable internal 32kHz OSC + SYS_EnableOscillatorLSI(); + // Set RTC clock source + RTC_SetClockSource(RTC_ClockSource_Internal); + // Set clock initial value + RTC_ConfigClock(22, 1, 9, 18, 59, 03, 0); + // Init clock + RTC_ConfigClockApply(); + // Enable second interrupt + EXTI_RTC_SetSecondIntState(HAL_State_ON); + // Enable global interrupt + EXTI_Global_SetIntState(HAL_State_ON); + // Clear interrupts + RTC_ClearAllInterrupts(); + // Enable RTC + RTC_SetRunState(HAL_State_ON); +} + +int main(void) +{ + SYS_SetClock(); + UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200); + RTC_Init(); + + while(1) + { + SFRX_ON(); + UART1_TxString("RTCCR:"); + UART1_TxHex(RTCCR); + UART1_TxString(" RTCCFG:"); + UART1_TxHex(RTCCFG); + UART1_TxString(" RTCIEN:"); + UART1_TxHex(RTCIEN); + UART1_TxString(" RTCIF:"); + UART1_TxHex(RTCIF); + SFRX_OFF(); + UART1_TxString(" Year:"); + UART1_TxHex(year); + UART1_TxString(" Month:"); + UART1_TxHex(month); + UART1_TxString(" Day:"); + UART1_TxHex(day); + UART1_TxString(" Hour:"); + UART1_TxHex(hour); + UART1_TxString(" Minute:"); + UART1_TxHex(minute); + UART1_TxString(" Second:"); + UART1_TxHex(second); + UART1_TxString("\r\n"); + SYS_Delay(900); + } +} diff --git a/include/fw_hal.h b/include/fw_hal.h index 36144fe..4792128 100644 --- a/include/fw_hal.h +++ b/include/fw_hal.h @@ -33,6 +33,7 @@ #endif #if (__CONF_MCU_TYPE == 3 ) #include "fw_pwm.h" +#include "fw_rtc.h" #endif #endif diff --git a/include/fw_rtc.h b/include/fw_rtc.h new file mode 100644 index 0000000..166f4ee --- /dev/null +++ b/include/fw_rtc.h @@ -0,0 +1,79 @@ +// Copyright 2021 IOsetting +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef ___FW_RTC_H___ +#define ___FW_RTC_H___ + +#include "fw_conf.h" +#include "fw_types.h" + +typedef enum +{ + RTC_ClockSource_External = 0x00, + RTC_ClockSource_Internal = 0x01, +} RTC_ClockSource_t; + +#define RTC_SetRunState(__STATE__) SFRX_ASSIGN(RTCCR, 0, (__STATE__)) +#define RTC_SetClockSource(__SOURCE__) SFRX_ASSIGN(RTCCFG, 1, (__SOURCE__)) +#define RTC_ConfigClockApply() SFRX_SET(RTCCFG, 0) + +/** + * Call SFRX_ON(); before calling following macros +*/ +#define RTC_ClearAllInterrupts() (RTCIF = 0x00) +#define RTC_IsAlarmInterrupt() (RTCIF & 0x80) +#define RTC_ClearAlarmInterrupt() (RTCIF &= ~0x80) +#define RTC_IsDayInterrupt() (RTCIF & 0x40) +#define RTC_ClearDayInterrupt() (RTCIF &= ~0x40) +#define RTC_IsHourInterrupt() (RTCIF & 0x20) +#define RTC_ClearHourInterrupt() (RTCIF &= ~0x20) +#define RTC_IsMinuteInterrupt() (RTCIF & 0x10) +#define RTC_ClearMinuteInterrupt() (RTCIF &= ~0x10) +#define RTC_IsSecondInterrupt() (RTCIF & 0x08) +#define RTC_ClearSecondInterrupt() (RTCIF &= ~0x08) +#define RTC_IsSecondDiv2Interrupt() (RTCIF & 0x04) +#define RTC_ClearSecondDiv2Interrupt() (RTCIF &= ~0x04) +#define RTC_IsSecondDiv8Interrupt() (RTCIF & 0x02) +#define RTC_ClearSecondDiv8Interrupt() (RTCIF &= ~0x02) +#define RTC_IsSecondDiv32Interrupt() (RTCIF & 0x01) +#define RTC_ClearSecondDiv32Interrupt() (RTCIF &= ~0x01) + +/** + * Set Alarm +*/ +#define RTC_ConfigAlarm(__HOUR__, __MIN__, __SEC__, __SSEC__) do { \ + SFRX_ON(); \ + ALAHOUR = (__HOUR__ & 0x1F); \ + ALAMIN = (__MIN__ & 0x3F); \ + ALASEC = (__SEC__ & 0x3F); \ + ALASSEC = (__SSEC__ & 0x7F); \ + SFRX_OFF(); \ + } while(0) +/** + * Set clock initial value +*/ +#define RTC_ConfigClock(__YEAR__, __MON__, __DAY__, __HOUR__, __MIN__, __SEC__, __SSEC__) do { \ + SFRX_ON(); \ + INIYEAR = (__YEAR__ & 0x7F); \ + INIMONTH = (__MON__ & 0x0F); \ + INIDAY = (__DAY__ & 0x1F); \ + INIHOUR = (__HOUR__ & 0x1F); \ + INIMIN = (__MIN__ & 0x3F); \ + INISEC = (__SEC__ & 0x3F); \ + INISSEC = (__SSEC__ & 0x7F); \ + SFRX_OFF(); \ + } while(0) + + +#endif \ No newline at end of file