source: rtems/bsps/arm/lpc32xx/rtc/rtc-config.c @ 5841e0b

Last change on this file since 5841e0b was 5841e0b, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 9, 2018 at 12:47:20 PM

bsp/lpc32xx: Include missing <time.h>

Update #3598.

  • 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#include <time.h>
29
30#define LPC32XX_RTC_COUNT 1U
31
32#define LPC32XX_RTC_COUNTER_DELTA 0xfffffffeU
33
34#define LPC32XX_RTC_KEY 0xb5c13f27U
35
36#define LPC32XX_RTC_CTRL_FORCE_ONSW (1U << 7)
37#define LPC32XX_RTC_CTRL_STOP (1U << 6)
38#define LPC32XX_RTC_CTRL_RESET (1U << 4)
39#define LPC32XX_RTC_CTRL_MATCH_1_ONSW (1U << 3)
40#define LPC32XX_RTC_CTRL_MATCH_0_ONSW (1U << 2)
41#define LPC32XX_RTC_CTRL_MATCH_1_INTR (1U << 1)
42#define LPC32XX_RTC_CTRL_MATCH_0_INTR (1U << 0)
43
44static void lpc32xx_rtc_set(uint32_t val)
45{
46  unsigned i = lpc32xx_arm_clk() / LPC32XX_OSCILLATOR_RTC;
47
48  lpc32xx.rtc.ctrl |= LPC32XX_RTC_CTRL_STOP;
49  lpc32xx.rtc.ucount = val;
50  lpc32xx.rtc.dcount = LPC32XX_RTC_COUNTER_DELTA - val;
51  lpc32xx.rtc.ctrl &= ~LPC32XX_RTC_CTRL_STOP;
52
53  /* It needs some time before we can read the values back */
54  while (i != 0) {
55    __asm__ volatile ("nop");
56    --i;
57  }
58}
59
60static void lpc32xx_rtc_reset(void)
61{
62  lpc32xx.rtc.ctrl = LPC32XX_RTC_CTRL_RESET;
63  lpc32xx.rtc.ctrl = 0;
64  lpc32xx.rtc.key = LPC32XX_RTC_KEY;
65  lpc32xx_rtc_set(0);
66}
67
68static void lpc32xx_rtc_initialize(int minor)
69{
70  uint32_t up_first = 0;
71  uint32_t up_second = 0;
72  uint32_t down_first = 0;
73  uint32_t down_second = 0;
74
75  if (lpc32xx.rtc.key != LPC32XX_RTC_KEY) {
76    lpc32xx_rtc_reset();
77  }
78
79  do {
80    up_first = lpc32xx.rtc.ucount;
81    down_first = lpc32xx.rtc.dcount;
82    up_second = lpc32xx.rtc.ucount;
83    down_second = lpc32xx.rtc.dcount;
84  } while (up_first != up_second || down_first != down_second);
85
86  if (up_first + down_first != LPC32XX_RTC_COUNTER_DELTA) {
87    lpc32xx_rtc_reset();
88  }
89}
90
91static int lpc32xx_rtc_get_time(int minor, rtems_time_of_day *tod)
92{
93  struct timeval now = {
94    .tv_sec = lpc32xx.rtc.ucount,
95    .tv_usec = 0
96  };
97  struct tm time;
98
99  gmtime_r(&now.tv_sec, &time);
100
101  tod->year   = time.tm_year + 1900;
102  tod->month  = time.tm_mon + 1;
103  tod->day    = time.tm_mday;
104  tod->hour   = time.tm_hour;
105  tod->minute = time.tm_min;
106  tod->second = time.tm_sec;
107  tod->ticks  = 0;
108
109  return RTEMS_SUCCESSFUL;
110}
111
112static int lpc32xx_rtc_set_time(int minor, const rtems_time_of_day *tod)
113{
114  lpc32xx_rtc_set(_TOD_To_seconds(tod));
115
116  return 0;
117}
118
119static bool lpc32xx_rtc_probe(int minor)
120{
121  return true;
122}
123
124const rtc_fns lpc32xx_rtc_ops = {
125  .deviceInitialize = lpc32xx_rtc_initialize,
126  .deviceGetTime = lpc32xx_rtc_get_time,
127  .deviceSetTime = lpc32xx_rtc_set_time
128};
129
130size_t RTC_Count = LPC32XX_RTC_COUNT;
131
132rtc_tbl RTC_Table [LPC32XX_RTC_COUNT] = {
133  {
134    .sDeviceName = "/dev/rtc",
135    .deviceType = RTC_CUSTOM,
136    .pDeviceFns = &lpc32xx_rtc_ops,
137    .deviceProbe = lpc32xx_rtc_probe,
138    .pDeviceParams = NULL,
139    .ulCtrlPort1 = 0,
140    .ulDataPort = 0,
141    .getRegister = NULL,
142    .setRegister = NULL
143  }
144};
Note: See TracBrowser for help on using the repository browser.