Skip to main content
Dick Lin
Senior
June 13, 2018
Question

HAL_CAN_AddTxMessage() issue

  • June 13, 2018
  • 2 replies
  • 9828 views
Posted on June 13, 2018 at 21:06

Hi,

I am using Nucleo-144 connecting CAN RX/TX PD0/PD1 to external CAN transceiver, PEAK.

The function is pretty straight forward. Prepare data then call

HAL_CAN_AddTxMessage() to transmit.

CAN_TxHeaderTypeDef TxHeader;

TxHeader.StdId = 0x321;

TxHeader.ExtId = 0x01;

TxHeader.RTR = CAN_RTR_DATA;

TxHeader.IDE = CAN_ID_STD;

TxHeader.DLC = 2;

uint8_t txData[8] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} ;

HAL_StatusTypeDef status = HAL_CAN_AddTxMessage(m_can, &TxHeader, txData, (uint32_t *)CAN_TX_MAILBOX0);

The

HAL_CAN_AddTxMessage() function bailed out on the CAN_TSR TME0/1/2 are all RESET. From the manual, seems all 3 mailboxes are full.

Why it's always full from the very beginning? How do I control this? __HAL_CAN_CLEAR_FLAG() doesn't have this flag.

/* Check that all the Tx mailboxes are not full */

if (((hcan->Instance->TSR & CAN_TSR_TME0) != RESET) ||

((hcan->Instance->TSR & CAN_TSR_TME1) != RESET) ||

((hcan->Instance->TSR & CAN_TSR_TME2) != RESET))

{

/* Return function status */

return HAL_OK;

}

else

{

/* Update error code */

hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;

return HAL_ERROR;

}

Bit 28 TME2: Transmit mailbox 2 empty

This bit is set by hardware when no transmit request is pending for mailbox 2.

Bit 27 TME1: Transmit mailbox 1 empty

This bit is set by hardware when no transmit request is pending for mailbox 1.

Bit 26 TME0: Transmit mailbox 0 empty

This bit is set by hardware when no transmit request is pending for mailbox 0.

Note: this post was migrated and contained many threaded conversations, some content may be missing.
    This topic has been closed for replies.

    2 replies

    T J
    Senior III
    June 13, 2018
    Posted on June 14, 2018 at 01:06

    The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6sj&d=%2Fa%2F0X0000000bxB%2FJ6v0XJnNmJpRrg8O18bARfmL8_H0LggvVNKKVr8dw0E&asPdf=false
    Dick Lin
    Dick LinAuthor
    Senior
    June 15, 2018
    Posted on June 15, 2018 at 01:42

    My problem now became I can't tx/rx using HAL_CAN_AddTxMessage/HAL_CAN_GetRxMessage even I can see CAN message from PEAK.

    The issue seems both RX/TX FIFO are empty.

    I thought the flags should control by STM32 CAN master.

    Thx

    T J
    Senior III
    June 15, 2018
    Posted on June 15, 2018 at 11:29

    i think your issue may be here

    what is m_can ?  I don't use this line, maybe it is your problem...

    HAL_StatusTypeDef status = HAL_CAN_AddTxMessage(m_can, &TxHeader, txData, (uint32_t *)CAN_TX_MAILBOX0);

    these lines look ok, but they are not working for you,.maybe there is a syntax issue

    /* Check that any of the Tx mailboxes are empty */   /* check if any one is ready to send*/  fixed comment

    if (((hcan->Instance->TSR & CAN_TSR_TME0) != RESET) ||

    ((hcan->Instance->TSR & CAN_TSR_TME1) != RESET) ||

    ((hcan->Instance->TSR & CAN_TSR_TME2) != RESET))

    {

    /* Return function status */

    return HAL_OK;

    }

    else

    {

    /* Update error code */

    hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;

    return HAL_ERROR;

    }

    check the differences in my code, its almost the same, just syntax differences

    if ((CAN->TSR & CAN_TSR_TME0) == CAN_TSR_TME0) // ready to transmit in this mailbox.

    mh_stm
    Associate II
    February 27, 2019

    The issue is here:

    HAL_CAN_AddTxMessage(m_can, &TxHeader, txData, (uint32_t *)CAN_TX_MAILBOX0);

    CAN_TX_MAILBOX0 will be the value that HAL_CAN_AddTxMessage will return and not an address you give. try:

    uint32_t mail_box;
    HAL_CAN_AddTxMessage(m_can, &TxHeader, txData, (uint32_t *)&mail_box);
    printf("I've used mailbox %d!", mail_box);
    /* if transmission errors/busy bus occured, mail_box could be used with : */
     HAL_CAN_AbortTxRequest (&m_can, mail_box);
     
    /* send another msg */
    // ...

    hope this helps

    NQuoc
    Visitor II
    May 15, 2019

    Hi mh_stm,

    Can you give me full source code?

    1. I had try with: HAL_CAN_AddTxMessage(m_can, &TxHeader, txData, (uint32_t *)&mail_box);

    But it not work.

    I check receive message with: HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0)

    Thank you so much.