2020-02-05 10:45 AM
Hello everyone,
I need assistance or any possible solutions in tackling this problem. I have a Nucleo F413zh, connected to three transceivers. They all work (yay!), however one of them doesn't receive messages properly but transmits properly! All of them have no specific can filter configurations, but interrupts enabled RX0s...
CAN 1 -Master
CAN 2 -Slave
CAN 3 -Master
I've changed the preemption priority, and it still doesn't do the trick. CAN1, CAN2 work perfectly fine in both receiving and transmitting. CAN3 can only send but not receive. I will post my code configurations below:
I think its something to do with the interrupts or how the priority works between these CANs, I want CAN3 to specifically take in different messages , hence I will implement a can filter configuration but I have to solve this issue in why it's not registering any messages through CAN3 and triggering RxFifo.
Any help would be GREATLY appreciated! :D :loudly_crying_face:
//Parameters~
CAN_HandleTypeDef hcan1;
CAN_HandleTypeDef hcan2;
CAN_HandleTypeDef hcan3;
/* USER CODE BEGIN PV */
CAN_TxHeaderTypeDef txheader1;
CAN_TxHeaderTypeDef txheader2;
CAN_TxHeaderTypeDef txheader3;
CAN_RxHeaderTypeDef rxheader1;
CAN_RxHeaderTypeDef rxheader2;
CAN_RxHeaderTypeDef rxheader3;
//CODE FUNCTIONS
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
//Message interrupt indicator
HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rxheader2, data2);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);/* Blue LED Blinks when MSG received */
//Do stuff...~~~
}
static void MX_CAN1_Init(void)
{
/* USER CODE BEGIN CAN1_Init 0 */
CAN_FilterTypeDef filter;
/* USER CODE END CAN1_Init 0 */
/* USER CODE BEGIN CAN1_Init 1 */
/* USER CODE END CAN1_Init 1 */
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 10;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_3TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = ENABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = DISABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN1_Init 2 */
filter.FilterIdHigh=0x0000;
filter.FilterIdLow=0x0000;
filter.FilterMaskIdHigh=0x0000;
filter.FilterMaskIdLow=0x0000;
filter.FilterFIFOAssignment=CAN_FilterFIFO0;
filter.FilterBank=0;
filter.FilterMode=CAN_FILTERMODE_IDMASK;
filter.FilterScale=CAN_FILTERSCALE_32BIT;
filter.FilterActivation=ENABLE;
filter.SlaveStartFilterBank=14;
if (HAL_CAN_ConfigFilter(&hcan1,&filter) !=HAL_OK)
{
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();
}
/* USER CODE END CAN1_Init 2 */
}
/**
* @brief CAN2 Initialization Function
* @param None
* @retval None
*/
static void MX_CAN2_Init(void)
{
/* USER CODE BEGIN CAN2_Init 0 */
CAN_FilterTypeDef filter;
/* USER CODE END CAN2_Init 0 */
/* USER CODE BEGIN CAN2_Init 1 */
/* USER CODE END CAN2_Init 1 */
hcan2.Instance = CAN2;
hcan2.Init.Prescaler = 10;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan2.Init.TimeSeg1 = CAN_BS1_3TQ;
hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan2.Init.TimeTriggeredMode = DISABLE;
hcan2.Init.AutoBusOff = DISABLE;
hcan2.Init.AutoWakeUp = DISABLE;
hcan2.Init.AutoRetransmission = DISABLE;
hcan2.Init.ReceiveFifoLocked = DISABLE;
hcan2.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN2_Init 2 */
filter.FilterIdHigh=0x0000;
filter.FilterIdLow=0x0000;
filter.FilterMaskIdHigh=0x0000;
filter.FilterMaskIdLow=0x0000;
filter.FilterFIFOAssignment=CAN_FilterFIFO0;
filter.FilterBank=14;
filter.FilterMode=CAN_FILTERMODE_IDMASK;
filter.FilterScale=CAN_FILTERSCALE_32BIT;
filter.FilterActivation=ENABLE;
filter.SlaveStartFilterBank=14;
if (HAL_CAN_ConfigFilter(&hcan2,&filter) !=HAL_OK)
{
Error_Handler();
}
if (HAL_CAN_Start(&hcan2) !=HAL_OK)
{
Error_Handler();
}
if (HAL_CAN_ActivateNotification(&hcan2,CAN_IT_RX_FIFO0_MSG_PENDING) !=HAL_OK)
{
Error_Handler();
}
/* USER CODE END CAN2_Init 2 */
}
/**
* @brief CAN3 Initialization Function
* @param None
* @retval None
*/
static void MX_CAN3_Init(void)
{
/* USER CODE BEGIN CAN3_Init 0 */
/* USER CODE END CAN3_Init 0 */
/* USER CODE BEGIN CAN3_Init 1 */
/* USER CODE END CAN3_Init 1 */
hcan3.Instance = CAN3;
hcan3.Init.Prescaler = 10;
hcan3.Init.Mode = CAN_MODE_NORMAL;
hcan3.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan3.Init.TimeSeg1 = CAN_BS1_3TQ;
hcan3.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan3.Init.TimeTriggeredMode = DISABLE;
hcan3.Init.AutoBusOff = DISABLE;
hcan3.Init.AutoWakeUp = DISABLE;
hcan3.Init.AutoRetransmission = DISABLE;
hcan3.Init.ReceiveFifoLocked = DISABLE;
hcan3.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN3_Init 2 */
// Activate CAN Interrupts, This fixed CAN3 in sending commands
HAL_CAN_ActivateNotification(&hcan3, CAN_IT_TX_MAILBOX_EMPTY);
HAL_CAN_ActivateNotification(&hcan3, CAN_IT_RX_FIFO0_MSG_PENDING);
HAL_CAN_ActivateNotification(&hcan3, CAN_IT_RX_FIFO0_FULL);
if (HAL_CAN_Start (&hcan3) != HAL_OK) {
// start error
Error_Handler();
}
if (HAL_CAN_ActivateNotification(&hcan3,CAN_IT_RX_FIFO0_MSG_PENDING) !=HAL_OK){
Error_Handler();
}
/* USER CODE END CAN3_Init 2 */
}
2020-02-05 11:01 AM
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 100;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
}