From 07bb5edd57340995e06c32844a33ad1ad72681de Mon Sep 17 00:00:00 2001 From: Oleg Date: Thu, 6 Mar 2025 10:08:08 +0300 Subject: [PATCH] Change/add methods 1. Change receiveMsg. Now it returns int16 with error identification 2. Change CANMessage structure. Now it only has dataLenght variable instead of previos MsgCtrlReg union 3. Change MsgCtrlReg. Add an OPC flag in it 4. Change configRxMBox method. Overwrite protection enabling is added --- Protocol/CAN.cpp | 11 ++++++----- Protocol/CAN.h | 8 +++++--- Protocol/CANConfig.cpp | 8 +++++--- main.cpp | 5 ++++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Protocol/CAN.cpp b/Protocol/CAN.cpp index 639ce67..c2b1777 100644 --- a/Protocol/CAN.cpp +++ b/Protocol/CAN.cpp @@ -14,7 +14,7 @@ void CAN::transmitMsg(Uint16 boxNumber, const CANMessage& message){ p_MailBox->MDH.all = 0x0; p_MailBox->MDL.all = 0x0; - p_MailBox->MSGCTRL.bit.DLC = message.msgctrl.bit.DLC; + p_MailBox->MSGCTRL.bit.DLC = message.dataLength; p_MailBox->MDH.all = message.mdh.all; p_MailBox->MDL.all = message.mdl.all; @@ -109,6 +109,7 @@ void CAN::updateTXMessage(Uint16 boxNumber, const CANMessage& message){ // TODO // Set change data request (CDR bit + MBOX number) p_CanRegs_->CANMC.all |= (128 + boxNumber); + // TODO Add lenght changing? p_MailBox->MDL.all = message.mdl.all; p_MailBox->MDH.all = message.mdh.all; @@ -146,7 +147,7 @@ void CAN::sendRemoteRequest(Uint16 boxNumber){ } -bool CAN::receiveMsg(Uint16 boxNumber, CANMessage& rxMessage){ // TODO faults just return -1 +int16 CAN::receiveMsg(Uint16 boxNumber, CANMessage& rxMessage){ // TODO faults just return -1 if (boxNumber > 31) { return -1; } Uint32 mboxControl(0); @@ -156,11 +157,11 @@ bool CAN::receiveMsg(Uint16 boxNumber, CANMessage& rxMessage){ // TODO faults ju p_MailBox = &(p_CanMBoxes_->MBOX0) + boxNumber; bool isNewMessageInBox = p_CanRegs_->CANRMP.all & mboxControl; - if(!isNewMessageInBox) return -1; + if(!isNewMessageInBox) return -2; p_CanRegs_->CANRMP.all &= mboxControl; - rxMessage.msgctrl.all = p_MailBox->MSGCTRL.all; + rxMessage.dataLength = p_MailBox->MSGCTRL.all; rxMessage.mdl.all = p_MailBox->MDL.all; rxMessage.mdh.all = p_MailBox->MDH.all; @@ -171,7 +172,7 @@ bool CAN::receiveMsg(Uint16 boxNumber, CANMessage& rxMessage){ // TODO faults ju lostMessage = p_CanRegs_->CANRML.all & mboxControl; if(newMessage || lostMessage) { - return -1; + return -3; } return 0; diff --git a/Protocol/CAN.h b/Protocol/CAN.h index dfd1c2f..f38226c 100644 --- a/Protocol/CAN.h +++ b/Protocol/CAN.h @@ -1,5 +1,6 @@ #pragma once +#include "DSP2833x_Device.h" #include "DSP28x_Project.h" namespace canSpace { @@ -36,7 +37,8 @@ enum configSystemIsrFlags{ struct MsgCtrlBits { // bits description Uint16 DLC:4; // 0:3 Uint16 RTR:1; // 4 - Uint16 rsvd1:3; // 7:5 reserved + Uint16 OPC:1; // 1 + Uint16 rsvd1:2; // 7:6 reserved Uint16 TPL:5; // 12:8 Uint16 rsvd2:3; // 15:13 reserved }; @@ -86,7 +88,7 @@ union MsgID { struct CANMessage { - union MsgCtrlReg msgctrl; + Uint16 dataLength; union CANMDL_REG mdl; union CANMDH_REG mdh; @@ -116,7 +118,7 @@ public: 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); + int16 receiveMsg(Uint16 boxNumber, CANMessage& rxMessage); private: CAN_VARIANT canPort; diff --git a/Protocol/CANConfig.cpp b/Protocol/CANConfig.cpp index a83daec..5eface1 100644 --- a/Protocol/CANConfig.cpp +++ b/Protocol/CANConfig.cpp @@ -252,9 +252,11 @@ void CAN::configRxMBox(Uint16 boxNumber, const MsgID& configID, const MsgCtrlReg // Overwrite protection // If "ON" make sure that an additional mailbox is configured to store ’overflow’ messages. - // CanShadow_.CANOPC.all = p_CanRegs_->CANOPC.all; - // CanShadow_.CANOPC.all |= mboxControl; // Should be one more mailbox to store 'overflow' messages - // p_CanRegs_->CANOPC.all = CanShadow_.CANOPC.all; + if(configCtrlReg.bit.OPC){ + CanShadow_.CANOPC.all = p_CanRegs_->CANOPC.all; + CanShadow_.CANOPC.all |= mboxControl; + p_CanRegs_->CANOPC.all = CanShadow_.CANOPC.all; + } // Enable Mailbox CanShadow_.CANME.all = p_CanRegs_->CANME.all; diff --git a/main.cpp b/main.cpp index e63ac35..7334870 100644 --- a/main.cpp +++ b/main.cpp @@ -88,7 +88,7 @@ void main() CpuTimer0.RegsAddr->TCR.bit.TSS = 0; - message.msgctrl.bit.DLC = 8; + message.dataLength = 8; message.mdl.byte.BYTE0 = 0x11; message.mdl.byte.BYTE1 = 0x22; message.mdl.byte.BYTE2 = 0x33; @@ -157,9 +157,12 @@ interrupt void canb_isr(void){ canISRcounter++; CANGIF0_REG CANGIF0_SHADOW; + volatile Uint32 resetBit; + CANGIF0_SHADOW.all = ECanbRegs.CANGIF0.all; if (CANGIF0_SHADOW.bit.WLIF0){ + resetBit = 256; ECanbRegs.CANGIF0.all = 256ul; }