cancel
Showing results for 
Search instead for 
Did you mean: 

CAN and USB on STM32767ZiT6

WIF
Associate II

Hello,

I have a custom board based on STM32767ZIT6 and it has the three CAN peripherals and the USB designed. The goal is to translate the CAN message to USB directly without the need for a bridge but I am facing issues activating the USB and the three CAN peripherals in the software. I am using basic CDC and when I connect the CAN port to a CAN network using the CANDoISO device, the USB disappear from the device manager and the GUI application.
Is it possible to use all three CAN peripherals with USB enabled or  have I messed up the configurations such as interrupt priorities and timing or will an RTOS help here?

1 ACCEPTED SOLUTION

Accepted Solutions

These are bad config for CAN bitrate:

hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;

Need to increase BS1 and BS2 and decrease the prescaler as much as possible in such way BS1 =~(75% to 85%) of (BS1+BS2).

Example with bitrate @625kb/s with a sampling point at 80%: prescaler =3, BS1=12, BS2 = 3. 

SofLit_0-1712918104521.png

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

11 REPLIES 11
SofLit
ST Employee

Hello, 

What do you mean by « connect the CAN port to a CAN network using a CANDoISO device?

please provide more details provide a schema.

Which CAN instance do you connect makes the behavior occur?

Check if you have some IO conflicts?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
WIF
Associate II

I was seeing the behavior with CAN1 instance. Today I don't see the behavior but still have not USB and CAN communication.

I am trying to get data from the CAN port and store it in a Tx FIFO that USB service reads from periodically to transfer data to the computer. Also I want to store the data received from USB in the Rx FIFO and the data CAN peripherals can read from periodically.

WIF_1-1712835996221.png

 

SofLit
ST Employee

when I connect the CAN port to a CAN network using the CANDoISO device, the USB disappear from the device manager and the GUI application.

Are you telling that this is a runtime issue? i.e. The USB is detected by your Host but when you connect to the CAN network the USB device is no more detected. By the way which CAN instance is causing that behavior?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
WIF
Associate II

Yes, the USB is detected by the host and I can do basic echo app. I was using CAN1 instance. Maybe it was my wiring and cables yesterday because today I don't observe the issue.

I wish to ask how I can simulate the triggering of CAN_IT_RX_FIFO0_MSG_PENDING interrupt that will call the HAL_CAN_RxFifo0MsgPendingCallback ISR. I have added the code to save the data gotten to from the CAN Rx FiFo to the USB Tx fifo but can't really test it without proper CAN device to connect to the peripheral


I wish to ask how I can simulate the triggering of CAN_IT_RX_FIFO0_MSG_PENDING interrupt that will call the HAL_CAN_RxFifo0MsgPendingCallback ISR. I have added the code to save the data gotten to from the CAN Rx FiFo to the USB Tx fifo but can't really test it without proper CAN device to connect to the peripheral


Here, I can suggest to use CAN in combined mode so the CAN cell is totally disconnected from outside the chip: no HW dependency.

SofLit_0-1712839504594.png

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
WIF
Associate II

I have activated the combine mode

WIF_0-1712860164806.png

But I get the Bit dorminate error when In try to transmit and no Rxfifo pending message callback, I guess beacause

CANTX is being held receive in this mode. I still don't understand why I don't get the CAN RxFiFo interrupts

WIF_1-1712860245597.png

 

Hello,

Could you please provide your CAN config? if you are using CubeMx could you please share your ioc file?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
WIF
Associate II

Hello, the code for initializing the three CANs. Attached is the ioc file

