cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F042K6 Nucleo CAN bus receive issue

hsamurai
Associate II

I am trying to test the CAN bus on STM32F042K6 Nucleo board. I can transmit data but cannot receive. Here is my settings. Data is sent from STM32H7 -Disco board whose CAN0 bitrate is set to 250K. I can send and receive data to/from STM32H7 board. So I know STM32H7 works. But STM32F042K6 Nucleo can transmit to STM32H7 but cannot receive the data. STM32H7 board has embedded transceiver, and STM32F042 I have an external CAN Waveshare transceiver. I confirm the data on both ends using logic analyzer.

On STM32F042K6-Nucleo, the can settings are:

Prescalar: 4

Time Quantum: 500ns

Tq BS1: 4

Tq BS2: 3

SJW: 1

Operating mode: normal

Pin PA11 (D10 on Nucleo Header): CAN_RX

Pin PA12 (D2 on Nucleo Header): CAN_TX

With these settings, Transmit works with 250Kb which I confirmed with logic analyzer and on STM32H7 board receive. 

On the STM32F042 RX line, I also confirmed data comes in to pin PA11 with same bitrate (4us for a bit) periodically using logic analyzer.

Here's the code which I am polling. GetRx call always end up in HAL_Error with error parameter 0x200000.

What am I doing wrong ? I appreciate if anyone can help. I am new to CAN protocol.

Thanks

MX_CAN_Init();
  /* USER CODE BEGIN 2 */
  HAL_StatusTypeDef status;
  HAL_StatusTypeDef txstatus;
  HAL_StatusTypeDef rxstatus;
  if (HAL_CAN_Start(&hcan) == HAL_OK) {
    status = HAL_UART_Transmit(&huart2, "CAN Start OK\r\n", 14, HAL_MAX_DELAY);
  }
  uint8_t txData[8] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} ;
  CAN_TxHeaderTypeDef txHeader;
  txHeader.StdId = 0x123;
  txHeader.RTR = CAN_RTR_DATA;
  txHeader.IDE = CAN_ID_STD;
  txHeader.DLC = 8;
  uint32_t txMailBox;
 
  CAN_RxHeaderTypeDef rxHeader;
  rxHeader.IDE = CAN_ID_STD;
  rxHeader.DLC = 8;
  rxHeader.StdId = 0x123;
  rxHeader.RTR = CAN_RTR_DATA;
  uint8_t rxData[8] = {0};
  for(int ii=0;ii<8;++ii) rxData[ii] = 0;
 
  char uartBuffer[100];
 
  volatile uint32_t err = 0;
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    HAL_Delay(1000);
//    txstatus = HAL_CAN_AddTxMessage(&hcan, &txHeader, txData,  &txMailBox);
//    if (txstatus == HAL_OK) {
//      status = HAL_UART_Transmit(&huart2, "CAN Transmit OK\r\n", 17, HAL_MAX_DELAY);
//    }
    uint32_t rxlevel0 = HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0);
    uint32_t rxlevel1 = HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO1);
    sprintf(uartBuffer, "RxFifoLevel:%u,%u\r\n", rxlevel0,rxlevel1);
    status = HAL_UART_Transmit(&huart2, uartBuffer, 20, HAL_MAX_DELAY);
 
      rxstatus = HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &rxHeader, rxData);
      if (rxstatus == HAL_OK) {
        status = HAL_UART_Transmit(&huart2, "CAN Receive OK\r\n", 16, HAL_MAX_DELAY);
      }
    err = HAL_CAN_GetError(&hcan);
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

1 ACCEPTED SOLUTION

Accepted Solutions
DJC
Senior

I don't think this code would work for me and I have specifically seen the same problem on my STM32F4 series. For me it was not setting up the HW filters on this device.

I can't give you the complete code for it because mine is a little mashed between other things. But it ends in this: HAL_CAN_ConfigFilter

Try setting up something in CAN_FILTERMODE_IDMASK and setting both FilterMaskIdLow and FilterMaskIdHigh to 0x0000

That should get you listening to everything.

View solution in original post

3 REPLIES 3
DJC
Senior

I don't think this code would work for me and I have specifically seen the same problem on my STM32F4 series. For me it was not setting up the HW filters on this device.

I can't give you the complete code for it because mine is a little mashed between other things. But it ends in this: HAL_CAN_ConfigFilter

Try setting up something in CAN_FILTERMODE_IDMASK and setting both FilterMaskIdLow and FilterMaskIdHigh to 0x0000

That should get you listening to everything.

I put my settings here now

Hello David,

Thank you so much. It worked finally. My filter config was not good.