Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility of knowing if all the frames is send. #57

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ a more universal API for CAN.

The needed can_common library is found here:
https://github.com/collin80/can_common

Note of Antonio Previtali:

Collin released version 2.01 in 2015 then worked on the master branch until 2019 without releasing any releases.
I am trying the master.
my attempt is to modify the master to add the possibility of knowing if all the frames of which I made the SendFrame have been sent and if they have not been sent to be able to remove from the transmission buffer the frames that have not yet been sent and insert new ones.
I added 2 methods:

isAllFrameSend (); // return true if all frame is sended

ClearTxBuff (void); // clear all frame not already sended, but not abort frame current in sending.

maybe it is possible to get this thing without modifying the library but I have not found a way and so I have modified.
34 changes: 33 additions & 1 deletion src/due_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ void CANRaw::initializeBuffers()
{
if (isInitialized()) return ;

Serial.println("Initialize buffers");
// AP# qui gli è rimasto un vecchio Serial
// AP# Serial.println("Initialize buffers");
AUTOBAUD_DEBUG("Initialize buffers");

// set up the transmit and receive ring buffers
if (tx_frame_buff==0) tx_frame_buff=new CAN_FRAME[sizeTxBuffer];
if (rx_frame_buff==0) rx_frame_buff=new CAN_FRAME[sizeRxBuffer];
Expand Down Expand Up @@ -909,6 +912,8 @@ bool CANRaw::sendFrame(CAN_FRAME& txFrame)
if ( usesGlobalTxRing(mbox) && (m_pCan->CAN_MB[mbox].CAN_MSR & CAN_MSR_MRDY) ) {
//is it also available (not sending anything?)
writeTxRegisters(txFrame,mbox);
// AP#
okAllTx = 0; // Tx in corso.
enable_interrupt(0x01u << mbox); //enable the TX interrupt for this box
result = true; //we've sent it. mission accomplished.
break; //no need to keep going. We sent our message
Expand Down Expand Up @@ -1443,7 +1448,10 @@ void CANRaw::mailbox_int_handler(uint8_t mb, uint32_t /*ul_status*/)
if (removeFromRingBuffer(*pRing, tempFrame)) //if there is a frame in the queue to send
writeTxRegisters(tempFrame,mb);
else
{ // AP#
okAllTx = 1; // tx frame end
disable_interrupt(0x01 << mb);
}
break;

case 5: // producer - technically still a transmit buffer
Expand All @@ -1452,6 +1460,30 @@ void CANRaw::mailbox_int_handler(uint8_t mb, uint32_t /*ul_status*/)
}
}


// AP# the prerequisites for this to work is that you use only one mailbox in TX as it is by default.
boolean CANRaw::isAllFrameSend(void)
{
return (okAllTx==1);
}

// AP#
void CANRaw::ClearTxBuff(void)
{
// the MB7 mailbox that is being sent cannot be deleted ...
// however, it is possible to delete the messages in the mailing queue so as to empty
// the queue and allow you to send or better put in the queue newer messages to send ...

irqLock();

txRing.tail = txRing.head; // simple !
// if you want you can do the head = tail but I prefer tail = head!

irqRelease();
}



/**
* \brief Interrupt dispatchers - Never directly call these
*
Expand Down
7 changes: 7 additions & 0 deletions src/due_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ class CANRaw: public CAN_COMMON
// Constructor
CANRaw( Can* pCan, uint32_t En);

// AP# the following 2 work if you use only one mailbox in TX and if you use the GlobalTxRing as it is by default.
boolean isAllFrameSend(void); // return true se all frame is sended
void ClearTxBuff(void); // clear all frame non already sended, but not abort frame current in sending.

// Before begin, you can define rx buffer size. Default is SIZE_RX_BUFFER. This does not have effect after begin.
void setRxBufferSize(uint16_t size) { if (!isInitialized() ) sizeRxBuffer=size; }

Expand Down Expand Up @@ -260,6 +264,9 @@ class CANRaw: public CAN_COMMON

uint32_t numBusErrors;
uint32_t numRxFrames;

// AP#
volatile uint8_t okAllTx = 1; // 0 if frame transmission is still in progress. 1=ok send all.
};

extern CANRaw Can0;
Expand Down