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

5
Last change on this file since dc95663e was dc95663e, checked in by Sebastian Huber <sebastian.huber@…>, on 03/09/17 at 13:14:42

bsp/tms570: Fix CPU counter frequency

The CPU counter runs with the processor frequency. Use
RTEMS_SYSINIT_ITEM() to initialize the CPU counter.

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