HAL Library 24- RTC for STM32Fxxx
RTC library was ported to HAL drivers for STM32Fxxx line from my previously library RTC for STM32F4xx. If features similar things as before except some functions were changed, which are already described in HAL API.
Library
Features
- Support Internal or external clock source
- PC14 and PC15 pins are used for external crystal oscillator
- STM32F4/429 Discovery does not have RTC crystal onboard. Check board’s manual on how to set it up
- Support wakeup interrupt
- Support to set 2 internal alarms to trigger interrupts
- they can also wake up STM32Fxxx from any low power mode
- Get seconds from 01.01.1970 00:00:00
- Get readable time from seconds from 01.01.1970 00:00:00
- Support to save/get data in binary or BCD format
- Support for read/write data to/from RTC backup registers
- Support for subsecond
- Support to write data in string format
- Date and time are checked before saved for valid input data
- Get days in month and year
Dependencies
- HAL
- TM
- STM32Fxxx HAL
- defines.h configuration file
- attributes.h
Functions and enumerations
Full functions list can be found on HAL API section.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
/** * Keil project example for RTC peripheral * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_rtc.h" /* RTC structure */ TM_RTC_t RTCD; /* RTC Alarm structure */ TM_RTC_AlarmTime_t RTCA; /* String variable for LCD */ char str[100]; /* Current seconds value */ uint8_t sec = 0; int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init button */ TM_DISCO_ButtonInit(); /* Init RTC */ if (TM_RTC_Init(TM_RTC_ClockSource_External)) { /* RTC was already initialized and time is running */ /* No need to set clock now */ } else { /* RTC was now initialized */ /* If you need to set new time, now is the time to do it */ } /* Wakeup IRQ every 1 second */ TM_RTC_Interrupts(TM_RTC_Int_1s); while (1) { /* Read RTC */ TM_RTC_GetDateTime(&RTCD, TM_RTC_Format_BIN); /* Check difference */ if (RTCD.Seconds != sec) { sec = RTCD.Seconds; /* Format date */ sprintf(str, "Date: %02d.%02d.%04d", RTCD.Day, RTCD.Month, RTCD.Year + 2000); /* Format time */ sprintf(str, "Time: %02d.%02d.%02d", RTCD.Hours, RTCD.Minutes, RTCD.Seconds); } /* Check if button pressed */ if (TM_DISCO_ButtonPressed()) { /* Set new date and time */ RTCD.Day = 4; RTCD.Month = 5; RTCD.Year = 15; RTCD.WeekDay = 5; RTCD.Hours = 23; RTCD.Minutes = 59; RTCD.Seconds = 50; /* Save new date and time */ TM_RTC_SetDateTime(&RTCD, TM_RTC_Format_BIN); /* Trigger alarm now after new time was set */ RTCA.Type = TM_RTC_AlarmType_DayInWeek; /*!< Alarm type is day in a week */ RTCA.Day = 5; /*!< Day 5 in week = Friday */ RTCA.Hours = RTCD.Hours; /*!< Select time for alarm */ RTCA.Minutes = RTCD.Minutes; RTCA.Seconds = RTCD.Seconds + 5; /* Enable alarm A */ TM_RTC_EnableAlarm(TM_RTC_Alarm_A, &RTCA, TM_RTC_Format_BIN); } } } /* Called when wakeup interrupt happens */ void TM_RTC_WakeupHandler(void) { /* Toggle leds */ TM_DISCO_LedToggle(LED_ALL); } /* Handle Alarm A event */ void TM_RTC_AlarmAHandler(void) { /* Alarm A triggered */ /* Do you stuff */ /* Disable alarm */ TM_RTC_DisableAlarm(TM_RTC_Alarm_A); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
/** * Keil project example for RTC peripheral with LCD * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_rtc.h" #include "tm_stm32_lcd.h" /* RTC structure */ TM_RTC_t RTCD; /* RTC Alarm structure */ TM_RTC_AlarmTime_t RTCA; /* String variable for LCD */ char str[100]; /* Current seconds value */ uint8_t sec = 0; int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init button */ TM_DISCO_ButtonInit(); /* Init LCD */ TM_LCD_Init(); #if defined(STM32F429_DISCOVERY) /* Rotate LCD */ TM_LCD_SetOrientation(2); #endif /* Set write location */ TM_LCD_SetXY(10, 10); /* Init RTC */ if (TM_RTC_Init(TM_RTC_ClockSource_External)) { /* RTC was already initialized and time is running */ TM_LCD_Puts("RTC already initialized!"); } else { /* RTC was now initialized */ /* If you need to set new time, now is the time to do it */ TM_LCD_Puts("RTC initialized first time!"); } /* Wakeup IRQ every 1 second */ TM_RTC_Interrupts(TM_RTC_Int_1s); while (1) { /* Read RTC */ TM_RTC_GetDateTime(&RTCD, TM_RTC_Format_BIN); /* Check difference */ if (RTCD.Seconds != sec) { sec = RTCD.Seconds; /* Format date */ sprintf(str, "Date: %02d.%02d.%04d", RTCD.Day, RTCD.Month, RTCD.Year + 2000); /* Set LCD location */ TM_LCD_SetXY(10, 50); /* Print time to LCD */ TM_LCD_Puts(str); /* Format time */ sprintf(str, "Time: %02d.%02d.%02d", RTCD.Hours, RTCD.Minutes, RTCD.Seconds); /* Set LCD location */ TM_LCD_SetXY(10, 70); /* Print time to LCD */ TM_LCD_Puts(str); } /* Check if button pressed */ if (TM_DISCO_ButtonPressed()) { /* Set new date and time */ RTCD.Day = 4; RTCD.Month = 5; RTCD.Year = 15; RTCD.WeekDay = 5; RTCD.Hours = 23; RTCD.Minutes = 59; RTCD.Seconds = 50; /* Save new date and time */ TM_RTC_SetDateTime(&RTCD, TM_RTC_Format_BIN); /* Trigger alarm now after new time was set */ RTCA.Type = TM_RTC_AlarmType_DayInWeek; RTCA.Hours = RTCD.Hours; RTCA.Minutes = RTCD.Minutes; RTCA.Seconds = RTCD.Seconds + 5; RTCA.Day = RTCD.Day; /* Enable alarm */ TM_RTC_EnableAlarm(TM_RTC_Alarm_A, &RTCA, TM_RTC_Format_BIN); } } } /* Called when wakeup interrupt happens */ void TM_RTC_WakeupHandler(void) { /* Toggle leds */ TM_DISCO_LedToggle(LED_ALL); } /* Handle Alarm A event */ void TM_RTC_AlarmAHandler(void) { /* Print to LCD */ TM_LCD_SetXY(10, 90); TM_LCD_Puts("ALARM TRIGGERED!"); /* Disable alarm */ TM_RTC_DisableAlarm(TM_RTC_Alarm_A); } |
Projects are available on Github, download all libraries below.
STM32 libraries based on STM32Fxxx HAL drivers.
Recent comments