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

Last change on this file since 6fc04f4c was 6fc04f4c, checked in by Joel Sherrill <joel@…>, on 06/27/22 at 13:46:02

bsps/arm/atsamv: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <bsp.h>
29#include <bsp/power.h>
30#include <bsp/irq.h>
31
32#include <libchip/chip.h>
33
34#define ATSAM_ENABLE_ALARM_INTERRUPT RTC_IER_ALREN
35
36static void set_rtc_alarm_interrupt(uint32_t interval)
37{
38        Rtc *rtc = RTC;
39        uint8_t hour;
40        uint8_t minute;
41        uint8_t second;
42        uint32_t time;
43
44        /* Clear current status register */
45        RTC_ClearSCCR(rtc, 0x3F);
46
47        RTC_GetTime(rtc, &hour, &minute, &second);
48
49        time = UINT32_C(3600) * hour + UINT32_C(60) * minute + second;
50        time = (time + interval) % (UINT32_C(24) * 3600);
51
52        second = (uint8_t) (time % 60);
53        minute = (uint8_t) ((time / 60) % 60);
54        hour = (uint8_t) (time / 3600);
55
56        if (interval < 60) {
57                RTC_SetTimeAlarm(rtc, NULL, NULL, &second);
58        } else if (interval < 3600) {
59                RTC_SetTimeAlarm(rtc, NULL, &minute, &second);
60        } else {
61                RTC_SetTimeAlarm(rtc, &hour, &minute, &second);
62        }
63
64        RTC_EnableIt(rtc, ATSAM_ENABLE_ALARM_INTERRUPT);
65}
66
67static void rtc_interrupt_handler(void *arg)
68{
69        atsam_power_data_rtc_driver *rtc_data;
70
71        rtc_data = (atsam_power_data_rtc_driver *)arg;
72        set_rtc_alarm_interrupt(rtc_data->interval);
73}
74
75static void rtc_alarm_handler(void *arg)
76{
77        Rtc *rtc = RTC;
78        rtems_status_code sc;
79
80        /* Clear current status register */
81        RTC_ClearSCCR(rtc, 0x3F);
82
83        /* Switch off all RTC interrupts */
84        RTC_DisableIt(rtc, 0x1F);
85
86        /* Install RTC interrupt handler */
87        sc = rtems_interrupt_handler_install(RTC_IRQn,
88            "RTC",
89            RTEMS_INTERRUPT_UNIQUE,
90            rtc_interrupt_handler,
91            arg
92        );
93        assert(sc == RTEMS_SUCCESSFUL);
94}
95
96static void set_time(void)
97{
98        rtems_time_of_day tod;
99        rtems_status_code sc;
100
101        atsam_rtc_get_time(&tod);
102        sc = rtems_clock_set(&tod);
103        assert(sc == RTEMS_SUCCESSFUL);
104}
105
106void atsam_power_handler_rtc_driver(
107    const atsam_power_control *control,
108    atsam_power_state state
109)
110{
111        atsam_power_data_rtc_driver *rtc_data;
112        rtems_interrupt_level level;
113        Rtc *rtc = RTC;
114
115        rtc_data = (atsam_power_data_rtc_driver *)control->data.arg;
116
117        switch (state) {
118                case ATSAM_POWER_ON:
119                        RTC_DisableIt(rtc, ATSAM_ENABLE_ALARM_INTERRUPT);
120                        set_time();
121                        break;
122                case ATSAM_POWER_OFF:
123                        set_rtc_alarm_interrupt(rtc_data->interval);
124                        break;
125                case ATSAM_POWER_INIT:
126                        /* Enable fast startup via RTC alarm */
127                        rtems_interrupt_disable(level);
128                        PMC->PMC_FSMR |= PMC_FSMR_RTCAL;
129                        rtems_interrupt_enable(level);
130
131                        rtc_alarm_handler(rtc_data);
132                        break;
133                default:
134                        break;
135        }
136}
Note: See TracBrowser for help on using the repository browser.