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 # define FPBA FOSC0
00109 #endif
00110
00111 #if BOARD == UC3L_EK
00112 # define EXAMPLE_USART (&AVR32_USART3)
00113 # define EXAMPLE_USART_RX_PIN AVR32_USART3_RXD_0_0_PIN
00114 # define EXAMPLE_USART_RX_FUNCTION AVR32_USART3_RXD_0_0_FUNCTION
00115 # define EXAMPLE_USART_TX_PIN AVR32_USART3_TXD_0_0_PIN
00116 # define EXAMPLE_USART_TX_FUNCTION AVR32_USART3_TXD_0_0_FUNCTION
00117 # define EXAMPLE_TARGET_DFLL_FREQ_HZ 96000000 // DFLL target frequency, in Hz
00118 # define EXAMPLE_TARGET_MCUCLK_FREQ_HZ 12000000 // MCU clock target frequency, in Hz
00119 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ 12000000 // PBA clock target frequency, in Hz
00120 # define FPBA EXAMPLE_TARGET_PBACLK_FREQ_HZ
00121
00123
00124 static scif_gclk_opt_t gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF };
00125 static pcl_freq_param_t pcl_dfll_freq_param =
00126 {
00127 .main_clk_src = PCL_MC_DFLL0,
00128 .cpu_f = EXAMPLE_TARGET_MCUCLK_FREQ_HZ,
00129 .pba_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
00130 .pbb_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
00131 .dfll_f = EXAMPLE_TARGET_DFLL_FREQ_HZ,
00132 .pextra_params = &gc_dfllif_ref_opt
00133 };
00135 #endif
00136
00137 #if !defined(EXAMPLE_USART) || \
00138 !defined(EXAMPLE_USART_RX_PIN) || \
00139 !defined(EXAMPLE_USART_RX_FUNCTION) || \
00140 !defined(EXAMPLE_USART_TX_PIN) || \
00141 !defined(EXAMPLE_USART_TX_FUNCTION)
00142 # error The USART configuration to use in this example is missing.
00143 #endif
00145
00146
00149 char *print_i(char *str, int n)
00150 {
00151 int i = 10;
00152
00153 str[i] = '\0';
00154 do
00155 {
00156 str[--i] = '0' + n%10;
00157 n /= 10;
00158 }while(n);
00159
00160 return &str[i];
00161 }
00162
00166 int main( void )
00167 {
00168 char temp[20];
00169 char *ptemp;
00170
00171 static const gpio_map_t USART_GPIO_MAP =
00172 {
00173 {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION},
00174 {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION}
00175 };
00176
00177
00178 static const usart_options_t USART_OPTIONS =
00179 {
00180 .baudrate = 57600,
00181 .charlength = 8,
00182 .paritytype = USART_NO_PARITY,
00183 .stopbits = USART_1_STOPBIT,
00184 .channelmode = 0
00185 };
00186
00187 #if BOARD == UC3L_EK
00188 scif_osc32_opt_t opt = {
00189 SCIF_OSC_MODE_2PIN_CRYSTAL_HICUR,
00190 AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC,
00191 true,
00192 false,
00193 true
00194 };
00195
00196 #else
00197 scif_osc32_opt_t opt;
00198 opt.mode = SCIF_OSC_MODE_2PIN_CRYSTAL;
00199 opt.startup = AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC;
00200 #endif
00201
00202 #if BOARD == UC3L_EK
00203
00204
00205
00206 pcl_configure_clocks(&pcl_dfll_freq_param);
00207
00208
00209
00210
00211
00212 #else
00213 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00214 #endif
00215
00216
00217 scif_start_osc32(&opt,true);
00218
00219
00220 gpio_enable_module(USART_GPIO_MAP,
00221 sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));
00222
00223
00224 usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FPBA);
00225
00226
00227 usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n");
00228 usart_write_line(EXAMPLE_USART, "AVR32 UC3 - AST example\r\n");
00229
00230 usart_write_line(EXAMPLE_USART, "AST 32 KHz oscillator program test.\r\n");
00231
00232 ast_calendar_t ast_calendar;
00233 ast_calendar.FIELD.sec = 0;
00234 ast_calendar.FIELD.min = 15;
00235 ast_calendar.FIELD.hour = 12;
00236 ast_calendar.FIELD.day = 5;
00237 ast_calendar.FIELD.month= 6;
00238 ast_calendar.FIELD.year = 9;
00239
00240
00241 if (!ast_init_calendar(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_calendar))
00242 {
00243 usart_write_line(EXAMPLE_USART, "Error initializing the AST\r\n");
00244 while(1);
00245 }
00246
00247 ast_enable(&AVR32_AST);
00248
00249 volatile int i;
00250 while(1)
00251 {
00252
00253 for ( i=0 ; i < 10000 ; i++);
00254 gpio_tgl_gpio_pin(LED0_GPIO);
00255
00256
00257 usart_write_line(EXAMPLE_USART, "\x1B[5;1H");
00258 ast_calendar = ast_get_calendar_value(&AVR32_AST);
00259 usart_write_line(EXAMPLE_USART, "Timer: ");
00260 ptemp = print_i(temp, ast_calendar.FIELD.sec);
00261 usart_write_line(EXAMPLE_USART, ptemp);
00262 usart_write_line(EXAMPLE_USART, " sec ");
00263 }
00264 }