2024-12-23 04:42 AM - last edited on 2024-12-24 12:20 AM by SofLit
Hi all
Can You provide me a correct code for how we can transfer TX frame using HAL .
please check my code below .
Issue what I am facing in my code is only I am seeing first ID data even data and ID are correctly stored inside buffer.
Please guide me .
PFA
2024-12-23 04:44 AM
2024-12-23 05:04 AM
It's a bad idea to insert code as screenshots, partly because you can't search images for text.
There are therefore instructions in the Community Guidelines on how to insert code, for which there is an explicit function field </>, which you have also used in previous posts. Please remove the images and replace them with the code block.
Thank you
/Peter
2024-12-23 05:04 AM - edited 2024-12-23 05:06 AM
Hello,
Your description is not clear. See: https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228
Also instead of attaching screenshots of your code, please use </> button to paste it here.
See: https://community.st.com/t5/community-guidelines/how-to-insert-source-code/ta-p/693413
2024-12-23 05:13 AM
Hi
I am editing in the same post
Please check code. I am getting HAL_OK every time but unable to see TI1R data on the BUS.ONLY TI0R data I can see. even transmit mail box value getting change alternative 0 to 1 still facing the issue
while (1)
{
// Example: Transmit a CAN message
CAN_TxHeaderTypeDef txHeader;
const uint8_t tx_data[8] = {0x11};
const uint8_t tx_data1[8] = {0x12};
uint32_t tx_mailbox_used;
txHeader.StdId = 0x3e8; // Example CAN ID
txHeader.RTR = CAN_RTR_DATA;
txHeader.IDE = CAN_ID_STD;
txHeader.DLC = 8;
if (HAL_CAN_AddTxMessage(&hcan1, &txHeader, tx_data, &tx_mailbox_used) != HAL_OK)
{
Error_Handler();
}
txHeader.StdId = 0x244; // Example CAN ID
txHeader.RTR = CAN_RTR_DATA;
txHeader.IDE = CAN_ID_STD;
txHeader.DLC = 8;
if (HAL_CAN_AddTxMessage(&hcan1, &txHeader, tx_data1, &tx_mailbox_used) != HAL_OK)
{
Error_Handler();
}
HAL_Delay(10);
}
HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, const CAN_TxHeaderTypeDef *pHeader,
const uint8_t aData[], uint32_t *pTxMailbox)
{
uint32_t transmitmailbox;
HAL_CAN_StateTypeDef state = hcan->State;
uint32_t tsr = READ_REG(hcan->Instance->TSR);
/* Check the parameters */
assert_param(IS_CAN_IDTYPE(pHeader->IDE));
assert_param(IS_CAN_RTR(pHeader->RTR));
assert_param(IS_CAN_DLC(pHeader->DLC));
if (pHeader->IDE == CAN_ID_STD)
{
assert_param(IS_CAN_STDID(pHeader->StdId));
}
else
{
assert_param(IS_CAN_EXTID(pHeader->ExtId));
}
assert_param(IS_FUNCTIONAL_STATE(pHeader->TransmitGlobalTime));
if ((state == HAL_CAN_STATE_READY) ||
(state == HAL_CAN_STATE_LISTENING))
{
/* Check that all the Tx mailboxes are not full */
if (((tsr & CAN_TSR_TME0) != 0U) ||
((tsr & CAN_TSR_TME1) != 0U) ||
((tsr & CAN_TSR_TME2) != 0U))
{
/* Select an empty transmit mailbox */
transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;
/* Store the Tx mailbox */
*pTxMailbox = (uint32_t)1 << transmitmailbox;
/* Set up the Id */
if (pHeader->IDE == CAN_ID_STD)
{
hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) |
pHeader->RTR);
}
else
{
hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->ExtId << CAN_TI0R_EXID_Pos) |
pHeader->IDE |
pHeader->RTR);
}
/* Set up the DLC */
hcan->Instance->sTxMailBox[transmitmailbox].TDTR = (pHeader->DLC);
/* Set up the Transmit Global Time mode */
if (pHeader->TransmitGlobalTime == ENABLE)
{
SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TDTR, CAN_TDT0R_TGT);
}
/* Set up the data field */
WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDHR,
((uint32_t)aData[7] << CAN_TDH0R_DATA7_Pos) |
((uint32_t)aData[6] << CAN_TDH0R_DATA6_Pos) |
((uint32_t)aData[5] << CAN_TDH0R_DATA5_Pos) |
((uint32_t)aData[4] << CAN_TDH0R_DATA4_Pos));
WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDLR,
((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) |
((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) |
((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) |
((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos));
/* Request transmission */
SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TIR, CAN_TI0R_TXRQ);
/* Return function status */
return HAL_OK;
}
else
{
/* Update error code */
hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
return HAL_ERROR;
}
}
else
{
/* Update error code */
hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
return HAL_ERROR;
}
}
2024-12-23 05:15 AM - edited 2024-12-23 05:16 AM
1- Please give more details about your HW. You are not even giving the MCU part number? the board? Which transceiver?
2- Are you using Normal or Loopback mode. Better to attach your project.
3- Your description still not clear. Which ID is not sent on the bus?
2024-12-23 05:23 AM
Hi Sir
I am using STM32F407evb
2.I am using normal mode .
3. I have tried to send ID:3e8 data is (ox11) and ID:244 data(0x12) but I can see both ID transmitted at 10 ms but for both ID it is showing 0x11 only. even data is stored correctly inside both tx buffers.
so please guide me how can I send multiple ID .
I tried sending one ID it is working fine but more than one is not working .
B.R
Ashish
2024-12-23 05:31 AM
I don't see any issue in your code you need to have both frames received:
ID: 0x3E8 with data 0x11 and ID: 244 with data 0x12.
Check your receiver.
Or use Loopback mode and receive what you are sending. If you are receiving well both frame, this means you have an issue with your receiver..
2024-12-23 05:35 AM
Hi
Okay I will try loopback mode and check .
Can it be issue because of clock .how much clock we should give atleast I am running 40mhz and 6 prescaler
2024-12-23 05:43 AM
Hi
Can you suggest me a example for LOOPback mode