summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/rcb230/src/halMacIsr.c
blob: e622372dc2405edf7eca08b1a2b0c79f89cd866c (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**************************************************************************//**
  \file  halMacIsr.c

  \brief mac interrupts implementation.

  \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:
      14/01/08 A. Mandychev - Created.
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/

/******************************************************************************
                        Includes section.
******************************************************************************/
#include <halMacIsr.h>
#include <halRfCtrl.h>
#include <atomic.h>
#include <halDiagnostic.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
#if defined(HAL_3d6864MHz)
  #define HAL_RTIMER_INTERVAL_CALCULATE(period)    (period >> 1)
#elif defined(HAL_4MHz)
  #define HAL_RTIMER_INTERVAL_CALCULATE(period)    (period >> 1)
#elif defined(HAL_7d3728MHz)
  #define HAL_RTIMER_INTERVAL_CALCULATE(period)    (period)
#elif defined(HAL_8MHz)
  #define HAL_RTIMER_INTERVAL_CALCULATE(period)    (period)
#endif

/******************************************************************************
                   Global variables section
******************************************************************************/
RTimerDescr_t __rtimer;

/******************************************************************************
                    Prototypes section
******************************************************************************/
/******************************************************************************
  Initializes Rtimer.
******************************************************************************/
void HAL_InitMacIsr(void);

/******************************************************************************
  Redirect interrupt event depending on the TrxState.
  Parameters: none.
  Returns: none.
******************************************************************************/
void phyDispatcheRTimerEvent(void);

/******************************************************************************
  Redirect interrupt event depending on the TrxState.
  Parameters: none.
  Returns: none.
******************************************************************************/
void phyDispatcheRfInterrupt(void);

/******************************************************************************
                    Implementations section
******************************************************************************/
/******************************************************************************
  Initializes Rtimer.
******************************************************************************/
void HAL_InitMacIsr(void)
{
  __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
  HAL_InitRfIrq();
}

/******************************************************************************
  Starts RTimer. Function should be invoked in critical section.
  Parameters:
    source  - source of invocation.
    mode    - RTimer mode.
    period  - RTimer period.
******************************************************************************/
bool HAL_StartRtimer(HAL_RTimerMode_t mode, uint16_t period)
{
  if (HAL_RTIMER_STOPPED_MODE != __rtimer.mode)
    return false;

  __rtimer.period    = HAL_RTIMER_INTERVAL_CALCULATE(period);
  __rtimer.mode      = mode;
  __rtimer.nextEvent = TCNT4 + __rtimer.period;
  if (__rtimer.nextEvent > TOP_TIMER_COUNTER_VALUE)
    __rtimer.nextEvent -= TOP_TIMER_COUNTER_VALUE;
  OCR4B = __rtimer.nextEvent;
  // clear possible interrupt by setting logical one.
  TIFR4 = (1 << OCF4B);
  // enable interrupt
  TIMSK4 |= (1 << OCIE4B);
  return true;
}

/******************************************************************************
  Stops RTimer. Function should be invoked in critical section.
******************************************************************************/
void HAL_StopRtimer(void)
{
  // clear possible interrupt
  TIFR4 &= ~(1 << OCF4B);
  // disable interrupt
  TIMSK4 &= ~(1 << OCIE4B);
  __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
}

/******************************************************************************
  Output compare unit (channel B) interrupt handler.
******************************************************************************/
ISR(TIMER4_COMPB_vect)
{
  BEGIN_MEASURE
  if (HAL_RTIMER_ONE_SHOT_MODE == __rtimer.mode)
  {
    TIMSK4 &= ~(1 << OCIE4B);
    __rtimer.mode = HAL_RTIMER_STOPPED_MODE;
  }
  else
  {
    __rtimer.nextEvent += __rtimer.period;
    if (__rtimer.nextEvent > TOP_TIMER_COUNTER_VALUE)
      __rtimer.nextEvent -= TOP_TIMER_COUNTER_VALUE;
    OCR4B = __rtimer.nextEvent;
  }
  phyDispatcheRTimerEvent();
  END_MEASURE(HALISR_TIMER3_COMPA_TIME_LIMIT)
}

/****************************************************************
  Interrupt service routine.
  Do not move this ISR! It could be omitted in your project.
****************************************************************/
ISR(TIMER1_CAPT_vect)
{
  BEGIN_MEASURE
  phyDispatcheRfInterrupt();
  END_MEASURE(HALISR_PHYDISPATCH_RFINT_TIME_LIMIT)
}

// eof halMacIsr.c