Blame view

RIOT/cpu/lpc11u34/periph/timer.c 3.88 KB
fb11e647   vrobic   reseau statique a...
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
  /*
   * Copyright (C) 2015 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_lpc11u34
   * @{
   *
   * @file
   * @brief       Implementation of the low-level timer driver for the LPC11U34
   *
   * @author      Paul RATHGEB <paul.rathgeb@skynet.be>
   * @}
   */
  
  #include <stdint.h>
  
  #include "cpu.h"
  #include "periph_conf.h"
  #include "periph/timer.h"
  
  /* guard file in case no timers are defined */
  #if TIMER_0_EN
  
  /**
   * @name Timer channel interrupt flags
   * @{
   */
  #define MR0_FLAG    (0x01)      /**< match for channel 0 */
  #define MR1_FLAG    (0x02)      /**< match for channel 1 */
  #define MR2_FLAG    (0x04)      /**< match for channel 2 */
  #define MR3_FLAG    (0x08)      /**< match for channel 3 */
  /** @} */
  
  /**
   * @brief UART device configurations
   */
  static timer_isr_ctx_t config[TIMER_NUMOF];
  
  int timer_init(tim_t dev, unsigned long freq, timer_cb_t cb, void *arg)
  {
      if (dev == TIMER_0) {
          /* save callback */
          config[TIMER_0].cb = cb;
          config[TIMER_0].arg = arg;
          /* enable power for timer */
          TIMER_0_CLKEN();
          /* set to timer mode */
          TIMER_0_DEV->CTCR = 0;
          /* configure prescaler */
          TIMER_0_DEV->PR = (TIMER_0_FREQ / freq) - 1;
          /* configure and enable timer interrupts */
          NVIC_SetPriority(TIMER_0_IRQ, TIMER_IRQ_PRIO);
          NVIC_EnableIRQ(TIMER_0_IRQ);
          /* enable timer */
          TIMER_0_DEV->TCR |= 1;
          return 0;
      }
      return -1;
  }
  
  int timer_set(tim_t dev, int channel, unsigned int timeout)
  {
      if (dev == TIMER_0) {
          unsigned int now = timer_read(dev);
          return timer_set_absolute(dev, channel, now + timeout);
      }
      return -1;
  }
  
  int timer_set_absolute(tim_t dev, int channel, unsigned int value)
  {
      if (dev == TIMER_0) {
          switch (channel) {
              case 0:
                  TIMER_0_DEV->MR0 = value;
                  break;
              case 1:
                  TIMER_0_DEV->MR1 = value;
                  break;
              case 2:
                  TIMER_0_DEV->MR2 = value;
                  break;
              case 3:
                  TIMER_0_DEV->MR3 = value;
                  break;
              default:
                  return -1;
          }
          TIMER_0_DEV->MCR |= (1 << (channel * 3));
          return 1;
      }
      return -1;
  }
  
  int timer_clear(tim_t dev, int channel)
  {
      if (dev == TIMER_0 && channel >= 0 && channel < TIMER_0_CHANNELS) {
          TIMER_0_DEV->MCR &= ~(1 << (channel * 3));
          return 1;
      }
      return -1;
  }
  
  unsigned int timer_read(tim_t dev)
  {
      if (dev == TIMER_0) {
          return (unsigned int)TIMER_0_DEV->TC;
      }
      return 0;
  }
  
  void timer_start(tim_t dev)
  {
      if (dev == TIMER_0) {
          TIMER_0_DEV->TCR |= 1;
      }
  }
  
  void timer_stop(tim_t dev)
  {
      if (dev == TIMER_0) {
          TIMER_0_DEV->TCR &= ~(1);
      }
  }
  
  void timer_irq_enable(tim_t dev)
  {
      if (dev == TIMER_0) {
          NVIC_EnableIRQ(TIMER_0_IRQ);
      }
  }
  
  void timer_irq_disable(tim_t dev)
  {
      if (dev == TIMER_0) {
          NVIC_DisableIRQ(TIMER_0_IRQ);
      }
  }
  
  void TIMER_0_ISR(void)
  {
      if (TIMER_0_DEV->IR & MR0_FLAG) {
          TIMER_0_DEV->IR |= (MR0_FLAG);
          TIMER_0_DEV->MCR &= ~(1 << 0);
          config[TIMER_0].cb(config[TIMER_0].arg, 0);
      }
      if (TIMER_0_DEV->IR & MR1_FLAG) {
          TIMER_0_DEV->IR |= (MR1_FLAG);
          TIMER_0_DEV->MCR &= ~(1 << 3);
          config[TIMER_0].cb(config[TIMER_0].arg, 1);
      }
      if (TIMER_0_DEV->IR & MR2_FLAG) {
          TIMER_0_DEV->IR |= (MR2_FLAG);
          TIMER_0_DEV->MCR &= ~(1 << 6);
          config[TIMER_0].cb(config[TIMER_0].arg, 2);
      }
      if (TIMER_0_DEV->IR & MR3_FLAG) {
          TIMER_0_DEV->IR |= (MR3_FLAG);
          TIMER_0_DEV->MCR &= ~(1 << 9);
          config[TIMER_0].cb(config[TIMER_0].arg, 3);
      }
      cortexm_isr_end();
  }
  
  #endif /* TIMER_0_EN */