source: rtems/bsps/arm/atsam/start/power-rtc.c @ 9964895

5
Last change on this file since 9964895 was 9964895, checked in by Sebastian Huber <sebastian.huber@…>, on 04/20/18 at 08:35:35

bsps: Move startup files to bsps

Adjust build support files to new directory layout.

This patch is a part of the BSP source reorganization.

Update #3285.

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/*
2 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#include <bsp.h>
16#include <bsp/power.h>
17#include <bsp/irq.h>
18
19#include <libchip/chip.h>
20
21#define ATSAM_ENABLE_ALARM_INTERRUPT (1u << 1)
22
23static void set_rtc_alarm_interrupt(uint8_t interval)
24{
25        Rtc *rtc = RTC;
26        rtems_time_of_day tod;
27
28        /* Clear current status register */
29        RTC_ClearSCCR(rtc, 0x3F);
30
31        atsam_rtc_get_time(&tod);
32        tod.second = (tod.second + interval) % 60;
33        tod.second = (((tod.second / 10) << 4) | (tod.second % 10));
34
35        rtc->RTC_TIMALR &= ~RTC_TIMALR_SECEN;
36        rtc->RTC_TIMALR = tod.second;
37        rtc->RTC_TIMALR |= RTC_TIMALR_SECEN;
38        RTC_EnableIt(rtc, ATSAM_ENABLE_ALARM_INTERRUPT);
39}
40
41static void rtc_interrupt_handler(void *arg)
42{
43        atsam_power_data_rtc_driver *rtc_data;
44
45        rtc_data = (atsam_power_data_rtc_driver *)arg;
46        set_rtc_alarm_interrupt(rtc_data->interval);
47}
48
49static void rtc_alarm_handler(void *arg)
50{
51        Rtc *rtc = RTC;
52        rtems_status_code sc;
53
54        /* Clear current status register */
55        RTC_ClearSCCR(rtc, 0x3F);
56
57        /* Switch off all RTC interrupts */
58        RTC_DisableIt(rtc, 0x1F);
59
60        /* Install RTC interrupt handler */
61        sc = rtems_interrupt_handler_install(RTC_IRQn,
62            "RTC",
63            RTEMS_INTERRUPT_UNIQUE,
64            rtc_interrupt_handler,
65            arg
66        );
67        assert(sc == RTEMS_SUCCESSFUL);
68}
69
70static void set_time(void)
71{
72        rtems_time_of_day tod;
73        rtems_status_code sc;
74
75        atsam_rtc_get_time(&tod);
76        sc = rtems_clock_set(&tod);
77        assert(sc == RTEMS_SUCCESSFUL);
78}
79
80void atsam_power_handler_rtc_driver(
81    const atsam_power_control *control,
82    atsam_power_state state
83)
84{
85        atsam_power_data_rtc_driver *rtc_data;
86        Rtc *rtc = RTC;
87
88        rtc_data = (atsam_power_data_rtc_driver *)control->data.arg;
89
90        switch (state) {
91                case ATSAM_POWER_ON:
92                        RTC_DisableIt(rtc, ATSAM_ENABLE_ALARM_INTERRUPT);
93                        set_time();
94                        break;
95                case ATSAM_POWER_OFF:
96                        set_rtc_alarm_interrupt(rtc_data->interval);
97                        break;
98                case ATSAM_POWER_INIT:
99                        rtc_alarm_handler(rtc_data);
100                        break;
101                default:
102                        break;
103        }
104}
Note: See TracBrowser for help on using the repository browser.