Skip to content

Commit dadd55f

Browse files
committed
small send rearrange, The send function will now skip sending a packet (aka NOT attempting to send it) if the mcp is not readyToSend() instead of waiting for it to be ready, which defeats the point of buffering packets indefiniteley
1 parent 49091cf commit dadd55f

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
lines changed

src/modm/driver/can/mcp2515.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ namespace modm
263263
bool tempR_ = false;
264264
bool tempS_ = false;
265265
bool temp_ = false;
266-
bool hasSend_ = false;
267266
bool receiveSuccess_ = false;
268267
};
269268
}

src/modm/driver/can/mcp2515_impl.hpp

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,13 @@ template <typename SPI, typename CS, typename INT>
216216
bool
217217
modm::Mcp2515<SPI, CS, INT>::sendMessage(const can::Message& message)
218218
{
219+
bool success = true;
219220
if (not modm_assert_continue_ignore(txQueue.push(message), "mcp2515.can.tx",
220221
"CAN transmit software buffer overflowed!", 1)) {
221-
return false;
222+
/// buffer full, could not send
223+
success = false;
222224
}
223-
return true;
225+
return success;
224226
}
225227

226228
// ----------------------------------------------------------------------------
@@ -297,8 +299,9 @@ modm::Mcp2515<SPI, CS, INT>::update(){
297299
/// check if device accepts messages and start emptying the transmit queue if not empty
298300
if (txQueue.isNotEmpty())
299301
{
300-
hasSend_ = RF_CALL(mcp2515SendMessage(txQueue.get()));
301-
txQueue.pop();
302+
if(RF_CALL(mcp2515SendMessage(txQueue.get()))){
303+
txQueue.pop();
304+
}
302305
}
303306
RF_END();
304307
}
@@ -336,12 +339,10 @@ modm::Mcp2515<SPI, CS, INT>::mcp2515IsReadyToSend(uint8_t status)
336339
// all buffers currently in use
337340
ready = false;
338341
}
339-
340342
return ready;
341343
}
342344

343345
// ----------------------------------------------------------------------------
344-
345346
template <typename SPI, typename CS, typename INT>
346347
modm::ResumableResult<bool>
347348
modm::Mcp2515<SPI, CS, INT>::mcp2515SendMessage(const can::Message& message)
@@ -351,52 +352,55 @@ modm::Mcp2515<SPI, CS, INT>::mcp2515SendMessage(const can::Message& message)
351352

352353
statusBufferS_ = RF_CALL(readStatus(READ_STATUS));
353354

354-
/// wait for ready to send status flags
355-
RF_WAIT_UNTIL(mcp2515IsReadyToSend(statusBufferS_));
356-
357-
if ((statusBufferS_ & TXB0CNTRL_TXREQ) == 0)
358-
{
359-
addressBufferS_ = 0x00; // TXB0SIDH
360-
} else if ((statusBufferS_ & TXB1CNTRL_TXREQ) == 0)
361-
{
362-
addressBufferS_ = 0x02; // TXB1SIDH
363-
} else if ((statusBufferS_ & TXB2CNTRL_TXREQ) == 0)
364-
{
365-
addressBufferS_ = 0x04; // TXB2SIDH
366-
} else
367-
{
368-
// all buffer are in use => could not send the message
369-
}
355+
addressBufferS_ = static_cast<uint8_t>(false);
370356

371-
if (addressBufferS_ == 0x00 || addressBufferS_ == 0x02 || addressBufferS_ == 0x04)
357+
// /// send if ready, else return that nothing was sent
358+
if(mcp2515IsReadyToSend(statusBufferS_))
372359
{
373-
RF_WAIT_UNTIL(this->acquireMaster());
374-
chipSelect.reset();
375-
RF_CALL(spi.transfer(WRITE_TX | addressBufferS_));
376-
RF_CALL(writeIdentifier(message.identifier, message.flags.extended));
377-
378-
// if the message is a rtr-frame, is has a length but no attached data
379-
if (message.flags.rtr)
360+
if ((statusBufferS_ & TXB0CNTRL_TXREQ) == 0)
380361
{
381-
RF_CALL(spi.transfer(MCP2515_RTR | message.length));
362+
addressBufferS_ = 0x00; // TXB0SIDH
363+
} else if ((statusBufferS_ & TXB1CNTRL_TXREQ) == 0)
364+
{
365+
addressBufferS_ = 0x02; // TXB1SIDH
366+
} else if ((statusBufferS_ & TXB2CNTRL_TXREQ) == 0)
367+
{
368+
addressBufferS_ = 0x04; // TXB2SIDH
382369
} else
383370
{
384-
RF_CALL(spi.transfer(message.length));
371+
// all buffer are in use => could not send the message
372+
}
385373

386-
for (i_ = 0; i_ < message.length; ++i_) {
387-
RF_CALL(spi.transfer(message.data[i_]));
374+
if (addressBufferS_ == 0x00 || addressBufferS_ == 0x02 || addressBufferS_ == 0x04)
375+
{
376+
RF_WAIT_UNTIL(this->acquireMaster());
377+
chipSelect.reset();
378+
RF_CALL(spi.transfer(WRITE_TX | addressBufferS_));
379+
RF_CALL(writeIdentifier(message.identifier, message.flags.extended));
380+
381+
// if the message is a rtr-frame, is has a length but no attached data
382+
if (message.flags.rtr)
383+
{
384+
RF_CALL(spi.transfer(MCP2515_RTR | message.length));
385+
} else
386+
{
387+
RF_CALL(spi.transfer(message.length));
388+
389+
for (i_ = 0; i_ < message.length; ++i_) {
390+
RF_CALL(spi.transfer(message.data[i_]));
391+
}
388392
}
393+
delayS_.restart(1ms);
394+
chipSelect.set();
395+
RF_WAIT_UNTIL(delayS_.isExpired());
396+
397+
// send message via RTS command
398+
chipSelect.reset();
399+
addressBufferS_ = (addressBufferS_ == 0) ? 1 : addressBufferS_; // 0 2 4 => 1 2 4
400+
RF_CALL(spi.transfer(RTS | addressBufferS_));
401+
RF_WAIT_UNTIL(this->releaseMaster());
402+
chipSelect.set();
389403
}
390-
delayS_.restart(1ms);
391-
chipSelect.set();
392-
RF_WAIT_UNTIL(delayS_.isExpired());
393-
394-
// send message via RTS command
395-
chipSelect.reset();
396-
addressBufferS_ = (addressBufferS_ == 0) ? 1 : addressBufferS_; // 0 2 4 => 1 2 4
397-
RF_CALL(spi.transfer(RTS | addressBufferS_));
398-
RF_WAIT_UNTIL(this->releaseMaster());
399-
chipSelect.set();
400404
}
401405

402406
RF_END_RETURN(static_cast<bool>(addressBufferS_));

0 commit comments

Comments
 (0)