cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H743Zit6 board CAN not working

bhargav23
Visitor

 

#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 

10 REPLIES 10

@bhargav23 wrote:

Hi @Andrew Neil 

yes exactly STM32H743ZIT6 board only  


You shouldn't use Normal mode if you are using one Node (STM32H743ZIT6 board). In your case you need to use Loopback mode. CAN is not neither USART nor SPI to test it in standalone in Normal mode.

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.