2024-12-11 02:54 AM - last edited on 2024-12-11 02:55 AM by Andrew Neil
#include"STM32H743.h"
/* CAN Handle */
FDCAN_HandleTypeDef hfdcan1;
/* Function to Initialize CAN Peripheral */
void CAN_Init(void)
{
/*CAN GPIO Configuration (PB12 as RX, PB13 as TX)*/
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE(); // Enable clock for GPIOB
// Configure PB12 and PB13 for CAN (RX and TX)
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13; // Combine pins
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function push-pull
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up or pull-down resistors
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1; // Alternate function for FDCAN1
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
hfdcan1.Instance = FDCAN1;
hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
hfdcan1.Init.AutoRetransmission = DISABLE;
hfdcan1.Init.TransmitPause = DISABLE;
hfdcan1.Init.ProtocolException = DISABLE;
hfdcan1.Init.NominalPrescaler = 161;
hfdcan1.Init.NominalSyncJumpWidth = 1;
hfdcan1.Init.NominalTimeSeg1 = 7;
hfdcan1.Init.NominalTimeSeg2 = 6;
hfdcan1.Init.DataPrescaler = 1;
hfdcan1.Init.DataSyncJumpWidth = 1;
hfdcan1.Init.DataTimeSeg1 = 1;
hfdcan1.Init.DataTimeSeg2 = 1;
hfdcan1.Init.MessageRAMOffset = 0;
hfdcan1.Init.StdFiltersNbr = 1;
hfdcan1.Init.ExtFiltersNbr = 0;
hfdcan1.Init.RxFifo0ElmtsNbr = 8;
hfdcan1.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;
hfdcan1.Init.RxFifo1ElmtsNbr = 0;
hfdcan1.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;
hfdcan1.Init.RxBuffersNbr = 0;
hfdcan1.Init.RxBufferSize = FDCAN_DATA_BYTES_8;
hfdcan1.Init.TxEventsNbr = 0;
hfdcan1.Init.TxBuffersNbr = 0;
hfdcan1.Init.TxFifoQueueElmtsNbr = 8;
hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
hfdcan1.Init.TxElmtSize = FDCAN_DATA_BYTES_8;
if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
{
while (1) { }
}
HAL_FDCAN_ConfigMessageRam(&hfdcan1);
/* Message RAM Configuration */
FDCAN1->SIDFC = (0x0000 << 2) | 16; // 11-bit filters: start at 0x0000, 16 elements
FDCAN1->XIDFC = (0x0010 << 2) | 8; // 29-bit filters: start at 0x0010, 8 elements
FDCAN1->RXF0C = (0x0020 << 2) | 32; // Rx FIFO 0: start at 0x0020, 32 elements
FDCAN1->TXBC = (0x0260 << 2) | 8; // Tx buffers: start at 0x0260, 8 elements
if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
{
while (1) { } // Start Error
}
}
/* Function to Transmit Data via CAN */
HAL_StatusTypeDef CAN_TransmitData(uint32_t id, uint8_t* data, uint8_t length)
{
FDCAN_TxHeaderTypeDef txHeader;
txHeader.Identifier = id;
txHeader.IdType = FDCAN_STANDARD_ID;
txHeader.TxFrameType = FDCAN_DATA_FRAME;
txHeader.DataLength = (length << 16);
txHeader.BitRateSwitch = FDCAN_BRS_OFF;
txHeader.FDFormat = FDCAN_CLASSIC_CAN;
if (data == NULL || length > 8)
{
return HAL_ERROR; // Debug here
}
return HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, data);
}
/*HAL_StatusTypeDef CAN_TransmitData(uint32_t id, uint8_t* data, uint8_t length)
{
if (data == NULL || length > 8)
{
return HAL_ERROR; // Invalid input
}
FDCAN_TxHeaderTypeDef txHeader;
txHeader.Identifier = id;
txHeader.IdType = FDCAN_STANDARD_ID;
txHeader.TxFrameType = FDCAN_DATA_FRAME;
txHeader.DataLength = (length << 16); // Valid DLC: 0 to 8
txHeader.BitRateSwitch = FDCAN_BRS_OFF;
txHeader.FDFormat = FDCAN_CLASSIC_CAN;
txHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
txHeader.MessageMarker = 0;
HAL_StatusTypeDef status = HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, data);
if (status != HAL_OK)
{
uint32_t errorCode = HAL_FDCAN_GetError(&hfdcan1);
// Inspect errorCode in debugger
while (1); // Debug here
}
return status;
}*/
/* Function to Receive Data from CAN */
HAL_StatusTypeDef CAN_ReceiveData(uint32_t* id, uint8_t* data, uint8_t* length)
{
FDCAN_RxHeaderTypeDef rxHeader;
uint32_t fifoIndex = 0;
if (HAL_FDCAN_GetRxMessage(&hfdcan1, fifoIndex, &rxHeader, data) != HAL_OK)
{
return HAL_ERROR; // Failed to receive the message
}
*id = rxHeader.Identifier;
*length = rxHeader.DataLength;
return HAL_OK;
}
here is full code of stm32h743zit6 can u help me out this code not working I'm using classic mode CAN
if any one know the CAN help me out
@eLKa @SofLit @Andrew Neil
2024-12-11 03:00 AM
"not working" tells us nothing - so impossible to help you out.
Please see the Posting Tips: How to write your question to maximize your chances to find a solution
In particular:
2024-12-11 03:07 AM - edited 2024-12-11 03:09 AM
Hello @bhargav23 and welcome to the community,
Is not working does not mean anything.
Please tell what is not working. Transmit? receive?
You are using Normal mode, do you have another node connected on the bus?
Also, I propose you to use STM32CubeMx to generate your code.
2024-12-11 03:16 AM - last edited on 2024-12-11 03:22 AM by SofLit
Hi @SofLit @Andrew Neil
transmit part is not working i didn't check the receive part can u help me out u have any working code
Can u help me out u can u check the code once i configure any wrong in the code
I'm using the stm32h743zit6 board
I don't have schematic of the board
when we going transmit part
return HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, data); in the tx function this line goes to below line when we are doing step in
it come sto this conition ad checks it not going in side --->if (hfdcan->State == HAL_FDCAN_STATE_BUSY) after this condition in this function it goes to all if 2 conditions given below when we are doing step in
{
/* Check that the Tx FIFO/Queue has an allocated area into the RAM */
if ((hfdcan->Instance->TXBC & FDCAN_TXBC_TFQS) == 0U)
{
/* Update error code */
hfdcan->ErrorCode |= HAL_FDCAN_ERROR_PARAM;
return HAL_ERROR;
}
/* Check that the Tx FIFO/Queue is not full */
if ((hfdcan->Instance->TXFQS & FDCAN_TXFQS_TFQF) != 0U)
{
/* Update error code */
hfdcan->ErrorCode |= HAL_FDCAN_ERROR_FIFO_FULL;
return HAL_ERROR;
}
after this if ocnitions it goes to below else conition in that else conition it goes to put index line and next step in it goes to FD CAN copy message and next it goes another function
else
{
/* Retrieve the Tx FIFO PutIndex */
PutIndex = ((hfdcan->Instance->TXFQS & FDCAN_TXFQS_TFQPI) >> FDCAN_TXFQS_TFQPI_Pos);
/* Add the message to the Tx FIFO/Queue */
FDCAN_CopyMessageToRAM(hfdcan, pTxHeader, pTxData, PutIndex);
/* Activate the corresponding transmission request */
hfdcan->Instance->TXBAR = ((uint32_t)1 << PutIndex);
/* Store the Latest Tx FIFO/Queue Request Buffer Index */
hfdcan->LatestTxFifoQRequest = ((uint32_t)1 << PutIndex);
}
/* Return function status */
return HAL_OK;
}
after else function it comes here down if conition it goes inside and checks and it goes next function
if (pTxHeader->IdType == FDCAN_STANDARD_ID)
{
TxElementW1 = (pTxHeader->ErrorStateIndicator |
FDCAN_STANDARD_ID |
pTxHeader->TxFrameType |
(pTxHeader->Identifier << 18U));
}
after the above if condition it goes to below tx elementw2 it goes and add upto 4,to 32 like that and next it goes ome line after this
TxElementW2 = ((pTxHeader->MessageMarker << 24U) |
pTxHeader->TxEventFifoControl |
pTxHeader->FDFormat |
pTxHeader->BitRateSwitch |
(pTxHeader->DataLength << 16U));
the above function after it goes to below line
/* Calculate Tx element address */
TxAddress = (uint32_t *)(hfdcan->msgRam.TxBufferSA + (BufferIndex * hfdcan->Init.TxElmtSize * 4U));
the above the line after it goes to below line and it chceks below condition
/* Write Tx element header to the message RAM */
*TxAddress = TxElementW1;
TxAddress++;
*TxAddress = TxElementW2;
TxAddress++;
and after above lines it comes to below and it checks some time aagain it goes hard fault hanldelr
/* Write Tx payload to the message RAM */
for (ByteCounter = 0; ByteCounter < DLCtoBytes[pTxHeader->DataLength]; ByteCounter += 4U)
{
*TxAddress = (((uint32_t)pTxData[ByteCounter + 3U] << 24U) |
((uint32_t)pTxData[ByteCounter + 2U] << 16U) |
((uint32_t)pTxData[ByteCounter + 1U] << 8U) |
(uint32_t)pTxData[ByteCounter]);
TxAddress++;
}
im new to this field can u help me out
2024-12-11 03:19 AM
You've already forgotten how to properly post source code?
@bhargav23 wrote:I'm using the stm32h743zit6 board
What "stm32h743zit6 board", exactly ?
@bhargav23 wrote:I don't have schematic of the board
So how do you even know that you have your CAN connections correct?
2024-12-11 03:19 AM - edited 2024-12-11 03:20 AM
First , to share code please use </> button. See this Tip.
Second, in next time please answer the questions we ask. Repeating the same phrases does not allow us to move forward.
So again:
You are using Normal mode, do you have another node connected on the bus?
2024-12-11 03:29 AM
And why are you using HAL and then doing bare metal?
/* Message RAM Configuration */
FDCAN1->SIDFC = (0x0000 << 2) | 16; // 11-bit filters: start at 0x0000, 16 elements
FDCAN1->XIDFC = (0x0010 << 2) | 8; // 29-bit filters: start at 0x0010, 8 elements
FDCAN1->RXF0C = (0x0020 << 2) | 32; // Rx FIFO 0: start at 0x0020, 32 elements
FDCAN1->TXBC = (0x0260 << 2) | 8; // Tx buffers: start at 0x0260, 8 elements
Look. Before starting with Normal mode, Use External loopback mode, validate Transmit and your receive especially the filters configs.
You can refer to the example provided in CubeHAL: https://github.com/STMicroelectronics/STM32CubeH7/tree/master/Projects/STM32H743I-EVAL/Examples/FDCAN/FDCAN_Classic_Frame_Networking
When you validate CAN in loopback mode you can move forward and use Normal mode.
Mixing HAL and bare metal implementation when we don't know what we are doing is a bad idea.
2024-12-11 03:37 AM
@SofLit @Andrew Neil
sorry Guys
I'm using to transmit the data from stm32h743zit6 board to stm32f407vgt6 board.
our company have said to me use the STM32Cube ide for HAL coding
There only create the board take the reference from here STM32H743VIT6 - STM32H7XX M | STM32-base project
I'm uploading the INIT,TX,RX function code here full code.
#include"STM32H743.h"
/* CAN Handle */
FDCAN_HandleTypeDef hfdcan1;
/* Function to Initialize CAN Peripheral */
void CAN_Init(void)
{
/*CAN GPIO Configuration (PB12 as RX, PB13 as TX)*/
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE(); // Enable clock for GPIOB
// Configure PB12 and PB13 for CAN (RX and TX)
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13; // Combine pins
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function push-pull
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up or pull-down resistors
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1; // Alternate function for FDCAN1
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
hfdcan1.Instance = FDCAN1;
hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
hfdcan1.Init.AutoRetransmission = DISABLE;
hfdcan1.Init.TransmitPause = DISABLE;
hfdcan1.Init.ProtocolException = DISABLE;
hfdcan1.Init.NominalPrescaler = 161;
hfdcan1.Init.NominalSyncJumpWidth = 1;
hfdcan1.Init.NominalTimeSeg1 = 7;
hfdcan1.Init.NominalTimeSeg2 = 6;
hfdcan1.Init.DataPrescaler = 1;
hfdcan1.Init.DataSyncJumpWidth = 1;
hfdcan1.Init.DataTimeSeg1 = 1;
hfdcan1.Init.DataTimeSeg2 = 1;
hfdcan1.Init.MessageRAMOffset = 0;
hfdcan1.Init.StdFiltersNbr = 1;
hfdcan1.Init.ExtFiltersNbr = 0;
hfdcan1.Init.RxFifo0ElmtsNbr = 8;
hfdcan1.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;
hfdcan1.Init.RxFifo1ElmtsNbr = 0;
hfdcan1.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;
hfdcan1.Init.RxBuffersNbr = 0;
hfdcan1.Init.RxBufferSize = FDCAN_DATA_BYTES_8;
hfdcan1.Init.TxEventsNbr = 0;
hfdcan1.Init.TxBuffersNbr = 0;
hfdcan1.Init.TxFifoQueueElmtsNbr = 8;
hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
hfdcan1.Init.TxElmtSize = FDCAN_DATA_BYTES_8;
if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
{
while (1) { }
}
/* Message RAM Configuration
FDCAN1->SIDFC = (0x0000 << 2) | 16; // 11-bit filters: start at 0x0000, 16 elements
FDCAN1->XIDFC = (0x0010 << 2) | 8; // 29-bit filters: start at 0x0010, 8 elements
FDCAN1->RXF0C = (0x0020 << 2) | 32; // Rx FIFO 0: start at 0x0020, 32 elements
FDCAN1->TXBC = (0x0260 << 2) | 8; // Tx buffers: start at 0x0260, 8 elements */
if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
{
while (1) { } // Start Error
}
}
/* Function to Transmit Data via CAN */
HAL_StatusTypeDef CAN_TransmitData(uint32_t id, uint8_t* data, uint8_t length)
{
FDCAN_TxHeaderTypeDef txHeader;
txHeader.Identifier = id;
txHeader.IdType = FDCAN_STANDARD_ID;
txHeader.TxFrameType = FDCAN_DATA_FRAME;
txHeader.DataLength = (length << 16);
txHeader.BitRateSwitch = FDCAN_BRS_OFF;
txHeader.FDFormat = FDCAN_CLASSIC_CAN;
if (data == NULL || length > 8)
{
return HAL_ERROR; // Debug here
}
return HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, data);
}
/* Function to Receive Data from CAN */
HAL_StatusTypeDef CAN_ReceiveData(uint32_t* id, uint8_t* data, uint8_t* length)
{
FDCAN_RxHeaderTypeDef rxHeader;
uint32_t fifoIndex = 0;
if (HAL_FDCAN_GetRxMessage(&hfdcan1, fifoIndex, &rxHeader, data) != HAL_OK)
{
return HAL_ERROR; // Failed to receive the message
}
*id = rxHeader.Identifier;
*length = rxHeader.DataLength;
return HAL_OK;
}
.
2024-12-11 04:01 AM
Hi @Andrew Neil
yes exactly STM32H743ZIT6 board only
they will give me pin out table there only I'm checking and developing code
2024-12-11 04:24 AM
Who is "they" ?