diff --git a/Protocol/CAN.cpp b/Protocol/CAN.cpp index 464b733..ec996bc 100644 --- a/Protocol/CAN.cpp +++ b/Protocol/CAN.cpp @@ -34,6 +34,69 @@ void CAN::transmitMsg(Uint16 boxNumber, const CANMessage& message){ } +void transmitMsg(Uint16 boxNumber, const Uint32& message){ + if (boxNumber > 31) return; + + Uint32 mboxControl(0); + mboxControl = 1ul << boxNumber; + + volatile MBOX* p_MailBox(0); + p_MailBox = &(p_CanMBoxes_->MBOX0) + boxNumber; + + p_MailBox->MDH.all = 0x0; + p_MailBox->MDL.all = 0x0; + + p_MailBox->MDH.all = message >> 16; + p_MailBox->MDL.all = message; + + // Set TRS for mailbox + CanShadow_.CANTRS.all = 0; + CanShadow_.CANTRS.all |= mboxControl; + p_CanRegs_->CANTRS.all = CanShadow_.CANTRS.all; + + // Wait for TA bit to be set + do { CanShadow_.CANTA.all = p_CanRegs_->CANTA.all; } + while((CanShadow_.CANTA.all & mboxControl) == 0 ); + + // Clear TA (transmit acknowledge bit) + CanShadow_.CANTA.all = 0; + CanShadow_.CANTA.all |= mboxControl; + p_CanRegs_->CANTA.all = CanShadow_.CANTA.all; +} + + +void transmitMsg(Uint16 boxNumber, const Uint32& message, const Uint16 dlc){ + if (boxNumber > 31) return; + + Uint32 mboxControl(0); + mboxControl = 1ul << boxNumber; + + volatile MBOX* p_MailBox(0); + p_MailBox = &(p_CanMBoxes_->MBOX0) + boxNumber; + + p_MailBox->MDH.all = 0x0; + p_MailBox->MDL.all = 0x0; + + p_MailBox->MSGCTRL.bit.DLC = dlc; + p_MailBox->MDH.all = message >> 16; + p_MailBox->MDL.all = message; + + // Set TRS for mailbox + CanShadow_.CANTRS.all = 0; + CanShadow_.CANTRS.all |= mboxControl; + p_CanRegs_->CANTRS.all = CanShadow_.CANTRS.all; + + // Wait for TA bit to be set + do { CanShadow_.CANTA.all = p_CanRegs_->CANTA.all; } + while((CanShadow_.CANTA.all & mboxControl) == 0 ); + + // Clear TA (transmit acknowledge bit) + CanShadow_.CANTA.all = 0; + CanShadow_.CANTA.all |= mboxControl; + 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. Once the TRS bit is set for a mailbox and then data is changed in the mailbox using the CDR // bit, the CAN module fails to transmit the new data and transmits the old data instead. To avoid this, // reset transmission in that mailbox using the TRRn bit and set the TRSn bit again. The new data is diff --git a/Protocol/CAN.h b/Protocol/CAN.h index 149a90b..d6fc14a 100644 --- a/Protocol/CAN.h +++ b/Protocol/CAN.h @@ -111,7 +111,9 @@ public: bool isNewMessage(); bool isNewMessage(Uint16 boxNumber); - void transmitMsg(Uint16 boxNumber, const CANMessage& message); + void transmitMsg(Uint16 boxNumber, const CANMessage& message); // TODO excessive method? + void transmitMsg(Uint16 boxNumber, const Uint32& message); + void transmitMsg(Uint16 boxNumber, const Uint32& message, const Uint16 dlc); void updateTXMessage(Uint16 boxNumber, const CANMessage& message); void sendRemoteRequest(Uint16 boxNumber); bool receiveMsg(Uint16 boxNumber, CANMessage& rxMessage);