• AVR Freaks

dspic33E can bus to send longer messages

Author
Javier Lopez
Pulsotron designer
  • Total Posts : 67
  • Reward points : 0
  • Joined: 2019/02/27 09:59:32
  • Location: Spain
  • Status: offline
2019/06/04 23:47:37 (permalink)
0

dspic33E can bus to send longer messages

Actually I have the following routines that sends 8 byte data very well at 250 and 500khz, but I have to send 12 16 bit words telemetries (24 bytes) every 2 milliseconds.
void Ecan1WriteMessage_frec( void )
{
    uint16_t ww1=(uint16_t) (dcvp.Wc1*10.f), ww2=(uint16_t) (dcvp.Wc2*10.f),wwout=(uint16_t) (dcvp.Wout*10.f), kk=(uint16_t) (dcvp.K*100.f);//RPM*10,K*100
    Ecan1WriteTxMsgBufId( 0, 0x1C1, 1, 0 );
    Ecan1WriteTxMsgBufData( 0, 8, ww1, ww2, wwout, kk );
    C1TR01CONbits.TXREQ0 = 1;
}
//That uses:
void Ecan1WriteTxMsgBufData( uint16_t buf, uint16_t dataLength, uint16_t data1, uint16_t data2, uint16_t data3,
                             uint16_t data4 )
{
    ecan1msgBuf[buf][2] = ( (ecan1msgBuf[buf][2] & 0xFFF0) + dataLength );

    ecan1msgBuf[buf][3] = data1;
    ecan1msgBuf[buf][4] = data2;
    ecan1msgBuf[buf][5] = data3;
    ecan1msgBuf[buf][6] = data4;
}

//and::
// Define ECAN Message Buffers
ECAN1MSGBUF ecan1msgBuf __attribute__( (aligned(ECAN1_MSG_BUF_LENGTH * 16)) );
//ECAN1MSGBUF ecan1msgBuf __attribute__((space(dma),aligned(NUM_OF_ECAN_BUFFERS*16)));

 
I suppose I have to increase the DMA size also but I do not know it very well. Here is my configuration of DMA:
 
void DMA0Init( void )
{
    DMAPWC = 0;
    DMARQC = 0;
    DMA0CON = 0x2020;
    DMA0PAD = ( int ) &C1TXD; /* ECAN 1 (C1TXD) */
    DMA0CNT = 0x0007;
    DMA0REQ = 0x0046; /* ECAN 1 Transmit */

    DMA0STAL = (uint16_t)(&ecan1msgBuf);
    DMA0STAH = 0;

    DMA0CONbits.CHEN = 1;
}

void DMA2Init( void )
{
    DMAPWC = 0;
    DMARQC = 0;
    DMA2CON = 0x0020;
    DMA2PAD = (volatile unsigned int) &C1RXD; /* ECAN 1 (C1RXD) */
    DMA2CNT = 0x0007;
    DMA2REQ = 0b00100010; /* ECAN 1 Receive */

    DMA2STAL = __builtin_dmaoffset(&ecan1msgBuf);
    DMA2STAH = 0x0000;

    DMA2CONbits.CHEN = 1;
}

void Ecan1ClkInit( int can_speed )
{
    /* FCAN is selected to be FCY */
    C1CTRL1bits.CANCKS = 0x0;

    /* Bit Time = (Sync Segment + Propagation Delay + Phase Segment 1 + Phase Segment 2) = 14*TQ
       Phase Segment 1 = 6TQ
       Phase Segment 2 = 6Tq
       Propagation Delay = 1Tq
       Sync Segment = 1TQ
       CiCFG1<BRP> =(FCAN /(2 × N × FBAUD))– 1 */

    C1CFG1 = 0x04; //BRP TQ = (2 x 5)/FCAN; SJW 1 x TQ;
    if (can_speed==500)
        C1CFG2 = 0x1A8; //500khz: NTQ=20
    else
        C1CFG2 = 0x4BD; // 250kHz WAKFIL disabled; SEG2PHTS Freely programmable; SEG2PH 5 x TQ; SEG1PH 8 x TQ; PRSEG 6 x TQ; SAM Once at the sample point;

    /*if (can_speed==500)
    {
        C1CFG1 = 0x04; //BRP TQ = (2 x 5 x NTQ)/FCAN; SJW 1 x TQ; frec=50MHZ/(2*5*NTQ)=500khz
        C1CFG2 = 0x1A0; //500khz: SYNC=1TQ SEG1=6TQ, SEG2PH=2TQ PROP=1TQ: NTQ=suma=10TQ. porcentaje=SYNC+PSEG1+PROP/NTQ=80%
    }
    else
    {
        C1CFG1 = 0x04; //BRP TQ = (2 x 5)/FCAN; SJW 1 x TQ; frec=50MHZ/(2*5*NTQ)=250khz
        C1CFG2 = 0x4BD; //250khz!!:SYNC=1 SEG1PH 8 x TQ; SEG2PH 5 x TQ; PRSEG 6 x TQ; NTQ=20 porcentaje=SYNC+PSEG1+PROP/NTQ=75%
        //
        //C1CFG1 = 0x09; //BRP TQ = (2 x 10)/FCAN; SJW 1 x TQ; frec=50MHZ/(2*10*NTQ)=250khz
        //C1CFG2 = 0x1A0; //500khz: SYNC=1TQ SEG1=6TQ, SEG2PH=2TQ PROP=1TQ: NTQ=suma=10TQ. porcentaje=SYNC+PSEG1+PROP/NTQ=80%
         
    }*/
}

