cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Transmit Loopback Mode

MZimm.3
Associate II

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

1 ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

10 REPLIES 10
Karl Yamashita
Lead III

Post relevant code so we can see any possible issue

I Can't Believe It's Not Butter. If you find my answers useful, click the accept button so that way others can see the solution.
Javier1
Principal

>>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?

we dont need to firmware by ourselves, lets talk
MZimm.3
Associate II

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

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;

we dont need to firmware by ourselves, lets talk
MZimm.3
Associate II

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

what about removing that Loopback and properly wiring the canbus somewhere

we dont need to firmware by ourselves, lets talk
MZimm.3
Associate II

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.

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
Associate II

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