• AVR Freaks

Hot!CAN interface does not works properly on XC16 1.4

Author
jmachm
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2019/09/07 02:10:31
  • Location: 0
  • Status: offline
2019/09/09 12:17:11 (permalink)
0

CAN interface does not works properly on XC16 1.4

I have try the CAN module in loopback mode using XC16 ver. 1.4 on dspic33ck32mp505 and I can not receive the response.
The same code compiled with XC16 ver. 1.36 works correctly: 64 bytes are received.
 
 

void ECAN_Initialize(void)
{
    /****************************************************************************
     * Set the PPS
     ***************************************************************************/
    __builtin_write_RPCON(0x0000); // unlock PPS
    RPINR26bits.CAN1RXR = 38; //R6->CAN:RX 15 PGC3/RP38/SCL2/RB6
    RPOR3bits.RP39R = 21; //RB7->CAN:TX 16 TDO/AN2/CMP3A/RP39/SDA3/RB7
    __builtin_write_RPCON(0x0800); // lock PPS
    /* Enable the CANFD module */
    C1CONLbits.CON = 1;
    /* Place CAN module in configuration mode */
    C1CONHbits.REQOP = 4;
    while (C1CONHbits.OPMOD != 4);
    /* Set up the CANFD module for 500Kbps of Nominal bit rate speed and 2Mbps of Data bit rate. */
    C1NBTCFGH = 0x003A;
    C1NBTCFGL = 0x1313;
    C1TDCH = 0x0002; //TDCMOD is Auto
    C1TDCL = 0x0F00;
   
    C1CONLbits.BRSDIS = 0x1;
    C1CONHbits.STEF = 0x0; //Don't save transmitted messages in TEF
    C1CONHbits.TXQEN = 0x0;   
    // FIFO1 as transmiter (1 messages)
    C1FIFOCON1Hbits.FSIZE = 0; //1 messages deep
    C1FIFOCON1Hbits.PLSIZE = 0x7; //64 bytes of data
    C1FIFOCON1Lbits.TXEN = 1; // Set TXEN bit, transmit fifo
    // FIFO2 as receiver (1 messages)
    C1FIFOCON2Hbits.FSIZE = 0; //1 messages deep
    C1FIFOCON2Hbits.PLSIZE = 0x7; //64 bytes of data
    C1FIFOCON2Lbits.TXEN = 0; // Clear TXEN bit, receive fifo   
    // filter 0
    C1FLTCON0Lbits.F0BP = 2; // Store messages in FIFO2
    C1FLTOBJ0H = 0x0000; // Filter 0 ID
    C1FLTOBJ0L = 0x00000; // Filter 0 ID
    C1FLTOBJ0Hbits.EXIDE = 0; // Filter only EID messages
    C1FLTCON0Lbits.FLTEN0 = 1; // Enable the filter
    // mask 0
    C1MASK0H = 0x000; // Ignore all bits in comparison
    C1MASK0L = 0x00000; // Ignore all bits in comparison
    C1MASK0Hbits.MIDE = 0; // Match only message types   
    /* Initialize CAN FIFO message buffer area. */
    C1FIFOBAL = (unsigned int) &CanTxBuffer;
    C1FIFOUA2L = (unsigned int) &CanRxBuffer;
    /* Place the CAN module in external loopback mode. */
    C1CONHbits.REQOP = 5;
    while (C1CONHbits.OPMOD != 5);
}
 
void CAN_ReceiveF(void)
{
    /* Get the address of the message buffer to read the received messages.*/
    /* set UINC bit to update the FIFO tail */
    uint8_t size;
    int i;
    if (C1FIFOSTA2bits.TFNRFNIF != 0)
    {
        printf("\r\nReceived message\r\n");
        rxObj = (CANFD_RX_MSGOBJ *) C1FIFOUA2L;
        size = rxObj->bF.ctrl.DLC;
        switch (size)
        {
            case 9:
                size = 12;
                break;
            case 10:
                size = 16;
                break;
            case 11:
                size = 20;
                break;
            case 12:
                size = 24;
                break;
            case 13:
                size = 32;
                break;
            case 14:
                size = 48;
                break;
            case 15:
                size = 64;
                break;
        }
        char *bufferData = (char*) rxObj + 8;
        for (i = 0; i < size; i++)
        {
            printf("0x%02X ", bufferData);
        }
        printf("\r\n");
        //Process the received messages
        C1FIFOCON2Lbits.UINC = 1; // Update the FIFO message pointer.
    }
}
 
void CAN_transmitF(void)
{
    unsigned char index;
    /* Get the address of the message buffer to write to. Load the buffer and then set the UINC bit.
    Set the TXREQ bit next to send the message. */
    CANFD_TX_MSGOBJ *txObj;
    /* Transmit message 0 from FIFO 1 - CANFD base frame with BRS*/
    txObj = (CANFD_TX_MSGOBJ *) C1FIFOUA1L;
    txObj->bF.id.SID = 0x300;
    txObj->bF.id.EID = 0x0002;
    txObj->bF.ctrl.BRS = 1; //Switch bit rate
    txObj->bF.ctrl.DLC = 0xF; //64 bytes
    txObj->bF.ctrl.FDF = 1; // CANFD frame
    txObj->bF.ctrl.IDE = 1;
    for (index = 0; index < 0x40; index++)
    {
        txObj->byte[index + 8] = index;
    }
    C1FIFOCON1Lbits.UINC = 1; // Set UINC bit
    C1FIFOCON1Lbits.TXREQ = 1; // Set TXREQ bit
}
#1

2 Replies Related Threads

    Wieschebrock
    Super Member
    • Total Posts : 210
    • Reward points : 0
    • Joined: 2005/01/22 07:57:44
    • Location: Germany
    • Status: offline
    Re: CAN interface does not works properly on XC16 1.4 2019/09/16 03:11:17 (permalink)
    0
    You can try to compare the generated assembler code.... 
    There shouldn't be much differences in the generated code so it might push you in the right direction.
    #2
    JPortici
    Super Member
    • Total Posts : 777
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: CAN interface does not works properly on XC16 1.4 2019/09/23 01:24:36 (permalink)
    0
    1) use code tags to post code
    2) weird, as there was an issue i found in 1.36 that prevented me from using bitfields in the can controller on the same core (wrong data was passed so the filters were misconfigured). The bug showed up if i wanted to use optimizations, but it was corrected in 1.40. there is a thread from me here showing the issue in detail (search for bitfields shenanigans).
    3) 1.41 was released soon after as they found out another issue with bitfields and optimizations on the dsPIC33C. Update, retry, report back.
    #3
    Jump to:
    © 2019 APG vNext Commercial Version 4.5