2012-12-17 07:12 AM
hi all,
I have bought a STM32F105R8 demo board, the board can communicate USART1 to CAN1. now I want to try the USBD and CAN1 in FS mode, so I downloaded the “STM32_USB-Host-Device_Lib_V2.1.0� from web, and modify the VCP program. I replaced the COM1 code with CAN1 in the ''USBD_CDC_VCP.C'', it show a COM3 on PC host when the stm32f105 board's usb port plug in. but it has a problem when the VCP ''com3'' send data to CAN1: step 1. CAN1 ---> COM3 , it is ok, the COM3 can continuty received datas from CAN1 step 2. COM3 ---> CAN1 , problem, the CAN1 only received one frame (11 bytes), then it looks like USBD stop. step 3. if I repleat the ''step 1'' twice or more, the COM3 will lost the first frame, the following data can be received. step 4. if I done the ''step1'', and then do ''step2'', the CAN1 also received one and couldn't get the following data. the functions ''VCP_DataRx'' is send USB data to CAN1, the ''VCP_DataTx'' is send CAN1 data to USB host in the ''USBD_CDC_VCP.c''. From the above steps, I didn't know why the ''VCP_DataRx'' only be called one time? the RX will be restored when ''VCP_DataTx'' be called. hope you can tell me how to set the config!2012-12-18 03:33 AM
Hi Evan,
Please chech that you don't have a CAN overrrun error flag set after the call for the VCP_DataRx() API. with regards mozra2012-12-18 04:31 AM
Hi Mozra,
thanks, I think it is no matter the CAN setting, because I set the VCP_DataRx() empty, only return the USBD_OK, if I still send data from COM3 to CAN, the VCP_DataRx will not be effected by CAN. but it happend same things when It called CAN --->COM3, that the COM3 will also lost the first frame data. if the COM3 do not send data (not call VCP_DataRx), the CAN--->COM3 will not lost any frame data. below is my functions: static uint16_t VCP_DataTx (uint8_t* Buf, uint32_t Len) { uint32_t i; for(i=0;i<Len;i++) { APP_Rx_Buffer[APP_Rx_ptr_in]=Buf[i]; APP_Rx_ptr_in++; /* To avoid buffer overflow */ if(APP_Rx_ptr_in == APP_RX_DATA_SIZE) { APP_Rx_ptr_in = 0; } } return USBD_OK; }static uint16_t VCP_DataRx (uint8_t* buf, uint32_t Len)
{CanTxMsg TxMessage;
u32 i = 0; u8 TransmitMailbox = 0;//STX; LEN, HEAD; ID1,ID2; DATA1....DATA8
//02; 0B; 08; 1E C0; 88 43 01 E6 DB 61 39 20 TxMessage.StdId=buf[3]<<8|buf[4]; TxMessage.IDE=CAN_ID_STD; TxMessage.RTR=CAN_RTR_DATA; TxMessage.DLC= buf[2]; for(i=0;i<buf[2];i++) TxMessage.Data[i] = buf[i+5]; TransmitMailbox = CAN_Transmit(CAN1, &TxMessage); i = 0; while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFF)) i++;return USBD_OK;
}2012-12-18 04:41 AM
void CAN_Configuration(void)
{ CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; /* CAN register init */ CAN_DeInit(CAN1); CAN_StructInit(&CAN_InitStructure);/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE; CAN_InitStructure.CAN_ABOM=DISABLE; CAN_InitStructure.CAN_AWUM=DISABLE; CAN_InitStructure.CAN_NART=DISABLE; CAN_InitStructure.CAN_RFLM=DISABLE; CAN_InitStructure.CAN_TXFP=DISABLE; CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack; CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; CAN_InitStructure.CAN_BS1=CAN_BS1_8tq; CAN_InitStructure.CAN_BS2=CAN_BS2_7tq; CAN_InitStructure.CAN_Prescaler = 18; CAN_Init(CAN1, &CAN_InitStructure);/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=0; CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000; CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0; ; CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_FilterInit(&CAN_FilterInitStructure);/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); }Hi Mozra,
thanks, I think it is no matter the CAN setting, because I set the VCP_DataRx() empty, only return the USBD_OK, if I still send data from COM3 to CAN, the VCP_DataRx will not be effected by CAN. but it happend same things when It called CAN --->COM3, that the COM3 will also lost the first frame data. if the COM3 do not send data (not call VCP_DataRx), the CAN--->COM3 will not lost any frame data. below is my functions: static uint16_t VCP_DataTx (uint8_t* Buf, uint32_t Len) { uint32_t i; for(i=0;i<Len;i++) { APP_Rx_Buffer[APP_Rx_ptr_in]=Buf[i]; APP_Rx_ptr_in++; /* To avoid buffer overflow */ if(APP_Rx_ptr_in == APP_RX_DATA_SIZE) { APP_Rx_ptr_in = 0; } } return USBD_OK; }static uint16_t VCP_DataRx (uint8_t* buf, uint32_t Len)
{CanTxMsg TxMessage;
u32 i = 0; u8 TransmitMailbox = 0;//STX; LEN, HEAD; ID1,ID2; DATA1....DATA8
//02; 0B; 08; 1E C0; 88 43 01 E6 DB 61 39 20 TxMessage.StdId=buf[3]<<8|buf[4]; TxMessage.IDE=CAN_ID_STD; TxMessage.RTR=CAN_RTR_DATA; TxMessage.DLC= buf[2]; for(i=0;i<buf[2];i++) TxMessage.Data[i] = buf[i+5]; TransmitMailbox = CAN_Transmit(CAN1, &TxMessage); i = 0; while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFF)) i++;return USBD_OK;
}