Skip to main content
MZimm.3
Associate II
January 31, 2023
Solved

CAN Transmit Loopback Mode

  • January 31, 2023
  • 7 replies
  • 5543 views

Hello all,

I would like to put the CAN2 interface into operation on a NUCLEO STM32F466RE board. For this purpose I have programmed an example in which a BYTE array HELLO is to be sent in the loopback.

If I now pass the TxHeader Struct to the HAL_CAN_AddTxMessage() method, this method is executed without error. The signal recording on the TX pin also shows a data transmission. It's just not the data I expected.

My data from the TxHeader cannot be entered in the mailbox register. It looks like the registers are locked, although all mailboxes report that they are free.

What else can I do wrong at this point?

What else could be the reason that the mailbox cannot be written to?

Greetings

Michael0693W00000Y9cC4QAJ.png

This topic has been closed for replies.
Best answer by mƎALLEm

Hello,

Try to enable also RCC CAN1 clock even using CAN2 only and see what happen.

7 replies

Karl Yamashita
Principal
January 31, 2023

Post relevant code so we can see any possible issue

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Javier1
Principal
January 31, 2023

>>What else could be the reason that the mailbox cannot be written to?

the mailbox could be already full, is your code checking before trying to send?

hit me up in https://www.linkedin.com/in/javiermuñoz/
MZimm.3
MZimm.3Author
Associate II
January 31, 2023

This is my CAN Init method. I actually took everything from an example on the internet. 

void main_CAN2Init(void)

{

hcan2.Instance = CAN2;

hcan2.Init.Mode = CAN_MODE_LOOPBACK; // Zum testen im Loopback mode

hcan2.Init.AutoBusOff = DISABLE;

hcan2.Init.AutoRetransmission = ENABLE;

hcan2.Init.AutoWakeUp = DISABLE;

hcan2.Init.ReceiveFifoLocked = DISABLE;

hcan2.Init.TimeTriggeredMode = DISABLE;

hcan2.Init.TransmitFifoPriority = DISABLE;

// Settings related to CAN bit Timing

hcan2.Init.Prescaler = 2;

hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;

hcan2.Init.TimeSeg1 = CAN_BS1_6TQ;

hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;

if(HAL_CAN_Init(&hcan2) != HAL_OK)

{

Error_Handler("Error main_CAN2Init\n");

}

}

And this is the send method

static void CAN2_Tx(void)

{

CAN_TxHeaderTypeDef TxHeader;

uint32_t TxMailbox;

uint8_t new_message[] = {'H','E','L','L','O'};

TxHeader.DLC = 5;

TxHeader.StdId = 0x65D;

TxHeader.ExtId = 0;

TxHeader.IDE = CAN_ID_STD;

TxHeader.RTR = CAN_RTR_DATA;

TxHeader.TransmitGlobalTime = DISABLE;

if(HAL_CAN_AddTxMessage(&hcan2, &TxHeader, new_message, &TxMailbox) != HAL_OK)

{

Error_Handler("Error in Methode CAN2_Tx\n");

}

while(HAL_CAN_IsTxMessagePending(&hcan2, TxMailbox));

sprintf(uartText,"CAN Message Transmitted\n");

uart2Transmit(0, (uint8_t*)uartText, strlen(uartText));

}

And the Transmit Mailbox Bits indicate that there is no transmit request pending

0693W00000Y9e05QAB.png 

Javier1
Principal
February 1, 2023

try this

	howmanyTXmailboxes = HAL_CAN_GetTxMailboxesFreeLevel(hcan_object_reference);
	if (howmanyTXmailboxes > 0) {
		status = HAL_CAN_AddTxMessage(hcan_object_reference, &canTxheader,
				messageTxData, &pTxMailbox);
		if (status != HAL_OK) {
			//Error_handler(); error de transmision
		}
	}else{
		status=HAL_BUSY;
	}
	return status;

hit me up in https://www.linkedin.com/in/javiermuñoz/
MZimm.3
MZimm.3Author
Associate II
February 1, 2023

Thanks for the idea

When I look at the answer in the debugger, all three mailboxes are free.

Name : howmanyTXmailboxes

Details:3

Default:3

Decimal:3

Hex:0x3

Binary:11

Octal:03

Javier1
Principal
February 1, 2023

what about removing that Loopback and properly wiring the canbus somewhere

hit me up in https://www.linkedin.com/in/javiermuñoz/
MZimm.3
MZimm.3Author
Associate II
February 1, 2023

I have also tried it with a CAN bus and a CAN analyser. Unfortunately with the same result.

Now I have done the whole CAN initialisation on CAN1. On CAN1 my test software runs immediately. I can see in the debugger how the mailboxes are written with my data and the data I expect is transmitted.

Now I am asking myself why it works on CAN1 and not on CAN2.

mƎALLEm
mƎALLEmBest answer
Technical Moderator
February 1, 2023

Hello,

Try to enable also RCC CAN1 clock even using CAN2 only and see what happen.

"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."
MZimm.3
MZimm.3Author
Associate II
February 2, 2023

Hello all,

that was the important tip. Now it works on both CAN interfaces. Document RM0390 also says that CAN2 is only a slave and has no direct access to the SRAM. That was probably the reason why I could not write to the mailboxes.

Many thanks to all for your time and effort. 

Greetings Michael

mƎALLEm
Technical Moderator
February 2, 2023

Hello,

Thank you for the feedback. Indeed SRAM2 is a slave and it was the reason why it was not working.

As it was solved please "Select as best" the comment that answered your question. This will help other users to find that answer faster.

Thanks

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