cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to initialize bxCAN controller of STM32F042K6

ZDu
Associate

Hi,

I tried to use the ST driver function HAL_CAN_Init(CAN_HandleTypeDef* hcan) to initialize the CAN controller. Here is the code:

HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef* hcan)

{

 uint32_t status = CAN_INITSTATUS_FAILED; /* Default init status */

 uint32_t tickstart = 0U;

 /* Check CAN handle */

 if(hcan == NULL)

 {

    return HAL_ERROR;

 }

 /* Check the parameters */

 assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));

 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TTCM));

 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ABOM));

 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AWUM));

 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.NART));

 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.RFLM));

 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TXFP));

 assert_param(IS_CAN_MODE(hcan->Init.Mode));

 assert_param(IS_CAN_SJW(hcan->Init.SJW));

 assert_param(IS_CAN_BS1(hcan->Init.BS1));

 assert_param(IS_CAN_BS2(hcan->Init.BS2));

 assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler));

 if(hcan->State == HAL_CAN_STATE_RESET)

 {

   /* Allocate lock resource and initialize it */

   hcan->Lock = HAL_UNLOCKED;

   /* Init the low level hardware */

   HAL_CAN_MspInit(hcan);

 }

 /* Initialize the CAN state*/

 hcan->State = HAL_CAN_STATE_BUSY;

////////////////////////////////////////////////

 /* Exit from sleep mode */

 CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);

 /* Request initialisation */

 SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);

 /* Get tick */

 tickstart = HAL_GetTick();  

 /* Wait the acknowledge */

 while(HAL_IS_BIT_CLR(hcan->Instance->MSR, CAN_MSR_INAK))

 {

   if((HAL_GetTick()-tickstart) > CAN_TIMEOUT_VALUE)

   {

     hcan->State= HAL_CAN_STATE_TIMEOUT;

     /* Process unlocked */

     __HAL_UNLOCK(hcan);

     return HAL_TIMEOUT;

   }

 }

I found I was not able to modify CAN_MCR register. I can't clear SLEEP bit, and can't set INRQ bit. I tried HSI clock and HSE clock.

What could be the reason? Can someone help?

Thanks,

0 REPLIES 0