cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F429 CAN communication

Sameer Wagh
Associate II
Posted on January 17, 2018 at 13:00

Hello,

   i am using STM32F429IIT controller.I am trying communicate between CAN1 and CAN2 but yet not success.can you please provide any reference code for the CAN communication.

Thanks

#stm32f429-can-communication
10 REPLIES 10
Mark Peter Vargha
Associate III
Posted on January 17, 2018 at 14:04

yet not success

What is the exact problem?

please provide any reference code

Check CubeMX examples repository.

Posted on January 17, 2018 at 14:22

It is as likely that you haven't wired it up appropriately.

Provide context about what you are currently trying, both code and physically connectivity. This will save a lot of time in understanding and explaining the problem.

I have previously posted code for the RedDragon 407 board demonstrating CAN1 to CAN2 comms on the platform.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
T J
Lead
Posted on January 17, 2018 at 21:36

the CAN Rx input pins must be pulled high.

Are you using a driver chip ?

Posted on January 18, 2018 at 04:46

I followed procedure on

https://www.youtube.com/watch?v=GYhsHJi6pKE

 

by this code my program stop in while loop of CAN2_RX

uint8_t CAN2_RX(void)

{

    while(!hcan2.Instance->RF0R & CAN_RF0R_FMP0);//CAN_RF0R_FMP0 FOR 3

          uint8_t RxD=(hcan2.Instance->sFIFOMailBox[0].RDLR)&0xFF;

        hcan2.Instance->RF0R|=CAN_RF0R_RFOM0;//CAN_RF0R_RFOM0 FOR 1<<5

    return RxD;

}

i received only first byte in sFIFOMailBox[0].RDLR i.e RxD.

My physical hardware connection is also ok . i have checked on scope i am receiving data(waveform) on both side

Here is the my code

Header 1

CAN_HandleTypeDef hcan1;

CAN_HandleTypeDef hcan2;

uint8_t k, RxData;void CAN1_TX(uint8_t data)

{

    hcan1.Instance->sTxMailBox[0].TDLR=data;

        hcan1.Instance->sTxMailBox[0].TIR|=1;//CAN_1TI0R_TXRQ FOR 1

}

uint8_t CAN2_RX(void)

{    while(!hcan2.Instance->RF0R & 3);//CAN_RF0R_FMP0 FOR 3

    BMS_MASTER_GREEN_LD_ON;

        uint8_t RxD=(hcan2.Instance->sFIFOMailBox[0].RDLR)&0xFF;

        hcan2.Instance->RF0R|=1<<5;//CAN_RF0R_RFOM0 FOR 1<<5

    return RxD;

}

int main (void){  HAL_Init();

  SystemClock_Config();

  MX_CAN1_Init();

  MX_CAN2_Init();  hcan1.Instance->sTxMailBox[0].TIR=(uint32_t)0;

  hcan1.Instance->sTxMailBox[0].TIR=(uint32_t)(0x245<<21)|CAN_ID_STD;

  hcan1.Instance->sTxMailBox[0].TIR|=CAN_RTR_DATA;

  hcan1.Instance->sTxMailBox[0].TDHR&=~CAN_TDT0R_DLC;

  hcan1.Instance->sTxMailBox[0].TDTR|=(1&CAN_TDT0R_DLC);  hcan1.Instance->FMR|=1<<0;

  hcan1.Instance->FMR|=14<<8;

  hcan1.Instance->FS1R|=1<<14;

  hcan1.Instance->sFilterRegister[14].FR1=0x245<<21;

  hcan1.Instance->FM1R|=(uint32_t)(1<<14);

  hcan1.Instance->FA1R|=1<<14;

  hcan1.Instance->FMR&=~((uint32_t)CAN_FMR_FINIT);  while(1)

 {      k++;

      if(k>25) k=0;

          CAN1_TX(k);

          RxData=CAN2_RX();

      HAL_Delay(1000);

  }

}static void MX_CAN1_Init(void)

{  hcan1.Instance = CAN1;

  hcan1.Init.Prescaler = 16;

  hcan1.Init.Mode = CAN_MODE_NORMAL;

  hcan1.Init.SJW = CAN_SJW_1TQ;

  hcan1.Init.BS1 = CAN_BS1_3TQ;

  hcan1.Init.BS2 = CAN_BS2_5TQ;

  hcan1.Init.TTCM = DISABLE;

  hcan1.Init.ABOM = DISABLE;

  hcan1.Init.AWUM = DISABLE;

  hcan1.Init.NART = DISABLE;

  hcan1.Init.RFLM = DISABLE;

  hcan1.Init.TXFP = DISABLE;

  if (HAL_CAN_Init(&hcan1) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }}/* CAN2 init function */

