diff --git a/README.md b/README.md index aa171c9..47c6b20 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/due_can.cpp b/src/due_can.cpp index e88aa98..75b0b2a 100644 --- a/src/due_can.cpp +++ b/src/due_can.cpp @@ -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]; @@ -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 @@ -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 @@ -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 * diff --git a/src/due_can.h b/src/due_can.h index 564171f..b4976f5 100644 --- a/src/due_can.h +++ b/src/due_can.h @@ -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; } @@ -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;