cancel
Showing results for 
Search instead for 
Did you mean: 

CAN NOT WORKING

digildavidpeyad
Associate II
Posted on March 09, 2016 at 11:55

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

        }

    //}

}

  
6 REPLIES 6
Posted on March 09, 2016 at 13:33

Is there anything else on the bus acting as a receiver?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
digildavidpeyad
Associate II
Posted on March 09, 2016 at 13:46

Yes I use a CAN MATE which will receive CAN message and monitor it in PC using CAN MATE pc application 

petr239955
Associate III
Posted on March 09, 2016 at 14:59

Hi,

do you use

any termination

resistor ? Please check also listen only mode

of CAN MATE, if it is possible for this adapter.

Petr
Posted on March 09, 2016 at 15:00

Can you provide the defines that you are using? And diagram the external components and connectivity.

Does the transmit work in loop-back mode?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
AvaTar
Lead
Posted on March 09, 2016 at 16:01

> Yes I use a CAN MATE which will receive CAN message and monitor it in PC using CAN MATE pc application

 

If this is just a passive listener, IMHO it won't help you much.

Like clive suggested, you can try direct loopback.

digildavidpeyad
Associate II