AnsweredAssumed Answered

STM32F446RE CAN in LOOPBACK MODE

Question asked by Ramesh Nayak on May 10, 2018
Latest reply on May 18, 2018 by T J

Hello Everyone, 

I want to test the CAN1 peripheral in loop back mode. please suggest me the required steps . 

I am using STM32F446RE nucleo board and currently i want to test without transceivers 

So far i have done this . 

/* CAN1 init function */
static void MX_CAN1_Init(void)
{

hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 16;
hcan1.Init.Mode = CAN_MODE_LOOPBACK;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = ENABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = DISABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

}

 

/*##-2- Configure the CAN Filter ###########################################*/
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;

if(HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}

 

uint8_t aData[]= {0xA5,0X55};
uint8_t RxData[8];
CAN_TxHeaderTypeDef tXheader;
CAN_RxHeaderTypeDef RxHeader;
tXheader.StdId = 0x11;
tXheader.ExtId = 0;
tXheader.IDE = CAN_ID_STD;
tXheader.RTR = CAN_RTR_DATA;
tXheader.TransmitGlobalTime = DISABLE;
tXheader.DLC = 2;
HAL_StatusTypeDef Status;

/*##-3- Start the CAN peripheral ###########################################*/
if (HAL_CAN_Start(&hcan1) != HAL_OK)
{
/* Start Error */
Error_Handler();
}

Status = HAL_CAN_AddTxMessage(&hcan1,&tXheader,aData,&TxMailbox);
if(Status != HAL_OK)
{
Error_Handler();
}

/* Wait transmission complete */
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) != 3) {}


/*##-5- Start the Reception process ########################################*/
if(HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0) != 1)
{
/* Reception Missing */
Error_Handler();
}

 

Start the Reception process : This function is entering in to error_handler 

Could you please help to solve this issue ?

Thank you !

Outcomes