cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Receive Error

pradeep swamy
Associate
Posted on March 30, 2018 at 10:25

I'm working on STM32F412VET6 micro, & Configured CAN1 for communication & it is initialized as

500Kbps, 75% Sample, ABP1 is @ 24MHz

void CANInit(void)

{

#ifdef LPBK_TEST

CanTxMsg CanTxMsg;

uint32_t uwCounter = 0;

uint8_t TransmitMailbox = 0;

#endif

NVIC_InitTypeDef NVIC_InitStructure;

// GPIOD->MODER |= 10; //PD1 & PD0 as Alternate function 0b10

// GPIOD->AFR[0] |= 9;

// GPIOD->OTYPER |= 0x0003; //open push-pull configuration

// GPIOD->OSPEEDR |= 0x000F; //high speed

// GPIOD->PUPDR |= 0x0005; //pull up

//

//

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

// /* Enable CAN clock */

// RCC->APB1ENR |= RCC_APB1ENR_CAN1EN;

GPIO_InitTypeDef GPIO_InitStructure;

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

/* Enable GPIO clock */

RCC->AHB1ENR |= RCC_AHB1Periph_GPIOD;

/* Configure CAN RX and TX pins */

GPIO_InitStructure.Pin = GPIO_PIN_0;

GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;

GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

// GPIO_InitStructure.OType = GPIO_OType_PP;

GPIO_InitStructure.Pull = GPIO_PULLUP;

GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_InitStructure.Pin = GPIO_PIN_1;

GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;

GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

// GPIO_InitStructure.OType = GPIO_OType_PP;

GPIO_InitStructure.Pull = GPIO_PULLUP;

GPIO_Init(GPIOD, &GPIO_InitStructure);

/* Connect CAN pins to AF9 */

GPIO_PinAFConfig(GPIOD, GPIO_PIN_0, GPIO_AF9_CAN1);

GPIO_PinAFConfig(GPIOD, GPIO_PIN_1, GPIO_AF9_CAN1);

GPIOE->BSRR = CAN_GPIO_Rx; //GPIOE as High for CAN TxRx to be active

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

/* Enable CAN clock */

RCC->APB1ENR |= RCC_APB1Periph_CAN1;

//Reset CAN1 & release from reset

RCC->APB1RSTR |= RCC_APB1Periph_CAN1;

RCC->APB1RSTR &= ~RCC_APB1Periph_CAN1;

CAN_InitStructure.CAN_TTCM = DISABLE;

CAN_InitStructure.CAN_ABOM = ENABLE;

CAN_InitStructure.CAN_AWUM = DISABLE;

CAN_InitStructure.CAN_NART = ENABLE;

CAN_InitStructure.CAN_RFLM = DISABLE;

CAN_InitStructure.CAN_TXFP = DISABLE;

#ifdef LPBK_TEST

CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack; //CAN_Mode_Normal;

#else

CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

#endif

CAN_InitStructure.CAN_SJW = CAN_SJW_2tq;

/* CAN Baudrate = 500kBps (CAN clocked at 24 MHz) */

CAN_InitStructure.CAN_BS1 = CAN_BS1_11tq;

CAN_InitStructure.CAN_BS2 = CAN_BS2_4tq;

CAN_InitStructure.CAN_Prescaler = 3;

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 = CAN_Filter_FIFO0;

CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);

CAN_ITConfig(CAN1, CAN_IT_FMP1, ENABLE);

CAN_FilterInit(&CAN_FilterInitStructure);

/* Transmit Structure preparation */

for(unsigned char i = 0; i < 10; i++)

{

gsCanTxMsg[i].StdId = 0x050;

gsCanTxMsg[i].ExtId = 0x00;

gsCanTxMsg[i].RTR = CAN_RTR_DATA;

gsCanTxMsg[i].IDE = CAN_ID_STD;

gsCanTxMsg[i].DLC = 8;

gsCanTxMsg[i].Data[0] = 0x01;

gsCanTxMsg[i].Data[1] = 0x02;

gsCanTxMsg[i].Data[2] = 0x03;

gsCanTxMsg[i].Data[3] = 0x04;

gsCanTxMsg[i].Data[4] = 0x05;

gsCanTxMsg[i].Data[5] = 0x06;

gsCanTxMsg[i].Data[6] = 0x07;

gsCanTxMsg[i].Data[7] = 0x08;

}

#ifdef LPBK_TEST

CanTxMsg.StdId = 0x11;

CanTxMsg.RTR = CAN_RTR_DATA;

CanTxMsg.IDE = CAN_ID_STD;

CanTxMsg.DLC = 2;

CanTxMsg.Data[0] = 0xCA;

CanTxMsg.Data[1] = 0xFE;

TransmitMailbox = CAN_Transmit(CAN1, &CanTxMsg);

uwCounter = 0;

while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (uwCounter != 0xFFFF))

{

uwCounter++;

}

uwCounter = 0;

while((CAN_MessagePending(CAN1, CAN_FIFO0) < 1) && (uwCounter != 0xFFFF))

{

uwCounter++;

}

/* receive */

gsCanRxMsg.StdId = 0x00;

gsCanRxMsg.IDE = CAN_ID_STD;

gsCanRxMsg.DLC = 0;

gsCanRxMsg.Data[0] = 0x00;

gsCanRxMsg.Data[1] = 0x00;

CAN_Receive(CAN1, CAN_FIFO0, &gsCanRxMsg);

if (gsCanRxMsg.StdId != 0x11)

{

uwCounter++;

}

uwCounter++;

if (gsCanRxMsg.IDE != CAN_ID_STD)

{

uwCounter++;

}

uwCounter++;

#else

/* Enable FIFO 0 message pending Interrupt */

// CAN_ITConfig(CAN1, CAN_IT_TME, 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);

// NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;

// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;

// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;

// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

// NVIC_Init(&NVIC_InitStructure);

//

// NVIC_InitStructure.NVIC_IRQChannel = CAN1_TX_IRQn;

// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;

// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;

// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

// NVIC_Init(&NVIC_InitStructure);

#endif

}

I'm able to transmit a CAN frame, but when I try sending a CAN frame from CAN analyzer i'm getting receive error

0690X0000060AI0QAM.png

Any idea where am I going wrong.

1 REPLY 1
T J
Lead
Posted on March 31, 2018 at 23:42

I would start with a scope, to check the bit rate first.

can you work on the receiver first, it is the fastest way.

that way you know the bit rate is correct and your code is the problem.