• AVR Freaks

Hot!CAN PLIB to Harmony V2.06 Driver:

Author
TS9
Super Member
  • Total Posts : 785
  • Reward points : 0
  • Joined: 2010/05/07 10:52:22
  • Status: offline
2019/07/31 01:07:58 (permalink)
0

CAN PLIB to Harmony V2.06 Driver:

Hi,
 
I have PIC32MX795F512L Explore 16 Board with CAN/LIN Pic Tail Board .
I am able to do CAN Commucation between two Nodes (to different boards) with EID (Old plib).
Now I am using Harmony V2.06 for Generating CAN Drive first only for transmission .
 

#pragma config FCANIO  = OFF  // Use Alternate CAN Pins
 
#define SYSTEM_FREQ 80000000
#define CAN_BUS_SPEED 250000
#define CAN1_BRPVAL 0x7 /* CAN speed select - 0x7 for 500kbps, 0xF for 250Kbps, 0x3 for 1Mbps */
#define CAN2_BRPVAL 0x7 /* Use same speed for both CAN modules. Time quanta per bit is set to 10. */
/* See function CAN1Init() and CAN2Init().*/
extern BYTE CAN1MessageFifoArea[2 * 8 * 16]; /* This is the CAN1 FIFO message area. */
extern BYTE CAN2MessageFifoArea[2 * 8 * 16]; /* This is the CAN2 FIFO message area. */
void CAN1Init(void)
{
// CAN 1 Configuration Setting
CAN_BIT_CONFIG canBitConfig;

/* This function will intialize
* CAN1 module. */
/* Step 1: Switch the CAN module
* ON and switch it to Configuration
* mode. Wait till the mode switch is
* complete. */
CANEnableModule(CAN1,TRUE);
CANSetOperatingMode(CAN1, CAN_CONFIGURATION);
while(CANGetOperatingMode(CAN1) != CAN_CONFIGURATION);

/* Step 2: Configure the Clock.The
* CAN_BIT_CONFIG data structure is used
* for this purpose. The propagation segment,
* phase segment 1 and phase segment 2
* are configured to have 3TQ. SYSTEM_FREQ
* and CAN_BUS_SPEED are defined in */

canBitConfig.phaseSeg2Tq = CAN_BIT_3TQ;
canBitConfig.phaseSeg1Tq = CAN_BIT_3TQ;
canBitConfig.propagationSegTq = CAN_BIT_3TQ;
canBitConfig.phaseSeg2TimeSelect = TRUE;
canBitConfig.sample3Time = TRUE;
canBitConfig.syncJumpWidth = CAN_BIT_2TQ;
CANSetSpeed(CAN1,&canBitConfig,SYSTEM_FREQ,CAN_BUS_SPEED);
/* Step 3: Assign the buffer area to the
* CAN module.
*/
CANAssignMemoryBuffer(CAN1,CAN1MessageFifoArea,(2 * 8 * 16));
/* Step 4: Configure channel 0 for TX and size of
* 8 message buffers with RTR disabled and low medium
* priority. Configure channel 1 for RX and size
* of 8 message buffers and receive the full message.
*/
CANConfigureChannelForTx(CAN1, CAN_CHANNEL0, 8, CAN_TX_RTR_DISABLED, CAN_LOW_MEDIUM_PRIORITY);
CANSetOperatingMode(CAN1, CAN_NORMAL_OPERATION);
while(CANGetOperatingMode(CAN1) != CAN_NORMAL_OPERATION);
}
void CAN1TxSendLEDMsg(void)
{
/* This function will send a message to
* CAN2 with EID 0x8004002. The data payload
* size is 1 byte. The value of the LED5Status
* will be toggled and then sent as
* the payload. CAN1 Channel 0 is used to
* send the message.*/
CANTxMessageBuffer * message;
/* Toggle LED5 status */
LED5Status ^= 0x1;

Count++ ;
/* Get a pointer to the next buffer in the channel
* check if the returned value is null. */
message = CANGetTxMessageBuffer(CAN1,CAN_CHANNEL0);
if(message != NULL)
{
/* Form a Extended ID CAN message.
* Start by clearing the buffer. */
message->messageWord[0] = 0;
message->messageWord[1] = 0;
message->messageWord[2] = 0;
message->messageWord[3] = 0;
message->msgSID.SID = 0x200; /* Send message to CAN2. */   
message->msgEID.EID = 0x4001; /* Send message to CAN2. */
message->msgEID.IDE = 1; /* IDE = 1 means Extended ID message. */
message->msgEID.RTR = 0; /* Not an RTR message. */
message->msgEID.DLC = 8; /* Send Eight byte of data. */


message->data[0] = LED5Status; /* This is the payload. */
message->data[1] = 'C' ;
message->data[2] = 'A' ;
message->data[3] = 'N' ;
message->data[4] = ' ' ;
message->data[5] = '1' ;
message->data[6] = 'T' ;
message->data[7] = 'X' ;

/* This function lets the CAN module
* know that the message processing is done
* and message is ready to be processed. */
CANUpdateChannel(CAN1,CAN_CHANNEL0);
/* Direct the CAN module to flush the
* TX channel. This will send any pending
* message in the TX channel. */
CANFlushTxChannel(CAN1,CAN_CHANNEL0);
}
}

