00001
00082 #include <stddef.h>
00083 #include <stdio.h>
00084 #include <avr32/io.h>
00085 #include "compiler.h"
00086 #include "board.h"
00087 #include "power_clocks_lib.h"
00088 #include "gpio.h"
00089 #include "pm_uc3c.h"
00090 #include "scif_uc3c.h"
00091 #include "intc.h"
00092 #include "can.h"
00093 #include "canif.h"
00094 #if defined (__GNUC__) && defined (__AVR32__)
00095 #include "nlao_cpu.h"
00096 #include "nlao_usart.h"
00097 #endif
00098 #include "usart.h"
00099
00100 #include "conf_can_example.h"
00101
00103 volatile can_msg_t mob_ram_ch0[NB_MOB_CHANNEL];
00104
00106 void can_example_prepare_data_to_send(U8 mode);
00107
00108
00109 volatile U8 nb_message_received_on_channel0 = 0;
00111 void can_out_callback_channel0(U8 handle, U8 event)
00112 {
00113 gpio_tgl_gpio_pin(LED0_GPIO);
00114
00115
00116 appli_rx_msg.can_msg->data.u64 = can_get_mob_data(0,handle).u64;
00117 appli_rx_msg.can_msg->id = can_get_mob_id(0,handle);
00118 appli_rx_msg.dlc = can_get_mob_dlc(0,handle);
00119 appli_rx_msg.status = event;
00120 can_mob_free(0,handle);
00121 nb_message_received_on_channel0 = 1;
00122
00123 }
00124
00127 static void init_exceptions(void)
00128 {
00129 #if defined (__GNUC__) && defined (__AVR32__)
00130
00131 extern void _evba;
00132
00133
00134
00135 Set_system_register(AVR32_EVBA, (int)&_evba);
00136 #endif
00137
00138
00139 Enable_global_exception();
00140 }
00141
00144 static void init_sys_clocks(void)
00145 {
00146
00147 scif_configure_osc_crystalmode(SCIF_OSC0, FOSC0);
00148 scif_enable_osc(SCIF_OSC0, OSC0_STARTUP, true);
00149 pm_set_mclk_source(PM_CLK_SRC_OSC0);
00150
00151
00152 scif_gc_setup(AVR32_SCIF_GCLK_CANIF,
00153 SCIF_GCCTRL_OSC0,
00154 AVR32_SCIF_GC_NO_DIV_CLOCK,
00155 0);
00156
00157 scif_gc_enable(AVR32_SCIF_GCLK_CANIF);
00158
00159 #if defined (__GNUC__) && defined (__AVR32__)
00160
00161 set_cpu_hz(FPBA_HZ);
00162 #endif
00163 }
00164
00167 static void init_stdio(void)
00168 {
00169
00170 static const gpio_map_t STDIO_USART_GPIO_MAP =
00171 {
00172 {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00173 {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00174 };
00175
00176 static const usart_options_t STDIO_USART_OPTIONS =
00177 {
00178 .baudrate = STDIO_USART_BAUDRATE,
00179 .charlength = 8,
00180 .paritytype = USART_NO_PARITY,
00181 .stopbits = USART_1_STOPBIT,
00182 .channelmode = USART_NORMAL_CHMODE
00183 };
00184
00185 gpio_enable_module(STDIO_USART_GPIO_MAP,
00186 sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00187
00188 usart_init_rs232(STDIO_USART, &STDIO_USART_OPTIONS, FPBA_HZ);
00189
00190 #if defined (__GNUC__) && defined (__AVR32__)
00191
00192 set_usart_base((void *)STDIO_USART);
00193 #endif
00194 }
00195
00196
00199 static void init_interrupts(void)
00200 {
00201
00202 INTC_init_interrupts();
00203
00204
00205 Enable_global_interrupt();
00206 }
00207
00208
00212 #if defined (__GNUC__) && defined (__AVR32__)
00213 int _init_startup(void)
00214 #elif defined (__ICCAVR32__)
00215 int __low_level_init(void)
00216 #endif
00217 {
00218 init_exceptions();
00219 init_sys_clocks();
00220 init_stdio();
00221 init_interrupts();
00222
00223
00224
00225 return 1;
00226 }
00227
00228
00229 int main(void)
00230 {
00231
00232 #if defined (__GNUC__) && defined (__AVR32__)
00233 setbuf(stdin, NULL);
00234 #endif
00235 setbuf(stdout, NULL);
00236
00237
00238 Disable_global_interrupt();
00239
00240
00241 INTC_init_interrupts();
00242
00243 static const gpio_map_t CAN_GPIO_MAP =
00244 {
00245 {AVR32_CANIF_RXLINE_0_0_PIN, AVR32_CANIF_RXLINE_0_0_FUNCTION},
00246 {AVR32_CANIF_TXLINE_0_0_PIN, AVR32_CANIF_TXLINE_0_0_FUNCTION}
00247 };
00248
00249 gpio_enable_module(CAN_GPIO_MAP,
00250 sizeof(CAN_GPIO_MAP) / sizeof(CAN_GPIO_MAP[0]));
00251
00252
00253 can_init(0,
00254 ((U32)&mob_ram_ch0[0]),
00255 CANIF_CHANNEL_MODE_LISTENING,
00256 can_out_callback_channel0);
00257
00258
00259 Enable_global_interrupt();
00260
00261 printf("\nUC3C CAN Examples 1\n");
00262
00263 printf(CAN_Wakeup);
00264
00265
00266 can_init(0,
00267 ((U32)&mob_ram_ch0[0]),
00268 CANIF_CHANNEL_MODE_NORMAL,
00269 can_out_callback_channel0);
00270
00271
00272 appli_rx_msg.handle = can_mob_alloc(0);
00273
00274
00275 can_rx(0,
00276 appli_rx_msg.handle,
00277 appli_rx_msg.req_type,
00278 appli_rx_msg.can_msg);
00279
00280
00281
00282 CANIF_clr_reset(0);
00283
00284 while(!CANIF_channel_enable_status(0));
00285
00286 CANIF_enable_wakeup(0);
00287
00288 SLEEP(AVR32_PM_SMODE_STATIC);
00289
00290 CANIF_clr_interrupt_status(0);
00291
00292 CANIF_disable_wakeup(0);
00293
00294 printf(CAN_WakeupD);
00295
00296 can_init(0,
00297 ((U32)&mob_ram_ch0[0]),
00298 CANIF_CHANNEL_MODE_NORMAL,
00299 can_out_callback_channel0);
00300
00301
00302 appli_rx_msg.handle = can_mob_alloc(0);
00303
00304
00305 can_rx(0,
00306 appli_rx_msg.handle,
00307 appli_rx_msg.req_type,
00308 appli_rx_msg.can_msg);
00309
00310 while(1);
00311
00312 }