Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:48

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2016 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #include <libchip/chip.h>
0029 #include <libchip/rtc.h>
0030 
0031 #include <bsp.h>
0032 
0033 #define ARBITRARY_DAY_OF_WEEK 1
0034 
0035 void atsam_rtc_get_time(rtems_time_of_day *tod)
0036 {
0037   Rtc *rtc = RTC;
0038   uint8_t hour;
0039   uint8_t minute;
0040   uint8_t second;
0041   uint16_t year;
0042   uint8_t month;
0043   uint8_t day;
0044   uint8_t week;
0045 
0046   RTC_GetDate(rtc, &year, &month, &day, &week);
0047   RTC_GetTime(rtc, &hour, &minute, &second);
0048 
0049   tod->ticks = 0;
0050   tod->second = second;
0051   tod->minute = minute;
0052   tod->hour = hour;
0053   tod->day = day;
0054   tod->month = month;
0055   tod->year = year;
0056 }
0057 
0058 static void atsam_rtc_device_initialize(int minor)
0059 {
0060   Rtc *rtc = RTC;
0061 
0062   RTC_DisableIt(rtc, 0x1F);
0063 }
0064 
0065 static int atsam_rtc_device_get_time(int minor, rtems_time_of_day *tod)
0066 {
0067   atsam_rtc_get_time(tod);
0068 
0069   return 0;
0070 }
0071 
0072 static int atsam_rtc_device_set_time(int minor, const rtems_time_of_day *tod)
0073 {
0074   Rtc *rtc = RTC;
0075   uint8_t hour;
0076   uint8_t minute;
0077   uint8_t second;
0078   uint16_t year;
0079   uint8_t month;
0080   uint8_t day;
0081   uint8_t week;
0082 
0083   second = (uint8_t) tod->second;
0084   minute = (uint8_t) tod->minute;
0085   hour = (uint8_t) tod->hour;
0086   day = (uint8_t) tod->day;
0087   month = (uint8_t) tod->month;
0088   week = ARBITRARY_DAY_OF_WEEK;
0089   year = (uint16_t) tod->year;
0090 
0091   RTC_SetDate(rtc, year, month, day, week);
0092   RTC_SetTime(rtc, hour, minute, second);
0093 
0094   return 0;
0095 }
0096 
0097 static bool atsam_rtc_device_probe(int minor)
0098 {
0099   return true;
0100 }
0101 
0102 const rtc_fns atsam_rtc_device_ops = {
0103   .deviceInitialize = atsam_rtc_device_initialize,
0104   .deviceGetTime = atsam_rtc_device_get_time,
0105   .deviceSetTime = atsam_rtc_device_set_time
0106 };
0107 
0108 rtc_tbl RTC_Table[] = {
0109   {
0110     .sDeviceName = "/dev/rtc",
0111     .deviceType = RTC_CUSTOM,
0112     .pDeviceFns = &atsam_rtc_device_ops,
0113     .deviceProbe = atsam_rtc_device_probe
0114   }
0115 };
0116 
0117 size_t RTC_Count = RTEMS_ARRAY_SIZE(RTC_Table);