cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] STM32F042 CAN init problem

zlajoan
Associate II
Posted on March 26, 2016 at 14:40

Hi ST community,

I am working on CAN communication using STM32F042F6P6 (20 pin, PA11/12). During initialization (CAN_Init function), I am receiving INAK_TIMEOUT (stuck in a while loop for cca 25 seconds) and the init status is CAN_InitStatus_Failed.

uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
... 
while (((CANx->MSR & CAN_MSR_INAK) == (uint16_t)CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
{
wait_ack++;
}
/* ...and check acknowledged */
if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
{
InitStatus = CAN_InitStatus_Failed;
}
else
{
InitStatus = CAN_InitStatus_Success ;
}
...

Here is my initialization function:

void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN GPIOs configuration **************************************************/
 /* PA11 and PA12 remap for TSSOP20 packages (only for STM32F042 devices) */
SYSCFG->CFGR1 |= (uint32_t)SYSCFG_CFGR1_PA11_PA12_RMP;
/* Connect CAN pins to AF4 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_4);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_4); 
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
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(GPIOA, &GPIO_InitStructure);
/* NVIC configuration *******************************************************/
NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* CAN configuration ********************************************************/ 
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
// CAN_DBGFreeze(CAN, DISABLE);
/* CAN register init */
CAN_DeInit(CAN);
CAN_StructInit(&CAN_InitStructure);
/* 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_Mode = CAN_Mode_LoopBack;
/* CAN Baudrate = 1MBps (CAN clocked at 36 MHz) */
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;
CAN_Init(CAN, &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 = CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* Enable FIFO 0 message pending Interrupt */
CAN_ITConfig(CAN, CAN_IT_FMP0, ENABLE);
}

It is same as CAN_Networking example for std library. I've changed pin locations and added PA11/12 remap in place of PA9/10 (datasheet Section 4, page 32 and 37). The same problem occurs in loopback mode also. Does anyone have a same problem or solution? Best regards, Zlatko
3 REPLIES 3
Posted on March 26, 2016 at 17:31

You enable GPIOA clock somewhere else?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zlajoan
Associate II
Posted on March 26, 2016 at 18:01

Clive,

I've enabled it before the CAN initialization.

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

Zlatko
zlajoan
Associate II
Posted on March 30, 2016 at 21:35

Hi,

I've found the solution. It is necessary to enable SYSCFG clock in order to remap PA11/12 insted of PA9/

/* PA11 and PA12 remap for TSSOP20 packages (only for STM32F042 devices)*/
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGCOMPEN;
SYSCFG->CFGR1 |= (uint32_t)SYSCFG_CFGR1_PA11_PA12_RMP;

Cheers, Zlatko