Helpful ReplyWhat I am doing wrong ??

Author
BHUSHAN
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2019/02/04 23:17:18
  • Location: 0
  • Status: offline
2019/04/15 00:21:27 (permalink)
0

What I am doing wrong ??

Hi 2 all. I am using PIC32(PIC32MX795F512L) for CAN communication. I am able to filter message SID but not able to get transmitted data(I am transmitting data using Microchip CAN bus analyzer). I think I have messed up my code. Kindly suggest me where I am wrong. I have configured my micro-controller for receiving data only so do I have to configure my main data structure as of in my code?
Following is part of code (code related to CAN protocol only):-All I want to do is to receive 1 byte of data and put it to PORTD
 
/* Create a CMSGSID data type. */
typedef struct {
unsigned SID:11;
unsigned FILHIT:5;
unsigned CMSGTS:16;
}rxcmsgsid;

/* Create a CMSGEID data type. */
typedef struct {
unsigned DLC:4;
unsigned RB0:1;
unsigned :3;
unsigned RB1:1;
unsigned RTR:1;
unsigned EID:18;
unsigned IDE:1;
unsigned SRR:1;
unsigned :2;
}rxcmsgeid;

/* Create a CMSGDATA0 data type. */
typedef struct{
unsigned Byte0:8;
unsigned Byte1:8;
unsigned Byte2:8;
unsigned Byte3:8;
}rxcmsgdata0;

/* Create a CMSGDATA1 data type. */
typedef struct{
unsigned Byte4:8;
unsigned Byte5:8;
unsigned Byte6:8;
unsigned Byte7:8;
}rxcmsgdata1;

/* This is the main data structure. */
typedef union uCANRxMessageBuffer {
 struct{
     rxcmsgsid CMSGSID;
     rxcmsgeid CMSGEID;
     rxcmsgdata0 CMSGDATA0;
     rxcmsgdata0 CMSGDATA1;
    };
 int messageWord[4];
 }CAN2RxMessageBuffer;


CAN2RxMessageBuffer *rxMessage;
unsigned int  *bufferToRead;
unsigned int CANFIFObuffer[4] ; //CAN FIFO buffer

void CAN_ConfigurationMode(void){
C2CONbits.REQOP = 4;
while(C2CONbits.OPMOD != 4); //wait till CAN module enters Configuration mode
}

void CAN_NormalMode(void){
C2CONbits.REQOP = 0;
while(C2CONbits.OPMOD != 0); //wait till CAN module enters Normal mode
}

/* Fbaud = Fosc/(2*N*BRP),  N = SYNC + PHSEG1 + PHSEG2 + PROPSEG = 16
Here Fosc = 8MHz and BRP = 2 therefore Fbaud = 125kps */
void CAN_BitTiming(void){
C2CFG = 0x0001a701 ;
}

/*Assigning FIFO0 as Rx buffer*/
void CAN_AssignFIFO(void){
C2FIFOBA = KVA_TO_PA(CANFIFObuffer);
C2FIFOCON0bits.FSIZE = 2;  //
C2FIFOCON0bits.DONLY = 1 ;
C2FIFOCON0CLR = 0x80;
}
 
void MaskandFilter_init(void){
C2FLTCON0bits.FSEL3 = 0 ;//store data in FIFO buffer 0
C2FLTCON0bits.MSEL3 = 1 ; //Acceptance Mask 1 accepted
C2RXF3bits.SID = 0x08f;
C2RXF3bits.EXID = 0; //Match only messages with standard identifier address
C2RXM1bits.SID = 0x7ff ;
C2RXM1bits.MIDE = 1;//Match only message types
C2FLTCON0bits.FLTEN3 = 1;//Filter enabled
}


void main(){
TRISD = 0 ;LATD = 0x00 ;
CAN_ConfigurationMode();
CAN_BitTiming();
C2CONbits.ON = 1;
MaskandFilter_init();
CAN_NormalMode();
while(1){
while(C2FIFOINT0 == 0);
rxMessage = (CAN2RxMessageBuffer *)PA_TO_KVA1(C2FIFOUA0);
bufferToRead[0] = rxMessage->messageWord[2];   //Program gets stuck here
LATD =0xff;
bufferToRead[1] = rxMessage->messageWord[1];
bufferToRead[2] = rxMessage->messageWord[2];
bufferToRead[3] = rxMessage->messageWord[3];
C2FIFOCON0SET = 0x2000;
}
}
post edited by BHUSHAN - 2019/04/15 00:38:03
#1
BHUSHAN
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2019/02/04 23:17:18
  • Location: 0
  • Status: offline
Re: What I am doing wrong ?? 2019/04/15 00:54:00 (permalink)
0
Leave it pals. I got the solution. Don't waste your time on this.
#2
jg_ee
Super Member
  • Total Posts : 142
  • Reward points : 0
  • Joined: 2015/04/30 10:54:52
  • Location: Colorado
  • Status: offline
Re: What I am doing wrong ?? 2019/04/15 07:48:52 (permalink)
0
Don't want to leave your solution for the future Google searchers?
#3
BHUSHAN
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2019/02/04 23:17:18
  • Location: 0
  • Status: offline
Re: What I am doing wrong ?? 2019/04/15 21:31:08 (permalink) ☄ Helpfulby Davide33 2019/04/16 08:19:08
0
Justin Grantham
Don't want to leave your solution for the future Google searchers?




Sure Sir.
Code snippet:-(rest of code remains same)
 
CAN2RxMessageBuffer *rxMessage;
unsigned int  data;
unsigned int CANFIFObuffer[4] ; //CAN FIFO buffer
void main(){
C2FIFOBA = KVA_TO_PA(CANFIFObuffer);
while(1){
if(C2FIFOINT0bits.RXNEMPTYIF == 1){
rxMessage = PA_TO_KVA1(C2FIFOUA1);//(CAN2RxMessageBuffer *)PA_TO_KVA1(C2FIFOUA0);
data  = rxMessage->messageWord[2]; //messageWord[2] has data
LATD = data ; //I am displaying data on PORTD
C2FIFOCON0SET = 0x2000;//FIFO tail increment by single message
}
#4
Jump to:
© 2019 APG vNext Commercial Version 4.5