AnsweredAssumed Answered

CAN NOT WORKING

Question asked by david.digil.001 on Mar 9, 2016
Latest reply on Mar 10, 2016 by david.digil.001
Hi,
   I have successfully configure CAN2 in STM32F429ZI discovery board. But when I am trying to transmit message it goes to transmit pending state. I have configure the baudrate of 500k at PCLK1 frequency 45MHz. I also share my code below. Please anyone help me to find problem.

static void CAN_Config(void)
{
      GPIO_InitTypeDef GPIO_InitStructureCAN_RX;
   GPIO_InitTypeDef GPIO_InitStructureCAN_TX;


      CAN_InitTypeDef CAN_InitStructure ;
     
      /* Enable the GPIO Clock */
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE); //CAN2 Peripharal clock 
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
     
       GPIO_PinAFConfig(CAN2_Port, GPIO_PinSource12, GPIO_AF_CAN2); //CAN_RX = PB12
    GPIO_PinAFConfig(CAN2_Port, GPIO_PinSource13, GPIO_AF_CAN2); //CAN_TX = PB13
     
  /* Configure the GPIO_CAN_Rx pin */
          GPIO_InitStructureCAN_RX.GPIO_Pin      = CAN2_Rx;
    GPIO_InitStructureCAN_RX.GPIO_Mode      = GPIO_Mode_AF;
    GPIO_InitStructureCAN_TX.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructureCAN_TX.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructureCAN_RX.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(CAN2_Port, &GPIO_InitStructureCAN_RX);
     
      /* GPIO CAN_TX Configuration */
    GPIO_InitStructureCAN_TX.GPIO_Pin      = CAN2_Tx;
    GPIO_InitStructureCAN_TX.GPIO_Mode      = GPIO_Mode_AF;
    GPIO_InitStructureCAN_TX.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructureCAN_TX.GPIO_PuPd      = GPIO_PuPd_NOPULL;
          //GPIO_InitStructureCAN_TX.GPIO_PuPd      = GPIO_PuPd_UP;
    GPIO_InitStructureCAN_TX.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(CAN2_Port, &GPIO_InitStructureCAN_TX);
          
          
     
           CAN_DeInit(CAN2);
//CAN_FilterConfiguration();
    /* CAN2 configuration */
    CAN_InitStructure.CAN_TTCM = DISABLE; // Time-triggered communication mode = DISABLED
    CAN_InitStructure.CAN_ABOM = DISABLE; // Automatic bus-off management mode = DISABLED
    CAN_InitStructure.CAN_AWUM = DISABLE; // Automatic wake-up mode = DISABLED
    CAN_InitStructure.CAN_NART = DISABLE; // Non-automatic retransmission mode = DISABLED
    CAN_InitStructure.CAN_RFLM = DISABLE; // Receive FIFO locked mode = DISABLED
    CAN_InitStructure.CAN_TXFP = DISABLE; // Transmit FIFO priority = DISABLED
    CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; // Normal CAN mode
          //CAN_InitStructure.CAN_Mode = CAN_Mode_Silent;
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // Synchronization jump width = 1
    CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq; //14
    CAN_InitStructure.CAN_BS2 = CAN_BS2_4tq;  //6


    CAN_InitStructure.CAN_Prescaler = 5;      // Baudrate 500 kbps
    //CAN_InitStructure.CAN_Prescaler = 16;  // Baudrate 125 kbps
          //x=CAN_Init( CAN2, &CAN_InitStructure);
          
          if(CAN_Init( CAN2, &CAN_InitStructure))
          {
           /* Initialize LEDs mounted on EVAL board */
               STM_EVAL_LEDInit(LED3);
            STM_EVAL_LEDOn(LED3);
          }
               if(!CAN_Init( CAN2, &CAN_InitStructure))while(1);
}


static void CAN_FilterConfiguration(void) {
    CAN_FilterInitTypeDef CAN_FilterInitStructure;


    /* CAN2 filter configuration */
    CAN_FilterInitStructure.CAN_FilterNumber = 0; // Filter number = 0 (0<=x<=13)
    CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // Filter mode = identifier mask based filtering
    CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
    CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0300 << 5; //0x0000;
    CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x03FF << 5;
    CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // FIFO = 0
    CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
    CAN_FilterInit(&CAN_FilterInitStructure);
}


static void CAN_TxMessage(void) {
    CanTxMsg TxMessage;


    /* CAN message to send */
    TxMessage.StdId = 0x321;
    TxMessage.ExtId = 0x01;
    TxMessage.RTR = CAN_RTR_DATA;
    TxMessage.IDE = CAN_ID_STD;
    TxMessage.DLC = 8;
    TxMessage.Data[0] = 0x04;
    TxMessage.Data[1] = 0x01;
    TxMessage.Data[2] = 0x00;
    TxMessage.Data[3] = 0x00;
    TxMessage.Data[4] = 0x00;
    TxMessage.Data[5] = 0x00;
    TxMessage.Data[6] = 0x00;
    TxMessage.Data[7] = 0x00;


    //while (1) {
        
        CAN_Transmit(CAN2, &TxMessage);
                    //CAN_TransmitStatus(CAN2, 0);
                    
        if(CAN_TransmitStatus(CAN2, 0) == CAN_TxStatus_Ok )
                    {
            STM_EVAL_LEDInit(LED4); // Initialize and
            STM_EVAL_LEDOn(LED4);   // turn ON green LED if transmit was successful
        }
    //}
}

  

Outcomes