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:
Mon Jul 06 09:00:34 2015 +0100
Revision:
582:a89625bcd809
Parent:
573:ad23fe03a082
Synchronized with git revision 45004fb61e4af8f74f4e916318df5207fcf6076d

Full URL: https://github.com/mbedmicro/mbed/commit/45004fb61e4af8f74f4e916318df5207fcf6076d/

DISCO_F746NG - Fix several issues

Who changed what in which revision?

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