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