summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/lpc24xx/clock/clock-config.c
blob: 26ac30d72c8d9001e6b6ed37d42010543accf3e5 (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
/**
 * @file
 *
 * @ingroup lpc24xx
 *
 * @brief Clock driver configuration.
 */

/*
 * Copyright (c) 2008
 * Embedded Brains GmbH
 * Obere Lagerstr. 30
 * D-82178 Puchheim
 * Germany
 * rtems@embedded-brains.de
 *
 * The license and distribution terms for this file may be found in the file
 * LICENSE in this distribution or at http://www.rtems.com/license/LICENSE.
 */

#define RTEMS_STATUS_CHECKS_USE_PRINTK

#include <rtems/status-checks.h>

#include <bsp.h>
#include <bsp/lpc24xx.h>
#include <bsp/irq.h>
#include <bsp/system-clocks.h>

/* This is defined in ../../../shared/clockdrv_shell.h */
rtems_isr Clock_isr( rtems_vector_number vector);

#define Clock_driver_support_at_tick() \
 do { \
   T0IR = TIR_MR0; \
 } while (0)

static void lpc24xx_clock_handler_install( void)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = rtems_interrupt_handler_install(
    LPC24XX_IRQ_TIMER_0,
    "Clock",
    RTEMS_INTERRUPT_UNIQUE,
    (rtems_interrupt_handler) Clock_isr,
    NULL
  );
  RTEMS_CHECK_SC_VOID( sc, "Install clock interrupt handler");
}

static void lpc24xx_clock_initialize( void)
{
  rtems_interrupt_level level;
  uint64_t interval = ((uint64_t) lpc24xx_cclk()
    * (uint64_t) rtems_configuration_get_microseconds_per_tick()) / 1000000;

  /* Set timer pclk to cclk */
  rtems_interrupt_disable( level);
  PCONP = SET_FLAGS( PCONP, 0x02);
  PCLKSEL0 = SET_FLAGS( PCLKSEL0, 0x04);
  rtems_interrupt_enable( level);

  /* Reset timer */
  T0TCR = TCR_RST;

  /* Clear interrupt flags */
  T0IR = 0xff;

  /* Set timer mode */
  T0CCR = 0;

  /* Timer is incremented every pclk tick */
  T0PR = 0;

  /* Set match registers */
  T0MR0 = (uint32_t) interval;
  T0MR1 = 0xdeadbeef;
  T0MR2 = 0xdeadbeef;
  T0MR3 = 0xdeadbeef;

  /* Generate interrupt and reset counter on match with MR0 */
  T0MCR = TMCR_MR0I | TMCR_MR0R;

  /* No external match */
  T0EMR = 0;

  /* Enable timer */
  T0TCR = TCR_EN;
}

static void lpc24xx_clock_cleanup( void)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  /* Disable timer */
  T0TCR = 0;

  /* Remove interrupt handler */
  sc = rtems_interrupt_handler_remove(
    LPC24XX_IRQ_TIMER_0,
    (rtems_interrupt_handler) Clock_isr,
    NULL
  );
  RTEMS_CHECK_SC_VOID( sc, "Remove clock interrupt handler");
}

static uint32_t lpc24xx_clock_nanoseconds_since_last_tick( void)
{
  uint64_t clock = LPC24XX_CCLK;
  uint32_t clicks = T0TC;
  uint64_t ns = ((uint64_t) clicks * 1000000000) / clock;
  
  return (uint32_t) ns;
}
 
#define Clock_driver_support_initialize_hardware() lpc24xx_clock_initialize()

#define Clock_driver_support_install_isr( isr, old_isr) lpc24xx_clock_handler_install()

#define Clock_driver_support_shutdown_hardware() lpc24xx_clock_cleanup()
  
#define Clock_driver_nanoseconds_since_last_tick lpc24xx_clock_nanoseconds_since_last_tick

/* Include shared source clock driver code */
#include "../../../../libbsp/shared/clockdrv_shell.h"