cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F423 bxCAN + Arduino MCP2515 — CAN reception issue

nikhilkrn
Associate II

Hardware connection

  • Node A: STM32F423RHTx, TCAN1462-Q1 transceiver, HSI→PLL→80MHz SYSCLK, 40MHz APB1, PA11/PA12
  • Node B: Arduino Uno + MCP2515 breakout (8MHz xtal, onboard transceiver), mcp_can v1.5. 120Ω termination, wiring verified 
  • CAN Timing (both at 500kbps)

CAN Configuration

CAN_HandleTypeDef hcan1;

/* CAN1 init function */
void MX_CAN1_Init(void)
{

  /* USER CODE BEGIN CAN1_Init 0 */

  /* USER CODE END CAN1_Init 0 */

  /* USER CODE BEGIN CAN1_Init 1 */

  /* USER CODE END CAN1_Init 1 */
  hcan1.Instance = CAN1;
  hcan1.Init.Prescaler = 8;
  hcan1.Init.Mode = CAN_MODE_NORMAL;
  hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan1.Init.TimeSeg1 = CAN_BS1_6TQ;
  hcan1.Init.TimeSeg2 = CAN_BS2_3TQ;
  hcan1.Init.TimeTriggeredMode = DISABLE;
  hcan1.Init.AutoBusOff = DISABLE;
  hcan1.Init.AutoWakeUp = ENABLE;
  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 */
  CAN_FilterTypeDef canfilterconfig;

  canfilterconfig.FilterBank = 0;
  canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;
  canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;
  canfilterconfig.FilterIdHigh = 0x0000;
  canfilterconfig.FilterIdLow = 0x0000;
  canfilterconfig.FilterMaskIdHigh = 0x0000;
  canfilterconfig.FilterMaskIdLow = 0x0000;
  canfilterconfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;
  canfilterconfig.SlaveStartFilterBank = 14;

  if (HAL_CAN_ConfigFilter(&hcan1, &canfilterconfig) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE END CAN1_Init 2 */

}

Notification is also enabled. 

  if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
  {
      Error_Handler();
  }

Callback function

CAN_RxHeaderTypeDef gRxHeader;
uint8_t             gRxData[8];

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	printf("Hi from CB\r\n");
        if (HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &gRxHeader, gRxData) == HAL_OK){
                printf("ID: 0x%03lX  DLC: %lu  Data:", gRxHeader.StdId, gRxHeader.DLC);
                for (uint8_t i = 0; i < gRxHeader.DLC; i++){
                        printf(" %02X", gRxData[i]);
                }
                printf("\r\n");
        }
}

What Works

  • STM32 → Arduino: Arduino serial monitor shows received frames every 1 second
  • STM32 Loopback mode: receives own frames internally
  • STM32 register dump: IER=0x02 (FMPIE0 armed), MSR shows normal mode, filter active

What Doesn't Work

  • Arduino → STM32: RF0R.FMP permanently 0, REC=0, interrupt never fires
  • Arduino sendMsgBuf returns CAN_SENDMSGTIMEOUT (7), EFLG=0x15 (TXEP+TXWAR+EWARN)
  • STM32's own TX frames show ACK Error (LEC=0x3) and TERR0=1 despite Arduino claiming to receive them — ACK contradiction

I have tried changing the filter configuration still that didn't worked.

13 REPLIES 13

Hi, 
I'm from nikhil's team,
I'll share a final tested project file here. Can you check once, let us know!

@Muruganandam 

Thank you for the sharing.

I meant to use this config:

For the clock (if the crystal is at 25MHz). Please ensure you have 25MHz.

mALLEm_0-1780491115088.png

and CAN bit timing:

mALLEm_2-1780491184817.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.

Hey ,
Even with the config you sent, the same issue persist.
For you reference i have also attached log for the MSR and MCR register data.

CAN Transm starts fails right after the 3rd transmission everytime as well. and timeout happens when waiting for the HAL_CAN_GetTxMailboxesFreeLevel().

18:43:07:287 -> MCR: 0x00010021 MSR: 0x00000C09
18:43:07:291 -> CAN driver init done
18:43:07:295 -> Start: 0 MSR: 0x00000C08 Err: 0x00000000 CAN State: 2
18:43:07:298 -> CAN_SENT
18:43:08:456 -> CAN_SENT
18:43:09:458 -> CAN_SENT
18:43:11:459 -> Timeout
18:43:11:467 -> CAN_FAIL
18:43:13:462 -> Timeout
18:43:13:463 -> CAN_FAIL
18:43:15:465 -> Timeout
18:43:15:466 -> CAN_FAIL
18:43:17:467 -> Timeout
18:43:17:469 -> CAN_FAIL
18:43:19:495 -> Timeout
18:43:19:496 -> CAN_FAIL
18:43:21:477 -> Timeout
18:43:21:479 -> CAN_FAIL
18:43:23:477 -> Timeout
18:43:23:481 -> CAN_FAIL
18:43:25:479 -> Timeout
18:43:25:482 -> CAN_FAIL
18:43:27:483 -> Timeout

Hello,

Sorry I don't know.

Check your Arduino config (CAN config/timings etc ..) as we don't have any idea about your setup from that side and we don't support Arduino hardware/environment.

Check also your Hardware and wiring

Check the filters as I suggested earlier.

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.
Announcement

We’re moving the ST Community to a new platform to give you a better and more reliable community experience.