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