/*********************************************************************//** * @brief CAN_IRQ Handler, control receive message operation * param[in] none * @return none **********************************************************************/ void CAN_IRQHandler() { uint8_t IntStatus; uint32_t data1; /* get interrupt status * Note that: Interrupt register CANICR will be reset after read. * So function "CAN_IntGetStatus" should be call only one time */ IntStatus = CAN_IntGetStatus(LPC_CAN2); //check receive interrupt if((IntStatus>>0)&0x01) { CAN_ReceiveMsg(LPC_CAN2,&RXMsg); PrintMessage(&RXMsg); CANRxCount++; //count success received message //increase data for next TX Message TXMsg.id ++; data1 = (TXMsg.dataA[0])|(((TXMsg.dataA[1]))<<8)|((TXMsg.dataA[2])<<16)|((TXMsg.dataA[3])<<24); if(data1 == 0xFFFFFFFF) data1 = 0; else data1++; *((uint8_t *) &TXMsg.dataA[0])= *((uint8_t *) &TXMsg.dataB[0])= data1 & 0x000000FF; *((uint8_t *) &TXMsg.dataA[1])= *((uint8_t *) &TXMsg.dataB[1])=(data1 & 0x0000FF00)>>8;; *((uint8_t *) &TXMsg.dataA[2])= *((uint8_t *) &TXMsg.dataB[2])=(data1 & 0x00FF0000)>>16; *((uint8_t *) &TXMsg.dataA[3])= *((uint8_t *) &TXMsg.dataB[3])=(data1 & 0xFF000000)>>24; CAN_SendMsg(LPC_CAN1, &TXMsg); } }
int platform_can_send( unsigned id, u32 canid, u8 idtype, u8 len, const u8 *data ) { CAN_MSG_Type msg_tx; LPC_CAN_TypeDef * canx; switch (id) { case 0: canx = LPC_CAN1; break; case 1: canx = LPC_CAN2; break; default: return PLATFORM_ERR; } // Wait for outgoing messages to clear while( (canx->GSR & (1<<3)) == 0 ); msg_tx.type = DATA_FRAME; msg_tx.format = idtype == ELUA_CAN_ID_EXT ? EXT_ID_FORMAT : STD_ID_FORMAT; msg_tx.id = canid; msg_tx.len = len; if (len <= 4) memcpy(msg_tx.dataA, data, len); else { memcpy(msg_tx.dataA, data, 4); memcpy(msg_tx.dataB, data+4, len-4); } CAN_SendMsg(canx, &msg_tx); return PLATFORM_OK; }
/*********************************************************************//** * @brief c_entry: Main CAN program body * @param[in] none * @return int **********************************************************************/ int c_entry(void) { /* Main Program */ PINSEL_CFG_Type PinCfg; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); print_menu(); /* Pin configuration * CAN1: select P0.0 as RD1. P0.1 as TD1 * CAN2: select P2.7 as RD2, P2.8 as RD2 */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 0; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 1; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 7; PinCfg.Portnum = 2; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 8; PINSEL_ConfigPin(&PinCfg); //Initialize CAN1 & CAN2 CAN_Init(LPC_CAN1, 125000); CAN_Init(LPC_CAN2, 125000); //Enable Interrupt CAN_IRQCmd(LPC_CAN2, CANINT_RIE, ENABLE); //Enable CAN Interrupt NVIC_EnableIRQ(CAN_IRQn); _DBG_("CAN test Bypass Mode function..."); _DBG_("Press '1' to initialize CAN message...");_DBG_(""); while(_DG !='1'); CAN_SetAFMode(LPC_CANAF,CAN_AccBP); CAN_InitMessage(); PrintMessage(&TXMsg); _DBG_("Message ID and data will be increased continuously..."); _DBG_("Press '2' to start CAN operation..."); while(_DG !='2'); /** To test Bypass Mode: we send infinite messages to CAN2 and check * receive process via COM1 */ CAN_SendMsg(LPC_CAN1, &TXMsg); while (1); }
/* send NMT telegraph */ bool CanNode::sendNMT(QString data,unsigned long canaddr, unsigned char can_idx, unsigned char can_node) { bool ok; QStringList listdata = data.split(" "); UINT8 pdata[8]; memset(pdata,0,8); int len = listdata.length(); for(int i=0; i<len; i++) pdata[i] =(UINT8) listdata[i].toUInt(&ok,16); return (CAN_SendMsg(can_idx,can_node, 0x700 + canaddr, len, pdata) > 0); }
void checkUsart(void){ u16 lengthUsart; u16 indexUsartTmp; if(usart_hasData()){ lengthUsart=usart_getDataLength();//得到此次接收到的数据长度 LED_ON_Green(); //点亮绿灯提示 printf("\r\n[$:]"); for(indexUsartTmp=0;indexUsartTmp<lengthUsart;indexUsartTmp++) printf("%02x ",USART_RX_BUF[indexUsartTmp]); printf("\t"); //从can发送出去 CAN_SendMsg( Req_29_Fun,USART_RX_BUF); usart_reset(); //重置状态标记 } }
/*********************************************************************//** * @brief c_entry: Main CAN program body * @param[in] none * @return none **********************************************************************/ void c_entry(void) { /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); print_menu(); /* Initialize CAN1 peripheral * Note: Self-test mode doesn't require pin selection */ CAN_Init(_USING_CAN_NO, 125000); //Enable self-test mode CAN_ModeConfig(_USING_CAN_NO, CAN_SELFTEST_MODE, ENABLE); //Enable Interrupt CAN_IRQCmd(_USING_CAN_NO, CANINT_RIE, ENABLE); CAN_IRQCmd(_USING_CAN_NO, CANINT_TIE1, ENABLE); //Enable CAN Interrupt NVIC_EnableIRQ(CAN_IRQn); CAN_SetAFMode(CAN_ACC_BP); CAN_InitMessage(); _DBG_("Transmitted buffer:"); PrintMessage(&TXMsg); /** To test Bypass Mode: we send infinite messages to CAN2 and check * receive process via COM1 */ CAN_SendMsg(_USING_CAN_NO, &TXMsg); #if (_USING_CAN_NO == CAN_1) LPC_CAN1->CMR |=(1<<4); //Self Reception Request #else LPC_CAN2->CMR |=(1<<4); #endif while (1); }
/*********************************************************************//** * @brief c_entry: Main CAN program body * @param[in] none * @return int **********************************************************************/ int c_entry(void) { /* Main Program */ uint32_t i; uint32_t cnt; CAN_ERROR error; PINSEL_CFG_Type PinCfg; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); print_menu(); /* Pin configuration * CAN1: select P0.0 as RD1. P0.1 as TD1 * CAN2: select P2.7 as RD2, P2.8 as RD2 */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 0; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 1; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 7; PinCfg.Portnum = 2; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 8; PINSEL_ConfigPin(&PinCfg); //Initialize CAN1 & CAN2 CAN_Init(LPC_CAN1, 125000); CAN_Init(LPC_CAN2, 125000); //Enable Receive Interrupt CAN_IRQCmd(LPC_CAN2, CANINT_FCE, ENABLE); CAN_IRQCmd(LPC_CAN2, CANINT_RIE, ENABLE); //Enable CAN Interrupt NVIC_EnableIRQ(CAN_IRQn); /* First, we send 10 messages: * - message 0,2,4,6,8 have id in AFLUT >>> will be received * - message 1,3,5,7,9 don't have id in AFLUT >>> will be ignored * Then, we change AFLUT by load/remove entries in AFLUT and re-send messages * - message 1,3,5,7,9 have id in AFLUT >>> will be received * - message 0,2,4,6,8 don't have id in AFLUT >>> will be ignored * Note that: FullCAN Object must be read from FullCAN Object Section next to AFLUT */ /*-------------------------Init Message & AF Look-up Table------------------------*/ _DBG_("Test Acceptance Filter function..."); _DBG_("Press '1' to initialize message and AF Loop-up Table...");_DBG_(""); while(_DG !='1'); CAN_InitAFMessage(); /* initialize Transmit Message */ _DBG_("Init message finished!!!"); CAN_SetupAFTable(); /* initialize AF Look-up Table sections*/ error = CAN_SetupAFLUT(LPC_CANAF,&AFTable); /* install AF Look-up Table */ if (error != CAN_OK) { _DBG_("Setup AF: ERROR..."); while (1); // AF Table has error } else _DBG_("Setup AF: SUCCESSFUL!!!");_DBG_(""); /*-------------------------Send messages------------------------*/ _DBG_("Press '2' to start CAN transferring operation...");_DBG_(""); while(_DG !='2'); for (i = 0; i < CAN_TX_MSG_CNT; i++) { CAN_SendMsg(LPC_CAN1, &AFTxMsg[i]); PrintMessage(&AFTxMsg[i]);_DBG_(""); for(cnt=0;cnt<10000;cnt++); //transmit delay CANTxCount++; } _DBG_("Sending finished !!!"); /*-------------------------Display Received messages------------------------*/ _DBG_("Press '3' to display received messages...");_DBG_(""); while(_DG !='3'); for (i = 0; i < CAN_RX_MSG_CNT; i++) { PrintMessage(&AFRxMsg[i]);_DBG_(""); } /*-------------------------Change AFLUT Table --------------------*/ _DBG_("Press '4' to change AF look-up table...");_DBG_(""); while(_DG !='4'); CAN_ChangeAFTable(); _DBG_("Change AFLUT: FINISHED!!!"); CAN_SetAFMode(LPC_CANAF, CAN_eFCAN); CAN_InitAFMessage(); CANRxCount = CANTxCount = 0; /*-------------------------Re-Send messages------------------------*/ _DBG_("Press '5' to re-send messages...");_DBG_(""); while(_DG !='5'); for (i = 0; i < CAN_TX_MSG_CNT; i++) { CAN_SendMsg(LPC_CAN1, &AFTxMsg[i]); PrintMessage(&AFTxMsg[i]);_DBG_(""); for(cnt=0;cnt<10000;cnt++); //transmit delay CANTxCount++; } /*-------------------------Display received messages------------------------*/ _DBG_("Re-Sending finished !!!"); _DBG_("Press '6' to display received messages...");_DBG_(""); while(_DG !='6'); for (i = 0; i < CAN_RX_MSG_CNT; i++) { PrintMessage(&AFRxMsg[i]);_DBG_(""); } _DBG_("Demo terminal !!!"); CAN_DeInit(LPC_CAN1); CAN_DeInit(LPC_CAN2); while (1); return 0; }
int main(void) { CanMessage tmp; /* Local Variables */ #if ( TERMINAL == 1 ) UART_Init(); /* UART */ ADC_Init(); /* ADC */ #endif GPIO_Init(); /* GPIO */ ExtINT_Init(); /* External Interrupt */ Timer_Init(); /* Timers */ SPI_Init(); /* SPI */ CanStatus res = CAN_Init(CAN_SPEED); /* Start CAN */ #if ( TERMINAL == 1 ) term_Start(res); /* Start Terminal */ #endif Msg_Init(); /* Construct Messages to be sent */ CAN_BufInit( &RxBuffer, CAN_RX_BUFFER_SIZE ); /* Initialize Receive Buffer */ CAN_BufInit( &TxBuffer, CAN_TX_BUFFER_SIZE ); /* Initialize Transmit Buffer */ sei(); /* Enable Global Interrupts */ /* ---------------------------*/ while(1){ wdt_enable(WDTO_1S); /* Enable Watchdog Timer for 2 second */ /* ------------------------------------------ */ /* Send Messages */ // Get Data ATOMIC_BLOCK( ATOMIC_FORCEON ){ if( CAN_BufState( &TxBuffer ) != BUFFER_EMPTY ){/* Check if empty */ CAN_BufDeq( &TxBuffer, &tmp ); /* Dequeue */ CAN_SendMsg( &tmp ); /* Send */ } } /* ------------------------------------------ */ /* Receive Messages */ ATOMIC_BLOCK( ATOMIC_FORCEON ){ /* Read Interrupt variables */ if( CAN_BufState( &RxBuffer ) != BUFFER_EMPTY ){/* Check if not empty */ CAN_BufDeq( &RxBuffer, &tmp ); /* Dequeue */ Msg_Chk( &tmp ); /* Check Received Message */ #if ( TERMINAL == 1 ) if( strm == MS_STREAM ) /* Enable Terminal Message Stream */ term_RxMsg( &tmp ); #endif } } /* ------------------------------------------ */ #if ( TERMINAL == 1 ) term_Main(); /* TERMINAL FOR DEBUGGING */ #endif } wdt_reset(); /* Reset Watchdog */ wdt_disable(); return 0; }
/*********************************************************************//** THE MAIN CAN BODY **********************************************************************/ int c_entry(void) { /* Main Program */ uint32_t i; uint32_t cnt; CAN_ERROR error; CAN_PinCFG_Type CAN1PinStruct, CAN2PinStruct; NVIC_DeInit(); NVIC_SCBDeInit(); /* Configure the NVIC Preemption Priority Bits: * two (2) bits of preemption priority, six (6) bits of sub-priority. * Since the Number of Bits used for Priority Levels is five (5), so the * actual bit number of sub-priority is three (3) */ NVIC_SetPriorityGrouping(0x06); // Set Vector table offset value #if (__RAM_MODE__==1) NVIC_SetVTOR(0x10000000); #else NVIC_SetVTOR(0x00000000); #endif debug_frmwrk_init(); print_menu(); /* Pin Configuration */ CAN1PinStruct.RD = CAN_RD1_P0_0; CAN1PinStruct.TD = CAN_TD1_P0_1; CAN2PinStruct.RD = CAN_RD2_P2_7; CAN2PinStruct.TD = CAN_TD2_P2_8; PINSEL_ConfigPin((PINSEL_CFG_Type *) (&can_rd1_pin[CAN1PinStruct.RD])); PINSEL_ConfigPin((PINSEL_CFG_Type *) (&can_td1_pin[CAN1PinStruct.TD])); PINSEL_ConfigPin((PINSEL_CFG_Type *) (&can_rd2_pin[CAN2PinStruct.RD])); PINSEL_ConfigPin((PINSEL_CFG_Type *) (&can_td2_pin[CAN2PinStruct.TD])); //Initialize CAN1 & CAN2 CAN_Init(LPC_CAN1, 125000); CAN_Init(LPC_CAN2, 125000); //Enable Receive Interrupt CAN_IRQCmd(LPC_CAN2, CANINT_FCE, ENABLE); CAN_IRQCmd(LPC_CAN2, CANINT_RIE, ENABLE); CAN_IRQCmd(LPC_CAN2, CANINT_DOIE, ENABLE); CAN_SetupCBS(CANINT_FCE, CAN_Callback0); CAN_SetupCBS(CANINT_RIE, CAN_Callback1); CAN_SetupCBS(CANINT_DOIE, CAN_Callback2); //Enable CAN Interrupt NVIC_EnableIRQ(CAN_IRQn); /* First, we send 10 messages: * - message 0,2,4,6,8 have id in AFLUT >>> will be received * - message 1,3,5,7,9 don't have id in AFLUT >>> will be ignored * Then, we change AFLUT by load/remove entries in AFLUT and re-send messages * - message 1,3,5,7,9 have id in AFLUT >>> will be received * - message 0,2,4,6,8 don't have id in AFLUT >>> will be ignored * Note that: FullCAN Object must be read from FullCAN Object Section next to AFLUT */ /*-------------------------Init Message & AF Look-up Table------------------------*/ _DBG_("Test Acceptance Filter function..."); _DBG_("Press '1' to initialize message and AF Loop-up Table...");_DBG_(""); while(_DG !='1'); CAN_InitAFMessage(); /* initialize Transmit Message */ _DBG_("Init message finished!!!"); CAN_SetupAFTable(); /* initialize AF Look-up Table sections*/ error = CAN_SetupAFLUT(LPC_CANAF,&AFTable); /* install AF Look-up Table */ if (error != CAN_OK) { _DBG_("Setup AF: ERROR..."); while (1); // AF Table has error } else _DBG_("Setup AF: SUCCESSFUL!!!");_DBG_(""); /*-------------------------Send messages------------------------*/ _DBG_("Press '2' to start CAN transferring operation...");_DBG_(""); while(_DG !='2'); for (i = 0; i < CAN_TX_MSG_CNT; i++) { CAN_SendMsg(LPC_CAN1, &AFTxMsg[i]); PrintMessage(&AFTxMsg[i]);_DBG_(""); for(cnt=0;cnt<10000;cnt++); //transmit delay CANTxCount++; } _DBG_("Sending finished !!!"); /*-------------------------Display Received messages------------------------*/ _DBG_("Press '3' to display received messages...");_DBG_(""); while(_DG !='3'); for (i = 0; i < CAN_RX_MSG_CNT; i++) { PrintMessage(&AFRxMsg[i]);_DBG_(""); } /*-------------------------Change AFLUT Table --------------------*/ _DBG_("Press '4' to change AF look-up table...");_DBG_(""); while(_DG !='4'); CAN_ChangeAFTable(); _DBG_("Change AFLUT: FINISHED!!!"); CAN_SetAFMode(LPC_CANAF, CAN_eFCAN); CAN_InitAFMessage(); CANRxCount = CANTxCount = 0; /*-------------------------Re-Send messages------------------------*/ _DBG_("Press '5' to re-send messages...");_DBG_(""); while(_DG !='5'); for (i = 0; i < CAN_TX_MSG_CNT; i++) { CAN_SendMsg(LPC_CAN1, &AFTxMsg[i]); PrintMessage(&AFTxMsg[i]);_DBG_(""); for(cnt=0;cnt<10000;cnt++); //transmit delay CANTxCount++; } /*-------------------------Display received messages------------------------*/ _DBG_("Re-Sending finished !!!"); _DBG_("Press '6' to display received messages...");_DBG_(""); while(_DG !='6'); for (i = 0; i < CAN_RX_MSG_CNT; i++) { PrintMessage(&AFRxMsg[i]);_DBG_(""); } _DBG_("Demo terminal !!!"); CAN_DeInit(LPC_CAN1); CAN_DeInit(LPC_CAN2); while (1); return 0; }
/*********************************************************************//** * @brief c_entry: Main CAN program body * @param[in] none * @return none **********************************************************************/ void c_entry(void) { uint32_t i; volatile uint32_t cnt; CAN_ERROR error; CANRxCount = CANTxCount = 0; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); print_menu(); /* Pin configuration * CAN1: select P0.0 as RD1. P0.1 as TD1 * CAN2: select P0.4 as RD2, P0.5 as RD2 */ PINSEL_ConfigPin (0, 0, 1); PINSEL_ConfigPin (0, 1, 1); #if (RECVD_CAN_NO != CAN_1) PINSEL_ConfigPin (0, 4, 2); PINSEL_ConfigPin (0, 5, 2); #endif //Initialize CAN1 & CAN2 CAN_Init(CAN_1, 125000); #if (RECVD_CAN_NO != CAN_1) CAN_Init(RECVD_CAN_NO, 125000); #endif //Enable Receive Interrupt CAN_IRQCmd(RECVD_CAN_NO, CANINT_FCE, ENABLE); CAN_IRQCmd(RECVD_CAN_NO, CANINT_RIE, ENABLE); //Enable CAN Interrupt NVIC_EnableIRQ(CAN_IRQn); /* First, we send 10 messages: * - message 0,2,4,6,8 have id in AFLUT >>> will be received * - message 1,3,5,7,9 don't have id in AFLUT >>> will be ignored * Then, we change AFLUT by load/remove entries in AFLUT and re-send messages * - message 1,3,5,7,9 have id in AFLUT >>> will be received * - message 0,2,4,6,8 don't have id in AFLUT >>> will be ignored * Note that: FullCAN Object must be read from FullCAN Object Section next to AFLUT */ /*-------------------------Init Message & AF Look-up Table------------------------*/ _DBG_("Test Acceptance Filter function..."); _DBG_("Press '1' to initialize message and AF Loop-up Table...");_DBG_(""); while(_DG !='1'); /* initialize Transmit Message */ CAN_InitAFMessage(); _DBG_("Init message finished!!!"); /* initialize AF Look-up Table sections*/ CAN_SetupAFTable(); /* install AF Look-up Table */ error = CAN_SetupAFLUT(&AFTable); if (error != CAN_OK) { _DBG_("Setup AF: ERROR..."); while (1); // AF Table has error } else { _DBG_("Setup AF: SUCCESSFUL!!!");_DBG_(""); } /*-------------------------Send messages------------------------*/ _DBG_("Press '2' to start CAN transferring operation...");_DBG_(""); while(_DG !='2'); for (i = 0; i < CAN_TX_MSG_CNT; i++) { CAN_SendMsg(CAN_1, (CAN_MSG_Type *)&AFTxMsg[i]); PrintMessage((CAN_MSG_Type *)&AFTxMsg[i]);_DBG_(""); for(cnt=0;cnt<10000;cnt++); //transmit delay CANTxCount++; } _DBG_("Sending finished !!!");_DBG_(""); if(CANRxCount == 0) { _DBG_(">>> No message is recieved. Please check the connection between 2 CANs!!!");_DBG_(""); } /*-------------------------Display Received messages------------------------*/ _DBG_("Press '3' to display received messages...");_DBG_(""); while(_DG !='3'); for (i = 0; i < CAN_RX_MSG_CNT; i++) { PrintMessage((CAN_MSG_Type *)&AFRxMsg[i]);_DBG_(""); } /*-------------------------Change AFLUT Table --------------------*/ _DBG_("Press '4' to change AF look-up table...");_DBG_(""); while(_DG !='4'); CAN_ChangeAFTable(); _DBG_("Change AFLUT: FINISHED!!!"); //CAN_SetAFMode(LPC_CANAF, CAN_eFCAN); CAN_SetAFMode(CAN_EFCAN); CAN_InitAFMessage(); CANRxCount = CANTxCount = 0; /*-------------------------Re-Send messages------------------------*/ _DBG_("Press '5' to re-send messages...");_DBG_(""); while(_DG !='5'); for (i = 0; i < CAN_TX_MSG_CNT; i++) { CAN_SendMsg(CAN_1, (CAN_MSG_Type *)&AFTxMsg[i]); PrintMessage((CAN_MSG_Type *)&AFTxMsg[i]);_DBG_(""); for(cnt=0;cnt<10000;cnt++); //transmit delay CANTxCount++; } /*-------------------------Display received messages------------------------*/ _DBG_("Re-Sending finished !!!");_DBG_(""); if(CANRxCount == 0) { _DBG_(">>> No message is recieved. Please check the connection between 2 CANs!!!");_DBG_(""); } _DBG_("Press '6' to display received messages...");_DBG_(""); while(_DG !='6'); for (i = 0; i < CAN_RX_MSG_CNT; i++) { PrintMessage((CAN_MSG_Type *)&AFRxMsg[i]);_DBG_(""); } _DBG_("Demo terminal !!!"); CAN_DeInit(CAN_1); #if (RECVD_CAN_NO != CAN_1) CAN_DeInit(CAN_2); #endif while (1); }
/******************************************************************************* * Function Name : SysTick_Handler * Description : This function handles SysTick Handler. * Input : None * Output : None * Return : None *******************************************************************************/ void SysTick_Handler(void) { if(bDeviceState==CONFIGURED){ /******************************************************************************** ********************* Message received from CAN BUS (ROV) *********************** *********************************************************************************/ if(CANpacket_receive==1 && Bridge.CAN_State != CAN_MSGINQUEUE && Bridge.CAN_State != CAN_PROC_MSGINQUEUE) { if(CAN_SaveMsg(&Bridge.RxMessage1,&Bridge.COM_Lastmessage.CDC_ToPC_Buffer)) { Bridge.CAN_State=ChangeState_level_Busier(Bridge.CAN_State); STM_EVAL_LEDToggle(LED9); } else { Bridge.CAN_State=ChangeState_level_Freer(Bridge.CAN_State); STM_EVAL_LEDToggle(LED10); } /*Send CAN message if queue isn't empty */ if (Bridge.CAN_State == CAN_MSGINQUEUE || Bridge.CAN_State == CAN_PROC_MSGINQUEUE) { COM_SendMsg(&Bridge.COM_Lastmessage.CDC_ToPC_Buffer); Bridge.COM_Acknowledgement.State=ACK_WAITING; Bridge.COM_Acknowledgement.COM_ACK=NO_ACK; } CANpacket_receive=0; } /******************************************************************************** ********************* Message received from USB port (pc) *********************** *********************************************************************************/ if( Bridge.COM_Status==COM_ReadyToProcess) { if(Bridge.COM_State == COM_PROCESSING) { COM_SaveMsg(&Bridge.Reception_Buffer,&Bridge.COM_Lastmessage.CDC_FromPC_Buffer); Bridge.COM_State=ChangeState_level_Busier(Bridge.COM_State); Bridge.COM_Status=COM_WaitingforHeader; } /*Send serial message if queue isn't empty */ if ((Bridge.COM_State == COM_MSGINQUEUE || Bridge.COM_State == COM_PROC_MSGINQUEUE) ) { CANTX_Counter=0; // make the function to send CAN messages through serial CAN_SendMsg(&Bridge.COM_Lastmessage.CDC_FromPC_Buffer); CAN_mbox=CAN_Transmit(CAN1,&Bridge.TxMessage1); while(((Bridge.CAN_Err.ErrorData=CAN_TransmitStatus(CAN1, CAN_mbox)) != CAN_TxStatus_Ok) && (CANTX_Counter != 0xFFFF)) { CANTX_Counter++; } if(CANTX_Counter!=0xFFFF) { if (Bridge.COM_State == COM_PROC_MSGINQUEUE) COM_SaveMsg(&Bridge.Reception_Buffer,&Bridge.COM_Lastmessage.CDC_FromPC_Buffer); Bridge.COM_State=ChangeState_level_Freer(Bridge.COM_State); } else Bridge.CAN_Err.ErrorId=CAN_SENDERROR; } } /******************************************************************************** ********************* Error Message on the USB port (pc) *********************** *********************************************************************************/ if (Bridge.COM_Err.ErrorId) { STM_EVAL_LEDToggle(LED5); //COM_SendSpecial(Bridge.COM_Err.ErrorId); to be replaced by a table to send Bridge.COM_Err.ErrorId=0; } /******************************************************************************** ********************* Error Message (ROV) *********************** *********************************************************************************/ if (Bridge.CAN_Err.ErrorId) { STM_EVAL_LEDToggle(LED8); //COM_SendSpecial(Bridge.CAN_Err.ErrorId); Bridge.CAN_Err.ErrorId=0; } /******************************************************************************** ********************* Acknowledgement Message (PC) *********************** *********************************************************************************/ if (Bridge.COM_Acknowledgement.State==ACK_RECEIVED) { if(Bridge.COM_Acknowledgement.COM_ACK==ACK) { Bridge.COM_Acknowledgement.State=ACK_IDLE; Bridge.CAN_State=ChangeState_level_Freer(Bridge.CAN_State); } else { Bridge.COM_Acknowledgement.Nb_Tries++; COM_SendMsg(&Bridge.COM_Lastmessage.CDC_ToPC_Buffer); Bridge.COM_Acknowledgement.State=ACK_WAITING; } } else if(Bridge.COM_Acknowledgement.State==ACK_WAITING) { Bridge.COM_Acknowledgement.Nb_Check++; STM_EVAL_LEDToggle(LED6); COM_SendMsg(&Bridge.COM_Lastmessage.CDC_ToPC_Buffer); } } STM_EVAL_LEDOff(LED7); }
/* send NMT telegraph */ bool CanNode::sendState(unsigned char cs, unsigned char COB_ID,unsigned char can_idx, unsigned char can_node) { UINT8 pdata[2] = {cs,COB_ID}; return (CAN_SendMsg(can_idx,can_node,0x00,2,pdata) > 0); }