static void MX_CAN2_Init(void)

{  hcan2.Instance = CAN2;

  hcan2.Init.Prescaler = 16;

  hcan2.Init.Mode = CAN_MODE_NORMAL;

  hcan2.Init.SJW = CAN_SJW_1TQ;

  hcan2.Init.BS1 = CAN_BS1_3TQ;

  hcan2.Init.BS2 = CAN_BS2_5TQ;

  hcan2.Init.TTCM = DISABLE;

  hcan2.Init.ABOM = DISABLE;

  hcan2.Init.AWUM = DISABLE;

  hcan2.Init.NART = DISABLE;

  hcan2.Init.RFLM = DISABLE;

  hcan2.Init.TXFP = DISABLE;

  if (HAL_CAN_Init(&hcan2) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }}
Posted on January 18, 2018 at 04:50

Here is the my can init function

static void MX_CAN1_Init(void)

{

  hcan1.Instance = CAN1;

  hcan1.Init.Prescaler = 16;

  hcan1.Init.Mode = CAN_MODE_NORMAL;

  hcan1.Init.SJW = CAN_SJW_1TQ;

  hcan1.Init.BS1 = CAN_BS1_3TQ;

  hcan1.Init.BS2 = CAN_BS2_5TQ;

  hcan1.Init.TTCM = DISABLE;

  hcan1.Init.ABOM = DISABLE;

  hcan1.Init.AWUM = DISABLE;

  hcan1.Init.NART = DISABLE;

  hcan1.Init.RFLM = DISABLE;

  hcan1.Init.TXFP = DISABLE;

  if (HAL_CAN_Init(&hcan1) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

}

/* CAN2 init function */

static void MX_CAN2_Init(void)

{

  hcan2.Instance = CAN2;

  hcan2.Init.Prescaler = 16;

  hcan2.Init.Mode = CAN_MODE_NORMAL;

  hcan2.Init.SJW = CAN_SJW_1TQ;

  hcan2.Init.BS1 = CAN_BS1_3TQ;

  hcan2.Init.BS2 = CAN_BS2_5TQ;

  hcan2.Init.TTCM = DISABLE;

  hcan2.Init.ABOM = DISABLE;

  hcan2.Init.AWUM = DISABLE;

  hcan2.Init.NART = DISABLE;

  hcan2.Init.RFLM = DISABLE;

  hcan2.Init.TXFP = DISABLE;

  if (HAL_CAN_Init(&hcan2) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

}

/**********************************/

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(hcan->Instance==CAN1)

  {

  /* USER CODE BEGIN CAN1_MspInit 0 */

  /* USER CODE END CAN1_MspInit 0 */

    /* Peripheral clock enable */

    HAL_RCC_CAN1_CLK_ENABLED++;

    if(HAL_RCC_CAN1_CLK_ENABLED==1){

      __HAL_RCC_CAN1_CLK_ENABLE();

    }

 

    /**CAN1 GPIO Configuration    

    PB8     ------> CAN1_RX

    PB9     ------> CAN1_TX

    */

    GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_PULLUP;//GPIO_NOPULL;//

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* USER CODE BEGIN CAN1_MspInit 1 */

  /* USER CODE END CAN1_MspInit 1 */

  }

  else if(hcan->Instance==CAN2)

  {

  /* USER CODE BEGIN CAN2_MspInit 0 */

  /* USER CODE END CAN2_MspInit 0 */

    /* Peripheral clock enable */

    __HAL_RCC_CAN2_CLK_ENABLE();

    HAL_RCC_CAN1_CLK_ENABLED++;

    if(HAL_RCC_CAN1_CLK_ENABLED==1){

      __HAL_RCC_CAN1_CLK_ENABLE();

    }

 

    /**CAN2 GPIO Configuration    

    PB13     ------> CAN2_TX

    PB5     ------> CAN2_RX

    */

    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_5;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* USER CODE BEGIN CAN2_MspInit 1 */

  /* USER CODE END CAN2_MspInit 1 */

  }

}

Sameer Wagh
Associate II
Posted on January 18, 2018 at 18:29

can any one tell what should be wrong with above code..

Posted on January 18, 2018 at 19:42

I think I'd avoid the register level coding until you have the HAL equivalent for RX/TX working.

Look at the available HAL examples, like

STM32Cube_FW_F4_V1.16.0\Projects\STM324xG_EVAL\Examples\CAN\CAN_Networking\Src\main.c

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
T J
Lead
Posted on January 18, 2018 at 23:47

hcan.Init.ABOM = ENABLE; 

Sameer Wagh
Associate II
Posted on January 20, 2018 at 05:37

thank you so much now it's working fine.

can i transmit(initiate) the CAN communication from CAN slave mode port to CAN master port.and how?