cancel
Showing results for 
Search instead for 
Did you mean: 

CAN messege transmission using HAL LIBRARY

Manoj Hiwarkar
Associate III

Please tell me how to send a CAN message using HAL_CAN_Transmit function.

For ex. if I want send a hex number say 0xFE.

2 REPLIES 2
T J
Lead

i use the cube to initialise all the peripherals with HAL. including CanBus

but it ends there,

I use this to transmit.

if ((CAN->TSR & CAN_TSR_TME0) == CAN_TSR_TME0)  // (1)   fifo0 is empty
 {
    CAN->sTxMailBox[0].TDTR = 8;                 // (2) length
    CAN->sTxMailBox[0].TDLR = 0x11223344;     // (3) 4bytes
    CAN->sTxMailBox[0].TDHR = 0x44556677;   // (3) 4bytes
    CAN->sTxMailBox[0].TIR  = ((uint32_t)TxMsgID << 21 | CAN_TI0R_TXRQ); // (4)
// sent to fifo now,  if the line is idle it will start now
}
    if ((CAN->TSR & CAN_TSR_TME1) == CAN_TSR_TME1)   // (1)   fifo1 is empty
{}
   if ((CAN->TSR & CAN_TSR_TME2) == CAN_TSR_TME2)   // (1)   fifo2 is empty
{}

Perhaps looks at some code examples for the STM32 part you're using?

STM32Cube_FW_L4_V1.12.0\Projects\STM32L476G-EVAL\Examples\CAN\CAN_Networking\Src\main.c

       /* Set the data to be transmitted */

       TxData[0] = 0xFE;

       /* Start the Transmission process */

       if (HAL_CAN_AddTxMessage(&CanHandle, &TxHeader, TxData, &TxMailbox) != HAL_OK)

       {

         /* Transmission request Error */

         Error_Handler();

       }

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..