diff options
author | Cristian Maglie <c.maglie@bug.st> | 2012-10-18 15:50:09 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2012-10-18 15:50:09 +0200 |
commit | 6a45ba48ab1f2d0a168373a02ba7fded40a3470e (patch) | |
tree | 163448869b4dfcbce95dc877ffff61a709fba6b6 /firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC | |
parent | c313b54c00635f1be14a1b09617dc9b8b562e589 (diff) | |
parent | 6d296e0faba5b9910084c307a3f93cb2653bf7f8 (diff) |
Merged upstream arduino branch
Diffstat (limited to 'firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC')
-rw-r--r-- | firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.c | 213 | ||||
-rw-r--r-- | firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.h | 191 |
2 files changed, 404 insertions, 0 deletions
diff --git a/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.c b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.c new file mode 100644 index 0000000..4cbae0f --- /dev/null +++ b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.c @@ -0,0 +1,213 @@ +/* This source file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */ + +/*This file is prepared for Doxygen automatic documentation generation.*/ +/*! \file ********************************************************************* + * + * \brief RTC driver for AVR32 UC3. + * + * AVR32 Real Time Counter driver module. + * + * - Compiler: GNU GCC for AVR32 + * - Supported devices: All AVR32 devices with an RTC and a PM module can be used. + * - AppNote: + * + * \author Atmel Corporation: http://www.atmel.com \n + * Support and FAQ: http://support.atmel.no/ + * + ******************************************************************************/ + +/* Copyright (c) 2009 Atmel Corporation. All rights reserved. + * + * 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 + * AVR 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 + * + */ + +#include <avr32/io.h> +#include "compiler.h" +#include "pm.h" +#include "rtc.h" + + +int rtc_is_busy(volatile avr32_rtc_t *rtc) +{ + return (rtc->ctrl & AVR32_RTC_CTRL_BUSY_MASK) != 0; +} + + +int rtc_init(volatile avr32_rtc_t *rtc, unsigned char osc_type, unsigned char psel) +{ + // If exit, it means that the configuration has not been set correctly + if (osc_type > (1 << AVR32_RTC_CTRL_CLK32_SIZE) - 1 || + psel > (1 << AVR32_RTC_CTRL_PSEL_SIZE) - 1) + return 0; + + // If we use the 32-kHz oscillator, we have to enable it first + if (osc_type == RTC_OSC_32KHZ) + { + // Select the 32-kHz oscillator crystal + pm_enable_osc32_crystal(&AVR32_PM); + // Enable the 32-kHz clock + pm_enable_clk32_no_wait(&AVR32_PM, AVR32_PM_OSCCTRL32_STARTUP_0_RCOSC); + } + + // Wait until the rtc CTRL register is up-to-date + while (rtc_is_busy(rtc)); + + // Set the new RTC configuration + rtc->ctrl = osc_type << AVR32_RTC_CTRL_CLK32_OFFSET | + psel << AVR32_RTC_CTRL_PSEL_OFFSET | + AVR32_RTC_CTRL_CLKEN_MASK; + + // Wait until write is done + while (rtc_is_busy(rtc)); + + // Set the counter value to 0 + rtc_set_value(rtc, 0x00000000); + // Set the top value to 0xFFFFFFFF + rtc_set_top_value(rtc, 0xFFFFFFFF); + + return 1; +} + + +void rtc_set_value(volatile avr32_rtc_t *rtc, unsigned long val) +{ + // Wait until we can write into the VAL register + while (rtc_is_busy(rtc)); + // Set the new val value + rtc->val = val; + // Wait until write is done + while (rtc_is_busy(rtc)); +} + + +unsigned long rtc_get_value(volatile avr32_rtc_t *rtc) +{ + return rtc->val; +} + + +void rtc_enable_wake_up(volatile avr32_rtc_t *rtc) +{ + // Wait until the rtc CTRL register is up-to-date + while (rtc_is_busy(rtc)); + // Enable the wake up of the RTC + rtc->ctrl |= AVR32_RTC_CTRL_WAKE_EN_MASK; + // Wait until write is done + while (rtc_is_busy(rtc)); +} + + +void rtc_disable_wake_up(volatile avr32_rtc_t *rtc) +{ + // Wait until the rtc CTRL register is up-to-date + while (rtc_is_busy(rtc)); + // Disable the wake up of the RTC + rtc->ctrl &= ~AVR32_RTC_CTRL_WAKE_EN_MASK; + // Wait until write is done + while (rtc_is_busy(rtc)); +} + + +void rtc_enable(volatile avr32_rtc_t *rtc) +{ + // Wait until the rtc CTRL register is up-to-date + while (rtc_is_busy(rtc)); + // Enable the RTC + rtc->ctrl |= AVR32_RTC_CTRL_EN_MASK; + // Wait until write is done + while (rtc_is_busy(rtc)); +} + + +void rtc_disable(volatile avr32_rtc_t *rtc) +{ + // Wait until the rtc CTRL register is up-to-date + while (rtc_is_busy(rtc)); + // Disable the RTC + rtc->ctrl &= ~AVR32_RTC_CTRL_EN_MASK; + // Wait until write is done + while (rtc_is_busy(rtc)); +} + + +void rtc_enable_interrupt(volatile avr32_rtc_t *rtc) +{ + rtc->ier = AVR32_RTC_IER_TOPI_MASK; +} + + +void rtc_disable_interrupt(volatile avr32_rtc_t *rtc) +{ + Bool global_interrupt_enabled = Is_global_interrupt_enabled(); + + if (global_interrupt_enabled) Disable_global_interrupt(); + rtc->idr = AVR32_RTC_IDR_TOPI_MASK; + rtc->imr; + if (global_interrupt_enabled) Enable_global_interrupt(); +} + + +void rtc_clear_interrupt(volatile avr32_rtc_t *rtc) +{ + Bool global_interrupt_enabled = Is_global_interrupt_enabled(); + + if (global_interrupt_enabled) Disable_global_interrupt(); + rtc->icr = AVR32_RTC_ICR_TOPI_MASK; + rtc->isr; + if (global_interrupt_enabled) Enable_global_interrupt(); +} + + +void rtc_set_top_value(volatile avr32_rtc_t *rtc, unsigned long top) +{ + // Wait until we can write into the VAL register + while (rtc_is_busy(rtc)); + // Set the new val value + rtc->top = top; + // Wait until write is done + while (rtc_is_busy(rtc)); +} + + +unsigned long rtc_get_top_value(volatile avr32_rtc_t *rtc) +{ + return rtc->top; +} + + +int rtc_interrupt_enabled(volatile avr32_rtc_t *rtc) +{ + return (rtc->imr & AVR32_RTC_IMR_TOPI_MASK) != 0; +} + + +int rtc_is_interrupt(volatile avr32_rtc_t *rtc) +{ + return (rtc->isr & AVR32_RTC_ISR_TOPI_MASK) != 0; +} diff --git a/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.h b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.h new file mode 100644 index 0000000..5702c29 --- /dev/null +++ b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.h @@ -0,0 +1,191 @@ +/* This header file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */ + +/*This file is prepared for Doxygen automatic documentation generation.*/ +/*! \file ********************************************************************* + * + * \brief RTC driver for AVR32 UC3. + * + * AVR32 Real Time Counter driver module. + * + * - Compiler: GNU GCC for AVR32 + * - Supported devices: All AVR32 devices with an RTC and a PM module can be used. + * - AppNote: + * + * \author Atmel Corporation: http://www.atmel.com \n + * Support and FAQ: http://support.atmel.no/ + * + ******************************************************************************/ + +/* Copyright (c) 2009 Atmel Corporation. All rights reserved. + * + * 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 + * AVR 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 + * + */ + +#ifndef _RTC_H_ +#define _RTC_H_ + +#include "compiler.h" +#include <avr32/io.h> + + +/*! \name Oscillator Types + */ +//! @{ +#define RTC_OSC_32KHZ 1 +#define RTC_OSC_RC 0 +//! @} + +/*! \name Predefined PSEL Values + */ +//! @{ + +//! The PSEL value to set the RTC source clock (after the prescaler) to 1 Hz, +//! when using an external 32-kHz crystal. +#define RTC_PSEL_32KHZ_1HZ 14 + +//! The PSEL value to set the RTC source clock (after the prescaler) to 1.76 Hz, +//! when using the internal RC oscillator (~ 115 kHz). +#define RTC_PSEL_RC_1_76HZ 15 + +//! @} + + +/*! + * \brief This function will initialise the RTC module. + * If you use the 32 KHz oscillator, it will enable this module. + * This function also set the top value of the RTC to 0xFFFFFFFF + * and the value to 0. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + * \param osc_type The oscillator you want to use. If you need a better + * accuracy, use the 32 KHz oscillator (i.e. RTC_OSC_32KHZ). + * \param psel The preselector value for the corresponding oscillator (4-bits). + * To obtain this value, you can use this formula: + * psel = log(Fosc/Frtc)/log(2)-1, where Fosc is the frequency of the + * oscillator you are using (32 KHz or 115 KHz) and Frtc the frequency + * desired. + * \return 1 if the initialisation succeds otherwize it will return 0. + */ +extern int rtc_init(volatile avr32_rtc_t *rtc, unsigned char osc_type, unsigned char psel); + +/*! + * \brief Enable the RTC. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + */ +extern void rtc_enable(volatile avr32_rtc_t *rtc); + +/*! + * \brief Disable the RTC. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + */ +extern void rtc_disable(volatile avr32_rtc_t *rtc); + +/*! + * \brief Enable the wake up feature of the RTC. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + */ +extern void rtc_enable_wake_up(volatile avr32_rtc_t *rtc); + +/*! + * \brief Disable the wake up feature of the RTC. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + */ +extern void rtc_disable_wake_up(volatile avr32_rtc_t *rtc); + +/*! + * \brief Enable the interrupt feature of the RTC. + * An interrupt is raised when the value of the RTC + * is equal to its top value. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + */ +extern void rtc_enable_interrupt(volatile avr32_rtc_t *rtc); + +/*! + * \brief Disable the interrupt feature of the RTC. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + */ +extern void rtc_disable_interrupt(volatile avr32_rtc_t *rtc); + +/*! + * \brief Clear the interrupt flag. + * Call this function once you handled the interrupt. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + */ +extern void rtc_clear_interrupt(volatile avr32_rtc_t *rtc); + +/*! + * \brief Get the status of interrupts. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + * \return 1 if the interrupts are enabled otherwize it returns 0. + */ +extern int rtc_interrupt_enabled(volatile avr32_rtc_t *rtc); + +/*! + * \brief Check if an interrupt is raised. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + * \return 1 if an interrupt is currently raised otherwize it returns 0. + */ +extern int rtc_is_interrupt(volatile avr32_rtc_t *rtc); + +/*! + * \brief This function sets the RTC current top value. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + * \param top The top value you want to store. + */ +extern void rtc_set_top_value(volatile avr32_rtc_t *rtc, unsigned long top); + +/*! + * \brief This function returns the RTC current top value. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + * \return The RTC current top value. + */ +extern unsigned long rtc_get_top_value(volatile avr32_rtc_t *rtc); + +/*! + * \brief This function checks if the RTC is busy or not. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + * \return 1 if the RTC is busy otherwize it will return 0. + */ +extern int rtc_is_busy(volatile avr32_rtc_t *rtc); + +/*! + * \brief This function sets the RTC current value. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + * \param val The value you want to store. + */ +extern void rtc_set_value(volatile avr32_rtc_t *rtc, unsigned long val); + +/*! + * \brief This function returns the RTC current value. + * \param rtc Base address of the RTC (i.e. &AVR32_RTC). + * \return The RTC current value. + */ +extern unsigned long rtc_get_value(volatile avr32_rtc_t *rtc); + + +#endif // _RTC_H_ |