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
00144 static const gpio_map_t USART_GPIO_MAP =
00145 {
00146 {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION},
00147 {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION}
00148 };
00149
00150
00151 static const usart_options_t USART_OPTIONS =
00152 {
00153 .baudrate = 57600,
00154 .charlength = 8,
00155 .paritytype = USART_NO_PARITY,
00156 .stopbits = USART_1_STOPBIT,
00157 .channelmode = 0
00158 };
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\r\n");
00173
00174 usart_write_line(EXAMPLE_USART, "AST 32 KHz oscillator program test.\r\n");
00175
00176 ast_calendar_t ast_calendar;
00177 ast_calendar.FIELD.sec = 0;
00178 ast_calendar.FIELD.min = 15;
00179 ast_calendar.FIELD.hour = 12;
00180 ast_calendar.FIELD.day = 5;
00181 ast_calendar.FIELD.month= 6;
00182 ast_calendar.FIELD.year = 9;
00183
00184
00185 if (!ast_init_calendar(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_calendar))
00186 {
00187 usart_write_line(EXAMPLE_USART, "Error initializing the AST\r\n");
00188 while(1);
00189 }
00190
00191 ast_enable(&AVR32_AST);
00192
00193 volatile int i;
00194 while(1)
00195 {
00196
00197 for ( i=0 ; i < 10000 ; i++);
00198 gpio_tgl_gpio_pin(LED0_GPIO);
00199
00200
00201 usart_write_line(EXAMPLE_USART, "\x1B[5;1H");
00202 ast_calendar = ast_get_calendar_value(&AVR32_AST);
00203 usart_write_line(EXAMPLE_USART, "Timer: ");
00204 ptemp = print_i(temp, ast_calendar.FIELD.sec);
00205 usart_write_line(EXAMPLE_USART, ptemp);
00206 usart_write_line(EXAMPLE_USART, " sec ");
00207 }
00208 }