From fd72d2833b97afebfe4ad55dddfa5b9524361c5a Mon Sep 17 00:00:00 2001 From: Oleg Date: Wed, 19 Feb 2025 17:38:46 +0300 Subject: [PATCH] Add remote frames --- Protocol/CAN.cpp | 54 +++++++++++++++++++++++++++++++++++++----- Protocol/CAN.h | 13 +++++----- Protocol/CANConfig.cpp | 7 +++++- main.cpp | 37 +++++++++++++++++++++++++---- 4 files changed, 93 insertions(+), 18 deletions(-) diff --git a/Protocol/CAN.cpp b/Protocol/CAN.cpp index a8018b3..4a6e8b0 100644 --- a/Protocol/CAN.cpp +++ b/Protocol/CAN.cpp @@ -1,4 +1,5 @@ #include "CAN.h" +#include "DSP2833x_Device.h" #include namespace canSpace { @@ -17,8 +18,9 @@ void CAN::transmitMsg(Uint16 boxNumber, const CANMessage& message){ p_MailBox->MDH.all = message.mdh.all; p_MailBox->MDL.all = message.mdl.all; + // Set TRS for mailbox CanShadow_.CANTRS.all = 0; - CanShadow_.CANTRS.all |= mboxControl; // Set TRS for mailbox under test + CanShadow_.CANTRS.all |= mboxControl; p_CanRegs_->CANTRS.all = CanShadow_.CANTRS.all; do { CanShadow_.CANTA.all = p_CanRegs_->CANTA.all; } // TODO add tx error somewhere here @@ -29,18 +31,44 @@ void CAN::transmitMsg(Uint16 boxNumber, const CANMessage& message){ p_CanRegs_->CANTA.all = CanShadow_.CANTA.all; } +void CAN::updateTXMessage(Uint16 boxNumber, const CANMessage& message){ // TODO check trs bit and set it at the end if it was here + volatile MBOX* p_MailBox(NULL); + p_MailBox = &(p_CanMBoxes_->MBOX0) + boxNumber; -bool CAN::isNewMessage(){ - return static_cast(p_CanRegs_->CANRMP.all); + p_CanRegs_->CANMC.all |= (128 + boxNumber); + + p_MailBox->MDL.all = message.mdl.all; + p_MailBox->MDH.all = message.mdh.all; + + CanShadow_.CANMC.all = p_CanRegs_->CANMC.all; + CanShadow_.CANMC.bit.CDR = 0; + p_CanRegs_->CANMC.all = CanShadow_.CANMC.all; } -bool CAN::isNewMessage(Uint16 boxNumber){ +void CAN::sendRemoteRequest(Uint16 boxNumber){ + volatile MBOX* p_MailBox(NULL); + p_MailBox = &(p_CanMBoxes_->MBOX0) + boxNumber; + Uint32 mboxControl(0); mboxControl = 1ul << boxNumber; - bool isNewMessageInBox = p_CanRegs_->CANRMP.all & mboxControl; - return isNewMessageInBox; + // Mailbox disable + CanShadow_.CANME.all = p_CanRegs_->CANME.all; + CanShadow_.CANME.all &= ~(mboxControl); + p_CanRegs_->CANME.all = CanShadow_.CANME.all; + + p_MailBox->MSGCTRL.bit.RTR = 1; + + // Mailbox enable + CanShadow_.CANME.all = p_CanRegs_->CANME.all; + CanShadow_.CANME.all |= mboxControl; + p_CanRegs_->CANME.all = CanShadow_.CANME.all; + + // Set TRS for mailbox + CanShadow_.CANTRS.all = 0; + CanShadow_.CANTRS.all |= mboxControl; + p_CanRegs_->CANTRS.all = CanShadow_.CANTRS.all; } @@ -75,6 +103,20 @@ bool CAN::receiveMsg(Uint16 boxNumber, CANMessage& rxMessage){ // TODO faults ju } +bool CAN::isNewMessage(){ + return static_cast(p_CanRegs_->CANRMP.all); +} + + +bool CAN::isNewMessage(Uint16 boxNumber){ + Uint32 mboxControl(0); + mboxControl = 1ul << boxNumber; + + bool isNewMessageInBox = p_CanRegs_->CANRMP.all & mboxControl; + return isNewMessageInBox; +} + + /* CANMessage CAN::receiveMsg(Uint16 boxNumber){ Uint32 mboxControl(0); mboxControl = 1ul << boxNumber; diff --git a/Protocol/CAN.h b/Protocol/CAN.h index 328350f..b068315 100644 --- a/Protocol/CAN.h +++ b/Protocol/CAN.h @@ -103,21 +103,22 @@ public: 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 configRxMBox(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); + 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_; - CAN_VARIANT canPort; }; diff --git a/Protocol/CANConfig.cpp b/Protocol/CANConfig.cpp index a3a8aa2..12224cb 100644 --- a/Protocol/CANConfig.cpp +++ b/Protocol/CANConfig.cpp @@ -219,7 +219,7 @@ void CAN::configTxMBox(Uint16 boxNumber, const MsgID& configID, const MsgCtrlReg } -void CAN::configRxMBoxes(Uint16 boxNumber, const MsgID& configID, const MsgCtrlReg& configCtrlReg){ +void CAN::configRxMBox(Uint16 boxNumber, const MsgID& configID, const MsgCtrlReg& configCtrlReg){ volatile MBOX* p_MailBox(NULL); p_MailBox = &(p_CanMBoxes_->MBOX0) + boxNumber; @@ -227,6 +227,11 @@ void CAN::configRxMBoxes(Uint16 boxNumber, const MsgID& configID, const MsgCtrlR Uint32 mboxControl(0); mboxControl = 1ul << boxNumber; + // Mailbox disable + CanShadow_.CANME.all = p_CanRegs_->CANME.all; + CanShadow_.CANME.all &= ~(mboxControl); + p_CanRegs_->CANME.all = CanShadow_.CANME.all; + // Write to the MSGID field p_MailBox->MSGID.all = 0x0; p_MailBox->MSGID.all = configID.all; diff --git a/main.cpp b/main.cpp index 7336abb..5eaa9da 100644 --- a/main.cpp +++ b/main.cpp @@ -20,7 +20,10 @@ Uint16 infCounter = 0; volatile Uint16 testCounter = 0; Uint32 testVar = 0; volatile bool startTX = false; +volatile bool update = false; +volatile bool sendRemote = false; canSpace::CANMessage message; +canSpace::CANMessage rxMessage; void main() { @@ -57,7 +60,7 @@ void main() // configuration_parameters.extract_configuration(test_config); InitCpuTimers(); - ConfigCpuTimer(&CpuTimer0, 150, 800); + ConfigCpuTimer(&CpuTimer0, 150, 10000); IER |= M_INT1; // Enable CPU Interrupt 1 // Enable ADCINT in PIE @@ -81,7 +84,10 @@ void main() canTest.initGpio(); canTest.config(100); canTest.configTxMBox(1, canSpace::MsgID(0xAAA), canSpace::MsgCtrlReg(0x8)); - canTest.configRxMBoxes(25, canSpace::MsgID(0xAAA), canSpace::MsgCtrlReg(0x8)); + canTest.configRxMBox(25, canSpace::MsgID(0xAAA), canSpace::MsgCtrlReg(0x8)); + // Remote frame + canTest.configRxMBox(2, canSpace::MsgID(0x111), canSpace::MsgCtrlReg(0x13)); + // canTest.configTxMBox(30, canSpace::MsgID(0x111, false, true), canSpace::MsgCtrlReg(0x3)); // for remote answer // InitECanGpio(); // InitECan(); @@ -97,6 +103,7 @@ void main() message.mdh.byte.BYTE5 = 0xBB; message.mdh.byte.BYTE6 = 0xCC; message.mdh.byte.BYTE7 = 0xDD; + // canTest.updateTXMessage(30, message); // for remote answer idle_loop(); // @@ -116,7 +123,21 @@ void idle_loop() } if(canTest.isNewMessage()){ testCounter++; - testVar = canTest.receiveMsg(25, message); + testVar = canTest.receiveMsg(25, rxMessage); + // testVar = canTest.receiveMsg(2, rxMessage); + } + + // testVar = canTest.receiveMsg(2, rxMessage); + + if (update){ + update = false; + message.mdh.all = 0xBBEEBBEE; + canTest.updateTXMessage(1, message); + } + + if (sendRemote){ + sendRemote = false; + canTest.sendRemoteRequest(2); } }//end while @@ -126,7 +147,13 @@ void idle_loop() interrupt void cpu_timer0_isr(void) { - - // + canTest.sendRemoteRequest(2); + + if(canTest.isNewMessage()){ + testCounter++; + testVar = canTest.receiveMsg(25, rxMessage); + testVar = canTest.receiveMsg(2, rxMessage); + } + PieCtrlRegs.PIEACK.all |= PIEACK_GROUP1; }//end