Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rt_Timer.c Source File

rt_Timer.c

Go to the documentation of this file.
00001 /**
00002  * @file    rt_Timer.c
00003  * @brief   
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "rt_TypeDef.h"
00023 #include "RTX_Config.h"
00024 #include "rt_Timer.h"
00025 #include "rt_MemBox.h"
00026 
00027 
00028 /*----------------------------------------------------------------------------
00029  *      Global Variables
00030  *---------------------------------------------------------------------------*/
00031 
00032 /* User Timer list pointer */
00033 struct OS_XTMR os_tmr;
00034 
00035 /*----------------------------------------------------------------------------
00036  *      Functions
00037  *---------------------------------------------------------------------------*/
00038 
00039 /*--------------------------- rt_tmr_tick -----------------------------------*/
00040 
00041 void rt_tmr_tick (void) {
00042   /* Decrement delta count of timer list head. Timers having the value of   */
00043   /* zero are removed from the list and the callback function is called.    */
00044   P_TMR p;
00045 
00046   if (os_tmr.next == NULL) {
00047     return;
00048   }
00049   os_tmr.tcnt--;
00050   while (os_tmr.tcnt == 0 && (p = os_tmr.next) != NULL) {
00051     /* Call a user provided function to handle an elapsed timer */
00052     os_tmr_call (p->info);
00053     os_tmr.tcnt = p->tcnt;
00054     os_tmr.next = p->next;
00055     rt_free_box ((U32 *)m_tmr, p);
00056   }
00057 }
00058 
00059 /*--------------------------- rt_tmr_create ---------------------------------*/
00060 
00061 OS_ID rt_tmr_create (U16 tcnt, U16 info)  {
00062   /* Create an user timer and put it into the chained timer list using      */
00063   /* a timeout count value of "tcnt". User parameter "info" is used as a    */
00064   /* parameter for the user provided callback function "os_tmr_call ()".    */
00065   P_TMR p_tmr, p;
00066   U32 delta,itcnt = tcnt;
00067 
00068   if (tcnt == 0 || m_tmr == NULL)  {
00069     return (NULL);
00070   }
00071   p_tmr = rt_alloc_box ((U32 *)m_tmr);
00072   if (!p_tmr)  {
00073     return (NULL);
00074   }
00075   p_tmr->info = info;
00076   p = (P_TMR)&os_tmr;
00077   delta = p->tcnt;
00078   while (delta < itcnt && p->next != NULL) {
00079     p = p->next;
00080     delta += p->tcnt;
00081   }
00082   /* Right place found, insert timer into the list */
00083   p_tmr->next = p->next;
00084   p_tmr->tcnt = (U16)(delta - itcnt);
00085   p->next = p_tmr;
00086   p->tcnt -= p_tmr->tcnt;
00087   return (p_tmr);
00088 }
00089 
00090 /*--------------------------- rt_tmr_kill -----------------------------------*/
00091 
00092 OS_ID rt_tmr_kill (OS_ID timer)  {
00093   /* Remove user timer from the chained timer list. */
00094   P_TMR p, p_tmr;
00095 
00096   p_tmr = (P_TMR)timer;
00097   p = (P_TMR)&os_tmr;
00098   /* Search timer list for requested timer */
00099   while (p->next != p_tmr)  {
00100     if (p->next == NULL) {
00101       /* Failed, "timer" is not in the timer list */
00102       return (p_tmr);
00103     }
00104     p = p->next;
00105   }
00106   /* Timer was found, remove it from the list */
00107   p->next = p_tmr->next;
00108   p->tcnt += p_tmr->tcnt;
00109   rt_free_box ((U32 *)m_tmr, p_tmr);
00110   /* Timer killed */
00111   return (NULL);
00112 }
00113 
00114 /*----------------------------------------------------------------------------
00115  * end of file
00116  *---------------------------------------------------------------------------*/