cancel
Showing results for 
Search instead for 
Did you mean: 

CAN initialisation problem on the connectivity line (STM32F107VC)

auto
Associate II
Posted on December 10, 2009 at 08:57

CAN initialisation problem on the connectivity line (STM32F107VC)

9 REPLIES 9
auto
Associate II
Posted on May 17, 2011 at 13:33

Hello,

I am trying to setup the CAN1 module for the STM32F107VC with the library functions (v3.1.2) but I

always get ''InitStatus = CANINITFAILED;'' in the ''stm32f10x_can.c''.

Code:

/*stm32f10x_can.c*/

/* ... */

/*uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)*/

/* ... */

/* Request leave initialisation */

CANx->MCR &= ~MCR_INRQ;

/* Wait the acknowledge */

wait_ack = 0x00;

while (((CANx->MSR & MSR_INAK) == MSR_INAK) && (wait_ack != INAK_TimeOut))

{

wait_ack++;

}

/* ...and check acknowledged */

if ((CANx->MSR & MSR_INAK) == MSR_INAK) /*!!! ALWAYS GET CANINITFAILED !!!*/

{

InitStatus = CANINITFAILED;

}

else

{

InitStatus = CANINITOK ;

}

}

/* At this step, return the status of initialization */

return InitStatus;

}

/* ... */

My function for initialising looks like this:

Code:

static void sv_INIT_init_CAN(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

CAN_InitTypeDef CAN_InitStructure;

CAN_FilterInitTypeDef CAN_FilterInitStructure;

/*CAN: Configuration start*/

/* Enable GPIO and AFIO clocks for CAN */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);

/*CAN remap to PD0, PD1*/

GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE);

/* CAN1 Periph clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

/* Configure CAN pin: RX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_Init(GPIOD, &GPIO_InitStructure);

/* Configure CAN pin: TX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOD, &GPIO_InitStructure);

/*CAN: Configuration end*/

/* CAN register init */

CAN_DeInit(CAN1);

CAN_StructInit(&CAN_InitStructure);

/* CAN cell init */

/*500 kbit/s @ 72MHz=HCLK & 72MHz/2=PCLK1*/

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_InitStructure.CAN_BS1 = CAN_BS1_15tq;

CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;

CAN_InitStructure.CAN_Prescaler = 4;

CAN_Init(CAN1, &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);

}/*sv_INIT_init_CAN()*/

I don't know, what I'm doing wrong.

Thanks,

auto.response

armmcu
Associate II
Posted on May 17, 2011 at 13:33

Before going in details,

is the problem perist when using older library verions(v3.0.0,v3.1.0..)?

tomas23
Associate II
Posted on May 17, 2011 at 13:33

Don't you have error state on your CAN lines?

auto
Associate II
Posted on May 17, 2011 at 13:33

I get the same result with v3.1.0 (my oldest version before v2.x.x).

armmcu
Associate II
Posted on May 17, 2011 at 13:33

Hi auto.response,

I geuss it's an optimization/timing problem impacting the exit instruction from the intialization mode.

Please change your compiler'optimization (try all levels) and let me know if this can impact.

Cheers,

Armmcu.

dschuethe9
Associate II
Posted on May 17, 2011 at 13:33

Hi, i have had the same problem in medium density device. But it works when i don't use these functions:

/*CAN remap to PD0, PD1*/

GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE);

/* Configure CAN pin: RX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_Init(GPIOD, &GPIO_InitStructure);

/* Configure CAN pin: TX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOD, &GPIO_InitStructure);

/*CAN: Configuration end*/

null

I am just writing directly the bit frame into the dedicated register, like

*(uint32_t *) address = 0x....

and this works fine. Maybe you can try it.

armmcu
Associate II
Posted on May 17, 2011 at 13:33

Hi all,

I think I found the root case:

well as I’ve thought before it’s a timing problem :-W

As it’s mentioned in STM32 documentation the state of the CAN “Tx�? pin is highly related to the CAN mode.

“While in Initialization Mode, all message transfers to and from the CAN bus are stopped and the status of the CAN bus output CANTX is recessive (high).�?

auto.response,

as I can see in your “TX�? pin config the GPIO slew rate is not defined.

So please do it: your config will be as follow:

/* Configure CAN pin: TX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOD, &GPIO_InitStructure);

Please let me know if this solves your issue.

Cheers,

Armmcu.

auto
Associate II
Posted on May 17, 2011 at 13:33

[Topic solved]

Thanks to all, especially armmcu.engineer.

After I added the GPIO_Speed to the TX configuration it works.

Here the corrected function:

Code:

static void sv_INIT_init_CAN(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

CAN_InitTypeDef CAN_InitStructure;

CAN_FilterInitTypeDef CAN_FilterInitStructure;

/*CAN: Configuration start*/

/* Enable GPIO and AFIO clocks for CAN */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);

/*CAN remap to PD0, PD1*/

GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE);

/* CAN1 Periph clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

/* Configure CAN pin: RX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_Init(GPIOD, &GPIO_InitStructure);

/* Configure CAN pin: TX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //here was my mistake

GPIO_Init(GPIOD, &GPIO_InitStructure);

/*CAN: Configuration end*/

/* CAN register init */

CAN_DeInit(CAN1);

CAN_StructInit(&CAN_InitStructure);

/* CAN cell init */

/*500 kbit/s @ 72MHz=HCLK & 72MHz/2=PCLK1*/

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_InitStructure.CAN_BS1 = CAN_BS1_15tq;

CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;

CAN_InitStructure.CAN_Prescaler = 4;

CAN_Init(CAN1, &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);

}/*sv_INIT_init_CAN()*/

Thanks,

auto.response

armmcu
Associate II
Posted on May 17, 2011 at 13:33

Good news 😉