mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jul 08 11:15:08 2014 +0100
Revision:
250:a49055e7a707
Parent:
237:f3da66175598
Child:
251:de9a1e4ffd79
Synchronized with git revision 3197042b65f8d28e856e1a7812d45e2fbe80e3f1

Full URL: https://github.com/mbedmicro/mbed/commit/3197042b65f8d28e856e1a7812d45e2fbe80e3f1/

error.h -> mbed_error.h

Who changed what in which revision?

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