• AVR Freaks

CAN receive data not present in ecan1RxBuf variable

Author
rajatkalyan
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2018/01/17 21:30:45
  • Location: 0
  • Status: offline
2018/02/28 01:12:39 (permalink)
0

CAN receive data not present in ecan1RxBuf variable

Hi everyone
 
I have been trying to receive the data from arduino uno to my dspic33ep32gp502.
I have configured the filters correctly and the data is present in the message assembly buffer,but somehow,the data from the message assembly buffer is not getting into the DMA configured variable "ecan1RxBuf".
ACK is also coming ,so baud rates are configured correctly. 
Its urgent ,kindly help me out of this. 
 
 
Here's my code 
// DSPIC33EP32GP502 Configuration Bit Settings

// 'C' source line config statements

/* FICD */
#pragma config ICS = PGD3 /* ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3) */
#pragma config JTAGEN = OFF /* JTAG Enable bit (JTAG is disabled) */

/* FPOR */
#pragma config ALTI2C1 = ON /* Alternate I2C1 pins (I2C1 mapped to ASDA1/ASCL1 pins) */
#pragma config ALTI2C2 = OFF /* Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins) */
#pragma config WDTWIN = WIN25 /* Watchdog Window Select bits (WDT Window is 25% of WDT period) */

/* FWDT */
#pragma config WDTPOST = PS32768 /* Watchdog Timer Postscaler bits (1:32,768) */
#pragma config WDTPRE = PR128 /* Watchdog Timer Prescaler bit (1:128) */
#pragma config PLLKEN = ON /* PLL Lock Enable bit (Clock switch will not wait for the PLL lock signal.) */
#pragma config WINDIS = OFF /* Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode) */
#pragma config FWDTEN = OFF /* Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software) */

/* FOSC */
#pragma config POSCMD = XT /* Primary Oscillator Mode Select bits (Primary Oscillator disabled) */
#pragma config OSCIOFNC = OFF /* OSC2 Pin Function bit (OSC2 is clock output) */
#pragma config IOL1WAY = OFF /* Peripheral pin select configuration (Allow only one reconfiguration) */
#pragma config FCKSM = CSECMD /* Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled) */

/* FOSCSEL */
#pragma config FNOSC = PRI /* Oscillator Source Selection (Internal Fast RC (FRCPLL)) */
#pragma config IESO = OFF /* Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source) */

/* FGS */
#pragma config GWRP = OFF /* General Segment Write-Protect bit (General Segment may be written) */
#pragma config GCP = OFF /* General Segment Code-Protect bit (General Segment Code protect is Disabled) */


/* #pragma config statements should precede project file includes. */
/* Use project enums instead of #define for ON and OFF. */

#include <xc.h>
#include "CAN_interra.h"

volatile unsigned int ecan1TxBuf[NUM_OF_ECAN_BUFFERS][8]
__attribute__((aligned(NUM_OF_ECAN_BUFFERS * 16)));

 volatile unsigned int ecan1RxBuf[NUM_OF_ECAN_BUFFERS][8]
__attribute__((aligned(NUM_OF_ECAN_BUFFERS * 16)));
 
 unsigned char z;
#define led LATBbits.LATB13
int count=0;
void delay(int value);
void timer_enable(void);
int main (void)
{
     timer_enable();
    IO_init();
    Can_Init();
    delay(1000);
    TRISB=0x0000; // All port pins as an output
    //led=1;
    
    while(1)
    {
    //delay(1000);
        
    /* Message was received. */
    while (C1RXFUL1bits.RXFUL10 == 0); ///this means "While the buffer is empty stay here and if it gets full ,get out of it and clear the rxful10 status register"
    C1RXFUL1bits.RXFUL10 = 0;
    
   /// while(DMALCAbits.LSTCH==0b0010); ///Check if last transfer was handled by dma2(RX DMA)
    if(ecan1RxBuf[3][0]!=0)
    {
    delay(300);
    led=1; //toggle LED
    delay(300);
    led=0; //toggle LED
    }
    }
            
}

