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