00001
00047 #include <stddef.h>
00048 #include <stdio.h>
00049 #include <avr32/io.h>
00050 #include "compiler.h"
00051 #include "board.h"
00052 #include "power_clocks_lib.h"
00053 #include "gpio.h"
00054 #include "pm_uc3c.h"
00055 #include "scif_uc3c.h"
00056
00057 #include "print_funcs.h"
00058
00059 #include "can_task.h"
00060 #include "conf_can_task.h"
00061
00062 #include "dsp.h"
00063 #include "conf_demo.h"
00064
00065 can_msg_t mob_ram_ch0[NB_MOB_CHANNEL];
00066 can_msg_t mob_ram_ch1[NB_MOB_CHANNEL];
00067
00069 void can_example_prepare_data_to_send();
00070 void can_example_prepare_data_to_receive();
00071
00072 volatile bool message_transmitted_on_channel1 = false;
00073
00074 extern volatile U16 adc_current_conversion;
00075
00079 void can_out_callback_channel1(U8 handle, U8 event)
00080 {
00081 gpio_tgl_gpio_pin(LED2_GPIO);
00082
00083 can_mob_free(1,handle);
00084 message_transmitted_on_channel1 = true;
00085 }
00086
00087 volatile bool message_received_on_channel0 = false;
00088
00089 A_ALIGNED dsp16_t signal3_buf[BUFFER_LENGTH];
00090
00093 void can_out_callback_channel0(U8 handle, U8 event)
00094 {
00095 gpio_tgl_gpio_pin(LED3_GPIO);
00096
00097 pCANMOB_message2[0].can_msg->data.u64 = can_get_mob_data(0,handle).u64;
00098 pCANMOB_message2[0].can_msg->id = can_get_mob_id(0,handle);
00099 pCANMOB_message2[0].dlc = can_get_mob_dlc(0,handle);
00100 pCANMOB_message2[0].status = event;
00101 can_mob_free(0,handle);
00102 message_received_on_channel0 = true;
00103 }
00104
00105 void can_task_init(void)
00106 {
00107
00108 scif_gc_setup(AVR32_SCIF_GCLK_CANIF,
00109 SCIF_GCCTRL_OSC0,
00110 AVR32_SCIF_GC_NO_DIV_CLOCK,
00111 0);
00112
00113 scif_gc_enable(AVR32_SCIF_GCLK_CANIF);
00114
00115 static const gpio_map_t CAN_GPIO_MAP =
00116 {
00117 {AVR32_CANIF_RXLINE_0_0_PIN, AVR32_CANIF_RXLINE_0_0_FUNCTION},
00118 {AVR32_CANIF_TXLINE_0_0_PIN, AVR32_CANIF_TXLINE_0_0_FUNCTION},
00119 {AVR32_CANIF_RXLINE_1_2_PIN, AVR32_CANIF_RXLINE_1_2_FUNCTION},
00120 {AVR32_CANIF_TXLINE_1_2_PIN, AVR32_CANIF_TXLINE_1_2_FUNCTION}
00121 };
00122
00123 gpio_enable_module(CAN_GPIO_MAP,
00124 sizeof(CAN_GPIO_MAP) / sizeof(CAN_GPIO_MAP[0]));
00125
00126 can_example_prepare_data_to_receive();
00127 can_example_prepare_data_to_send();
00128 }
00129
00134 void can_task(void)
00135 {
00136 int i;
00137 int value;
00138 if (message_transmitted_on_channel1 == true)
00139 {
00140 message_transmitted_on_channel1 = false;
00141 if (message_received_on_channel0 == true)
00142 {
00143 message_received_on_channel0 = false;
00144 for (i=BUFFER_LENGTH-1;i>=1;i--)
00145 {
00146 signal3_buf[i] = signal3_buf[i-1];
00147 }
00148 value = (pCANMOB_message2[0].can_msg->data.u8[0]<<8)|(pCANMOB_message2[0].can_msg->data.u8[1]);
00149 signal3_buf[0] = ( value*0x20) - 0x8000;
00150 can_example_prepare_data_to_receive();
00151 can_example_prepare_data_to_send();
00152 }
00153 }
00154 }
00155
00161 void can_example_prepare_data_to_send()
00162 {
00163
00164 can_init(1,
00165 ((U32)&mob_ram_ch1[0]),
00166 CANIF_CHANNEL_MODE_NORMAL,
00167 can_out_callback_channel1);
00168
00169 pCANMOB_message0[0].handle = can_mob_alloc(1);
00170
00171
00172 if (pCANMOB_message0[0].handle==CAN_CMD_REFUSED)
00173 {
00174 while(1);
00175 }
00176 pCANMOB_message0[0].can_msg->data.u8[0] = ((adc_current_conversion&0xFF00)>>8);
00177 pCANMOB_message0[0].can_msg->data.u8[1] = (adc_current_conversion&0xFF);
00178 can_tx(1,
00179 pCANMOB_message0[0].handle,
00180 pCANMOB_message0[0].dlc,
00181 pCANMOB_message0[0].req_type,
00182 pCANMOB_message0[0].can_msg);
00183 }
00184
00189 void can_example_prepare_data_to_receive()
00190 {
00191
00192 can_init(0,
00193 ((U32)&mob_ram_ch0[0]),
00194 CANIF_CHANNEL_MODE_NORMAL,
00195 can_out_callback_channel0);
00196
00197
00198 pCANMOB_message2[0].handle = can_mob_alloc(0);
00199
00200 can_rx(0,
00201 pCANMOB_message2[0].handle,
00202 pCANMOB_message2[0].req_type,
00203 pCANMOB_message2[0].can_msg);
00204 }