Modification of Mbed-dev library for LQFP48 package microcontrollers: STM32F103C8 (STM32F103C8T6) and STM32F103CB (STM32F103CBT6) (Bluepill boards, Maple mini etc. )
Fork of mbed-STM32F103C8_org by
Library for STM32F103C8 (Bluepill boards etc.).
Use this instead of mbed library.
This library allows the size of the code in the FLASH up to 128kB. Therefore, code also runs on microcontrollers STM32F103CB (eg. Maple mini).
But in the case of STM32F103C8, check the size of the resulting code would not exceed 64kB.
To compile a program with this library, use NUCLEO-F103RB as the target name. !
Changes:
- Corrected initialization of the HSE + crystal clock (mbed permanent bug), allowing the use of on-board xtal (8MHz).(1)
- Additionally, it also set USB clock (48Mhz).(2)
- Definitions of pins and peripherals adjusted to LQFP48 case.
- Board led LED1 is now PC_13 (3)
- USER_BUTTON is now PC_14 (4)
Now the library is complete rebuilt based on mbed-dev v160 (and not yet fully tested).
notes
(1) - In case 8MHz xtal on board, CPU frequency is 72MHz. Without xtal is 64MHz.
(2) - Using the USB interface is only possible if STM32 is clocking by on-board 8MHz xtal or external clock signal 8MHz on the OSC_IN pin.
(3) - On Bluepill board led operation is reversed, i.e. 0 - led on, 1 - led off.
(4) - Bluepill board has no real user button
Information
After export to SW4STM (AC6):
- add line
#include "mbed_config.h"
in files Serial.h and RawSerial.h - in project properties change
Optimisation Level
toOptimise for size (-Os)
targets/TARGET_STM/rtc_api.c@148:8b0b02bf146f, 2017-04-27 (annotated)
- Committer:
- mega64
- Date:
- Thu Apr 27 23:56:38 2017 +0000
- Revision:
- 148:8b0b02bf146f
- Parent:
- 146:03e976389d16
Remove unnecessary folders
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mega64 | 146:03e976389d16 | 1 | /* mbed Microcontroller Library |
mega64 | 146:03e976389d16 | 2 | ******************************************************************************* |
mega64 | 146:03e976389d16 | 3 | * Copyright (c) 2016, STMicroelectronics |
mega64 | 146:03e976389d16 | 4 | * All rights reserved. |
mega64 | 146:03e976389d16 | 5 | * |
mega64 | 146:03e976389d16 | 6 | * Redistribution and use in source and binary forms, with or without |
mega64 | 146:03e976389d16 | 7 | * modification, are permitted provided that the following conditions are met: |
mega64 | 146:03e976389d16 | 8 | * |
mega64 | 146:03e976389d16 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mega64 | 146:03e976389d16 | 10 | * this list of conditions and the following disclaimer. |
mega64 | 146:03e976389d16 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mega64 | 146:03e976389d16 | 12 | * this list of conditions and the following disclaimer in the documentation |
mega64 | 146:03e976389d16 | 13 | * and/or other materials provided with the distribution. |
mega64 | 146:03e976389d16 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mega64 | 146:03e976389d16 | 15 | * may be used to endorse or promote products derived from this software |
mega64 | 146:03e976389d16 | 16 | * without specific prior written permission. |
mega64 | 146:03e976389d16 | 17 | * |
mega64 | 146:03e976389d16 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mega64 | 146:03e976389d16 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mega64 | 146:03e976389d16 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mega64 | 146:03e976389d16 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mega64 | 146:03e976389d16 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mega64 | 146:03e976389d16 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mega64 | 146:03e976389d16 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mega64 | 146:03e976389d16 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mega64 | 146:03e976389d16 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mega64 | 146:03e976389d16 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mega64 | 146:03e976389d16 | 28 | ******************************************************************************* |
mega64 | 146:03e976389d16 | 29 | */ |
mega64 | 146:03e976389d16 | 30 | #if DEVICE_RTC |
mega64 | 146:03e976389d16 | 31 | |
mega64 | 146:03e976389d16 | 32 | #include "rtc_api.h" |
mega64 | 146:03e976389d16 | 33 | #include "rtc_api_hal.h" |
mega64 | 146:03e976389d16 | 34 | #include "mbed_error.h" |
mega64 | 146:03e976389d16 | 35 | |
mega64 | 146:03e976389d16 | 36 | static RTC_HandleTypeDef RtcHandle; |
mega64 | 146:03e976389d16 | 37 | |
mega64 | 146:03e976389d16 | 38 | #if RTC_LSI |
mega64 | 146:03e976389d16 | 39 | #define RTC_CLOCK LSI_VALUE |
mega64 | 146:03e976389d16 | 40 | #else |
mega64 | 146:03e976389d16 | 41 | #define RTC_CLOCK LSE_VALUE |
mega64 | 146:03e976389d16 | 42 | #endif |
mega64 | 146:03e976389d16 | 43 | |
mega64 | 146:03e976389d16 | 44 | #if DEVICE_LOWPOWERTIMER |
mega64 | 146:03e976389d16 | 45 | #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) |
mega64 | 146:03e976389d16 | 46 | #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) |
mega64 | 146:03e976389d16 | 47 | #else |
mega64 | 146:03e976389d16 | 48 | #define RTC_ASYNCH_PREDIV (0x007F) |
mega64 | 146:03e976389d16 | 49 | #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) |
mega64 | 146:03e976389d16 | 50 | #endif |
mega64 | 146:03e976389d16 | 51 | |
mega64 | 146:03e976389d16 | 52 | #if DEVICE_LOWPOWERTIMER |
mega64 | 146:03e976389d16 | 53 | static void (*irq_handler)(void); |
mega64 | 146:03e976389d16 | 54 | static void RTC_IRQHandler(void); |
mega64 | 146:03e976389d16 | 55 | #endif |
mega64 | 146:03e976389d16 | 56 | |
mega64 | 146:03e976389d16 | 57 | void rtc_init(void) |
mega64 | 146:03e976389d16 | 58 | { |
mega64 | 146:03e976389d16 | 59 | RCC_OscInitTypeDef RCC_OscInitStruct; |
mega64 | 146:03e976389d16 | 60 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
mega64 | 146:03e976389d16 | 61 | |
mega64 | 146:03e976389d16 | 62 | // Enable access to Backup domain |
mega64 | 146:03e976389d16 | 63 | HAL_PWR_EnableBkUpAccess(); |
mega64 | 146:03e976389d16 | 64 | |
mega64 | 146:03e976389d16 | 65 | RtcHandle.Instance = RTC; |
mega64 | 146:03e976389d16 | 66 | |
mega64 | 146:03e976389d16 | 67 | #if !RTC_LSI |
mega64 | 146:03e976389d16 | 68 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
mega64 | 146:03e976389d16 | 69 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
mega64 | 146:03e976389d16 | 70 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
mega64 | 146:03e976389d16 | 71 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
mega64 | 146:03e976389d16 | 72 | |
mega64 | 146:03e976389d16 | 73 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { |
mega64 | 146:03e976389d16 | 74 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); |
mega64 | 146:03e976389d16 | 75 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); |
mega64 | 146:03e976389d16 | 76 | } else { |
mega64 | 146:03e976389d16 | 77 | error("Cannot initialize RTC with LSE\n"); |
mega64 | 146:03e976389d16 | 78 | } |
mega64 | 146:03e976389d16 | 79 | |
mega64 | 146:03e976389d16 | 80 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
mega64 | 146:03e976389d16 | 81 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
mega64 | 146:03e976389d16 | 82 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
mega64 | 146:03e976389d16 | 83 | error("PeriphClkInitStruct RTC failed with LSE\n"); |
mega64 | 146:03e976389d16 | 84 | } |
mega64 | 146:03e976389d16 | 85 | #else /* !RTC_LSI */ |
mega64 | 146:03e976389d16 | 86 | __PWR_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 87 | |
mega64 | 146:03e976389d16 | 88 | // Reset Backup domain |
mega64 | 146:03e976389d16 | 89 | __HAL_RCC_BACKUPRESET_FORCE(); |
mega64 | 146:03e976389d16 | 90 | __HAL_RCC_BACKUPRESET_RELEASE(); |
mega64 | 146:03e976389d16 | 91 | |
mega64 | 146:03e976389d16 | 92 | // Enable LSI clock |
mega64 | 146:03e976389d16 | 93 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; |
mega64 | 146:03e976389d16 | 94 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
mega64 | 146:03e976389d16 | 95 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
mega64 | 146:03e976389d16 | 96 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
mega64 | 146:03e976389d16 | 97 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
mega64 | 146:03e976389d16 | 98 | error("Cannot initialize RTC with LSI\n"); |
mega64 | 146:03e976389d16 | 99 | } |
mega64 | 146:03e976389d16 | 100 | |
mega64 | 146:03e976389d16 | 101 | __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); |
mega64 | 146:03e976389d16 | 102 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
mega64 | 146:03e976389d16 | 103 | |
mega64 | 146:03e976389d16 | 104 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
mega64 | 146:03e976389d16 | 105 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; |
mega64 | 146:03e976389d16 | 106 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
mega64 | 146:03e976389d16 | 107 | error("PeriphClkInitStruct RTC failed with LSI\n"); |
mega64 | 146:03e976389d16 | 108 | } |
mega64 | 146:03e976389d16 | 109 | #endif /* !RTC_LSI */ |
mega64 | 146:03e976389d16 | 110 | |
mega64 | 146:03e976389d16 | 111 | // Enable RTC |
mega64 | 146:03e976389d16 | 112 | __HAL_RCC_RTC_ENABLE(); |
mega64 | 146:03e976389d16 | 113 | |
mega64 | 146:03e976389d16 | 114 | #if TARGET_STM32F1 |
mega64 | 146:03e976389d16 | 115 | RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; |
mega64 | 146:03e976389d16 | 116 | #else /* TARGET_STM32F1 */ |
mega64 | 146:03e976389d16 | 117 | RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
mega64 | 146:03e976389d16 | 118 | RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; |
mega64 | 146:03e976389d16 | 119 | RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; |
mega64 | 146:03e976389d16 | 120 | RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
mega64 | 146:03e976389d16 | 121 | RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
mega64 | 146:03e976389d16 | 122 | RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
mega64 | 146:03e976389d16 | 123 | #endif /* TARGET_STM32F1 */ |
mega64 | 146:03e976389d16 | 124 | |
mega64 | 146:03e976389d16 | 125 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
mega64 | 146:03e976389d16 | 126 | error("RTC error: RTC initialization failed."); |
mega64 | 146:03e976389d16 | 127 | } |
mega64 | 146:03e976389d16 | 128 | |
mega64 | 146:03e976389d16 | 129 | #if DEVICE_LOWPOWERTIMER |
mega64 | 146:03e976389d16 | 130 | |
mega64 | 146:03e976389d16 | 131 | #if !RTC_LSI |
mega64 | 146:03e976389d16 | 132 | if (!rtc_isenabled()) |
mega64 | 146:03e976389d16 | 133 | #endif /* !RTC_LSI */ |
mega64 | 146:03e976389d16 | 134 | { |
mega64 | 146:03e976389d16 | 135 | rtc_write(0); |
mega64 | 146:03e976389d16 | 136 | } |
mega64 | 146:03e976389d16 | 137 | |
mega64 | 146:03e976389d16 | 138 | NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); |
mega64 | 146:03e976389d16 | 139 | NVIC_DisableIRQ(RTC_WKUP_IRQn); |
mega64 | 146:03e976389d16 | 140 | NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); |
mega64 | 146:03e976389d16 | 141 | NVIC_EnableIRQ(RTC_WKUP_IRQn); |
mega64 | 146:03e976389d16 | 142 | |
mega64 | 146:03e976389d16 | 143 | #endif /* DEVICE_LOWPOWERTIMER */ |
mega64 | 146:03e976389d16 | 144 | } |
mega64 | 146:03e976389d16 | 145 | |
mega64 | 146:03e976389d16 | 146 | void rtc_free(void) |
mega64 | 146:03e976389d16 | 147 | { |
mega64 | 146:03e976389d16 | 148 | #if RTC_LSI |
mega64 | 146:03e976389d16 | 149 | // Enable Power clock |
mega64 | 146:03e976389d16 | 150 | __PWR_CLK_ENABLE(); |
mega64 | 146:03e976389d16 | 151 | |
mega64 | 146:03e976389d16 | 152 | // Enable access to Backup domain |
mega64 | 146:03e976389d16 | 153 | HAL_PWR_EnableBkUpAccess(); |
mega64 | 146:03e976389d16 | 154 | |
mega64 | 146:03e976389d16 | 155 | // Reset Backup domain |
mega64 | 146:03e976389d16 | 156 | __HAL_RCC_BACKUPRESET_FORCE(); |
mega64 | 146:03e976389d16 | 157 | __HAL_RCC_BACKUPRESET_RELEASE(); |
mega64 | 146:03e976389d16 | 158 | |
mega64 | 146:03e976389d16 | 159 | // Disable access to Backup domain |
mega64 | 146:03e976389d16 | 160 | HAL_PWR_DisableBkUpAccess(); |
mega64 | 146:03e976389d16 | 161 | #endif |
mega64 | 146:03e976389d16 | 162 | |
mega64 | 146:03e976389d16 | 163 | // Disable LSI and LSE clocks |
mega64 | 146:03e976389d16 | 164 | RCC_OscInitTypeDef RCC_OscInitStruct; |
mega64 | 146:03e976389d16 | 165 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
mega64 | 146:03e976389d16 | 166 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
mega64 | 146:03e976389d16 | 167 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
mega64 | 146:03e976389d16 | 168 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
mega64 | 146:03e976389d16 | 169 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
mega64 | 146:03e976389d16 | 170 | } |
mega64 | 146:03e976389d16 | 171 | |
mega64 | 146:03e976389d16 | 172 | /* |
mega64 | 146:03e976389d16 | 173 | ST RTC_DateTypeDef structure |
mega64 | 146:03e976389d16 | 174 | WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
mega64 | 146:03e976389d16 | 175 | Month 0x1=january, 0x2=february, ..., 0x12=december |
mega64 | 146:03e976389d16 | 176 | Date day of the month 1-31 |
mega64 | 146:03e976389d16 | 177 | Year year 0-99 |
mega64 | 146:03e976389d16 | 178 | |
mega64 | 146:03e976389d16 | 179 | ST RTC_TimeTypeDef structure |
mega64 | 146:03e976389d16 | 180 | Hours 0-12 if the RTC_HourFormat_12 is selected during init |
mega64 | 146:03e976389d16 | 181 | 0-23 if the RTC_HourFormat_24 is selected during init |
mega64 | 146:03e976389d16 | 182 | Minutes 0-59 |
mega64 | 146:03e976389d16 | 183 | Seconds 0-59 |
mega64 | 146:03e976389d16 | 184 | TimeFormat RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM |
mega64 | 146:03e976389d16 | 185 | SubSeconds time unit range between [0-1] Second with [1 Sec / SecondFraction +1] granularity |
mega64 | 146:03e976389d16 | 186 | SecondFraction range or granularity of Sub Second register content corresponding to Synchronous pre-scaler factor value (PREDIV_S) |
mega64 | 146:03e976389d16 | 187 | DayLightSaving RTC_DAYLIGHTSAVING_SUB1H/RTC_DAYLIGHTSAVING_ADD1H/RTC_DAYLIGHTSAVING_NONE |
mega64 | 146:03e976389d16 | 188 | StoreOperation RTC_STOREOPERATION_RESET/RTC_STOREOPERATION_SET |
mega64 | 146:03e976389d16 | 189 | |
mega64 | 146:03e976389d16 | 190 | struct tm |
mega64 | 146:03e976389d16 | 191 | tm_sec seconds after the minute 0-61 |
mega64 | 146:03e976389d16 | 192 | tm_min minutes after the hour 0-59 |
mega64 | 146:03e976389d16 | 193 | tm_hour hours since midnight 0-23 |
mega64 | 146:03e976389d16 | 194 | tm_mday day of the month 1-31 |
mega64 | 146:03e976389d16 | 195 | tm_mon months since January 0-11 |
mega64 | 146:03e976389d16 | 196 | tm_year years since 1900 |
mega64 | 146:03e976389d16 | 197 | tm_wday days since Sunday 0-6 |
mega64 | 146:03e976389d16 | 198 | tm_yday days since January 1 0-365 |
mega64 | 146:03e976389d16 | 199 | tm_isdst Daylight Saving Time flag |
mega64 | 146:03e976389d16 | 200 | */ |
mega64 | 146:03e976389d16 | 201 | |
mega64 | 146:03e976389d16 | 202 | /* |
mega64 | 146:03e976389d16 | 203 | Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4: |
mega64 | 146:03e976389d16 | 204 | BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits. |
mega64 | 146:03e976389d16 | 205 | Because the first year is reserved to see if the RTC is init, the supposed range is 01-99. |
mega64 | 146:03e976389d16 | 206 | 1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t). |
mega64 | 146:03e976389d16 | 207 | 2nd point is to keep the year 1970 and the leap years synchronized. |
mega64 | 146:03e976389d16 | 208 | |
mega64 | 146:03e976389d16 | 209 | So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038. |
mega64 | 146:03e976389d16 | 210 | 68 is also a multiple of 4 so it let the leap year synchronized. |
mega64 | 146:03e976389d16 | 211 | |
mega64 | 146:03e976389d16 | 212 | Information about STM32F1: |
mega64 | 146:03e976389d16 | 213 | 32bit register is used (no BCD format) for the seconds and a software structure to store dates. |
mega64 | 146:03e976389d16 | 214 | It is then not a problem to not use shifts. |
mega64 | 146:03e976389d16 | 215 | */ |
mega64 | 146:03e976389d16 | 216 | |
mega64 | 146:03e976389d16 | 217 | time_t rtc_read(void) |
mega64 | 146:03e976389d16 | 218 | { |
mega64 | 146:03e976389d16 | 219 | RTC_DateTypeDef dateStruct; |
mega64 | 146:03e976389d16 | 220 | RTC_TimeTypeDef timeStruct; |
mega64 | 146:03e976389d16 | 221 | struct tm timeinfo; |
mega64 | 146:03e976389d16 | 222 | |
mega64 | 146:03e976389d16 | 223 | RtcHandle.Instance = RTC; |
mega64 | 146:03e976389d16 | 224 | |
mega64 | 146:03e976389d16 | 225 | // Read actual date and time |
mega64 | 146:03e976389d16 | 226 | // Warning: the time must be read first! |
mega64 | 146:03e976389d16 | 227 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
mega64 | 146:03e976389d16 | 228 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
mega64 | 146:03e976389d16 | 229 | |
mega64 | 146:03e976389d16 | 230 | // Setup a tm structure based on the RTC |
mega64 | 146:03e976389d16 | 231 | /* tm_wday information is ignored by mktime */ |
mega64 | 146:03e976389d16 | 232 | timeinfo.tm_mon = dateStruct.Month - 1; |
mega64 | 146:03e976389d16 | 233 | timeinfo.tm_mday = dateStruct.Date; |
mega64 | 146:03e976389d16 | 234 | timeinfo.tm_year = dateStruct.Year + 68; |
mega64 | 146:03e976389d16 | 235 | timeinfo.tm_hour = timeStruct.Hours; |
mega64 | 146:03e976389d16 | 236 | timeinfo.tm_min = timeStruct.Minutes; |
mega64 | 146:03e976389d16 | 237 | timeinfo.tm_sec = timeStruct.Seconds; |
mega64 | 146:03e976389d16 | 238 | // Daylight Saving Time information is not available |
mega64 | 146:03e976389d16 | 239 | timeinfo.tm_isdst = -1; |
mega64 | 146:03e976389d16 | 240 | |
mega64 | 146:03e976389d16 | 241 | // Convert to timestamp |
mega64 | 146:03e976389d16 | 242 | time_t t = mktime(&timeinfo); |
mega64 | 146:03e976389d16 | 243 | |
mega64 | 146:03e976389d16 | 244 | return t; |
mega64 | 146:03e976389d16 | 245 | } |
mega64 | 146:03e976389d16 | 246 | |
mega64 | 146:03e976389d16 | 247 | void rtc_write(time_t t) |
mega64 | 146:03e976389d16 | 248 | { |
mega64 | 146:03e976389d16 | 249 | RTC_DateTypeDef dateStruct; |
mega64 | 146:03e976389d16 | 250 | RTC_TimeTypeDef timeStruct; |
mega64 | 146:03e976389d16 | 251 | |
mega64 | 146:03e976389d16 | 252 | RtcHandle.Instance = RTC; |
mega64 | 146:03e976389d16 | 253 | |
mega64 | 146:03e976389d16 | 254 | // Convert the time into a tm |
mega64 | 146:03e976389d16 | 255 | struct tm *timeinfo = localtime(&t); |
mega64 | 146:03e976389d16 | 256 | |
mega64 | 146:03e976389d16 | 257 | // Fill RTC structures |
mega64 | 146:03e976389d16 | 258 | if (timeinfo->tm_wday == 0) { |
mega64 | 146:03e976389d16 | 259 | dateStruct.WeekDay = 7; |
mega64 | 146:03e976389d16 | 260 | } else { |
mega64 | 146:03e976389d16 | 261 | dateStruct.WeekDay = timeinfo->tm_wday; |
mega64 | 146:03e976389d16 | 262 | } |
mega64 | 146:03e976389d16 | 263 | dateStruct.Month = timeinfo->tm_mon + 1; |
mega64 | 146:03e976389d16 | 264 | dateStruct.Date = timeinfo->tm_mday; |
mega64 | 146:03e976389d16 | 265 | dateStruct.Year = timeinfo->tm_year - 68; |
mega64 | 146:03e976389d16 | 266 | timeStruct.Hours = timeinfo->tm_hour; |
mega64 | 146:03e976389d16 | 267 | timeStruct.Minutes = timeinfo->tm_min; |
mega64 | 146:03e976389d16 | 268 | timeStruct.Seconds = timeinfo->tm_sec; |
mega64 | 146:03e976389d16 | 269 | |
mega64 | 146:03e976389d16 | 270 | #if !(TARGET_STM32F1) |
mega64 | 146:03e976389d16 | 271 | timeStruct.TimeFormat = RTC_HOURFORMAT_24; |
mega64 | 146:03e976389d16 | 272 | timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
mega64 | 146:03e976389d16 | 273 | timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; |
mega64 | 146:03e976389d16 | 274 | #endif /* TARGET_STM32F1 */ |
mega64 | 146:03e976389d16 | 275 | |
mega64 | 146:03e976389d16 | 276 | // Change the RTC current date/time |
mega64 | 146:03e976389d16 | 277 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); |
mega64 | 146:03e976389d16 | 278 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); |
mega64 | 146:03e976389d16 | 279 | } |
mega64 | 146:03e976389d16 | 280 | |
mega64 | 146:03e976389d16 | 281 | int rtc_isenabled(void) |
mega64 | 146:03e976389d16 | 282 | { |
mega64 | 146:03e976389d16 | 283 | #if !(TARGET_STM32F1) |
mega64 | 146:03e976389d16 | 284 | return ( ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) && ((RTC->ISR & RTC_ISR_RSF) == RTC_ISR_RSF) ); |
mega64 | 146:03e976389d16 | 285 | #else /* TARGET_STM32F1 */ |
mega64 | 146:03e976389d16 | 286 | return ((RTC->CRL & RTC_CRL_RSF) == RTC_CRL_RSF); |
mega64 | 146:03e976389d16 | 287 | #endif /* TARGET_STM32F1 */ |
mega64 | 146:03e976389d16 | 288 | } |
mega64 | 146:03e976389d16 | 289 | |
mega64 | 146:03e976389d16 | 290 | #if DEVICE_LOWPOWERTIMER |
mega64 | 146:03e976389d16 | 291 | |
mega64 | 146:03e976389d16 | 292 | static void RTC_IRQHandler(void) |
mega64 | 146:03e976389d16 | 293 | { |
mega64 | 146:03e976389d16 | 294 | HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); |
mega64 | 146:03e976389d16 | 295 | } |
mega64 | 146:03e976389d16 | 296 | |
mega64 | 146:03e976389d16 | 297 | void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) |
mega64 | 146:03e976389d16 | 298 | { |
mega64 | 146:03e976389d16 | 299 | if (irq_handler) { |
mega64 | 146:03e976389d16 | 300 | // Fire the user callback |
mega64 | 146:03e976389d16 | 301 | irq_handler(); |
mega64 | 146:03e976389d16 | 302 | } |
mega64 | 146:03e976389d16 | 303 | } |
mega64 | 146:03e976389d16 | 304 | |
mega64 | 146:03e976389d16 | 305 | void rtc_set_irq_handler(uint32_t handler) |
mega64 | 146:03e976389d16 | 306 | { |
mega64 | 146:03e976389d16 | 307 | irq_handler = (void (*)(void))handler; |
mega64 | 146:03e976389d16 | 308 | } |
mega64 | 146:03e976389d16 | 309 | |
mega64 | 146:03e976389d16 | 310 | uint32_t rtc_read_subseconds(void) |
mega64 | 146:03e976389d16 | 311 | { |
mega64 | 146:03e976389d16 | 312 | return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); |
mega64 | 146:03e976389d16 | 313 | } |
mega64 | 146:03e976389d16 | 314 | |
mega64 | 146:03e976389d16 | 315 | void rtc_set_wake_up_timer(uint32_t delta) |
mega64 | 146:03e976389d16 | 316 | { |
mega64 | 146:03e976389d16 | 317 | uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); |
mega64 | 146:03e976389d16 | 318 | |
mega64 | 146:03e976389d16 | 319 | if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, |
mega64 | 146:03e976389d16 | 320 | RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { |
mega64 | 146:03e976389d16 | 321 | error("Set wake up timer failed\n"); |
mega64 | 146:03e976389d16 | 322 | } |
mega64 | 146:03e976389d16 | 323 | } |
mega64 | 146:03e976389d16 | 324 | |
mega64 | 146:03e976389d16 | 325 | void rtc_deactivate_wake_up_timer(void) |
mega64 | 146:03e976389d16 | 326 | { |
mega64 | 146:03e976389d16 | 327 | HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); |
mega64 | 146:03e976389d16 | 328 | } |
mega64 | 146:03e976389d16 | 329 | |
mega64 | 146:03e976389d16 | 330 | void rtc_synchronize(void) |
mega64 | 146:03e976389d16 | 331 | { |
mega64 | 146:03e976389d16 | 332 | HAL_RTC_WaitForSynchro(&RtcHandle); |
mega64 | 146:03e976389d16 | 333 | } |
mega64 | 146:03e976389d16 | 334 | #endif /* DEVICE_LOWPOWERTIMER */ |
mega64 | 146:03e976389d16 | 335 | |
mega64 | 146:03e976389d16 | 336 | #endif /* DEVICE_RTC */ |