summaryrefslogtreecommitdiffstats
path: root/bsps/arm/lpc176x/include/bsp/timer-defs.h
blob: 8ff1931bea0f3d57b1009a874331bcdaf709d7a3 (plain) (blame)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
 * @file
 *
 * @ingroup RTEMSBSPsARMLPC176X
 *
 * @brief API definitions of the for the timer of the lpc176x bsp.
 */

/*
 * Copyright (c) 2014 Taller Technologies.
 *
 * @author  Boretto Martin    (martin.boretto@tallertechnologies.com)
 * @author  Diaz Marcos (marcos.diaz@tallertechnologies.com)
 * @author  Lenarduzzi Federico  (federico.lenarduzzi@tallertechnologies.com)
 * @author  Daniel Chicco  (daniel.chicco@tallertechnologies.com)
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifndef LIBBSP_ARM_LPC176X_TIMER_DEFS_H
#define LIBBSP_ARM_LPC176X_TIMER_DEFS_H

#include <bsp/common-types.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* Timer 0 */
#define LPC176X_TMR0_BASE_ADDR 0x40004000U

#define LPC176X_T0IR ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                0x00U ) )
#define LPC176X_T0TCR ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x04U ) )
#define LPC176X_T0TC ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                0x08U ) )
#define LPC176X_T0PR ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                0x0CU ) )
#define LPC176X_T0PC ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                0x10U ) )
#define LPC176X_T0MCR ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x14U ) )
#define LPC176X_T0MR0 ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x18U ) )
#define LPC176X_T0MR1 ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x1CU ) )
#define LPC176X_T0MR2 ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x20U ) )
#define LPC176X_T0MR3 ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x24U ) )
#define LPC176X_T0CCR ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x28U ) )
#define LPC176X_T0CR0 ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x2CU ) )
#define LPC176X_T0CR1 ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x30U ) )
#define LPC176X_T0CR2 ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x34U ) )
#define LPC176X_T0CR3 ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x38U ) )
#define LPC176X_T0EMR ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                 0x3CU ) )
#define LPC176X_T0CTCR ( *(volatile uint32_t *) ( LPC176X_TMR0_BASE_ADDR + \
                                                  0x70U ) )

/* Timer 1 */
#define LPC176X_TMR1_BASE_ADDR 0x40008000U

#define LPC176X_T1IR ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                0x00U ) )
#define LPC176X_T1TCR ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x04U ) )
#define LPC176X_T1TC ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                0x08U ) )
#define LPC176X_T1PR ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                0x0CU ) )
#define LPC176X_T1PC ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                0x10U ) )
#define LPC176X_T1MCR ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x14U ) )
#define LPC176X_T1MR0 ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x18U ) )
#define LPC176X_T1MR1 ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x1CU ) )
#define LPC176X_T1MR2 ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x20U ) )
#define LPC176X_T1MR3 ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x24U ) )
#define LPC176X_T1CCR ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x28U ) )
#define LPC176X_T1CR0 ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x2CU ) )
#define LPC176X_T1CR1 ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x30U ) )
#define LPC176X_T1CR2 ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x34U ) )
#define LPC176X_T1CR3 ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x38U ) )
#define LPC176X_T1EMR ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                 0x3CU ) )
#define LPC176X_T1CTCR ( *(volatile uint32_t *) ( LPC176X_TMR1_BASE_ADDR + \
                                                  0x70U ) )

/* Timer 2 */
#define LPC176X_TMR2_BASE_ADDR 0x40090000U

#define LPC176X_T2IR ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                0x00U ) )
#define LPC176X_T2TCR ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x04U ) )
#define LPC176X_T2TC ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                0x08U ) )
#define LPC176X_T2PR ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                0x0CU ) )
#define LPC176X_T2PC ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                0x10U ) )
#define LPC176X_T2MCR ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x14U ) )
#define LPC176X_T2MR0 ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x18U ) )
#define LPC176X_T2MR1 ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x1CU ) )
#define LPC176X_T2MR2 ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x20U ) )
#define LPC176X_T2MR3 ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x24U ) )
#define LPC176X_T2CCR ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x28U ) )
#define LPC176X_T2CR0 ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x2CU ) )
#define LPC176X_T2CR1 ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x30U ) )
#define LPC176X_T2CR2 ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x34U ) )
#define LPC176X_T2CR3 ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x38U ) )
#define LPC176X_T2EMR ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                 0x3CU ) )
#define LPC176X_T2CTCR ( *(volatile uint32_t *) ( LPC176X_TMR2_BASE_ADDR + \
                                                  0x70U ) )

