cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, I try to communication with two stm32f4 via CAN bus. the first board (which transmit the message) as can1 and the second (which receive the message) as can2. I'm using the new HAL_CAN function.

AS.5
Associate II

In polling mode, HAL_CAN_AddTxMessage() returns HAL_OK, but HAL_CAN_GetRxMessage() returns HAL_CAN_ERROR_PARAM.

In interrupt mode, the function HAL_CAN_RxFifo0MsgPendingCallback() never has been called (CAN1_RX0_IRQHandler not been called).

Also HAL_CAN_TxMailbox0CompleteCallback() not been called because HAL_CAN_IRQHandler() return HAL_CAN_ERROR_TX_TERR0;

How can I Communicate between the boards?

thanks

5 REPLIES 5
DJC
Senior

I do CAN on the F4, using those functions. ... they work.

I can say, this is not really enough info to know what is going wrong without seeing more info. Like the receiver configuration code. Could be anything. Set breakpoints. Put a can-sniffer that you know works on the wires. etc.

I can say... i dont get CAN_ERORR_PRAM when its not even plugged in. So probably the issue is just configuration on that board alone. Its likely to have nothing to do with hardware and everything to do with some missing parameters on the setup.

AS.5
Associate II

Hi, thanks for your answer.

Do you have any example for communication with two boards?

Here are some configurations from my code.

There are two separate codes for each CAN bus.

this is my CAN1 Init:

 hcan1.Instance = CAN1;

 hcan1.Init.Prescaler = 20;

 hcan1.Init.Mode = CAN_MODE_NORMAL;

 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan1.Init.TimeSeg1 = CAN_BS1_4TQ;

 hcan1.Init.TimeSeg2 = CAN_BS2_5TQ;

 hcan1.Init.TimeTriggeredMode = DISABLE;

 hcan1.Init.AutoBusOff = DISABLE;

 hcan1.Init.AutoWakeUp = DISABLE;

 hcan1.Init.AutoRetransmission = DISABLE;

 hcan1.Init.ReceiveFifoLocked = DISABLE;

 hcan1.Init.TransmitFifoPriority = DISABLE;

this is my CAN1 filter configuration:

MyFilterConfig.FilterFIFOAssignment= CAN_FILTER_FIFO0;

    MyFilterConfig.FilterIdHigh=0;

    MyFilterConfig.FilterIdLow=0;

    MyFilterConfig.FilterMaskIdHigh=0;

    MyFilterConfig.FilterMaskIdLow=0;

    MyFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK;

    MyFilterConfig.FilterScale= CAN_FILTERSCALE_32BIT;

    MyFilterConfig.FilterActivation=ENABLE;

    MyFilterConfig.SlaveStartFilterBank=14;

    MyFilterConfig.FilterBank=0;

this is my CAN2 Init:

hcan2.Instance = CAN2;

 hcan2.Init.Prescaler = 20;

 hcan2.Init.Mode = CAN_MODE_NORMAL;

 hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan2.Init.TimeSeg1 = CAN_BS1_4TQ;

 hcan2.Init.TimeSeg2 = CAN_BS2_5TQ;

 hcan2.Init.TimeTriggeredMode = DISABLE;

 hcan2.Init.AutoBusOff = DISABLE;

 hcan2.Init.AutoWakeUp = DISABLE;

 hcan2.Init.AutoRetransmission = DISABLE;

 hcan2.Init.ReceiveFifoLocked = DISABLE;

 hcan2.Init.TransmitFifoPriority = DISABLE;

this is my CAN2 filter configuration:

 MyFilterConfig.FilterFIFOAssignment= CAN_FILTER_FIFO0;

  MyFilterConfig.FilterIdHigh=0;

  MyFilterConfig.FilterIdLow=0;

  MyFilterConfig.FilterMaskIdHigh=0;

  MyFilterConfig.FilterMaskIdLow=0;

  MyFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK;

  MyFilterConfig.FilterScale= CAN_FILTERSCALE_32BIT;

  MyFilterConfig.FilterActivation=ENABLE;

  MyFilterConfig.SlaveStartFilterBank=14;

  MyFilterConfig.FilterBank=14;

This is mine for the F4 that does send and receive just fine.

My other board is something else, so i haven't included it.

myfilterconfig is just in some parent struct, thats why it starts with can-> its nothing todo with the hcan driver itself.

I'm no expert at this, but on thing that flags interest is your filter: try to start with the same filter config that i have here. Its just setting the mask to not care about anything and accept everything.

Why do you use FilterBank14?

and why do you have a Slave Filter bank?.. isn't this for when you run two can interfaces on the same MCU?

I didn't check any of your time seg stuff. I just use this website (when its up) to make sure those are correct.

http://www.bittiming.can-wiki.info/

Mine is designed to give me 500kbps for an APB1 clock speed of 48mhz.

  hcan1.Instance = CAN1;
  hcan1.Init.Prescaler = 6;
  hcan1.Init.Mode = CAN_MODE_NORMAL;
  hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;
  hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
  hcan1.Init.TimeTriggeredMode = DISABLE;
  hcan1.Init.AutoBusOff = ENABLE;
  hcan1.Init.AutoWakeUp = ENABLE;
  hcan1.Init.AutoRetransmission = ENABLE;
  hcan1.Init.ReceiveFifoLocked = ENABLE;
  hcan1.Init.TransmitFifoPriority = ENABLE;
 
	can->filterConfig.FilterBank = 0;
	can->filterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
	can->filterConfig.FilterScale = CAN_FILTERSCALE_16BIT;
	// These are 16 bit values! SEE DocID13902 Rev 16 RM0008 Controller area network (bxCAN)
	// It is split into 4 registers because of the ability to devide 1 filter into 4x 16bit or 2x 32bit
	// ALSO the addresses are LEFT aligned with STID as MSB and EXTID as LSB (which is how it is transmitted)
 
	// // ID list mode allows 2x32 bit filters. I don't think this does anything since i have select mask mode and dont care about anything
	can->filterConfig.FilterIdHigh = 0xFFFF;
	can->filterConfig.FilterIdLow = 0x0000;
 
	// Mask now because the opportunity for a 2nd filter.
	// This is now the address of board
	can->filterConfig.FilterMaskIdHigh = 0x0000;
	can->filterConfig.FilterMaskIdLow = 0x0000; // LSB (isEXT = 1, RTR = 0, reserved = 0)
	can->filterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
	can->filterConfig.FilterActivation = ENABLE;
 
	if (HAL_CAN_ConfigFilter((can->hcan), &(can->filterConfig)) != HAL_OK) {
		/* Filter configuration Error */
		Error_Handler();
	}

AS.5
Associate II

When I debug I pat attention the ESR (error status register) changed after transmit a message. The last error code bits are 001, according to  reference manual it means-  Stuff Error .

What does this error means? How can I solve it?

It means "stuff" is broken! 😂

It means that there is a bit-stuffing error. It's probably really a timing problem. Look up bit-stuffing, send data that doesn't result in needing bit stuffing and see if the problem changes to something else, like CRC, jumbled data, no ack.

Make sure both your devices have the right timing quanta setup. Use the link i sent above.

Do you have some sort of credible device for listening to your CAN bus? ie: a kvaser or a pcan? There also various raspberry pi shields using socketcan that work well.

Get communication working with 1 device and that first. Then the other. Then put them together with the device sitting on the bus to tell you what it sees.