ast_example2.c File Reference


Detailed Description

AST example driver for AVR32 UC3.

This file provides an example for the AST on AVR32 UC3 devices.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file ast_example2.c.

#include <avr32/io.h>
#include "intc.h"
#include "board.h"
#include "compiler.h"
#include "ast.h"
#include "usart.h"
#include "gpio.h"
#include "power_clocks_lib.h"

Go to the source code of this file.

Defines

USART Settings
#define EXAMPLE_USART   (&AVR32_USART2)
#define EXAMPLE_USART_RX_FUNCTION   AVR32_USART2_RXD_0_1_FUNCTION
#define EXAMPLE_USART_RX_PIN   AVR32_USART2_RXD_0_1_PIN
#define EXAMPLE_USART_TX_FUNCTION   AVR32_USART2_TXD_0_1_FUNCTION
#define EXAMPLE_USART_TX_PIN   AVR32_USART2_TXD_0_1_PIN

Functions

int main (void)
 main function : do init and loop (poll if configured so)
char * print_i (char *str, int n)
 print_i function : convert the given number to an ASCII decimal representation.


Define Documentation

#define EXAMPLE_USART   (&AVR32_USART2)

Definition at line 103 of file ast_example2.c.

#define EXAMPLE_USART_RX_FUNCTION   AVR32_USART2_RXD_0_1_FUNCTION

Definition at line 105 of file ast_example2.c.

#define EXAMPLE_USART_RX_PIN   AVR32_USART2_RXD_0_1_PIN

Definition at line 104 of file ast_example2.c.

#define EXAMPLE_USART_TX_FUNCTION   AVR32_USART2_TXD_0_1_FUNCTION

Definition at line 107 of file ast_example2.c.

#define EXAMPLE_USART_TX_PIN   AVR32_USART2_TXD_0_1_PIN

Definition at line 106 of file ast_example2.c.


Function Documentation

int main ( void   ) 

main function : do init and loop (poll if configured so)

Definition at line 139 of file ast_example2.c.

References ast_disable_alarm0(), ast_enable(), ast_enable_alarm0(), ast_get_counter_value(), ast_init_counter(), AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_set_alarm0_value(), EXAMPLE_USART, EXAMPLE_USART_RX_FUNCTION, EXAMPLE_USART_RX_PIN, EXAMPLE_USART_TX_FUNCTION, EXAMPLE_USART_TX_PIN, ast_calendar_t::FIELD, ast_counter_t::FIELD, and print_i().

00140 {
00141   char temp[20];
00142   char *ptemp;
00143   ast_calendar_t ast_alarm;
00144 
00145   static const gpio_map_t USART_GPIO_MAP =
00146   {
00147     {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION},
00148     {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION}
00149   };
00150 
00151   // USART options
00152   static const usart_options_t USART_OPTIONS =
00153   {
00154     .baudrate     = 57600,
00155     .charlength   = 8,
00156     .paritytype   = USART_NO_PARITY,
00157     .stopbits     = USART_1_STOPBIT,
00158     .channelmode  = 0
00159   };
00160 
00161   pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00162   
00163   // Assign GPIO pins to USART0.
00164   gpio_enable_module(USART_GPIO_MAP,
00165                      sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));
00166 
00167   // Initialize USART in RS232 mode
00168   usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FOSC0);
00169 
00170   // Welcome sentence
00171   usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n");
00172   usart_write_line(EXAMPLE_USART, "AVR32 UC3 - AST example 2\r\n");
00173   usart_write_line(EXAMPLE_USART, "AST 32 KHz oscillator counter example.\r\n");
00174   usart_write_line(EXAMPLE_USART, "Alarm0 wakeup from static sleep mode every second.\r\n");
00175 
00176   // Unsing counter mode and set it to 0
00177   ast_counter_t ast_counter;
00178   ast_counter.FIELD.val  = 0;
00179   
00180   // Initialize the AST
00181   if (!ast_init_counter(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_counter))
00182   {
00183     usart_write_line(EXAMPLE_USART, "Error initializing the AST\r\n");
00184     while(1);
00185   }
00186   
00187   // Alarm 0 sends a wakeup signal to the Power manager
00188   AVR32_AST.WER.alarm0=1;
00189  
00190   // Enable the AST
00191   ast_enable(&AVR32_AST);
00192    
00193   while(1)
00194   {
00195       // disable alarm 0
00196       ast_disable_alarm0(&AVR32_AST);
00197     
00198       //ast_init_counter Set Alarm to current time+30 seconds
00199       ast_counter = ast_get_counter_value(&AVR32_AST);
00200       ast_alarm.FIELD.sec = ast_alarm.FIELD.sec+1;
00201       ast_set_alarm0_value(&AVR32_AST,ast_alarm);
00202       
00203       // Enable alarm 0
00204       ast_enable_alarm0(&AVR32_AST);
00205 
00206       // Go into static sleep mode 
00207       SLEEP(AVR32_PM_SMODE_STATIC);
00208       
00209       // After wake up, clear the Alarm0
00210       AVR32_AST.SCR.alarm0=1;
00211 
00212       // Toggle Led0
00213       gpio_tgl_gpio_pin(LED0_GPIO);
00214       
00215       // Set cursor to the position (1; 6)
00216       usart_write_line(EXAMPLE_USART, "\x1B[6;1H");
00217       ast_counter = ast_get_counter_value(&AVR32_AST);
00218       usart_write_line(EXAMPLE_USART, "Timer: ");
00219       ptemp = print_i(temp, ast_counter.FIELD.val);
00220       usart_write_line(EXAMPLE_USART, ptemp);
00221       usart_write_line(EXAMPLE_USART, " sec ");
00222   }
00223 }

char* print_i ( char *  str,
int  n 
)

print_i function : convert the given number to an ASCII decimal representation.

Definition at line 122 of file ast_example2.c.

00123 {
00124   int i = 10;
00125 
00126   str[i] = '\0';
00127   do
00128   {
00129     str[--i] = '0' + n%10;
00130     n /= 10;
00131   }while(n);
00132 
00133   return &str[i];
00134 }


Generated on Mon Dec 14 20:25:08 2009 for AVR32 - AST Driver Example 2 by  doxygen 1.5.5