Blame view

RIOT/cpu/sam3/periph/timer.c 4.58 KB
a752c7ab   elopes   add first test an...
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  /*
   * Copyright (C) 2014-2016 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     cpu_sam3
   * @ingroup     drivers_periph_timer
   * @{
   *
   * @file
   * @brief       Low-level timer driver implementation
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdlib.h>
  #include <stdio.h>
  
  #include "board.h"
  #include "cpu.h"
  #include "periph/timer.h"
  #include "periph_conf.h"
  
  /**
   * @brief   Memory to store the interrupt context
   */
  static timer_isr_ctx_t isr_ctx[TIMER_NUMOF];
  
  /**
   * @brief   Enable the clock for the selected timer channels
   */
  static inline void clk_en(tim_t tim)
  {
      uint8_t id = timer_config[tim].id_ch0;
  
      if (id < 32) {
          PMC->PMC_PCER0 = ((1 << id) | (1 << (id + 1)));
      } else {
          id -= 32;
          PMC->PMC_PCER1 = ((1 << id) | (1 << (id + 1)));
      }
  }
  
  /**
   * @brief   Get the timer ID from the timer's base address
   */
  static inline Tc *dev(tim_t tim)
  {
      return timer_config[tim].dev;
  }
  
  /**
   * @brief   Setup the given timer
   *
   * The SAM3X8E has 3 timers build of 3 independent channels. Each of these
   * channels has 3 capture compare outputs (A-C).
   *
   * RIOT uses the 2 of the channels in WAVE mode with the following clock
   * chaining:
   *
   * ----------                ----------
   * |        |                |        |-> IRQ-compareA
   * | TCx[1] | ---- TIOA1 --->| TCx[0] |-> IRQ-compareB
   * |        |                |        |-> IRQ-compareC
   * ----------                ----------
   *     ^
   * TIMER_CLOCK1
   *
   * For each timer, channel 1 is used to implement a prescaler. Channel 1 is
   * driven by the MCK / 2 (42MHz) (TIMER_CLOCK1).
   */
  int timer_init(tim_t tim, unsigned long freq, timer_cb_t cb, void *arg)
  {
      /* check if device is valid */
      if (tim >= TIMER_NUMOF) {
          return -1;
      }
  
      /* enable the device clock */
      clk_en(tim);
  
      /* save callback */
      isr_ctx[tim].cb = cb;
      isr_ctx[tim].arg = arg;
  
      /* configure the timer block by connecting TIOA1 to XC0 */
      dev(tim)->TC_BMR = TC_BMR_TC0XC0S_TIOA1;
      /* configure and enable channel 0 to use XC0 as input */
      dev(tim)->TC_CHANNEL[0].TC_CMR = (TC_CMR_TCCLKS_XC0 |
                                   TC_CMR_WAVE | TC_CMR_EEVT_XC0);
      dev(tim)->TC_CHANNEL[0].TC_CCR = (TC_CCR_CLKEN | TC_CCR_SWTRG);
  
      /* configure channel 1:
       * - select wave mode
       * - set input clock to TIMER_CLOCK1 (MCK/2)
       * - reload on TC_CV == TC_RC
       * - let TIOA2 signal be toggled when TC_CV == TC_RC
       */
      dev(tim)->TC_CHANNEL[1].TC_CMR = (TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_WAVE |
                                        TC_CMR_WAVSEL_UP_RC | TC_CMR_ACPC_TOGGLE);
  
      /* configure the frequency of channel 1 to freq * 4
       *
       * note: as channel 0 is only incremented on rising edges of TIOA1 line and
       * channel 1 toggles this line on each timer tick, the actual frequency
       * driving channel 0 is f_ch2 / 2 --> f_ch0/1 = (MCK / 2) / 2 / freq.
       */
      dev(tim)->TC_CHANNEL[1].TC_RC = (CLOCK_CORECLOCK / 4) / freq;
  
      /* start channel 1 */
      dev(tim)->TC_CHANNEL[1].TC_CCR = (TC_CCR_CLKEN | TC_CCR_SWTRG);
  
      /* enable global interrupts for given timer */
      NVIC_EnableIRQ(timer_config[tim].id_ch0);
  
      return 0;
  }
  
  int timer_set_absolute(tim_t tim, int channel, unsigned int value)
  {
      if (channel >= TIMER_CHANNELS) {
          return -1;
      }
      (&dev(tim)->TC_CHANNEL[0].TC_RA)[channel] = value;
      dev(tim)->TC_CHANNEL[0].TC_IER = (TC_IER_CPAS << channel);
  
      return 0;
  }
  
  int timer_clear(tim_t tim, int channel)
  {
      if (channel >= TIMER_CHANNELS) {
          return -1;
      }
  
      dev(tim)->TC_CHANNEL[0].TC_IDR = (TC_IDR_CPAS << channel);
  
      return 1;
  }
  
  unsigned int timer_read(tim_t tim)
  {
      return dev(tim)->TC_CHANNEL[0].TC_CV;
  }
  
  void timer_start(tim_t tim)
  {
      dev(tim)->TC_CHANNEL[1].TC_CCR = (TC_CCR_CLKEN | TC_CCR_SWTRG);
  }
  
  void timer_stop(tim_t tim)
  {
      dev(tim)->TC_CHANNEL[1].TC_CCR = TC_CCR_CLKDIS;
  }
  
  static inline void isr_handler(tim_t tim)
  {
      uint32_t status = dev(tim)->TC_CHANNEL[0].TC_SR;
  
      for (int i = 0; i < TIMER_CHANNELS; i++) {
          if (status & (TC_SR_CPAS << i)) {
              dev(tim)->TC_CHANNEL[0].TC_IDR = (TC_IDR_CPAS << i);
              isr_ctx[tim].cb(isr_ctx[tim].arg, i);
          }
      }
  
      cortexm_isr_end();
  }
  
  #ifdef TIMER_0_ISR
  void TIMER_0_ISR(void)
  {
      isr_handler(0);
  }
  #endif
  
  #ifdef TIMER_1_ISR
  void TIMER_1_ISR(void)
  {
      isr_handler(1);
  }
  #endif
  
  #ifdef TIMER_2_ISR
  void TIMER_2_ISR(void)
  {
      isr_handler(2);
  }
  #endif