summaryrefslogtreecommitdiffstats
path: root/watch-library/hal
diff options
context:
space:
mode:
authorJoey Castillo <jose.castillo@gmail.com>2021-09-27 17:51:52 -0400
committerJoey Castillo <jose.castillo@gmail.com>2021-09-27 17:51:52 -0400
commitb353e47506be60a067c54bb30dfabe413cc1a6d2 (patch)
tree5289d7a4588c3ef570a064215da71f48ac9b6dc3 /watch-library/hal
parenta65dcf1ec8071878988d5aa0390c5be66ba52a11 (diff)
downloadSensor-Watch-b353e47506be60a067c54bb30dfabe413cc1a6d2.tar.gz
Sensor-Watch-b353e47506be60a067c54bb30dfabe413cc1a6d2.tar.bz2
Sensor-Watch-b353e47506be60a067c54bb30dfabe413cc1a6d2.zip
remove unused ASF code
Diffstat (limited to 'watch-library/hal')
-rw-r--r--watch-library/hal/documentation/calendar.rst72
-rw-r--r--watch-library/hal/include/hal_calendar.h159
-rw-r--r--watch-library/hal/include/hpl_calendar.h251
-rw-r--r--watch-library/hal/src/hal_calendar.c645
4 files changed, 0 insertions, 1127 deletions
diff --git a/watch-library/hal/documentation/calendar.rst b/watch-library/hal/documentation/calendar.rst
deleted file mode 100644
index 8a3de6e8..00000000
--- a/watch-library/hal/documentation/calendar.rst
+++ /dev/null
@@ -1,72 +0,0 @@
-===============================
-The Calendar driver (bare-bone)
-===============================
-
-The Calendar driver provides means to set and get current date and time.
-After enabling, an instance of the driver starts counting time from the base date with
-the resolution of one second. The default base date is 00:00:00 1st of January 1970.
-Only the base year of the base date can be changed via the driver API.
-
-The current date and time is kept internally in a relative form as the difference between
-current date and time and the base date and time. This means that changing the base year changes
-current date.
-
-The base date and time defines time "zero" or the earliest possible point in time that the calender driver can describe,
-this means that current time and alarms can not be set to anything earlier than this time.
-
-The Calendar driver provides alarm functionality.
-An alarm is a software trigger which fires on particular date and time with particular periodicity.
-Upon firing the given callback function is called.
-
-An alarm can be in single-shot mode, firing only once at matching time; or in repeating mode, meaning that it will
-reschedule a new alarm automatically based on repeating mode configuration.
-In single-shot mode an alarm is removed from the alarm queue before its callback is called. It allows an application to
-reuse the memory of expired alarm in the callback.
-
-An alarm can be triggered on the following events: match on second, minute, hour, day, month or year.
-Matching on second means that the alarm is triggered when the value of seconds of the current time is equal to
-the alarm's value of seconds. This means repeating alarm with match on seconds is triggered with the period of a minute.
-Matching on minute means that the calendars minute and seconds values has to match the alarms, the rest of the date-time
-value is ignored. In repeating mode this means a new alarm every hour.
-The same logic is applied to match on hour, day, month and year.
-
-Each instance of the Calendar driver supports infinite amount of software alarms, only limited by the amount of RAM available.
-
-Features
---------
-* Initialization and de-initialization
-* Enabling and disabling
-* Date and time operations
-* Software alarms
-
-Applications
-------------
-* A source of current date and time for an embedded system.
-* Periodical functionality in low-power applications since the driver is designed to use 1Hz clock.
-* Periodical function calls in case if it is more convenient to operate with absolute time.
-
-Dependencies
-------------
-* This driver expects a counter to be increased by one every second to count date and time correctly.
-* Each instance of the driver requires separate hardware timer.
-
-Concurrency
------------
-The Calendar driver is an interrupt driven driver.This means that the interrupt that triggers an alarm may occur during
-the process of adding or removing an alarm via the driver's API. In such case the interrupt processing is postponed
-until the alarm adding or removing is complete.
-
-The alarm queue is not protected from the access by interrupts not used by the driver. Due to this
-it is not recommended to add or remove an alarm from such interrupts: in case if a higher priority interrupt supersedes
-the driver's interrupt, adding or removing an alarm may cause unpredictable behavior of the driver.
-
-Limitations
------------
-* Only years divisible by 4 are deemed a leap year, this gives a correct result between the years 1901 to 2099.
-* The driver is designed to work outside of an operating system environment, the software alarm queue is therefore processed in interrupt context which may delay execution of other interrupts.
-* If there are a lot of frequently called interrupts with the priority higher than the driver's one, it may cause delay in alarm's triggering.
-* Changing the base year or setting current date or time does not shift alarms' date and time accordingly or expires alarms.
-
-Knows issues and workarounds
-----------------------------
-Not applicable
diff --git a/watch-library/hal/include/hal_calendar.h b/watch-library/hal/include/hal_calendar.h
deleted file mode 100644
index 26949a57..00000000
--- a/watch-library/hal/include/hal_calendar.h
+++ /dev/null
@@ -1,159 +0,0 @@
-/**
- * \file
- *
- * \brief Generic CALENDAR functionality declaration.
- *
- * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
- *
- * \asf_license_start
- *
- * \page License
- *
- * Subject to your compliance with these terms, you may use Microchip
- * software and any derivatives exclusively with Microchip products.
- * It is your responsibility to comply with third party license terms applicable
- * to your use of third party software (including open source software) that
- * may accompany Microchip software.
- *
- * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
- * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
- * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
- * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
- * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
- * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
- * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
- * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
- * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
- * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
- * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
- *
- * \asf_license_stop
- *
- */
-
-#ifndef _HAL_CALENDER_H_INCLUDED
-#define _HAL_CALENDER_H_INCLUDED
-
-#include "hpl_calendar.h"
-#include <utils_list.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * \addtogroup doc_driver_hal_calendar_async
- *
- *@{
- */
-
-/** \brief Prototype of callback on alarm match
- * \param calendar Pointer to the HAL Calendar instance.
- */
-typedef void (*calendar_cb_alarm_t)(struct calendar_descriptor *const calendar);
-
-/** \brief Struct for alarm time
- */
-struct calendar_alarm {
- struct list_element elem;
- struct _calendar_alarm cal_alarm;
- calendar_cb_alarm_t callback;
-};
-
-/** \brief Initialize the Calendar HAL instance and hardware
- *
- * \param calendar Pointer to the HAL Calendar instance.
- * \param hw Pointer to the hardware instance.
- * \return Operation status of init
- * \retval 0 Completed successfully.
- */
-int32_t calendar_init(struct calendar_descriptor *const calendar, const void *hw);
-
-/** \brief Reset the Calendar HAL instance and hardware
- *
- * Reset Calendar instance to hardware defaults.
- *
- * \param calendar Pointer to the HAL Calendar instance.
- * \return Operation status of reset.
- * \retval 0 Completed successfully.
- */
-int32_t calendar_deinit(struct calendar_descriptor *const calendar);
-
-/** \brief Enable the Calendar HAL instance and hardware
- *
- * \param calendar Pointer to the HAL Calendar instance.
- * \return Operation status of init
- * \retval 0 Completed successfully.
- */
-int32_t calendar_enable(struct calendar_descriptor *const calendar);
-
-/** \brief Disable the Calendar HAL instance and hardware
- *
- * Disable Calendar instance to hardware defaults.
- *
- * \param calendar Pointer to the HAL Calendar instance.
- * \return Operation status of reset.
- * \retval 0 Completed successfully.
- */
-int32_t calendar_disable(struct calendar_descriptor *const calendar);
-
-/** \brief Configure the base year for calendar HAL instance and hardware
- *
- * \param calendar Pointer to the HAL Calendar instance.
- * \param p_base_year The desired base year.
- * \retval 0 Completed successfully.
- */
-int32_t calendar_set_baseyear(struct calendar_descriptor *const calendar, const uint32_t p_base_year);
-
-/** \brief Configure the time for calendar HAL instance and hardware
- *
- * \param calendar Pointer to the HAL Calendar instance.
- * \param p_calendar_time Pointer to the time configuration.
- * \retval 0 Completed successfully.
- */
-int32_t calendar_set_time(struct calendar_descriptor *const calendar, struct calendar_time *const p_calendar_time);
-
-/** \brief Configure the date for calendar HAL instance and hardware
- *
- * \param calendar Pointer to the HAL Calendar instance.
- * \param p_calendar_date Pointer to the date configuration.
- * \return Operation status of time set.
- * \retval 0 Completed successfully.
- */
-int32_t calendar_set_date(struct calendar_descriptor *const calendar, struct calendar_date *const p_calendar_date);
-
-/** \brief Get the time for calendar HAL instance and hardware
- *
- * \param calendar Pointer to the HAL Calendar instance.
- * \param date_time Pointer to the value that will be filled with the current time.
- * \return Operation status of time retrieve.
- * \retval 0 Completed successfully.
- */
-int32_t calendar_get_date_time(struct calendar_descriptor *const calendar, struct calendar_date_time *const date_time);
-
-/** \brief Config the alarm time for calendar HAL instance and hardware
- *
- * Set the alarm time to calendar instance. If the callback is NULL, remove
- * the alarm if the alarm is already added, otherwise, ignore the alarm.
- *
- * \param calendar Pointer to the HAL Calendar instance.
- * \param alarm Pointer to the configuration.
- * \param callback Pointer to the callback function.
- * \return Operation status of alarm time set.
- * \retval 0 Completed successfully.
- */
-int32_t calendar_set_alarm(struct calendar_descriptor *const calendar, struct calendar_alarm *const alarm,
- calendar_cb_alarm_t callback);
-
-/** \brief Retrieve the current driver version
- * \return Current driver version.
- */
-uint32_t calendar_get_version(void);
-
-/**@}*/
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _HAL_CALENDER_H_INCLUDED */
diff --git a/watch-library/hal/include/hpl_calendar.h b/watch-library/hal/include/hpl_calendar.h
index f94249b9..0dce40fd 100644
--- a/watch-library/hal/include/hpl_calendar.h
+++ b/watch-library/hal/include/hpl_calendar.h
@@ -33,69 +33,11 @@
#ifndef _HPL_CALENDER_H_INCLUDED
#define _HPL_CALENDER_H_INCLUDED
-#include <compiler.h>
-#include <utils_list.h>
-#include "hpl_irq.h"
-
#ifdef __cplusplus
extern "C" {
#endif
/**
- * \brief Calendar structure
- *
- * The Calendar structure forward declaration.
- */
-struct calendar_dev;
-
-/**
- * \brief Available mask options for alarms.
- *
- * Available mask options for alarms.
- */
-enum calendar_alarm_option {
- /** Alarm disabled. */
- CALENDAR_ALARM_MATCH_DISABLED = 0,
- /** Alarm match on second. */
- CALENDAR_ALARM_MATCH_SEC,
- /** Alarm match on second and minute. */
- CALENDAR_ALARM_MATCH_MIN,
- /** Alarm match on second, minute, and hour. */
- CALENDAR_ALARM_MATCH_HOUR,
- /** Alarm match on second, minute, hour, and day. */
- CALENDAR_ALARM_MATCH_DAY,
- /** Alarm match on second, minute, hour, day, and month. */
- CALENDAR_ALARM_MATCH_MONTH,
- /** Alarm match on second, minute, hour, day, month and year. */
- CALENDAR_ALARM_MATCH_YEAR
-};
-
-/**
- * \brief Available mode for alarms.
- */
-enum calendar_alarm_mode { ONESHOT = 1, REPEAT };
-/**
- * \brief Prototype of callback on alarm match
- */
-typedef void (*calendar_drv_cb_t)();
-typedef void (*calendar_drv_extwake_cb_t)(uint8_t reason);
-
-/**
- * \brief Structure of Calendar instance
- */
-struct calendar_dev {
- /** Pointer to the hardware base */
- void *hw;
- /** Alarm match callback */
- calendar_drv_cb_t callback_alarm;
- /** Tamper callback */
- calendar_drv_extwake_cb_t callback_tamper;
- /** Tick callback */
- calendar_drv_cb_t callback_tick;
- /** IRQ struct */
- struct _irq_descriptor irq;
-};
-/**
* \brief Time struct for calendar
*/
struct calendar_time {
@@ -119,17 +61,6 @@ struct calendar_date {
uint16_t year;
};
-/** \brief Calendar driver struct
- *
- */
-struct calendar_descriptor {
- struct calendar_dev device;
- struct list_descriptor alarms;
- /*base date/time = base_year/1/1/0/0/0(year/month/day/hour/min/sec)*/
- uint32_t base_year;
- uint8_t flags;
-};
-
/** \brief Date&Time struct for calendar
*/
struct calendar_date_time {
@@ -137,188 +68,6 @@ struct calendar_date_time {
struct calendar_date date;
};
-/** \brief struct for alarm time
- */
-struct _calendar_alarm {
- struct calendar_date_time datetime;
- uint32_t timestamp;
- enum calendar_alarm_option option;
- enum calendar_alarm_mode mode;
-};
-
-/** \enum for tamper detection mode
- */
-enum tamper_detection_mode { TAMPER_MODE_OFF = 0U, TAMPER_MODE_WAKE, TAMPER_MODE_CAPTURE, TAMPER_MODE_ACTL };
-
-/** \enum for tamper detection mode
- */
-enum tamper_id { TAMPID0 = 0U, TAMPID1, TAMPID2, TAMPID3, TAMPID4 };
-/**
- * \brief Initialize Calendar instance
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _calendar_init(struct calendar_dev *const dev);
-
-/**
- * \brief Deinitialize Calendar instance
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _calendar_deinit(struct calendar_dev *const dev);
-
-/**
- * \brief Enable Calendar instance
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _calendar_enable(struct calendar_dev *const dev);
-
-/**
- * \brief Disable Calendar instance
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _calendar_disable(struct calendar_dev *const dev);
-/**
- * \brief Set counter for calendar
- *
- * \param[in] dev The pointer to calendar device struct
- * \param[in] counter The counter for set
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _calendar_set_counter(struct calendar_dev *const dev, const uint32_t counter);
-
-/**
- * \brief Get counter for calendar
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return return current counter value
- */
-uint32_t _calendar_get_counter(struct calendar_dev *const dev);
-
-/**
- * \brief Set compare value for calendar
- *
- * \param[in] dev The pointer to calendar device struct
- * \param[in] comp The compare value for set
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _calendar_set_comp(struct calendar_dev *const dev, const uint32_t comp);
-
-/**
- * \brief Get compare value for calendar
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return return current compare value
- */
-uint32_t _calendar_get_comp(struct calendar_dev *const dev);
-
-/**
- * \brief Register callback for calendar alarm
- *
- * \param[in] dev The pointer to calendar device struct
- * \param[in] callback The pointer to callback function
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _calendar_register_callback(struct calendar_dev *const dev, calendar_drv_cb_t callback);
-
-/**
- * \brief Set calendar IRQ
- *
- * \param[in] dev The pointer to calendar device struct
- */
-void _calendar_set_irq(struct calendar_dev *const dev);
-
-/**
- * \brief Register callback for 1Hz tick from prescaler
- *
- * \param[in] dev The pointer to calendar device struct
- * \param[in] callback The pointer to callback function
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _prescaler_register_callback(struct calendar_dev *const dev, calendar_drv_cb_t callback);
-
-/**
- * \brief Register callback for tamper detection
- *
- * \param[in] dev The pointer to calendar device struct
- * \param[in] callback The pointer to callback function
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _extwake_register_callback(struct calendar_dev *const dev, calendar_drv_extwake_cb_t callback);
-
-/**
- * \brief Find tamper is detected on specified pin
- *
- * \param[in] dev The pointer to calendar device struct
- * \param[in] enum Tamper ID number
- *
- * \return true on detection success and false on failure.
- */
-bool _is_tamper_detected(struct calendar_dev *const dev, enum tamper_id tamper_id_pin);
-
-/**
- * \brief brief Clear the Tamper ID flag
- *
- * \param[in] dev The pointer to calendar device struct
- * \param[in] enum Tamper ID number
- *
- * \return ERR_NONE
- */
-int32_t _tamper_clear_tampid_flag(struct calendar_dev *const dev, enum tamper_id tamper_id_pin);
-
-/**
- * \brief Enable Debounce Asynchronous Feature
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _tamper_enable_debounce_asynchronous(struct calendar_dev *const dev);
-
-/**
- * \brief Disable Tamper Debounce Asynchronous Feature
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _tamper_disable_debounce_asynchronous(struct calendar_dev *const dev);
-
-/**
- * \brief Enable Tamper Debounce Majority Feature
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _tamper_enable_debounce_majority(struct calendar_dev *const dev);
-
-/**
- * \brief Enable Tamper Debounce Majority Feature
- *
- * \param[in] dev The pointer to calendar device struct
- *
- * \return ERR_NONE on success, or an error code on failure.
- */
-int32_t _tamper_disable_debounce_majority(struct calendar_dev *const dev);
-
#ifdef __cplusplus
}
#endif
diff --git a/watch-library/hal/src/hal_calendar.c b/watch-library/hal/src/hal_calendar.c
deleted file mode 100644
index 68cb7286..00000000
--- a/watch-library/hal/src/hal_calendar.c
+++ /dev/null
@@ -1,645 +0,0 @@
-/**
- * \file
- *
- * \brief Generic CALENDAR functionality implementation.
- *
- * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
- *
- * \asf_license_start
- *
- * \page License
- *
- * Subject to your compliance with these terms, you may use Microchip
- * software and any derivatives exclusively with Microchip products.
- * It is your responsibility to comply with third party license terms applicable
- * to your use of third party software (including open source software) that
- * may accompany Microchip software.
- *
- * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
- * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
- * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
- * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
- * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
- * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
- * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
- * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
- * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
- * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
- * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
- *
- * \asf_license_stop
- *
- */
-
-#include "hal_calendar.h"
-#include <utils.h>
-#include <utils_assert.h>
-#include <hal_atomic.h>
-
-#define CALENDAR_VERSION 0x00000001u
-#define SECS_IN_LEAP_YEAR 31622400
-#define SECS_IN_NON_LEAP_YEAR 31536000
-#define SECS_IN_31DAYS 2678400
-#define SECS_IN_30DAYS 2592000
-#define SECS_IN_29DAYS 2505600
-#define SECS_IN_28DAYS 2419200
-#define SECS_IN_DAY 86400
-#define SECS_IN_HOUR 3600
-#define SECS_IN_MINUTE 60
-#define DEFAULT_BASE_YEAR 1970
-
-#define SET_ALARM_BUSY 1
-#define PROCESS_ALARM_BUSY 2
-
-/** \brief leap year check
- * \retval false not leap year.
- * \retval true leap year.
- */
-static bool leap_year(uint16_t year)
-{
- if (year & 3) {
- return false;
- } else {
- return true;
- }
-}
-
-/** \brief calculate the seconds in specified year/month
- * \retval 0 month error.
- */
-static uint32_t get_secs_in_month(uint32_t year, uint8_t month)
-{
- uint32_t sec_in_month = 0;
-
- if (leap_year(year)) {
- switch (month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- sec_in_month = SECS_IN_31DAYS;
- break;
- case 2:
- sec_in_month = SECS_IN_29DAYS;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- sec_in_month = SECS_IN_30DAYS;
- break;
- default:
- break;
- }
- } else {
- switch (month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- sec_in_month = SECS_IN_31DAYS;
- break;
- case 2:
- sec_in_month = SECS_IN_28DAYS;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- sec_in_month = SECS_IN_30DAYS;
- break;
- default:
- break;
- }
- }
-
- return sec_in_month;
-}
-
-/** \brief convert timestamp to date/time
- */
-static int32_t convert_timestamp_to_datetime(struct calendar_descriptor *const calendar, uint32_t ts,
- struct calendar_date_time *dt)
-{
- uint32_t tmp, sec_in_year, sec_in_month;
- uint32_t tmp_year = calendar->base_year;
- uint8_t tmp_month = 1;
- uint8_t tmp_day = 1;
- uint8_t tmp_hour = 0;
- uint8_t tmp_minutes = 0;
-
- tmp = ts;
-
- /* Find year */
- while (true) {
- sec_in_year = leap_year(tmp_year) ? SECS_IN_LEAP_YEAR : SECS_IN_NON_LEAP_YEAR;
-
- if (tmp >= sec_in_year) {
- tmp -= sec_in_year;
- tmp_year++;
- } else {
- break;
- }
- }
- /* Find month of year */
- while (true) {
- sec_in_month = get_secs_in_month(tmp_year, tmp_month);
-
- if (tmp >= sec_in_month) {
- tmp -= sec_in_month;
- tmp_month++;
- } else {
- break;
- }
- }
- /* Find day of month */
- while (true) {
- if (tmp >= SECS_IN_DAY) {
- tmp -= SECS_IN_DAY;
- tmp_day++;
- } else {
- break;
- }
- }
- /* Find hour of day */
- while (true) {
- if (tmp >= SECS_IN_HOUR) {
- tmp -= SECS_IN_HOUR;
- tmp_hour++;
- } else {
- break;
- }
- }
- /* Find minute in hour */
- while (true) {
- if (tmp >= SECS_IN_MINUTE) {
- tmp -= SECS_IN_MINUTE;
- tmp_minutes++;
- } else {
- break;
- }
- }
-
- dt->date.year = tmp_year;
- dt->date.month = tmp_month;
- dt->date.day = tmp_day;
- dt->time.hour = tmp_hour;
- dt->time.min = tmp_minutes;
- dt->time.sec = tmp;
-
- return ERR_NONE;
-}
-
-/** \brief convert date/time to timestamp
- * \return timestamp
- */
-static uint32_t convert_datetime_to_timestamp(struct calendar_descriptor *const calendar, struct calendar_date_time *dt)
-{
- uint32_t tmp = 0;
- uint32_t i = 0;
- uint8_t year, month, day, hour, minutes, seconds;
-
- year = dt->date.year - calendar->base_year;
- month = dt->date.month;
- day = dt->date.day;
- hour = dt->time.hour;
- minutes = dt->time.min;
- seconds = dt->time.sec;
-
- /* tot up year field */
- for (i = 0; i < year; ++i) {
- if (leap_year(calendar->base_year + i)) {
- tmp += SECS_IN_LEAP_YEAR;
- } else {
- tmp += SECS_IN_NON_LEAP_YEAR;
- }
- }
-
- /* tot up month field */
- for (i = 1; i < month; ++i) {
- tmp += get_secs_in_month(dt->date.year, i);
- }
-
- /* tot up day/hour/minute/second fields */
- tmp += (day - 1) * SECS_IN_DAY;
- tmp += hour * SECS_IN_HOUR;
- tmp += minutes * SECS_IN_MINUTE;
- tmp += seconds;
-
- return tmp;
-}
-
-/** \brief calibrate timestamp to make desired timestamp ahead of current timestamp
- */
-static void calibrate_timestamp(struct calendar_descriptor *const calendar, struct calendar_alarm *alarm,
- struct calendar_alarm *current_dt)
-{
- uint32_t alarm_ts;
- uint32_t current_ts = current_dt->cal_alarm.timestamp;
-
- (void)calendar;
-
- alarm_ts = alarm->cal_alarm.timestamp;
-
- /* calibrate timestamp */
- switch (alarm->cal_alarm.option) {
- case CALENDAR_ALARM_MATCH_SEC:
-
- if (alarm_ts <= current_ts) {
- alarm_ts += SECS_IN_MINUTE;
- }
-
- break;
- case CALENDAR_ALARM_MATCH_MIN:
-
- if (alarm_ts <= current_ts) {
- alarm_ts += SECS_IN_HOUR;
- }
-
- break;
- case CALENDAR_ALARM_MATCH_HOUR:
-
- if (alarm_ts <= current_ts) {
- alarm_ts += SECS_IN_DAY;
- }
-
- break;
- case CALENDAR_ALARM_MATCH_DAY:
-
- if (alarm_ts <= current_ts) {
- alarm_ts += get_secs_in_month(current_dt->cal_alarm.datetime.date.year,
- current_dt->cal_alarm.datetime.date.month);
- }
-
- break;
- case CALENDAR_ALARM_MATCH_MONTH:
-
- if (alarm_ts <= current_ts) {
- if (leap_year(current_dt->cal_alarm.datetime.date.year)) {
- alarm_ts += SECS_IN_LEAP_YEAR;
- } else {
- alarm_ts += SECS_IN_NON_LEAP_YEAR;
- }
- }
-
- break;
- /* do nothing for year match */
- case CALENDAR_ALARM_MATCH_YEAR:
- default:
- break;
- }
-
- /* desired timestamp after calibration */
- alarm->cal_alarm.timestamp = alarm_ts;
-}
-
-/** \brief complete alarm to absolute date/time, then fill up the timestamp
- */
-static void fill_alarm(struct calendar_descriptor *const calendar, struct calendar_alarm *alarm)
-{
- struct calendar_alarm current_dt;
- uint32_t tmp, current_ts;
-
- /* get current date/time */
- current_ts = _calendar_get_counter(&calendar->device);
- convert_timestamp_to_datetime(calendar, current_ts, &current_dt.cal_alarm.datetime);
-
- current_dt.cal_alarm.timestamp = current_ts;
-
- /* complete alarm */
- switch (alarm->cal_alarm.option) {
- case CALENDAR_ALARM_MATCH_SEC:
- alarm->cal_alarm.datetime.date.year = current_dt.cal_alarm.datetime.date.year;
- alarm->cal_alarm.datetime.date.month = current_dt.cal_alarm.datetime.date.month;
- alarm->cal_alarm.datetime.date.day = current_dt.cal_alarm.datetime.date.day;
- alarm->cal_alarm.datetime.time.hour = current_dt.cal_alarm.datetime.time.hour;
- alarm->cal_alarm.datetime.time.min = current_dt.cal_alarm.datetime.time.min;
- break;
- case CALENDAR_ALARM_MATCH_MIN:
- alarm->cal_alarm.datetime.date.year = current_dt.cal_alarm.datetime.date.year;
- alarm->cal_alarm.datetime.date.month = current_dt.cal_alarm.datetime.date.month;
- alarm->cal_alarm.datetime.date.day = current_dt.cal_alarm.datetime.date.day;
- alarm->cal_alarm.datetime.time.hour = current_dt.cal_alarm.datetime.time.hour;
- break;
- case CALENDAR_ALARM_MATCH_HOUR:
- alarm->cal_alarm.datetime.date.year = current_dt.cal_alarm.datetime.date.year;
- alarm->cal_alarm.datetime.date.month = current_dt.cal_alarm.datetime.date.month;
- alarm->cal_alarm.datetime.date.day = current_dt.cal_alarm.datetime.date.day;
- break;
- case CALENDAR_ALARM_MATCH_DAY:
- alarm->cal_alarm.datetime.date.year = current_dt.cal_alarm.datetime.date.year;
- alarm->cal_alarm.datetime.date.month = current_dt.cal_alarm.datetime.date.month;
- break;
- case CALENDAR_ALARM_MATCH_MONTH:
- alarm->cal_alarm.datetime.date.year = current_dt.cal_alarm.datetime.date.year;
- break;
- case CALENDAR_ALARM_MATCH_YEAR:
- break;
- default:
- break;
- }
-
- /* fill up the timestamp */
- tmp = convert_datetime_to_timestamp(calendar, &alarm->cal_alarm.datetime);
- alarm->cal_alarm.timestamp = tmp;
-
- /* calibrate the timestamp */
- calibrate_timestamp(calendar, alarm, &current_dt);
- convert_timestamp_to_datetime(calendar, alarm->cal_alarm.timestamp, &alarm->cal_alarm.datetime);
-}
-
-/** \brief add new alarm into the list in ascending order
- */
-static int32_t calendar_add_new_alarm(struct list_descriptor *list, struct calendar_alarm *alarm)
-{
- struct calendar_descriptor *calendar = CONTAINER_OF(list, struct calendar_descriptor, alarms);
- struct calendar_alarm * head, *it, *prev = NULL;
-
- /*get the head of alarms list*/
- head = (struct calendar_alarm *)list_get_head(list);
-
- /*if head is null, insert new alarm as head*/
- if (!head) {
- list_insert_as_head(list, alarm);
- _calendar_set_comp(&calendar->device, alarm->cal_alarm.timestamp);
- return ERR_NONE;
- }
-
- /*insert the new alarm in accending order, the head will be invoked firstly */
- for (it = head; it; it = (struct calendar_alarm *)list_get_next_element(it)) {
- if (alarm->cal_alarm.timestamp <= it->cal_alarm.timestamp) {
- break;
- }
-
- prev = it;
- }
-
- /*insert new alarm into the list */
- if (it == head) {
- list_insert_as_head(list, alarm);
- /*get the head and set it into register*/
- _calendar_set_comp(&calendar->device, alarm->cal_alarm.timestamp);
-
- } else {
- list_insert_after(prev, alarm);
- }
-
- return ERR_NONE;
-}
-
-/** \brief callback for alarm
- */
-static void calendar_alarm(struct calendar_dev *const dev)
-{
- struct calendar_descriptor *calendar = CONTAINER_OF(dev, struct calendar_descriptor, device);
-
- struct calendar_alarm *head, *it, current_dt;
-
- if ((calendar->flags & SET_ALARM_BUSY) || (calendar->flags & PROCESS_ALARM_BUSY)) {
- calendar->flags |= PROCESS_ALARM_BUSY;
- return;
- }
-
- /* get current timestamp */
- current_dt.cal_alarm.timestamp = _calendar_get_counter(dev);
-
- /* get the head */
- head = (struct calendar_alarm *)list_get_head(&calendar->alarms);
- ASSERT(head);
-
- /* remove all alarms and invoke them*/
- for (it = head; it; it = (struct calendar_alarm *)list_get_head(&calendar->alarms)) {
- /* check the timestamp with current timestamp*/
- if (it->cal_alarm.timestamp <= current_dt.cal_alarm.timestamp) {
- list_remove_head(&calendar->alarms);
- it->callback(calendar);
-
- if (it->cal_alarm.mode == REPEAT) {
- calibrate_timestamp(calendar, it, &current_dt);
- convert_timestamp_to_datetime(calendar, it->cal_alarm.timestamp, &it->cal_alarm.datetime);
- calendar_add_new_alarm(&calendar->alarms, it);
- }
- } else {
- break;
- }
- }
-
- /*if no alarm in the list, register null */
- if (!it) {
- _calendar_register_callback(&calendar->device, NULL);
- return;
- }
-
- /*put the new head into register */
- _calendar_set_comp(&calendar->device, it->cal_alarm.timestamp);
-}
-
-/** \brief Initialize Calendar
- */
-int32_t calendar_init(struct calendar_descriptor *const calendar, const void *hw)
-{
- int32_t ret = 0;
-
- /* Sanity check arguments */
- ASSERT(calendar);
-
- if (calendar->device.hw == hw) {
- /* Already initialized with current configuration */
- return ERR_NONE;
- } else if (calendar->device.hw != NULL) {
- /* Initialized with another configuration */
- return ERR_ALREADY_INITIALIZED;
- }
- calendar->device.hw = (void *)hw;
- ret = _calendar_init(&calendar->device);
- calendar->base_year = DEFAULT_BASE_YEAR;
-
- return ret;
-}
-
-/** \brief Reset the Calendar
- */
-int32_t calendar_deinit(struct calendar_descriptor *const calendar)
-{
- /* Sanity check arguments */
- ASSERT(calendar);
-
- if (calendar->device.hw == NULL) {
- return ERR_NOT_INITIALIZED;
- }
- _calendar_deinit(&calendar->device);
- calendar->device.hw = NULL;
-
- return ERR_NONE;
-}
-
-/** \brief Enable the Calendar
- */
-int32_t calendar_enable(struct calendar_descriptor *const calendar)
-{
- /* Sanity check arguments */
- ASSERT(calendar);
-
- _calendar_enable(&calendar->device);
-
- return ERR_NONE;
-}
-
-/** \brief Disable the Calendar
- */
-int32_t calendar_disable(struct calendar_descriptor *const calendar)
-{
- /* Sanity check arguments */
- ASSERT(calendar);
-
- _calendar_disable(&calendar->device);
-
- return ERR_NONE;
-}
-
-/** \brief Set base year for calendar
- */
-int32_t calendar_set_baseyear(struct calendar_descriptor *const calendar, const uint32_t p_base_year)
-{
- /* Sanity check arguments */
- ASSERT(calendar);
-
- calendar->base_year = p_base_year;
-
- return ERR_NONE;
-}
-
-/** \brief Set time for calendar
- */
-int32_t calendar_set_time(struct calendar_descriptor *const calendar, struct calendar_time *const p_calendar_time)
-{
- struct calendar_date_time dt;
- uint32_t current_ts, new_ts;
-
- /* Sanity check arguments */
- ASSERT(calendar);
-
- /* convert time to timestamp */
- current_ts = _calendar_get_counter(&calendar->device);
- convert_timestamp_to_datetime(calendar, current_ts, &dt);
- dt.time.sec = p_calendar_time->sec;
- dt.time.min = p_calendar_time->min;
- dt.time.hour = p_calendar_time->hour;
-
- new_ts = convert_datetime_to_timestamp(calendar, &dt);
-
- _calendar_set_counter(&calendar->device, new_ts);
-
- return ERR_NONE;
-}
-
-/** \brief Set date for calendar
- */
-int32_t calendar_set_date(struct calendar_descriptor *const calendar, struct calendar_date *const p_calendar_date)
-{
- struct calendar_date_time dt;
- uint32_t current_ts, new_ts;
-
- /* Sanity check arguments */
- ASSERT(calendar);
-
- /* convert date to timestamp */
- current_ts = _calendar_get_counter(&calendar->device);
- convert_timestamp_to_datetime(calendar, current_ts, &dt);
- dt.date.day = p_calendar_date->day;
- dt.date.month = p_calendar_date->month;
- dt.date.year = p_calendar_date->year;
-
- new_ts = convert_datetime_to_timestamp(calendar, &dt);
-
- _calendar_set_counter(&calendar->device, new_ts);
-
- return ERR_NONE;
-}
-
-/** \brief Get date/time for calendar
- */
-int32_t calendar_get_date_time(struct calendar_descriptor *const calendar, struct calendar_date_time *const date_time)
-{
- uint32_t current_ts;
-
- /* Sanity check arguments */
- ASSERT(calendar);
-
- /* convert current timestamp to date/time */
- current_ts = _calendar_get_counter(&calendar->device);
- convert_timestamp_to_datetime(calendar, current_ts, date_time);
-
- return ERR_NONE;
-}
-
-/** \brief Set alarm for calendar
- */
-int32_t calendar_set_alarm(struct calendar_descriptor *const calendar, struct calendar_alarm *const alarm,
- calendar_cb_alarm_t callback)
-{
- struct calendar_alarm *head;
-
- /* Sanity check arguments */
- ASSERT(calendar);
- ASSERT(alarm);
-
- alarm->callback = callback;
-
- fill_alarm(calendar, alarm);
-
- calendar->flags |= SET_ALARM_BUSY;
-
- head = (struct calendar_alarm *)list_get_head(&calendar->alarms);
-
- if (head != NULL) {
- /* already added */
- if (is_list_element(&calendar->alarms, alarm)) {
- if (callback == NULL) {
- /* remove alarm */
- list_delete_element(&calendar->alarms, alarm);
-
- if (!list_get_head(&calendar->alarms)) {
- _calendar_register_callback(&calendar->device, NULL);
- }
- } else {
- /* re-add */
- list_delete_element(&calendar->alarms, alarm);
- calendar_add_new_alarm(&calendar->alarms, alarm);
- }
- } else if (callback != NULL) {
- calendar_add_new_alarm(&calendar->alarms, alarm);
- }
-
- calendar->flags &= ~SET_ALARM_BUSY;
-
- if (calendar->flags & PROCESS_ALARM_BUSY) {
- CRITICAL_SECTION_ENTER()
- calendar->flags &= ~PROCESS_ALARM_BUSY;
- _calendar_set_irq(&calendar->device);
- CRITICAL_SECTION_LEAVE()
- }
- } else if (callback != NULL) {
- /* if head is NULL, Register callback*/
- _calendar_register_callback(&calendar->device, calendar_alarm);
- calendar_add_new_alarm(&calendar->alarms, alarm);
- }
-
- calendar->flags &= ~SET_ALARM_BUSY;
-
- return ERR_NONE;
-}
-
-/** \brief Retrieve driver version
- * \return Current driver version
- */
-uint32_t calendar_get_version(void)
-{
- return CALENDAR_VERSION;
-}