AnsweredAssumed Answered

BF609 CAN Interface

Question asked by KAUSTUBH_IRDE on Nov 24, 2015
Latest reply on Dec 21, 2015 by KAUSTUBH_IRDE

hi all

 

i have configured CAN interface on BF609 processor. as per hardware reference manual it is looking ok, but it is not communicating. communication means, first i am trying only data transmit on CAN interface. configuration is like that after transmit, it should generate transmit interrupt.

but this is not working.

i have attached configuration code with CAN transmit and transmit interrupt service routine function.

 

bool Init_CANPort(int config)

{

    int i = 0,j = 0;

    unsigned int nTimer;

    short msgID;

    char mbID;

    volatile unsigned short *pCAN_Ptr;

 

    *pREG_SEC0_CPMSK0 = 0x30;

    *pREG_SEC0_SCTL0 = 0x00003005;

 

    // PortG MUX and FER registers are configured (PG4=CAN RX, PG1=CAN TX)

    /* PORTG_MUX = 0x0000 0208

       PORTG_FER = 0x0012

    */

    *pREG_PORTG_MUX &= ~(BITM_PORT_MUX_MUX1 | BITM_PORT_MUX_MUX4);   

    *pREG_PORTG_MUX |= ((( 2 << BITP_PORT_MUX_MUX1) & BITM_PORT_MUX_MUX1) | (( 2 << BITP_PORT_MUX_MUX4) & BITM_PORT_MUX_MUX4));   

 

    *pREG_PORTG_FER_SET  =    ( BITM_PORT_FER_PX1 | BITM_PORT_FER_PX4 );

    ssync();

 

    // Initialize the CAN interrupts

    if(config & CAN_RX)

        adi_int_InstallHandler(INTR_CAN0_RX, (ADI_INT_HANDLER_PTR)CAN_RX_ISR, NULL, false);

 

    if(config & CAN_TX)

        adi_int_InstallHandler(INTR_CAN0_TX, (ADI_INT_HANDLER_PTR)CAN_TX_ISR, NULL, false);

 

    //adi_int_InstallHandler(INTR_CAN0_STAT, (ADI_INT_HANDLER_PTR)CAN_ERR_ISR, NULL, false);

 

    /* Initialize the CAN timing registers and can clock

    SCLK = 125MHZ, CAN Clock = 500kHz (so tBIT = 2us)

    CAN_CLOCK  : Prescaler (BRP)

    CAN_TIMING : SJW = 3, TSEG2 = 5, TSEG1 = 2

    */

 

    *pREG_CAN0_TIMING = 0x0352;    // try 0x0325 // 0x352

    *pREG_CAN0_CLK  = 24;    // its value depends upon CANCLK and SCLK

    ssync();

 

    // Initialize the CAN mail boxes and turn on the can ISR.

    /* Mailboxes 16-31 are TX mailbox (Mailbox 16 Transmits ID 0x200) */

    msgID = 0x200;

    for (mbID = 16; mbID<32; mbID++)

    {

        pCAN_Ptr = (unsigned short *)(pREG_CAN0_MB00_DATA0 + (0x10 * mbID));

 

        *(pCAN_Ptr + 0)  = msgID;         /* write msgID to DATA0 */

        *(pCAN_Ptr + 2)  = msgID;         /* write msgID to DATA1 */

        *(pCAN_Ptr + 4)  = msgID;         /* write msgID to DATA2 */

        *(pCAN_Ptr + 6)  = msgID;         /* write msgID to DATA3 */

        *(pCAN_Ptr + 8)  = 8;             /* write 8 to LENGTH */

        *(pCAN_Ptr + 10) = 0;             /* write 0 to TIMESTAMP */

        *(pCAN_Ptr + 12) = 0;             /* write 0 to ID0 */

        *(pCAN_Ptr + 14) = (msgID << 2);     /* write msgID to ID1.BASEID */

 

        msgID++;                /* Increment msgID for next mailbox */

    } /* end for (Initialize TX mailbox area) */

 

    /* Mailboxes 0-15 are RX mailbox (Mailbox 0 Receives ID 0x200) */

    msgID = 0x200;

    for (mbID = 0; mbID<16; mbID++)

    {

        pCAN_Ptr = (unsigned short *)(pREG_CAN0_MB00_DATA0 + (0x10 * mbID));

 

        *(pCAN_Ptr + 0)  = 0;              /* write msgID to DATA0 */

        *(pCAN_Ptr + 2)  = 0;             /* write msgID to DATA1 */

        *(pCAN_Ptr + 4)  = 0;             /* write msgID to DATA2 */

        *(pCAN_Ptr + 6)  = 0;             /* write msgID to DATA3 */

        *(pCAN_Ptr + 8)  = 8;             /* write 8 to LENGTH */

        *(pCAN_Ptr + 10) = 0;             /* write 0 to TIMESTAMP */

        *(pCAN_Ptr + 12) = 0;             /* write 0 to ID0 */

        *(pCAN_Ptr + 14) = (msgID << 2);     /* write msgID to ID1.BASEID */

 

        msgID ++;                /* Increment msgID for next mailbox */

    } /* end for (Initialize RX mailbox area) */

 

    /* enable the CAN and the mailboxes */

    *pREG_CAN0_MD1 = CAN_RX_MB_LO;        /* mailboxes 0-15 are configured as Receiver */

    *pREG_CAN0_MD2 = CAN_RX_MB_HI;        /* mailboxes 16-31 are configured as Transmitter */

 

    /* Enable All Mailboxes */

    *pREG_CAN0_MC1 = CAN_EN_MB_LO;         /* mailboxes 0-15 */

    *pREG_CAN0_MC2 = CAN_EN_MB_HI;         /* mailboxes 16-31 */

    ssync();

 

    // Enable CAN Configuration Mode (Clear CCR)

    /* Exit CAN Configuration, Suspend and sleep mode and then wait for CAN to exit all mode */

    *pREG_CAN0_CTL = 0;

 

    /* Wait for CAN Configuration Acknowledge (CCA) */

    for(i=0;i<65535;i++);

 

    do

    {

        asm("nop;");

        for(j=0;j<65535;j++);

    }while(*pREG_CAN0_STAT & BITP_CAN_STAT_CCA);

 

 

    *pREG_CAN0_MBIM1 = CAN_EN_MB_LO;    /* Enable Interrupt for Mailboxes 0-15 */

    *pREG_CAN0_MBIM2 = CAN_EN_MB_HI;    /* Enable Interrupt for Mailboxes 16-31 */

    ssync();

 

    // Enable CAN Receive, CAN Transmit Interrupt

    if(config & CAN_RX)

        adi_int_EnableInt (INTR_CAN0_RX, true);

 

    if(config & CAN_TX)

        adi_int_EnableInt (INTR_CAN0_TX, true);

 

    //adi_int_EnableInt (INTR_CAN0_STAT, true);

 

    return true;

 

} /* End Init_Port () */

 

 

