mbed library sources for airmote

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jun 16 07:30:08 2015 +0100
Revision:
568:08b2e7b6bab9
Parent:
522:aee49fe30179
Synchronized with git revision 1d20254fc7dcdb739e9133d44b29e934d582961e

Full URL: https://github.com/mbedmicro/mbed/commit/1d20254fc7dcdb739e9133d44b29e934d582961e/

allow outside access to RtcHandle

Who changed what in which revision?

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