Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

Who changed what in which revision?

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