void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{

GPIO_InitTypeDef GPIO_InitStruct = {0};
if(canHandle->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspInit 0 */

/* USER CODE END CAN1_MspInit 0 */
/* CAN1 clock enable */
HAL_RCC_CAN1_CLK_ENABLED++;
if(HAL_RCC_CAN1_CLK_ENABLED==1){
__HAL_RCC_CAN1_CLK_ENABLE();
}

__HAL_RCC_GPIOB_CLK_ENABLE();
/**CAN1 GPIO Configuration
PB8 ------> CAN1_RX
PB9 ------> CAN1_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* CAN1 interrupt Init */
HAL_NVIC_SetPriority(CAN1_TX_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);

/* USER CODE BEGIN CAN1_MspInit 1 */

/* USER CODE END CAN1_MspInit 1 */
}
else if(canHandle->Instance==CAN2)
{
/* USER CODE BEGIN CAN2_MspInit 0 */

/* USER CODE END CAN2_MspInit 0 */
/* CAN2 clock enable */
HAL_RCC_CAN3_CLK_ENABLED++;
if(HAL_RCC_CAN3_CLK_ENABLED==1){
__HAL_RCC_CAN3_CLK_ENABLE();
}
HAL_RCC_CAN2_CLK_ENABLED++;
if(HAL_RCC_CAN2_CLK_ENABLED==1){
__HAL_RCC_CAN2_CLK_ENABLE();
}
HAL_RCC_CAN1_CLK_ENABLED++;
if(HAL_RCC_CAN1_CLK_ENABLED==1){
__HAL_RCC_CAN1_CLK_ENABLE();
}

__HAL_RCC_GPIOB_CLK_ENABLE();
/**CAN2 GPIO Configuration
PB5 ------> CAN2_RX
PB6 ------> CAN2_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* CAN2 interrupt Init */
HAL_NVIC_SetPriority(CAN2_TX_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(CAN2_TX_IRQn);
HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
/* USER CODE BEGIN CAN2_MspInit 1 */

/* USER CODE END CAN2_MspInit 1 */
}
else if(canHandle->Instance==CAN3)
{
/* USER CODE BEGIN CAN3_MspInit 0 */

/* USER CODE END CAN3_MspInit 0 */
/* CAN3 clock enable */
HAL_RCC_CAN3_CLK_ENABLED++;
if(HAL_RCC_CAN3_CLK_ENABLED==1){
__HAL_RCC_CAN3_CLK_ENABLE();
}
HAL_RCC_CAN2_CLK_ENABLED++;
if(HAL_RCC_CAN2_CLK_ENABLED==1){
__HAL_RCC_CAN2_CLK_ENABLE();
}
HAL_RCC_CAN1_CLK_ENABLED++;
if(HAL_RCC_CAN1_CLK_ENABLED==1){
__HAL_RCC_CAN1_CLK_ENABLE();
}

__HAL_RCC_GPIOB_CLK_ENABLE();
/**CAN3 GPIO Configuration
PB3 ------> CAN3_RX
PB4 ------> CAN3_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF11_CAN3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* CAN3 interrupt Init */
HAL_NVIC_SetPriority(CAN3_TX_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(CAN3_TX_IRQn);
HAL_NVIC_SetPriority(CAN3_RX0_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(CAN3_RX0_IRQn);
/* USER CODE BEGIN CAN3_MspInit 1 */

/* USER CODE END CAN3_MspInit 1 */
}
}

 

//The other CANs follow the same initialization

/* CAN1 init function */
void MX_CAN1_Init(void)
{
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 16;
hcan1.Init.Mode = CAN_MODE_SILENT_LOOPBACK;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = DISABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = DISABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = ENABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}

}

 

void CAN1_Config_Filters(void) {
CAN_FilterTypeDef sFilterConfig;
hcan1.Instance = CAN1;

//##-2- Configure the CAN Filter ###########################################
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = 0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;

if(HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK) {
// Filter configuration Error
Error_Handler();
}
if(HAL_CAN_Start(&hcan1) != HAL_OK){
Error_Handler();
}
if(HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK){
Error_Handler();
}

}

These are bad config for CAN bitrate:

hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;

Need to increase BS1 and BS2 and decrease the prescaler as much as possible in such way BS1 =~(75% to 85%) of (BS1+BS2).

Example with bitrate @625kb/s with a sampling point at 80%: prescaler =3, BS1=12, BS2 = 3. 

SofLit_0-1712918104521.png

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.