cancel
Showing results for 
Search instead for 
Did you mean: 

HAL CAN Bus Transmission Problem

ninad911
Associate III
Posted on September 08, 2016 at 16:57

Hello,

       I am trying to send 1000 values from data stored in DMA on to CAN Bus. i am using the Data frame(8 bits each) of CAN to transmit data but i am not able to receive all the data frames. i am not able to understand what is the problem . i am using HAL Library and STM32F4_Discovery board. Have attached the CAN related code.

uint16_t IT_buffer[1000]; // buffer storing 1000 values

/*CAN Transmit*/

void CAN_Tx(void)

{

volatile int j=0;

volatile int b=0;

for(j=0;j<250;j++)  // each time 4values out of 1000values are send therefore 1000/4=250

   {

    //♯♯-3- Start the Transmission process ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯

    CanHandle.pTxMsg->StdId = 0x123;

    CanHandle.pTxMsg->RTR = CAN_RTR_DATA;

    CanHandle.pTxMsg->IDE = CAN_ID_STD;

    CanHandle.pTxMsg->DLC = 8;

    CanHandle.pTxMsg->Data[0] = (IT_buffer[b] >> 8);  

    CanHandle.pTxMsg->Data[1] = (IT_buffer[b]);

    CanHandle.pTxMsg->Data[2] = (IT_buffer[b+1] >> 8);  

    CanHandle.pTxMsg->Data[3] = (IT_buffer[b+1]);

    CanHandle.pTxMsg->Data[4] = (IT_buffer[b+2] >> 8);

    CanHandle.pTxMsg->Data[5] = (IT_buffer[b+2]);

    CanHandle.pTxMsg->Data[6] = (IT_buffer[b+3] >> 8);

    CanHandle.pTxMsg->Data[7] = (IT_buffer[b+3]);

    

    TransmitMailbox = HAL_CAN_Transmit(&CanHandle, 30);

    b=b+4;

    }

}

/* CAN1 init function */

void MX_CAN1_Init(void)

{

HAL_CAN_DeInit(&CanHandle);

static CanTxMsgTypeDef        TxMessage;

static CanRxMsgTypeDef        RxMessage;

/*♯♯-1- Configure the CAN peripheral ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

 /*Baurd rate defined at 1Mhz */

 CanHandle.Instance = CAN1;

 CanHandle.pTxMsg = &TxMessage;

 CanHandle.pRxMsg = &RxMessage;

 CanHandle.Init.TTCM = DISABLE;

 CanHandle.Init.ABOM = DISABLE;

 CanHandle.Init.AWUM = DISABLE;

 CanHandle.Init.NART = DISABLE;

 CanHandle.Init.RFLM = DISABLE;

 CanHandle.Init.TXFP = DISABLE;

 CanHandle.Init.Mode = CAN_MODE_NORMAL;

 /* CAN Baudrate = 175kbps (CAN clocked at 42 MHz) */

 CanHandle.Init.SJW = CAN_SJW_1TQ;

 CanHandle.Init.BS1 = CAN_BS1_6TQ;

 CanHandle.Init.BS2 = CAN_BS2_8TQ;

 CanHandle.Init.Prescaler = 16;

 HAL_CAN_Init(&CanHandle);

}

/*CAN1 filter function*/

void CAN_filter_init(void)

{

CAN_FilterConfTypeDef  sFilterConfig;

 /*♯♯-2- Configure the CAN Filter ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

sFilterConfig.FilterNumber = 0;

    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

    sFilterConfig.FilterIdHigh = 0x0000;//0x100 << 5;;

    sFilterConfig.FilterIdLow = 0x0000;

    sFilterConfig.FilterMaskIdHigh = 0x0000;//0x700 << 5;

    sFilterConfig.FilterMaskIdLow = 0x0000;

    sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;

    sFilterConfig.FilterActivation = ENABLE;

    sFilterConfig.BankNumber =14;

    HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig);

}

can any one help me please!!!

#stm32 #hal #can #cubemx #canbus
2 REPLIES 2
christoph2399
Associate II
Posted on September 09, 2016 at 13:48

the only thing 175kbits are very uncommon for a can-bus (at least in the automotive)

100 - 125- 250 - 500

if everything is ok with your baudrate and samplepointer @ 75-85%  check your network bus-termination 60ohms for a highspeed network between can-high and can-low

Walid FTITI_O
Senior II
Posted on September 09, 2016 at 15:45

Hi saraf.ninad.001,

Which pins you are using (TX/RX). How you have configured the GPIOs ? Share also your main() function to see how you manage your application and how Transmit/receive functions are called.

-Hannibal-