source: rtems/c/src/lib/libbsp/arm/tms570/clock/clock.c @ b2c252b

5
Last change on this file since b2c252b was b2c252b, checked in by Premysl Houdek <kom541000@…>, on 07/17/15 at 15:04:06

bsp/tms570 Use bitfields instead of hard-coded values

Signed-off-by: Premysl Houdek <kom541000@…>

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/**
2 * @file clock.c
3 *
4 * @ingroup tms570
5 *
6 * @brief clock functions definitions.
7 */
8
9/*
10 * Copyright (c) 2014 Premysl Houdek <kom541000@gmail.com>
11 *
12 * Google Summer of Code 2014 at
13 * Czech Technical University in Prague
14 * Zikova 1903/4
15 * 166 36 Praha 6
16 * Czech Republic
17 *
18 * Based on LPC24xx and LPC1768 BSP
19 * by embedded brains GmbH and others
20 *
21 * The license and distribution terms for this file may be
22 * found in the file LICENSE in this distribution or at
23 * http://www.rtems.org/license/LICENSE.
24 */
25
26#include <stdlib.h>
27
28#include <rtems.h>
29#include <bsp.h>
30#include <bsp/irq.h>
31#include <bsp/tms570-rti.h>
32#include <rtems/counter.h>
33#include <rtems/timecounter.h>
34
35static struct timecounter tms570_rti_tc;
36
37static uint32_t tms570_rti_get_timecount(struct timecounter *tc)
38{
39  return TMS570_RTI.CNT[0].FRCx;
40}
41
42#ifndef TMS570_PREFERRED_TC_FREQUENCY
43/*
44 * Define preferred main time base counter frequency
45 * The value of 1MHz is the best matching RTEMS
46 * timing system because then there is no need
47 * to scale RTEMS configuration microseconds_per_tick
48 * parameter
49 */
50#define TMS570_PREFERRED_TC_FREQUENCY 1000000
51#endif /* TMS570_PREFERRED_TC_FREQUENCY */
52
53/**
54 *  @brief Initialize the HW peripheral for clock driver
55 *
56 *  Clock driver is implemented by RTI module
57 *
58 * @retval Void
59 */
60static void tms570_clock_driver_support_initialize_hardware( void )
61{
62
63  uint32_t microsec_per_tick;
64  uint32_t tc_frequency;
65  uint32_t tc_prescaler;
66  uint32_t tc_increments_per_tick;
67
68  microsec_per_tick = rtems_configuration_get_microseconds_per_tick();
69  tc_frequency = TMS570_PREFERRED_TC_FREQUENCY;
70
71  rtems_counter_initialize_converter(BSP_PLL_OUT_CLOCK);
72
73  tc_prescaler = (BSP_PLL_OUT_CLOCK + tc_frequency) / (tc_frequency * 2);
74
75  /* Recompute actual most close frequency which can be realized */
76  tc_frequency = (BSP_PLL_OUT_CLOCK + tc_prescaler) / (tc_prescaler * 2);
77
78  /*
79   * Recompute tick period to adjust for configurable or exact
80   * preferred time base 1 usec resolution.
81   */
82  tc_increments_per_tick = ((uint64_t)microsec_per_tick * tc_frequency +
83                           500000) / 1000000;
84
85  /* Hardware specific initialize */
86  TMS570_RTI.GCTRL = 0;
87  TMS570_RTI.CNT[0].CPUCx = tc_prescaler - 1;
88  TMS570_RTI.TBCTRL = TMS570_RTI_TBCTRL_INC;
89  TMS570_RTI.CAPCTRL = 0;
90  TMS570_RTI.COMPCTRL = 0;
91  /* set counter to zero */
92  TMS570_RTI.CNT[0].UCx = 0;
93  TMS570_RTI.CNT[0].FRCx = 0;
94  /* clear interrupts*/
95  TMS570_RTI.CLEARINTENA = TMS570_RTI_CLEARINTENA_CLEAROVL1INT |
96                           TMS570_RTI_CLEARINTENA_CLEAROVL0INT |
97                           TMS570_RTI_CLEARINTENA_CLEARTBINT |
98                           TMS570_RTI_CLEARINTENA_CLEARDMA3 |
99                           TMS570_RTI_CLEARINTENA_CLEARDMA2 |
100                           TMS570_RTI_CLEARINTENA_CLEARDMA1 |
101                           TMS570_RTI_CLEARINTENA_CLEARDMA0 |
102                           TMS570_RTI_CLEARINTENA_CLEARINT3 |
103                           TMS570_RTI_CLEARINTENA_CLEARINT2 |
104                           TMS570_RTI_CLEARINTENA_CLEARINT1 |
105                           TMS570_RTI_CLEARINTENA_CLEARINT0;
106  TMS570_RTI.INTFLAG = TMS570_RTI_INTFLAG_OVL1INT |
107                       TMS570_RTI_INTFLAG_OVL0INT |
108                       TMS570_RTI_INTFLAG_TBINT |
109                       TMS570_RTI_INTFLAG_INT3 |
110                       TMS570_RTI_INTFLAG_INT2 |
111                       TMS570_RTI_INTFLAG_INT1 |
112                       TMS570_RTI_INTFLAG_INT0;
113  /* set timer */
114  TMS570_RTI.CMP[0].COMPx = TMS570_RTI.CNT[0].FRCx + tc_increments_per_tick;
115  TMS570_RTI.COMP0CLR = TMS570_RTI.CMP[0].COMPx + tc_increments_per_tick / 2;
116  TMS570_RTI.CMP[0].UDCPx = tc_increments_per_tick;
117  /* enable interupt */
118  TMS570_RTI.SETINTENA = TMS570_RTI_SETINTENA_SETINT0;
119  /* enable timer */
120  TMS570_RTI.GCTRL = TMS570_RTI_GCTRL_CNT0EN;
121  /* set timecounter */
122  tms570_rti_tc.tc_get_timecount = tms570_rti_get_timecount;
123  tms570_rti_tc.tc_counter_mask = 0xffffffff;
124  tms570_rti_tc.tc_frequency = tc_frequency;
125  tms570_rti_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
126  rtems_timecounter_install(&tms570_rti_tc);
127}
128
129/**
130 * @brief Clears interrupt source
131 *
132 * @retval Void
133 */
134static void tms570_clock_driver_support_at_tick( void )
135{
136  TMS570_RTI.INTFLAG = TMS570_RTI_INTFLAG_INT0;
137}
138
139/**
140 * @brief registers RTI interrupt handler
141 *
142 * @param[in] Clock_isr new ISR handler
143 * @param[in] Old_ticker old ISR handler (unused and type broken)
144 *
145 * @retval Void
146 */
147static void tms570_clock_driver_support_install_isr(
148  rtems_isr_entry Clock_isr
149)
150{
151  rtems_status_code sc = RTEMS_SUCCESSFUL;
152
153  sc = rtems_interrupt_handler_install(
154    TMS570_IRQ_TIMER_0,
155    "Clock",
156    RTEMS_INTERRUPT_UNIQUE,
157    (rtems_interrupt_handler) Clock_isr,
158    NULL
159  );
160  if ( sc != RTEMS_SUCCESSFUL ) {
161    rtems_fatal_error_occurred(0xdeadbeef);
162  }
163}
164
165/**
166 * @brief disables RTI interrupt
167 *
168 * Called when closing clock driver
169 *
170 * @retval Void
171 */
172static void tms570_clock_driver_support_shutdown_hardware( void )
173{
174  /* turn off the timer interrupts */
175  TMS570_RTI.CLEARINTENA = TMS570_RTI_CLEARINTENA_CLEAROVL0INT |
176                           TMS570_RTI_CLEARINTENA_CLEARINT0; 
177}
178
179#define Clock_driver_support_initialize_hardware \
180                        tms570_clock_driver_support_initialize_hardware
181#define Clock_driver_support_at_tick \
182                        tms570_clock_driver_support_at_tick
183#define Clock_driver_support_initialize_hardware \
184                        tms570_clock_driver_support_initialize_hardware
185#define Clock_driver_support_shutdown_hardware \
186                        tms570_clock_driver_support_shutdown_hardware
187
188#define Clock_driver_support_install_isr(Clock_isr, Old_ticker ) \
189              tms570_clock_driver_support_install_isr( Clock_isr )
190
191void Clock_isr(void *arg); /* to supress warning */
192
193#include "../../../shared/clockdrv_shell.h"
Note: See TracBrowser for help on using the repository browser.