#pragma once #include "DSP2833x_Device.h" #include "DSP28x_Project.h" namespace canSpace { enum CAN_VARIANT{ CANA, CANB }; enum configFlags{ NONE = 0, MSB_ENABLE = 1u << 0, STM_ENABLE = 1u << 1 }; enum configSystemIsrFlags{ I0EN_ENABLE = 1ul, I1EN_ENABLE = 1ul << 1, GIL_ENABLE = 1ul << 2, WLIM_ENABLE = 1ul << 8, EPIM_ENABLE = 1ul << 9, BOIM_ENABLE = 1ul << 10, RMLIM_ENABLE = 1ul << 11, WUIM_ENABLE = 1ul << 12, WDIM_ENABLE = 1ul << 13, AAIM_ENABLE = 1ul << 14, TCOM_ENABLE = 1ul << 16, MTOM_ENABLE = 1ul << 17 }; // 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 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 = 1000, Uint16 flags = 0); void configTxMBox(Uint16 boxNumber, const MsgID& configID, const MsgCtrlReg& configCtrlReg); void configRxMBox(Uint16 boxNumber, const MsgID& configID, const MsgCtrlReg& configCtrlReg); void configSystemIsr(Uint32 flags); void configMBoxIsr(Uint16 boxNumber); // TODO not realized yet bool isNewMessage(); bool isNewMessage(Uint16 boxNumber); void transmitMsg(Uint16 boxNumber, const CANMessage& message); void updateTXMessage(Uint16 boxNumber, const CANMessage& message); void sendRemoteRequest(Uint16 boxNumber); bool receiveMsg(Uint16 boxNumber, CANMessage& rxMessage); private: CAN_VARIANT canPort; volatile ECAN_REGS* p_CanRegs_; ECAN_REGS CanShadow_; volatile ECAN_MBOXES* p_CanMBoxes_; }; } // canSpace