2023-01-31 06:26 AM
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
Michael
Solved! Go to Solution.
2023-02-01 07:18 AM
Hello,
Try to enable also RCC CAN1 clock even using CAN2 only and see what happen.
2023-01-31 07:45 AM
Post relevant code so we can see any possible issue
2023-01-31 08:41 AM
>>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?
2023-01-31 01:11 PM
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
2023-02-01 12:26 AM
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;
2023-02-01 01:00 AM
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
2023-02-01 01:26 AM
what about removing that Loopback and properly wiring the canbus somewhere
2023-02-01 05:55 AM
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.
2023-02-01 07:18 AM
Hello,
Try to enable also RCC CAN1 clock even using CAN2 only and see what happen.
2023-02-01 10:41 PM
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