void timer_enable(void)
{
    T1CON = 0x0000; // Stops the Timer1,Continue timer operation in Idle mode,Reset T1CON register,
    TMR1 = 0x0000; //Clear contents of the timer register
    
    PR1 =0x9C40; //Load the Period register with the value for 1ms delay
    IEC0bits.T1IE=1; //Set Timer1 interrupt
    IPC0= 0x7000; // desired priority level
    IFS0bits.T1IF=0 ; //Clear the Timer1 interrupt status flag
    T1CON= 0x8000; //Start Timer1 with prescaler settings 1:1, and clock source : Internal
                                    // clock (FOSC/4),Gated time accumulation disabled

}
void delay(int value)
{
    count=0;
    while(count!=value);
    
    
    
}




void IO_init(void)
{
  RCONbits.SWDTEN = 0; /* Disable the watchdog */
  AD1CON1bits.ADON=0;
  TRISBbits.TRISB13=0; /* Set the pin as output*/
// CAN RX Pin
  ANSELBbits.ANSB2=0; /* Set analog pin to digital */
  ANSELB = 0x00;
  TRISBbits.TRISB2 =1; /* Set can rx pin as input pin */
  LATBbits.LATB2=1;
    
 // CAN TX Pin
 
  ANSELBbits.ANSB3=0; /* Set analog pin to digital */
  TRISBbits.TRISB3 =0; /* Set this pin as output */
  LATBbits.LATB3=0;
  
 
 
}

void LED(char a)
{
    LATBbits.LATB13=a;
    
}

void Can_Init(void)
{
   
    
   OSCCONL=0x46; /* Load OSCCON Register with the required values(Procedure to unlock or lock IO ,PASS 0X46 ,PASS 0X57 AND THEN SET OR CLEAR THE IOLOCK BIT) */
  OSCCONL=0x57;
  OSCCONL=OSCCONL & 0x17; /* Unlock the registers by clearing the 6th bit of OSCCON register (IOLOCK) */

    RPINR26=0b00100010; /* RPINR 26 is the register that controls the Pin42 which is RX */
  // RPINR26 = 0x22;
    RPOR2bits.RP39R =0b001110; /* RPOR2 controls pin39 and pin40 lower 8 bits for pin39 and upper 8 bits for pin 40 */

    
    
  OSCCONL=0x46; /* Load the required values in OSCCON registers(Procedure to unlock or lock IO ,LOAD OSCCON WITH 0X46 ,LOAD OSCCON WITH 0X57 AND THEN SET OR CLEAR THE IOLOCK BIT) */
 OSCCONL=0x57;
  OSCCONL=OSCCONL | 0x40 ; /* LOCK THE REGISTER */

  
  
    PLLFBDbits.PLLDIV = 38; /* M = 40 */
    CLKDIVbits.PLLPOST = 0; /* N1 = 2 */
    CLKDIVbits.PLLPRE = 0; /* N2 = 2 */

    
    __builtin_write_OSCCONH(0x03);
    __builtin_write_OSCCONL(OSCCON | 0x01);
   // OSCCONH=0x01;
    //OSCCONL=OSCCON | 0x01;
    while (OSCCONbits.COSC!= 0b011); /* WAIT FOR CLOCK SWITCH TO OCCUR */

    /* Wait for PLL to lock */
    while (OSCCONbits.LOCK!= 1);

 
    
    C1CTRL1bits.WIN = 0;
    C1CTRL1bits.REQOP=0b100; /* Requesting configuration mode */
    while(C1CTRL1bits.OPMODE!=4); /* stay here until it is in configuration mode */

    C1CTRL1bits.WIN = 0;
   
    
   // C1CFG1=0x47;
   // C1CFG2=0x2D2;
   // C1FCTRL=0xC01F;
    
  // disabled to try above values ///
    C1CTRL1bits.CANCAP=0; /* Disables CAN capture */
    C1CTRL1bits.CSIDL=0; /* Continue operation in idle mode */
    C1CTRL1bits.ABAT=0; /* Do not abort pending transmissions */
    C1CTRL1bits.CANCKS=0; /* fcan=fp, 0=fp, 1=2xfp */

    C1CFG1bits.SJW=0x3;
    C1CFG1bits.BRP=BRP_VAL;
    
    C1CFG2bits.SEG1PH=0x7;
    C1CFG2bits.SEG2PHTS=0x1;
    C1CFG2bits.SEG2PH=0x5;
    C1CFG2bits.PRSEG=0x4;
    C1CFG2bits.SAM=0x1;
    
    C1FCTRLbits.DMABS = 0x4; // 4 CAN messages to be buffered in DMA RAM
    C1FCTRLbits.FSA = 1; // FIFO starts at RB1
    
   
    
  // C1CTRL1bits.REQOP = 0b000;
   // while(C1CTRL1bits.OPMODE != 0b000);
      
   
    
  //C1CTRL1bits.REQOP = 0b000;
     //while(C1CTRL1bits.OPMODE != 0b000);
    
    
        // Clear all interrupt bits
     DMA_Config();
     Can_Rx_Filter();
    C1TR01CONbits.TXEN0 = 1; /* ECAN1, Buffer 0 is a Transmit Buffer */
    
    C1TR01CONbits.TX0PRI = 0b11; /* Message Buffer 0 Priority Level */
     C1CTRL1bits.REQOP = 0b000;
     while(C1CTRL1bits.OPMODE != 0b000);
     
     // Enable ECAN1 Interrupt
    IEC2bits.C1IE = 1; // CAN interrupt enable
    C1INTEbits.TBIE = 1; // Tx interrupt enable
    
    
}



