void manageCAN() { /*struct rx_stat rxStat; int32 rxId; int8 rxData[8]; int8 rxLen;*/ /*if(can_kbhit()) // Une donnée est présente dans le buffer de réception du CAN { if(can_getd(rxId,&rxData[0],rxLen,rxStat)) { if(rxId == XXX_ID) // TODO : reçoit-on une donnée pour la carte alim ? Pas sûr ! { } } }*/ if(can_tbe()) { if(send_charge) { can_putd(ALM_CHARGE_ID,&charge_courante,8,1,false,false); } } }
void manageCAN() { struct rx_stat rxStat; int32 rxId; int8 rxData[8]; int8 rxLen; int1 txData[8]; if(can_kbhit()) // Une donnée est présente dans le buffer de réception du CAN { if(can_getd(rxId,rxData,rxLen,rxStat)) { switch(rxId) // TODO : Le TdB reçoit des données ? Vérifier lesquelles { case MAB_BOOST_ACK_ID: periode_boost = 1000; state_boost = false; break; case MAB_BRAKE_ACK_ID: periode_brake = 1000; state_brake = false; break; case MAB_ERR_ID: output_bit(ERR_1,rxData[0]); output_bit(ERR_2,rxData[1]); output_bit(ERR_3,rxData[2]); if(can_tbe()) { can_putd(TDB_ERR_ACK_ID,0,0,3,false,false); } break; } } } if(can_tbe()) { if(send_phares) { txData[0] = input(FEUX); txData[1] = input(CODES)|input(FEUX); txData[2] = input(CLIGN_G)|input(WARNING); txData[3] = input(CLIGN_D)|input(WARNING); can_putd(TDB_PHARES_ID,txData,4,1,false,false); } if(send_boost) { txData[0] = state_boost; can_putd(TDB_BOOST_ID,txData,1,1,false,false); } if(send_brake) { txData[0] = state_brake; can_putd(TDB_BRAKE_ID,txData,1,1,false,false); } } }
void main() { struct rx_stat rxstat; int32 rx_id; int in_data[8]; int rx_len; int out_data[8]; int16 tmp; int32 tx_id; int1 tx_rtr=0; int1 tx_ext=0; int tx_len=8; int tx_pri=3; // bitmap, expanded to 8 bit (needed?) int8 sequence[MAX_CHANNELS]; int32 val; int i; for (i = 0; i < MAX_CHANNELS; i++) sequence[i] = MAX_CHANNELS; // resource initialization. setup_adc_ports(AN0); setup_adc(ADC_CLOCK_INTERNAL); setup_spi(FALSE); setup_wdt(WDT_OFF); setup_timer_0(RTCC_INTERNAL); setup_timer_1(T1_DISABLED); //setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //set_timer1(16000); setup_timer_2(T2_DISABLED,0,1); setup_timer_3(T3_DISABLED|T3_DIV_BY_1); _board_ID = read_eeprom(0); can_init(); adc_init(); enable_interrupts(INT_TIMER1); enable_interrupts(GLOBAL); while(TRUE) { if (can_kbhit() || _wait) // wait for a message on the CAN bus { // handles timer message if (_wait) { _wait = 0; for (i = 0; i <= MAX_CHANNELS-3; i+=3) { if (sequence[i] < MAX_CHANNELS) tmp = read_analog(i); else tmp = 0; out_data[1] = (int8)(tmp); out_data[2] = (int8)(tmp>>8); if (sequence[i+1] < MAX_CHANNELS) tmp = read_analog(i+1); else tmp = 0; out_data[3] = (int8)(tmp); out_data[4] = (int8)(tmp>>8); if (sequence[i+2] < MAX_CHANNELS) tmp = read_analog(i+2); else tmp = 0; out_data[5] = (int8)(tmp); out_data[6] = (int8)(tmp>>8); while (!can_tbe()) ; tx_id = ID_BASE; tx_id |= ((_board_ID) << 4); //tx_id |= ((0 & 0x00f0) >> 4); out_data[0] = 0x30+i; tx_len = 7; can_putd(tx_id, out_data, tx_len, tx_pri, tx_ext, tx_rtr); } // last message. if (sequence[30] < MAX_CHANNELS) tmp = read_analog(30); else tmp = 0; out_data[1] = (int8)(tmp); out_data[2] = (int8)(tmp>>8); if (sequence[31] < MAX_CHANNELS) tmp = read_analog(31); else tmp = 0; out_data[3] = (int8)(tmp); out_data[4] = (int8)(tmp>>8); while (!can_tbe()) ; tx_id = ID_BASE; tx_id |= ((_board_ID) << 4); //tx_id |= ((0 & 0x00f0) >> 4); out_data[0] = 0x30+30; tx_len = 5; can_putd(tx_id, out_data, tx_len, tx_pri, tx_ext, tx_rtr); } if (can_getd(rx_id, &in_data[0], rx_len, rxstat)) { // handles message for the analog channel if ((rx_len == 1) && ((rx_id & 0x700) == 0x200) && (in_data[0] < MAX_CHANNELS)) { tmp = read_analog(in_data[0]); out_data[1] = (int8)(tmp); out_data[2] = (int8)(tmp>>8); while (!can_tbe()) ; tx_id = ID_BASE; tx_id |= ((_board_ID) << 4); tx_id |= ((rx_id & 0x00f0) >> 4); out_data[0] = in_data[0]; tx_len = 3; // replies to message. can_putd(tx_id, out_data, tx_len, tx_pri, tx_ext, tx_rtr); } // handles message to prepare a sequence (32). else if ((rx_len == 5) && ((rx_id & 0x700) == 0x200) && (in_data[0] == MAX_CHANNELS)) { for (i = 0; i < MAX_CHANNELS; i++) sequence[i] = MAX_CHANNELS; // perhaps this is not needed. while (!can_tbe()) ; tx_id = ID_BASE; tx_id |= ((_board_ID) << 4); tx_id |= ((rx_id & 0x00f0) >> 4); out_data[0] = in_data[0]; tx_len = 1; setup_timer_1(T1_DISABLED); // replies to message. can_putd(tx_id, out_data, tx_len, tx_pri, tx_ext, tx_rtr); } // handles message to prepare a broadcast sequence (33). else if ((rx_len == 5) && ((rx_id & 0x700) == 0x200) && (in_data[0] == MAX_CHANNELS+1)) { for (i = 0; i < MAX_CHANNELS; i++) { val = *((int32 *)(&in_data[1])); if (val & 0x00000001) { sequence[MAX_CHANNELS-1-i] = MAX_CHANNELS-1-i; } else { sequence[MAX_CHANNELS-1-i] = MAX_CHANNELS; } val >>= 1; } // perhaps this is not needed. // LATER: check the driver. while (!can_tbe()) ; tx_id = ID_BASE; tx_id |= ((_board_ID) << 4); tx_id |= ((rx_id & 0x00f0) >> 4); out_data[0] = in_data[0]; tx_len = 1; val = *((int32 *)(&in_data[1])); if (val == 0) { setup_timer_1(T1_DISABLED); } else { setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); set_timer1(65536-5000); } // replies to message. can_putd(tx_id, out_data, tx_len, tx_pri, tx_ext, tx_rtr); } // handles CAN bus messages for the downloader else if ((rx_len == 1) && (((rx_id>>8) & 0x7)==7))