cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4X CPU halts after CAN Bus connector unplug

h2399
Associate II
Posted on August 09, 2015 at 14:42

Hi,

I am using CAN bus on STM32F407 using standard peripheral library.

When I unplug CAN bus connector, the CPU halts. After reconnecting CAN bus connector, the CPU is still freezing and needs hardware restart.

I want to microcontroller continues running after the bus disconnected.

I have used debugger and I see the CPU stuck in assert line:

uint8_t

CAN_TransmitStatus

(CAN_TypeDef* CANx, uint8_t TransmitMailbox)

{

 

uint32_t state = 0;

 

/* Check the parameters */

 

assert_param(

IS_CAN_ALL_PERIPH

(CANx));

 

assert_param(

IS_CAN_TRANSMITMAILBOX

(TransmitMailbox));

sticks here!!!!

// ===================================================

CAN_InitTypeDef

       

CAN_InitStructure;

CAN_FilterInitTypeDef

 

CAN_FilterInitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

CanTxMsg TxMessage;

volatile

uint8_t

StatusCAN =

0

;

volatile

uint16_t

CanBusTxFailureCounter =

0x0000

;

volatile

uint16_t

CanBusTxFailureCounterMax =

0xFFFF

;

void

CAN_Config

(void)

{

 

GPIO_InitTypeDef

 

GPIO_InitStructure;

 

  

/* CAN GPIOs configuration **************************************************/

 

/* Enable GPIO clock */

 

RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE);

 

/* Connect CAN pins to AF9 */

 

GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT);

 

GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT);

  

  

/* Configure CAN RX and TX pins */

 

GPIO_InitStructure

.GPIO_Pin

= CAN_RX_PIN | CAN_TX_PIN;

 

GPIO_InitStructure

.GPIO_Mode

= GPIO_Mode_AF;

 

GPIO_InitStructure

.GPIO_Speed

= GPIO_Speed_50MHz;

 

GPIO_InitStructure

.GPIO_OType

= GPIO_OType_PP;

 

GPIO_InitStructure

.GPIO_PuPd

 

= GPIO_PuPd_UP;

 

GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);

 

/* CAN configuration ********************************************************/

 

  

/* Enable CAN clock */

 

RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);

 

  

/* CAN register init */

 

CAN_DeInit(CANx);

 

/* CAN cell init */

 

CAN_InitStructure

.CAN_TTCM

= DISABLE;

 

CAN_InitStructure

.CAN_ABOM

= DISABLE;

 

CAN_InitStructure

.CAN_AWUM

= DISABLE;

 

CAN_InitStructure

.CAN_NART

= DISABLE;

 

CAN_InitStructure

.CAN_RFLM

= DISABLE;

 

CAN_InitStructure

.CAN_TXFP

= DISABLE;

 

CAN_InitStructure

.CAN_Mode

= CAN_Mode_Normal;

 

CAN_InitStructure

.CAN_SJW

= CAN_SJW_1tq;

   

  

/* CAN Baudrate = 500 Kbit/

s

(CAN clocked at 42 MHz) */

    

// CAN_BitRate = CAN_CLK / (CAN_Prescaler * (CAN_SJW + CAN_BS1 + CAN_BS2)) - 500Kbit/s = 42MHz/(4 * (1 + 12 + 8))

 

CAN_InitStructure

.CAN_BS1

= CAN_BS1_12tq;

 

CAN_InitStructure

.CAN_BS2

= CAN_BS2_8tq;

 

CAN_InitStructure

.CAN_Prescaler

= 4;

 

CAN_Init(CANx, &CAN_InitStructure);

 

/* CAN filter init */

 

CAN_FilterInitStructure

.CAN_FilterNumber

= 0;

 

CAN_FilterInitStructure

.CAN_FilterMode

= CAN_FilterMode_IdMask;

 

CAN_FilterInitStructure

.CAN_FilterScale

= CAN_FilterScale_32bit;

 

CAN_FilterInitStructure

.CAN_FilterIdHigh

= 0x0000;

 

CAN_FilterInitStructure

.CAN_FilterIdLow

= 0x0000;

 

CAN_FilterInitStructure

.CAN_FilterMaskIdHigh

= 0x0000;

 

CAN_FilterInitStructure

.CAN_FilterMaskIdLow

= 0x0000;

 

CAN_FilterInitStructure

.CAN_FilterFIFOAssignment

= 0;

 

CAN_FilterInitStructure

.CAN_FilterActivation

= ENABLE;

 

CAN_FilterInit(&CAN_FilterInitStructure);

 

  

/* Transmit Structure preparation */

//

 

TxMessage

.StdId

= 0x300;

 

TxMessage

.ExtId

= 0x01;

 

TxMessage

.RTR

= CAN_RTR_DATA;

 

TxMessage

.IDE

= CAN_ID_STD;

 

TxMessage

.DLC

= 8;

//

 

TxMessage

.Data

[0] = 0x00;

 

  

/* Enable FIFO 0 message pending Interrupt */

 

CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);

   

    

NVIC_InitStructure

.NVIC_IRQChannel

= CAN1_RX0_IRQn;

   

NVIC_InitStructure

.NVIC_IRQChannelPreemptionPriority

= 0x0;

   

NVIC_InitStructure

.NVIC_IRQChannelSubPriority

= 0x1;

   

NVIC_InitStructure

.NVIC_IRQChannelCmd

= ENABLE;

   

NVIC_Init(&NVIC_InitStructure);

}

void

CANBusTaskID3FFh

(void){

   

TxMessage

.StdId

= 0x3FF;

   

TxMessage

.Data

[0] = 0x00;

   

TxMessage

.Data

[1] = 0x00;

   

TxMessage

.Data

[2] = 0x00;

   

TxMessage

.Data

[3] = 0x00;

   

TxMessage

.Data

[4] = (((uint16_t)(CC*80)) & 0xFF00) >> 8; //MSB

   

TxMessage

.Data

[5] = ((uint16_t)(CC*80)) & 0x00FF; //LSB

   

TxMessage

.Data

[6] = (((uint16_t)(DC*50))& 0xFF00) >> 8; //MSB;

   

TxMessage

.Data

[7] = ((uint16_t)(DC*50)) & 0x00FF; //LSB;

   

StatusCAN = CAN_Transmit(CAN1,&TxMessage);

   

CanBusTxFailureCounter = 0;

   

while((

CAN_TransmitStatus

(CAN1, StatusCAN) != CANTXOK) && (CanBusTxFailureCounter != CanBusTxFailureCounterMax) ) // Wait on Transmit

 

{

       

CanBusTxFailureCounter++;

   

}

}

#halt-cpu #can-bus #unplug-connec
2 REPLIES 2
Posted on August 09, 2015 at 16:20

So is it asserting because it sees the wrong/incorrect parameter?

If it is address that to resolve the problem.

You can also turn off the assertion.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
h2399
Associate II
Posted on August 09, 2015 at 21:45

Hi clive,

Thank you for your reply.

As you saw I have the same problem with I2C port.

I didn't change the input type, I just disconnected the CAN bus connector.

I will test it without assert.