void DMA_Config(void)
{
    /* DMA0 configured for can transmission */
    /* DMA1 configured for can reception */
    
    DMA0CONbits.AMODE=0b10; /* Peripheral Indirect Addressing Mode */
    DMA2CONbits.AMODE=0b10;
    
    DMA0CONbits.SIZE=0; /* Word Size data transfer */
    DMA2CONbits.SIZE=0;
     
    DMA0CONbits.DIR=1; /* Writes data to the Peripheral */
    DMA2CONbits.DIR=0;

    /* Operating Mode: Continuous, Ping-Pong modes disabled */
    DMA0CONbits.MODE = 0x00;
    DMA2CONbits.MODE=0x00;

    /* Set Number of DMA Transfer per ECAN message to 8 words */
    DMA0CNT = 7;
    DMA2CNT=7;

    /* Set up the address of the peripheral ECAN1 (C1TXD) */
    DMA0PAD = (volatile unsigned int)&C1TXD;
    DMA2PAD=(volatile unsigned int)&C1RXD;


    /* Automatic DMA TX initiation by DMA request */
    DMA0REQ = 0x0046;
    DMA2REQ=0x0022;
    
    //DMA0STAL = __builtin_dmaoffset(&ecan1TxBuf);
    //DMA0STAH = __builtin_dmaoffset(&ecan1TxBuf);
    DMA0STAL = (unsigned int)&ecan1TxBuf;
    DMA0STAH = (unsigned int)&ecan1TxBuf;
    
    DMA2STAL = (unsigned int)&ecan1RxBuf;
    //DMA1STAH = (unsigned int)&ecan1RxBuf;
    DMA2STAH=0x00;
    DMAPWC=0; /* CLEAR WRITE COLLISION REGSITER*/
    DMARQC=0; /*CLEAR REQUEST COLLISION REGSITER*/
            
    
    
    DMA0CONbits.CHEN=1; /* Channel 0 enabled */
    DMA2CONbits.CHEN=1;
    
    //MA0IF=1; /* DMA channel 1 interrupt flag clears */
    
    IFS0bits.DMA0IF=0;
}