void CAN_TX_ISR(void)

{

    unsigned short mbint_status;

    unsigned short mask = 1;

    char mailbox = 0;

 

    mbint_status = *pREG_CAN0_MBTIF2;

 

    while(mbint_status != 0)

    {

        if(mbint_status & mask)

        {

            TX_CANPort.mbTx_Status[mailbox] = true;

            mbint_status &= ~mask;

            *pREG_CAN0_MBTIF2 = mask;    //w1c

            *pREG_CAN0_TA2    = mask;    //w1c

            //ssync();

        }

        mask = mask << 1;

        mailbox++;

    }// while(mbint_status != 0)

   

    ssync();

    return;

}

 

void CANPort_Transmit(CAN_TxPORT *canPort)

{

    int mbID = 0, j = 0;

    volatile unsigned short *pCAN_Ptr;

 

    mbID = canPort->baseMB;

   

    for(; mbID<32; mbID++)

    {

        pCAN_Ptr = (unsigned short *)(pREG_CAN0_MB00_DATA0 + (0x10 * mbID));

       

        *(pCAN_Ptr + 6) = canPort->CAN_TxBuffer[j++];

        *(pCAN_Ptr + 4) = canPort->CAN_TxBuffer[j++];

        *(pCAN_Ptr + 2) = canPort->CAN_TxBuffer[j++];

        *(pCAN_Ptr + 0) = canPort->CAN_TxBuffer[j++];

    }

   

    *pREG_CAN0_TRS1 = 0x0000;

    *pREG_CAN0_TRS2 = 0xFFFF;

    ssync();

   

    return;

}

Outcomes