A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.

Dependencies:   mbed

Committer:
fahadmirza
Date:
Thu Jul 12 00:50:48 2018 +0000
Revision:
0:a0c5877bd360
Porting complete!

Who changed what in which revision?

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