AnsweredAssumed Answered

STM32f042 CAN Tx issue

Question asked by reddy.shyam on Mar 20, 2017
Latest reply on Mar 21, 2017 by reddy.shyam

stm32f042can

stm32 can

can init

can bus

can code

 

Hi,

      I am trying out CAN communication with STM32F042 Nucleo dev board. I am getting passive error all the time on Tx message. When i try to debug found last_error as passive and Tx_message status as pending. I dint think there is problem in the CAN initialization, my code is attached below. I am out of my thinking. I would be thankful if i can get some suggestions on this.

 

int main(void)
{
CanTxMsg TxMessage;
CanRxMsg RxMessage;
static unsigned int TxMailBox=0, Status =0, ErrStatus=0, RxErrCnt=0;

/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f0xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file
*/
RCC_init(); //Clock Setting

if (SysTick_Config(SystemCoreClock / 1000))
{
/* Capture error */
while (1);
}
/* CAN configuration */
CAN_Bus_Initialization();
/* Infinite loop */
while(1)
{
// CAN_SetSpeed(25 );
if(TickCont > 1000)
{
TickCont=0;
TxMessage.IDE = CAN_ID_STD;
TxMessage.StdId = 0x07;
TxMessage.ExtId = 0x00;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.DLC = 1;
TxMessage.Data[0] = 5;
if(TxMailBox != 4)
{
TxMailBox = CAN_Transmit(CANx, &TxMessage);
}
Status=CAN_TransmitStatus(CANx,TxMailBox);
// RxMessage.IDE = 0x00;
// RxMessage.StdId = CAN_ID_STD;
// RxMessage.ExtId = 0x00;
// RxMessage.RTR = CAN_RTR_DATA;
// RxMessage.DLC = 0;
// RxMessage.Data[0] = 0;
// CAN_Receive(CANx, CAN_FIFO0, &RxMessage);
ErrStatus=CAN_GetLastErrorCode(CAN);
// RxErrCnt = CAN_GetReceiveErrorCounter(CAN);
if(((CANx->ESR) & (uint8_t)CAN_ESR_EPVF))
{
ErrStatus |= CAN_ESR_EPVF;
}
else
{
ErrStatus &= ~CAN_ESR_EPVF;
}
if(((CANx->ESR) & (uint8_t)CAN_ESR_BOFF))
{
ErrStatus |= CAN_ESR_BOFF;
}
else
{
ErrStatus &= ~CAN_ESR_BOFF;
}
// Status=CAN_TransmitStatus(CANx,CAN_Transmit(CANx, &TxMessage));
// TxErrCnt = CAN_GetLSBTransmitErrorCounter(CAN);

}
}
}

//CanTxMsg TxMessage = {0};
void CAN_Bus_Initialization()
{
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGCOMPEN;
SYSCFG->CFGR1 |= SYSCFG_CFGR1_PA11_PA12_RMP;
CAN_Config();
}
/**
* @brief Configures the CAN.
* @param None
* @retval None
*/
static void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;

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

/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(CAN_GPIO_CLK, ENABLE);

/* Connect CAN pins to AF7 */
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN1_RX_PIN_Source, CAN_AF_PORT );
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN1_TX_PIN_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);

/* NVIC configuration *******************************************************/
// NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);

/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);

/* CAN register init */
CAN_DeInit(CANx);
CAN_StructInit(&CAN_InitStructure);

/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = ENABLE;
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 = 1MBps (CAN clocked at 48 MHz) */
#if 1 // Sample-Pint : 50%
CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;
#else // Sample-Pint : 87.5%
CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;// ErrStatus=CAN_GetLastErrorCode(CAN);
CAN_InitStructure.CAN_Prescaler = 12;

#endif
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);

// /* CAN FIFO0 message pending interrupt enable */
// CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
/* Enable FIFO 0 message pending Interrupt */
// CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
}

 

in regards,

Sundar.

Outcomes