summaryrefslogtreecommitdiff
path: root/c/src/lib/libbsp/arm/xm-tms570/clock/clock.c
blob: 379376b09d71151c492dd40ff5eeaee4848f8666 (plain)
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
/*
 * @file clock.c
 *
 * @author Miguel Masmano <mmasmano@fentiss.com>
 *
 * @copyright
 * Copyright 2016 Fent Innovative Software Solutions (FENTISS). 
 * All rights reserved.
 *
 * Copyright 2017 embedded brains GmbH.
 *
 * 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 <rtems/timecounter.h>

#include <bsp.h>
#include <bsp/irq.h>
#include <bsp/fatal.h>
#include <xm.h>

static struct timecounter xm_tms570_tc;

static uint32_t xm_tms570_clock_last;

static uint32_t xm_tms570_clock_get_time(void)
{
  xmTime_t now;
  XM_get_time(XM_HW_CLOCK, &now);
  return (uint32_t) now;
}

static uint32_t xm_tms570_clock_get_timecount(struct timecounter *tc)
{
  (void) tc;
  return xm_tms570_clock_get_time();
}

static void xm_tms570_clock_initialize_hardware(void)
{
  xmTime_t now;
  uint32_t us_per_tick;

  XM_get_time(XM_HW_CLOCK, &now);
  us_per_tick = rtems_configuration_get_microseconds_per_tick();
  XM_set_timer(XM_HW_CLOCK, now + us_per_tick, us_per_tick);
  xm_tms570_clock_last = (uint32_t) now;

  xm_tms570_tc.tc_get_timecount = xm_tms570_clock_get_timecount;
  xm_tms570_tc.tc_counter_mask = 0xffffffff;
  xm_tms570_tc.tc_frequency = 1000000;
  xm_tms570_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
  rtems_timecounter_install(&xm_tms570_tc);
}

static void xm_tms570_clock_timecounter_tick(void)
{
  uint32_t us_per_tick;
  uint32_t now;
  uint32_t last;

  now = xm_tms570_clock_get_time();
  us_per_tick = rtems_configuration_get_microseconds_per_tick();
  last = xm_tms570_clock_last;

  while ((now - last) >= us_per_tick) {
    rtems_timecounter_tick();
    last += us_per_tick;
  }

  xm_tms570_clock_last = last;
}

static void xm_tms570_clock_shutdown_hardware(void)
{
  XM_set_irqmask(0, 1U << XM_VT_EXT_HW_TIMER);
  XM_set_timer(XM_HW_CLOCK, 0, 0);
}

static void xm_tms570_clock_install_isr(rtems_interrupt_handler handler)
{
  rtems_status_code sc;

  sc = rtems_interrupt_handler_install(
    XM_TMS570_IRQ_HWTIMER,
    "Clock",
    RTEMS_INTERRUPT_UNIQUE,
    handler,
    NULL
  );
  if (sc != RTEMS_SUCCESSFUL) {
    bsp_fatal(XM_BSP_FATAL_TMS570_CLOCK_IRQ_INSTALL);
  }
}

#define Clock_driver_support_initialize_hardware \
  xm_tms570_clock_initialize_hardware

#define Clock_driver_timecounter_tick() \
  xm_tms570_clock_timecounter_tick()

#define Clock_driver_support_initialize_hardware \
  xm_tms570_clock_initialize_hardware

#define Clock_driver_support_shutdown_hardware \
  xm_tms570_clock_shutdown_hardware

#define Clock_driver_support_install_isr(clock_isr, old_isr) \
  xm_tms570_clock_install_isr(clock_isr)

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