source: rtems/c/src/lib/libcpu/arm/lpc22xx/clock/clockdrv.c @ 75acd9e

4.115
Last change on this file since 75acd9e was 75acd9e, checked in by Alexander Krutwig <alexander.krutwig@…>, on 04/01/15 at 13:33:25

bsps: Convert clock drivers to use a timecounter

Update #2271.

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 *  LPC22XX/LPC21xx clock specific using the System Timer
3 *
4 *  Set the Time0 to generate click for RTEMS
5 */
6
7/*
8 *  Copyright (c) 2006 by Ray <rayx.cn@gmail.com>
9 *
10 *  The license and distribution terms for this file may be
11 *  found in the file LICENSE in this distribution or at
12 *  http://www.rtems.org/license/LICENSE.
13 */
14
15#include <rtems.h>
16#include <bsp.h>
17#include <bsp/irq.h>
18#include <lpc22xx.h>
19#include <rtems/bspIo.h>  /* for printk */
20#include <rtems/timecounter.h>
21
22void Clock_isr(rtems_irq_hdl_param arg);
23static void clock_isr_on(const rtems_irq_connect_data *unused);
24static void clock_isr_off(const rtems_irq_connect_data *unused);
25static int clock_isr_is_on(const rtems_irq_connect_data *irq);
26
27static rtems_timecounter_simple lpc22xx_tc;
28
29static uint32_t lpc22xx_tc_get(rtems_timecounter_simple *tc)
30{
31  return T0TC;
32}
33
34static bool lpc22xx_tc_is_pending(rtems_timecounter_simple *tc)
35{
36  return (T0IR & 0x1) != 0;
37}
38
39static uint32_t lpc22xx_tc_get_timecount(struct timecounter *tc)
40{
41  return rtems_timecounter_simple_upcounter_get(
42    tc,
43    lpc22xx_tc_get,
44    lpc22xx_tc_is_pending
45  );
46}
47
48static void lpc22xx_tc_tick(void)
49{
50  rtems_timecounter_simple_upcounter_tick(&lpc22xx_tc, lpc22xx_tc_get);
51}
52
53/* Replace the first value with the clock's interrupt name. */
54rtems_irq_connect_data clock_isr_data = {
55  .name   = LPC22xx_INTERRUPT_TIMER0,
56  .hdl    = Clock_isr,
57  .handle = NULL,
58  .on     = clock_isr_on,
59  .off    = clock_isr_off,
60  .isOn   = clock_isr_is_on,
61};
62
63/* use the /shared/clockdrv_shell.h code template */
64
65/**
66 * When we get the clock interrupt
67 *    - clear the interrupt bit?
68 *    - restart the timer?
69 */
70#define Clock_driver_support_at_tick() \
71 do {                                  \
72   if (!(T0IR & 0x01))                 \
73     return;                           \
74   T0IR = 0x01;                        \
75   VICVectAddr = 0x00;                 \
76 } while(0)
77
78/**
79 * Installs the clock ISR. You shouldn't need to change this.
80 */
81#define Clock_driver_support_install_isr( _new, _old ) \
82  do {                                                 \
83    (_old) = NULL;                                   \
84    BSP_install_rtems_irq_handler(&clock_isr_data);  \
85  } while(0)
86
87/**
88 * Initialize the hardware for the clock
89 *   - Set the frequency
90 *   - enable it
91 *   - clear any pending interrupts
92 *
93 * Since you may want the clock always running, you can
94 * enable interrupts here. If you do so, the clock_isr_on(),
95 * clock_isr_off(), and clock_isr_is_on() functions can be
96 * NOPs.
97 *
98 * set timer to generate interrupt every
99 * rtems_configuration_get_microseconds_per_tick()
100 *     MR0/(LPC22xx_Fpclk/(PR0+1)) = 10/1000 = 0.01s
101 */
102#define Clock_driver_support_initialize_hardware() \
103  do { \
104    uint32_t mask; \
105    /* disable and clear timer 0, set to  */ \
106    T0TCR &= 0;                              \
107    /* TC is incremented on every pclk.*/    \
108    T0PC   = 0;                              \
109    /* initialize the timer period and prescaler */  \
110    T0MR0  = ((LPC22xx_Fpclk/1000 *          \
111             rtems_configuration_get_microseconds_per_tick()) / 1000); \
112    /* generate interrupt when T0MR0 match T0TC and Reset Timer Count*/ \
113    T0MCR |= 0x03;          \
114    /* No external match */ \
115    T0EMR = 0;              \
116    /* enable timer0 */     \
117    T0TCR = 1;              \
118    /* enable interrupt, skyeye will check this*/ \
119    T0IR |= 0x01; \
120    /* install timecounter */ \
121    rtems_timecounter_simple_install( \
122      &lpc22xx_tc, \
123      LPC22xx_Fpclk, \
124      T0MR0, \
125      lpc22xx_tc_get_timecount \
126    ); \
127  } while (0)
128
129/**
130 * Do whatever you need to shut the clock down and remove the
131 * interrupt handler. Since this normally only gets called on
132 * RTEMS shutdown, you may not need to do anything other than
133 * remove the ISR.
134 */
135#define Clock_driver_support_shutdown_hardware()                        \
136  do {                                                                  \
137    /* Disable timer */ \
138    T0TCR&=~0x02; \
139    BSP_remove_rtems_irq_handler(&clock_isr_data);                  \
140  } while (0)
141
142/**
143 * Enables clock interrupt.
144 *
145 * If the interrupt is always on, this can be a NOP.
146 */
147static void clock_isr_on(const rtems_irq_connect_data *unused)
148{
149  T0IR&=0x01;
150}
151
152/**
153 * Disables clock interrupts
154 *
155 * If the interrupt is always on, this can be a NOP.
156 */
157static void clock_isr_off(const rtems_irq_connect_data *unused)
158{
159  T0IR=0x00;
160}
161
162/**
163 * Tests to see if clock interrupt is enabled, and returns 1 if so.
164 * If interrupt is not enabled, returns 0.
165 *
166 * If the interrupt is always on, this always returns 1.
167 */
168static int clock_isr_is_on(const rtems_irq_connect_data *irq)
169{
170  return T0IR & 0x01;  /* MR0 mask */
171}
172
173#define Clock_driver_timecounter_tick() lpc22xx_tc_tick()
174
175/* Make sure to include this, and only at the end of the file */
176#include "../../../../libbsp/shared/clockdrv_shell.h"
177
Note: See TracBrowser for help on using the repository browser.