mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Fri Aug 14 13:15:17 2015 +0100
Revision:
610:813dcc80987e
Synchronized with git revision 6d84db41c6833e0b9b024741eb0616a5f62d5599

Full URL: https://github.com/mbedmicro/mbed/commit/6d84db41c6833e0b9b024741eb0616a5f62d5599/

DISCO_F746NG - Improvements

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 610:813dcc80987e 1 /* mbed Microcontroller Library
mbed_official 610:813dcc80987e 2 *******************************************************************************
mbed_official 610:813dcc80987e 3 * Copyright (c) 2015, STMicroelectronics
mbed_official 610:813dcc80987e 4 * All rights reserved.
mbed_official 610:813dcc80987e 5 *
mbed_official 610:813dcc80987e 6 * Redistribution and use in source and binary forms, with or without
mbed_official 610:813dcc80987e 7 * modification, are permitted provided that the following conditions are met:
mbed_official 610:813dcc80987e 8 *
mbed_official 610:813dcc80987e 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 610:813dcc80987e 10 * this list of conditions and the following disclaimer.
mbed_official 610:813dcc80987e 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 610:813dcc80987e 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 610:813dcc80987e 13 * and/or other materials provided with the distribution.
mbed_official 610:813dcc80987e 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 610:813dcc80987e 15 * may be used to endorse or promote products derived from this software
mbed_official 610:813dcc80987e 16 * without specific prior written permission.
mbed_official 610:813dcc80987e 17 *
mbed_official 610:813dcc80987e 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 610:813dcc80987e 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 610:813dcc80987e 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 610:813dcc80987e 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 610:813dcc80987e 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 610:813dcc80987e 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 610:813dcc80987e 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 610:813dcc80987e 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 610:813dcc80987e 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 610:813dcc80987e 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 610:813dcc80987e 28 *******************************************************************************
mbed_official 610:813dcc80987e 29 */
mbed_official 610:813dcc80987e 30 #include "rtc_api.h"
mbed_official 610:813dcc80987e 31
mbed_official 610:813dcc80987e 32 #if DEVICE_RTC
mbed_official 610:813dcc80987e 33
mbed_official 610:813dcc80987e 34 #include "mbed_error.h"
mbed_official 610:813dcc80987e 35
mbed_official 610:813dcc80987e 36 static int rtc_inited = 0;
mbed_official 610:813dcc80987e 37
mbed_official 610:813dcc80987e 38 static RTC_HandleTypeDef RtcHandle;
mbed_official 610:813dcc80987e 39
mbed_official 610:813dcc80987e 40 void rtc_init(void)
mbed_official 610:813dcc80987e 41 {
mbed_official 610:813dcc80987e 42 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
mbed_official 610:813dcc80987e 43 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
mbed_official 610:813dcc80987e 44 uint32_t rtc_freq = 0;
mbed_official 610:813dcc80987e 45
mbed_official 610:813dcc80987e 46 if (rtc_inited) return;
mbed_official 610:813dcc80987e 47 rtc_inited = 1;
mbed_official 610:813dcc80987e 48
mbed_official 610:813dcc80987e 49 RtcHandle.Instance = RTC;
mbed_official 610:813dcc80987e 50
mbed_official 610:813dcc80987e 51 // Enable Power clock
mbed_official 610:813dcc80987e 52 __HAL_RCC_PWR_CLK_ENABLE();
mbed_official 610:813dcc80987e 53
mbed_official 610:813dcc80987e 54 // Enable access to Backup domain
mbed_official 610:813dcc80987e 55 HAL_PWR_EnableBkUpAccess();
mbed_official 610:813dcc80987e 56
mbed_official 610:813dcc80987e 57 // Reset Backup domain
mbed_official 610:813dcc80987e 58 __HAL_RCC_BACKUPRESET_FORCE();
mbed_official 610:813dcc80987e 59 __HAL_RCC_BACKUPRESET_RELEASE();
mbed_official 610:813dcc80987e 60
mbed_official 610:813dcc80987e 61 // Enable LSE Oscillator
mbed_official 610:813dcc80987e 62 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
mbed_official 610:813dcc80987e 63 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
mbed_official 610:813dcc80987e 64 RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT
mbed_official 610:813dcc80987e 65 RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
mbed_official 610:813dcc80987e 66 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly
mbed_official 610:813dcc80987e 67 // Connect LSE to RTC
mbed_official 610:813dcc80987e 68 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
mbed_official 610:813dcc80987e 69 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
mbed_official 610:813dcc80987e 70 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
mbed_official 610:813dcc80987e 71 rtc_freq = LSE_VALUE;
mbed_official 610:813dcc80987e 72 } else { // LSE didn't start, try with LSI
mbed_official 610:813dcc80987e 73 // Enable LSI clock
mbed_official 610:813dcc80987e 74 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
mbed_official 610:813dcc80987e 75 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
mbed_official 610:813dcc80987e 76 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
mbed_official 610:813dcc80987e 77 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
mbed_official 610:813dcc80987e 78 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
mbed_official 610:813dcc80987e 79 error("Cannot initialize RTC with LSI\n");
mbed_official 610:813dcc80987e 80 }
mbed_official 610:813dcc80987e 81 // Connect LSI to RTC
mbed_official 610:813dcc80987e 82 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
mbed_official 610:813dcc80987e 83 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
mbed_official 610:813dcc80987e 84 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
mbed_official 610:813dcc80987e 85 error("Cannot initialize RTC with LSI\n");
mbed_official 610:813dcc80987e 86 }
mbed_official 610:813dcc80987e 87 // This value is LSI typical value. To be measured precisely using a timer input capture for example.
mbed_official 610:813dcc80987e 88 rtc_freq = 40000;
mbed_official 610:813dcc80987e 89 }
mbed_official 610:813dcc80987e 90
mbed_official 610:813dcc80987e 91 // Enable RTC
mbed_official 610:813dcc80987e 92 __HAL_RCC_RTC_ENABLE();
mbed_official 610:813dcc80987e 93
mbed_official 610:813dcc80987e 94 RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
mbed_official 610:813dcc80987e 95 RtcHandle.Init.AsynchPrediv = 127;
mbed_official 610:813dcc80987e 96 RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1;
mbed_official 610:813dcc80987e 97 RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
mbed_official 610:813dcc80987e 98 RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
mbed_official 610:813dcc80987e 99 RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
mbed_official 610:813dcc80987e 100
mbed_official 610:813dcc80987e 101 if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
mbed_official 610:813dcc80987e 102 error("Cannot initialize RTC\n");
mbed_official 610:813dcc80987e 103 }
mbed_official 610:813dcc80987e 104 }
mbed_official 610:813dcc80987e 105
mbed_official 610:813dcc80987e 106 void rtc_free(void)
mbed_official 610:813dcc80987e 107 {
mbed_official 610:813dcc80987e 108 // Enable Power clock
mbed_official 610:813dcc80987e 109 __HAL_RCC_PWR_CLK_ENABLE();
mbed_official 610:813dcc80987e 110
mbed_official 610:813dcc80987e 111 // Enable access to Backup domain
mbed_official 610:813dcc80987e 112 HAL_PWR_EnableBkUpAccess();
mbed_official 610:813dcc80987e 113
mbed_official 610:813dcc80987e 114 // Reset Backup domain
mbed_official 610:813dcc80987e 115 __HAL_RCC_BACKUPRESET_FORCE();
mbed_official 610:813dcc80987e 116 __HAL_RCC_BACKUPRESET_RELEASE();
mbed_official 610:813dcc80987e 117
mbed_official 610:813dcc80987e 118 // Disable access to Backup domain
mbed_official 610:813dcc80987e 119 HAL_PWR_DisableBkUpAccess();
mbed_official 610:813dcc80987e 120
mbed_official 610:813dcc80987e 121 // Disable LSI and LSE clocks
mbed_official 610:813dcc80987e 122 RCC_OscInitTypeDef RCC_OscInitStruct;
mbed_official 610:813dcc80987e 123 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
mbed_official 610:813dcc80987e 124 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
mbed_official 610:813dcc80987e 125 RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
mbed_official 610:813dcc80987e 126 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
mbed_official 610:813dcc80987e 127 HAL_RCC_OscConfig(&RCC_OscInitStruct);
mbed_official 610:813dcc80987e 128
mbed_official 610:813dcc80987e 129 rtc_inited = 0;
mbed_official 610:813dcc80987e 130 }
mbed_official 610:813dcc80987e 131
mbed_official 610:813dcc80987e 132 int rtc_isenabled(void)
mbed_official 610:813dcc80987e 133 {
mbed_official 610:813dcc80987e 134 return rtc_inited;
mbed_official 610:813dcc80987e 135 }
mbed_official 610:813dcc80987e 136
mbed_official 610:813dcc80987e 137 /*
mbed_official 610:813dcc80987e 138 RTC Registers
mbed_official 610:813dcc80987e 139 RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
mbed_official 610:813dcc80987e 140 RTC_Month 1=january, 2=february, ..., 12=december
mbed_official 610:813dcc80987e 141 RTC_Date day of the month 1-31
mbed_official 610:813dcc80987e 142 RTC_Year year 0-99
mbed_official 610:813dcc80987e 143 struct tm
mbed_official 610:813dcc80987e 144 tm_sec seconds after the minute 0-61
mbed_official 610:813dcc80987e 145 tm_min minutes after the hour 0-59
mbed_official 610:813dcc80987e 146 tm_hour hours since midnight 0-23
mbed_official 610:813dcc80987e 147 tm_mday day of the month 1-31
mbed_official 610:813dcc80987e 148 tm_mon months since January 0-11
mbed_official 610:813dcc80987e 149 tm_year years since 1900
mbed_official 610:813dcc80987e 150 tm_wday days since Sunday 0-6
mbed_official 610:813dcc80987e 151 tm_yday days since January 1 0-365
mbed_official 610:813dcc80987e 152 tm_isdst Daylight Saving Time flag
mbed_official 610:813dcc80987e 153 */
mbed_official 610:813dcc80987e 154 time_t rtc_read(void)
mbed_official 610:813dcc80987e 155 {
mbed_official 610:813dcc80987e 156 RTC_DateTypeDef dateStruct;
mbed_official 610:813dcc80987e 157 RTC_TimeTypeDef timeStruct;
mbed_official 610:813dcc80987e 158 struct tm timeinfo;
mbed_official 610:813dcc80987e 159
mbed_official 610:813dcc80987e 160 RtcHandle.Instance = RTC;
mbed_official 610:813dcc80987e 161
mbed_official 610:813dcc80987e 162 // Read actual date and time
mbed_official 610:813dcc80987e 163 // Warning: the time must be read first!
mbed_official 610:813dcc80987e 164 HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
mbed_official 610:813dcc80987e 165 HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
mbed_official 610:813dcc80987e 166
mbed_official 610:813dcc80987e 167 // Setup a tm structure based on the RTC
mbed_official 610:813dcc80987e 168 timeinfo.tm_wday = dateStruct.WeekDay;
mbed_official 610:813dcc80987e 169 timeinfo.tm_mon = dateStruct.Month - 1;
mbed_official 610:813dcc80987e 170 timeinfo.tm_mday = dateStruct.Date;
mbed_official 610:813dcc80987e 171 timeinfo.tm_year = dateStruct.Year + 100;
mbed_official 610:813dcc80987e 172 timeinfo.tm_hour = timeStruct.Hours;
mbed_official 610:813dcc80987e 173 timeinfo.tm_min = timeStruct.Minutes;
mbed_official 610:813dcc80987e 174 timeinfo.tm_sec = timeStruct.Seconds;
mbed_official 610:813dcc80987e 175
mbed_official 610:813dcc80987e 176 // Convert to timestamp
mbed_official 610:813dcc80987e 177 time_t t = mktime(&timeinfo);
mbed_official 610:813dcc80987e 178
mbed_official 610:813dcc80987e 179 return t;
mbed_official 610:813dcc80987e 180 }
mbed_official 610:813dcc80987e 181
mbed_official 610:813dcc80987e 182 void rtc_write(time_t t)
mbed_official 610:813dcc80987e 183 {
mbed_official 610:813dcc80987e 184 RTC_DateTypeDef dateStruct;
mbed_official 610:813dcc80987e 185 RTC_TimeTypeDef timeStruct;
mbed_official 610:813dcc80987e 186
mbed_official 610:813dcc80987e 187 RtcHandle.Instance = RTC;
mbed_official 610:813dcc80987e 188
mbed_official 610:813dcc80987e 189 // Convert the time into a tm
mbed_official 610:813dcc80987e 190 struct tm *timeinfo = localtime(&t);
mbed_official 610:813dcc80987e 191
mbed_official 610:813dcc80987e 192 // Fill RTC structures
mbed_official 610:813dcc80987e 193 dateStruct.WeekDay = timeinfo->tm_wday;
mbed_official 610:813dcc80987e 194 dateStruct.Month = timeinfo->tm_mon + 1;
mbed_official 610:813dcc80987e 195 dateStruct.Date = timeinfo->tm_mday;
mbed_official 610:813dcc80987e 196 dateStruct.Year = timeinfo->tm_year - 100;
mbed_official 610:813dcc80987e 197 timeStruct.Hours = timeinfo->tm_hour;
mbed_official 610:813dcc80987e 198 timeStruct.Minutes = timeinfo->tm_min;
mbed_official 610:813dcc80987e 199 timeStruct.Seconds = timeinfo->tm_sec;
mbed_official 610:813dcc80987e 200 timeStruct.TimeFormat = RTC_HOURFORMAT12_PM;
mbed_official 610:813dcc80987e 201 timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
mbed_official 610:813dcc80987e 202 timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
mbed_official 610:813dcc80987e 203
mbed_official 610:813dcc80987e 204 // Change the RTC current date/time
mbed_official 610:813dcc80987e 205 HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
mbed_official 610:813dcc80987e 206 HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
mbed_official 610:813dcc80987e 207 }
mbed_official 610:813dcc80987e 208
mbed_official 610:813dcc80987e 209 #endif