Above Code is Working Fine .. I am able to transmit and receive (Both are Old Plib) .
 
Below is Harmony Generated Driver.

 
#pragma config FCANIO =     OFF
 
static CAN_TX_MSG_BUFFER *drv_Message0;
static CAN_TX_MSG_BUFFER can_message_buffer0[2*8*16];

// *****************************************************************************
// *****************************************************************************
// Section: Instance 0 static driver functions
// *****************************************************************************
// *****************************************************************************
void DRV_CAN0_Initialize(void)
{
/* Switch the CAN module ON */
PLIB_CAN_Enable(CAN_ID_1);
/* Switch the CAN module to Configuration mode. Wait until the switch is complete */
PLIB_CAN_OperationModeSelect(CAN_ID_1, CAN_CONFIGURATION_MODE);
while(PLIB_CAN_OperationModeGet(CAN_ID_1) != CAN_CONFIGURATION_MODE);
PLIB_CAN_PhaseSegment2LengthFreelyProgrammableEnable(CAN_ID_1);
//Set the Baud rate to 1000 kbps
PLIB_CAN_PropagationTimeSegmentSet(CAN_ID_1, 1-1);
PLIB_CAN_PhaseSegment1LengthSet(CAN_ID_1, 3-1);
PLIB_CAN_PhaseSegment2LengthSet(CAN_ID_1, 3-1);
PLIB_CAN_SyncJumpWidthSet(CAN_ID_1, 1-1);

// PLIB_CAN_BaudRatePrescaleSet(CAN_ID_1, 9); //Set the Baud rate to 500 kbps

PLIB_CAN_BaudRatePrescaleSet(CAN_ID_1, 7); // set to 1 higher then ECAN tool

/* Assign the buffer area to the CAN module.
In this case assign enough memory for 2
channels, each with 8 message buffers.*/
PLIB_CAN_MemoryBufferAssign(CAN_ID_1, can_message_buffer0);
/* Configure CAN_ID_1 Channel for CAN_TX_RTR_DISABLED operation. Allocate 8 message buffer, and assign low medium priority for transmissions. */
PLIB_CAN_ChannelForTransmitSet(CAN_ID_1, CAN_CHANNEL0, 8, CAN_TX_RTR_DISABLED, CAN_LOW_MEDIUM_PRIORITY);

/* Switch the CAN module to Normal mode. Wait until the switch is complete */
PLIB_CAN_OperationModeSelect(CAN_ID_1, CAN_NORMAL_MODE);
while(PLIB_CAN_OperationModeGet(CAN_ID_1) != CAN_NORMAL_MODE);
}
 
uint8_t SendMessage[8]; //Test message to transmit on CAN
case APP_STATE_SERVICE_TASKS:
{
LED5Status ^= 0x1;

SendMessage[0] = LED5Status ; // Toggle Bit As Other Side
SendMessage[1] = 'A';
SendMessage[2] = 'B';
SendMessage[3] = 'C';
SendMessage[4] = 'D';
SendMessage[5] = 'E';
SendMessage[6] = 'F';
SendMessage[7] = 'G';

IMB_LED2Toggle(); // Toggle LED
DRV_CAN0_ChannelMessageTransmit(CAN_CHANNEL0,0x2004001,8,&SendMessage[0]);  // ??
for(i=0;i<1000;i++)
for(j=0;j<4000;j++){} // Dummy Delay
// Wait for Some Time To Send
// Next Message
appData.state = APP_STATE_SERVICE_TASKS;
break;
}

Above code is not working fine .. I am not able to transmit (and receive) .
My receive code is written in plib .I have not changed that..
 
My Hardware and CAN Nodes connections are correct .
What I am missing .. ? Please advice ?
 
--
TS9
#1

1 Reply Related Threads

    TS9
    Super Member
    • Total Posts : 785
    • Reward points : 0
    • Joined: 2010/05/07 10:52:22
    • Status: offline
    Re: CAN PLIB to Harmony V2.06 Driver: 2019/08/01 15:37:01 (permalink)
    0
    Any Update?
    #2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5