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

Dependencies:   mbed

Committer:
fahadmirza
Date:
Thu Jan 24 23:45:12 2019 +0000
Revision:
39:cb0e5a76ab15
Parent:
32:2d0678039a09
Doxygen style header

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: Generic lora driver implementation
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, Gregory Cristian and Wael Guibene
fahadmirza 0:a0c5877bd360 14 */
fahadmirza 0:a0c5877bd360 15 /******************************************************************************
fahadmirza 0:a0c5877bd360 16 * @file time_server.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 Time server infrastructure
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 <time.h>
fahadmirza 0:a0c5877bd360 63 #include "hw_rtc.h"
fahadmirza 0:a0c5877bd360 64 #include "time_server.h"
fahadmirza 0:a0c5877bd360 65
fahadmirza 0:a0c5877bd360 66
fahadmirza 0:a0c5877bd360 67 /*!
fahadmirza 0:a0c5877bd360 68 * safely execute call back
fahadmirza 0:a0c5877bd360 69 */
fahadmirza 0:a0c5877bd360 70 #define exec_cb( _callback_ ) \
fahadmirza 0:a0c5877bd360 71 do { \
fahadmirza 0:a0c5877bd360 72 if( _callback_ != NULL ) \
fahadmirza 0:a0c5877bd360 73 { \
fahadmirza 0:a0c5877bd360 74 _callback_( ); \
fahadmirza 0:a0c5877bd360 75 } \
fahadmirza 0:a0c5877bd360 76 } while(0);
fahadmirza 0:a0c5877bd360 77
fahadmirza 0:a0c5877bd360 78
fahadmirza 0:a0c5877bd360 79
fahadmirza 0:a0c5877bd360 80 /*!
fahadmirza 0:a0c5877bd360 81 * Timers list head pointer
fahadmirza 0:a0c5877bd360 82 */
fahadmirza 0:a0c5877bd360 83 static TimerEvent_t *TimerListHead = NULL;
fahadmirza 0:a0c5877bd360 84
fahadmirza 0:a0c5877bd360 85 /*!
fahadmirza 0:a0c5877bd360 86 * \brief Adds or replace the head timer of the list.
fahadmirza 0:a0c5877bd360 87 *
fahadmirza 0:a0c5877bd360 88 * \remark The list is automatically sorted. The list head always contains the
fahadmirza 0:a0c5877bd360 89 * next timer to expire.
fahadmirza 0:a0c5877bd360 90 *
fahadmirza 0:a0c5877bd360 91 * \param [IN] obj Timer object to be become the new head
fahadmirza 0:a0c5877bd360 92 * \param [IN] remainingTime Remaining time of the previous head to be replaced
fahadmirza 0:a0c5877bd360 93 */
fahadmirza 0:a0c5877bd360 94 static void TimerInsertNewHeadTimer( TimerEvent_t *obj );
fahadmirza 0:a0c5877bd360 95
fahadmirza 0:a0c5877bd360 96 /*!
fahadmirza 0:a0c5877bd360 97 * \brief Adds a timer to the list.
fahadmirza 0:a0c5877bd360 98 *
fahadmirza 0:a0c5877bd360 99 * \remark The list is automatically sorted. The list head always contains the
fahadmirza 0:a0c5877bd360 100 * next timer to expire.
fahadmirza 0:a0c5877bd360 101 *
fahadmirza 0:a0c5877bd360 102 * \param [IN] obj Timer object to be added to the list
fahadmirza 0:a0c5877bd360 103 * \param [IN] remainingTime Remaining time of the running head after which the object may be added
fahadmirza 0:a0c5877bd360 104 */
fahadmirza 0:a0c5877bd360 105 static void TimerInsertTimer( TimerEvent_t *obj );
fahadmirza 0:a0c5877bd360 106
fahadmirza 0:a0c5877bd360 107 /*!
fahadmirza 0:a0c5877bd360 108 * \brief Sets a timeout with the duration "timestamp"
fahadmirza 0:a0c5877bd360 109 *
fahadmirza 0:a0c5877bd360 110 * \param [IN] timestamp Delay duration
fahadmirza 0:a0c5877bd360 111 */
fahadmirza 0:a0c5877bd360 112 static void TimerSetTimeout( TimerEvent_t *obj );
fahadmirza 0:a0c5877bd360 113
fahadmirza 0:a0c5877bd360 114 /*!
fahadmirza 0:a0c5877bd360 115 * \brief Check if the Object to be added is not already in the list
fahadmirza 0:a0c5877bd360 116 *
fahadmirza 0:a0c5877bd360 117 * \param [IN] timestamp Delay duration
fahadmirza 0:a0c5877bd360 118 * \retval true (the object is already in the list) or false
fahadmirza 0:a0c5877bd360 119 */
fahadmirza 0:a0c5877bd360 120 static bool TimerExists( TimerEvent_t *obj );
fahadmirza 0:a0c5877bd360 121
fahadmirza 0:a0c5877bd360 122 static void TimerIrqHandler( void );
fahadmirza 0:a0c5877bd360 123
fahadmirza 0:a0c5877bd360 124
fahadmirza 0:a0c5877bd360 125
fahadmirza 0:a0c5877bd360 126 void TimerInit( TimerEvent_t *obj, void ( *callback )( void ) )
fahadmirza 0:a0c5877bd360 127 {
fahadmirza 0:a0c5877bd360 128 obj->Timestamp = 0;
fahadmirza 0:a0c5877bd360 129 obj->ReloadValue = 0;
fahadmirza 0:a0c5877bd360 130 obj->IsRunning = false;
fahadmirza 0:a0c5877bd360 131 obj->Callback = callback;
fahadmirza 0:a0c5877bd360 132 obj->Next = NULL;
fahadmirza 0:a0c5877bd360 133 }
fahadmirza 0:a0c5877bd360 134
fahadmirza 32:2d0678039a09 135 void TimerStart( TimerEvent_t *obj)
fahadmirza 0:a0c5877bd360 136 {
fahadmirza 0:a0c5877bd360 137 uint32_t elapsedTime = 0;
fahadmirza 0:a0c5877bd360 138
fahadmirza 0:a0c5877bd360 139 BACKUP_PRIMASK();
fahadmirza 0:a0c5877bd360 140
fahadmirza 0:a0c5877bd360 141 DISABLE_IRQ( );
fahadmirza 0:a0c5877bd360 142
fahadmirza 0:a0c5877bd360 143
fahadmirza 0:a0c5877bd360 144 if( ( obj == NULL ) || ( TimerExists( obj ) == true ) )
fahadmirza 0:a0c5877bd360 145 {
fahadmirza 0:a0c5877bd360 146 RESTORE_PRIMASK( );
fahadmirza 0:a0c5877bd360 147 return;
fahadmirza 0:a0c5877bd360 148 }
fahadmirza 0:a0c5877bd360 149 obj->Timestamp = obj->ReloadValue;
fahadmirza 0:a0c5877bd360 150 obj->IsRunning = false;
fahadmirza 0:a0c5877bd360 151
fahadmirza 0:a0c5877bd360 152 if( TimerListHead == NULL )
fahadmirza 0:a0c5877bd360 153 {
fahadmirza 0:a0c5877bd360 154 HW_RTC_SetTimerContext( );
fahadmirza 0:a0c5877bd360 155 TimerInsertNewHeadTimer( obj ); // insert a timeout at now+obj->Timestamp
fahadmirza 0:a0c5877bd360 156 }
fahadmirza 0:a0c5877bd360 157 else
fahadmirza 0:a0c5877bd360 158 {
fahadmirza 0:a0c5877bd360 159 elapsedTime = HW_RTC_GetTimerElapsedTime( );
fahadmirza 0:a0c5877bd360 160 obj->Timestamp += elapsedTime;
fahadmirza 0:a0c5877bd360 161
fahadmirza 0:a0c5877bd360 162 if( obj->Timestamp < TimerListHead->Timestamp )
fahadmirza 0:a0c5877bd360 163 {
fahadmirza 0:a0c5877bd360 164 TimerInsertNewHeadTimer( obj);
fahadmirza 0:a0c5877bd360 165 }
fahadmirza 0:a0c5877bd360 166 else
fahadmirza 0:a0c5877bd360 167 {
fahadmirza 0:a0c5877bd360 168 TimerInsertTimer( obj );
fahadmirza 0:a0c5877bd360 169 }
fahadmirza 0:a0c5877bd360 170 }
fahadmirza 0:a0c5877bd360 171 RESTORE_PRIMASK( );
fahadmirza 0:a0c5877bd360 172 }
fahadmirza 0:a0c5877bd360 173
fahadmirza 0:a0c5877bd360 174 static void TimerInsertTimer( TimerEvent_t *obj)
fahadmirza 0:a0c5877bd360 175 {
fahadmirza 0:a0c5877bd360 176 TimerEvent_t* cur = TimerListHead;
fahadmirza 0:a0c5877bd360 177 TimerEvent_t* next = TimerListHead->Next;
fahadmirza 0:a0c5877bd360 178
fahadmirza 0:a0c5877bd360 179 while (cur->Next != NULL )
fahadmirza 0:a0c5877bd360 180 {
fahadmirza 0:a0c5877bd360 181 if( obj->Timestamp > next->Timestamp )
fahadmirza 0:a0c5877bd360 182 {
fahadmirza 0:a0c5877bd360 183 cur = next;
fahadmirza 0:a0c5877bd360 184 next = next->Next;
fahadmirza 0:a0c5877bd360 185 }
fahadmirza 0:a0c5877bd360 186 else
fahadmirza 0:a0c5877bd360 187 {
fahadmirza 0:a0c5877bd360 188 cur->Next = obj;
fahadmirza 0:a0c5877bd360 189 obj->Next = next;
fahadmirza 0:a0c5877bd360 190 return;
fahadmirza 0:a0c5877bd360 191 }
fahadmirza 0:a0c5877bd360 192 }
fahadmirza 0:a0c5877bd360 193 cur->Next = obj;
fahadmirza 0:a0c5877bd360 194 obj->Next = NULL;
fahadmirza 0:a0c5877bd360 195 }
fahadmirza 0:a0c5877bd360 196
fahadmirza 0:a0c5877bd360 197 static void TimerInsertNewHeadTimer( TimerEvent_t *obj )
fahadmirza 0:a0c5877bd360 198 {
fahadmirza 0:a0c5877bd360 199 TimerEvent_t* cur = TimerListHead;
fahadmirza 0:a0c5877bd360 200
fahadmirza 0:a0c5877bd360 201 if( cur != NULL )
fahadmirza 0:a0c5877bd360 202 {
fahadmirza 0:a0c5877bd360 203 cur->IsRunning = false;
fahadmirza 0:a0c5877bd360 204 }
fahadmirza 0:a0c5877bd360 205
fahadmirza 0:a0c5877bd360 206 obj->Next = cur;
fahadmirza 0:a0c5877bd360 207 TimerListHead = obj;
fahadmirza 0:a0c5877bd360 208 TimerSetTimeout( TimerListHead );
fahadmirza 0:a0c5877bd360 209 }
fahadmirza 0:a0c5877bd360 210
fahadmirza 0:a0c5877bd360 211 /**
fahadmirza 0:a0c5877bd360 212 * @brief Alarm A callback.
fahadmirza 0:a0c5877bd360 213 * @param hrtc: RTC handle
fahadmirza 0:a0c5877bd360 214 * @retval None
fahadmirza 0:a0c5877bd360 215 */
fahadmirza 0:a0c5877bd360 216 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
fahadmirza 0:a0c5877bd360 217 {
fahadmirza 0:a0c5877bd360 218 TimerIrqHandler( );
fahadmirza 0:a0c5877bd360 219 }
fahadmirza 0:a0c5877bd360 220
fahadmirza 0:a0c5877bd360 221
fahadmirza 0:a0c5877bd360 222 /*!
fahadmirza 0:a0c5877bd360 223 * \brief Timer IRQ event handler
fahadmirza 0:a0c5877bd360 224 * \note Head Timer Object is automatically removed from the List
fahadmirza 0:a0c5877bd360 225 * \note e.g. it is not needed to stop it
fahadmirza 0:a0c5877bd360 226 */
fahadmirza 0:a0c5877bd360 227 static void TimerIrqHandler( void )
fahadmirza 0:a0c5877bd360 228 {
fahadmirza 0:a0c5877bd360 229 TimerEvent_t* cur;
fahadmirza 0:a0c5877bd360 230 TimerEvent_t* next;
fahadmirza 0:a0c5877bd360 231
fahadmirza 0:a0c5877bd360 232
fahadmirza 0:a0c5877bd360 233
fahadmirza 0:a0c5877bd360 234 uint32_t old = HW_RTC_GetTimerContext( );
fahadmirza 0:a0c5877bd360 235 uint32_t now = HW_RTC_SetTimerContext( );
fahadmirza 0:a0c5877bd360 236 uint32_t DeltaContext = now - old; //Intentional wrap around
fahadmirza 0:a0c5877bd360 237
fahadmirza 0:a0c5877bd360 238 /* update timeStamp based upon new Time Reference
fahadmirza 0:a0c5877bd360 239 * Because delta context should never exceed 2^32
fahadmirza 0:a0c5877bd360 240 */
fahadmirza 0:a0c5877bd360 241 if ( TimerListHead != NULL )
fahadmirza 0:a0c5877bd360 242 {
fahadmirza 0:a0c5877bd360 243 for (cur=TimerListHead; cur->Next != NULL; cur= cur->Next)
fahadmirza 0:a0c5877bd360 244 {
fahadmirza 0:a0c5877bd360 245 next =cur->Next;
fahadmirza 0:a0c5877bd360 246 if (next->Timestamp > DeltaContext)
fahadmirza 0:a0c5877bd360 247 {
fahadmirza 0:a0c5877bd360 248 next->Timestamp -= DeltaContext;
fahadmirza 0:a0c5877bd360 249 }
fahadmirza 0:a0c5877bd360 250 else
fahadmirza 0:a0c5877bd360 251 {
fahadmirza 0:a0c5877bd360 252 next->Timestamp = 0 ;
fahadmirza 0:a0c5877bd360 253 }
fahadmirza 0:a0c5877bd360 254 }
fahadmirza 0:a0c5877bd360 255 }
fahadmirza 0:a0c5877bd360 256
fahadmirza 0:a0c5877bd360 257 /* execute immediately the alarm callback */
fahadmirza 0:a0c5877bd360 258 if ( TimerListHead != NULL )
fahadmirza 0:a0c5877bd360 259 {
fahadmirza 0:a0c5877bd360 260 cur = TimerListHead;
fahadmirza 0:a0c5877bd360 261 TimerListHead = TimerListHead->Next;
fahadmirza 0:a0c5877bd360 262 cur->IsRunning = false;
fahadmirza 0:a0c5877bd360 263 exec_cb( cur->Callback );
fahadmirza 0:a0c5877bd360 264 }
fahadmirza 0:a0c5877bd360 265
fahadmirza 0:a0c5877bd360 266
fahadmirza 0:a0c5877bd360 267 // remove all the expired object from the list
fahadmirza 0:a0c5877bd360 268 while( ( TimerListHead != NULL ) && ( TimerListHead->Timestamp < HW_RTC_GetTimerElapsedTime( ) ))
fahadmirza 0:a0c5877bd360 269 {
fahadmirza 0:a0c5877bd360 270 cur = TimerListHead;
fahadmirza 0:a0c5877bd360 271 TimerListHead = TimerListHead->Next;
fahadmirza 0:a0c5877bd360 272 cur->IsRunning = false;
fahadmirza 0:a0c5877bd360 273 exec_cb( cur->Callback );
fahadmirza 0:a0c5877bd360 274 }
fahadmirza 0:a0c5877bd360 275
fahadmirza 0:a0c5877bd360 276 /* start the next TimerListHead if it exists AND NOT running */
fahadmirza 0:a0c5877bd360 277 if(( TimerListHead != NULL ) && (TimerListHead->IsRunning == false))
fahadmirza 0:a0c5877bd360 278 {
fahadmirza 0:a0c5877bd360 279 TimerSetTimeout( TimerListHead );
fahadmirza 0:a0c5877bd360 280 }
fahadmirza 0:a0c5877bd360 281 }
fahadmirza 0:a0c5877bd360 282
fahadmirza 0:a0c5877bd360 283 void TimerStop( TimerEvent_t *obj )
fahadmirza 0:a0c5877bd360 284 {
fahadmirza 0:a0c5877bd360 285 BACKUP_PRIMASK();
fahadmirza 0:a0c5877bd360 286
fahadmirza 0:a0c5877bd360 287 DISABLE_IRQ( );
fahadmirza 0:a0c5877bd360 288
fahadmirza 0:a0c5877bd360 289 TimerEvent_t* prev = TimerListHead;
fahadmirza 0:a0c5877bd360 290 TimerEvent_t* cur = TimerListHead;
fahadmirza 0:a0c5877bd360 291
fahadmirza 0:a0c5877bd360 292 // List is empty or the Obj to stop does not exist
fahadmirza 0:a0c5877bd360 293 if( ( TimerListHead == NULL ) || ( obj == NULL ) )
fahadmirza 0:a0c5877bd360 294 {
fahadmirza 0:a0c5877bd360 295 RESTORE_PRIMASK( );
fahadmirza 0:a0c5877bd360 296 return;
fahadmirza 0:a0c5877bd360 297 }
fahadmirza 0:a0c5877bd360 298
fahadmirza 0:a0c5877bd360 299 if( TimerListHead == obj ) // Stop the Head
fahadmirza 0:a0c5877bd360 300 {
fahadmirza 0:a0c5877bd360 301 if( TimerListHead->IsRunning == true ) // The head is already running
fahadmirza 0:a0c5877bd360 302 {
fahadmirza 0:a0c5877bd360 303 if( TimerListHead->Next != NULL )
fahadmirza 0:a0c5877bd360 304 {
fahadmirza 0:a0c5877bd360 305 TimerListHead->IsRunning = false;
fahadmirza 0:a0c5877bd360 306 TimerListHead = TimerListHead->Next;
fahadmirza 0:a0c5877bd360 307 TimerSetTimeout( TimerListHead );
fahadmirza 0:a0c5877bd360 308 }
fahadmirza 0:a0c5877bd360 309 else
fahadmirza 0:a0c5877bd360 310 {
fahadmirza 0:a0c5877bd360 311 HW_RTC_StopAlarm( );
fahadmirza 0:a0c5877bd360 312 TimerListHead = NULL;
fahadmirza 0:a0c5877bd360 313 }
fahadmirza 0:a0c5877bd360 314 }
fahadmirza 0:a0c5877bd360 315 else // Stop the head before it is started
fahadmirza 0:a0c5877bd360 316 {
fahadmirza 0:a0c5877bd360 317 if( TimerListHead->Next != NULL )
fahadmirza 0:a0c5877bd360 318 {
fahadmirza 0:a0c5877bd360 319 TimerListHead = TimerListHead->Next;
fahadmirza 0:a0c5877bd360 320 }
fahadmirza 0:a0c5877bd360 321 else
fahadmirza 0:a0c5877bd360 322 {
fahadmirza 0:a0c5877bd360 323 TimerListHead = NULL;
fahadmirza 0:a0c5877bd360 324 }
fahadmirza 0:a0c5877bd360 325 }
fahadmirza 0:a0c5877bd360 326 }
fahadmirza 0:a0c5877bd360 327 else // Stop an object within the list
fahadmirza 0:a0c5877bd360 328 {
fahadmirza 0:a0c5877bd360 329 while( cur != NULL )
fahadmirza 0:a0c5877bd360 330 {
fahadmirza 0:a0c5877bd360 331 if( cur == obj )
fahadmirza 0:a0c5877bd360 332 {
fahadmirza 0:a0c5877bd360 333 if( cur->Next != NULL )
fahadmirza 0:a0c5877bd360 334 {
fahadmirza 0:a0c5877bd360 335 cur = cur->Next;
fahadmirza 0:a0c5877bd360 336 prev->Next = cur;
fahadmirza 0:a0c5877bd360 337 }
fahadmirza 0:a0c5877bd360 338 else
fahadmirza 0:a0c5877bd360 339 {
fahadmirza 0:a0c5877bd360 340 cur = NULL;
fahadmirza 0:a0c5877bd360 341 prev->Next = cur;
fahadmirza 0:a0c5877bd360 342 }
fahadmirza 0:a0c5877bd360 343 break;
fahadmirza 0:a0c5877bd360 344 }
fahadmirza 0:a0c5877bd360 345 else
fahadmirza 0:a0c5877bd360 346 {
fahadmirza 0:a0c5877bd360 347 prev = cur;
fahadmirza 0:a0c5877bd360 348 cur = cur->Next;
fahadmirza 0:a0c5877bd360 349 }
fahadmirza 0:a0c5877bd360 350 }
fahadmirza 0:a0c5877bd360 351 }
fahadmirza 0:a0c5877bd360 352
fahadmirza 0:a0c5877bd360 353 RESTORE_PRIMASK( );
fahadmirza 0:a0c5877bd360 354 }
fahadmirza 0:a0c5877bd360 355
fahadmirza 0:a0c5877bd360 356 static bool TimerExists( TimerEvent_t *obj )
fahadmirza 0:a0c5877bd360 357 {
fahadmirza 0:a0c5877bd360 358 TimerEvent_t* cur = TimerListHead;
fahadmirza 0:a0c5877bd360 359
fahadmirza 0:a0c5877bd360 360 while( cur != NULL )
fahadmirza 0:a0c5877bd360 361 {
fahadmirza 0:a0c5877bd360 362 if( cur == obj )
fahadmirza 0:a0c5877bd360 363 {
fahadmirza 0:a0c5877bd360 364 return true;
fahadmirza 0:a0c5877bd360 365 }
fahadmirza 0:a0c5877bd360 366 cur = cur->Next;
fahadmirza 0:a0c5877bd360 367 }
fahadmirza 0:a0c5877bd360 368 return false;
fahadmirza 0:a0c5877bd360 369 }
fahadmirza 0:a0c5877bd360 370
fahadmirza 32:2d0678039a09 371 void TimerReset( TimerEvent_t *obj )
fahadmirza 32:2d0678039a09 372 {
fahadmirza 32:2d0678039a09 373 TimerStop( obj );
fahadmirza 32:2d0678039a09 374 TimerStart( obj );
fahadmirza 32:2d0678039a09 375 }
fahadmirza 0:a0c5877bd360 376
fahadmirza 0:a0c5877bd360 377 /******************************************************************************
fahadmirza 0:a0c5877bd360 378 * @Brief : Set timer new timeout value
fahadmirza 0:a0c5877bd360 379 * @Param : obj - pointer to TimerEvent object
fahadmirza 0:a0c5877bd360 380 * value - timeout value
fahadmirza 0:a0c5877bd360 381 * @Return : None
fahadmirza 0:a0c5877bd360 382 ******************************************************************************/
fahadmirza 32:2d0678039a09 383 void TimerSetValue( TimerEvent_t *obj, uint32_t timeoutMs )
fahadmirza 0:a0c5877bd360 384 {
fahadmirza 0:a0c5877bd360 385 uint32_t minValue = 0;
fahadmirza 0:a0c5877bd360 386 uint32_t ticks = HW_RTC_ms2Tick( timeoutMs );
fahadmirza 0:a0c5877bd360 387
fahadmirza 0:a0c5877bd360 388 TimerStop( obj );
fahadmirza 0:a0c5877bd360 389
fahadmirza 0:a0c5877bd360 390 minValue = HW_RTC_GetMinimumTimeout( );
fahadmirza 0:a0c5877bd360 391
fahadmirza 0:a0c5877bd360 392 if( ticks < minValue )
fahadmirza 0:a0c5877bd360 393 {
fahadmirza 0:a0c5877bd360 394 ticks = minValue;
fahadmirza 0:a0c5877bd360 395 }
fahadmirza 0:a0c5877bd360 396
fahadmirza 0:a0c5877bd360 397 obj->Timestamp = ticks;
fahadmirza 0:a0c5877bd360 398 obj->ReloadValue = ticks;
fahadmirza 0:a0c5877bd360 399 }
fahadmirza 0:a0c5877bd360 400
fahadmirza 0:a0c5877bd360 401 /******************************************************************************
fahadmirza 0:a0c5877bd360 402 * @Brief : Get current time in millisecond
fahadmirza 0:a0c5877bd360 403 * @Param : void
fahadmirza 0:a0c5877bd360 404 * @Return : current ms
fahadmirza 0:a0c5877bd360 405 ******************************************************************************/
fahadmirza 0:a0c5877bd360 406 uint32_t TimerGetCurrentTime( void )
fahadmirza 0:a0c5877bd360 407 {
fahadmirza 0:a0c5877bd360 408 uint32_t now = HW_RTC_GetTimerValue( );
fahadmirza 0:a0c5877bd360 409 return HW_RTC_Tick2ms(now);
fahadmirza 0:a0c5877bd360 410 }
fahadmirza 0:a0c5877bd360 411
fahadmirza 0:a0c5877bd360 412 uint32_t TimerGetElapsedTime( uint32_t past )
fahadmirza 0:a0c5877bd360 413 {
fahadmirza 0:a0c5877bd360 414 uint32_t nowInTicks = HW_RTC_GetTimerValue( );
fahadmirza 0:a0c5877bd360 415 uint32_t pastInTicks = HW_RTC_ms2Tick( past );
fahadmirza 0:a0c5877bd360 416 /* intentional wrap around. Works OK if tick duration below 1ms */
fahadmirza 0:a0c5877bd360 417 return HW_RTC_Tick2ms( nowInTicks- pastInTicks );
fahadmirza 0:a0c5877bd360 418 }
fahadmirza 0:a0c5877bd360 419
fahadmirza 0:a0c5877bd360 420 static void TimerSetTimeout( TimerEvent_t *obj )
fahadmirza 0:a0c5877bd360 421 {
fahadmirza 0:a0c5877bd360 422 int32_t minTicks= HW_RTC_GetMinimumTimeout( );
fahadmirza 0:a0c5877bd360 423 obj->IsRunning = true;
fahadmirza 0:a0c5877bd360 424
fahadmirza 0:a0c5877bd360 425 //in case deadline too soon
fahadmirza 0:a0c5877bd360 426 if(obj->Timestamp < (HW_RTC_GetTimerElapsedTime( ) + minTicks) )
fahadmirza 0:a0c5877bd360 427 {
fahadmirza 0:a0c5877bd360 428 obj->Timestamp = HW_RTC_GetTimerElapsedTime( ) + minTicks;
fahadmirza 0:a0c5877bd360 429 }
fahadmirza 0:a0c5877bd360 430 HW_RTC_SetAlarm( obj->Timestamp );
fahadmirza 0:a0c5877bd360 431 }
fahadmirza 0:a0c5877bd360 432 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
fahadmirza 0:a0c5877bd360 433