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