This is a RTC additional function. This is only for Nucleo F401RE & F411RE mbed(Added L152RE, F334R8, L476RG & F746xx). If you connected battery backup circuit for internal RTC, you can make a power-off and reset condition. RTC still has proper time and date.
Dependents: Nucleo_rtos_sample PB_Emma_Ethernet
Please refer following NOTE information.
/users/kenjiArai/notebook/nucleo-series-rtc-control-under-power-onoff-and-re/
rtc_api_F334.c@13:44e5327acb05, 2016-05-27 (annotated)
- Committer:
- kenjiArai
- Date:
- Fri May 27 20:16:09 2016 +0000
- Revision:
- 13:44e5327acb05
- Parent:
- 9:1af4e107ca7b
Added L476RG mbed.Changed SetRTC and rtc_api.c for F401RE & F411RE, F334R8 based on latest mbed-dev revision (141).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 13:44e5327acb05 | 1 | #if 0 |
kenjiArai | 13:44e5327acb05 | 2 | /* mbed Microcontroller Library |
kenjiArai | 13:44e5327acb05 | 3 | ******************************************************************************* |
kenjiArai | 13:44e5327acb05 | 4 | * Copyright (c) 2014, STMicroelectronics |
kenjiArai | 13:44e5327acb05 | 5 | * All rights reserved. |
kenjiArai | 13:44e5327acb05 | 6 | * |
kenjiArai | 13:44e5327acb05 | 7 | * Redistribution and use in source and binary forms, with or without |
kenjiArai | 13:44e5327acb05 | 8 | * modification, are permitted provided that the following conditions are met: |
kenjiArai | 13:44e5327acb05 | 9 | * |
kenjiArai | 13:44e5327acb05 | 10 | * 1. Redistributions of source code must retain the above copyright notice, |
kenjiArai | 13:44e5327acb05 | 11 | * this list of conditions and the following disclaimer. |
kenjiArai | 13:44e5327acb05 | 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
kenjiArai | 13:44e5327acb05 | 13 | * this list of conditions and the following disclaimer in the documentation |
kenjiArai | 13:44e5327acb05 | 14 | * and/or other materials provided with the distribution. |
kenjiArai | 13:44e5327acb05 | 15 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
kenjiArai | 13:44e5327acb05 | 16 | * may be used to endorse or promote products derived from this software |
kenjiArai | 13:44e5327acb05 | 17 | * without specific prior written permission. |
kenjiArai | 13:44e5327acb05 | 18 | * |
kenjiArai | 13:44e5327acb05 | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
kenjiArai | 13:44e5327acb05 | 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
kenjiArai | 13:44e5327acb05 | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
kenjiArai | 13:44e5327acb05 | 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
kenjiArai | 13:44e5327acb05 | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
kenjiArai | 13:44e5327acb05 | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
kenjiArai | 13:44e5327acb05 | 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
kenjiArai | 13:44e5327acb05 | 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
kenjiArai | 13:44e5327acb05 | 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
kenjiArai | 13:44e5327acb05 | 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kenjiArai | 13:44e5327acb05 | 29 | ******************************************************************************* |
kenjiArai | 13:44e5327acb05 | 30 | */ |
kenjiArai | 13:44e5327acb05 | 31 | #include "rtc_api.h" |
kenjiArai | 13:44e5327acb05 | 32 | |
kenjiArai | 13:44e5327acb05 | 33 | #if DEVICE_RTC |
kenjiArai | 13:44e5327acb05 | 34 | |
kenjiArai | 13:44e5327acb05 | 35 | #include "mbed_error.h" |
kenjiArai | 13:44e5327acb05 | 36 | |
kenjiArai | 13:44e5327acb05 | 37 | #if DEVICE_RTC_LSI |
kenjiArai | 13:44e5327acb05 | 38 | static int rtc_inited = 0; |
kenjiArai | 13:44e5327acb05 | 39 | #endif |
kenjiArai | 13:44e5327acb05 | 40 | |
kenjiArai | 13:44e5327acb05 | 41 | static RTC_HandleTypeDef RtcHandle; |
kenjiArai | 13:44e5327acb05 | 42 | |
kenjiArai | 13:44e5327acb05 | 43 | //#define ORIGINAL |
kenjiArai | 13:44e5327acb05 | 44 | #if defined(ORIGINAL) |
kenjiArai | 13:44e5327acb05 | 45 | #warning "You cannot use SetRTC functions" |
kenjiArai | 13:44e5327acb05 | 46 | #else |
kenjiArai | 13:44e5327acb05 | 47 | #warning "You modified mbed-src rtc_api.c for SetRTC functions" |
kenjiArai | 13:44e5327acb05 | 48 | #endif |
kenjiArai | 13:44e5327acb05 | 49 | |
kenjiArai | 13:44e5327acb05 | 50 | #if defined(ORIGINAL) // modify part1 of 4 (Start) |
kenjiArai | 13:44e5327acb05 | 51 | #else |
kenjiArai | 13:44e5327acb05 | 52 | #define RTC_DAT0 0x55aa55aa |
kenjiArai | 13:44e5327acb05 | 53 | #define RTC_DAT1 0xaa55aa55 |
kenjiArai | 13:44e5327acb05 | 54 | |
kenjiArai | 13:44e5327acb05 | 55 | uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR) |
kenjiArai | 13:44e5327acb05 | 56 | { |
kenjiArai | 13:44e5327acb05 | 57 | __IO uint32_t tmp = 0; |
kenjiArai | 13:44e5327acb05 | 58 | |
kenjiArai | 13:44e5327acb05 | 59 | // Check the parameters |
kenjiArai | 13:44e5327acb05 | 60 | assert_param(IS_RTC_BKP(RTC_BKP_DR)); |
kenjiArai | 13:44e5327acb05 | 61 | tmp = RTC_BASE + 0x50; |
kenjiArai | 13:44e5327acb05 | 62 | tmp += (RTC_BKP_DR * 4); |
kenjiArai | 13:44e5327acb05 | 63 | // Read the specified register |
kenjiArai | 13:44e5327acb05 | 64 | return (*(__IO uint32_t *)tmp); |
kenjiArai | 13:44e5327acb05 | 65 | } |
kenjiArai | 13:44e5327acb05 | 66 | |
kenjiArai | 13:44e5327acb05 | 67 | /* Check RTC Backup registers contents */ |
kenjiArai | 13:44e5327acb05 | 68 | uint32_t Check_Rtc_backup_reg( void ) |
kenjiArai | 13:44e5327acb05 | 69 | { |
kenjiArai | 13:44e5327acb05 | 70 | if ( RTC_ReadBackupRegister( RTC_BKP_DR0 ) == RTC_DAT0 ) { |
kenjiArai | 13:44e5327acb05 | 71 | if ( RTC_ReadBackupRegister( RTC_BKP_DR1 ) == RTC_DAT1 ) { |
kenjiArai | 13:44e5327acb05 | 72 | return 1; |
kenjiArai | 13:44e5327acb05 | 73 | } |
kenjiArai | 13:44e5327acb05 | 74 | } |
kenjiArai | 13:44e5327acb05 | 75 | return 0; |
kenjiArai | 13:44e5327acb05 | 76 | } |
kenjiArai | 13:44e5327acb05 | 77 | |
kenjiArai | 13:44e5327acb05 | 78 | void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data) |
kenjiArai | 13:44e5327acb05 | 79 | { |
kenjiArai | 13:44e5327acb05 | 80 | __IO uint32_t tmp = 0; |
kenjiArai | 13:44e5327acb05 | 81 | |
kenjiArai | 13:44e5327acb05 | 82 | // Check the parameters/ |
kenjiArai | 13:44e5327acb05 | 83 | assert_param(IS_RTC_BKP(RTC_BKP_DR)); |
kenjiArai | 13:44e5327acb05 | 84 | tmp = RTC_BASE + 0x50; |
kenjiArai | 13:44e5327acb05 | 85 | tmp += (RTC_BKP_DR * 4); |
kenjiArai | 13:44e5327acb05 | 86 | // Write the specified register/ |
kenjiArai | 13:44e5327acb05 | 87 | *(__IO uint32_t *)tmp = (uint32_t)Data; |
kenjiArai | 13:44e5327acb05 | 88 | } |
kenjiArai | 13:44e5327acb05 | 89 | #endif // modify part1 of 4 (End) |
kenjiArai | 13:44e5327acb05 | 90 | |
kenjiArai | 13:44e5327acb05 | 91 | void rtc_init(void) |
kenjiArai | 13:44e5327acb05 | 92 | { |
kenjiArai | 13:44e5327acb05 | 93 | RCC_OscInitTypeDef RCC_OscInitStruct; |
kenjiArai | 13:44e5327acb05 | 94 | uint32_t rtc_freq = 0; |
kenjiArai | 13:44e5327acb05 | 95 | |
kenjiArai | 13:44e5327acb05 | 96 | #if DEVICE_RTC_LSI |
kenjiArai | 13:44e5327acb05 | 97 | rtc_inited = 1; |
kenjiArai | 13:44e5327acb05 | 98 | #endif |
kenjiArai | 13:44e5327acb05 | 99 | |
kenjiArai | 13:44e5327acb05 | 100 | RtcHandle.Instance = RTC; |
kenjiArai | 13:44e5327acb05 | 101 | |
kenjiArai | 13:44e5327acb05 | 102 | #if defined(ORIGINAL) // modify part2 of 4 (Start) |
kenjiArai | 13:44e5327acb05 | 103 | #else |
kenjiArai | 13:44e5327acb05 | 104 | RtcHandle.Instance = RTC; |
kenjiArai | 13:44e5327acb05 | 105 | |
kenjiArai | 13:44e5327acb05 | 106 | // Enable Power clock |
kenjiArai | 13:44e5327acb05 | 107 | __PWR_CLK_ENABLE(); |
kenjiArai | 13:44e5327acb05 | 108 | |
kenjiArai | 13:44e5327acb05 | 109 | // Enable access to Backup domain |
kenjiArai | 13:44e5327acb05 | 110 | HAL_PWR_EnableBkUpAccess(); |
kenjiArai | 13:44e5327acb05 | 111 | |
kenjiArai | 13:44e5327acb05 | 112 | // Check backup condition |
kenjiArai | 13:44e5327acb05 | 113 | if ( Check_Rtc_backup_reg() ) { |
kenjiArai | 13:44e5327acb05 | 114 | return; |
kenjiArai | 13:44e5327acb05 | 115 | } |
kenjiArai | 13:44e5327acb05 | 116 | #endif // modify part2 of 4 (End) |
kenjiArai | 13:44e5327acb05 | 117 | #if !DEVICE_RTC_LSI |
kenjiArai | 13:44e5327acb05 | 118 | // Enable LSE Oscillator |
kenjiArai | 13:44e5327acb05 | 119 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
kenjiArai | 13:44e5327acb05 | 120 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; /* Mandatory, otherwise the PLL is reconfigured! */ |
kenjiArai | 13:44e5327acb05 | 121 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; /* External 32.768 kHz clock on OSC_IN/OSC_OUT */ |
kenjiArai | 13:44e5327acb05 | 122 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { |
kenjiArai | 13:44e5327acb05 | 123 | // Connect LSE to RTC |
kenjiArai | 13:44e5327acb05 | 124 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); |
kenjiArai | 13:44e5327acb05 | 125 | rtc_freq = LSE_VALUE; |
kenjiArai | 13:44e5327acb05 | 126 | } |
kenjiArai | 13:44e5327acb05 | 127 | else { |
kenjiArai | 13:44e5327acb05 | 128 | error("RTC error: LSE clock initialization failed."); |
kenjiArai | 13:44e5327acb05 | 129 | } |
kenjiArai | 13:44e5327acb05 | 130 | #else |
kenjiArai | 13:44e5327acb05 | 131 | // Enable Power clock |
kenjiArai | 13:44e5327acb05 | 132 | __PWR_CLK_ENABLE(); |
kenjiArai | 13:44e5327acb05 | 133 | |
kenjiArai | 13:44e5327acb05 | 134 | // Enable access to Backup domain |
kenjiArai | 13:44e5327acb05 | 135 | HAL_PWR_EnableBkUpAccess(); |
kenjiArai | 13:44e5327acb05 | 136 | |
kenjiArai | 13:44e5327acb05 | 137 | // Reset Backup domain |
kenjiArai | 13:44e5327acb05 | 138 | __HAL_RCC_BACKUPRESET_FORCE(); |
kenjiArai | 13:44e5327acb05 | 139 | __HAL_RCC_BACKUPRESET_RELEASE(); |
kenjiArai | 13:44e5327acb05 | 140 | |
kenjiArai | 13:44e5327acb05 | 141 | // Enable LSI clock |
kenjiArai | 13:44e5327acb05 | 142 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
kenjiArai | 13:44e5327acb05 | 143 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
kenjiArai | 13:44e5327acb05 | 144 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
kenjiArai | 13:44e5327acb05 | 145 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
kenjiArai | 13:44e5327acb05 | 146 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
kenjiArai | 13:44e5327acb05 | 147 | error("RTC error: LSI clock initialization failed."); |
kenjiArai | 13:44e5327acb05 | 148 | } |
kenjiArai | 13:44e5327acb05 | 149 | // Connect LSI to RTC |
kenjiArai | 13:44e5327acb05 | 150 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
kenjiArai | 13:44e5327acb05 | 151 | // Note: The LSI clock can be measured precisely using a timer input capture. |
kenjiArai | 13:44e5327acb05 | 152 | rtc_freq = LSI_VALUE; |
kenjiArai | 13:44e5327acb05 | 153 | #endif |
kenjiArai | 13:44e5327acb05 | 154 | |
kenjiArai | 13:44e5327acb05 | 155 | // Enable RTC |
kenjiArai | 13:44e5327acb05 | 156 | __HAL_RCC_RTC_ENABLE(); |
kenjiArai | 13:44e5327acb05 | 157 | |
kenjiArai | 13:44e5327acb05 | 158 | RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
kenjiArai | 13:44e5327acb05 | 159 | RtcHandle.Init.AsynchPrediv = 127; |
kenjiArai | 13:44e5327acb05 | 160 | RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1; |
kenjiArai | 13:44e5327acb05 | 161 | RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
kenjiArai | 13:44e5327acb05 | 162 | RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
kenjiArai | 13:44e5327acb05 | 163 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
kenjiArai | 13:44e5327acb05 | 164 | |
kenjiArai | 13:44e5327acb05 | 165 | #if defined(ORIGINAL) // modify part3 of 4 (Start) |
kenjiArai | 13:44e5327acb05 | 166 | #else |
kenjiArai | 13:44e5327acb05 | 167 | /* Write backup data into the backup RAM */ |
kenjiArai | 13:44e5327acb05 | 168 | RTC_WriteBackupRegister( RTC_BKP_DR0, RTC_DAT0 ); |
kenjiArai | 13:44e5327acb05 | 169 | RTC_WriteBackupRegister( RTC_BKP_DR1, RTC_DAT1 ); |
kenjiArai | 13:44e5327acb05 | 170 | #endif // modify part3 of 4 (End) |
kenjiArai | 13:44e5327acb05 | 171 | |
kenjiArai | 13:44e5327acb05 | 172 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
kenjiArai | 13:44e5327acb05 | 173 | error("RTC error: RTC initialization failed."); |
kenjiArai | 13:44e5327acb05 | 174 | } |
kenjiArai | 13:44e5327acb05 | 175 | } |
kenjiArai | 13:44e5327acb05 | 176 | |
kenjiArai | 13:44e5327acb05 | 177 | void rtc_free(void) |
kenjiArai | 13:44e5327acb05 | 178 | { |
kenjiArai | 13:44e5327acb05 | 179 | #if DEVICE_RTC_LSI |
kenjiArai | 13:44e5327acb05 | 180 | // Enable Power clock |
kenjiArai | 13:44e5327acb05 | 181 | __PWR_CLK_ENABLE(); |
kenjiArai | 13:44e5327acb05 | 182 | |
kenjiArai | 13:44e5327acb05 | 183 | // Enable access to Backup domain |
kenjiArai | 13:44e5327acb05 | 184 | HAL_PWR_EnableBkUpAccess(); |
kenjiArai | 13:44e5327acb05 | 185 | |
kenjiArai | 13:44e5327acb05 | 186 | // Reset Backup domain |
kenjiArai | 13:44e5327acb05 | 187 | __HAL_RCC_BACKUPRESET_FORCE(); |
kenjiArai | 13:44e5327acb05 | 188 | __HAL_RCC_BACKUPRESET_RELEASE(); |
kenjiArai | 13:44e5327acb05 | 189 | |
kenjiArai | 13:44e5327acb05 | 190 | // Disable access to Backup domain |
kenjiArai | 13:44e5327acb05 | 191 | HAL_PWR_DisableBkUpAccess(); |
kenjiArai | 13:44e5327acb05 | 192 | #endif |
kenjiArai | 13:44e5327acb05 | 193 | |
kenjiArai | 13:44e5327acb05 | 194 | // Disable LSI and LSE clocks |
kenjiArai | 13:44e5327acb05 | 195 | RCC_OscInitTypeDef RCC_OscInitStruct; |
kenjiArai | 13:44e5327acb05 | 196 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
kenjiArai | 13:44e5327acb05 | 197 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
kenjiArai | 13:44e5327acb05 | 198 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
kenjiArai | 13:44e5327acb05 | 199 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
kenjiArai | 13:44e5327acb05 | 200 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
kenjiArai | 13:44e5327acb05 | 201 | |
kenjiArai | 13:44e5327acb05 | 202 | #if DEVICE_RTC_LSI |
kenjiArai | 13:44e5327acb05 | 203 | rtc_inited = 0; |
kenjiArai | 13:44e5327acb05 | 204 | #endif |
kenjiArai | 13:44e5327acb05 | 205 | } |
kenjiArai | 13:44e5327acb05 | 206 | |
kenjiArai | 13:44e5327acb05 | 207 | int rtc_isenabled(void) |
kenjiArai | 13:44e5327acb05 | 208 | { |
kenjiArai | 13:44e5327acb05 | 209 | #if DEVICE_RTC_LSI |
kenjiArai | 13:44e5327acb05 | 210 | return rtc_inited; |
kenjiArai | 13:44e5327acb05 | 211 | #else |
kenjiArai | 13:44e5327acb05 | 212 | #if defined(ORIGINAL) // modify part4 of 4 (Start) |
kenjiArai | 13:44e5327acb05 | 213 | if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return 1; |
kenjiArai | 13:44e5327acb05 | 214 | else return 0; |
kenjiArai | 13:44e5327acb05 | 215 | #else |
kenjiArai | 13:44e5327acb05 | 216 | return Check_Rtc_backup_reg(); |
kenjiArai | 13:44e5327acb05 | 217 | #endif // modify part4 of 4 (End) |
kenjiArai | 13:44e5327acb05 | 218 | #endif |
kenjiArai | 13:44e5327acb05 | 219 | } |
kenjiArai | 13:44e5327acb05 | 220 | |
kenjiArai | 13:44e5327acb05 | 221 | /* |
kenjiArai | 13:44e5327acb05 | 222 | RTC Registers |
kenjiArai | 13:44e5327acb05 | 223 | RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
kenjiArai | 13:44e5327acb05 | 224 | RTC_Month 1=january, 2=february, ..., 12=december |
kenjiArai | 13:44e5327acb05 | 225 | RTC_Date day of the month 1-31 |
kenjiArai | 13:44e5327acb05 | 226 | RTC_Year year 0-99 |
kenjiArai | 13:44e5327acb05 | 227 | struct tm |
kenjiArai | 13:44e5327acb05 | 228 | tm_sec seconds after the minute 0-61 |
kenjiArai | 13:44e5327acb05 | 229 | tm_min minutes after the hour 0-59 |
kenjiArai | 13:44e5327acb05 | 230 | tm_hour hours since midnight 0-23 |
kenjiArai | 13:44e5327acb05 | 231 | tm_mday day of the month 1-31 |
kenjiArai | 13:44e5327acb05 | 232 | tm_mon months since January 0-11 |
kenjiArai | 13:44e5327acb05 | 233 | tm_year years since 1900 |
kenjiArai | 13:44e5327acb05 | 234 | tm_wday days since Sunday 0-6 |
kenjiArai | 13:44e5327acb05 | 235 | tm_yday days since January 1 0-365 |
kenjiArai | 13:44e5327acb05 | 236 | tm_isdst Daylight Saving Time flag |
kenjiArai | 13:44e5327acb05 | 237 | */ |
kenjiArai | 13:44e5327acb05 | 238 | time_t rtc_read(void) |
kenjiArai | 13:44e5327acb05 | 239 | { |
kenjiArai | 13:44e5327acb05 | 240 | RTC_DateTypeDef dateStruct; |
kenjiArai | 13:44e5327acb05 | 241 | RTC_TimeTypeDef timeStruct; |
kenjiArai | 13:44e5327acb05 | 242 | struct tm timeinfo; |
kenjiArai | 13:44e5327acb05 | 243 | |
kenjiArai | 13:44e5327acb05 | 244 | RtcHandle.Instance = RTC; |
kenjiArai | 13:44e5327acb05 | 245 | |
kenjiArai | 13:44e5327acb05 | 246 | // Read actual date and time |
kenjiArai | 13:44e5327acb05 | 247 | // Warning: the time must be read first! |
kenjiArai | 13:44e5327acb05 | 248 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); |
kenjiArai | 13:44e5327acb05 | 249 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); |
kenjiArai | 13:44e5327acb05 | 250 | |
kenjiArai | 13:44e5327acb05 | 251 | // Setup a tm structure based on the RTC |
kenjiArai | 13:44e5327acb05 | 252 | timeinfo.tm_wday = dateStruct.WeekDay; |
kenjiArai | 13:44e5327acb05 | 253 | timeinfo.tm_mon = dateStruct.Month - 1; |
kenjiArai | 13:44e5327acb05 | 254 | timeinfo.tm_mday = dateStruct.Date; |
kenjiArai | 13:44e5327acb05 | 255 | timeinfo.tm_year = dateStruct.Year + 100; |
kenjiArai | 13:44e5327acb05 | 256 | timeinfo.tm_hour = timeStruct.Hours; |
kenjiArai | 13:44e5327acb05 | 257 | timeinfo.tm_min = timeStruct.Minutes; |
kenjiArai | 13:44e5327acb05 | 258 | timeinfo.tm_sec = timeStruct.Seconds; |
kenjiArai | 13:44e5327acb05 | 259 | |
kenjiArai | 13:44e5327acb05 | 260 | // Convert to timestamp |
kenjiArai | 13:44e5327acb05 | 261 | time_t t = mktime(&timeinfo); |
kenjiArai | 13:44e5327acb05 | 262 | |
kenjiArai | 13:44e5327acb05 | 263 | return t; |
kenjiArai | 13:44e5327acb05 | 264 | } |
kenjiArai | 13:44e5327acb05 | 265 | |
kenjiArai | 13:44e5327acb05 | 266 | void rtc_write(time_t t) |
kenjiArai | 13:44e5327acb05 | 267 | { |
kenjiArai | 13:44e5327acb05 | 268 | RTC_DateTypeDef dateStruct; |
kenjiArai | 13:44e5327acb05 | 269 | RTC_TimeTypeDef timeStruct; |
kenjiArai | 13:44e5327acb05 | 270 | |
kenjiArai | 13:44e5327acb05 | 271 | RtcHandle.Instance = RTC; |
kenjiArai | 13:44e5327acb05 | 272 | |
kenjiArai | 13:44e5327acb05 | 273 | // Convert the time into a tm |
kenjiArai | 13:44e5327acb05 | 274 | struct tm *timeinfo = localtime(&t); |
kenjiArai | 13:44e5327acb05 | 275 | |
kenjiArai | 13:44e5327acb05 | 276 | // Fill RTC structures |
kenjiArai | 13:44e5327acb05 | 277 | dateStruct.WeekDay = timeinfo->tm_wday; |
kenjiArai | 13:44e5327acb05 | 278 | dateStruct.Month = timeinfo->tm_mon + 1; |
kenjiArai | 13:44e5327acb05 | 279 | dateStruct.Date = timeinfo->tm_mday; |
kenjiArai | 13:44e5327acb05 | 280 | dateStruct.Year = timeinfo->tm_year - 100; |
kenjiArai | 13:44e5327acb05 | 281 | timeStruct.Hours = timeinfo->tm_hour; |
kenjiArai | 13:44e5327acb05 | 282 | timeStruct.Minutes = timeinfo->tm_min; |
kenjiArai | 13:44e5327acb05 | 283 | timeStruct.Seconds = timeinfo->tm_sec; |
kenjiArai | 13:44e5327acb05 | 284 | timeStruct.TimeFormat = RTC_HOURFORMAT12_PM; |
kenjiArai | 13:44e5327acb05 | 285 | timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
kenjiArai | 13:44e5327acb05 | 286 | timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; |
kenjiArai | 13:44e5327acb05 | 287 | |
kenjiArai | 13:44e5327acb05 | 288 | // Change the RTC current date/time |
kenjiArai | 13:44e5327acb05 | 289 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); |
kenjiArai | 13:44e5327acb05 | 290 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); |
kenjiArai | 13:44e5327acb05 | 291 | } |
kenjiArai | 13:44e5327acb05 | 292 | |
kenjiArai | 13:44e5327acb05 | 293 | #endif |
kenjiArai | 13:44e5327acb05 | 294 | #endif |