/* Timer 3 */
#define LPC176X_TMR3_BASE_ADDR 0x40094000U

#define LPC176X_T3IR ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                0x00U ) )
#define LPC176X_T3TCR ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x04U ) )
#define LPC176X_T3TC ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                0x08U ) )
#define LPC176X_T3PR ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                0x0CU ) )
#define LPC176X_T3PC ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                0x10U ) )
#define LPC176X_T3MCR ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x14U ) )
#define LPC176X_T3MR0 ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x18U ) )
#define LPC176X_T3MR1 ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x1CU ) )
#define LPC176X_T3MR2 ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x20U ) )
#define LPC176X_T3MR3 ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x24U ) )
#define LPC176X_T3CCR ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x28U ) )
#define LPC176X_T3CR0 ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x2CU ) )
#define LPC176X_T3CR1 ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x30U ) )
#define LPC176X_T3CR2 ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x34U ) )
#define LPC176X_T3CR3 ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x38U ) )
#define LPC176X_T3EMR ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                 0x3CU ) )
#define LPC176X_T3CTCR ( *(volatile uint32_t *) ( LPC176X_TMR3_BASE_ADDR + \
                                                  0x70U ) )

/**
 * @brief Represents the timer device registers.
 */
typedef struct {
  /**
   * @brief Interrupt Register.
   */
  volatile uint32_t IR;
  /**
   * @brief Timer Control Register.
   */
  volatile uint32_t TCR;
  /**
   * @brief Timer Counter.
   */
  volatile uint32_t TC;
  /**
   * @brief Prescale Register.
   */
  volatile uint32_t PR;
  /**
   * @brief Prescale Counter.
   */
  volatile uint32_t PC;
  /**
   * @brief Match Control Register.
   */
  volatile uint32_t MCR;
  /**
   * @brief Match Register (0, 1, 2, 3)
   */
  volatile uint32_t MR[ 4 ];
  /**
   * @brief Capture Control Register.
   */
  volatile uint32_t CCR;
  /**
   * @brief Capture Register (0, 1)
   */
  volatile uint32_t CR[ 2 ];
  volatile uint32_t reserved0;
  volatile uint32_t reserved1;
  /**
   * @brief External Match Register.
   */
  volatile uint32_t EMR;
  volatile uint32_t reserved2[ 12 ];
  /**
   * @brief Count Control Register.
   */
  volatile uint32_t CTCR;
} lpc176x_timer_device;

#define LPC176X_PIN_SELECT_TIMER 3U
#define LPC176X_PINSEL_NO_PORT 999U

#define LPC176X_TIMER_RESET ( 1U << 1U )
#define LPC176X_TIMER_START 1U
#define LPC176X_TIMER_MODE_COUNTER_SOURCE_CAP0 0U
#define LPC176X_TIMER_MODE_COUNTER_SOURCE_CAP1 ( 1U << 2U )
#define LPC176X_TIMER0_CAPTURE_PORTS { 58U, 59U }
#define LPC176X_TIMER1_CAPTURE_PORTS { 50U, 51U }
#define LPC176X_TIMER2_CAPTURE_PORTS { 4U, 5U }
#define LPC176X_TIMER3_CAPTURE_PORTS { 23U, 24U }
#define LPC176X_TIMER0_EMATCH_PORTS { 60U,   \
                                      61U,    \
                                      LPC176X_PINSEL_NO_PORT, \
                                      LPC176X_PINSEL_NO_PORT }
