summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/timer.c
blob: ca36cca98e39cc00a78e48847ae0820c9d1cb8ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**************************************************************************//**
  \file  timer.c

  \brief Functions to manipulate by timers list.

  \author
      Atmel Corporation: http://www.atmel.com \n
      Support email: avr@atmel.com

    Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
    Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
      7/12/07 A. Khromykh - Created
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/

/******************************************************************************
                   Includes section
******************************************************************************/
#include <bcTimer.h>

/******************************************************************************
                   Implementations section
******************************************************************************/
/******************************************************************************
Adds timer to the timer's list.
Parameters:
  head - address of pointer to head of the timers list.
  timer - address of timer that must be added to the list.
  sysTime - current time.
Returns:
  none.
******************************************************************************/
void halAddTimer(Timer_t **head, Timer_t *new, uint32_t sysTime)
{
  if (!*head)
  {
    *head = new;
    return;
  }

  Timer_t *it, *prev = NULL;
  for (it = *head; it; it = it->service.next)
  {
    uint32_t remain = it->service.sysTimeLabel + it->interval - sysTime;
    if ((remain < INT32_MAX) && (remain >= new->interval))
      break;
    prev = it;
  }
  if (it == *head)
  {
    new->service.next = *head;
    *head = new;
  }
  else
  {
    prev->service.next = new;
    new->service.next = it;
  }
}

/******************************************************************************
Removes timer from the timers list.
Parameters:
  head - address of pointer to head of the timers list.
  prev - address of the timer before the timer that must be removed from the list.
  p - address of timer that must be removed from the list.
Returns:
  pointer to next cell or pointer to head if deleting is head
******************************************************************************/
Timer_t* halRemoveTimer(Timer_t **head, Timer_t *prev, Timer_t *p)
{
  Timer_t *t;

  if (p == *head)
  {// removing first element of list
    t = p->service.next;
    p->service.next = 0;
    *head = t;
    return *head;
  }
  else
  {
    prev->service.next = p->service.next;
    p->service.next = 0;
    return prev->service.next;
  }
}

/******************************************************************************
The search of the timer in the timers list before one.
Parameters:
  head - address of pointer to head of the timers list.
Returns:
  pointer to saerching timer
******************************************************************************/
Timer_t *halFindPrevTimer(Timer_t **head,  Timer_t *p)
{
  Timer_t *t = *head;

  for (; t ;)
  {
    if (t->service.next == p)
      return t;
    t = t->service.next;
  }
  return NULL;
}
//eof timer.c