00001
00047 #include <avr32/io.h>
00048 #include "compiler.h"
00049 #include "board.h"
00050 #include "power_clocks_lib.h"
00051 #include "gpio.h"
00052 #include "tc.h"
00053 #include "intc.h"
00054 #include "usart.h"
00055 #include "lin.h"
00056 #include "dsp.h"
00057 #include "lin_task.h"
00058 #include "conf_demo.h"
00059 # define TC_INSTANCE &AVR32_TC0
00060 # define TC_INSTANCE_IRQ AVR32_TC0_IRQ0
00061 # define TC_INSTANCE_IRQ_GROUP AVR32_TC0_IRQ_GROUP
00062
00063
00064 #define FPBA FOSC0
00065
00066 #define TC_CHANNEL 0
00067 #include "can.h"
00068
00069
00070 extern void lin_master_task_ID12(void);
00071
00072 extern void lin_slave_task_ID12(U8* d_buf);
00073
00074 extern void lin_master_cb_task_ID12(U8* d_buf);
00075
00076 U8 lin_data_out_node0[8];
00077
00078 volatile U8 lin_data_in_node1[8];
00079
00080 volatile unsigned char task_id = 0;
00081
00082 extern can_mob_t pCANMOB_message2[1];
00083 A_ALIGNED dsp16_t signal2_buf[BUFFER_LENGTH];
00084
00087 #if defined (__GNUC__)
00088 __attribute__((__interrupt__))
00089 #elif defined (__ICCAVR32__)
00090 #pragma handler = TC_INSTANCE_IRQ_GROUP, 1
00091 __interrupt
00092 #endif
00093 static void tc_irq(void)
00094 {
00095
00096 tc_read_sr(TC_INSTANCE, TC_CHANNEL);
00097 lin_master_task_ID12();
00098 }
00099
00100 volatile avr32_tc_t *tc = TC_INSTANCE;
00101
00104 void start_scheduler(void)
00105 {
00106
00107 static const tc_waveform_opt_t WAVEFORM_OPT =
00108 {
00109 .channel = TC_CHANNEL,
00110
00111 .bswtrg = TC_EVT_EFFECT_NOOP,
00112 .beevt = TC_EVT_EFFECT_NOOP,
00113 .bcpc = TC_EVT_EFFECT_NOOP,
00114 .bcpb = TC_EVT_EFFECT_NOOP,
00115
00116 .aswtrg = TC_EVT_EFFECT_NOOP,
00117 .aeevt = TC_EVT_EFFECT_NOOP,
00118 .acpc = TC_EVT_EFFECT_NOOP,
00119 .acpa = TC_EVT_EFFECT_NOOP,
00120
00121 .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,
00122 .enetrg = FALSE,
00123 .eevt = 0,
00124 .eevtedg = TC_SEL_NO_EDGE,
00125 .cpcdis = FALSE,
00126 .cpcstop = FALSE,
00127
00128 .burst = FALSE,
00129 .clki = FALSE,
00130 .tcclks = TC_CLOCK_SOURCE_TC5
00131 };
00132
00133 static const tc_interrupt_t TC_INTERRUPT =
00134 {
00135 .etrgs = 0,
00136 .ldrbs = 0,
00137 .ldras = 0,
00138 .cpcs = 1,
00139 .cpbs = 0,
00140 .cpas = 0,
00141 .lovrs = 0,
00142 .covfs = 0
00143 };
00144
00145 INTC_register_interrupt(&tc_irq, TC_INSTANCE_IRQ, AVR32_INTC_INT0);
00146
00147
00148 tc_init_waveform(tc, &WAVEFORM_OPT);
00149
00150
00151
00152
00153
00154 tc_write_rc(tc, TC_CHANNEL, (FPBA / 128)/2);
00155
00156 tc_configure_interrupts(tc, TC_CHANNEL, &TC_INTERRUPT);
00157
00158
00159 tc_start(tc, TC_CHANNEL);
00160 }
00161
00162 extern volatile avr32_usart_t* usart_lin_node1;
00163
00169 void lin_task_init(void)
00170 {
00171
00172
00173 lin_init (TRUE,0,9600,FOSC0);
00174
00175
00176 lin_init (FALSE,1,9600,FOSC0);
00177
00178
00179
00180
00181 lin_descript_list_node0[0].l_id = 0x22;
00182 lin_descript_list_node0[0].l_dlc = 8;
00183 lin_descript_list_node0[0].l_cmd = PUBLISH;
00184 lin_descript_list_node0[0].l_status = 0;
00185 lin_descript_list_node0[0].l_pt_data = lin_data_out_node0;
00186 lin_descript_list_node0[0].l_pt_function = lin_master_cb_task_ID12;
00187
00188
00189
00190
00191 lin_descript_list_node1[0].l_id = 0x22;
00192 lin_descript_list_node1[0].l_dlc = 8;
00193 lin_descript_list_node1[0].l_cmd = SUBSCRIBE;
00194 lin_descript_list_node1[0].l_status = 0;
00195 lin_descript_list_node1[0].l_pt_data = (U8 *)lin_data_in_node1;
00196 lin_descript_list_node1[0].l_pt_function = lin_slave_task_ID12;
00197
00198
00199 start_scheduler();
00200 }
00201
00205 void lin_task()
00206 {
00207 int i;
00208 int value;
00209 for (i=BUFFER_LENGTH-1;i>=1;i--)
00210 {
00211 signal2_buf[i] = signal2_buf[i-1];
00212 }
00213 value = (lin_descript_list_node1[0].l_pt_data[0]<<8)|(lin_descript_list_node1[1].l_pt_data[1]);
00214 signal2_buf[0] = ( value*0x20) - 0x8000;
00215 }
00218 void lin_task_stop()
00219 {
00220 }
00223 void lin_task_restart()
00224 {
00225 Disable_global_interrupt();
00226 lin_init (FALSE,1,9600,FOSC0);
00227 Enable_global_interrupt();
00228 }
00229
00230
00231
00235 void lin_master_task_ID12(void) {
00236 lin_send_cmd (0,lin_descript_list_node0[0].l_id, \
00237 lin_descript_list_node0[0].l_dlc );
00238 }
00242 void lin_master_cb_task_ID12(U8* d_buf) {
00243 gpio_tgl_gpio_pin(LED1_GPIO);
00244
00245 d_buf[0] = pCANMOB_message2[0].can_msg->data.u8[0];
00246 d_buf[1] = pCANMOB_message2[0].can_msg->data.u8[1];
00247
00248 }
00249
00250
00253 void lin_slave_task_ID12(U8* d_buf) {
00254 gpio_tgl_gpio_pin(LED0_GPIO);
00255 }
00256