Skip to main content
andy2399
Senior
April 15, 2018
Question

Problem Sending CAN Message

  • April 15, 2018
  • 25 replies
  • 8259 views
Posted on April 15, 2018 at 23:27

I am following the example code found here to send and receive CAN messages:

STM32Cube_FW_F4_V1.21.0\Projects\STM324xG_EVAL\Examples\CAN\CAN_Networking\Src

I can happily receive CAN messages but when I call the following code, the CAN message is never sent.

HAL_CAN_AddTxMessage( &hcan1, &TxHeader, TxData, &TxMailbox );

while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) != 3) {}

TxHeader is configured as:

TxHeader.StdId = 0x123;

TxHeader.ExtId = 0x01;

TxHeader.RTR = CAN_RTR_DATA;

TxHeader.IDE = CAN_ID_STD;

TxHeader.DLC = 7;

TxHeader.TransmitGlobalTime = DISABLE;

And TxData is filled with seven bytes of data.

HAL_CAN_GetTxMailboxesFreeLevel always returns 2 and I don't see the CAN message on the bus.

Any help or idea would be gratefully appreciated.

Andy

    This topic has been closed for replies.

    25 replies

    T J
    Senior III
    January 29, 2019
    Please tell me the values of:

    CAN_TDH0R_DATA7_Pos
    CAN_TDH0R_DATA6_Pos
    CAN_TDH0R_DATA5_Pos
    CAN_TDH0R_DATA4_Pos
    CAN_TDL0R_DATA3_Pos
    CAN_TDL0R_DATA2_Pos
    CAN_TDL0R_DATA1_Pos
    CAN_TDL0R_DATA0_Pos



    ((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos)
    kgedi
    Associate
    January 29, 2019

    CAN_TDH0R_DATA7_Pos=0x00000018

    ​

    CAN_TDH0R_DATA6_Pos=0x00000010

    ​

    CAN_TDH0R_DATA5_Pos=0x00000008

    ​

    CAN_TDH0R_DATA4_Pos=0x00000000

    ​

    CAN_TDL0R_DATA3_Pos=0x00000018

    ​

    CAN_TDL0R_DATA2_Pos=0x00000010

    ​

    CAN_TDL0R_DATA1_Pos=0x00000008

    ​

    CAN_TDL0R_DATA0_Pos=0x00000000

    T J
    Senior III
    January 29, 2019
    Are you sending packets ?
    But the data is incorrect ?
    Do you have a scope to see the CanBus wires are actually transmitting a packet ?

    If you are not sure about this snippet, check it… can you single step ?

    uint32_t CheckData = ((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) |
    ((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) |
    ((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) |
    ((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos);

    Do you have a Transmitter and a slave ? You need both for the CanBus to operate.
    Do you have a known good transmitter, and you can receive packets ?, this is the best starting point.
    I used a CanDo unit. Very easy to start that way.
    kgedi
    Associate
    January 29, 2019

    Thank you so much for your answers ..

    I have no scope to watch bus lines.

    When I debug the receive side received value before and after start the system (always) equals to 0x00000001 and some times ​it takes 0x00000004. But there is no error.(it reads 8 bytes rxData and all of them equals to 0)

    I use mcp2551 and Two stm32f407vg if you want to check i can upload the code here (the code is stm32cube's example just can filter configurations are mine).

    ​And also i dont want to steal your time i always feel guilty about "time" when i ask for help thank you again .

    T J
    Senior III
    January 29, 2019
    Do you have a known good transmitter on the bus ?
    You really need a scope…

    You must have a known good transmitter and a receiver set to the correct bit rate to make the CanBus operate correctly.
    You can see that easily on the scope.

    You missed step 1 of 1000 steps.
    gkema
    Associate
    February 14, 2019

    I finally borrow a oscilloscope sorry for delay

    when only transmitter side on the bus no another node

    by using this script

    if ( ( hcan1.Instance->TSR & CAN_TSR_TME0 ) == CAN_TSR_TME0 ) // (1)
     
     
     
    {
     
     
    hcan1.Instance->sTxMailBox[ 0 ].TDTR = 8;
     
     
    hcan1.Instance->sTxMailBox[ 0 ].TDLR = 0x55555555;
     
     
    hcan1.Instance->sTxMailBox[ 0 ].TDHR = 0x55555555;
     
     
    hcan1.Instance->sTxMailBox[ 0 ].TIR = ( ( uint32_t )0x123 << 21 | CAN_TI0R_TXRQ );
     
     
    }

    i get

    0 001 0010 0011 0001 000 0101 0101 0101 0101 0101 0101 0101 0101  0101 0101 0101 0101 0101 0101 0101 0101 110010101111101111111 

    when two node on the bus with this script

    if ( ( hcan1.Instance->TSR & CAN_TSR_TME0 ) == CAN_TSR_TME0 ) // (1)
     
     
     
    {
     
     
    hcan1.Instance->sTxMailBox[ 0 ].TDTR = 8;
     
     
    hcan1.Instance->sTxMailBox[ 0 ].TDLR = 0x22222222;
     
     
    hcan1.Instance->sTxMailBox[ 0 ].TDHR = 0x22222222;
     
     
    hcan1.Instance->sTxMailBox[ 0 ].TIR = ( ( uint32_t )0x123 << 21 | CAN_TI0R_TXRQ );
     
     
    }

    i get

    0 001 0010 0011 0001 000 001000110011001000010111001100011 0010 0010 0010 0010 0010 0010 0010 0010 11010001100111111

    thank you. Best Regards.

    T J
    Senior III
    February 14, 2019
    You have to receive first, then work on transmit.
    Do you have a known good dongle ?
    Send packets every 1second and work on the receiver.
    Then you can work on transmitter
    SUrte.1
    Associate II
    August 24, 2022

    Hi,

    Did you solve the transmit error caused by not receiving ACK? If you solved it, can you please tell me your solution, I am using the same transciever and stm card as you