HXC Client Shield Repository.

Dependencies:   mbed

Committer:
kashish_mbed
Date:
Mon Apr 19 17:43:09 2021 +0000
Revision:
3:5e1a54378107
Parent:
0:bacc6e701fb4
Successful Build file with CmdProcess Functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kashish_mbed 0:bacc6e701fb4 1 /*
kashish_mbed 0:bacc6e701fb4 2 / _____) _ | |
kashish_mbed 0:bacc6e701fb4 3 ( (____ _____ ____ _| |_ _____ ____| |__
kashish_mbed 0:bacc6e701fb4 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
kashish_mbed 0:bacc6e701fb4 5 _____) ) ____| | | || |_| ____( (___| | | |
kashish_mbed 0:bacc6e701fb4 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
kashish_mbed 0:bacc6e701fb4 7 (C)2013 Semtech
kashish_mbed 0:bacc6e701fb4 8
kashish_mbed 0:bacc6e701fb4 9 Description: MCU RTC timer
kashish_mbed 0:bacc6e701fb4 10
kashish_mbed 0:bacc6e701fb4 11 License: Revised BSD License, see LICENSE.TXT file include in the project
kashish_mbed 0:bacc6e701fb4 12
kashish_mbed 0:bacc6e701fb4 13 Maintainer: Miguel Luis and Gregory Cristian
kashish_mbed 0:bacc6e701fb4 14 */
kashish_mbed 0:bacc6e701fb4 15 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 16 * @file hw_rtc.c
kashish_mbed 0:bacc6e701fb4 17 * @author MCD Application Team
kashish_mbed 0:bacc6e701fb4 18 * @version V1.1.4
kashish_mbed 0:bacc6e701fb4 19 * @date 08-January-2018
kashish_mbed 0:bacc6e701fb4 20 * @brief driver for RTC
kashish_mbed 0:bacc6e701fb4 21 ******************************************************************************
kashish_mbed 0:bacc6e701fb4 22 * @attention
kashish_mbed 0:bacc6e701fb4 23 *
kashish_mbed 0:bacc6e701fb4 24 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
kashish_mbed 0:bacc6e701fb4 25 * All rights reserved.</center></h2>
kashish_mbed 0:bacc6e701fb4 26 *
kashish_mbed 0:bacc6e701fb4 27 * Redistribution and use in source and binary forms, with or without
kashish_mbed 0:bacc6e701fb4 28 * modification, are permitted, provided that the following conditions are met:
kashish_mbed 0:bacc6e701fb4 29 *
kashish_mbed 0:bacc6e701fb4 30 * 1. Redistribution of source code must retain the above copyright notice,
kashish_mbed 0:bacc6e701fb4 31 * this list of conditions and the following disclaimer.
kashish_mbed 0:bacc6e701fb4 32 * 2. Redistributions in binary form must reproduce the above copyright notice,
kashish_mbed 0:bacc6e701fb4 33 * this list of conditions and the following disclaimer in the documentation
kashish_mbed 0:bacc6e701fb4 34 * and/or other materials provided with the distribution.
kashish_mbed 0:bacc6e701fb4 35 * 3. Neither the name of STMicroelectronics nor the names of other
kashish_mbed 0:bacc6e701fb4 36 * contributors to this software may be used to endorse or promote products
kashish_mbed 0:bacc6e701fb4 37 * derived from this software without specific written permission.
kashish_mbed 0:bacc6e701fb4 38 * 4. This software, including modifications and/or derivative works of this
kashish_mbed 0:bacc6e701fb4 39 * software, must execute solely and exclusively on microcontroller or
kashish_mbed 0:bacc6e701fb4 40 * microprocessor devices manufactured by or for STMicroelectronics.
kashish_mbed 0:bacc6e701fb4 41 * 5. Redistribution and use of this software other than as permitted under
kashish_mbed 0:bacc6e701fb4 42 * this license is void and will automatically terminate your rights under
kashish_mbed 0:bacc6e701fb4 43 * this license.
kashish_mbed 0:bacc6e701fb4 44 *
kashish_mbed 0:bacc6e701fb4 45 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
kashish_mbed 0:bacc6e701fb4 46 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
kashish_mbed 0:bacc6e701fb4 47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
kashish_mbed 0:bacc6e701fb4 48 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
kashish_mbed 0:bacc6e701fb4 49 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
kashish_mbed 0:bacc6e701fb4 50 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
kashish_mbed 0:bacc6e701fb4 51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
kashish_mbed 0:bacc6e701fb4 52 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
kashish_mbed 0:bacc6e701fb4 53 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
kashish_mbed 0:bacc6e701fb4 54 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
kashish_mbed 0:bacc6e701fb4 55 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
kashish_mbed 0:bacc6e701fb4 56 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
kashish_mbed 0:bacc6e701fb4 57 *
kashish_mbed 0:bacc6e701fb4 58 ******************************************************************************
kashish_mbed 0:bacc6e701fb4 59 */
kashish_mbed 0:bacc6e701fb4 60
kashish_mbed 0:bacc6e701fb4 61 /* Includes ------------------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 62 #include "hw.h"
kashish_mbed 0:bacc6e701fb4 63 #include "low_power_manager.h"
kashish_mbed 0:bacc6e701fb4 64
kashish_mbed 0:bacc6e701fb4 65 /* Private typedef -----------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 66 typedef struct
kashish_mbed 0:bacc6e701fb4 67 {
kashish_mbed 0:bacc6e701fb4 68 uint32_t Rtc_Time; /* Reference time */
kashish_mbed 0:bacc6e701fb4 69
kashish_mbed 0:bacc6e701fb4 70 RTC_TimeTypeDef RTC_Calndr_Time; /* Reference time in calendar format */
kashish_mbed 0:bacc6e701fb4 71
kashish_mbed 0:bacc6e701fb4 72 RTC_DateTypeDef RTC_Calndr_Date; /* Reference date in calendar format */
kashish_mbed 0:bacc6e701fb4 73
kashish_mbed 0:bacc6e701fb4 74 } RtcTimerContext_t;
kashish_mbed 0:bacc6e701fb4 75
kashish_mbed 0:bacc6e701fb4 76 /* Private define ------------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 77
kashish_mbed 0:bacc6e701fb4 78 /* MCU Wake Up Time */
kashish_mbed 0:bacc6e701fb4 79 #define MIN_ALARM_DELAY 3U /* in ticks */
kashish_mbed 0:bacc6e701fb4 80
kashish_mbed 0:bacc6e701fb4 81 /* subsecond number of bits */
kashish_mbed 0:bacc6e701fb4 82 #define N_PREDIV_S 10U
kashish_mbed 0:bacc6e701fb4 83
kashish_mbed 0:bacc6e701fb4 84 /* Synchronous prediv */
kashish_mbed 0:bacc6e701fb4 85 #define PREDIV_S ((1<<N_PREDIV_S)-1)
kashish_mbed 0:bacc6e701fb4 86
kashish_mbed 0:bacc6e701fb4 87 /* Asynchronous prediv */
kashish_mbed 0:bacc6e701fb4 88 #define PREDIV_A (1<<(15-N_PREDIV_S))-1
kashish_mbed 0:bacc6e701fb4 89
kashish_mbed 0:bacc6e701fb4 90 /* Sub-second mask definition */
kashish_mbed 0:bacc6e701fb4 91 #if (N_PREDIV_S == 10)
kashish_mbed 0:bacc6e701fb4 92 #define HW_RTC_ALARMSUBSECONDMASK RTC_ALARMSUBSECONDMASK_SS14_10
kashish_mbed 0:bacc6e701fb4 93 #else
kashish_mbed 0:bacc6e701fb4 94 #error "Please define HW_RTC_ALARMSUBSECONDMASK"
kashish_mbed 0:bacc6e701fb4 95 #endif
kashish_mbed 0:bacc6e701fb4 96
kashish_mbed 0:bacc6e701fb4 97 /* RTC Time base in us */
kashish_mbed 0:bacc6e701fb4 98 #define USEC_NUMBER 1000000U
kashish_mbed 0:bacc6e701fb4 99 #define MSEC_NUMBER (USEC_NUMBER/1000)
kashish_mbed 0:bacc6e701fb4 100 #define RTC_ALARM_TIME_BASE (USEC_NUMBER>>N_PREDIV_S)
kashish_mbed 0:bacc6e701fb4 101
kashish_mbed 0:bacc6e701fb4 102 #define COMMON_FACTOR 3U
kashish_mbed 0:bacc6e701fb4 103 #define CONV_NUMER (MSEC_NUMBER>>COMMON_FACTOR)
kashish_mbed 0:bacc6e701fb4 104 #define CONV_DENOM (1<<(N_PREDIV_S-COMMON_FACTOR))
kashish_mbed 0:bacc6e701fb4 105
kashish_mbed 0:bacc6e701fb4 106
kashish_mbed 0:bacc6e701fb4 107 /* Private macro -------------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 108 /* Private variables ---------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 109 /*!
kashish_mbed 0:bacc6e701fb4 110 * \brief Indicates if the RTC is already Initialized or not
kashish_mbed 0:bacc6e701fb4 111 */
kashish_mbed 0:bacc6e701fb4 112 static bool HW_RTC_Initalized = false;
kashish_mbed 0:bacc6e701fb4 113
kashish_mbed 0:bacc6e701fb4 114 /*!
kashish_mbed 0:bacc6e701fb4 115 * \brief compensates MCU wakeup time
kashish_mbed 0:bacc6e701fb4 116 */
kashish_mbed 0:bacc6e701fb4 117
kashish_mbed 0:bacc6e701fb4 118 static bool McuWakeUpTimeInitialized = false;
kashish_mbed 0:bacc6e701fb4 119
kashish_mbed 0:bacc6e701fb4 120 /*!
kashish_mbed 0:bacc6e701fb4 121 * \brief compensates MCU wakeup time
kashish_mbed 0:bacc6e701fb4 122 */
kashish_mbed 0:bacc6e701fb4 123
kashish_mbed 0:bacc6e701fb4 124 static int16_t McuWakeUpTimeCal = 0;
kashish_mbed 0:bacc6e701fb4 125
kashish_mbed 0:bacc6e701fb4 126 /*!
kashish_mbed 0:bacc6e701fb4 127 * Number of seconds in a minute
kashish_mbed 0:bacc6e701fb4 128 */
kashish_mbed 0:bacc6e701fb4 129 static const uint8_t SecondsInMinute = 60;
kashish_mbed 0:bacc6e701fb4 130
kashish_mbed 0:bacc6e701fb4 131 /*!
kashish_mbed 0:bacc6e701fb4 132 * Number of seconds in an hour
kashish_mbed 0:bacc6e701fb4 133 */
kashish_mbed 0:bacc6e701fb4 134 static const uint16_t SecondsInHour = 3600;
kashish_mbed 0:bacc6e701fb4 135
kashish_mbed 0:bacc6e701fb4 136 /*!
kashish_mbed 0:bacc6e701fb4 137 * Number of seconds in a day
kashish_mbed 0:bacc6e701fb4 138 */
kashish_mbed 0:bacc6e701fb4 139 static const uint32_t SecondsInDay = 86400;
kashish_mbed 0:bacc6e701fb4 140
kashish_mbed 0:bacc6e701fb4 141 /*!
kashish_mbed 0:bacc6e701fb4 142 * Number of hours in a day
kashish_mbed 0:bacc6e701fb4 143 */
kashish_mbed 0:bacc6e701fb4 144 static const uint8_t HoursInDay = 24;
kashish_mbed 0:bacc6e701fb4 145
kashish_mbed 0:bacc6e701fb4 146 /*!
kashish_mbed 0:bacc6e701fb4 147 * Number of days in a standard year
kashish_mbed 0:bacc6e701fb4 148 */
kashish_mbed 0:bacc6e701fb4 149 static const uint16_t DaysInYear = 365;
kashish_mbed 0:bacc6e701fb4 150
kashish_mbed 0:bacc6e701fb4 151 /*!
kashish_mbed 0:bacc6e701fb4 152 * Number of days in a leap year
kashish_mbed 0:bacc6e701fb4 153 */
kashish_mbed 0:bacc6e701fb4 154 static const uint16_t DaysInLeapYear = 366;
kashish_mbed 0:bacc6e701fb4 155
kashish_mbed 0:bacc6e701fb4 156 /*!
kashish_mbed 0:bacc6e701fb4 157 * Number of days in each month on a normal year
kashish_mbed 0:bacc6e701fb4 158 */
kashish_mbed 0:bacc6e701fb4 159 static const uint8_t DaysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
kashish_mbed 0:bacc6e701fb4 160
kashish_mbed 0:bacc6e701fb4 161 /*!
kashish_mbed 0:bacc6e701fb4 162 * Number of days in each month on a leap year
kashish_mbed 0:bacc6e701fb4 163 */
kashish_mbed 0:bacc6e701fb4 164 static const uint8_t DaysInMonthLeapYear[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
kashish_mbed 0:bacc6e701fb4 165
kashish_mbed 0:bacc6e701fb4 166 static RTC_HandleTypeDef RtcHandle={0};
kashish_mbed 0:bacc6e701fb4 167
kashish_mbed 0:bacc6e701fb4 168 static RTC_AlarmTypeDef RTC_AlarmStructure;
kashish_mbed 0:bacc6e701fb4 169
kashish_mbed 0:bacc6e701fb4 170 /*!
kashish_mbed 0:bacc6e701fb4 171 * Keep the value of the RTC timer when the RTC alarm is set
kashish_mbed 0:bacc6e701fb4 172 * Set with the HW_RTC_SetTimerContext function
kashish_mbed 0:bacc6e701fb4 173 * Value is kept as a Reference to calculate alarm
kashish_mbed 0:bacc6e701fb4 174 */
kashish_mbed 0:bacc6e701fb4 175 static RtcTimerContext_t RtcTimerContext;
kashish_mbed 0:bacc6e701fb4 176
kashish_mbed 0:bacc6e701fb4 177 /* Private function prototypes -----------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 178
kashish_mbed 0:bacc6e701fb4 179 static void HW_RTC_SetConfig( void );
kashish_mbed 0:bacc6e701fb4 180
kashish_mbed 0:bacc6e701fb4 181 static void HW_RTC_SetAlarmConfig( void );
kashish_mbed 0:bacc6e701fb4 182
kashish_mbed 0:bacc6e701fb4 183 static void HW_RTC_StartWakeUpAlarm( uint32_t timeoutValue );
kashish_mbed 0:bacc6e701fb4 184
kashish_mbed 0:bacc6e701fb4 185 static uint32_t HW_RTC_GetCalendarValue( RTC_DateTypeDef* RTC_DateStruct, RTC_TimeTypeDef* RTC_TimeStruct );
kashish_mbed 0:bacc6e701fb4 186
kashish_mbed 0:bacc6e701fb4 187 /* Exported functions ---------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 188
kashish_mbed 0:bacc6e701fb4 189 /*!
kashish_mbed 0:bacc6e701fb4 190 * @brief Initializes the RTC timer
kashish_mbed 0:bacc6e701fb4 191 * @note The timer is based on the RTC
kashish_mbed 0:bacc6e701fb4 192 * @param none
kashish_mbed 0:bacc6e701fb4 193 * @retval none
kashish_mbed 0:bacc6e701fb4 194 */
kashish_mbed 0:bacc6e701fb4 195 void HW_RTC_Init( void )
kashish_mbed 0:bacc6e701fb4 196 {
kashish_mbed 0:bacc6e701fb4 197 if( HW_RTC_Initalized == false )
kashish_mbed 0:bacc6e701fb4 198 {
kashish_mbed 0:bacc6e701fb4 199 HW_RTC_SetConfig( );
kashish_mbed 0:bacc6e701fb4 200 HW_RTC_SetAlarmConfig( );
kashish_mbed 0:bacc6e701fb4 201 HW_RTC_SetTimerContext( );
kashish_mbed 0:bacc6e701fb4 202 HW_RTC_Initalized = true;
kashish_mbed 0:bacc6e701fb4 203 }
kashish_mbed 0:bacc6e701fb4 204 }
kashish_mbed 0:bacc6e701fb4 205
kashish_mbed 0:bacc6e701fb4 206 /*!
kashish_mbed 0:bacc6e701fb4 207 * @brief Configures the RTC timer
kashish_mbed 0:bacc6e701fb4 208 * @note The timer is based on the RTC
kashish_mbed 0:bacc6e701fb4 209 * @param none
kashish_mbed 0:bacc6e701fb4 210 * @retval none
kashish_mbed 0:bacc6e701fb4 211 */
kashish_mbed 0:bacc6e701fb4 212 static void HW_RTC_SetConfig( void )
kashish_mbed 0:bacc6e701fb4 213 {
kashish_mbed 0:bacc6e701fb4 214 RTC_TimeTypeDef RTC_TimeStruct;
kashish_mbed 0:bacc6e701fb4 215 RTC_DateTypeDef RTC_DateStruct;
kashish_mbed 0:bacc6e701fb4 216
kashish_mbed 0:bacc6e701fb4 217 RtcHandle.Instance = RTC;
kashish_mbed 0:bacc6e701fb4 218
kashish_mbed 0:bacc6e701fb4 219 RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
kashish_mbed 0:bacc6e701fb4 220 RtcHandle.Init.AsynchPrediv = PREDIV_A; /* RTC_ASYNCH_PREDIV; */
kashish_mbed 0:bacc6e701fb4 221 RtcHandle.Init.SynchPrediv = PREDIV_S; /* RTC_SYNCH_PREDIV; */
kashish_mbed 0:bacc6e701fb4 222 RtcHandle.Init.OutPut = RTC_OUTPUT;
kashish_mbed 0:bacc6e701fb4 223 RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
kashish_mbed 0:bacc6e701fb4 224 RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
kashish_mbed 0:bacc6e701fb4 225
kashish_mbed 0:bacc6e701fb4 226 HAL_RTC_Init( &RtcHandle );
kashish_mbed 0:bacc6e701fb4 227
kashish_mbed 0:bacc6e701fb4 228 /*Monday 1st January 2016*/
kashish_mbed 0:bacc6e701fb4 229 RTC_DateStruct.Year = 16;
kashish_mbed 0:bacc6e701fb4 230 RTC_DateStruct.Month = RTC_MONTH_JANUARY;
kashish_mbed 0:bacc6e701fb4 231 RTC_DateStruct.Date = 1;
kashish_mbed 0:bacc6e701fb4 232 RTC_DateStruct.WeekDay = RTC_WEEKDAY_MONDAY;
kashish_mbed 0:bacc6e701fb4 233 HAL_RTC_SetDate(&RtcHandle , &RTC_DateStruct, RTC_FORMAT_BIN);
kashish_mbed 0:bacc6e701fb4 234
kashish_mbed 0:bacc6e701fb4 235 /*at 0:0:0*/
kashish_mbed 0:bacc6e701fb4 236 RTC_TimeStruct.Hours = 0;
kashish_mbed 0:bacc6e701fb4 237 RTC_TimeStruct.Minutes = 0;
kashish_mbed 0:bacc6e701fb4 238
kashish_mbed 0:bacc6e701fb4 239 RTC_TimeStruct.Seconds = 0;
kashish_mbed 0:bacc6e701fb4 240 RTC_TimeStruct.TimeFormat = 0;
kashish_mbed 0:bacc6e701fb4 241 RTC_TimeStruct.SubSeconds = 0;
kashish_mbed 0:bacc6e701fb4 242 RTC_TimeStruct.StoreOperation = RTC_DAYLIGHTSAVING_NONE;
kashish_mbed 0:bacc6e701fb4 243 RTC_TimeStruct.DayLightSaving = RTC_STOREOPERATION_RESET;
kashish_mbed 0:bacc6e701fb4 244
kashish_mbed 0:bacc6e701fb4 245 HAL_RTC_SetTime(&RtcHandle , &RTC_TimeStruct, RTC_FORMAT_BIN);
kashish_mbed 0:bacc6e701fb4 246
kashish_mbed 0:bacc6e701fb4 247 /*Enable Direct Read of the calendar registers (not through Shadow) */
kashish_mbed 0:bacc6e701fb4 248 HAL_RTCEx_EnableBypassShadow(&RtcHandle);
kashish_mbed 0:bacc6e701fb4 249 }
kashish_mbed 0:bacc6e701fb4 250
kashish_mbed 0:bacc6e701fb4 251 /*!
kashish_mbed 0:bacc6e701fb4 252 * @brief calculates the wake up time between wake up and mcu start
kashish_mbed 0:bacc6e701fb4 253 * @note resolution in RTC_ALARM_TIME_BASE in timer ticks
kashish_mbed 0:bacc6e701fb4 254 * @param none
kashish_mbed 0:bacc6e701fb4 255 * @retval none
kashish_mbed 0:bacc6e701fb4 256 */
kashish_mbed 0:bacc6e701fb4 257 void HW_RTC_setMcuWakeUpTime( void )
kashish_mbed 0:bacc6e701fb4 258 {
kashish_mbed 0:bacc6e701fb4 259 RTC_TimeTypeDef RTC_TimeStruct;
kashish_mbed 0:bacc6e701fb4 260 RTC_DateTypeDef RTC_DateStruct;
kashish_mbed 0:bacc6e701fb4 261
kashish_mbed 0:bacc6e701fb4 262 uint32_t now, hit;
kashish_mbed 0:bacc6e701fb4 263 int16_t McuWakeUpTime;
kashish_mbed 0:bacc6e701fb4 264
kashish_mbed 0:bacc6e701fb4 265 if ((McuWakeUpTimeInitialized == false) &&
kashish_mbed 0:bacc6e701fb4 266 ( HAL_NVIC_GetPendingIRQ( RTC_Alarm_IRQn ) == 1))
kashish_mbed 0:bacc6e701fb4 267 { /* warning: works ok if now is below 30 days
kashish_mbed 0:bacc6e701fb4 268 it is ok since it's done once at first alarm wake-up*/
kashish_mbed 0:bacc6e701fb4 269 McuWakeUpTimeInitialized = true;
kashish_mbed 0:bacc6e701fb4 270 now = HW_RTC_GetCalendarValue( &RTC_DateStruct, &RTC_TimeStruct );
kashish_mbed 0:bacc6e701fb4 271
kashish_mbed 0:bacc6e701fb4 272 DBG_GPIO_SET(GPIOB, GPIO_PIN_13);
kashish_mbed 0:bacc6e701fb4 273 DBG_GPIO_RST(GPIOB, GPIO_PIN_13);
kashish_mbed 0:bacc6e701fb4 274 HAL_RTC_GetAlarm(&RtcHandle, &RTC_AlarmStructure, RTC_ALARM_A, RTC_FORMAT_BIN );
kashish_mbed 0:bacc6e701fb4 275 hit = RTC_AlarmStructure.AlarmTime.Seconds+
kashish_mbed 0:bacc6e701fb4 276 60*(RTC_AlarmStructure.AlarmTime.Minutes+
kashish_mbed 0:bacc6e701fb4 277 60*(RTC_AlarmStructure.AlarmTime.Hours+
kashish_mbed 0:bacc6e701fb4 278 24*(RTC_AlarmStructure.AlarmDateWeekDay)));
kashish_mbed 0:bacc6e701fb4 279 hit = ( hit << N_PREDIV_S ) + (PREDIV_S - RTC_AlarmStructure.AlarmTime.SubSeconds);
kashish_mbed 0:bacc6e701fb4 280
kashish_mbed 0:bacc6e701fb4 281 McuWakeUpTime = (int16_t) ((now-hit));
kashish_mbed 0:bacc6e701fb4 282 McuWakeUpTimeCal += McuWakeUpTime;
kashish_mbed 0:bacc6e701fb4 283 DBG_PRINTF("Cal=%d, %d\n",McuWakeUpTimeCal, McuWakeUpTime);
kashish_mbed 0:bacc6e701fb4 284 }
kashish_mbed 0:bacc6e701fb4 285 }
kashish_mbed 0:bacc6e701fb4 286
kashish_mbed 0:bacc6e701fb4 287 int16_t HW_RTC_getMcuWakeUpTime( void )
kashish_mbed 0:bacc6e701fb4 288 {
kashish_mbed 0:bacc6e701fb4 289 return McuWakeUpTimeCal;
kashish_mbed 0:bacc6e701fb4 290 }
kashish_mbed 0:bacc6e701fb4 291
kashish_mbed 0:bacc6e701fb4 292 /*!
kashish_mbed 0:bacc6e701fb4 293 * @brief returns the wake up time in ticks
kashish_mbed 0:bacc6e701fb4 294 * @param none
kashish_mbed 0:bacc6e701fb4 295 * @retval wake up time in ticks
kashish_mbed 0:bacc6e701fb4 296 */
kashish_mbed 0:bacc6e701fb4 297 uint32_t HW_RTC_GetMinimumTimeout( void )
kashish_mbed 0:bacc6e701fb4 298 {
kashish_mbed 0:bacc6e701fb4 299 return( MIN_ALARM_DELAY );
kashish_mbed 0:bacc6e701fb4 300 }
kashish_mbed 0:bacc6e701fb4 301
kashish_mbed 0:bacc6e701fb4 302 /*!
kashish_mbed 0:bacc6e701fb4 303 * @brief converts time in ms to time in ticks
kashish_mbed 0:bacc6e701fb4 304 * @param [IN] time in milliseconds
kashish_mbed 0:bacc6e701fb4 305 * @retval returns time in timer ticks
kashish_mbed 0:bacc6e701fb4 306 */
kashish_mbed 0:bacc6e701fb4 307 uint32_t HW_RTC_ms2Tick( uint32_t timeMicroSec )
kashish_mbed 0:bacc6e701fb4 308 {
kashish_mbed 0:bacc6e701fb4 309 /*return( ( timeMicroSec / RTC_ALARM_TIME_BASE ) ); */
kashish_mbed 0:bacc6e701fb4 310 return ( uint32_t) ( ( ((uint64_t)timeMicroSec) * CONV_DENOM ) / CONV_NUMER );
kashish_mbed 0:bacc6e701fb4 311 }
kashish_mbed 0:bacc6e701fb4 312
kashish_mbed 0:bacc6e701fb4 313 /*!
kashish_mbed 0:bacc6e701fb4 314 * @brief converts time in ticks to time in ms
kashish_mbed 0:bacc6e701fb4 315 * @param [IN] time in timer ticks
kashish_mbed 0:bacc6e701fb4 316 * @retval returns time in milliseconds
kashish_mbed 0:bacc6e701fb4 317 */
kashish_mbed 0:bacc6e701fb4 318 uint32_t HW_RTC_Tick2ms( uint32_t tick )
kashish_mbed 0:bacc6e701fb4 319 {
kashish_mbed 0:bacc6e701fb4 320 /*return( ( timeMicroSec * RTC_ALARM_TIME_BASE ) ); */
kashish_mbed 0:bacc6e701fb4 321 return ( ( (uint64_t)( tick )* CONV_NUMER ) / CONV_DENOM );
kashish_mbed 0:bacc6e701fb4 322 }
kashish_mbed 0:bacc6e701fb4 323
kashish_mbed 0:bacc6e701fb4 324 /*!
kashish_mbed 0:bacc6e701fb4 325 * @brief Set the alarm
kashish_mbed 0:bacc6e701fb4 326 * @note The alarm is set at now (read in this funtion) + timeout
kashish_mbed 0:bacc6e701fb4 327 * @param timeout Duration of the Timer ticks
kashish_mbed 0:bacc6e701fb4 328 */
kashish_mbed 0:bacc6e701fb4 329 void HW_RTC_SetAlarm( uint32_t timeout )
kashish_mbed 0:bacc6e701fb4 330 {
kashish_mbed 0:bacc6e701fb4 331 /* we don't go in Low Power mode for timeout below MIN_ALARM_DELAY */
kashish_mbed 0:bacc6e701fb4 332 if ( (MIN_ALARM_DELAY + McuWakeUpTimeCal ) < ((timeout - HW_RTC_GetTimerElapsedTime( ) )) )
kashish_mbed 0:bacc6e701fb4 333 {
kashish_mbed 0:bacc6e701fb4 334 LPM_SetStopMode(LPM_RTC_Id , LPM_Enable );
kashish_mbed 0:bacc6e701fb4 335 }
kashish_mbed 0:bacc6e701fb4 336 else
kashish_mbed 0:bacc6e701fb4 337 {
kashish_mbed 0:bacc6e701fb4 338 LPM_SetStopMode(LPM_RTC_Id , LPM_Disable );
kashish_mbed 0:bacc6e701fb4 339 }
kashish_mbed 0:bacc6e701fb4 340
kashish_mbed 0:bacc6e701fb4 341 /*In case stop mode is required */
kashish_mbed 0:bacc6e701fb4 342 if( LPM_GetMode() == LPM_StopMode )
kashish_mbed 0:bacc6e701fb4 343 {
kashish_mbed 0:bacc6e701fb4 344 timeout = timeout - McuWakeUpTimeCal;
kashish_mbed 0:bacc6e701fb4 345 }
kashish_mbed 0:bacc6e701fb4 346
kashish_mbed 0:bacc6e701fb4 347 HW_RTC_StartWakeUpAlarm( timeout );
kashish_mbed 0:bacc6e701fb4 348 }
kashish_mbed 0:bacc6e701fb4 349
kashish_mbed 0:bacc6e701fb4 350 /*!
kashish_mbed 0:bacc6e701fb4 351 * @brief Get the RTC timer elapsed time since the last Alarm was set
kashish_mbed 0:bacc6e701fb4 352 * @param none
kashish_mbed 0:bacc6e701fb4 353 * @retval RTC Elapsed time in ticks
kashish_mbed 0:bacc6e701fb4 354 */
kashish_mbed 0:bacc6e701fb4 355 uint32_t HW_RTC_GetTimerElapsedTime( void )
kashish_mbed 0:bacc6e701fb4 356 {
kashish_mbed 0:bacc6e701fb4 357 RTC_TimeTypeDef RTC_TimeStruct;
kashish_mbed 0:bacc6e701fb4 358 RTC_DateTypeDef RTC_DateStruct;
kashish_mbed 0:bacc6e701fb4 359
kashish_mbed 0:bacc6e701fb4 360 uint32_t CalendarValue = HW_RTC_GetCalendarValue(&RTC_DateStruct, &RTC_TimeStruct );
kashish_mbed 0:bacc6e701fb4 361
kashish_mbed 0:bacc6e701fb4 362 return( ( uint32_t )( CalendarValue - RtcTimerContext.Rtc_Time ));
kashish_mbed 0:bacc6e701fb4 363 }
kashish_mbed 0:bacc6e701fb4 364
kashish_mbed 0:bacc6e701fb4 365 /*!
kashish_mbed 0:bacc6e701fb4 366 * @brief Get the RTC timer value
kashish_mbed 0:bacc6e701fb4 367 * @param none
kashish_mbed 0:bacc6e701fb4 368 * @retval RTC Timer value in ticks
kashish_mbed 0:bacc6e701fb4 369 */
kashish_mbed 0:bacc6e701fb4 370 uint32_t HW_RTC_GetTimerValue( void )
kashish_mbed 0:bacc6e701fb4 371 {
kashish_mbed 0:bacc6e701fb4 372 RTC_TimeTypeDef RTC_TimeStruct;
kashish_mbed 0:bacc6e701fb4 373 RTC_DateTypeDef RTC_DateStruct;
kashish_mbed 0:bacc6e701fb4 374
kashish_mbed 0:bacc6e701fb4 375 uint32_t CalendarValue = (uint32_t) HW_RTC_GetCalendarValue(&RTC_DateStruct, &RTC_TimeStruct );
kashish_mbed 0:bacc6e701fb4 376
kashish_mbed 0:bacc6e701fb4 377 return( CalendarValue );
kashish_mbed 0:bacc6e701fb4 378 }
kashish_mbed 0:bacc6e701fb4 379
kashish_mbed 0:bacc6e701fb4 380 /*!
kashish_mbed 0:bacc6e701fb4 381 * @brief Stop the Alarm
kashish_mbed 0:bacc6e701fb4 382 * @param none
kashish_mbed 0:bacc6e701fb4 383 * @retval none
kashish_mbed 0:bacc6e701fb4 384 */
kashish_mbed 0:bacc6e701fb4 385 void HW_RTC_StopAlarm( void )
kashish_mbed 0:bacc6e701fb4 386 {
kashish_mbed 0:bacc6e701fb4 387
kashish_mbed 0:bacc6e701fb4 388 /* Clear RTC Alarm Flag */
kashish_mbed 0:bacc6e701fb4 389 __HAL_RTC_ALARM_CLEAR_FLAG( &RtcHandle, RTC_FLAG_ALRAF);
kashish_mbed 0:bacc6e701fb4 390
kashish_mbed 0:bacc6e701fb4 391 /* Disable the Alarm A interrupt */
kashish_mbed 0:bacc6e701fb4 392
kashish_mbed 0:bacc6e701fb4 393 HAL_RTC_DeactivateAlarm(&RtcHandle, RTC_ALARM_A );
kashish_mbed 0:bacc6e701fb4 394 }
kashish_mbed 0:bacc6e701fb4 395
kashish_mbed 0:bacc6e701fb4 396 /*!
kashish_mbed 0:bacc6e701fb4 397 * @brief RTC IRQ Handler on the RTC Alarm
kashish_mbed 0:bacc6e701fb4 398 * @param none
kashish_mbed 0:bacc6e701fb4 399 * @retval none
kashish_mbed 0:bacc6e701fb4 400 */
kashish_mbed 0:bacc6e701fb4 401 void HW_RTC_IrqHandler ( void )
kashish_mbed 0:bacc6e701fb4 402 {
kashish_mbed 0:bacc6e701fb4 403 RTC_HandleTypeDef* hrtc=&RtcHandle;
kashish_mbed 0:bacc6e701fb4 404 /* enable low power at irq*/
kashish_mbed 0:bacc6e701fb4 405 LPM_SetStopMode(LPM_RTC_Id , LPM_Enable );
kashish_mbed 0:bacc6e701fb4 406
kashish_mbed 0:bacc6e701fb4 407 /* Get the AlarmA interrupt source enable status */
kashish_mbed 0:bacc6e701fb4 408 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != RESET)
kashish_mbed 0:bacc6e701fb4 409 {
kashish_mbed 0:bacc6e701fb4 410 /* Get the pending status of the AlarmA Interrupt */
kashish_mbed 0:bacc6e701fb4 411 if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != RESET)
kashish_mbed 0:bacc6e701fb4 412 {
kashish_mbed 0:bacc6e701fb4 413 /* Clear the AlarmA interrupt pending bit */
kashish_mbed 0:bacc6e701fb4 414 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
kashish_mbed 0:bacc6e701fb4 415 /* Clear the EXTI's line Flag for RTC Alarm */
kashish_mbed 0:bacc6e701fb4 416 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
kashish_mbed 0:bacc6e701fb4 417 /* AlarmA callback */
kashish_mbed 0:bacc6e701fb4 418 HAL_RTC_AlarmAEventCallback(hrtc);
kashish_mbed 0:bacc6e701fb4 419 }
kashish_mbed 0:bacc6e701fb4 420 }
kashish_mbed 0:bacc6e701fb4 421 }
kashish_mbed 0:bacc6e701fb4 422
kashish_mbed 0:bacc6e701fb4 423
kashish_mbed 0:bacc6e701fb4 424 /*!
kashish_mbed 0:bacc6e701fb4 425 * @brief a delay of delay ms by polling RTC
kashish_mbed 0:bacc6e701fb4 426 * @param delay in ms
kashish_mbed 0:bacc6e701fb4 427 * @retval none
kashish_mbed 0:bacc6e701fb4 428 */
kashish_mbed 0:bacc6e701fb4 429 void HW_RTC_DelayMs( uint32_t delay )
kashish_mbed 0:bacc6e701fb4 430 {
kashish_mbed 0:bacc6e701fb4 431 uint32_t delayValue = 0;
kashish_mbed 0:bacc6e701fb4 432 uint32_t timeout = 0;
kashish_mbed 0:bacc6e701fb4 433
kashish_mbed 0:bacc6e701fb4 434 delayValue = HW_RTC_ms2Tick( delay );
kashish_mbed 0:bacc6e701fb4 435
kashish_mbed 0:bacc6e701fb4 436 /* Wait delay ms */
kashish_mbed 0:bacc6e701fb4 437 timeout = HW_RTC_GetTimerValue( );
kashish_mbed 0:bacc6e701fb4 438 while( ( ( HW_RTC_GetTimerValue( ) - timeout ) ) < delayValue )
kashish_mbed 0:bacc6e701fb4 439 {
kashish_mbed 0:bacc6e701fb4 440 __NOP( );
kashish_mbed 0:bacc6e701fb4 441 }
kashish_mbed 0:bacc6e701fb4 442 }
kashish_mbed 0:bacc6e701fb4 443
kashish_mbed 0:bacc6e701fb4 444 /*!
kashish_mbed 0:bacc6e701fb4 445 * @brief set Time Reference set also the RTC_DateStruct and RTC_TimeStruct
kashish_mbed 0:bacc6e701fb4 446 * @param none
kashish_mbed 0:bacc6e701fb4 447 * @retval Timer Value
kashish_mbed 0:bacc6e701fb4 448 */
kashish_mbed 0:bacc6e701fb4 449 uint32_t HW_RTC_SetTimerContext( void )
kashish_mbed 0:bacc6e701fb4 450 {
kashish_mbed 0:bacc6e701fb4 451 RtcTimerContext.Rtc_Time = HW_RTC_GetCalendarValue( &RtcTimerContext.RTC_Calndr_Date, &RtcTimerContext.RTC_Calndr_Time );
kashish_mbed 0:bacc6e701fb4 452 return ( uint32_t ) RtcTimerContext.Rtc_Time;
kashish_mbed 0:bacc6e701fb4 453 }
kashish_mbed 0:bacc6e701fb4 454
kashish_mbed 0:bacc6e701fb4 455 /*!
kashish_mbed 0:bacc6e701fb4 456 * @brief Get the RTC timer Reference
kashish_mbed 0:bacc6e701fb4 457 * @param none
kashish_mbed 0:bacc6e701fb4 458 * @retval Timer Value in Ticks
kashish_mbed 0:bacc6e701fb4 459 */
kashish_mbed 0:bacc6e701fb4 460 uint32_t HW_RTC_GetTimerContext( void )
kashish_mbed 0:bacc6e701fb4 461 {
kashish_mbed 0:bacc6e701fb4 462 return (uint32_t) RtcTimerContext.Rtc_Time;
kashish_mbed 0:bacc6e701fb4 463 }
kashish_mbed 0:bacc6e701fb4 464 /* Private functions ---------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 465
kashish_mbed 0:bacc6e701fb4 466 /*!
kashish_mbed 0:bacc6e701fb4 467 * @brief configure alarm at init
kashish_mbed 0:bacc6e701fb4 468 * @param none
kashish_mbed 0:bacc6e701fb4 469 * @retval none
kashish_mbed 0:bacc6e701fb4 470 */
kashish_mbed 0:bacc6e701fb4 471 static void HW_RTC_SetAlarmConfig( void )
kashish_mbed 0:bacc6e701fb4 472 {
kashish_mbed 0:bacc6e701fb4 473 HAL_RTC_DeactivateAlarm(&RtcHandle, RTC_ALARM_A);
kashish_mbed 0:bacc6e701fb4 474 }
kashish_mbed 0:bacc6e701fb4 475
kashish_mbed 0:bacc6e701fb4 476 /*!
kashish_mbed 0:bacc6e701fb4 477 * @brief start wake up alarm
kashish_mbed 0:bacc6e701fb4 478 * @note alarm in RtcTimerContext.Rtc_Time + timeoutValue
kashish_mbed 0:bacc6e701fb4 479 * @param timeoutValue in ticks
kashish_mbed 0:bacc6e701fb4 480 * @retval none
kashish_mbed 0:bacc6e701fb4 481 */
kashish_mbed 0:bacc6e701fb4 482 static void HW_RTC_StartWakeUpAlarm( uint32_t timeoutValue )
kashish_mbed 0:bacc6e701fb4 483 {
kashish_mbed 0:bacc6e701fb4 484 uint16_t rtcAlarmSubSeconds = 0;
kashish_mbed 0:bacc6e701fb4 485 uint16_t rtcAlarmSeconds = 0;
kashish_mbed 0:bacc6e701fb4 486 uint16_t rtcAlarmMinutes = 0;
kashish_mbed 0:bacc6e701fb4 487 uint16_t rtcAlarmHours = 0;
kashish_mbed 0:bacc6e701fb4 488 uint16_t rtcAlarmDays = 0;
kashish_mbed 0:bacc6e701fb4 489 RTC_TimeTypeDef RTC_TimeStruct = RtcTimerContext.RTC_Calndr_Time;
kashish_mbed 0:bacc6e701fb4 490 RTC_DateTypeDef RTC_DateStruct = RtcTimerContext.RTC_Calndr_Date;
kashish_mbed 0:bacc6e701fb4 491
kashish_mbed 0:bacc6e701fb4 492 HW_RTC_StopAlarm( );
kashish_mbed 0:bacc6e701fb4 493 DBG_GPIO_SET(GPIOB, GPIO_PIN_13);
kashish_mbed 0:bacc6e701fb4 494
kashish_mbed 0:bacc6e701fb4 495 /*reverse counter */
kashish_mbed 0:bacc6e701fb4 496 rtcAlarmSubSeconds = PREDIV_S - RTC_TimeStruct.SubSeconds;
kashish_mbed 0:bacc6e701fb4 497 rtcAlarmSubSeconds += ( timeoutValue & PREDIV_S);
kashish_mbed 0:bacc6e701fb4 498 /* convert timeout to seconds */
kashish_mbed 0:bacc6e701fb4 499 timeoutValue >>= N_PREDIV_S; /* convert timeout in seconds */
kashish_mbed 0:bacc6e701fb4 500
kashish_mbed 0:bacc6e701fb4 501 /*convert microsecs to RTC format and add to 'Now' */
kashish_mbed 0:bacc6e701fb4 502 rtcAlarmDays = RTC_DateStruct.Date;
kashish_mbed 0:bacc6e701fb4 503 while (timeoutValue >= SecondsInDay)
kashish_mbed 0:bacc6e701fb4 504 {
kashish_mbed 0:bacc6e701fb4 505 timeoutValue -= SecondsInDay;
kashish_mbed 0:bacc6e701fb4 506 rtcAlarmDays++;
kashish_mbed 0:bacc6e701fb4 507 }
kashish_mbed 0:bacc6e701fb4 508
kashish_mbed 0:bacc6e701fb4 509 /* calc hours */
kashish_mbed 0:bacc6e701fb4 510 rtcAlarmHours = RTC_TimeStruct.Hours;
kashish_mbed 0:bacc6e701fb4 511 while (timeoutValue >= SecondsInHour)
kashish_mbed 0:bacc6e701fb4 512 {
kashish_mbed 0:bacc6e701fb4 513 timeoutValue -= SecondsInHour;
kashish_mbed 0:bacc6e701fb4 514 rtcAlarmHours++;
kashish_mbed 0:bacc6e701fb4 515 }
kashish_mbed 0:bacc6e701fb4 516
kashish_mbed 0:bacc6e701fb4 517 /* calc minutes */
kashish_mbed 0:bacc6e701fb4 518 rtcAlarmMinutes = RTC_TimeStruct.Minutes;
kashish_mbed 0:bacc6e701fb4 519 while (timeoutValue >= SecondsInMinute)
kashish_mbed 0:bacc6e701fb4 520 {
kashish_mbed 0:bacc6e701fb4 521 timeoutValue -= SecondsInMinute;
kashish_mbed 0:bacc6e701fb4 522 rtcAlarmMinutes++;
kashish_mbed 0:bacc6e701fb4 523 }
kashish_mbed 0:bacc6e701fb4 524
kashish_mbed 0:bacc6e701fb4 525 /* calc seconds */
kashish_mbed 0:bacc6e701fb4 526 rtcAlarmSeconds = RTC_TimeStruct.Seconds + timeoutValue;
kashish_mbed 0:bacc6e701fb4 527
kashish_mbed 0:bacc6e701fb4 528 /***** correct for modulo********/
kashish_mbed 0:bacc6e701fb4 529 while (rtcAlarmSubSeconds >= (PREDIV_S+1))
kashish_mbed 0:bacc6e701fb4 530 {
kashish_mbed 0:bacc6e701fb4 531 rtcAlarmSubSeconds -= (PREDIV_S+1);
kashish_mbed 0:bacc6e701fb4 532 rtcAlarmSeconds++;
kashish_mbed 0:bacc6e701fb4 533 }
kashish_mbed 0:bacc6e701fb4 534
kashish_mbed 0:bacc6e701fb4 535 while (rtcAlarmSeconds >= 60)
kashish_mbed 0:bacc6e701fb4 536 {
kashish_mbed 0:bacc6e701fb4 537 rtcAlarmSeconds -= 60;
kashish_mbed 0:bacc6e701fb4 538 rtcAlarmMinutes++;
kashish_mbed 0:bacc6e701fb4 539 }
kashish_mbed 0:bacc6e701fb4 540
kashish_mbed 0:bacc6e701fb4 541 while (rtcAlarmMinutes >= 60)
kashish_mbed 0:bacc6e701fb4 542 {
kashish_mbed 0:bacc6e701fb4 543 rtcAlarmMinutes -= 60;
kashish_mbed 0:bacc6e701fb4 544 rtcAlarmHours++;
kashish_mbed 0:bacc6e701fb4 545 }
kashish_mbed 0:bacc6e701fb4 546
kashish_mbed 0:bacc6e701fb4 547 while (rtcAlarmHours >= HoursInDay)
kashish_mbed 0:bacc6e701fb4 548 {
kashish_mbed 0:bacc6e701fb4 549 rtcAlarmHours -= HoursInDay;
kashish_mbed 0:bacc6e701fb4 550 rtcAlarmDays++;
kashish_mbed 0:bacc6e701fb4 551 }
kashish_mbed 0:bacc6e701fb4 552
kashish_mbed 0:bacc6e701fb4 553 if( RTC_DateStruct.Year % 4 == 0 )
kashish_mbed 0:bacc6e701fb4 554 {
kashish_mbed 0:bacc6e701fb4 555 if( rtcAlarmDays > DaysInMonthLeapYear[ RTC_DateStruct.Month - 1 ] )
kashish_mbed 0:bacc6e701fb4 556 {
kashish_mbed 0:bacc6e701fb4 557 rtcAlarmDays = rtcAlarmDays % DaysInMonthLeapYear[ RTC_DateStruct.Month - 1 ];
kashish_mbed 0:bacc6e701fb4 558 }
kashish_mbed 0:bacc6e701fb4 559 }
kashish_mbed 0:bacc6e701fb4 560 else
kashish_mbed 0:bacc6e701fb4 561 {
kashish_mbed 0:bacc6e701fb4 562 if( rtcAlarmDays > DaysInMonth[ RTC_DateStruct.Month - 1 ] )
kashish_mbed 0:bacc6e701fb4 563 {
kashish_mbed 0:bacc6e701fb4 564 rtcAlarmDays = rtcAlarmDays % DaysInMonth[ RTC_DateStruct.Month - 1 ];
kashish_mbed 0:bacc6e701fb4 565 }
kashish_mbed 0:bacc6e701fb4 566 }
kashish_mbed 0:bacc6e701fb4 567
kashish_mbed 0:bacc6e701fb4 568 /* Set RTC_AlarmStructure with calculated values*/
kashish_mbed 0:bacc6e701fb4 569 RTC_AlarmStructure.AlarmTime.SubSeconds = PREDIV_S-rtcAlarmSubSeconds;
kashish_mbed 0:bacc6e701fb4 570 RTC_AlarmStructure.AlarmSubSecondMask = HW_RTC_ALARMSUBSECONDMASK;
kashish_mbed 0:bacc6e701fb4 571 RTC_AlarmStructure.AlarmTime.Seconds = rtcAlarmSeconds;
kashish_mbed 0:bacc6e701fb4 572 RTC_AlarmStructure.AlarmTime.Minutes = rtcAlarmMinutes;
kashish_mbed 0:bacc6e701fb4 573 RTC_AlarmStructure.AlarmTime.Hours = rtcAlarmHours;
kashish_mbed 0:bacc6e701fb4 574 RTC_AlarmStructure.AlarmDateWeekDay = ( uint8_t )rtcAlarmDays;
kashish_mbed 0:bacc6e701fb4 575 RTC_AlarmStructure.AlarmTime.TimeFormat = RTC_TimeStruct.TimeFormat;
kashish_mbed 0:bacc6e701fb4 576 RTC_AlarmStructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
kashish_mbed 0:bacc6e701fb4 577 RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_NONE;
kashish_mbed 0:bacc6e701fb4 578 RTC_AlarmStructure.Alarm = RTC_ALARM_A;
kashish_mbed 0:bacc6e701fb4 579 RTC_AlarmStructure.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
kashish_mbed 0:bacc6e701fb4 580 RTC_AlarmStructure.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
kashish_mbed 0:bacc6e701fb4 581
kashish_mbed 0:bacc6e701fb4 582 /* Set RTC_Alarm */
kashish_mbed 0:bacc6e701fb4 583 HAL_RTC_SetAlarm_IT( &RtcHandle, &RTC_AlarmStructure, RTC_FORMAT_BIN );
kashish_mbed 0:bacc6e701fb4 584
kashish_mbed 0:bacc6e701fb4 585 /* Debug Printf*/
kashish_mbed 0:bacc6e701fb4 586 DBG( HW_RTC_GetCalendarValue( &RTC_DateStruct, &RTC_TimeStruct ); );
kashish_mbed 0:bacc6e701fb4 587 //DBG_PRINTF("it's %d:%d:%d:%d ", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds, (uint16_t)((PREDIV_S - RTC_TimeStruct.SubSeconds)*1000)>>N_PREDIV_S);
kashish_mbed 0:bacc6e701fb4 588 //DBG_PRINTF("WU@ %d:%d:%d:%d\n", rtcAlarmHours, rtcAlarmMinutes, rtcAlarmSeconds, (rtcAlarmSubSeconds*1000)>>N_PREDIV_S );
kashish_mbed 0:bacc6e701fb4 589
kashish_mbed 0:bacc6e701fb4 590 DBG_GPIO_RST(GPIOB, GPIO_PIN_13);
kashish_mbed 0:bacc6e701fb4 591 }
kashish_mbed 0:bacc6e701fb4 592
kashish_mbed 0:bacc6e701fb4 593
kashish_mbed 0:bacc6e701fb4 594 /*!
kashish_mbed 0:bacc6e701fb4 595 * @brief get current time from calendar in ticks
kashish_mbed 0:bacc6e701fb4 596 * @param pointer to RTC_DateStruct
kashish_mbed 0:bacc6e701fb4 597 * @param pointer to RTC_TimeStruct
kashish_mbed 0:bacc6e701fb4 598 * @retval time in ticks
kashish_mbed 0:bacc6e701fb4 599 */
kashish_mbed 0:bacc6e701fb4 600 static uint32_t HW_RTC_GetCalendarValue( RTC_DateTypeDef* RTC_DateStruct, RTC_TimeTypeDef* RTC_TimeStruct )
kashish_mbed 0:bacc6e701fb4 601 {
kashish_mbed 0:bacc6e701fb4 602 uint32_t calendarValue = 0;
kashish_mbed 0:bacc6e701fb4 603 uint32_t i = 0;
kashish_mbed 0:bacc6e701fb4 604 uint32_t first_read;
kashish_mbed 0:bacc6e701fb4 605
kashish_mbed 0:bacc6e701fb4 606 /* Get Time and Date*/
kashish_mbed 0:bacc6e701fb4 607 HAL_RTC_GetTime( &RtcHandle, RTC_TimeStruct, RTC_FORMAT_BIN );
kashish_mbed 0:bacc6e701fb4 608
kashish_mbed 0:bacc6e701fb4 609 /* make sure it is correct due to asynchronus nature of RTC*/
kashish_mbed 0:bacc6e701fb4 610 do {
kashish_mbed 0:bacc6e701fb4 611 first_read = RTC_TimeStruct->SubSeconds;
kashish_mbed 0:bacc6e701fb4 612 HAL_RTC_GetDate( &RtcHandle, RTC_DateStruct, RTC_FORMAT_BIN );
kashish_mbed 0:bacc6e701fb4 613 HAL_RTC_GetTime( &RtcHandle, RTC_TimeStruct, RTC_FORMAT_BIN );
kashish_mbed 0:bacc6e701fb4 614 } while (first_read != RTC_TimeStruct->SubSeconds);
kashish_mbed 0:bacc6e701fb4 615
kashish_mbed 0:bacc6e701fb4 616 /* years (calc valid up to year 2099)*/
kashish_mbed 0:bacc6e701fb4 617 for( i = 0; i < RTC_DateStruct->Year; i++ )
kashish_mbed 0:bacc6e701fb4 618 {
kashish_mbed 0:bacc6e701fb4 619 if( (i % 4) == 0 )
kashish_mbed 0:bacc6e701fb4 620 {
kashish_mbed 0:bacc6e701fb4 621 calendarValue += DaysInLeapYear * SecondsInDay;
kashish_mbed 0:bacc6e701fb4 622 }
kashish_mbed 0:bacc6e701fb4 623 else
kashish_mbed 0:bacc6e701fb4 624 {
kashish_mbed 0:bacc6e701fb4 625 calendarValue += DaysInYear * SecondsInDay;
kashish_mbed 0:bacc6e701fb4 626 }
kashish_mbed 0:bacc6e701fb4 627 }
kashish_mbed 0:bacc6e701fb4 628
kashish_mbed 0:bacc6e701fb4 629 /* months (calc valid up to year 2099)*/
kashish_mbed 0:bacc6e701fb4 630 if(( (RTC_DateStruct->Year % 4) == 0 ) )
kashish_mbed 0:bacc6e701fb4 631 {
kashish_mbed 0:bacc6e701fb4 632 for( i = 0; i < ( RTC_DateStruct->Month - 1 ); i++ )
kashish_mbed 0:bacc6e701fb4 633 {
kashish_mbed 0:bacc6e701fb4 634 calendarValue += DaysInMonthLeapYear[i] * SecondsInDay;
kashish_mbed 0:bacc6e701fb4 635 }
kashish_mbed 0:bacc6e701fb4 636 }
kashish_mbed 0:bacc6e701fb4 637 else
kashish_mbed 0:bacc6e701fb4 638 {
kashish_mbed 0:bacc6e701fb4 639 for( i = 0; i < ( RTC_DateStruct->Month - 1 ); i++ )
kashish_mbed 0:bacc6e701fb4 640 {
kashish_mbed 0:bacc6e701fb4 641 calendarValue += DaysInMonth[i] * SecondsInDay;
kashish_mbed 0:bacc6e701fb4 642 }
kashish_mbed 0:bacc6e701fb4 643 }
kashish_mbed 0:bacc6e701fb4 644
kashish_mbed 0:bacc6e701fb4 645 /* days */
kashish_mbed 0:bacc6e701fb4 646 calendarValue += ( ( uint32_t )RTC_TimeStruct->Seconds +
kashish_mbed 0:bacc6e701fb4 647 ( ( uint32_t )RTC_TimeStruct->Minutes * SecondsInMinute ) +
kashish_mbed 0:bacc6e701fb4 648 ( ( uint32_t )RTC_TimeStruct->Hours * SecondsInHour ) +
kashish_mbed 0:bacc6e701fb4 649 ( ( uint32_t )( RTC_DateStruct->Date * SecondsInDay ) ) );
kashish_mbed 0:bacc6e701fb4 650
kashish_mbed 0:bacc6e701fb4 651 calendarValue = (calendarValue<<N_PREDIV_S) + ( PREDIV_S - RTC_TimeStruct->SubSeconds);
kashish_mbed 0:bacc6e701fb4 652
kashish_mbed 0:bacc6e701fb4 653 return( calendarValue );
kashish_mbed 0:bacc6e701fb4 654 }
kashish_mbed 0:bacc6e701fb4 655
kashish_mbed 0:bacc6e701fb4 656
kashish_mbed 0:bacc6e701fb4 657 /**
kashish_mbed 0:bacc6e701fb4 658 * @brief RTC MSP Initialization
kashish_mbed 0:bacc6e701fb4 659 * This function configures the hardware resources used in this example:
kashish_mbed 0:bacc6e701fb4 660 * - Peripheral's clock enable
kashish_mbed 0:bacc6e701fb4 661 * @param hrtc: RTC handle pointer
kashish_mbed 0:bacc6e701fb4 662 * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
kashish_mbed 0:bacc6e701fb4 663 * the RTC clock source; in this case the Backup domain will be reset in
kashish_mbed 0:bacc6e701fb4 664 * order to modify the RTC Clock source, as consequence RTC registers (including
kashish_mbed 0:bacc6e701fb4 665 * the backup registers) and RCC_CSR register are set to their reset values.
kashish_mbed 0:bacc6e701fb4 666 * @retval None
kashish_mbed 0:bacc6e701fb4 667 */
kashish_mbed 0:bacc6e701fb4 668 void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
kashish_mbed 0:bacc6e701fb4 669 {
kashish_mbed 0:bacc6e701fb4 670 RCC_OscInitTypeDef RCC_OscInitStruct;
kashish_mbed 0:bacc6e701fb4 671 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
kashish_mbed 0:bacc6e701fb4 672
kashish_mbed 0:bacc6e701fb4 673 /*##-1- Configue the RTC clock soucre ######################################*/
kashish_mbed 0:bacc6e701fb4 674 /* -a- Enable LSE Oscillator */
kashish_mbed 0:bacc6e701fb4 675 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
kashish_mbed 0:bacc6e701fb4 676 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
kashish_mbed 0:bacc6e701fb4 677 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
kashish_mbed 0:bacc6e701fb4 678 if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
kashish_mbed 0:bacc6e701fb4 679 {
kashish_mbed 0:bacc6e701fb4 680 DBG_PRINTF("Error_Handler\n\r");
kashish_mbed 0:bacc6e701fb4 681 while(1);
kashish_mbed 0:bacc6e701fb4 682 }
kashish_mbed 0:bacc6e701fb4 683
kashish_mbed 0:bacc6e701fb4 684 /* -b- Select LSI as RTC clock source */
kashish_mbed 0:bacc6e701fb4 685 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
kashish_mbed 0:bacc6e701fb4 686 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
kashish_mbed 0:bacc6e701fb4 687 if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
kashish_mbed 0:bacc6e701fb4 688 {
kashish_mbed 0:bacc6e701fb4 689 DBG_PRINTF("Error_Handler\n\r");
kashish_mbed 0:bacc6e701fb4 690 while(1);
kashish_mbed 0:bacc6e701fb4 691 }
kashish_mbed 0:bacc6e701fb4 692
kashish_mbed 0:bacc6e701fb4 693 /*##-2- Enable the RTC peripheral Clock ####################################*/
kashish_mbed 0:bacc6e701fb4 694 /* Enable RTC Clock */
kashish_mbed 0:bacc6e701fb4 695 __HAL_RCC_RTC_ENABLE();
kashish_mbed 0:bacc6e701fb4 696
kashish_mbed 0:bacc6e701fb4 697 /*##-3- Configure the NVIC for RTC Alarm ###################################*/
kashish_mbed 0:bacc6e701fb4 698 HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0);
kashish_mbed 0:bacc6e701fb4 699 HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
kashish_mbed 0:bacc6e701fb4 700 }
kashish_mbed 0:bacc6e701fb4 701
kashish_mbed 0:bacc6e701fb4 702 /**
kashish_mbed 0:bacc6e701fb4 703 * @brief RTC MSP De-Initialization
kashish_mbed 0:bacc6e701fb4 704 * This function freeze the hardware resources used in this example:
kashish_mbed 0:bacc6e701fb4 705 * - Disable the Peripheral's clock
kashish_mbed 0:bacc6e701fb4 706 * @param hrtc: RTC handle pointer
kashish_mbed 0:bacc6e701fb4 707 * @retval None
kashish_mbed 0:bacc6e701fb4 708 */
kashish_mbed 0:bacc6e701fb4 709 void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
kashish_mbed 0:bacc6e701fb4 710 {
kashish_mbed 0:bacc6e701fb4 711 /* Reset peripherals */
kashish_mbed 0:bacc6e701fb4 712 __HAL_RCC_RTC_DISABLE();
kashish_mbed 0:bacc6e701fb4 713 }
kashish_mbed 0:bacc6e701fb4 714 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
kashish_mbed 0:bacc6e701fb4 715
kashish_mbed 0:bacc6e701fb4 716