mbed library sources. With a patch for the can_api

Fork of mbed-dev by mbed official

Committer:
DangerousElectrician
Date:
Mon Nov 14 04:39:23 2016 +0000
Revision:
151:91825d030f9b
Parent:
149:156823d33999
stuff changed?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 *******************************************************************************
<> 149:156823d33999 3 * Copyright (c) 2015, STMicroelectronics
<> 149:156823d33999 4 * All rights reserved.
<> 149:156823d33999 5 *
<> 149:156823d33999 6 * Redistribution and use in source and binary forms, with or without
<> 149:156823d33999 7 * modification, are permitted provided that the following conditions are met:
<> 149:156823d33999 8 *
<> 149:156823d33999 9 * 1. Redistributions of source code must retain the above copyright notice,
<> 149:156823d33999 10 * this list of conditions and the following disclaimer.
<> 149:156823d33999 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
<> 149:156823d33999 12 * this list of conditions and the following disclaimer in the documentation
<> 149:156823d33999 13 * and/or other materials provided with the distribution.
<> 149:156823d33999 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
<> 149:156823d33999 15 * may be used to endorse or promote products derived from this software
<> 149:156823d33999 16 * without specific prior written permission.
<> 149:156823d33999 17 *
<> 149:156823d33999 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
<> 149:156823d33999 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
<> 149:156823d33999 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 149:156823d33999 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
<> 149:156823d33999 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
<> 149:156823d33999 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
<> 149:156823d33999 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
<> 149:156823d33999 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
<> 149:156823d33999 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
<> 149:156823d33999 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 149:156823d33999 28 *******************************************************************************
<> 149:156823d33999 29 */
<> 149:156823d33999 30 #include "rtc_api.h"
<> 149:156823d33999 31 #include "rtc_api_hal.h"
<> 149:156823d33999 32
<> 149:156823d33999 33 #if DEVICE_RTC
<> 149:156823d33999 34
<> 149:156823d33999 35 #include "mbed_error.h"
<> 149:156823d33999 36
<> 149:156823d33999 37 #if RTC_LSI
<> 149:156823d33999 38 static int rtc_inited = 0;
<> 149:156823d33999 39 #endif
<> 149:156823d33999 40
<> 149:156823d33999 41 static RTC_HandleTypeDef RtcHandle;
<> 149:156823d33999 42
<> 149:156823d33999 43 #if RTC_LSI
<> 149:156823d33999 44 #define RTC_CLOCK LSI_VALUE
<> 149:156823d33999 45 #else
<> 149:156823d33999 46 #define RTC_CLOCK LSE_VALUE
<> 149:156823d33999 47 #endif
<> 149:156823d33999 48
<> 149:156823d33999 49 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 50 #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000)
<> 149:156823d33999 51 #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1)
<> 149:156823d33999 52 #else
<> 149:156823d33999 53 #define RTC_ASYNCH_PREDIV (0x007F)
<> 149:156823d33999 54 #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1)
<> 149:156823d33999 55 #endif
<> 149:156823d33999 56
<> 149:156823d33999 57 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 58 static void (*irq_handler)(void);
<> 149:156823d33999 59 static void RTC_IRQHandler(void);
<> 149:156823d33999 60 #endif
<> 149:156823d33999 61
<> 149:156823d33999 62 void rtc_init(void)
<> 149:156823d33999 63 {
<> 149:156823d33999 64 RCC_OscInitTypeDef RCC_OscInitStruct;
<> 149:156823d33999 65
<> 149:156823d33999 66 #if RTC_LSI
<> 149:156823d33999 67 if (rtc_inited) return;
<> 149:156823d33999 68 rtc_inited = 1;
<> 149:156823d33999 69 #endif
<> 149:156823d33999 70
<> 149:156823d33999 71 RtcHandle.Instance = RTC;
<> 149:156823d33999 72
<> 149:156823d33999 73 #if !RTC_LSI
<> 149:156823d33999 74 // Enable LSE Oscillator
<> 149:156823d33999 75 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
<> 149:156823d33999 76 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
<> 149:156823d33999 77 RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT
<> 149:156823d33999 78 RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
<> 149:156823d33999 79 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly
<> 149:156823d33999 80 // Connect LSE to RTC
<> 149:156823d33999 81 __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
<> 149:156823d33999 82 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
<> 149:156823d33999 83 } else {
<> 149:156823d33999 84 error("Cannot initialize RTC with LSE\n");
<> 149:156823d33999 85 }
<> 149:156823d33999 86 #else
<> 149:156823d33999 87 // Enable Power clock
<> 149:156823d33999 88 __PWR_CLK_ENABLE();
<> 149:156823d33999 89
<> 149:156823d33999 90 // Enable access to Backup domain
<> 149:156823d33999 91 HAL_PWR_EnableBkUpAccess();
<> 149:156823d33999 92
<> 149:156823d33999 93 // Reset Backup domain
<> 149:156823d33999 94 __HAL_RCC_BACKUPRESET_FORCE();
<> 149:156823d33999 95 __HAL_RCC_BACKUPRESET_RELEASE();
<> 149:156823d33999 96
<> 149:156823d33999 97 // Enable LSI clock
<> 149:156823d33999 98 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
<> 149:156823d33999 99 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
<> 149:156823d33999 100 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
<> 149:156823d33999 101 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
<> 149:156823d33999 102 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
<> 149:156823d33999 103 error("Cannot initialize RTC with LSI\n");
<> 149:156823d33999 104 }
<> 149:156823d33999 105 // Connect LSI to RTC
<> 149:156823d33999 106 __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
<> 149:156823d33999 107 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
<> 149:156823d33999 108 #endif
<> 149:156823d33999 109
<> 149:156823d33999 110 // Enable RTC
<> 149:156823d33999 111 __HAL_RCC_RTC_ENABLE();
<> 149:156823d33999 112
<> 149:156823d33999 113 RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
<> 149:156823d33999 114 RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
<> 149:156823d33999 115 RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
<> 149:156823d33999 116 RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
<> 149:156823d33999 117 RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
<> 149:156823d33999 118 RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
<> 149:156823d33999 119
<> 149:156823d33999 120 if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
<> 149:156823d33999 121 error("RTC error: RTC initialization failed.");
<> 149:156823d33999 122 }
<> 149:156823d33999 123
<> 149:156823d33999 124 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 125 #if RTC_LSI
<> 149:156823d33999 126 rtc_write(0);
<> 149:156823d33999 127 #else
<> 149:156823d33999 128 if (!rtc_isenabled()) {
<> 149:156823d33999 129 rtc_write(0);
<> 149:156823d33999 130 }
<> 149:156823d33999 131 #endif
<> 149:156823d33999 132 NVIC_ClearPendingIRQ(RTC_WKUP_IRQn);
<> 149:156823d33999 133 NVIC_DisableIRQ(RTC_WKUP_IRQn);
<> 149:156823d33999 134 NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
<> 149:156823d33999 135 NVIC_EnableIRQ(RTC_WKUP_IRQn);
<> 149:156823d33999 136 #endif
<> 149:156823d33999 137 }
<> 149:156823d33999 138
<> 149:156823d33999 139 void rtc_free(void)
<> 149:156823d33999 140 {
<> 149:156823d33999 141 #if RTC_LSI
<> 149:156823d33999 142 // Enable Power clock
<> 149:156823d33999 143 __PWR_CLK_ENABLE();
<> 149:156823d33999 144
<> 149:156823d33999 145 // Enable access to Backup domain
<> 149:156823d33999 146 HAL_PWR_EnableBkUpAccess();
<> 149:156823d33999 147
<> 149:156823d33999 148 // Reset Backup domain
<> 149:156823d33999 149 __HAL_RCC_BACKUPRESET_FORCE();
<> 149:156823d33999 150 __HAL_RCC_BACKUPRESET_RELEASE();
<> 149:156823d33999 151
<> 149:156823d33999 152 // Disable access to Backup domain
<> 149:156823d33999 153 HAL_PWR_DisableBkUpAccess();
<> 149:156823d33999 154 #endif
<> 149:156823d33999 155
<> 149:156823d33999 156 // Disable LSI and LSE clocks
<> 149:156823d33999 157 RCC_OscInitTypeDef RCC_OscInitStruct;
<> 149:156823d33999 158 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
<> 149:156823d33999 159 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
<> 149:156823d33999 160 RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
<> 149:156823d33999 161 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
<> 149:156823d33999 162 HAL_RCC_OscConfig(&RCC_OscInitStruct);
<> 149:156823d33999 163
<> 149:156823d33999 164 #if RTC_LSI
<> 149:156823d33999 165 rtc_inited = 0;
<> 149:156823d33999 166 #endif
<> 149:156823d33999 167 }
<> 149:156823d33999 168
<> 149:156823d33999 169 int rtc_isenabled(void)
<> 149:156823d33999 170 {
<> 149:156823d33999 171 #if RTC_LSI
<> 149:156823d33999 172 return rtc_inited;
<> 149:156823d33999 173 #else
<> 149:156823d33999 174 if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) {
<> 149:156823d33999 175 return 1;
<> 149:156823d33999 176 } else {
<> 149:156823d33999 177 return 0;
<> 149:156823d33999 178 }
<> 149:156823d33999 179 #endif
<> 149:156823d33999 180 }
<> 149:156823d33999 181
<> 149:156823d33999 182 /*
<> 149:156823d33999 183 RTC Registers
<> 149:156823d33999 184 RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
<> 149:156823d33999 185 RTC_Month 1=january, 2=february, ..., 12=december
<> 149:156823d33999 186 RTC_Date day of the month 1-31
<> 149:156823d33999 187 RTC_Year year 0-99
<> 149:156823d33999 188 struct tm
<> 149:156823d33999 189 tm_sec seconds after the minute 0-61
<> 149:156823d33999 190 tm_min minutes after the hour 0-59
<> 149:156823d33999 191 tm_hour hours since midnight 0-23
<> 149:156823d33999 192 tm_mday day of the month 1-31
<> 149:156823d33999 193 tm_mon months since January 0-11
<> 149:156823d33999 194 tm_year years since 1900
<> 149:156823d33999 195 tm_wday days since Sunday 0-6
<> 149:156823d33999 196 tm_yday days since January 1 0-365
<> 149:156823d33999 197 tm_isdst Daylight Saving Time flag
<> 149:156823d33999 198 */
<> 149:156823d33999 199 time_t rtc_read(void)
<> 149:156823d33999 200 {
<> 149:156823d33999 201 RTC_DateTypeDef dateStruct;
<> 149:156823d33999 202 RTC_TimeTypeDef timeStruct;
<> 149:156823d33999 203 struct tm timeinfo;
<> 149:156823d33999 204
<> 149:156823d33999 205 RtcHandle.Instance = RTC;
<> 149:156823d33999 206
<> 149:156823d33999 207 // Read actual date and time
<> 149:156823d33999 208 // Warning: the time must be read first!
<> 149:156823d33999 209 HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
<> 149:156823d33999 210 HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
<> 149:156823d33999 211
<> 149:156823d33999 212 // Setup a tm structure based on the RTC
<> 149:156823d33999 213 timeinfo.tm_wday = dateStruct.WeekDay;
<> 149:156823d33999 214 timeinfo.tm_mon = dateStruct.Month - 1;
<> 149:156823d33999 215 timeinfo.tm_mday = dateStruct.Date;
<> 149:156823d33999 216 timeinfo.tm_year = dateStruct.Year + 68;
<> 149:156823d33999 217 timeinfo.tm_hour = timeStruct.Hours;
<> 149:156823d33999 218 timeinfo.tm_min = timeStruct.Minutes;
<> 149:156823d33999 219 timeinfo.tm_sec = timeStruct.Seconds;
<> 149:156823d33999 220 // Daylight Saving Time information is not available
<> 149:156823d33999 221 timeinfo.tm_isdst = -1;
<> 149:156823d33999 222
<> 149:156823d33999 223 // Convert to timestamp
<> 149:156823d33999 224 time_t t = mktime(&timeinfo);
<> 149:156823d33999 225
<> 149:156823d33999 226 return t;
<> 149:156823d33999 227 }
<> 149:156823d33999 228
<> 149:156823d33999 229 void rtc_write(time_t t)
<> 149:156823d33999 230 {
<> 149:156823d33999 231 RTC_DateTypeDef dateStruct;
<> 149:156823d33999 232 RTC_TimeTypeDef timeStruct;
<> 149:156823d33999 233
<> 149:156823d33999 234 RtcHandle.Instance = RTC;
<> 149:156823d33999 235
<> 149:156823d33999 236 // Enable write access to Backup domain
<> 149:156823d33999 237 HAL_PWR_EnableBkUpAccess();
<> 149:156823d33999 238
<> 149:156823d33999 239 // Convert the time into a tm
<> 149:156823d33999 240 struct tm *timeinfo = localtime(&t);
<> 149:156823d33999 241
<> 149:156823d33999 242 // Fill RTC structures
<> 149:156823d33999 243 dateStruct.WeekDay = timeinfo->tm_wday;
<> 149:156823d33999 244 dateStruct.Month = timeinfo->tm_mon + 1;
<> 149:156823d33999 245 dateStruct.Date = timeinfo->tm_mday;
<> 149:156823d33999 246 dateStruct.Year = timeinfo->tm_year - 68;
<> 149:156823d33999 247 timeStruct.Hours = timeinfo->tm_hour;
<> 149:156823d33999 248 timeStruct.Minutes = timeinfo->tm_min;
<> 149:156823d33999 249 timeStruct.Seconds = timeinfo->tm_sec;
<> 149:156823d33999 250 timeStruct.TimeFormat = RTC_HOURFORMAT_24;
<> 149:156823d33999 251 timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
<> 149:156823d33999 252 timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
<> 149:156823d33999 253
<> 149:156823d33999 254 // Change the RTC current date/time
<> 149:156823d33999 255 HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
<> 149:156823d33999 256 HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
<> 149:156823d33999 257 }
<> 149:156823d33999 258
<> 149:156823d33999 259 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 260
<> 149:156823d33999 261 static void RTC_IRQHandler(void)
<> 149:156823d33999 262 {
<> 149:156823d33999 263 HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
<> 149:156823d33999 264 }
<> 149:156823d33999 265
<> 149:156823d33999 266 void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
<> 149:156823d33999 267 {
<> 149:156823d33999 268 if (irq_handler) {
<> 149:156823d33999 269 // Fire the user callback
<> 149:156823d33999 270 irq_handler();
<> 149:156823d33999 271 }
<> 149:156823d33999 272 }
<> 149:156823d33999 273
<> 149:156823d33999 274 void rtc_set_irq_handler(uint32_t handler)
<> 149:156823d33999 275 {
<> 149:156823d33999 276 irq_handler = (void (*)(void))handler;
<> 149:156823d33999 277 }
<> 149:156823d33999 278
<> 149:156823d33999 279 uint32_t rtc_read_subseconds(void)
<> 149:156823d33999 280 {
<> 149:156823d33999 281 return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1));
<> 149:156823d33999 282 }
<> 149:156823d33999 283
<> 149:156823d33999 284 void rtc_set_wake_up_timer(uint32_t delta)
<> 149:156823d33999 285 {
<> 149:156823d33999 286 uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK);
<> 149:156823d33999 287
<> 149:156823d33999 288 if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter,
<> 149:156823d33999 289 RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) {
<> 149:156823d33999 290 error("Set wake up timer failed\n");
<> 149:156823d33999 291 }
<> 149:156823d33999 292 }
<> 149:156823d33999 293
<> 149:156823d33999 294 void rtc_deactivate_wake_up_timer(void)
<> 149:156823d33999 295 {
<> 149:156823d33999 296 HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
<> 149:156823d33999 297 }
<> 149:156823d33999 298
<> 149:156823d33999 299 void rtc_synchronize(void)
<> 149:156823d33999 300 {
<> 149:156823d33999 301 HAL_RTC_WaitForSynchro(&RtcHandle);
<> 149:156823d33999 302 }
<> 149:156823d33999 303 #endif // DEVICE_LOWPOWERTIMER
<> 149:156823d33999 304
<> 149:156823d33999 305 #endif