AnsweredAssumed Answered

STM32F4 Discovery. CAN communication FAIL. HAL_TIMEOUT.

Question asked by LorenzoSTM32 on Sep 22, 2015
Latest reply on Sep 22, 2015 by peacock.jack.003
Hello everyone, this is my first post in the forum, but I read a great deal of threads about CAN communication between PC and STM32 Discovery using HAL Libraries.
The problem, basically, is that I'm unable to send/receive CAN frames. The configuration of the peripheral is pretty standard, and my code looks right, but when I try to send a message through HAL_CAN_Transmit I get an HAL_TIMEOUT state.
Can someone help me? I'm pretty stuck!


CAN_HandleTypeDef     CAN_HandleStruc;

void CAN1_Init()
{
     //Struct for GPIO and CAN initialization
     GPIO_InitTypeDef     GPIO_InitStruc;
     CAN_FilterConfTypeDef     CAN_FilterConfStruc; 
     
     static CanRxMsgTypeDef msgRX;
     static CanTxMsgTypeDef msgTX;
     
     
     
     //Enable GPIOD clock
     __HAL_RCC_CAN1_CLK_ENABLE();
     __GPIOD_CLK_ENABLE();
     
     GPIO_InitStruc.Pin = CAN_RX_PIN | CAN_TX_PIN;
     GPIO_InitStruc.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruc.Speed = GPIO_SPEED_HIGH; //100 Mhz
     GPIO_InitStruc.Pull = GPIO_PULLUP;
     GPIO_InitStruc.Alternate = GPIO_AF9_CAN1;
     HAL_GPIO_Init(CAN_GPIO_PORT,&GPIO_InitStruc);
     
     /*##1 Configure the CAN peripheral #########################*/
     // CAN Baudrate (1Mbps) = 168MHz/[(SJW+BS1+BS2)*Prescaler]
     
     HAL_CAN_DeInit(&CAN_HandleStruc);
     
     CAN_HandleStruc.Instance = CAN1;
     CAN_HandleStruc.pTxMsg = &msgTX;
     CAN_HandleStruc.pRxMsg = &msgRX;
     
     CAN_HandleStruc.Init.TTCM = DISABLE;
     CAN_HandleStruc.Init.ABOM = DISABLE;
     CAN_HandleStruc.Init.AWUM = DISABLE;
     CAN_HandleStruc.Init.NART = DISABLE;
     CAN_HandleStruc.Init.RFLM = DISABLE;
     CAN_HandleStruc.Init.TXFP = DISABLE;
     CAN_HandleStruc.Init.Mode = CAN_MODE_NORMAL;
     CAN_HandleStruc.Init.SJW = CAN_SJW_1TQ; 
     CAN_HandleStruc.Init.BS1 = CAN_BS1_14TQ;
     CAN_HandleStruc.Init.BS2 = CAN_BS2_6TQ;
     CAN_HandleStruc.Init.Prescaler = 8; 
     
     
     if(HAL_CAN_Init(&CAN_HandleStruc) != HAL_OK)
          Error_Handler();
     
     /*##2 Configure the CAN filter #########################*/
     CAN_FilterConfStruc.FilterNumber = 0;
     CAN_FilterConfStruc.FilterMode = CAN_FILTERMODE_IDMASK;
     CAN_FilterConfStruc.FilterScale = CAN_FILTERSCALE_32BIT;
     CAN_FilterConfStruc.FilterIdHigh = 0x0000;
     CAN_FilterConfStruc.FilterIdLow = 0x0000;
     CAN_FilterConfStruc.FilterMaskIdHigh = 0x0000;
     CAN_FilterConfStruc.FilterMaskIdLow = 0x0000;
     CAN_FilterConfStruc.FilterFIFOAssignment = CAN_FILTER_FIFO0;
     CAN_FilterConfStruc.FilterActivation = ENABLE;
     //CAN_FilterConfStruc.BankNumber = 14;
     
     HAL_CAN_ConfigFilter(&CAN_HandleStruc,&CAN_FilterConfStruc);
     
     //Enable CAN1 RX Interrupt
     HAL_NVIC_SetPriority(CAN1_RX0_IRQn,0,0); //Priority and Sub Priority
     HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
     
     
     
     //Enable FIFO0 pending interrupt
     __HAL_CAN_ENABLE_IT(&CAN_HandleStruc,CAN_IT_FMP0);
     
     /*##3 Configure Transmission process #########################*/
     CAN_HandleStruc.pTxMsg->StdId = 0x1;
     CAN_HandleStruc.pTxMsg->ExtId = 0x0;
     CAN_HandleStruc.pTxMsg->RTR = CAN_RTR_DATA;
     CAN_HandleStruc.pTxMsg->IDE = CAN_ID_STD;
     CAN_HandleStruc.pTxMsg->DLC = 2;
     CAN_HandleStruc.pTxMsg->Data[0] = 0x01;
    CAN_HandleStruc.pTxMsg->Data[1] = 0x01;
     
     
     //Below the function that return an HAL_TIMEOUT!!!!!!
     HAL_CAN_Transmit(&CAN_HandleStruc, 10);     
}


I should write down the RX interrupt, but meanwhile I'm trying to get Polling mode working..with no result so far.

Outcomes