00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00088 #include <avr32/io.h>
00089 #if __GNUC__
00090 # include "intc.h"
00091 #endif
00092 #include "board.h"
00093 #include "compiler.h"
00094 #include "rtc.h"
00095 #include "usart.h"
00096 #include "gpio.h"
00097 #include "pm.h"
00098
00099
00102
00103 #if BOARD == EVK1100
00104 # define EXAMPLE_USART (&AVR32_USART1)
00105 # define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_0_PIN
00106 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_0_FUNCTION
00107 # define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_0_PIN
00108 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
00109 #elif BOARD == EVK1101
00110 # define EXAMPLE_USART (&AVR32_USART1)
00111 # define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_0_PIN
00112 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_0_FUNCTION
00113 # define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_0_PIN
00114 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
00115 #elif BOARD == EVK1104
00116 # define EXAMPLE_USART (&AVR32_USART1)
00117 # define EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_0_PIN
00118 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_0_FUNCTION
00119 # define EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_0_PIN
00120 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
00121 #elif BOARD == EVK1105
00122 # define EXAMPLE_USART (&AVR32_USART0)
00123 # define EXAMPLE_USART_RX_PIN AVR32_USART0_RXD_0_0_PIN
00124 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART0_RXD_0_0_FUNCTION
00125 # define EXAMPLE_USART_TX_PIN AVR32_USART0_TXD_0_0_PIN
00126 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART0_TXD_0_0_FUNCTION
00127 #endif
00128
00129 #if !defined(EXAMPLE_USART) || \
00130 !defined(EXAMPLE_USART_RX_PIN) || \
00131 !defined(EXAMPLE_USART_RX_FUNCTION) || \
00132 !defined(EXAMPLE_USART_TX_PIN) || \
00133 !defined(EXAMPLE_USART_TX_FUNCTION)
00134 # error The USART configuration to use in this example is missing.
00135 #endif
00137
00138
00139
00140 volatile static int print_sec = 1;
00141
00142
00143 static int sec = 0;
00144
00145 #if __GNUC__
00146 __attribute__((__interrupt__))
00147 #elif __ICCAVR32__
00148
00149 #pragma handler = AVR32_RTC_IRQ_GROUP, 1
00150 __interrupt
00151 #endif
00152 void rtc_irq(void)
00153 {
00154
00155 sec++;
00156
00157
00158 rtc_clear_interrupt(&AVR32_RTC);
00159
00160
00161 print_sec = 1;
00162 }
00163
00167 char *print_i(char *str, int n)
00168 {
00169 int i = 10;
00170
00171 str[i] = '\0';
00172 do
00173 {
00174 str[--i] = '0' + n%10;
00175 n /= 10;
00176 }while(n);
00177
00178 return &str[i];
00179 }
00180
00184 int main( void )
00185 {
00186 char temp[20];
00187 char *ptemp;
00188
00189 static const gpio_map_t USART_GPIO_MAP =
00190 {
00191 {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION},
00192 {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION}
00193 };
00194
00195
00196 static const usart_options_t USART_OPTIONS =
00197 {
00198 .baudrate = 57600,
00199 .charlength = 8,
00200 .paritytype = USART_NO_PARITY,
00201 .stopbits = USART_1_STOPBIT,
00202 .channelmode = 0
00203 };
00204
00205 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00206
00207
00208 gpio_enable_module(USART_GPIO_MAP,
00209 sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));
00210
00211
00212 usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, 12000000);
00213
00214
00215 usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n");
00216 usart_write_line(EXAMPLE_USART, "AVR32 UC3 - RTC example\r\n");
00217
00218 usart_write_line(EXAMPLE_USART, "RTC 32 KHz oscillator program test.\r\n");
00219
00220
00221 Disable_global_interrupt();
00222
00223
00224 #if __GNUC__
00225
00226 INTC_init_interrupts();
00227
00228
00229 INTC_register_interrupt(&rtc_irq, AVR32_RTC_IRQ, AVR32_INTC_INT0);
00230 #endif
00231
00232
00233 if (!rtc_init(&AVR32_RTC, RTC_OSC_32KHZ, RTC_PSEL_32KHZ_1HZ))
00234 {
00235 usart_write_line(&AVR32_USART0, "Error initializing the RTC\r\n");
00236 while(1);
00237 }
00238
00239 rtc_set_top_value(&AVR32_RTC, 0);
00240
00241 rtc_enable_interrupt(&AVR32_RTC);
00242
00243 rtc_enable(&AVR32_RTC);
00244
00245
00246 Enable_global_interrupt();
00247
00248 while(1)
00249 {
00250 if (print_sec)
00251 {
00252
00253 usart_write_line(EXAMPLE_USART, "\x1B[5;1H");
00254 ptemp = print_i(temp, sec);
00255 usart_write_line(EXAMPLE_USART, "Timer: ");
00256 usart_write_line(EXAMPLE_USART, ptemp);
00257 usart_write_line(EXAMPLE_USART, "s");
00258 print_sec = 0;
00259 }
00260 }
00261 }