void Ecan1Init( int canspeed )
{
    /* Request Configuration Mode */
    C1CTRL1bits.REQOP = 4;
    while( C1CTRL1bits.OPMODE != 4 );

    Ecan1ClkInit(canspeed);

    C1FCTRLbits.FSA = 2; /* FIFO starts from buffer 2 */
    C1FCTRLbits.DMABS = 0b000; /* 4 CAN Message Buffers in DMA RAM */
    
    /* Filter Configuration

    Ecan1WriteRxAcptFilter(int n, long identifier, unsigned int exide, unsigned int bufPnt, unsigned int maskSel)

    n = 0 to 15 -> Filter number
    identifier -> SID <10:0> : EID <17:0>
    exide = 0 -> Match messages with standard identifier addresses
    exide = 1 -> Match messages with extended identifier addresses
    bufPnt = 0 to 14 -> RX Buffer 0 to 14
    bufPnt = 15 -> RX FIFO Buffer

    maskSel = 0 -> Acceptance Mask 0 register contains mask
    maskSel = 1 -> Acceptance Mask 1 register contains mask
    maskSel = 2 -> Acceptance Mask 2 register contains mask
    maskSel = 3 -> No Mask Selection */
    
    //Ecan1WriteRxAcptMask( 1, 0x1FFFFF00, 1, 1 );//any 1FFFFFxx extended
    int32_t id1=0x1A0,mask1=0x1FFFFFF0; //solo mensajes 1Ax (hex))
    Ecan1WriteRxAcptFilter( 1, id1, 0, 1, 1 );//standard. 0x1FF

    /* Mask Configuration
     void Ecan1WriteRxAcptMask( int16_t m, int32_t identifier, uint16_t mide, uint16_t exide )

    m-> Mask number [0-2]
    identifier -> SID <10:0> : EID <17:0>

    mide = 0 -> Match either standard or extended address message if filters match
    mide = 1 -> Match only message types that correspond to 'exide' bit in filter

    exide = 0 -> Match messages with standard identifier addresses
    exide = 1 -> Match messages with extended identifier addresses */
    
     Ecan1WriteRxAcptMask( 1, mask1, 1, 0 );

    /* Enter Normal Mode */
    C1CTRL1bits.REQOP = 0;
    while( C1CTRL1bits.OPMODE != 0 );

    /* ECAN transmit/receive message control */
    C1RXFUL1 = C1RXFUL2 = C1RXOVF1 = C1RXOVF2 = 0x0000;
    C1TR01CONbits.TXEN0 = 1; /* ECAN1, Buffer 0 is a Transmit Buffer */
    C1TR01CONbits.TXEN1 = 0; /* ECAN1, Buffer 1 is a Receive Buffer */
    C1TR01CONbits.TX0PRI = 0b11; /* Message Buffer 0 Priority Level */
    C1TR01CONbits.TX1PRI = 0b11; /* Message Buffer 1 Priority Level */

    /* Setup I/O pins */
    /* The PPS configuration varies from device to device. Refer the datasheet of the device being used and
       use the appropriate values for the RPINR/RPOR registers. */
    //RPOR10bits.RP48R = 14; /* Can 1 Tx = RP48, Pin 6 */
    //RPINR26bits.C1RXR = 61; /* Can 1 Rx = RP61, Pin 7 */
    
    RPOR1bits.RP36R = 0x000E; //RB4->ECAN1:C1TX
   // RPINR3bits.T2CKR = 0x0032; //RC2->TMR2:T2CK
    RPINR26bits.C1RXR = 0x0018; //RA8->ECAN1:C1RX CAN RX a pin RPI24!
    
}

 
The question is if it is possible to send longer messages using that software and if the can driver allow it.
My micro is a dspic33EP128GM604
 
best regards
#1
Jim Nickerson
User 452
  • Total Posts : 6736
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: dspic33E can bus to send longer messages 2019/06/05 07:55:41 (permalink)
0
8 is the limit per message.
#2
Jump to:
© 2020 APG vNext Commercial Version 4.5