void listenCAN() // Fonction assurant la réception des messages sur le CAN et l'envoi de ces derniers sur le RS232 { struct rx_stat rxStat; int32 rxId; int8 rxData[8]; int8 rxLen; int i; 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)) // on récupère le message { putc(255); // on envoie le message de début de trame putc(rxId); // on envoie l'ID for(i=0;i<rxLen;i++) { putc(rxData[i]); // on envoie les données si besoin } #ifdef TRACE_CAN tmp=ms+1000*sec; printf("\r\n [%Lu] - CAN_DEBUG - BUFF=%u - ID=%u - LEN=%u - OVF=%u", tmp,rxStat.buffer, rxId, rxLen, rxStat.err_ovfl); #endif } else { #ifdef TRACE_CAN restart_wdt(); tmp=ms+1000*sec; printf("[%Lu] - CAN_DEBUG - FAIL on can_getd function", tmp); #endif } } }
void manageCAN() //cette carte ne fait qu'écouter le CAN, elle n'a aucune donnée à envoyer. { 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 == FEUX_ARR_DATA_ID) // on veut modifier les feux arrière ? Utiliser rxData pour activer ou désactiver les feux. { feuxstop = rxData[0]; feuxarr = rxData[1]; cligng = rxData[2]; clignd = rxData[3]; } } } }
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))
void main() { unsigned int8 i=0; int r=0; struct rx_stat rxStat; int32 rxId; int8 rxData[8]; int8 rxLen; int16 * toto=0; int8 txdata[8]; int16 umot1=1; int16 umot2=2; int16 usc=3; int16 uconv=4; // int pongflagemit=0; // int pong=0; //initialisation des interruptions enable_interrupts(INT_TIMER2); //configuration des interruptions enable_interrupts(GLOBAL); setup_timer_2(T2_DIV_BY_4,250,5); //setup up timer2 to interrupt every 1ms //initialisation du CAN can_init(); //initialise le CAN txdata[0]=umot1; txdata[2]=umot2; txdata[4]=usc; txdata[6]=uconv; // BOUCLE DE SAISIE while(true) { // printf("\r\n Tick : %Lu ms \r\n \r\n",ms); i++; // Envoie sur le CAN if(can_tbe()) // On vérifie que le buffer d'emission est libre // { // printf("CAN TX %u \r\n",i); // printf("uconv : %Ld \r\n",uconv); /* TEST DEBIT FEUX ARRIERES */ // r=can_putd(6,input(PIN_A0),1,0,false,false); //emission du message de 8octets // if (r != 0xFF) // { // printf("CAN_TX - %u - ID=%u - LEN=1", r, PIC_TO_MAB); // } // else // { // printf("CAN_ERROR - FAIL on can_putd function \r\n"); // } // } // else // { // printf("Buffer d'emission plein \r\n"); // } // On écoute le can et on retransmet sur le rs232 if(can_kbhit()) { if(can_getd(rxId,&rxData[0],rxLen,rxStat)) // on récupère le message { if(rxId==20) { toto=rxData[0]; printf("[%Lu] - RX ID %Lu - UMOT1 - Value = %Lu \r\n",ms,rxId,*toto); toto=rxData[2]; printf("RX ID %Lu - UMOT2 - Value = %Lu \r\n",rxId,*toto); toto=rxData[4]; printf("RX ID %Lu - USC - Value = %Lu \r\n",rxId,*toto); toto=rxData[6]; printf("RX ID %Lu - UCONV - Value = %Lu \r\n",rxId,*toto); // printf("RX ID %Lu - Len %d- Value = %Ld \r\n",rxId,rxLen,*toto); // printf("%Ld \r\n",*toto); // LOG_TESTING_D(TRACE_ALL||TRACE_BRAKE_LIGHT,"Brake light status incomming from the CAN is ",rxData[0],ms*1000,ms) } } } else { // printf("\r\n Nothing hit the can bus"); } } }