#pragma once #include "DSP2833x_Device.h" #include "DSP28x_Project.h" namespace canSpace { enum CAN_VARIANT{ CANA, CANB }; // eCAN Message Control Register (MSGCTRL) bit definitions struct MsgCtrlBits { // bits description Uint16 DLC:4; // 0:3 Uint16 RTR:1; // 4 Uint16 rsvd1:3; // 7:5 reserved Uint16 TPL:5; // 12:8 Uint16 rsvd2:3; // 15:13 reserved }; union MsgCtrlReg { Uint16 all; struct MsgCtrlBits bit; MsgCtrlReg(Uint16 configData = 0){ all = configData; } }; struct MsgID_Bits { // bits description Uint16 EXTMSGID_L:16; // 0:15 Uint16 EXTMSGID_H:2; // 16:17 Uint16 STDMSGID:11; // 18:28 Uint16 AAM:1; // 29 Uint16 AME:1; // 30 Uint16 IDE:1; // 31 }; // Allow access to the bit fields or entire register union MsgID { Uint32 all; struct MsgID_Bits bit; MsgID(Uint32 boxID = 0xAAA, bool isExtendedID = false, bool isAAM = false, bool isAME = false) { if(!isExtendedID){ bit.STDMSGID = boxID; bit.EXTMSGID_H = 0; bit.EXTMSGID_L = 0; } else{ all = boxID; } bit.IDE = isExtendedID; bit.AAM = isAAM; bit.AME = isAME; } MsgID(const MsgID& init) { all = init.all; } }; /* struct ConfigMsgIDReg{ Uint32 boxID; bool isExtendedID; bool isAAM; bool isAME; ConfigMsgIDReg(Uint32 initBoxID = 0xAAA, bool initIsExtendedID = false, bool initIsAAM = false, bool initIsAME = false) : boxID(initBoxID), isExtendedID(initIsExtendedID), isAAM(initIsAAM), isAME(initIsAME) {} }; */ /* struct ConfigMBox{ MsgID msgID; MsgCtrlReg msgCtrlReg; ConfigMBox(const MsgID& configMsgID, const Uint16& configMsgCtrlReg) : // msgID(configMsgID.boxID, configMsgID.isExtendedID, configMsgID.isAAM, configMsgID.isAME), msgID(configMsgID), msgCtrlReg(configMsgCtrlReg) {} }; */ struct CANMessage { union MsgCtrlReg msgctrl; union CANMDL_REG mdl; union CANMDH_REG mdh; CANMessage(){ mdl.all = 0; mdh.all = 0; } }; class CAN{ public: CAN(CAN_VARIANT canVariant); void initGpio(); void config(Uint16 baudrate); // void configTxMBox(Uint16 boxNumber, const ConfigMBox& configData); void configTxMBox(Uint16 boxNumber, const MsgID& configID, const MsgCtrlReg& configCtrlReg); void configRxMBoxes(Uint16 boxNumber, const MsgID& configID, const MsgCtrlReg& configCtrlReg); void transmitMsg(Uint16 boxNumber, const CANMessage& message); bool receiveMsg(Uint16 boxNumber, CANMessage& rxMessage); // CANMessage receiveMsg(Uint16 boxNumber); bool isNewMessage(); bool isNewMessage(Uint16 boxNumber); private: volatile ECAN_REGS* p_CanRegs_; ECAN_REGS CanShadow_; volatile ECAN_MBOXES* p_CanMBoxes_; CAN_VARIANT canPort; }; } // canSpace