http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a280628a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_count_interrupt.h ---------------------------------------------------------------------- diff --git a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_count_interrupt.h b/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_count_interrupt.h deleted file mode 100755 index 02fdd5d..0000000 --- a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_count_interrupt.h +++ /dev/null @@ -1,90 +0,0 @@ -/** - * \file - * - * \brief SAM RTC Driver (Count Interrupt Mode) - * - * Copyright (C) 2013-2015 Atmel Corporation. All rights reserved. - * - * \asf_license_start - * - * \page License - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. The name of Atmel may not be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 4. This software may only be redistributed and used in connection with an - * Atmel microcontroller product. - * - * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE - * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * \asf_license_stop - * - */ -/* - * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> - */ - -#ifndef RTC_COUNT_INTERRUPT_H_INCLUDED -#define RTC_COUNT_INTERRUPT_H_INCLUDED - -#include "rtc_count.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * \addtogroup asfdoc_sam0_rtc_count_group - * @{ - */ - - /** - * \name Callbacks - * @{ - */ -enum status_code rtc_count_register_callback( - struct rtc_module *const module, - rtc_count_callback_t callback, - enum rtc_count_callback callback_type); - -enum status_code rtc_count_unregister_callback( - struct rtc_module *const module, - enum rtc_count_callback callback_type); - -void rtc_count_enable_callback( - struct rtc_module *const module, - enum rtc_count_callback callback_type); - -void rtc_count_disable_callback( - struct rtc_module *const module, - enum rtc_count_callback callback_type); - -/** @} */ - -/** @} */ - -#ifdef __cplusplus -} -#endif - -#endif /* RTC_COUNT_INTERRUPT_H_INCLUDED */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a280628a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_calendar.c ---------------------------------------------------------------------- diff --git a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_calendar.c b/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_calendar.c deleted file mode 100755 index 189aa55..0000000 --- a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_calendar.c +++ /dev/null @@ -1,665 +0,0 @@ -/** - * \file - * - * \brief SAM RTC Driver (Calendar Mode) - * - * Copyright (c) 2012-2015 Atmel Corporation. All rights reserved. - * - * \asf_license_start - * - * \page License - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. The name of Atmel may not be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 4. This software may only be redistributed and used in connection with an - * Atmel microcontroller product. - * - * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE - * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * \asf_license_stop - * - */ -/* - * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> - */ -#include "rtc_calendar.h" -#include <gclk.h> - -#if !defined(__DOXYGEN__) -struct rtc_module *_rtc_instance[RTC_INST_NUM]; -#endif - -/** - * \brief Determines if the hardware module(s) are currently synchronizing to the bus. - * - * Checks to see if the underlying hardware peripheral module(s) are currently - * synchronizing across multiple clock domains to the hardware bus, This - * function can be used to delay further operations on a module until such time - * that it is ready, to prevent blocking delays for synchronization in the - * user application. - * - * \param[in] module RTC hardware module - * - * \return Synchronization status of the underlying hardware module(s). - * - * \retval false If the module has completed synchronization - * \retval true If the module synchronization is ongoing - */ -static inline bool rtc_calendar_is_syncing(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - if (rtc_module->MODE2.STATUS.reg & RTC_STATUS_SYNCBUSY) { - return true; - } - - return false; -} - -/** - * \brief Enables the RTC module. - * - * Enables the RTC module once it has been configured, ready for use. Most - * module configuration parameters cannot be altered while the module is enabled. - * - * \param[in,out] module Pointer to the software instance struct - */ -void rtc_calendar_enable(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - -#if RTC_CALENDAR_ASYNC == true - system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_RTC); -#endif - - while (rtc_calendar_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Enable RTC module. */ - rtc_module->MODE2.CTRL.reg |= RTC_MODE2_CTRL_ENABLE; -} - -/** - * \brief Disables the RTC module. - * - * Disables the RTC module. - * - * \param[in,out] module Pointer to the software instance struct - */ -void rtc_calendar_disable(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - -#if RTC_CALENDAR_ASYNC == true - system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_RTC); -#endif - - while (rtc_calendar_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Disable RTC module. */ - rtc_module->MODE2.CTRL.reg &= ~RTC_MODE2_CTRL_ENABLE; -} - -/** - * \brief Resets the RTC module - * - * Resets the RTC module to hardware defaults. - * - * \param[in,out] module Pointer to the software instance struct - */ -void rtc_calendar_reset(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Disable module before reset. */ - rtc_calendar_disable(module); - -#if RTC_CALENDAR_ASYNC == true - module->registered_callback = 0; - module->enabled_callback = 0; -#endif - - while (rtc_calendar_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Initiate software reset. */ - rtc_module->MODE2.CTRL.reg |= RTC_MODE2_CTRL_SWRST; -} - -/** - * \brief Convert time structure to register_value. - * Retrieves register_value convert by the time structure. - * - * \param[in, out] module Pointer to the software instance struct - * \param[in] time Pointer to the time structure - * - * \return 32-bit value - */ -uint32_t rtc_calendar_time_to_register_value( - struct rtc_module *const module, - const struct rtc_calendar_time *const time) -{ - /* Initialize return value. */ - uint32_t register_value; - - /* Set year value into register_value minus initial year. */ - register_value = (time->year - module->year_init_value) << - RTC_MODE2_CLOCK_YEAR_Pos; - - /* Set month value into register_value. */ - register_value |= (time->month << RTC_MODE2_CLOCK_MONTH_Pos); - - /* Set day value into register_value. */ - register_value |= (time->day << RTC_MODE2_CLOCK_DAY_Pos); - - /* Set 24 hour value into register_value. */ - register_value |= (time->hour << RTC_MODE2_CLOCK_HOUR_Pos); - - /* Check if 24 h clock and set pm flag. */ - if (!(module->clock_24h) && (time->pm)) { - /* Set pm flag. */ - register_value |= RTC_MODE2_CLOCK_HOUR_PM; - } - - /* Set minute value into register_value. */ - register_value |= (time->minute << RTC_MODE2_CLOCK_MINUTE_Pos); - - /* Set second value into register_value. */ - register_value |= (time->second << RTC_MODE2_CLOCK_SECOND_Pos); - - return register_value; -} - -/** - * \brief Convert register_value to time structure. - * Retrieves the time structure convert by register_value. - * - * \param[in, out] module Pointer to the software instance struct - * \param[in] register_value The value stored in register - * \param[out] time Pointer to the time structure - */ -void rtc_calendar_register_value_to_time( - struct rtc_module *const module, - const uint32_t register_value, - struct rtc_calendar_time *const time) -{ - /* Set year plus value of initial year. */ - time->year = ((register_value & RTC_MODE2_CLOCK_YEAR_Msk) >> - RTC_MODE2_CLOCK_YEAR_Pos) + module->year_init_value; - - /* Set month value into time struct. */ - time->month = ((register_value & RTC_MODE2_CLOCK_MONTH_Msk) >> - RTC_MODE2_CLOCK_MONTH_Pos); - - /* Set day value into time struct. */ - time->day = ((register_value & RTC_MODE2_CLOCK_DAY_Msk) >> - RTC_MODE2_CLOCK_DAY_Pos); - - if (module->clock_24h) { - /* Set hour in 24h mode. */ - time->hour = ((register_value & RTC_MODE2_CLOCK_HOUR_Msk) >> - RTC_MODE2_CLOCK_HOUR_Pos); - } else { - /* Set hour in 12h mode. */ - time->hour = ((register_value & - (RTC_MODE2_CLOCK_HOUR_Msk & ~RTC_MODE2_CLOCK_HOUR_PM)) >> - RTC_MODE2_CLOCK_HOUR_Pos); - - /* Set pm flag */ - time->pm = ((register_value & RTC_MODE2_CLOCK_HOUR_PM) != 0); - } - - /* Set minute value into time struct. */ - time->minute = ((register_value & RTC_MODE2_CLOCK_MINUTE_Msk) >> - RTC_MODE2_CLOCK_MINUTE_Pos); - - /* Set second value into time struct. */ - time->second = ((register_value & RTC_MODE2_CLOCK_SECOND_Msk) >> - RTC_MODE2_CLOCK_SECOND_Pos); -} - -/** - * \internal Applies the given configuration. - * - * Set the configurations given from the configuration structure to the - * hardware module. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] config Pointer to the configuration structure. - */ -static void _rtc_calendar_set_config( - struct rtc_module *const module, - const struct rtc_calendar_config *const config) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Set up temporary register value. */ - uint16_t tmp_reg; - - /* Set to calendar mode and set the prescaler. */ - tmp_reg = RTC_MODE2_CTRL_MODE(2) | config->prescaler; - - /* Check clock mode. */ - if (!(config->clock_24h)) { - /* Set clock mode 12h. */ - tmp_reg |= RTC_MODE2_CTRL_CLKREP; - } - - /* Check for clear on compare match. */ - if (config->clear_on_match) { - /* Set clear on compare match. */ - tmp_reg |= RTC_MODE2_CTRL_MATCHCLR; - } - - /* Set temporary value to register. */ - rtc_module->MODE2.CTRL.reg = tmp_reg; - - /* Check to set continuously clock read update mode. */ - if (config->continuously_update) { - /* Set continuously mode. */ - rtc_module->MODE2.READREQ.reg |= RTC_READREQ_RCONT; - } - - /* Set alarm time registers. */ - for (uint8_t i = 0; i < RTC_NUM_OF_ALARMS; i++) { - rtc_calendar_set_alarm(module, &(config->alarm[i]), (enum rtc_calendar_alarm)i); - } -} - -/** - * \brief Initializes the RTC module with given configurations. - * - * Initializes the module, setting up all given configurations to provide - * the desired functionality of the RTC. - * - * \param[out] module Pointer to the software instance struct - * \param[in] hw Pointer to hardware instance - * \param[in] config Pointer to the configuration structure - */ -void rtc_calendar_init( - struct rtc_module *const module, - Rtc *const hw, - const struct rtc_calendar_config *const config) -{ - /* Sanity check arguments */ - Assert(module); - Assert(hw); - Assert(config); - - /* Initialize device instance */ - module->hw = hw; - - /* Turn on the digital interface clock */ - system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBA, PM_APBAMASK_RTC); - - /* Set up GCLK */ - struct system_gclk_chan_config gclk_chan_conf; - system_gclk_chan_get_config_defaults(&gclk_chan_conf); - gclk_chan_conf.source_generator = GCLK_GENERATOR_2; - system_gclk_chan_set_config(RTC_GCLK_ID, &gclk_chan_conf); - system_gclk_chan_enable(RTC_GCLK_ID); - - /* Reset module to hardware defaults. */ - rtc_calendar_reset(module); - - /* Save conf_struct internally for continued use. */ - module->clock_24h = config->clock_24h; - module->continuously_update = config->continuously_update; - module->year_init_value = config->year_init_value; - -#if (RTC_INST_NUM == 1) - _rtc_instance[0] = module; -#else - /* Register this instance for callbacks*/ - _rtc_instance[_rtc_get_inst_index(hw)] = module; -#endif - - /* Set config. */ - _rtc_calendar_set_config(module, config); -} - -/** - * \brief Swaps between 12h and 24h clock mode. - * - * Swaps the current RTC time mode: - * - If currently in 12h mode, it will swap to 24h - * - If currently in 24h mode, it will swap to 12h - * - * \note This will not change setting in user's configuration structure. - * - * \param[in, out] module Pointer to the software instance struct - */ -void rtc_calendar_swap_time_mode(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Initialize time structure. */ - struct rtc_calendar_time time; - struct rtc_calendar_alarm_time alarm; - - /* Get current time. */ - rtc_calendar_get_time(module, &time); - - /* Check current mode. */ - if (module->clock_24h) { - /* Set pm flag. */ - time.pm = (uint8_t)(time.hour / 12); - - /* Set 12h clock hour value. */ - time.hour = time.hour % 12; - if (time.hour == 0) { - time.hour = 12; - } - - /* Update alarms */ - for (uint8_t i = 0; i < RTC_NUM_OF_ALARMS; i++) { - rtc_calendar_get_alarm(module, &alarm, (enum rtc_calendar_alarm)i); - alarm.time.pm = (uint8_t)(alarm.time.hour / 12); - alarm.time.hour = alarm.time.hour % 12; - if (alarm.time.hour == 0) { - alarm.time.hour = 12; - } - module->clock_24h = false; - rtc_calendar_set_alarm(module, &alarm, (enum rtc_calendar_alarm)i); - module->clock_24h = true; - } - - /* Change value in configuration structure. */ - module->clock_24h = false; - } else { - /* Set hour value based on pm flag. */ - if (time.pm == 1) { - time.hour = time.hour + 12; - - time.pm = 0; - } else if (time.hour == 12) { - time.hour = 0; - } - - /* Update alarms */ - for (uint8_t i = 0; i < RTC_NUM_OF_ALARMS; i++) { - rtc_calendar_get_alarm(module, &alarm, (enum rtc_calendar_alarm)i); - if (alarm.time.pm == 1) { - alarm.time.hour = alarm.time.hour + 12; - alarm.time.pm = 0; - module->clock_24h = true; - rtc_calendar_set_alarm(module, &alarm, (enum rtc_calendar_alarm)i); - module->clock_24h = false; - } else if (alarm.time.hour == 12) { - alarm.time.hour = 0; - } - } - - /* Change value in configuration structure. */ - module->clock_24h = true; - } - - /* Disable RTC so new configuration can be set. */ - rtc_calendar_disable(module); - - /* Toggle mode. */ - rtc_module->MODE2.CTRL.reg ^= RTC_MODE2_CTRL_CLKREP; - - /* Enable RTC. */ - rtc_calendar_enable(module); - - /* Set new time format in CLOCK register. */ - rtc_calendar_set_time(module, &time); -} - -/** - * \brief Set the current calendar time to desired time. - * - * Sets the time provided to the calendar. - * - * \param[in, out] module Pointer to the software instance struct - * \param[in] time The time to set in the calendar - */ -void rtc_calendar_set_time( - struct rtc_module *const module, - const struct rtc_calendar_time *const time) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - uint32_t register_value = rtc_calendar_time_to_register_value(module, time); - - while (rtc_calendar_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Write value to register. */ - rtc_module->MODE2.CLOCK.reg = register_value; -} - -/** - * \brief Get the current calendar value. - * - * Retrieves the current time of the calendar. - * - * \param[in, out] module Pointer to the software instance struct - * \param[out] time Pointer to value that will be filled with current time - */ -void rtc_calendar_get_time( - struct rtc_module *const module, - struct rtc_calendar_time *const time) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Change of read method based on value of continuously_update value in - * the configuration structure. */ - if (!(module->continuously_update)) { - /* Request read on CLOCK register. */ - rtc_module->MODE2.READREQ.reg = RTC_READREQ_RREQ; - - while (rtc_calendar_is_syncing(module)) { - /* Wait for synchronization */ - } - } - - /* Read value. */ - uint32_t register_value = rtc_module->MODE2.CLOCK.reg; - - /* Convert value to time structure. */ - rtc_calendar_register_value_to_time(module, register_value, time); -} - -/** - * \brief Set the alarm time for the specified alarm. - * - * Sets the time and mask specified to the requested alarm. - * - * \param[in, out] module Pointer to the software instance struct - * \param[in] alarm The alarm struct to set the alarm with - * \param[in] alarm_index The index of the alarm to set - * - * \return Status of setting alarm. - * \retval STATUS_OK If alarm was set correctly - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided - */ -enum status_code rtc_calendar_set_alarm( - struct rtc_module *const module, - const struct rtc_calendar_alarm_time *const alarm, - const enum rtc_calendar_alarm alarm_index) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Sanity check. */ - if ((uint32_t)alarm_index > RTC_NUM_OF_ALARMS) { - return STATUS_ERR_INVALID_ARG; - } - - /* Get register_value from time. */ - uint32_t register_value = rtc_calendar_time_to_register_value(module, &(alarm->time)); - - while (rtc_calendar_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Set alarm value. */ - rtc_module->MODE2.Mode2Alarm[alarm_index].ALARM.reg = register_value; - - /* Set alarm mask */ - rtc_module->MODE2.Mode2Alarm[alarm_index].MASK.reg = alarm->mask; - - return STATUS_OK; -} - -/** - * \brief Get the current alarm time of specified alarm. - * - * Retrieves the current alarm time for the alarm specified alarm. - * - * \param[in, out] module Pointer to the software instance struct - * \param[out] alarm Pointer to the struct that will be filled with alarm - * time and mask of the specified alarm - * \param[in] alarm_index Index of alarm to get alarm time from - * - * \return Status of getting alarm. - * \retval STATUS_OK If alarm was read correctly - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided - */ -enum status_code rtc_calendar_get_alarm( - struct rtc_module *const module, - struct rtc_calendar_alarm_time *const alarm, - const enum rtc_calendar_alarm alarm_index) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Sanity check. */ - if ((uint32_t)alarm_index > RTC_NUM_OF_ALARMS) { - return STATUS_ERR_INVALID_ARG; - } - - /* Read alarm value. */ - uint32_t register_value = - rtc_module->MODE2.Mode2Alarm[alarm_index].ALARM.reg; - - /* Convert to time structure. */ - rtc_calendar_register_value_to_time(module, register_value, &(alarm->time)); - - /* Read alarm mask */ - alarm->mask = (enum rtc_calendar_alarm_mask)rtc_module->MODE2.Mode2Alarm[alarm_index].MASK.reg; - - return STATUS_OK; -} - -/** - * \brief Calibrate for too-slow or too-fast oscillator. - * - * When used, the RTC will compensate for an inaccurate oscillator. The - * RTC module will add or subtract cycles from the RTC prescaler to adjust the - * frequency in approximately 1 PPM steps. The provided correction value should - * be between -127 and 127, allowing for a maximum 127 PPM correction in either - * direction. - * - * If no correction is needed, set value to zero. - * - * \note Can only be used when the RTC is operated at 1Hz. - * - * \param[in, out] module Pointer to the software instance struct - * \param[in] value Between -127 and 127 used for the correction - * - * \return Status of the calibration procedure. - * \retval STATUS_OK If calibration was done correctly - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided - */ -enum status_code rtc_calendar_frequency_correction( - struct rtc_module *const module, - const int8_t value) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Check if valid argument. */ - if (abs(value) > 0x7F) { - /* Value bigger than allowed, return invalid argument. */ - return STATUS_ERR_INVALID_ARG; - } - - uint32_t new_correction_value; - - /* Load the new correction value as a positive value, sign added later */ - new_correction_value = abs(value); - - /* Convert to positive value and adjust register sign bit. */ - if (value < 0) { - new_correction_value |= RTC_FREQCORR_SIGN; - } - - while (rtc_calendar_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Set value. */ - rtc_module->MODE2.FREQCORR.reg = new_correction_value; - - return STATUS_OK; -} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a280628a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_calendar_interrupt.c ---------------------------------------------------------------------- diff --git a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_calendar_interrupt.c b/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_calendar_interrupt.c deleted file mode 100755 index 58acf77..0000000 --- a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_calendar_interrupt.c +++ /dev/null @@ -1,270 +0,0 @@ -/** - * \file - * - * \brief SAM RTC Driver (Calendar Interrupt Mode) - * - * Copyright (C) 2013-2015 Atmel Corporation. All rights reserved. - * - * \asf_license_start - * - * \page License - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. The name of Atmel may not be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 4. This software may only be redistributed and used in connection with an - * Atmel microcontroller product. - * - * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE - * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * \asf_license_stop - * - */ -/* - * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> - */ -#include "rtc_calendar_interrupt.h" - -extern struct rtc_module *_rtc_instance[RTC_INST_NUM]; - -/** - * \brief Registers callback for the specified callback type - * - * Associates the given callback function with the - * specified callback type. - * To enable the callback, the \ref rtc_calendar_enable_callback function - * must be used. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] callback Pointer to the function desired for the specified - * callback - * \param[in] callback_type Callback type to register - * - * \return Status of registering callback. - * \retval STATUS_OK Registering was done successfully - * \retval STATUS_ERR_INVALID_ARG If trying to register a callback not available - */ -enum status_code rtc_calendar_register_callback( - struct rtc_module *const module, - rtc_calendar_callback_t callback, - enum rtc_calendar_callback callback_type) -{ - - enum status_code status = STATUS_OK; - - /* Overflow callback */ - if (callback_type == RTC_CALENDAR_CALLBACK_OVERFLOW) { - status = STATUS_OK; - } else if (callback_type > RTC_NUM_OF_ALARMS) { - /* Make sure alarm callback can be registered */ - status = STATUS_ERR_INVALID_ARG; - } - - if (status == STATUS_OK) { - /* Register callback */ - module->callbacks[callback_type] = callback; - /* Set corresponding bit to set callback as registered */ - module->registered_callback |= (1 << callback_type); - } - - return status; -} - -/** - * \brief Unregisters callback for the specified callback type - * - * When called, the currently registered callback for the given callback type - * will be removed. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] callback_type Specifies the callback type to unregister - * - * \return Status of unregistering callback. - * \retval STATUS_OK Unregistering was done successfully - * \retval STATUS_ERR_INVALID_ARG If trying to unregister a callback not available - */ -enum status_code rtc_calendar_unregister_callback( - struct rtc_module *const module, - enum rtc_calendar_callback callback_type) -{ - enum status_code status = STATUS_OK; - - /* Overflow callback */ - if (callback_type == RTC_CALENDAR_CALLBACK_OVERFLOW) { - status = STATUS_OK; - } else if (callback_type > RTC_NUM_OF_ALARMS) { - /* Make sure alarm callback can be unregistered */ - status = STATUS_ERR_INVALID_ARG; - } - - if (status == STATUS_OK) { - /* Unregister callback */ - module->callbacks[callback_type] = NULL; - - /* Clear corresponding bit to set callback as unregistered */ - module->registered_callback &= ~(1 << callback_type); - } - return status; -} - -/** - * \brief Enables callback - * - * Enables the callback specified by the callback_type. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] callback_type Callback type to enable - */ -void rtc_calendar_enable_callback( - struct rtc_module *const module, - enum rtc_calendar_callback callback_type) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - if (callback_type == RTC_CALENDAR_CALLBACK_OVERFLOW) { - rtc_module->MODE2.INTENSET.reg = RTC_MODE2_INTFLAG_OVF; - } else { - rtc_module->MODE2.INTENSET.reg = RTC_MODE2_INTFLAG_ALARM(1 << callback_type); - } - /* Mark callback as enabled. */ - module->enabled_callback |= (1 << callback_type); -} - -/** - * \brief Disables callback - * - * Disables the callback specified by the callback_type. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] callback_type Callback type to disable - */ -void rtc_calendar_disable_callback( - struct rtc_module *const module, - enum rtc_calendar_callback callback_type) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Disable interrupt */ - if (callback_type == RTC_CALENDAR_CALLBACK_OVERFLOW) { - rtc_module->MODE2.INTENCLR.reg = RTC_MODE2_INTFLAG_OVF; - } else { - rtc_module->MODE2.INTENCLR.reg = RTC_MODE2_INTFLAG_ALARM(1 << callback_type); - } - - /* Mark callback as disabled. */ - module->enabled_callback &= ~(1 << callback_type); -} - -/** - * \internal Interrupt handler for RTC - * - * \param [in] instance_index Default value 0 - */ -static void _rtc_interrupt_handler(const uint32_t instance_index) -{ - struct rtc_module *module = _rtc_instance[instance_index]; - - Rtc *const rtc_module = module->hw; - - /* Combine callback registered and enabled masks */ - uint16_t callback_mask = module->enabled_callback; - callback_mask &= module->registered_callback; - - /* Read and mask interrupt flag register */ - uint16_t interrupt_status = rtc_module->MODE2.INTFLAG.reg; - interrupt_status &= rtc_module->MODE2.INTENSET.reg; - - if (interrupt_status & RTC_MODE2_INTFLAG_OVF) { - /* Overflow interrupt */ - if (callback_mask & (1 << RTC_CALENDAR_CALLBACK_OVERFLOW)) { - module->callbacks[RTC_CALENDAR_CALLBACK_OVERFLOW](); - } - - /* Clear interrupt flag */ - rtc_module->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_OVF; - - } else if (interrupt_status & RTC_MODE2_INTFLAG_ALARM(1 << 0)) { - /* Alarm 0 interrupt */ - if (callback_mask & (1 << RTC_CALENDAR_CALLBACK_ALARM_0)) { - module->callbacks[RTC_CALENDAR_CALLBACK_ALARM_0](); - } - /* Clear interrupt flag */ - rtc_module->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_ALARM(1 << 0); - - } else if (interrupt_status & RTC_MODE2_INTFLAG_ALARM(1 << 1)) { - #if (RTC_NUM_OF_ALARMS > 1) || defined(__DOXYGEN__) - /* Alarm 1 interrupt */ - if (callback_mask & (1 << RTC_CALENDAR_CALLBACK_ALARM_1)) { - module->callbacks[RTC_CALENDAR_CALLBACK_ALARM_1](); - } - /* Clear interrupt flag */ - rtc_module->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_ALARM(1 << 1); - #endif - - } else if (interrupt_status & RTC_MODE2_INTFLAG_ALARM(1 << 2)) { - #if (RTC_NUM_OF_ALARMS > 2) || defined(__DOXYGEN__) - /* Alarm 2 interrupt */ - if (callback_mask & (1 << RTC_CALENDAR_CALLBACK_ALARM_2)) { - module->callbacks[RTC_CALENDAR_CALLBACK_ALARM_2](); - } - /* Clear interrupt flag */ - rtc_module->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_ALARM(1 << 2); - #endif - - } else if (interrupt_status & RTC_MODE2_INTFLAG_ALARM(1 << 3)) { - #if (RTC_NUM_OF_ALARMS > 3) || defined(__DOXYGEN__) - /* Alarm 3 interrupt */ - if (callback_mask & (1 << RTC_CALENDAR_CALLBACK_ALARM_3)) { - module->callbacks[RTC_CALENDAR_CALLBACK_ALARM_3](); - } - /* Clear interrupt flag */ - rtc_module->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_ALARM(1 << 3); - #endif - } -} - -/** - * \internal ISR handler for RTC - */ -#if (RTC_INST_NUM == 1) -void RTC_Handler(void) -{ - _rtc_interrupt_handler(0); -} -#elif (RTC_INST_NUM > 1) -# define _RTC_INTERRUPT_HANDLER(n, unused) \ - void RTC##n##_Handler(void) \ - { \ - _rtc_interrupt_handler(n); \ - } - -MREPEAT(RTC_INST_NUM, _RTC_INTERRUPT_HANDLER, ~) -#endif /* (RTC_INST_NUM > 1) */ \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a280628a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_count.c ---------------------------------------------------------------------- diff --git a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_count.c b/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_count.c deleted file mode 100755 index a3f0dbc..0000000 --- a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_count.c +++ /dev/null @@ -1,773 +0,0 @@ -/** - * \file - * - * \brief SAM RTC Driver (Count Mode) - * - * Copyright (C) 2012-2015 Atmel Corporation. All rights reserved. - * - * \asf_license_start - * - * \page License - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. The name of Atmel may not be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 4. This software may only be redistributed and used in connection with an - * Atmel microcontroller product. - * - * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE - * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * \asf_license_stop - * - */ -/* - * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> - */ -#include "rtc_count.h" -#include <gclk.h> - -#if !defined(__DOXYGEN__) -struct rtc_module *_rtc_instance[RTC_INST_NUM]; -#endif - -/** - * \brief Determines if the hardware module(s) are currently synchronizing to the bus. - * - * Checks to see if the underlying hardware peripheral module(s) are currently - * synchronizing across multiple clock domains to the hardware bus, This - * function can be used to delay further operations on a module until such time - * that it is ready, to prevent blocking delays for synchronization in the - * user application. - * - * \param[in] module RTC hardware module - * - * \return Synchronization status of the underlying hardware module(s). - * - * \retval true if the module synchronization is ongoing - * \retval false if the module has completed synchronization - */ -static bool rtc_count_is_syncing(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - if (rtc_module->MODE0.STATUS.reg & RTC_STATUS_SYNCBUSY) { - return true; - } - - return false; -} - -/** - * \brief Enables the RTC module. - * - * Enables the RTC module once it has been configured, ready for use. Most - * module configuration parameters cannot be altered while the module is enabled. - * - * \param[in,out] module RTC hardware module - */ -void rtc_count_enable(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - -#if RTC_COUNT_ASYNC == true - system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_RTC); -#endif - - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Enable RTC module. */ - rtc_module->MODE0.CTRL.reg |= RTC_MODE0_CTRL_ENABLE; -} - -/** - * \brief Disables the RTC module. - * - * Disables the RTC module. - * - * \param[in,out] module RTC hardware module - */ -void rtc_count_disable(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - -#if RTC_COUNT_ASYNC == true - system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_RTC); -#endif - - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Disable RTC module. */ - rtc_module->MODE0.CTRL.reg &= ~RTC_MODE0_CTRL_ENABLE; -} - -/** - * \brief Resets the RTC module. - * Resets the RTC to hardware defaults. - * - * \param[in,out] module Pointer to the software instance struct - */ -void rtc_count_reset(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Disable module before reset. */ - rtc_count_disable(module); - -#if RTC_COUNT_ASYNC == true - module->registered_callback = 0; - module->enabled_callback = 0; -#endif - - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Initiate software reset. */ - rtc_module->MODE0.CTRL.reg |= RTC_MODE0_CTRL_SWRST; -} - -/** - * \internal Applies the given configuration. - * - * Sets the configurations given from the configuration structure to the - * hardware module. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] config Pointer to the configuration structure. - * - * \return Status of the configuration procedure. - * \retval STATUS_OK RTC configurations was set successfully. - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were given. - */ -static enum status_code _rtc_count_set_config( - struct rtc_module *const module, - const struct rtc_count_config *const config) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - rtc_module->MODE0.CTRL.reg = RTC_MODE0_CTRL_MODE(0) | config->prescaler; - - /* Set mode and clear on match if applicable. */ - switch (config->mode) { - case RTC_COUNT_MODE_32BIT: - /* Set 32bit mode and clear on match if applicable. */ - rtc_module->MODE0.CTRL.reg |= RTC_MODE0_CTRL_MODE(0); - - /* Check if clear on compare match should be set. */ - if (config->clear_on_match) { - /* Set clear on match. */ - rtc_module->MODE0.CTRL.reg |= RTC_MODE0_CTRL_MATCHCLR; - } - /* Set compare values. */ - for (uint8_t i = 0; i < RTC_NUM_OF_COMP32; i++) { - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - - rtc_count_set_compare(module, config->compare_values[i], - (enum rtc_count_compare)i); - } - break; - - case RTC_COUNT_MODE_16BIT: - /* Set 16bit mode. */ - rtc_module->MODE1.CTRL.reg |= RTC_MODE1_CTRL_MODE(1); - - /* Check if match on clear is set, and return invalid - * argument if set. */ - if (config->clear_on_match) { - Assert(false); - return STATUS_ERR_INVALID_ARG; - } - /* Set compare values. */ - for (uint8_t i = 0; i < RTC_NUM_OF_COMP16; i++) { - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - - rtc_count_set_compare(module, config->compare_values[i], - (enum rtc_count_compare)i); - } - break; - default: - Assert(false); - return STATUS_ERR_INVALID_ARG; - } - - /* Check to set continuously clock read update mode. */ - if (config->continuously_update) { - /* Set continuously mode. */ - rtc_module->MODE0.READREQ.reg |= RTC_READREQ_RCONT; - } - - /* Return status OK if everything was configured. */ - return STATUS_OK; -} - -/** - * \brief Initializes the RTC module with given configurations. - * - * Initializes the module, setting up all given configurations to provide - * the desired functionality of the RTC. - * - * \param[out] module Pointer to the software instance struct - * \param[in] hw Pointer to hardware instance - * \param[in] config Pointer to the configuration structure - * - * \return Status of the initialization procedure. - * \retval STATUS_OK If the initialization was run stressfully - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were given - */ -enum status_code rtc_count_init( - struct rtc_module *const module, - Rtc *const hw, - const struct rtc_count_config *const config) -{ - /* Sanity check arguments */ - Assert(module); - Assert(hw); - Assert(config); - - /* Initialize device instance */ - module->hw = hw; - - /* Turn on the digital interface clock */ - system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBA, PM_APBAMASK_RTC); - - /* Set up GCLK */ - struct system_gclk_chan_config gclk_chan_conf; - system_gclk_chan_get_config_defaults(&gclk_chan_conf); - gclk_chan_conf.source_generator = GCLK_GENERATOR_2; - system_gclk_chan_set_config(RTC_GCLK_ID, &gclk_chan_conf); - system_gclk_chan_enable(RTC_GCLK_ID); - - /* Reset module to hardware defaults. */ - rtc_count_reset(module); - - /* Save conf_struct internally for continued use. */ - module->mode = config->mode; - module->continuously_update = config->continuously_update; - -# if (RTC_INST_NUM == 1) - _rtc_instance[0] = module; -# else - /* Register this instance for callbacks*/ - _rtc_instance[_rtc_get_inst_index(hw)] = module; -# endif - - /* Set config and return status. */ - return _rtc_count_set_config(module, config); -} - -/** - * \brief Set the current count value to desired value. - * - * Sets the value of the counter to the specified value. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] count_value The value to be set in count register - * - * \return Status of setting the register. - * \retval STATUS_OK If everything was executed correctly - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided - */ -enum status_code rtc_count_set_count( - struct rtc_module *const module, - const uint32_t count_value) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Set count according to mode */ - switch(module->mode){ - case RTC_COUNT_MODE_32BIT: - /* Write value to register. */ - rtc_module->MODE0.COUNT.reg = count_value; - - break; - - case RTC_COUNT_MODE_16BIT: - /* Check if 16-bit value is provided. */ - if(count_value > 0xffff){ - return STATUS_ERR_INVALID_ARG; - } - - /* Write value to register. */ - rtc_module->MODE1.COUNT.reg = (uint32_t)count_value; - - break; - - default: - Assert(false); - return STATUS_ERR_INVALID_ARG; - } - return STATUS_OK; -} - -/** - * \brief Get the current count value. - * - * \param[in,out] module Pointer to the software instance struct - * - * Returns the current count value. - * - * \return The current counter value as a 32-bit unsigned integer. - */ -uint32_t rtc_count_get_count(struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Initialize return value. */ - uint32_t ret_val; - - /* Change of read method based on value of continuously_update value in - * the configuration structure. */ - if(!(module->continuously_update)) { - /* Request read on count register. */ - rtc_module->MODE0.READREQ.reg = RTC_READREQ_RREQ; - - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - } - - /* Read value based on mode. */ - switch (module->mode) { - case RTC_COUNT_MODE_32BIT: - /* Return count value in 32-bit mode. */ - ret_val = rtc_module->MODE0.COUNT.reg; - - break; - - case RTC_COUNT_MODE_16BIT: - /* Return count value in 16-bit mode. */ - ret_val = (uint32_t)rtc_module->MODE1.COUNT.reg; - - break; - - default: - Assert(false); - /* Counter not initialized. Assume counter value 0.*/ - ret_val = 0; - break; - } - - return ret_val; -} - -/** - * \brief Set the compare value for the specified compare. - * - * Sets the value specified by the implementer to the requested compare. - * - * \note Compare 4 and 5 are only available in 16-bit mode. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] comp_value The value to be written to the compare - * \param[in] comp_index Index of the compare to set - * - * \return Status indicating if compare was successfully set. - * \retval STATUS_OK If compare was successfully set - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided - * \retval STATUS_ERR_BAD_FORMAT If the module was not initialized in a mode - */ -enum status_code rtc_count_set_compare( - struct rtc_module *const module, - const uint32_t comp_value, - const enum rtc_count_compare comp_index) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Set compare values based on operation mode. */ - switch (module->mode) { - case RTC_COUNT_MODE_32BIT: - /* Check sanity of comp_index. */ - if ((uint32_t)comp_index > RTC_NUM_OF_COMP32) { - return STATUS_ERR_INVALID_ARG; - } - - /* Set compare value for COMP. */ - rtc_module->MODE0.COMP[comp_index].reg = comp_value; - - break; - - case RTC_COUNT_MODE_16BIT: - /* Check sanity of comp_index. */ - if ((uint32_t)comp_index > RTC_NUM_OF_COMP16) { - return STATUS_ERR_INVALID_ARG; - } - - /* Check that 16-bit value is provided. */ - if (comp_value > 0xffff) { - Assert(false); - return STATUS_ERR_INVALID_ARG; - } - - /* Set compare value for COMP. */ - rtc_module->MODE1.COMP[comp_index].reg = comp_value & 0xffff; - - break; - - default: - Assert(false); - return STATUS_ERR_BAD_FORMAT; - } - - /* Return status if everything is OK. */ - return STATUS_OK; -} - -/** - * \brief Get the current compare value of specified compare. - * - * Retrieves the current value of the specified compare. - * - * \note Compare 4 and 5 are only available in 16-bit mode. - * - * \param[in,out] module Pointer to the software instance struct - * \param[out] comp_value Pointer to 32-bit integer that will be populated with - * the current compare value - * \param[in] comp_index Index of compare to check - * - * \return Status of the reading procedure. - * \retval STATUS_OK If the value was read correctly - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided - * \retval STATUS_ERR_BAD_FORMAT If the module was not initialized in a mode - */ -enum status_code rtc_count_get_compare( - struct rtc_module *const module, - uint32_t *const comp_value, - const enum rtc_count_compare comp_index) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - switch (module->mode) { - case RTC_COUNT_MODE_32BIT: - /* Check sanity of comp_index. */ - if ((uint32_t)comp_index > RTC_NUM_OF_COMP32) { - return STATUS_ERR_INVALID_ARG; - } - - /* Get compare value for COMP. */ - *comp_value = rtc_module->MODE0.COMP[comp_index].reg; - - break; - - case RTC_COUNT_MODE_16BIT: - /* Check sanity of comp_index. */ - if ((uint32_t)comp_index > RTC_NUM_OF_COMP16) { - return STATUS_ERR_INVALID_ARG; - } - - /* Get compare value for COMP. */ - *comp_value = (uint32_t)rtc_module->MODE1.COMP[comp_index].reg; - - break; - - default: - Assert(false); - return STATUS_ERR_BAD_FORMAT; - } - /* Return status showing everything is OK. */ - return STATUS_OK; -} - -/** - * \brief Retrieves the value of period. - * - * Retrieves the value of the period for the 16-bit mode counter. - * - * \note Only available in 16-bit mode. - * - * \param[in,out] module Pointer to the software instance struct - * \param[out] period_value Pointer to value for return argument - * - * \return Status of getting the period value. - * \retval STATUS_OK If the period value was read correctly - * \retval STATUS_ERR_UNSUPPORTED_DEV If incorrect mode was set - */ -enum status_code rtc_count_get_period( - struct rtc_module *const module, - uint16_t *const period_value) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Check that correct mode is set. */ - if (module->mode != RTC_COUNT_MODE_16BIT) { - return STATUS_ERR_UNSUPPORTED_DEV; - } - - /* Returns the value. */ - *period_value = rtc_module->MODE1.PER.reg; - - return STATUS_OK; -} - -/** - * \brief Set the given value to the period. - * - * Sets the given value to the period. - * - * \note Only available in 16-bit mode. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] period_value The value to set to the period - * - * \return Status of setting the period value. - * \retval STATUS_OK If the period was set correctly - * \retval STATUS_ERR_UNSUPPORTED_DEV If module is not operated in 16-bit mode - */ -enum status_code rtc_count_set_period( - struct rtc_module *const module, - const uint16_t period_value) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Check that correct mode is set. */ - if (module->mode != RTC_COUNT_MODE_16BIT) { - return STATUS_ERR_UNSUPPORTED_DEV; - } - - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Write value to register. */ - rtc_module->MODE1.PER.reg = period_value; - - return STATUS_OK; -} - -/** - * \brief Check if RTC compare match has occurred. - * - * Checks the compare flag to see if a match has occurred. The compare flag is - * set when there is a compare match between counter and the compare. - * - * \note Compare 4 and 5 are only available in 16-bit mode. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] comp_index Index of compare to check current flag - */ -bool rtc_count_is_compare_match( - struct rtc_module *const module, - const enum rtc_count_compare comp_index) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Check sanity. */ - switch (module->mode) { - case RTC_COUNT_MODE_32BIT: - /* Check sanity for 32-bit mode. */ - if (comp_index > RTC_NUM_OF_COMP32) { - return false; - } - - break; - - case RTC_COUNT_MODE_16BIT: - /* Check sanity for 16-bit mode. */ - if (comp_index > RTC_NUM_OF_COMP16) { - return false; - } - - break; - - default: - Assert(false); - return false; - } - - /* Set status of INTFLAG as return argument. */ - return (rtc_module->MODE0.INTFLAG.reg & (1 << comp_index)) ? true : false; -} - -/** - * \brief Clears RTC compare match flag. - * - * Clears the compare flag. The compare flag is set when there is a compare - * match between the counter and the compare. - * - * \note Compare 4 and 5 are only available in 16-bit mode. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] comp_index Index of compare to check current flag - * - * \return Status indicating if flag was successfully cleared. - * \retval STATUS_OK If flag was successfully cleared - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided - * \retval STATUS_ERR_BAD_FORMAT If the module was not initialized in a mode - */ -enum status_code rtc_count_clear_compare_match( - struct rtc_module *const module, - const enum rtc_count_compare comp_index) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Check sanity. */ - switch (module->mode){ - case RTC_COUNT_MODE_32BIT: - /* Check sanity for 32-bit mode. */ - if (comp_index > RTC_NUM_OF_COMP32) { - return STATUS_ERR_INVALID_ARG; - } - - break; - - case RTC_COUNT_MODE_16BIT: - /* Check sanity for 16-bit mode. */ - if (comp_index > RTC_NUM_OF_COMP16) { - return STATUS_ERR_INVALID_ARG; - } - - break; - - default: - Assert(false); - return STATUS_ERR_BAD_FORMAT; - } - - /* Clear INTFLAG. */ - rtc_module->MODE0.INTFLAG.reg = RTC_MODE1_INTFLAG_CMP(1 << comp_index); - - return STATUS_OK; -} - -/** - * \brief Calibrate for too-slow or too-fast oscillator. - * - * When used, the RTC will compensate for an inaccurate oscillator. The - * RTC module will add or subtract cycles from the RTC prescaler to adjust the - * frequency in approximately 1 PPM steps. The provided correction value should - * be between 0 and 127, allowing for a maximum 127 PPM correction. - * - * If no correction is needed, set value to zero. - * - * \note Can only be used when the RTC is operated in 1Hz. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] value Ranging from -127 to 127 used for the correction - * - * \return Status of the calibration procedure. - * \retval STATUS_OK If calibration was executed correctly - * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided - */ -enum status_code rtc_count_frequency_correction( - struct rtc_module *const module, - const int8_t value) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Check if valid argument. */ - if (abs(value) > 0x7F) { - /* Value bigger than allowed, return invalid argument. */ - return STATUS_ERR_INVALID_ARG; - } - - uint32_t new_correction_value; - - /* Load the new correction value as a positive value, sign added later */ - new_correction_value = abs(value); - - /* Convert to positive value and adjust register sign bit. */ - if (value < 0) { - new_correction_value |= RTC_FREQCORR_SIGN; - } - - while (rtc_count_is_syncing(module)) { - /* Wait for synchronization */ - } - - /* Set value. */ - rtc_module->MODE0.FREQCORR.reg = new_correction_value; - - return STATUS_OK; -} - http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a280628a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_count_interrupt.c ---------------------------------------------------------------------- diff --git a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_count_interrupt.c b/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_count_interrupt.c deleted file mode 100755 index 0622bee..0000000 --- a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_sam_d_r/rtc_count_interrupt.c +++ /dev/null @@ -1,320 +0,0 @@ -/** - * \file - * - * \brief SAM RTC Driver (Count Interrupt Mode) - * - * Copyright (C) 2013-2015 Atmel Corporation. All rights reserved. - * - * \asf_license_start - * - * \page License - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. The name of Atmel may not be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 4. This software may only be redistributed and used in connection with an - * Atmel microcontroller product. - * - * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE - * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * \asf_license_stop - * - */ -/* - * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> - */ -#include "rtc_count_interrupt.h" - -extern struct rtc_module *_rtc_instance[RTC_INST_NUM]; - -/** - * \brief Registers callback for the specified callback type - * - * Associates the given callback function with the - * specified callback type. - * To enable the callback, the \ref rtc_count_enable_callback function - * must be used. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] callback Pointer to the function desired for the specified - * callback - * \param[in] callback_type Callback type to register - * - * \return Status of registering callback. - * \retval STATUS_OK Registering was done successfully - * \retval STATUS_ERR_INVALID_ARG If trying to register a callback not available - */ -enum status_code rtc_count_register_callback( - struct rtc_module *const module, - rtc_count_callback_t callback, - enum rtc_count_callback callback_type) -{ - - enum status_code status = STATUS_OK; - - /* Overflow callback */ - if (callback_type == RTC_COUNT_CALLBACK_OVERFLOW) { - status = STATUS_OK; - } else { - /* Make sure callback type can be registered */ - switch (module->mode) { - case RTC_COUNT_MODE_32BIT: - /* Check sanity for 32-bit mode. */ - if (callback_type > RTC_NUM_OF_COMP32) { - status = STATUS_ERR_INVALID_ARG; - } - - break; - case RTC_COUNT_MODE_16BIT: - /* Check sanity for 16-bit mode. */ - if (callback_type > RTC_NUM_OF_COMP16) { - status = STATUS_ERR_INVALID_ARG; - } - break; - default: - status = STATUS_ERR_INVALID_ARG; - } - } - - if (status == STATUS_OK) { - /* Register callback */ - module->callbacks[callback_type] = callback; - /* Set corresponding bit to set callback as registered */ - module->registered_callback |= (1 << callback_type); - } - - return status; -} - -/** - * \brief Unregisters callback for the specified callback type - * - * When called, the currently registered callback for the given callback type - * will be removed. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] callback_type Specifies the callback type to unregister - * - * \return Status of unregistering callback. - * \retval STATUS_OK Unregistering was done successfully - * \retval STATUS_ERR_INVALID_ARG If trying to unregister a callback not available - */ -enum status_code rtc_count_unregister_callback( - struct rtc_module *const module, - enum rtc_count_callback callback_type) -{ - enum status_code status = STATUS_OK; - - /* Overflow callback */ - if (callback_type == RTC_COUNT_CALLBACK_OVERFLOW) { - status = STATUS_OK; - } else { - /* Make sure callback type can be unregistered */ - switch (module->mode) { - case RTC_COUNT_MODE_32BIT: - /* Check sanity for 32-bit mode. */ - if (callback_type > RTC_NUM_OF_COMP32) { - status = STATUS_ERR_INVALID_ARG; - } - break; - case RTC_COUNT_MODE_16BIT: - /* Check sanity for 16-bit mode. */ - if (callback_type > RTC_NUM_OF_COMP16) { - status = STATUS_ERR_INVALID_ARG; - } - break; - default: - status = STATUS_ERR_INVALID_ARG; - } - } - if (status == STATUS_OK) { - /* Unregister callback */ - module->callbacks[callback_type] = NULL; - - /* Clear corresponding bit to set callback as unregistered */ - module->registered_callback &= ~(1 << callback_type); - } - return status; -} - -/** - * \brief Enables callback - * - * Enables the callback specified by the callback_type. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] callback_type Callback type to enable - */ -void rtc_count_enable_callback( - struct rtc_module *const module, - enum rtc_count_callback callback_type) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - if (callback_type == RTC_COUNT_CALLBACK_OVERFLOW) { - rtc_module->MODE0.INTENSET.reg = RTC_MODE0_INTFLAG_OVF; - } else { - rtc_module->MODE0.INTENSET.reg = RTC_MODE1_INTFLAG_CMP(1 << callback_type); - } - /* Mark callback as enabled. */ - module->enabled_callback |= (1 << callback_type); -} - -/** - * \brief Disables callback - * - * Disables the callback specified by the callback_type. - * - * \param[in,out] module Pointer to the software instance struct - * \param[in] callback_type Callback type to disable - */ -void rtc_count_disable_callback( - struct rtc_module *const module, - enum rtc_count_callback callback_type) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - Rtc *const rtc_module = module->hw; - - /* Disable interrupt */ - if (callback_type == RTC_COUNT_CALLBACK_OVERFLOW) { - rtc_module->MODE0.INTENCLR.reg = RTC_MODE0_INTFLAG_OVF; - } else { - rtc_module->MODE0.INTENCLR.reg = RTC_MODE1_INTFLAG_CMP(1 << callback_type); - } - - /* Mark callback as disabled. */ - module->enabled_callback &= ~(1 << callback_type); -} - -/** - * \internal Interrupt handler for RTC - * - * \param [in] instance_index Default value 0 - */ -static void _rtc_interrupt_handler(const uint32_t instance_index) -{ - struct rtc_module *module = _rtc_instance[instance_index]; - - Rtc *const rtc_module = module->hw; - - /* Combine callback registered and enabled masks */ - uint16_t callback_mask = module->enabled_callback; - callback_mask &= module->registered_callback; - - /* Read and mask interrupt flag register */ - uint16_t interrupt_status = rtc_module->MODE0.INTFLAG.reg; - interrupt_status &= rtc_module->MODE0.INTENSET.reg; - - if (interrupt_status & RTC_MODE0_INTFLAG_OVF) { - /* Overflow interrupt */ - if (callback_mask & (1 << RTC_COUNT_CALLBACK_OVERFLOW)) { - module->callbacks[RTC_COUNT_CALLBACK_OVERFLOW](); - } - - /* Clear interrupt flag */ - rtc_module->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_OVF; - - } else if (interrupt_status & RTC_MODE1_INTFLAG_CMP(1 << 0)) { - /* Compare 0 interrupt */ - if (callback_mask & (1 << RTC_COUNT_CALLBACK_COMPARE_0)) { - module->callbacks[RTC_COUNT_CALLBACK_COMPARE_0](); - } - /* Clear interrupt flag */ - rtc_module->MODE0.INTFLAG.reg = RTC_MODE1_INTFLAG_CMP(1 << 0); - - } else if (interrupt_status & RTC_MODE1_INTFLAG_CMP(1 << 1)) { - #if (RTC_NUM_OF_COMP16 > 1) || defined(__DOXYGEN__) - /* Compare 1 interrupt */ - if (callback_mask & (1 << RTC_COUNT_CALLBACK_COMPARE_1)) { - module->callbacks[RTC_COUNT_CALLBACK_COMPARE_1](); - } - /* Clear interrupt flag */ - rtc_module->MODE0.INTFLAG.reg = RTC_MODE1_INTFLAG_CMP(1 << 1); - #endif - - } else if (interrupt_status & RTC_MODE1_INTFLAG_CMP(1 << 2)) { - #if (RTC_NUM_OF_COMP16 > 2) || defined(__DOXYGEN__) - /* Compare 2 interrupt */ - if (callback_mask & (1 << RTC_COUNT_CALLBACK_COMPARE_2)) { - module->callbacks[RTC_COUNT_CALLBACK_COMPARE_2](); - } - /* Clear interrupt flag */ - rtc_module->MODE0.INTFLAG.reg = RTC_MODE1_INTFLAG_CMP(1 << 2); - #endif - - } else if (interrupt_status & RTC_MODE1_INTFLAG_CMP(1 << 3)) { - #if (RTC_NUM_OF_COMP16 > 3) || defined(__DOXYGEN__) - /* Compare 3 interrupt */ - if (callback_mask & (1 << RTC_COUNT_CALLBACK_COMPARE_3)) { - module->callbacks[RTC_COUNT_CALLBACK_COMPARE_3](); - } - /* Clear interrupt flag */ - rtc_module->MODE0.INTFLAG.reg = RTC_MODE1_INTFLAG_CMP(1 << 3); - #endif - - } else if (interrupt_status & RTC_MODE1_INTFLAG_CMP(1 << 4)) { - #if (RTC_NUM_OF_COMP16 > 4) || defined(__DOXYGEN__) - /* Compare 4 interrupt */ - if (callback_mask & (1 << RTC_COUNT_CALLBACK_COMPARE_4)) { - module->callbacks[RTC_COUNT_CALLBACK_COMPARE_4](); - } - /* Clear interrupt flag */ - rtc_module->MODE0.INTFLAG.reg = RTC_MODE1_INTFLAG_CMP(1 << 4); - #endif - - } else if (interrupt_status & RTC_MODE1_INTFLAG_CMP(1 << 5)) { - #if (RTC_NUM_OF_COMP16 > 5) || defined(__DOXYGEN__) - /* Compare 5 interrupt */ - if (callback_mask & (1 << RTC_COUNT_CALLBACK_COMPARE_5)) { - module->callbacks[RTC_COUNT_CALLBACK_COMPARE_5](); - } - /* Clear interrupt flag */ - rtc_module->MODE0.INTFLAG.reg = RTC_MODE1_INTFLAG_CMP(1 << 5); - #endif - } -} - -/** - * \internal ISR handler for RTC - */ -#if (RTC_INST_NUM == 1) -void RTC_Handler(void) -{ - _rtc_interrupt_handler(0); -} -#elif (RTC_INST_NUM > 1) -# define _RTC_INTERRUPT_HANDLER(n, unused) \ - void RTC##n##_Handler(void) \ - { \ - _rtc_interrupt_handler(n); \ - } - -MREPEAT(RTC_INST_NUM, _RTC_INTERRUPT_HANDLER, ~) -#endif /* (RTC_INST_NUM > 1) */ \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a280628a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_tamper.h ---------------------------------------------------------------------- diff --git a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_tamper.h b/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_tamper.h deleted file mode 100755 index d386730..0000000 --- a/hw/mcu/atmel/samd21xx/src/sam0/drivers/rtc/rtc_tamper.h +++ /dev/null @@ -1,378 +0,0 @@ -/** - * \file - * - * \brief SAM RTC Driver (Tamper) - * - * Copyright (C) 2015 Atmel Corporation. All rights reserved. - * - * \asf_license_start - * - * \page License - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. The name of Atmel may not be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 4. This software may only be redistributed and used in connection with an - * Atmel microcontroller product. - * - * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE - * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * \asf_license_stop - * - */ -/* - * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> - */ - -#ifndef RTC_TAMPER_H_INCLUDED -#define RTC_TAMPER_H_INCLUDED - -/** - * - * \section asfdoc_sam0_rtc_tamper_detect RTC Tamper Detect - * The RTC provides several selectable polarity external inputs (INn) that can be - * used for tamper detection. When detect, tamper inputs support the four actions: - * - Off - * - Wake - * - Capture - * - Active layer protection - * - * \note The Active Layer Protection is a means of detecting broken traces on the - * PCB provided by RTC. In this mode an RTC output signal is routed over critical - * components onthe board and fed back to one of the RTC inputs. The input and - * output signals are compared and a tamper condition is detected when they do not match. - * - * - * Separate debouncers are embedded for each external input. The detection time - * depends on whether the debouncer operates synchronously or asynchronously, - * and whether majority detection is enabled or not. Details refer to the section - * "Tamper Detection" of datasheet. - * \if RTC_COUNT_CALLBACK_MODE - * \addtogroup asfdoc_sam0_rtc_count_group - * \else - * \if RTC_CALENDAR_CALLBACK_MODE - * \addtogroup asfdoc_sam0_rtc_calendar_group - * \endif - * \endif - * @{ - */ - -#if defined(FEATURE_RTC_TAMPER_DETECTION) || defined(__DOXYGEN__) - -/** RTC tamper ID0 detection bitmask. */ -#define RTC_TAMPER_DETECT_ID0 (1UL << 0) -/** RTC tamper ID1 detection bitmask. */ -#define RTC_TAMPER_DETECT_ID1 (1UL << 1) -/** RTC tamper ID2 detection bitmask. */ -#define RTC_TAMPER_DETECT_ID2 (1UL << 2) -/** RTC tamper ID3 detection bitmask. */ -#define RTC_TAMPER_DETECT_ID3 (1UL << 3) -/** RTC tamper ID4 detection bitmask. */ -#define RTC_TAMPER_DETECT_ID4 (1UL << 4) -/** RTC tamper input event detection bitmask. */ -#define RTC_TAMPER_DETECT_EVT (1UL << 5) - - - -/** - * \brief RTC tamper active layer frequency divider. - * - * The available prescaler factor for the RTC clock output used during active - * layer protection. - */ -enum rtc_tamper_active_layer_freq_divider { - /** RTC active layer frequency is prescaled by a factor of 2 */ - RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_2 = RTC_MODE0_CTRLB_ACTF_DIV2, - /** RTC active layer frequency is prescaled by a factor of 4 */ - RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_4 = RTC_MODE0_CTRLB_ACTF_DIV4, - /** RTC active layer frequency is prescaled by a factor of 8 */ - RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_8 = RTC_MODE0_CTRLB_ACTF_DIV8, - /** RTC active layer frequency is prescaled by a factor of 16 */ - RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_16 = RTC_MODE0_CTRLB_ACTF_DIV16, - /** RTC active layer frequency is prescaled by a factor of 32 */ - RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_32 = RTC_MODE0_CTRLB_ACTF_DIV32, - /** RTC active layer frequency is prescaled by a factor of 64 */ - RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_64 = RTC_MODE0_CTRLB_ACTF_DIV64, - /** RTC active layer frequency is prescaled by a factor of 128 */ - RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_128 = RTC_MODE0_CTRLB_ACTF_DIV128, - /** RTC active layer frequency is prescaled by a factor of 256 */ - RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_256 = RTC_MODE0_CTRLB_ACTF_DIV256, -}; - -/** - * \brief RTC tamper debounce frequency divider. - * - * The available prescaler factor for the input debouncers. - */ -enum rtc_tamper_debounce_freq_divider { - /** RTC debounce frequency is prescaled by a factor of 2 */ - RTC_TAMPER_DEBOUNCE_FREQ_DIV_2 = RTC_MODE0_CTRLB_DEBF_DIV2, - /** RTC debounce frequency is prescaled by a factor of 4 */ - RTC_TAMPER_DEBOUNCE_FREQ_DIV_4 = RTC_MODE0_CTRLB_DEBF_DIV4, - /** RTC debounce frequency is prescaled by a factor of 8 */ - RTC_TAMPER_DEBOUNCE_FREQ_DIV_8 = RTC_MODE0_CTRLB_DEBF_DIV8, - /** RTC debounce frequency is prescaled by a factor of 16 */ - RTC_TAMPER_DEBOUNCE_FREQ_DIV_16 = RTC_MODE0_CTRLB_DEBF_DIV16, - /** RTC debounce frequency is prescaled by a factor of 32 */ - RTC_TAMPER_DEBOUNCE_FREQ_DIV_32 = RTC_MODE0_CTRLB_DEBF_DIV32, - /** RTC debounce frequency is prescaled by a factor of 64 */ - RTC_TAMPER_DEBOUNCE_FREQ_DIV_64 = RTC_MODE0_CTRLB_DEBF_DIV64, - /** RTC debounce frequency is prescaled by a factor of 128 */ - RTC_TAMPER_DEBOUNCE_FREQ_DIV_128 = RTC_MODE0_CTRLB_DEBF_DIV128, - /** RTC debounce frequency is prescaled by a factor of 256 */ - RTC_TAMPER_DEBOUNCE_FREQ_DIV_256 = RTC_MODE0_CTRLB_DEBF_DIV256, -}; - -/** - * \brief RTC tamper input action. - * - * The available action taken by the tamper input. - */ -enum rtc_tamper_input_action { - /** RTC tamper input action is disabled */ - RTC_TAMPER_INPUT_ACTION_OFF = RTC_TAMPCTRL_IN0ACT_OFF, - /** RTC tamper input action is wake and set tamper flag */ - RTC_TAMPER_INPUT_ACTION_WAKE = RTC_TAMPCTRL_IN0ACT_WAKE, - /** RTC tamper input action is capture timestamp and set tamper flag */ - RTC_TAMPER_INPUT_ACTION_CAPTURE = RTC_TAMPCTRL_IN0ACT_CAPTURE, - /** RTC tamper input action is compare IN to OUT, when a mismatch occurs, - * capture timestamp and set tamper flag */ - RTC_TAMPER_INPUT_ACTION_ACTL = RTC_TAMPCTRL_IN0ACT_ACTL, -}; - -/** - * \brief RTC tamper input level select. - * - * The available edge condition for tamper INn level select. - */ -enum rtc_tamper_level_sel { - /** A falling edge condition will be detected on Tamper input */ - RTC_TAMPER_LEVEL_FALLING = (0), - /** A rising edge condition will be detected on Tamper input */ - RTC_TAMPER_LEVEL_RISING = (1), -}; - -/** - * \brief RTC tamper debounce sequential. - * - * The available sequential for tamper debounce. - */ -enum rtc_tamper_debounce_seq { - /** Tamper input detect edge with synchronous stability debounce */ - RTC_TAMPER_DEBOUNCE_SYNC, - /** Tamper input detect edge with asynchronous stability debounce */ - RTC_TAMPER_DEBOUNCE_ASYNC, - /** Tamper input detect edge with majority debounce */ - RTC_TAMPER_DEBOUNCE_MAJORITY, -}; - -/** - * \brief RTC tamper input configuration structure. - * - * The configuration structure for tamper INn. - */ -struct rtc_tamper_input_config { - /** Debounce enable */ - bool debounce_enable; - /** Tamper level select */ - enum rtc_tamper_level_sel level; - /** Tamper input action */ - enum rtc_tamper_input_action action; -}; - -/** - * \brief RTC Tamper configuration structure. - * - * The configuration structure for the RTC tamper. This structure should - * be initialized using the \ref rtc_tamper_get_config_defaults() before any - * user configurations are set. - */ -struct rtc_tamper_config { - /** Backup register reset on tamper enable */ - bool bkup_reset_on_tamper; - /** GP register reset on tamper enable */ - bool gp_reset_on_tamper; - /** Active layer frequency */ - enum rtc_tamper_active_layer_freq_divider actl_freq_div; - /** Debounce frequency */ - enum rtc_tamper_debounce_freq_divider deb_freq_div; - /** Debounce sequential */ - enum rtc_tamper_debounce_seq deb_seq; - /** DMA on tamper enable */ - bool dma_tamper_enable; - /** General Purpose 0/1 Enable */ - bool gp0_enable; - /** Tamper IN configuration */ - struct rtc_tamper_input_config in_cfg[RTC_TAMPER_NUM]; -}; - -/** - * \name RTC Tamper Detection - * @{ - */ - -/** - * \brief Gets the RTC tamper default configurations. - * - * Initializes the configuration structure to default values. - * - * The default configuration is as follows: - * - Disable backup register reset on tamper - * - Disable GP register reset on tamper - * - Active layer clock divided by a factor of 8 - * - Debounce clock divided by a factor of 8 - * - Detect edge on INn with synchronous stability debouncing - * - Disable DMA on tamper - * - Enable GP register - * - Disable debouce, detect on falling edge and no action on INn - * - * \param[out] config Configuration structure to be initialized to default values. - */ -static inline void rtc_tamper_get_config_defaults( - struct rtc_tamper_config *const config) -{ - /* Sanity check argument */ - Assert(config); - - config->bkup_reset_on_tamper= false; - config->gp_reset_on_tamper = false; - config->actl_freq_div = RTC_TAMPER_ACTIVE_LAYER_FREQ_DIV_8; - config->deb_freq_div = RTC_TAMPER_DEBOUNCE_FREQ_DIV_8; - config->deb_seq = RTC_TAMPER_DEBOUNCE_SYNC; - config->dma_tamper_enable = false; - config->gp0_enable = true; - - for (uint8_t id = 0; id < RTC_TAMPER_NUM; id++) { - config->in_cfg[id].debounce_enable = false; - config->in_cfg[id].level = RTC_TAMPER_LEVEL_FALLING; - config->in_cfg[id].action = RTC_TAMPER_INPUT_ACTION_OFF; - } -} -enum status_code rtc_tamper_set_config (struct rtc_module *const module, - struct rtc_tamper_config *const tamper_cfg); - -/** - * \brief Retrieves the RTC tamper detection status. - * - * Retrieves the detection status of each input pin and the input event. - * - * \param[in] module Pointer to the RTC software instance struct - * - * \return Bitmask of detection flags. - * - * \retval RTC_TAMPER_DETECT_ID0 Tamper condition on IN0 has been detected - * \retval RTC_TAMPER_DETECT_ID1 Tamper condition on IN1 has been detected - * \retval RTC_TAMPER_DETECT_ID2 Tamper condition on IN2 has been detected - * \retval RTC_TAMPER_DETECT_ID3 Tamper condition on IN3 has been detected - * \retval RTC_TAMPER_DETECT_ID4 Tamper condition on IN4 has been detected - * \retval RTC_TAMPER_DETECT_EVT Tamper input event has been detected - */ -static inline uint32_t rtc_tamper_get_detect_flag (struct rtc_module *const module) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - uint32_t tamper_id = module->hw->MODE0.TAMPID.reg; - uint32_t detect_flags = 0; - - if (tamper_id & RTC_TAMPID_TAMPID0) { - detect_flags |= RTC_TAMPER_DETECT_ID0; - } - - if (tamper_id & RTC_TAMPID_TAMPID1) { - detect_flags |= RTC_TAMPER_DETECT_ID1; - } - - if (tamper_id & RTC_TAMPID_TAMPID2) { - detect_flags |= RTC_TAMPER_DETECT_ID2; - } - - if (tamper_id & RTC_TAMPID_TAMPID3) { - detect_flags |= RTC_TAMPER_DETECT_ID3; - } - - if (tamper_id & RTC_TAMPID_TAMPID4) { - detect_flags |= RTC_TAMPER_DETECT_ID4; - } - - if (tamper_id & RTC_TAMPID_TAMPEVT) { - detect_flags |= RTC_TAMPER_DETECT_EVT; - } - - return detect_flags; -} - -/** - * \brief Clears RTC tamper detection flag. - * - * Clears the given detection flag of the module. - * - * \param[in] module Pointer to the TC software instance struct - * \param[in] detect_flags Bitmask of detection flags - */ -static inline void rtc_tamper_clear_detect_flag( - struct rtc_module *const module, - const uint32_t detect_flags) -{ - /* Sanity check arguments */ - Assert(module); - Assert(module->hw); - - uint32_t tamper_id = 0; - - if (detect_flags & RTC_TAMPER_DETECT_ID0) { - tamper_id |= RTC_TAMPID_TAMPID0; - } - - if (detect_flags & RTC_TAMPER_DETECT_ID1) { - tamper_id |= RTC_TAMPID_TAMPID1; - } - - if (detect_flags & RTC_TAMPER_DETECT_ID2) { - tamper_id |= RTC_TAMPID_TAMPID2; - } - - if (detect_flags & RTC_TAMPER_DETECT_ID3) { - tamper_id |= RTC_TAMPID_TAMPID3; - } - - if (detect_flags & RTC_TAMPER_DETECT_ID4) { - tamper_id |= RTC_TAMPID_TAMPID4; - } - - if (detect_flags & RTC_TAMPER_DETECT_EVT) { - tamper_id |= RTC_TAMPID_TAMPEVT; - } - - module->hw->MODE0.TAMPID.reg = tamper_id; - -} - -/** @} */ - -#endif -/** @} */ - - -#endif /* RTC_TAMPER_H_INCLUDED */ \ No newline at end of file