#define LPC176X_TIMER1_EMATCH_PORTS { 54U, \
                                      57U,    \
                                      LPC176X_PINSEL_NO_PORT, \
                                      LPC176X_PINSEL_NO_PORT }
#define LPC176X_TIMER2_EMATCH_PORTS { 6U, 7U, 8U, 9U }
#define LPC176X_TIMER3_EMATCH_PORTS { 10U, \
                                      11U, \
                                      LPC176X_PINSEL_NO_PORT, \
                                      LPC176X_PINSEL_NO_PORT }
#define LPC176X_TIMER_DEFAULT_RESOLUTION 1U
#define LPC176X_TIMER_MCR_MASK 7U
#define LPC176X_TIMER_MCR_MASK_SIZE 3U
#define LPC176X_TIMER_CCR_MASK 7U
#define LPC176X_TIMER_CCR_MASK_SIZE 3U
#define LPC176X_TIMER_EMR_MASK 3U
#define LPC176X_TIMER_EMR_MASK_SIZE 2U
#define LPC176X_TIMER_EMR_MASK_OFFSET 4U
#define LPC176X_TIMER_CLEAR_FUNCTION 0U
#define LPC176X_TIMER_PRESCALER_DIVISOR 1000000U
#define LPC176X_TIMER_VECTOR_NUMBER( timernumber ) ( timernumber + 1U )
#define LPC176X_TIMER_INTERRUPT_SOURCE_BIT( i ) ( 1U << i )
#define LPC176X_TIMER_MATCH_FUNCTION_COUNT 8U
#define LPC176X_TIMER_CAPTURE_FUNCTION_COUNT 8U

#define LPC176X_ISR_NAME_STRING_SIZE 10U

#define LPC176X_SET_MCR( mcr, match_port, function )  \
  SET_FIELD( mcr, \
  function, \
  ( 0x7U << ( 3U * match_port ) ), \
  ( 3U * match_port ) )
#define LPC176X_SET_CCR( mcr, capture_port, function )  \
  SET_FIELD( mcr, function, ( 0x7U << ( 3U * capture_port ) ), \
  ( 3U * capture_port ) )
#define LPC176X_SET_EMR( mcr, match_port, function )  \
  SET_FIELD( mcr, function, ( 0x3U << ( 2U * match_port + 4U ) ), \
  ( 2U * match_port + 4U ) )

/**
 * @brief Capture ports of a timer.
 *
 * Enumerated type to define the set of capture ports for a timer device.
 */
typedef enum {
  LPC176X_CAPn_0,
  LPC176X_CAPn_1,
  LPC176X_CAPTURE_PORTS_COUNT
} lpc176x_capture_port;

/**
 * @brief Match ports of a timer.
 *
 * Enumerated type to define the set of match ports for a timer device.
 */
typedef enum {
  LPC176X_MATn_0,
  LPC176X_MATn_1,
  LPC176X_MATn_2,
  LPC176X_MATn_3,
  LPC176X_EMATCH_PORTS_COUNT
} lpc176x_match_port;

/**
 * @brief Timer modes of a timer.
 *
 * Enumerated type to define the set of modes for a timer device.
 */
typedef enum {
  LPC176X_TIMER_MODE_TIMER,
  LPC176X_TIMER_MODE_COUNTER_RISING_CAP0,
  LPC176X_TIMER_MODE_COUNTER_FALLING_CAP0,
  LPC176X_TIMER_MODE_COUNTER_BOTH_CAP0,
  LPC176X_TIMER_MODE_COUNTER_RISING_CAP1 = ( 1U & ( 1U << 2U ) ),
  LPC176X_TIMER_MODE_COUNTER_FALLING_CAP1 = ( 2U & ( 1U << 2U ) ),
  LPC176X_TIMER_MODE_COUNTER_BOTH_CAP1 = ( 3U & ( 1U << 2U ) ),
} lpc176x_timer_mode;

/**
 * @brief The timer devices in the board.
 *
 * Enumerated type to define the timer device's numbers.
 */
