summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/tms570/clock/clock.c
blob: 2a8bb5f4a6c80e66160edd80f508e83966a42719 (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
/**
 * @file clock.c
 *
 * @ingroup tms570
 *
 * @brief clock functions definitions.
 */

/*
 * Copyright (c) 2014 Premysl Houdek <kom541000@gmail.com>
 *
 * Google Summer of Code 2014 at
 * Czech Technical University in Prague
 * Zikova 1903/4
 * 166 36 Praha 6
 * Czech Republic
 *
 * Based on LPC24xx and LPC1768 BSP
 * by embedded brains GmbH and others
 *
 * 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.
 */

#include <stdlib.h>

#include <rtems.h>
#include <bsp.h>
#include <bsp/irq.h>
#include <bsp/tms570-rti.h>

/**
 *  holds HW counter value since last interrupt event
 *  sets in tms570_clock_driver_support_at_tick
 *  used in tms570_clock_driver_nanoseconds_since_last_tick
 */
static uint32_t tms570_rti_last_tick_fcr0;

/**
 *  @brief Initialize the HW peripheral for clock driver
 *
 *  Clock driver is implemented by RTI module
 *
 * @retval Void
 */
static void tms570_clock_driver_support_initialize_hardware( void )
{

  uint32_t microsec_per_tick = rtems_configuration_get_microseconds_per_tick();

  /* Hardware specific initialize */
  TMS570_RTI.RTIGCTRL = 0;
  TMS570_RTI.RTICPUC0 = BSP_PLL_OUT_CLOCK /1000000 / 2; /* prescaler */
  TMS570_RTI.RTITBCTRL = 2;
  TMS570_RTI.RTICAPCTRL = 0;
  TMS570_RTI.RTICOMPCTRL = 0;
  /* set counter to zero */
  TMS570_RTI.RTIUC0 = 0;
  TMS570_RTI.RTIFRC0 = 0;
  /* clear interrupts*/
  TMS570_RTI.RTICLEARINTENA = 0x00070f0f;
  TMS570_RTI.RTIINTFLAG = 0x0007000f;
  /* set timer */
  TMS570_RTI.RTICOMP0 = TMS570_RTI.RTIFRC0 + microsec_per_tick;
  TMS570_RTI.RTICOMP0CLR = TMS570_RTI.RTICOMP0 + microsec_per_tick / 2;
  TMS570_RTI.RTIUDCP0 = microsec_per_tick;
  /* enable interupt */
  TMS570_RTI.RTISETINTENA = 0x1;
  /* enable timer */
  TMS570_RTI.RTIGCTRL = 1;
}

/**
 * @brief Clears interrupt source
 *
 * @retval Void
 */
static void tms570_clock_driver_support_at_tick( void )
{
  TMS570_RTI.RTIINTFLAG = 0x00000001;
  tms570_rti_last_tick_fcr0 = TMS570_RTI.RTICOMP0 - TMS570_RTI.RTIUDCP0;
  /* TMS570_RTI.RTICOMP0 += 1000; */
}

/**
 * @brief registers RTI interrupt handler
 *
 * @param[in] Clock_isr new ISR handler
 * @param[in] Old_ticker old ISR handler (unused and type broken)
 *
 * @retval Void
 */
static void tms570_clock_driver_support_install_isr(
  rtems_isr_entry Clock_isr
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = rtems_interrupt_handler_install(
    TMS570_IRQ_TIMER_0,
    "Clock",
    RTEMS_INTERRUPT_UNIQUE,
    (rtems_interrupt_handler) Clock_isr,
    NULL
  );
  if ( sc != RTEMS_SUCCESSFUL ) {
    rtems_fatal_error_occurred(0xdeadbeef);
  }
}

/**
 * @brief disables RTI interrupt
 *
 * Called when closing clock driver
 *
 * @retval Void
 */
static void tms570_clock_driver_support_shutdown_hardware( void )
{
  /* turn off the timer interrupts */
  TMS570_RTI.RTICLEARINTENA = 0x20000;
}

/**
 * @brief returns the nanoseconds since last tick
 *
 * Return the nanoseconds since last tick
 *
 * @retval x nanoseconds
 *
 */
static uint32_t tms570_clock_driver_nanoseconds_since_last_tick( void )
{
  uint32_t actual_fcr0 = TMS570_RTI.RTIFRC0;
  uint32_t usec_since_tick;

  usec_since_tick = actual_fcr0 - tms570_rti_last_tick_fcr0;

  return usec_since_tick * 1000;
}

#define Clock_driver_support_initialize_hardware \
                        tms570_clock_driver_support_initialize_hardware
#define Clock_driver_support_at_tick \
                        tms570_clock_driver_support_at_tick
#define Clock_driver_support_initialize_hardware \
                        tms570_clock_driver_support_initialize_hardware
#define Clock_driver_support_shutdown_hardware \
                        tms570_clock_driver_support_shutdown_hardware
#define Clock_driver_nanoseconds_since_last_tick \
                        tms570_clock_driver_nanoseconds_since_last_tick

#define Clock_driver_support_install_isr(Clock_isr, Old_ticker ) \
              tms570_clock_driver_support_install_isr( Clock_isr )

void Clock_isr(void *arg); /* to supress warning */

#include "../../../shared/clockdrv_shell.h"