00001
00089 #include <avr32/io.h>
00090 #include "compiler.h"
00091 #include "board.h"
00092 #include "power_clocks_lib.h"
00093 #include "gpio.h"
00094 #include "tc.h"
00095 #include "intc.h"
00096 #include "usart.h"
00097 #include "lin.h"
00098
00099 #if BOARD == UC3C_EK
00100 # define TC_INSTANCE &AVR32_TC0
00101 # define TC_INSTANCE_IRQ AVR32_TC0_IRQ0
00102 # define TC_INSTANCE_IRQ_GROUP AVR32_TC0_IRQ_GROUP
00103 #elif BOARD == EVK1101
00104 # define TC_INSTANCE &AVR32_TC
00105 # define TC_INSTANCE_IRQ AVR32_TC_IRQ0
00106 # define TC_INSTANCE_IRQ_GROUP AVR32_TC_IRQ_GROUP
00107 #else
00108 #error Wrong board
00109 #endif
00110
00111
00113 #define FPBA FOSC0
00114
00116 #define TC_CHANNEL 0
00117
00119 extern void lin_master_task_ID12(void);
00120
00122 extern void lin_master_task_ID15(void);
00123
00125 extern void lin_slave_task_ID12(U8* d_buf);
00126
00128 extern void lin_slave_task_ID15(U8* d_buf);
00129
00131 U8 lin_data_out_node0[8];
00132
00134 U8 lin_data_in_node0[8];
00135
00137 volatile unsigned char task_id = 0;
00138
00141 #if defined (__GNUC__)
00142 __attribute__((__interrupt__))
00143 #elif defined (__ICCAVR32__)
00144 #pragma handler = TC_INSTANCE_IRQ_GROUP, 1
00145 __interrupt
00146 #endif
00147 static void tc_irq(void)
00148 {
00149
00150 tc_read_sr(TC_INSTANCE, TC_CHANNEL);
00151
00152
00153 switch(task_id)
00154 {
00155 case 0 :
00156 lin_master_task_ID12();
00157 task_id=1;
00158 break;
00159
00160 case 1 :
00161 lin_master_task_ID15();
00162 task_id=0;
00163 break;
00164
00165 default :
00166 break;
00167 }
00168 }
00169
00170
00171
00174 void start_scheduler(void)
00175 {
00176 volatile avr32_tc_t *tc = TC_INSTANCE;
00177
00178
00179 static const tc_waveform_opt_t WAVEFORM_OPT =
00180 {
00181 .channel = TC_CHANNEL,
00182
00183 .bswtrg = TC_EVT_EFFECT_NOOP,
00184 .beevt = TC_EVT_EFFECT_NOOP,
00185 .bcpc = TC_EVT_EFFECT_NOOP,
00186 .bcpb = TC_EVT_EFFECT_NOOP,
00187
00188 .aswtrg = TC_EVT_EFFECT_NOOP,
00189 .aeevt = TC_EVT_EFFECT_NOOP,
00190 .acpc = TC_EVT_EFFECT_NOOP,
00191 .acpa = TC_EVT_EFFECT_NOOP,
00192
00193 .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,
00194 .enetrg = FALSE,
00195 .eevt = 0,
00196 .eevtedg = TC_SEL_NO_EDGE,
00197 .cpcdis = FALSE,
00198 .cpcstop = FALSE,
00199
00200 .burst = FALSE,
00201 .clki = FALSE,
00202 .tcclks = TC_CLOCK_SOURCE_TC5
00203 };
00204
00205 static const tc_interrupt_t TC_INTERRUPT =
00206 {
00207 .etrgs = 0,
00208 .ldrbs = 0,
00209 .ldras = 0,
00210 .cpcs = 1,
00211 .cpbs = 0,
00212 .cpas = 0,
00213 .lovrs = 0,
00214 .covfs = 0
00215 };
00216
00217
00218 INTC_register_interrupt(&tc_irq, TC_INSTANCE_IRQ, AVR32_INTC_INT0);
00219
00220
00221 tc_init_waveform(tc, &WAVEFORM_OPT);
00222
00223
00224
00225
00226
00227 tc_write_rc(tc, TC_CHANNEL, (FPBA / 128)/2);
00228
00229 tc_configure_interrupts(tc, TC_CHANNEL, &TC_INTERRUPT);
00230
00231
00232 tc_start(tc, TC_CHANNEL);
00233
00234 }
00235
00239 int main(void)
00240 {
00241
00242 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00243
00244 AVR32_HMATRIX.mcfg[AVR32_HMATRIX_MASTER_CPU_INSN] = 0x1;
00245
00246
00247
00248 INTC_init_interrupts();
00249
00250
00251 #ifdef MASTER_MODE
00252
00253 lin_init( TRUE, 0, 9600, FOSC0);
00254 #else
00255
00256 lin_init( FALSE, 0, 9600, FOSC0);
00257 #endif
00258
00259
00260
00261
00262 lin_descript_list_node0[0].l_id = 0x12;
00263 lin_descript_list_node0[0].l_dlc = 2;
00264 lin_descript_list_node0[0].l_cmd = PUBLISH;
00265 lin_descript_list_node0[0].l_status = 0;
00266 lin_descript_list_node0[0].l_pt_data = lin_data_out_node0;
00267 lin_descript_list_node0[0].l_pt_function = lin_slave_task_ID12;
00268
00269
00270
00271
00272 lin_descript_list_node0[1].l_id = 0x15;
00273 lin_descript_list_node0[1].l_dlc = 2;
00274 lin_descript_list_node0[1].l_cmd = SUBSCRIBE;
00275 lin_descript_list_node0[1].l_status = 0;
00276 lin_descript_list_node0[1].l_pt_data = lin_data_in_node0;
00277 lin_descript_list_node0[1].l_pt_function = lin_slave_task_ID15;
00278
00279 #ifdef MASTER_MODE
00280
00281 start_scheduler();
00282 #else
00283
00284 #endif
00285
00286 Enable_global_interrupt();
00287
00288 while (TRUE);
00289 }
00290
00291
00292
00296 void lin_master_task_ID12(void)
00297 {
00298 lin_send_cmd (0,lin_descript_list_node0[0].l_id, lin_descript_list_node0[0].l_dlc );
00299 }
00300
00304 void lin_master_task_ID15(void)
00305 {
00306 lin_send_cmd (0,lin_descript_list_node0[1].l_id, lin_descript_list_node0[1].l_dlc );
00307 }
00308
00309
00310
00314 void lin_slave_task_ID12(U8* d_buf)
00315 {
00316 gpio_tgl_gpio_pin(LED0_GPIO);
00317 d_buf[0] = 0x77;
00318 d_buf[1] = 0x88;
00319 }
00320
00324 void lin_slave_task_ID15(U8* d_buf)
00325 {
00326 gpio_tgl_gpio_pin(LED1_GPIO);
00327 }