cancel
Showing results for 
Search instead for 
Did you mean: 

F446RE and CAN2 in loopback mode

jungle_jim
Associate II

I'm working on a custom board with a F446RET6 and testing through some code bits. I've been able to successfully test that the board programs and functions with a led-blink style program. My problem is that I'm attempting to use CAN2; I'm trying to test via loopback but I'm having issues getting any type of IRQ for a receive. I feel like its something basic I'm missing here but after reading through some other posts I'm either brain-dead or missing something.

  • currently using the HSI clock but I wouldn't think that would matter for loopback testing (understand for the "real" thing would be best to use the HSE).
  • RX GPIO is set to pull-up
  • Verified (after looking at some old posts using an older version of Cube) that the CAN1 clock is being initialized

Also I understand that the "CAN2" device is really the "slave" to CAN1. I've got my slave start for the filter set to 14 to split everything half and half and just arbitrarily chose bank 15:

 

 

 

hcan2Filter.FilterActivation = ENABLE;
hcan2Filter.FilterIdHigh = 0x0000;
hcan2Filter.FilterIdLow = 0x0000;
hcan2Filter.FilterMaskIdHigh = 0x0000;
hcan2Filter.FilterMaskIdLow = 0x0000;
hcan2Filter.FilterMode = CAN_FILTERMODE_IDMASK;
hcan2Filter.FilterScale = CAN_FILTERSCALE_32BIT;
hcan2Filter.FilterFIFOAssignment = CAN_RX_FIFO1;
hcan2Filter.FilterBank = 15;
hcan2Filter.SlaveStartFilterBank = 14;

 

 

 

 

Also I have the FIFO1 message pending interrupt set up:

 

 

 

//enable CAN2 interrupt
if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING) != HAL_OK)
{
  printf("CAN IRQ enable error\r\n");
  Error_Handler();
}

 

 

 

 

The IRQ function for now just spits out the rx'd ID:

 

 

 

void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
  if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO1, &RxHeader, RxData) != HAL_OK)	//get can message
  {
    printf("CAN msg RX error\r\n");
    Error_Handler();
  }										//handle any errors if not OK
  printf("CAN Message Rx'd from ID %d\r\n", RxHeader.StdId);
  return;
}

 

 

 

 

I've attached my IOC and main, can someone point out what's probably staring me in the face?

1 ACCEPTED SOLUTION

Accepted Solutions

Yes, I understand how to adjust the CAN timing. My question was on if the CAN-TX pin (PB13) is still exposed in loopback or not. I thought I remembered coming across that it was.

 

For anyone that comes across this with google, the latest ioc managed to pass loopback after updating the mode. I think when I re-wrote the code I had it and was just fighting a few different things. Some good threads out there explaining better the filtering and banks.

 

Filter banks explained 

Filter slave start address 

 

Some other things that tripped me up which i guess was just a cumulative mess:

  • One thing that I guess when I started wasn't abundantly clear is that you HAVE to set the filter, even if its "let everything through" so the RX data gets assigned to a FIFO.
  • See above for the filter setup. If you're using CAN1, you have to select a bank BELOW the "slave start filter bank" address. If you're using CAN2, you have to select a bank ABOVE the "slave start" address.
  • Remember "rx0 interrupt" and "rx1 interrupt" in the NVIC settings correspond to fifo0 and fifo1, respectively.
  • Remember you can setup the rx data to go to either Fifo0 OR Fifo1, no matter which CAN peripheral you're using. Just make sure the IRQ hook and callback are set appropriately (based on your filter configuration).

View solution in original post

5 REPLIES 5
SofLit
ST Employee

Hello,

Checking your code, you are configuring the CAN in normal mode not in loopback mode.

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.

Valid. I had tried to update the same code prior for CAN1 and was having the same issues, I missed that in reverting back. Updated the OP to make clear. Changing to loopback still results in the same issue.

Speaking of loopback, so long as its in "loopback" but not "loopback + silent" the TX pin (PB13) should still be exposed, correct? I'll see if I can slow the bitrate down to something my "dumb" logic analyzer can pick up or not just to test if there's anything getting out at all.

Hello,

Try to change the following parameters' values:

 

  hcan2.Init.Prescaler = 12;
  hcan2.Init.TimeSeg1 = CAN_BS1_3TQ;
  hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;

 

Decrease, the prescaler as much as possible and increase BS1 and BS2 as much as possible to fit your bitrate.  

Try for example:

Prescaler = 3

BS1 = 14

BS2 = 5

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.

Yes, I understand how to adjust the CAN timing. My question was on if the CAN-TX pin (PB13) is still exposed in loopback or not. I thought I remembered coming across that it was.

 

For anyone that comes across this with google, the latest ioc managed to pass loopback after updating the mode. I think when I re-wrote the code I had it and was just fighting a few different things. Some good threads out there explaining better the filtering and banks.

 

Filter banks explained 

Filter slave start address 

 

Some other things that tripped me up which i guess was just a cumulative mess:

  • One thing that I guess when I started wasn't abundantly clear is that you HAVE to set the filter, even if its "let everything through" so the RX data gets assigned to a FIFO.
  • See above for the filter setup. If you're using CAN1, you have to select a bank BELOW the "slave start filter bank" address. If you're using CAN2, you have to select a bank ABOVE the "slave start" address.
  • Remember "rx0 interrupt" and "rx1 interrupt" in the NVIC settings correspond to fifo0 and fifo1, respectively.
  • Remember you can setup the rx data to go to either Fifo0 OR Fifo1, no matter which CAN peripheral you're using. Just make sure the IRQ hook and callback are set appropriately (based on your filter configuration).

Hello,

I'm confused... Your first question was regarding the CAN is not working (no message received in the RX callback), now you're asking about the exposal of pin PB13!

The exposal of the Tx pin depends on your configuration:

Silent mode / Loop back mode / Loop back combined with silent mode.

So only in loop back mode the Tx pin is exposed.

Now regarding your comment:

 

Yes, I understand how to adjust the CAN timing. My question was on if the CAN-TX pin (PB13) is still .


-> It's true that your configuration of the prescaler, BS1 and BS2 gives you your 500kb/s but these values are not recommended as BS1 and BS2 values are low. Since your request was regarding a communication issue I thought about your bitrate configuration. I don't think this is due to a filter config (already checked). That's why I recommended you to change your bitrate settings.

 

 

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.