Johannes Stratmann / mbed-dev

Fork of mbed-dev by mbed official

Committer:
JojoS
Date:
Sat Sep 10 15:32:04 2016 +0000
Revision:
147:ba84b7dc41a7
Parent:
144:ef7eb2e8f9f7
added prescaler for 16 bit timers (solution as in LPC11xx), default prescaler 31 for max 28 ms period time

Who changed what in which revision?

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