char Can_Tx(char msg,int t1,int t2,int h1,int h2)
{
    
    unsigned int x;
    
    x=0;
    x=msg;
    x=x<<2;
    ecan1TxBuf[0][0] = x;
    ecan1TxBuf[0][1] = 0x0000; /* WORD 1 CONTAINS EID WHICH IS 0 FOR STANDARD COMMUNICATION */
    ecan1TxBuf[0][2] = 0x0008; /* WORD 2 CONTAINS DLC AND RESERVED BITS ALONGWITH RTR AND EXTENDED IDENTIFIER BITS ,ONLY DLC IS CHANGED AS IT DETERMINES THE LENGTH OF THE WORD */

    /* Write message data bytes */
    //ecan1TxBuf[0][3] = 0x3131;
    //ecan1TxBuf[0][4] = 0x3131;
    //ecan1TxBuf[0][5] = 0x3131;
    //ecan1TxBuf[0][6] = 0x3131;
    
    ecan1TxBuf[0][3] = t1;
    ecan1TxBuf[0][4] = t2;
    ecan1TxBuf[0][5] = h1;
    ecan1TxBuf[0][6] = h2;
    while(C1TR01CONbits.TXREQ0 != 0);

    /* Request message buffer 0 transmission */
    C1TR01CONbits.TXREQ0 = 1;
   while(C1TR01CONbits.TXREQ0 == 1);
  return 0;
}
void Can_Rx_Filter(void)
{
    C1CTRL1bits.WIN = 1;
 /* Select Acceptance Filter Mask 0 for Acceptance Filter 0 */
C1FMSKSEL1bits.F0MSK=0x0;

 /* Configure Acceptance Filter Mask 0 register to mask SID<2:0>
 * Mask Bits (11-bits) : 0b111 1111 1000 */

 C1RXM0SIDbits.SID = 0x7F8;

 /* Configure Acceptance Filter 0 to match standard identifier
Filter Bits (11-bits): 0b011 1010 xxx with the mask setting, message with SID
range 0x1D0-0x1D7 will be accepted by the ECAN module. */
C1RXF0SIDbits.SID = 0x01D0;

 /* Acceptance Filter 0 to check for Standard Identifier */
C1RXM0SIDbits.MIDE = 0x1;
 C1RXF0SIDbits.EXIDE= 0x0;

 /* Acceptance Filter 0 to use Message Buffer 10 to store message */

 C1BUFPNT1bits.F0BP = 0xA;
 
 
/* Filter 0 enabled for Identifier match with incoming message */
C1FEN1bits.FLTEN0=0x1;

 /* Clear Window Bit to Access ECAN
 * Control Registers */

 C1CTRL1bits.WIN=0x0;


}

/* Interrupt Service Routine 1*/
/* No fast context save, and no variables stacked */

void __attribute__((interrupt, no_auto_psv))_C1Interrupt(void)
{
    /* clear interrupt flag */
    if( C1INTFbits.ERRIF )
    {
        C1INTFbits.ERRIF = 0;
    }
    if( C1INTFbits.IVRIF )
    {
        C1INTFbits.IVRIF = 0;

    }
    if( C1INTFbits.TXBP )
    {
        C1INTFbits.TXBP = 0;
    }
    if(C1INTFbits.TBIF)
    {
       // puts_ecanTc(&canTxMessage);
        /* clear flag */
        C1INTFbits.TBIF = 0;
    }
     
    else;
/* clear interrupt flag */
  
     
    IFS2bits.C1IF=0;
     // Enable ECAN1 Interrupt
   
}

void __attribute__((interrupt,no_auto_psv)) _T1Interrupt(void)
{
 
 IFS0bits.T1IF = 0; // clear the interrupt flag
 TMR1 = 0x0000;
 count++;
}
 

#1
rajatkalyan
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2018/01/17 21:30:45
  • Location: 0
  • Status: offline
Re: CAN receive data not present in ecan1RxBuf variable 2018/02/28 01:54:08 (permalink)
0
I have tackled the issue successfully. 
The reason was , i was configuring buffer 10 ,but my maximum length of ecanrxbuff variable was only till 8 (ecanrxbuf[8]) but all the data was present in values starting from rxbuf[10].
 I changed the value of "NO OF ECAN BUFFERS " from 8 to 16 and checked for ecanrxbuf[10][0] instead of ecanrxbuf[1][0].
If it is not clear ,kindly ask in comments.
Thanks and Regards
#2
Jump to:
© 2020 APG vNext Commercial Version 4.5