00001
00088 #include <avr32/io.h>
00089 #if defined (__GNUC__)
00090 # include "intc.h"
00091 #endif
00092 #include "board.h"
00093 #include "compiler.h"
00094 #include "ast.h"
00095 #include "usart.h"
00096 #include "gpio.h"
00097 #include "power_clocks_lib.h"
00098
00101
00102 #if BOARD == UC3C_EK
00103 # define EXAMPLE_USART (&AVR32_USART2)
00104 # define EXAMPLE_USART_RX_PIN AVR32_USART2_RXD_0_1_PIN
00105 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART2_RXD_0_1_FUNCTION
00106 # define EXAMPLE_USART_TX_PIN AVR32_USART2_TXD_0_1_PIN
00107 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART2_TXD_0_1_FUNCTION
00108 #endif
00109
00110 #if !defined(EXAMPLE_USART) || \
00111 !defined(EXAMPLE_USART_RX_PIN) || \
00112 !defined(EXAMPLE_USART_RX_FUNCTION) || \
00113 !defined(EXAMPLE_USART_TX_PIN) || \
00114 !defined(EXAMPLE_USART_TX_FUNCTION)
00115 # error The USART configuration to use in this example is missing.
00116 #endif
00118
00119
00122 char *print_i(char *str, int n)
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 }
00135
00139 int main( void )
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
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
00164 gpio_enable_module(USART_GPIO_MAP,
00165 sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));
00166
00167
00168 usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FOSC0);
00169
00170
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
00177 ast_counter_t ast_counter;
00178 ast_counter.FIELD.val = 0;
00179
00180
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
00188 AVR32_AST.WER.alarm0=1;
00189
00190
00191 ast_enable(&AVR32_AST);
00192
00193 while(1)
00194 {
00195
00196 ast_disable_alarm0(&AVR32_AST);
00197
00198
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
00204 ast_enable_alarm0(&AVR32_AST);
00205
00206
00207 SLEEP(AVR32_PM_SMODE_STATIC);
00208
00209
00210 AVR32_AST.SCR.alarm0=1;
00211
00212
00213 gpio_tgl_gpio_pin(LED0_GPIO);
00214
00215
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 }