SMART ARM-based Microcontrollers AT07942: SAM4L Asynchronous Timer (AST) Driver APPLICATION NOTE Introduction ® ® This driver for Atmel | SMART ARM -based microcontrollers provides an interface for the configuration and management of the device's Asynchronous Timer functionality. The Asynchronous Timer can generate periodic interrupts and peripheral events from output from the prescaler, as well as alarm interrupts and peripheral events, which can trigger at any counter value. Devices from the following series can use this module: • Atmel | SMART SAM4L The outline of this documentation is as follows: • Prerequisites • Module Overview • Special Considerations • Extra Information • Examples • API Overview Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 Table of Contents Introduction......................................................................................................................1 1. Software License....................................................................................................... 5 2. Prerequisites..............................................................................................................6 3. Module Overview....................................................................................................... 7 3.1. 3.2. 3.3. Alarm Interrupt..............................................................................................................................7 Periodic Events.............................................................................................................................7 Digital Tuner................................................................................................................................. 8 4. Special Considerations.............................................................................................. 9 4.1. 4.2. Crystal Selection...........................................................................................................................9 Year Limit......................................................................................................................................9 5. Extra Information..................................................................................................... 10 6. Examples................................................................................................................. 11 7. API Overview........................................................................................................... 12 7.1. 7.2. 7.3. 7.4. Variable and Type Definitions..................................................................................................... 12 7.1.1. Type ast_callback_t..................................................................................................... 12 7.1.2. Type ast_event_source_t.............................................................................................12 7.1.3. Type ast_interrupt_source_t........................................................................................ 12 7.1.4. Type ast_mode_t......................................................................................................... 12 7.1.5. Type ast_oscillator_type_t........................................................................................... 12 7.1.6. Type ast_wakeup_source_t......................................................................................... 12 Structure Definitions................................................................................................................... 13 7.2.1. Struct ast_calendar......................................................................................................13 7.2.2. Union ast_calendar.__unnamed__.............................................................................. 13 7.2.3. Struct ast_calv............................................................................................................. 13 7.2.4. Struct ast_config.......................................................................................................... 13 Macro Definitions........................................................................................................................14 7.3.1. Predefined PSEL Values..............................................................................................14 7.3.2. Macro AST_POLL_TIMEOUT..................................................................................... 14 Function Definitions....................................................................................................................14 7.4.1. 7.4.2. 7.4.3. 7.4.4. 7.4.5. 7.4.6. 7.4.7. 7.4.8. 7.4.9. 7.4.10. Function ast_clear_interrupt_flag().............................................................................. 14 Function ast_clear_prescalar().................................................................................... 14 Function ast_configure_digital_tuner()........................................................................ 14 Function ast_disable()................................................................................................. 15 Function ast_disable_digital_tuner()............................................................................15 Function ast_disable_event().......................................................................................15 Function ast_disable_interrupt().................................................................................. 16 Function ast_disable_wakeup()................................................................................... 16 Function ast_enable().................................................................................................. 16 Function ast_enable_counter_clear_on_alarm()......................................................... 16 Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 2 7.4.11. 7.5. Function ast_enable_event()....................................................................................... 17 7.4.12. Function ast_enable_interrupt()...................................................................................17 7.4.13. Function ast_enable_wakeup()....................................................................................17 7.4.14. Function ast_init_digital_tuner()...................................................................................18 7.4.15. Function ast_is_busy().................................................................................................18 7.4.16. Function ast_is_clkbusy()............................................................................................ 18 7.4.17. Function ast_is_enabled()........................................................................................... 19 7.4.18. Function ast_read_calendar_value()........................................................................... 19 7.4.19. Function ast_read_counter_value()............................................................................. 20 7.4.20. Function ast_read_interrupt_mask()............................................................................20 7.4.21. Function ast_read_status().......................................................................................... 20 7.4.22. Function ast_set_callback()......................................................................................... 20 7.4.23. Function ast_set_config()............................................................................................ 21 7.4.24. Function ast_start()......................................................................................................21 7.4.25. Function ast_stop()...................................................................................................... 22 7.4.26. Function ast_write_alarm0_value()..............................................................................22 7.4.27. Function ast_write_calendar_value()...........................................................................22 7.4.28. Function ast_write_counter_value().............................................................................22 7.4.29. Function ast_write_periodic0_value().......................................................................... 23 Enumeration Definitions............................................................................................................. 23 7.5.1. Enum ast_event_source.............................................................................................. 23 7.5.2. Enum ast_interrupt_source..........................................................................................23 7.5.3. Enum ast_mode...........................................................................................................24 7.5.4. Enum ast_oscillator_type.............................................................................................24 7.5.5. Enum ast_wakeup_source.......................................................................................... 24 8. Extra Information for Asynchronous Timer Driver....................................................25 8.1. 8.2. 8.3. 8.4. Acronyms....................................................................................................................................25 Dependencies.............................................................................................................................25 Errata..........................................................................................................................................25 Module History............................................................................................................................25 9. Examples for Asynchronous Timer Driver............................................................... 26 9.1. 9.2. 9.3. Quick Start Guide for the AST driver..........................................................................................26 9.1.1. Use Cases................................................................................................................... 26 9.1.2. AST Basic Usage.........................................................................................................26 9.1.3. Setup Steps................................................................................................................. 26 9.1.4. Usage Steps................................................................................................................ 27 Asynchronous Timer (AST) - Example 1 Calendar Mode.......................................................... 27 9.2.1. Purpose....................................................................................................................... 27 9.2.2. Requirements.............................................................................................................. 27 9.2.3. Description...................................................................................................................27 9.2.4. Main Files.................................................................................................................... 27 9.2.5. Compilation Information...............................................................................................28 9.2.6. Usage.......................................................................................................................... 28 Asynchronous Timer (AST) - Example 2 Alarm Wakeup............................................................28 9.3.1. Purpose....................................................................................................................... 28 9.3.2. Requirements.............................................................................................................. 28 9.3.3. Description...................................................................................................................28 Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 3 9.3.4. Main Files.................................................................................................................... 28 9.3.5. 9.3.6. Compilation Information...............................................................................................29 Usage.......................................................................................................................... 29 10. Document Revision History..................................................................................... 30 Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 4 1. Software 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. Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 5 2. Prerequisites There are no prerequisites for this module. Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 6 3. Module Overview The AST module in the SAM4L devices is a 32-bit counter, with a 32-bit programmable prescaler. Typically, the AST clock is run continuously, including in the device's low-power sleep modes, to track the current time and date information. The AST can also wake-up the system from backup mode using either the alarm wakeup, periodic wakeup, or overflow wake-up mechanisms. The AST has been designed to meet the system tick and Real-Time Clock requirements of most embedded operating systems. In this driver, the AST is operated in Calendar Mode. This allows for an easy integration of a Real-Time Clock and calendar into a user application to track the passing of time and/or perform scheduled tasks. Whilst operating in Calendar Mode, the AST features: • Time tracking in seconds, minutes, and hours in 24 hour format • Date tracking in day, month, and year • Automatic leap year correction 3.1. Alarm Interrupt The AST has a device dependent number of independent hardware alarms that can be configured by the user application. These alarms will be triggered on a match with the current clock value, and can be set up to trigger an interrupt, event, or both. The AST can also be configured to clear the clock value on alarm match which will generate an overflow interrupt. Note: Refer to module configuration at the end of the AST section of the device datasheet for the number of alarms supported. Whilst in Calendar Mode and using a nominal 1Hz input clock frequency, a register overflow will occur after 64 years. 3.2. Periodic Events The AST can generate events at periodic intervals, allowing for direct peripheral actions without CPU intervention. The periodic events can be generated on an AST prescaler match, and will be generated on the rising edge transition of the specified bit. The resulting periodic frequency can be calculated by the following formula: Where ���� ��������� = ���� 2� + 1 refers to the asynchronous clock set up in the AST module configuration. Note: The connection of events between modules requires the use of the SAM4L Peripheral Event Controller (PEVC) to route an output event of one module to the the input event of another. For more information on event routing, refer to the event driver documentation. Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 7 3.3. Digital Tuner The AST module contains Digital Tuner logic to compensate for inaccurate source clock frequencies, which would otherwise result in skewed time measurements. Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 8 4. Special Considerations 4.1. Crystal Selection The external crystal selection used by the AST module in the final system design must take into account: • Current consumption to achieve the best power savings in low power operating modes • Frequency drift (due to temperature effects on the circuit) for the best time accuracy 4.2. Year Limit The AST module has a year range of 63 years from the starting year configured when the module is initialized. Dates outside the start to end year range described below will need software adjustment: ���������, ��������� + 64 Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 9 5. Extra Information For extra information, see Extra Information for Asynchronous Timer Driver. This includes: • Acronyms • Dependencies • Errata • Module History Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 10 6. Examples For a list of examples related to this driver, see Examples for Asynchronous Timer Driver. Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 11 7. API Overview 7.1. Variable and Type Definitions 7.1.1. Type ast_callback_t typedef void(* ast_callback_t )(void) AST interrupt callback. 7.1.2. Type ast_event_source_t typedef enum ast_event_source ast_event_source_t AST event source. 7.1.3. Type ast_interrupt_source_t typedef enum ast_interrupt_source ast_interrupt_source_t AST interrupt source. 7.1.4. Type ast_mode_t typedef enum ast_mode ast_mode_t AST Calendar Mode. 7.1.5. Type ast_oscillator_type_t typedef enum ast_oscillator_type ast_oscillator_type_t AST Oscillator type. 7.1.6. Type ast_wakeup_source_t typedef enum ast_wakeup_source ast_wakeup_source_t AST wake-up source. Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 12 7.2. Structure Definitions 7.2.1. Struct ast_calendar Table 7-1 Members 7.2.2. Type Name union ast_calendar.@1 @1 Description Union ast_calendar.__unnamed__ Table 7-2 Members 7.2.3. Type Name uint32_t field struct ast_calv FIELD Description Calendar Struct ast_calv Description for Calendar Field. Table 7-3 Members 7.2.4. Type Name Description uint32_t day Day in the range 1 to 31 uint32_t hour Hours in the range 0 to 23 uint32_t min Minutes in the range range 0 to 59 uint32_t month Month in the range 1 to 12 uint32_t sec Seconds in the range 0 to 59 uint32_t year Year in the range 0 to 63 Struct ast_config AST configuration. Table 7-4 Members Type Name Description struct ast_calendar calendar Initial calendar Value uint32_t counter Initial counter Value ast_mode_t mode Mode: Calendar Mode: AST_CALENDAR_MODE or Counter Mode: AST_COUNTER_MODE ast_oscillator_type_t osc_type Oscillator type uint8_t psel Prescaler Value Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 13 7.3. Macro Definitions 7.3.1. Predefined PSEL Values 7.3.1.1. Macro AST_PSEL_32KHZ_1HZ #define AST_PSEL_32KHZ_1HZ 7.3.1.2. Macro AST_PSEL_RC_1_76HZ #define AST_PSEL_RC_1_76HZ 7.3.2. Macro AST_POLL_TIMEOUT #define AST_POLL_TIMEOUT Timeout to prevent code hang in clock initialization. 7.4. Function Definitions 7.4.1. Function ast_clear_interrupt_flag() Clear an AST interrupt status flag. void ast_clear_interrupt_flag( Ast * ast, ast_interrupt_source_t source) Table 7-5 Parameters 7.4.2. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] source AST interrupt source for which the status is to be cleared Function ast_clear_prescalar() Clear the AST periodic prescaler counter to zero. void ast_clear_prescalar( Ast * ast) Table 7-6 Parameters 7.4.3. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer Function ast_configure_digital_tuner() Tune the AST prescaler frequency to the desired frequency. uint32_t ast_configure_digital_tuner( Ast * ast, Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 14 uint32_t input_freq, uint32_t tuned_freq) Note: Refer to the section entitled "digital tuner" in the ast chapter of the device-specific datasheet for more information. Table 7-7 Parameters 7.4.4. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] input_freq Prescaled AST Clock Frequency [in] tuned_freq Desired AST frequency Function ast_disable() Disable the AST counter and module. void ast_disable( Ast * ast) Table 7-8 Parameters 7.4.5. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer Function ast_disable_digital_tuner() Disable the AST digital tuner. void ast_disable_digital_tuner( Ast * ast) Table 7-9 Parameters 7.4.6. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer Function ast_disable_event() Disable an AST event. void ast_disable_event( Ast * ast, ast_event_source_t source) Table 7-10 Parameters Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] source AST event source to be disabled Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 15 7.4.7. Function ast_disable_interrupt() Disable an AST interrupt. void ast_disable_interrupt( Ast * ast, ast_interrupt_source_t source) Table 7-11 Parameters 7.4.8. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] source AST interrupt source to be disabled Function ast_disable_wakeup() Disable an AST asynchronous wake-up source. void ast_disable_wakeup( Ast * ast, ast_wakeup_source_t source) Table 7-12 Parameters 7.4.9. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] source AST wake-up source to be disabled Function ast_enable() Enable the AST module. void ast_enable( Ast * ast) Table 7-13 Parameters 7.4.10. Data direction Parameter name Description [in] ast Module hardware register base address pointer Function ast_enable_counter_clear_on_alarm() Enable the option to clear the counter on an AST alarm. void ast_enable_counter_clear_on_alarm( Ast * ast, uint8_t alarm_channel) Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 16 Table 7-14 Parameters 7.4.11. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] alarm_channel AST Alarm Channel Function ast_enable_event() Enable an AST event. void ast_enable_event( Ast * ast, ast_event_source_t source) Table 7-15 Parameters 7.4.12. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] source AST event source to be enabled Function ast_enable_interrupt() Enable an AST interrupt. void ast_enable_interrupt( Ast * ast, ast_interrupt_source_t source) Table 7-16 Parameters 7.4.13. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] source AST interrupt source to be enabled Function ast_enable_wakeup() Enable an AST asynchronous wake-up source. void ast_enable_wakeup( Ast * ast, ast_wakeup_source_t source) Table 7-17 Parameters Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] source AST wake-up source to be enabled Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 17 7.4.14. Function ast_init_digital_tuner() Initialize the AST digital tuner. void ast_init_digital_tuner( Ast * ast, bool add, uint8_t value, uint8_t exp) Table 7-18 Parameters Data direction Parameter name Description 7.4.15. [in, out] ast Module hardware register base address pointer [in] add Set to true if the frequency needs to be increased, false if it has to be decreased. [in] value Parameter used in the formula [in] exp Parameter used in the formula Function ast_is_busy() Check the busy status of AST. bool ast_is_busy( Ast * ast) Table 7-19 Parameters Data direction Parameter name Description [in] ast Module hardware register base address pointer Returns AST busy status. Table 7-20 Return Values 7.4.16. Return value Description false AST is not busy true AST is busy Function ast_is_clkbusy() Check the busy status of the AST clock. bool ast_is_clkbusy( Ast * ast) Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 18 Table 7-21 Parameters Data direction Parameter name Description [in] ast Module hardware register base address pointer Returns AST clock busy status. Table 7-22 Return Values 7.4.17. Return value Description false AST clock is not busy true AST clock is busy Function ast_is_enabled() Check the status of the AST module. bool ast_is_enabled( Ast * ast) Table 7-23 Parameters Data direction Parameter name Description [in, out] ast Module hardware register base address pointer Table 7-24 Return Values 7.4.18. Return value Description false AST is not enabled true AST is enabled Function ast_read_calendar_value() Get the AST current calendar value. struct ast_calendar ast_read_calendar_value( Ast * ast) Note: There maybe a compiler warning about returning a structure type value, however it is safe because ast_calendar is actually uint32_t type. Table 7-25 Parameters Data direction Parameter name Description [in] ast Module hardware register base address pointer Returns The AST current calendar value. Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 19 7.4.19. Function ast_read_counter_value() Get the AST current counter value. uint32_t ast_read_counter_value( Ast * ast) Table 7-26 Parameters Data direction Parameter name Description [in] ast Module hardware register base address pointer Returns AST current counter value. 7.4.20. Function ast_read_interrupt_mask() Get the AST interrupt mask value. uint32_t ast_read_interrupt_mask( Ast * ast) Table 7-27 Parameters Data direction Parameter name Description [in] ast Module hardware register base address pointer Returns AST Interrupt mask value. 7.4.21. Function ast_read_status() Get the status of AST. uint32_t ast_read_status( Ast * ast) Table 7-28 Parameters Data direction Parameter name Description [in] ast Module hardware register base address pointer Returns AST status. 7.4.22. Function ast_set_callback() Set callback for AST interrupts. void ast_set_callback( Ast * ast, ast_interrupt_source_t source, ast_callback_t callback, Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 20 uint8_t irq_line, uint8_t irq_level) Table 7-29 Parameters 7.4.23. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] source AST interrupt source [in] callback Callback function pointer [in] irq_line Interrupt line [in] irq_level Interrupt level Function ast_set_config() Initialize and enable the AST module in Calendar Mode or Counter Mode. uint32_t ast_set_config( Ast * ast, struct ast_config * ast_conf) Note: If you use the 32kHz oscillator it will be enabled by this function. Table 7-30 Parameters Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] ast_conf AST configuration structure pointer Returns Initialization result. Table 7-31 Return Values 7.4.24. Return value Description 0 Initialization failed due to a software timeout 1 Initialization succeeded Function ast_start() Start the AST counter. void ast_start( Ast * ast) Table 7-32 Parameters Data direction Parameter name Description [in, out] ast Module hardware register base address pointer Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 21 7.4.25. Function ast_stop() Stop the AST counter. void ast_stop( Ast * ast) Table 7-33 Parameters 7.4.26. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer Function ast_write_alarm0_value() Set the AST alarm0 value. void ast_write_alarm0_value( Ast * ast, uint32_t alarm_value) Table 7-34 Parameters 7.4.27. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] alarm_value AST alarm0 value Function ast_write_calendar_value() Set the AST current calendar value. void ast_write_calendar_value( Ast * ast, struct ast_calendar calendar) Table 7-35 Parameters 7.4.28. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] calendar Startup date structure Function ast_write_counter_value() Set the AST current counter value. void ast_write_counter_value( Ast * ast, uint32_t ast_counter) Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 22 Table 7-36 Parameters 7.4.29. Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] ast_counter Startup counter value Function ast_write_periodic0_value() Set the AST periodic0 value. void ast_write_periodic0_value( Ast * ast, uint32_t pir) Table 7-37 Parameters Data direction Parameter name Description [in, out] ast Module hardware register base address pointer [in] pir AST periodic0 7.5. Enumeration Definitions 7.5.1. Enum ast_event_source AST event source. Table 7-38 Members 7.5.2. Enum value Description AST_EVENT_ALARM Alarm event generation AST_EVENT_PER Peripheral event generation AST_EVENT_OVF Counter overflow event generation Enum ast_interrupt_source AST interrupt source. Table 7-39 Members Enum value Description AST_INTERRUPT_ALARM Alarm interrupt AST_INTERRUPT_PER Periodic interrupt AST_INTERRUPT_OVF Overflow interrupt AST_INTERRUPT_READY Synchronization complete interrupt AST_INTERRUPT_CLKREADY Clock synchronization complete interrupt Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 23 7.5.3. Enum ast_mode AST Calendar Mode. Table 7-40 Members 7.5.4. Enum value Description AST_COUNTER_MODE Counter Mode AST_CALENDAR_MODE Calendar Mode Enum ast_oscillator_type AST Oscillator type. Table 7-41 Members 7.5.5. Enum value Description AST_OSC_RC System RC oscillator (RCSYS) AST_OSC_32KHZ 32kHz oscillator (OSC32 or RC32) AST_OSC_PB APB clock AST_OSC_GCLK Generic clock (GCLK) AST_OSC_1KHZ 1kHz clock from the 32kHz oscillator or 32kHz RC oscillator (CLK_1K) Enum ast_wakeup_source AST wake-up source. Table 7-42 Members Enum value Description AST_WAKEUP_ALARM Alarm wake-up source AST_WAKEUP_PER Peripheral interrupt wake-up source AST_WAKEUP_OVF Counter overflow wake-up source Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 24 8. Extra Information for Asynchronous Timer Driver 8.1. Acronyms Below is a table listing the acronyms used in this module, along with their intended meanings. 8.2. Acronym Definition BPM Backup Power Manager QSG Quick Start Guide Dependencies This driver has the following dependencies: • 8.3. None Errata There are no errata related to this driver. 8.4. Module History An overview of the module history is presented in the table below, with details on the enhancements and fixes made to the module since its first release. The current version of this corresponds to the newest version in the table. Changelog Initial document release Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 25 9. Examples for Asynchronous Timer Driver This is a list of the available Quick Start Guides (QSGs) and example applications for SAM4L Asynchronous Timer (AST) Driver. QSGs are simple examples with step-by-step instructions to configure and use this driver in a selection of use cases. Note that a QSG can be compiled as a standalone application or be added to the user application. • • • 9.1. Quick Start Guide for the AST driver Asynchronous Timer (AST) - Example 1 Calendar Mode Asynchronous Timer (AST) - Example 2 Alarm Wakeup Quick Start Guide for the AST driver This is the quick start guide for the SAM4L Asynchronous Timer (AST) Driver, with step-by-step instructions on how to configure and use the driver for a specific use case.The code examples can be copied into the main application loop or any other function that will need to control the AST module. 9.1.1. Use Cases • 9.1.2. AST Basic Usage AST Basic Usage This use case will demonstrate how to initialize the AST module to calendar or counter mode. 9.1.3. Setup Steps 9.1.3.1. Prerequisites This module requires the following service: • System Clock Management (sysclock) 9.1.3.2. Setup Code Add this to the main loop or a setup function: osc_priv_enable_osc32(); 9.1.3.3. Workflow 1. Enable the AST module: ast_enable(AST); 2. Initialize the AST to counter mode: * * * * * * * 3. struct ast_config ast_conf; ast_conf.mode = AST_COUNTER_MODE; ast_conf.osc_type = AST_OSC_32KHZ; ast_conf.psel = AST_PSEL_32KHZ_1HZ; ast_conf.counter = 0; ast_set_config(AST, &ast_conf); Or initialize the AST to calendar mode: * struct ast_calendar calendar; * struct ast_config ast_conf; Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 26 * * * * * * * * * * * * calendar.FIELD.sec = 0; calendar.FIELD.min = 15; calendar.FIELD.hour = 12; calendar.FIELD.day = 20; calendar.FIELD.month = 9; calendar.FIELD.year = 12; struct ast_config ast_conf; ast_conf.mode = AST_CALENDAR_MODE; ast_conf.osc_type = AST_OSC_32KHZ; ast_conf.psel = AST_PSEL_32KHZ_1HZ; ast_conf.calendar = calendar; ast_set_config(AST, &ast_conf) Note: We need to set the clock after prescaler to 1Hz. 9.1.4. Usage Steps 9.1.4.1. Usage Code We can get the calendar value by: calendar = ast_read_calendar_value(AST); Or we can get the counter value by: ast_counter = ast_read_counter_value(AST); We can set the alarm interrupt by: * ast_write_alarm0_value(AST, calendar.field + 1); * ast_set_callback(AST, ast_interrupt_alarm, ast_alarm_callback, * AST_ALARM_IRQn, 1); And we can set the periodic interrupt by: * ast_write_periodic0_value(AST, AST_PSEL_32KHZ_1HZ - 4); * ast_set_callback(AST, ast_interrupt_per, ast_per_callback, * AST_PER_IRQn, 1); 9.2. Asynchronous Timer (AST) - Example 1 Calendar Mode 9.2.1. Purpose This example demonstrates how to use the AST driver with the 32kHz oscillator crystal. 9.2.2. Requirements This example can be used with SAM4L evaluation kits. 9.2.3. Description Upon startup, the program uses the USART driver to display a real time counter incremented every second. The display is triggered by the interrupt controller module. 9.2.4. Main Files • ast.c: Asynchronous Timer driver Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 27 • • 9.2.5. ast.h: Asynchronous Timer driver header file ast_example1.c: Asynchronous Timer example application Compilation Information ® ® This software is written for GNU GCC and IAR Embedded Workbench for Atmel . Other compilers may or may not work. 9.2.6. Usage 1. 2. 3. 4. Build the program and download it into the evaluation board. ® On the computer, open, and configure a terminal application (e.g., HyperTerminal on Microsoft ® Windows ) with these settings: • 115200 baud • 8 bits of data • No parity • 1 stop bit • No flow control Start the application. In the terminal window, the following text should appear: * * * * * * * 5. -- AST Example 1 in Calendar Mode --- xxxxxx-xx -- Compiled: xxx xx xxxx xx:xx:xx -Config AST with 32 KHz oscillator. Calendar: Year:XX Month:XX Day:XX, XXhXXmXXs Approximately every second the time should increment. 9.3. Asynchronous Timer (AST) - Example 2 Alarm Wakeup 9.3.1. Purpose This example demonstrates how to use the AST driver with the 32kHz oscillator crystal and to also setup an alarm to wake-up the device from its low power modes. 9.3.2. Requirements This example can be used with SAM4L evaluation kits. 9.3.3. Description Upon startup, the program uses the USART driver to display a menu that lets the user select the low power mode from which the device will wake-up when the AST Alarm triggers. 9.3.4. Main Files • • • ast.c: Asynchronous Timer driver ast.h: Asynchronous Timer driver header file ast_example2.c: Asynchronous Timer example application using AST wakeup and BPM sleep function. Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 28 9.3.5. Compilation Information This software is written for GNU GCC and IAR Embedded Workbench for Atmel. Other compilers may or may not work. 9.3.6. Usage 1. 2. 3. 4. Build the program and download it into the evaluation board. ® On the computer, open, and configure a terminal application (e.g., HyperTerminal on Microsoft ® Windows ) with these settings: • 115200 baud • 8 bits of data • No parity • 1 stop bit • No flow control Start the application. In the terminal window, the following text should appear: * * * * * * * * * * * * * * * * -- -- AST Example 2 in Counter Mode --- xxxxxx-xx -- Compiled: xxx xx xxxx xx:xx:xx -Config AST with 32kHz oscillator. Use alarm0 to wakeup from low power mode. Menu : -1: 2: 3: 4: 5: 6: 7: Select low power mode: Sleep mode 0 Sleep mode 1 Sleep mode 2 Sleep mode 3 Wait mode Retention mode Backup mode Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 29 10. Document Revision History Doc. Rev. Date Comments 42283B 07/2015 Updated title of application note and added list of supported devices 42283A 05/2014 Initial document release Atmel AT07942: SAM4L Asynchronous Timer (AST) Driver [APPLICATION NOTE] Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 30 Atmel Corporation © 1600 Technology Drive, San Jose, CA 95110 USA T: (+1)(408) 441.0311 F: (+1)(408) 436.4200 | www.atmel.com 2015 Atmel Corporation. / Rev.: Atmel-42283B-SAM4L-Asynchronous-Timer-AST_AT07942_Application Note-07/2015 ® ® Atmel , Atmel logo and combinations thereof, Enabling Unlimited Possibilities , and others are registered trademarks or trademarks of Atmel Corporation in U.S. and ® ® ® other countries. ARM , ARM Connected , and others are registered trademarks of ARM Ltd. Windows is a registered trademark of Microsoft Corporation in U.S. and or other countries. Other terms and product names may be trademarks of others. DISCLAIMER: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and products descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. SAFETY-CRITICAL, MILITARY, AND AUTOMOTIVE APPLICATIONS DISCLAIMER: Atmel products are not designed for and will not be used in connection with any applications where the failure of such products would reasonably be expected to result in significant personal injury or death (“Safety-Critical Applications”) without an Atmel officer's specific written consent. Safety-Critical Applications include, without limitation, life support devices and systems, equipment or systems for the operation of nuclear facilities and weapons systems. Atmel products are not designed nor intended for use in military or aerospace applications or environments unless specifically designated by Atmel as military-grade. Atmel products are not designed nor intended for use in automotive applications unless specifically designated by Atmel as automotive-grade.