cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Problem STM32F4

Rui Loureiro
Associate II
Posted on March 30, 2017 at 13:09

I'm having a hard time getting my STM32F4 discovery board to communicate with my CAN bus. I've successfully implemented a communication between CAN1 and CAN2 on the same Discovery Board, with a transceiver on each CAN port.

However, when I try to connect it to my CAN bus, I get HAL_TIMEOUT in the function HAL_CAN_Transmit();

I have dspic30f's that communicate flawlessly with my CAN bus, so I matched the STM32F4 Discovery bitrate to the pics, aswell as the relations between SJW, BS1 and BS2.

I'm only trying to transmit a CAN message, for the time being.

I've also tried to read the TX pin with a logic analyser. It detects the message but can't decode it properly.

Any help will be greatly appreciated. 

Here's my code:

MX_GPIO_Init();

static

CanTxMsgTypeDef

        TxMessage1;

 

static

CanRxMsgTypeDef

        RxMessage1;

 

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

  CanHandle1.

Instance

= CAN1;

  CanHandle1.

pTxMsg

= &TxMessage1;

  CanHandle1.

pRxMsg

= &RxMessage1;

CAN_FilterConfTypeDef

  sFilterConfig1;

 

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

  CanHandle1.

Init

.

TTCM

=

DISABLE

;

  CanHandle1.

Init

.

ABOM

=

DISABLE

;

  CanHandle1.

Init

.

AWUM

=

DISABLE

;

  CanHandle1.

Init

.

NART

=

DISABLE

;

  CanHandle1.

Init

.

RFLM

=

DISABLE

;

  CanHandle1.

Init

.

TXFP

=

DISABLE

;

  CanHandle1.

Init

.

Mode

= CAN_MODE_NORMAL;

  CanHandle1.

Init

.

SJW

= CAN_SJW_1TQ;

  CanHandle1.

Init

.

BS1

= CAN_BS1_2TQ;

  CanHandle1.

Init

.

BS2

= CAN_BS2_1TQ;

  CanHandle1.

Init

.

Prescaler

= 11;

 

if

(HAL_CAN_Init(&CanHandle1) !=

HAL_OK

)

  {

   

/* Initialization Error */

    Error_Handler();

  }

 

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

  sFilterConfig1.

FilterNumber

= 1;

  sFilterConfig1.

FilterMode

= CAN_FILTERMODE_IDMASK;

  sFilterConfig1.

FilterScale

= CAN_FILTERSCALE_32BIT;

  sFilterConfig1.

FilterIdHigh

= 0x0000;

  sFilterConfig1.

FilterIdLow

= 0x0000;

  sFilterConfig1.

FilterMaskIdHigh

= 0x0000;

  sFilterConfig1.

FilterMaskIdLow

= 0x0000;

  sFilterConfig1.

FilterFIFOAssignment

= 0;

  sFilterConfig1.

FilterActivation

= 1;

  sFilterConfig1.

BankNumber

= 0;

 

if

(HAL_CAN_ConfigFilter(&CanHandle1, &sFilterConfig1) !=

HAL_OK

)

  {

    Error_Handler();

  }

  CanHandle1.

pTxMsg

->

StdId

= 0x11;

  CanHandle1.

pTxMsg

->

RTR

= CAN_RTR_DATA;

  CanHandle1.

pTxMsg

->

IDE

= CAN_ID_STD;

  CanHandle1.

pTxMsg

->

DLC

= 2;

  CanHandle1.

pTxMsg

->

Data

[0] = 0xCA;

  CanHandle1.

pTxMsg

->

Data

[1] = 0xFE;

 

// Infinite loop

 

while

(1)

    {

   

if

(HAL_CAN_Transmit(&CanHandle1, 10) !=

HAL_OK

)

    {

     

/* Transmission Error */

      Error_Handler();

    }

   

if

(HAL_CAN_GetState(&CanHandle1) !=

HAL_CAN_STATE_READY

)

    {

      Error_Handler();

    }

     HAL_Delay(10);

    }

}

#stm32f4-discovery #controller-area-network #can
1 REPLY 1
Rui Loureiro
Associate II
Posted on April 03, 2017 at 16:03

It's working now. I had to match the exact time quantas of all the microcontrollers in the can bus.