source: rtems/bsps/arm/lpc32xx/rtc/rtc-config.c @ 4fb1b79

5
Last change on this file since 4fb1b79 was 4fb1b79, checked in by Sebastian Huber <sebastian.huber@…>, on 04/23/18 at 07:55:15

bsps: Move RTC drivers to bsps

This patch is a part of the BSP source reorganization.

Update #3285.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup arm_lpc32xx
5 *
6 * @brief RTC configuration.
7 */
8
9/*
10 * Copyright (c) 2009-2011 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Obere Lagerstr. 30
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.org/license/LICENSE.
21 */
22
23#include <libchip/rtc.h>
24
25#include <bsp.h>
26#include <bsp/lpc32xx.h>
27
28#define LPC32XX_RTC_COUNT 1U
29
30#define LPC32XX_RTC_COUNTER_DELTA 0xfffffffeU
31
32#define LPC32XX_RTC_KEY 0xb5c13f27U
33
34#define LPC32XX_RTC_CTRL_FORCE_ONSW (1U << 7)
35#define LPC32XX_RTC_CTRL_STOP (1U << 6)
36#define LPC32XX_RTC_CTRL_RESET (1U << 4)
37#define LPC32XX_RTC_CTRL_MATCH_1_ONSW (1U << 3)
38#define LPC32XX_RTC_CTRL_MATCH_0_ONSW (1U << 2)
39#define LPC32XX_RTC_CTRL_MATCH_1_INTR (1U << 1)
40#define LPC32XX_RTC_CTRL_MATCH_0_INTR (1U << 0)
41
42static void lpc32xx_rtc_set(uint32_t val)
43{
44  unsigned i = lpc32xx_arm_clk() / LPC32XX_OSCILLATOR_RTC;
45
46  lpc32xx.rtc.ctrl |= LPC32XX_RTC_CTRL_STOP;
47  lpc32xx.rtc.ucount = val;
48  lpc32xx.rtc.dcount = LPC32XX_RTC_COUNTER_DELTA - val;
49  lpc32xx.rtc.ctrl &= ~LPC32XX_RTC_CTRL_STOP;
50
51  /* It needs some time before we can read the values back */
52  while (i != 0) {
53    __asm__ volatile ("nop");
54    --i;
55  }
56}
57
58static void lpc32xx_rtc_reset(void)
59{
60  lpc32xx.rtc.ctrl = LPC32XX_RTC_CTRL_RESET;
61  lpc32xx.rtc.ctrl = 0;
62  lpc32xx.rtc.key = LPC32XX_RTC_KEY;
63  lpc32xx_rtc_set(0);
64}
65
66static void lpc32xx_rtc_initialize(int minor)
67{
68  uint32_t up_first = 0;
69  uint32_t up_second = 0;
70  uint32_t down_first = 0;
71  uint32_t down_second = 0;
72
73  if (lpc32xx.rtc.key != LPC32XX_RTC_KEY) {
74    lpc32xx_rtc_reset();
75  }
76
77  do {
78    up_first = lpc32xx.rtc.ucount;
79    down_first = lpc32xx.rtc.dcount;
80    up_second = lpc32xx.rtc.ucount;
81    down_second = lpc32xx.rtc.dcount;
82  } while (up_first != up_second || down_first != down_second);
83
84  if (up_first + down_first != LPC32XX_RTC_COUNTER_DELTA) {
85    lpc32xx_rtc_reset();
86  }
87}
88
89static int lpc32xx_rtc_get_time(int minor, rtems_time_of_day *tod)
90{
91  struct timeval now = {
92    .tv_sec = lpc32xx.rtc.ucount,
93    .tv_usec = 0
94  };
95  struct tm time;
96
97  gmtime_r(&now.tv_sec, &time);
98
99  tod->year   = time.tm_year + 1900;
100  tod->month  = time.tm_mon + 1;
101  tod->day    = time.tm_mday;
102  tod->hour   = time.tm_hour;
103  tod->minute = time.tm_min;
104  tod->second = time.tm_sec;
105  tod->ticks  = 0;
106
107  return RTEMS_SUCCESSFUL;
108}
109
110static int lpc32xx_rtc_set_time(int minor, const rtems_time_of_day *tod)
111{
112  lpc32xx_rtc_set(_TOD_To_seconds(tod));
113
114  return 0;
115}
116
117static bool lpc32xx_rtc_probe(int minor)
118{
119  return true;
120}
121
122const rtc_fns lpc32xx_rtc_ops = {
123  .deviceInitialize = lpc32xx_rtc_initialize,
124  .deviceGetTime = lpc32xx_rtc_get_time,
125  .deviceSetTime = lpc32xx_rtc_set_time
126};
127
128size_t RTC_Count = LPC32XX_RTC_COUNT;
129
130rtc_tbl RTC_Table [LPC32XX_RTC_COUNT] = {
131  {
132    .sDeviceName = "/dev/rtc",
133    .deviceType = RTC_CUSTOM,
134    .pDeviceFns = &lpc32xx_rtc_ops,
135    .deviceProbe = lpc32xx_rtc_probe,
136    .pDeviceParams = NULL,
137    .ulCtrlPort1 = 0,
138    .ulDataPort = 0,
139    .getRegister = NULL,
140    .setRegister = NULL
141  }
142};
Note: See TracBrowser for help on using the repository browser.