cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_CAN_AddTxMessage() issue

Dick Lin
Senior
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.
14 REPLIES 14
Posted on June 19, 2018 at 01:37

There are 2 issue I found 

1. the bxCAN is in initialization mode since I didn't call HAL_CAN_Start() right. Used to have issue with tick so I commented it out.

2. The HAL_GPIO_Init() function seems dropped the CAN_TX voltage from 3.3 to 2.8. So I commented following code out.

/* Check the Speed parameter */

assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));

/* Configure the IO Speed */

temp = GPIOx->OSPEEDR;

temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2));

temp |= (GPIO_Init->Speed << (position * 2));

GPIOx->OSPEEDR = temp;

Now my issue became the PEAK doesn't get the ACK from STM32. Not sure where to setup it up in STM32.

Thx

Posted on June 20, 2018 at 18:39

I finally have it working by setting the clock to 80 MHz and set the bit timing as following. Thx

500 Kbps

CanHandle.Init.SJW = CAN_SJW_1TQ;

CanHandle.Init.BS1 = CAN_BS1_4TQ;

CanHandle.Init.BS2 = CAN_BS2_5TQ;

CanHandle.Init.Prescaler = 16;

Bus clock 80 MHz

80,000,000 / (16 * (1+4+5))

80,000,000 / (16 * 10)

80,000,000 / 160 = 500k

mh_stm
Associate II

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

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.