Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: WizFi250_AP_HelloWorld
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/rtc_api.c@489:119543c9f674, 2015-03-12 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Mar 12 14:30:49 2015 +0000
- Revision:
- 489:119543c9f674
- Parent:
- 402:09075a3b15e3
Synchronized with git revision 051854181516992fb498d51f9ee6e70cbad9e083
Full URL: https://github.com/mbedmicro/mbed/commit/051854181516992fb498d51f9ee6e70cbad9e083/
Fix ksdk mcu HAL - stopbit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 60:142c6c6f5949 | 1 | /* mbed Microcontroller Library |
| mbed_official | 70:c1fbde68b492 | 2 | ******************************************************************************* |
| mbed_official | 70:c1fbde68b492 | 3 | * Copyright (c) 2014, STMicroelectronics |
| mbed_official | 70:c1fbde68b492 | 4 | * All rights reserved. |
| mbed_official | 60:142c6c6f5949 | 5 | * |
| mbed_official | 70:c1fbde68b492 | 6 | * Redistribution and use in source and binary forms, with or without |
| mbed_official | 70:c1fbde68b492 | 7 | * modification, are permitted provided that the following conditions are met: |
| mbed_official | 60:142c6c6f5949 | 8 | * |
| mbed_official | 70:c1fbde68b492 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
| mbed_official | 70:c1fbde68b492 | 10 | * this list of conditions and the following disclaimer. |
| mbed_official | 70:c1fbde68b492 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| mbed_official | 70:c1fbde68b492 | 12 | * this list of conditions and the following disclaimer in the documentation |
| mbed_official | 70:c1fbde68b492 | 13 | * and/or other materials provided with the distribution. |
| mbed_official | 70:c1fbde68b492 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| mbed_official | 70:c1fbde68b492 | 15 | * may be used to endorse or promote products derived from this software |
| mbed_official | 70:c1fbde68b492 | 16 | * without specific prior written permission. |
| mbed_official | 60:142c6c6f5949 | 17 | * |
| mbed_official | 70:c1fbde68b492 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| mbed_official | 70:c1fbde68b492 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| mbed_official | 70:c1fbde68b492 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| mbed_official | 70:c1fbde68b492 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| mbed_official | 70:c1fbde68b492 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| mbed_official | 70:c1fbde68b492 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| mbed_official | 70:c1fbde68b492 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| mbed_official | 70:c1fbde68b492 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| mbed_official | 70:c1fbde68b492 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| mbed_official | 70:c1fbde68b492 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| mbed_official | 70:c1fbde68b492 | 28 | ******************************************************************************* |
| mbed_official | 60:142c6c6f5949 | 29 | */ |
| mbed_official | 60:142c6c6f5949 | 30 | #include "rtc_api.h" |
| mbed_official | 60:142c6c6f5949 | 31 | |
| mbed_official | 174:8bb9f3a33240 | 32 | #if DEVICE_RTC |
| mbed_official | 174:8bb9f3a33240 | 33 | |
| mbed_official | 489:119543c9f674 | 34 | #include "mbed_error.h" |
| mbed_official | 174:8bb9f3a33240 | 35 | |
| mbed_official | 60:142c6c6f5949 | 36 | static int rtc_inited = 0; |
| mbed_official | 60:142c6c6f5949 | 37 | |
| mbed_official | 489:119543c9f674 | 38 | static RTC_HandleTypeDef RtcHandle; |
| mbed_official | 489:119543c9f674 | 39 | |
| mbed_official | 402:09075a3b15e3 | 40 | void rtc_init(void) |
| mbed_official | 402:09075a3b15e3 | 41 | { |
| mbed_official | 489:119543c9f674 | 42 | RCC_OscInitTypeDef RCC_OscInitStruct; |
| mbed_official | 174:8bb9f3a33240 | 43 | |
| mbed_official | 489:119543c9f674 | 44 | if (rtc_inited) return; |
| mbed_official | 489:119543c9f674 | 45 | rtc_inited = 1; |
| mbed_official | 489:119543c9f674 | 46 | |
| mbed_official | 489:119543c9f674 | 47 | RtcHandle.Instance = RTC; |
| mbed_official | 60:142c6c6f5949 | 48 | |
| mbed_official | 489:119543c9f674 | 49 | // Enable Power clock |
| mbed_official | 489:119543c9f674 | 50 | __HAL_RCC_PWR_CLK_ENABLE(); |
| mbed_official | 174:8bb9f3a33240 | 51 | |
| mbed_official | 489:119543c9f674 | 52 | // Enable access to Backup domain |
| mbed_official | 489:119543c9f674 | 53 | HAL_PWR_EnableBkUpAccess(); |
| mbed_official | 174:8bb9f3a33240 | 54 | |
| mbed_official | 489:119543c9f674 | 55 | // Reset Backup domain |
| mbed_official | 489:119543c9f674 | 56 | __HAL_RCC_BACKUPRESET_FORCE(); |
| mbed_official | 489:119543c9f674 | 57 | __HAL_RCC_BACKUPRESET_RELEASE(); |
| mbed_official | 174:8bb9f3a33240 | 58 | |
| mbed_official | 489:119543c9f674 | 59 | // Enable LSE Oscillator |
| mbed_official | 489:119543c9f674 | 60 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
| mbed_official | 489:119543c9f674 | 61 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
| mbed_official | 489:119543c9f674 | 62 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT |
| mbed_official | 489:119543c9f674 | 63 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { |
| mbed_official | 489:119543c9f674 | 64 | // Connect LSE to RTC |
| mbed_official | 489:119543c9f674 | 65 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); |
| mbed_official | 174:8bb9f3a33240 | 66 | } else { |
| mbed_official | 489:119543c9f674 | 67 | // Enable LSI clock |
| mbed_official | 489:119543c9f674 | 68 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
| mbed_official | 489:119543c9f674 | 69 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! |
| mbed_official | 489:119543c9f674 | 70 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
| mbed_official | 489:119543c9f674 | 71 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
| mbed_official | 489:119543c9f674 | 72 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| mbed_official | 489:119543c9f674 | 73 | error("RTC error: LSI clock initialization failed."); |
| mbed_official | 489:119543c9f674 | 74 | } |
| mbed_official | 489:119543c9f674 | 75 | // Connect LSI to RTC |
| mbed_official | 489:119543c9f674 | 76 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); |
| mbed_official | 174:8bb9f3a33240 | 77 | } |
| mbed_official | 174:8bb9f3a33240 | 78 | |
| mbed_official | 489:119543c9f674 | 79 | // Enable RTC |
| mbed_official | 489:119543c9f674 | 80 | __HAL_RCC_RTC_ENABLE(); |
| mbed_official | 60:142c6c6f5949 | 81 | |
| mbed_official | 489:119543c9f674 | 82 | RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; |
| mbed_official | 174:8bb9f3a33240 | 83 | |
| mbed_official | 489:119543c9f674 | 84 | if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { |
| mbed_official | 489:119543c9f674 | 85 | error("RTC error: RTC initialization failed."); |
| mbed_official | 489:119543c9f674 | 86 | } |
| mbed_official | 60:142c6c6f5949 | 87 | } |
| mbed_official | 60:142c6c6f5949 | 88 | |
| mbed_official | 402:09075a3b15e3 | 89 | void rtc_free(void) |
| mbed_official | 402:09075a3b15e3 | 90 | { |
| mbed_official | 489:119543c9f674 | 91 | // Enable Power clock |
| mbed_official | 489:119543c9f674 | 92 | __PWR_CLK_ENABLE(); |
| mbed_official | 489:119543c9f674 | 93 | |
| mbed_official | 489:119543c9f674 | 94 | // Enable access to Backup domain |
| mbed_official | 489:119543c9f674 | 95 | HAL_PWR_EnableBkUpAccess(); |
| mbed_official | 489:119543c9f674 | 96 | |
| mbed_official | 489:119543c9f674 | 97 | // Reset Backup domain |
| mbed_official | 489:119543c9f674 | 98 | __HAL_RCC_BACKUPRESET_FORCE(); |
| mbed_official | 489:119543c9f674 | 99 | __HAL_RCC_BACKUPRESET_RELEASE(); |
| mbed_official | 489:119543c9f674 | 100 | |
| mbed_official | 489:119543c9f674 | 101 | // Disable access to Backup domain |
| mbed_official | 489:119543c9f674 | 102 | HAL_PWR_DisableBkUpAccess(); |
| mbed_official | 489:119543c9f674 | 103 | |
| mbed_official | 489:119543c9f674 | 104 | // Disable LSI and LSE clocks |
| mbed_official | 489:119543c9f674 | 105 | RCC_OscInitTypeDef RCC_OscInitStruct; |
| mbed_official | 489:119543c9f674 | 106 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; |
| mbed_official | 489:119543c9f674 | 107 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
| mbed_official | 489:119543c9f674 | 108 | RCC_OscInitStruct.LSIState = RCC_LSI_OFF; |
| mbed_official | 489:119543c9f674 | 109 | RCC_OscInitStruct.LSEState = RCC_LSE_OFF; |
| mbed_official | 489:119543c9f674 | 110 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
| mbed_official | 215:83cf97a28428 | 111 | |
| mbed_official | 60:142c6c6f5949 | 112 | rtc_inited = 0; |
| mbed_official | 60:142c6c6f5949 | 113 | } |
| mbed_official | 60:142c6c6f5949 | 114 | |
| mbed_official | 402:09075a3b15e3 | 115 | int rtc_isenabled(void) |
| mbed_official | 402:09075a3b15e3 | 116 | { |
| mbed_official | 60:142c6c6f5949 | 117 | return rtc_inited; |
| mbed_official | 60:142c6c6f5949 | 118 | } |
| mbed_official | 60:142c6c6f5949 | 119 | |
| mbed_official | 489:119543c9f674 | 120 | /* |
| mbed_official | 489:119543c9f674 | 121 | RTC Registers |
| mbed_official | 489:119543c9f674 | 122 | RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday |
| mbed_official | 489:119543c9f674 | 123 | RTC_Month 1=january, 2=february, ..., 12=december |
| mbed_official | 489:119543c9f674 | 124 | RTC_Date day of the month 1-31 |
| mbed_official | 489:119543c9f674 | 125 | RTC_Year year 0-99 |
| mbed_official | 489:119543c9f674 | 126 | struct tm |
| mbed_official | 489:119543c9f674 | 127 | tm_sec seconds after the minute 0-61 |
| mbed_official | 489:119543c9f674 | 128 | tm_min minutes after the hour 0-59 |
| mbed_official | 489:119543c9f674 | 129 | tm_hour hours since midnight 0-23 |
| mbed_official | 489:119543c9f674 | 130 | tm_mday day of the month 1-31 |
| mbed_official | 489:119543c9f674 | 131 | tm_mon months since January 0-11 |
| mbed_official | 489:119543c9f674 | 132 | tm_year years since 1900 |
| mbed_official | 489:119543c9f674 | 133 | tm_wday days since Sunday 0-6 |
| mbed_official | 489:119543c9f674 | 134 | tm_yday days since January 1 0-365 |
| mbed_official | 489:119543c9f674 | 135 | tm_isdst Daylight Saving Time flag |
| mbed_official | 489:119543c9f674 | 136 | */ |
| mbed_official | 402:09075a3b15e3 | 137 | time_t rtc_read(void) |
| mbed_official | 402:09075a3b15e3 | 138 | { |
| mbed_official | 489:119543c9f674 | 139 | RTC_DateTypeDef dateStruct; |
| mbed_official | 489:119543c9f674 | 140 | RTC_TimeTypeDef timeStruct; |
| mbed_official | 489:119543c9f674 | 141 | struct tm timeinfo; |
| mbed_official | 489:119543c9f674 | 142 | |
| mbed_official | 489:119543c9f674 | 143 | RtcHandle.Instance = RTC; |
| mbed_official | 489:119543c9f674 | 144 | |
| mbed_official | 489:119543c9f674 | 145 | // Read actual date and time |
| mbed_official | 489:119543c9f674 | 146 | // Warning: the time must be read first! |
| mbed_official | 489:119543c9f674 | 147 | HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); |
| mbed_official | 489:119543c9f674 | 148 | HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); |
| mbed_official | 489:119543c9f674 | 149 | |
| mbed_official | 489:119543c9f674 | 150 | // Setup a tm structure based on the RTC |
| mbed_official | 489:119543c9f674 | 151 | timeinfo.tm_wday = dateStruct.WeekDay; |
| mbed_official | 489:119543c9f674 | 152 | timeinfo.tm_mon = dateStruct.Month - 1; |
| mbed_official | 489:119543c9f674 | 153 | timeinfo.tm_mday = dateStruct.Date; |
| mbed_official | 489:119543c9f674 | 154 | timeinfo.tm_year = dateStruct.Year + 100; |
| mbed_official | 489:119543c9f674 | 155 | timeinfo.tm_hour = timeStruct.Hours; |
| mbed_official | 489:119543c9f674 | 156 | timeinfo.tm_min = timeStruct.Minutes; |
| mbed_official | 489:119543c9f674 | 157 | timeinfo.tm_sec = timeStruct.Seconds; |
| mbed_official | 489:119543c9f674 | 158 | |
| mbed_official | 489:119543c9f674 | 159 | // Convert to timestamp |
| mbed_official | 489:119543c9f674 | 160 | time_t t = mktime(&timeinfo); |
| mbed_official | 489:119543c9f674 | 161 | |
| mbed_official | 489:119543c9f674 | 162 | return t; |
| mbed_official | 60:142c6c6f5949 | 163 | } |
| mbed_official | 60:142c6c6f5949 | 164 | |
| mbed_official | 402:09075a3b15e3 | 165 | void rtc_write(time_t t) |
| mbed_official | 402:09075a3b15e3 | 166 | { |
| mbed_official | 489:119543c9f674 | 167 | RTC_DateTypeDef dateStruct; |
| mbed_official | 489:119543c9f674 | 168 | RTC_TimeTypeDef timeStruct; |
| mbed_official | 489:119543c9f674 | 169 | |
| mbed_official | 489:119543c9f674 | 170 | RtcHandle.Instance = RTC; |
| mbed_official | 489:119543c9f674 | 171 | |
| mbed_official | 489:119543c9f674 | 172 | // Convert the time into a tm |
| mbed_official | 489:119543c9f674 | 173 | struct tm *timeinfo = localtime(&t); |
| mbed_official | 489:119543c9f674 | 174 | |
| mbed_official | 489:119543c9f674 | 175 | // Fill RTC structures |
| mbed_official | 489:119543c9f674 | 176 | dateStruct.WeekDay = timeinfo->tm_wday; |
| mbed_official | 489:119543c9f674 | 177 | dateStruct.Month = timeinfo->tm_mon + 1; |
| mbed_official | 489:119543c9f674 | 178 | dateStruct.Date = timeinfo->tm_mday; |
| mbed_official | 489:119543c9f674 | 179 | dateStruct.Year = timeinfo->tm_year - 100; |
| mbed_official | 489:119543c9f674 | 180 | timeStruct.Hours = timeinfo->tm_hour; |
| mbed_official | 489:119543c9f674 | 181 | timeStruct.Minutes = timeinfo->tm_min; |
| mbed_official | 489:119543c9f674 | 182 | timeStruct.Seconds = timeinfo->tm_sec; |
| mbed_official | 489:119543c9f674 | 183 | |
| mbed_official | 489:119543c9f674 | 184 | // Change the RTC current date/time |
| mbed_official | 489:119543c9f674 | 185 | HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); |
| mbed_official | 489:119543c9f674 | 186 | HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); |
| mbed_official | 60:142c6c6f5949 | 187 | } |
| mbed_official | 174:8bb9f3a33240 | 188 | |
| mbed_official | 174:8bb9f3a33240 | 189 | #endif |