typedef enum {
  LPC176X_TIMER_0,
  LPC176X_TIMER_1,
  LPC176X_TIMER_2,
  LPC176X_TIMER_3,
  LPC176X_TIMER_COUNT
} lpc176x_timer_number;

/**
 * @brief The index for the isr_funct_vector representing the functions
 *        that attends each possible interrupt source for a timer.
 *
 * Enumerated type to define the set of  isr timer functions .
 */
typedef enum {
  LPC176X_MAT0_ISR_FUNCTION,
  LPC176X_MAT1_ISR_FUNCTION,
  LPC176X_MAT2_ISR_FUNCTION,
  LPC176X_MAT3_ISR_FUNCTION,
  LPC176X_CAP0_ISR_FUNCTION,
  LPC176X_CAP1_ISR_FUNCTION,
  LPC176X_ISR_FUNCTIONS_COUNT
} lpc176x_isr_function;

/**
 * @brief The possible functions at match. This options could be
 *        used together.
 *
 * Enumerated type to define the set of  functions at mach for a
 *     timer device.
 */
typedef enum {
  LPC176X_TIMER_MATCH_FUNCTION_NONE = 0U,
  LPC176X_TIMER_MATCH_FUNCTION_INTERRUPT = 1U,
  LPC176X_TIMER_MATCH_FUNCTION_RESET = ( 1U << 1U ),
  LPC176X_TIMER_MATCH_FUNCTION_STOP = ( 1U << 2U )
} lpc176x_match_function;

/**
 * @brief The possible functions at capture. This options could
 *        be used together.
 *
 * Enumerated type to define the set of  functions at capture for
 *     a timer device.
 */
typedef enum {
  LPC176X_TIMER_CAPTURE_FUNCTION_NONE = 0U,
  LPC176X_TIMER_CAPTURE_FUNCTION_RISING = 1U,
  LPC176X_TIMER_CAPTURE_FUNCTION_FALLING = ( 1U << 1U ),
  LPC176X_TIMER_CAPTURE_FUNCTION_INTERRUPT = ( 1U << 2U )
} lpc176x_capture_function;

/**
 * @brief The possible functions at match, for the external ports.
 *
 * Enumerated type to define the set of  functions at match, for external
 *     ports, for a timer device.
 */
typedef enum {
  LPC176X_TIMER_EXTMATCH_FUNCTION_NONE,
  LPC176X_TIMER_EXTMATCH_FUNCTION_CLEAR,
  LPC176X_TIMER_EXTMATCH_FUNCTION_SET,
  LPC176X_TIMER_EXTMATCH_FUNCTION_TOGGLE
} lpc176x_ext_match_function;

/**
 * @brief A function that attends an interruption for a timer.
 *
 * @param  tnumber Timer number.
 * @return Pointer to the match function.
 */
typedef void (*lpc176x_isr_funct) ( const lpc176x_timer_number tnumber );

/**
 * @brief The vector of functions that attends each possible interrupt
 *        source for a timer.
 */
typedef lpc176x_isr_funct const lpc176x_isr_funct_vector[
    LPC176X_ISR_FUNCTIONS_COUNT ];

/**
 * @brief The Timer device representation.
 */
typedef struct {
  /**
   * @brief The address of the controlling registers for the timer.
   */
  lpc176x_timer_device *const device;
  /**
   * @brief The module for the RTEMS module starting (power and clock).
   */
  const lpc176x_module module;
  /**
   * @brief The Pins for the Capture ports of this timer.
   */
  const lpc176x_pin_number pinselcap[ LPC176X_CAPTURE_PORTS_COUNT ];
  /**
   * @brief The Pins for the external match ports of this timer.
   */
  const lpc176x_pin_number pinselemat[ LPC176X_EMATCH_PORTS_COUNT ];
} lpc176x_timer;

/**
 * @brief The Timer functions.
 */
typedef struct {
  /**
   * @brief The vector of isr functions for this timer.
   */
  const lpc176x_isr_funct_vector *funct_vector;
} lpc176x_timer_functions;

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* LIBBSP_ARM_LPC176X_TIMER_DEFS_H */