/*********************************************************************
*
* Includes
*
*********************************************************************/
#include "bsp.h"
/*********************************************************************
*
* Defines
*
*********************************************************************/
// ECAN bitrate define, only can choose one rate
#define F_ECAN_100 0 // 1 set ECAN module on 100Kbps
#define F_ECAN_125 0 // 1 set ECAN module on 125Kbps
#define F_ECAN_500 1 // 1 set ECAN module on 500Kbps
#define F_ECAN_1000 0 // 1 set ECAN module on 1Mbps
/*********************************************************************
*
* Function: Initialize the CAN Module
*
*********************************************************************/
void ECAN_Init(void)
{
CAN_STB_SetDigitalOutput();//CAN收发器待机模式端配置为输出
CAN_TX_SetDigitalOutput();
CAN_RX_SetDigitalInput();
// Place CAN module in configuration mode, see CANCON register data
CANCON = 0x80; //REQOP bits <2:0> = 0b100
// while(!(CANSTATbits.OPMODE ==0x04)); //Wait for op-mode bits in the
while(CANSTAT != 0X80); //CANSTAT register to = 0b100
//to indicate config mode OK
// Enter CAN module into Mode 0, standard legacy mode; see ECANCON register
ECANCON = 0x00;
// See Microchip application note AN754, Understanding Microchip's
// CAN Module Bit Timing." See also: Microchip Controller Area Network
//(CAN) Bit Timing Calculator, available at Intrepid Control Systems:
//www.intrepidcs.com/support/mbtime.htm.
// Initialize CAN Bus bit rate timing. Assumes only four standard rates.
// Initialize CAN Timing
if (F_ECAN_100==1)
{
// 100 Kbps @ 32MHz
BRGCON1 = 0x13; //SJW=1TQ BRP 19
BRGCON2 = 0x90; //SEG2PHTS 1 sampled once PS1=3TQ PropagationT 1TQ
BRGCON3 = 0x02; //PS2 3TQ
}
else if (F_ECAN_125==1)
{
// 125 Kbps @ 32MHz
BRGCON1 = 0x0F; //SJW=1TQ BRP 15
BRGCON2 = 0x90; //SEG2PHTS 1 sampled once PS1=3TQ PropagationT 1TQ
BRGCON3 = 0x02; //PS2 3TQ
}
else if (F_ECAN_500==1)
{
// 125 Kbps @ 32MHz
BRGCON1 = 0x03; //SJW=1TQ BRP 3
BRGCON2 = 0x90; //SEG2PHTS 1 sampled once PS1=3TQ PropagationT 1TQ
BRGCON3 = 0x02; //PS2 3TQ
}
else if (F_ECAN_1000==1)
{
// 1000 Kbps @ 32MHz
BRGCON1 = 0x01; //SJW=1TQ BRP 1
BRGCON2 = 0x90; //SEG2PHTS 1 sampled once PS1=3TQ PropagationT 1TQ
BRGCON3 = 0x02; //PS2 3TQ
}
// Initialize Receive Masks, see registers RXMxEIDH, RXMxEIDL, etc...
// Mask 0 (M0) will accept NO extended addresses, but any standard address
RXM0EIDH = 0x00; // Extended Address receive acceptance mask, high byte
RXM0EIDL = 0x00; // Extended Address receive acceptance mask, low byte
RXM0SIDH = 0xFF; // Standard Address receive acceptance mask, high byte
RXM0SIDL = 0xE0; // Standard Address receive acceptance mask, low byte
// Mode 0 allows use of receiver filters RXF0 through RXF5. Enable filters
// RXF0 and RXF1, all others disabled. See register RXFCONn.
// Only using two filters
RXFCON0 = 0x01; //Enable Filter-0; disable others
RXFCON1 = 0x00; //Disable Filters 8 through 15
// Initialize Receive Filters
// Filter 0 = 0x312
RXF0EIDH = 0x00; //Extended Address Filter-0 unused, set high byte to 0
RXF0EIDL = 0x00; //Extended Address Filter-0 unused, set low byte to 0
RXF0SIDH = (uint8_t)(0x312>>3); //Standard Address Filter-0 high byte set to xx
RXF0SIDL = (uint8_t)(0x312<<5)&0xE0; //Standard Address Filter-0 low byte set to xx
TXB0CON=0X03;//TXB0为最高优先级3
// After configuring CAN module with above settings, return it
// to Normal mode
CANCON = 0x00;
// while(CANSTATbits.OPMODE!=0x00); //Wait for op-mode bits in the
while(CANSTAT != 0X00); //CANSTAT register to = 0b000
//to indicate Normal mode OK
/*********************************************************************
*
* Function: Check the buffers to determine if they have messages
* if so, transfer the info to the temporary-storage
* variables. Note: Messages to receiver 0 or 1 get saved in
* the same variables. This id done for simplicity in
* this example. You could save messages to separate
* variables, or in separate arrays, if you wish.
*
*********************************************************************/
void ECAN_Receive(CanRxMsg* RxMessage)
{
if (RXB0CONbits.RXFUL) // Check RXB0CON bit RXFUL to see if RX Buffer 0
// has received a message, if so, get the
// associated data from the buffer and save it.
{
/* Get the Id */
RxMessage->IDE = RXB0SIDLbits.EXID;
if (RxMessage->IDE == CAN_Id_Standard)
{
RxMessage->StdId = (uint16_t)RXB0SIDH<<3 | (RXB0SIDL>>5);
}
else
{
}
RxMessage->RTR = RXB0DLCbits.RXRTR;
/* Get the DLC */
RxMessage->DLC = RXB0DLC&0x0F;
/* Get the FMI */
// RxMessage->FMI = 0x00;
/* Get the data field */
RxMessage->Data[0] = RXB0D0;
RxMessage->Data[1] = RXB0D1;
RxMessage->Data[2] = RXB0D2;
RxMessage->Data[3] = RXB0D3;
RxMessage->Data[4] = RXB0D4;
RxMessage->Data[5] = RXB0D5;
RxMessage->Data[6] = RXB0D6;
RxMessage->Data[7] = RXB0D7;
RXB0CONbits.RXFUL = 0; // Reset buffer-0-full bit to show "empty"
}
}
#define CAN_MODE_NORMAL 0 //CAN收发器正常模式
#define CAN_MODE_STANDBY 1 //CAN收发器待机模式
typedef struct
{
uint16_t StdId; /*!< Specifies the standard identifier.
This parameter can be a value between 0 to 0x7FF. */
uint16_t ExtId; /*!< Specifies the extended identifier.
This parameter can be a value between 0 to 0x1FFFFFFF. */
uint8_t IDE; /*!< Specifies the type of identifier for the message that
will be transmitted. This parameter can be a value
of @ref CAN_identifier_type */
uint8_t RTR; /*!< Specifies the type of frame for the message that will
be transmitted. This parameter can be a value of
@ref CAN_remote_transmission_request */
uint8_t DLC; /*!< Specifies the length of the frame that will be
transmitted. This parameter can be a value between
0 to 8 */
uint8_t Data[8]; /*!< Contains the data to be transmitted. It ranges from 0
to 0xFF. */
} CanTxMsg;
/**
* @brief CAN Rx message structure definition
*/
typedef struct
{
uint16_t StdId; /*!< Specifies the standard identifier.
This parameter can be a value between 0 to 0x7FF. */
uint16_t ExtId; /*!< Specifies the extended identifier.
This parameter can be a value between 0 to 0x1FFFFFFF. */
uint8_t IDE; /*!< Specifies the type of identifier for the message that
will be received. This parameter can be a value of
@ref CAN_identifier_type */
uint8_t RTR; /*!< Specifies the type of frame for the received message.
This parameter can be a value of
@ref CAN_remote_transmission_request */
uint8_t DLC; /*!< Specifies the length of the frame that will be received.
This parameter can be a value between 0 to 8 */
uint8_t Data[8]; /*!< Contains the data to be received. It ranges from 0 to
0xFF. */
uint8_t FMI; /*!< Specifies the index of the filter the message stored in
the mailbox passes through. This parameter can be a
value between 0 to 0xFF */
} CanRxMsg;