Skip to content

Commit 2b5b355

Browse files
committed
@rleh: added rx & tx queue options to lbuild + mcp2515 driver; removed unused project xml things; cleaned up unused headers; changed mcp2515 driver class attributes to not have _ underscore postfix
1 parent 5ac11f4 commit 2b5b355

File tree

6 files changed

+120
-88
lines changed

6 files changed

+120
-88
lines changed

examples/nucleo_f439zi/can_m2515/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include <modm/board.hpp>
1515
#include <modm/platform/spi/spi_master_1.hpp>
16-
#include <modm/platform/spi/spi_master_2.hpp>
1716
#include <modm/processing/protothread.hpp>
1817
#include <modm/driver/can/mcp2515.hpp>
1918

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
<library>
22
<extends>modm:nucleo-f439zi</extends>
33
<options>
4-
<option name="modm:build:build.path">../../../build/stm32f4_discovery/mcp2515</option>
4+
<option name="modm:build:build.path">../../../build/nucleo-f439zi/mcp2515</option>
5+
<option name="modm:driver:mcp2515:buffer.tx">32</option>
6+
<option name="modm:driver:mcp2515:buffer.rx">32</option>
57
</options>
68
<modules>
7-
<module>modm:platform:gpio</module>
8-
<module>modm:platform:spi.bitbang</module>
99
<module>modm:platform:spi:1</module>
10-
<module>modm:platform:spi:2</module>
1110
<module>modm:build:scons</module>
12-
1311
<module>modm:processing:protothread</module>
1412
<module>modm:processing:timer</module>
1513
<module>modm:driver:mcp2515</module>
16-
1714
</modules>
1815
</library>

src/modm/driver/can/mcp2515.hpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
#include <modm/architecture/interface/delay.hpp>
2222
#include <modm/architecture/interface/can.hpp>
2323
#include <modm/architecture/interface/spi_device.hpp>
24-
#include <modm/architecture/driver/atomic/queue.hpp>
2524
#include <modm/processing/protothread.hpp>
2625
#include <modm/processing/resumable.hpp>
2726
#include <modm/processing/timer.hpp>
2827
#include <modm/debug/logger.hpp>
2928

3029
#include "mcp2515_definitions.hpp"
30+
#include "mcp2515_options.hpp"
3131

3232
/**
3333
* \name Restructure filter and mask bits for the MCP2515
@@ -125,7 +125,7 @@ namespace modm
125125
protected modm::NestedResumable<4>
126126
{
127127
public:
128-
Mcp2515() : messageBuffer_{}, delayR_{}, delayS_{} {
128+
Mcp2515() : messageBuffer{}, delayS{} {
129129
this->attachConfigurationHandler([]() {
130130
SPI::setDataMode(SPI::DataMode::Mode3);
131131
SPI::setDataOrder(SPI::DataOrder::MsbFirst);
@@ -240,30 +240,29 @@ namespace modm
240240
readIdentifier(uint32_t& identifier);
241241

242242
private:
243-
inline static modm::atomic::Queue<modm::can::Message, 32> txQueue;
244-
inline static modm::atomic::Queue<modm::can::Message, 32> rxQueue;
243+
inline static modm::mcp2515::options::TX_QUEUE txQueue;
244+
inline static modm::mcp2515::options::RX_QUEUE rxQueue;
245245

246246
static SPI spi;
247247
static CS chipSelect;
248248
static INT interruptPin;
249249

250-
modm::can::Message messageBuffer_;
251-
modm::ShortTimeout delayR_;
252-
modm::ShortTimeout delayS_;
253-
uint8_t statusBuffer_ = 0;
254-
uint8_t statusBufferR_ = 0;
255-
uint8_t statusBufferS_ = 0;
256-
uint8_t statusBufferReady_ = 0;
257-
uint8_t addressBufferR_ = 0;
258-
uint8_t addressBufferS_ = 0;
259-
uint8_t i_, j_ = 0;
260-
uint8_t a_ = 0;
261-
uint8_t b_ = 0;
262-
uint8_t data_ = 0;
263-
bool readTemp_ = false;
264-
bool tempS_ = false;
265-
bool temp_ = false;
266-
bool receiveSuccess_ = false;
250+
modm::can::Message messageBuffer;
251+
modm::ShortTimeout delayS;
252+
uint8_t statusBuffer = 0;
253+
uint8_t statusBufferR = 0;
254+
uint8_t statusBufferS = 0;
255+
uint8_t statusBufferReady = 0;
256+
uint8_t addressBufferR = 0;
257+
uint8_t addressBufferS = 0;
258+
uint8_t i, j = 0;
259+
uint8_t a = 0;
260+
uint8_t b = 0;
261+
uint8_t data = 0;
262+
bool readTemp = false;
263+
bool tempS = false;
264+
bool temp = false;
265+
bool receiveSuccess = false;
267266
};
268267
}
269268

src/modm/driver/can/mcp2515.lb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ def prepare(module, options):
2525
"is selected according to the clock speed.",
2626
default="16MHz"))
2727

28+
module.add_option(
29+
NumericOption(
30+
name="buffer.tx",
31+
description="",
32+
minimum=1, maximum=2 ** 16 - 2,
33+
default=32))
34+
module.add_option(
35+
NumericOption(
36+
name="buffer.rx",
37+
description="",
38+
minimum=1, maximum=2 ** 16 - 2,
39+
default=32))
40+
2841
module.depends(
2942
":architecture:accessor",
3043
":architecture:assert",
@@ -44,3 +57,5 @@ def build(env):
4457
env.copy("mcp2515_bit_timings.hpp")
4558
env.copy("mcp2515_definitions.hpp")
4659
env.template("mcp2515.cpp.in")
60+
env.template("mcp2515_options.hpp.in")
61+

src/modm/driver/can/mcp2515_impl.hpp

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#error "Don't include this file directly, use 'mcp2515.hpp' instead!"
1818
#endif
1919
#include "mcp2515_bit_timings.hpp"
20-
#include "mcp2515_definitions.hpp"
20+
#include "mcp2515_options.hpp"
2121
#include <modm/architecture/interface/assert.hpp>
2222

2323
// Set the log level
@@ -234,46 +234,46 @@ modm::Mcp2515<SPI, CS, INT>::mcp2515ReadMessage()
234234
RF_BEGIN();
235235

236236
// read status flag of the device
237-
statusBufferR_ = RF_CALL(readStatus(RX_STATUS));
237+
statusBufferR = RF_CALL(readStatus(RX_STATUS));
238238

239-
readTemp_ = true;
240-
if (statusBufferR_ & FLAG_RXB0_FULL) {
241-
addressBufferR_ = READ_RX; // message in buffer 0
239+
readTemp = true;
240+
if (statusBufferR & FLAG_RXB0_FULL) {
241+
addressBufferR = READ_RX; // message in buffer 0
242242
}
243-
else if (statusBufferR_ & FLAG_RXB1_FULL) {
244-
addressBufferR_ = READ_RX | 0x04; // message in buffer 1 (RXB1SIDH)
243+
else if (statusBufferR & FLAG_RXB1_FULL) {
244+
addressBufferR = READ_RX | 0x04; // message in buffer 1 (RXB1SIDH)
245245
}
246246
else {
247-
readTemp_ = false; // Error: no message available
247+
readTemp = false; // Error: no message available
248248
}
249249

250-
if(readTemp_)
250+
if(readTemp)
251251
{
252252
RF_WAIT_UNTIL(this->acquireMaster());
253253
chipSelect.reset();
254-
RF_CALL(spi.transfer(addressBufferR_));
254+
RF_CALL(spi.transfer(addressBufferR));
255255

256-
messageBuffer_.flags.extended = RF_CALL(readIdentifier(messageBuffer_.identifier));
257-
if (statusBufferR_ & FLAG_RTR) {
258-
messageBuffer_.flags.rtr = true;
256+
messageBuffer.flags.extended = RF_CALL(readIdentifier(messageBuffer.identifier));
257+
if (statusBufferR & FLAG_RTR) {
258+
messageBuffer.flags.rtr = true;
259259
}
260260
else {
261-
messageBuffer_.flags.rtr = false;
261+
messageBuffer.flags.rtr = false;
262262
}
263263

264-
messageBuffer_.length = RF_CALL(spi.transfer(0xff)) & 0x0f;
264+
messageBuffer.length = RF_CALL(spi.transfer(0xff)) & 0x0f;
265265

266-
for (j_ = 0; j_ < messageBuffer_.length; ++j_) {
267-
data_ = RF_CALL(spi.transfer(0xff));
268-
messageBuffer_.data[j_] = data_;
266+
for (j = 0; j < messageBuffer.length; ++j) {
267+
data = RF_CALL(spi.transfer(0xff));
268+
messageBuffer.data[j] = data;
269269
}
270270
RF_WAIT_UNTIL(this->releaseMaster());
271271
chipSelect.set();
272272
}
273273
// RX0IF or RX1IF respectivly were already cleared automatically by rising CS.
274274
// See section 12.4 in datasheet.
275275

276-
RF_END_RETURN(readTemp_);
276+
RF_END_RETURN(readTemp);
277277
}
278278

279279
template <typename SPI, typename CS, typename INT>
@@ -288,7 +288,7 @@ modm::Mcp2515<SPI, CS, INT>::update(){
288288
if(!interruptPin.read()){
289289
if(RF_CALL(mcp2515ReadMessage()))
290290
{
291-
if(not modm_assert_continue_ignore(rxQueue.push(messageBuffer_), "mcp2515.can.tx",
291+
if(not modm_assert_continue_ignore(rxQueue.push(messageBuffer), "mcp2515.can.tx",
292292
"CAN transmit software buffer overflowed!", 1)){}
293293
}
294294
}
@@ -313,16 +313,16 @@ modm::Mcp2515<SPI, CS, INT>::mcp2515IsReadyToSend()
313313

314314
RF_BEGIN();
315315

316-
tempS_ = true;
317-
statusBufferReady_ = RF_CALL(readStatus(READ_STATUS));
318-
if (( statusBufferReady_& (TXB2CNTRL_TXREQ | TXB1CNTRL_TXREQ | TXB0CNTRL_TXREQ)) ==
316+
tempS = true;
317+
statusBufferReady = RF_CALL(readStatus(READ_STATUS));
318+
if (( statusBufferReady& (TXB2CNTRL_TXREQ | TXB1CNTRL_TXREQ | TXB0CNTRL_TXREQ)) ==
319319
(TXB2CNTRL_TXREQ | TXB1CNTRL_TXREQ | TXB0CNTRL_TXREQ))
320320
{
321321
// all buffers currently in use
322-
tempS_ = false;
322+
tempS = false;
323323
}
324324

325-
RF_END_RETURN(tempS_);
325+
RF_END_RETURN(tempS);
326326
}
327327
template <typename SPI, typename CS, typename INT>
328328
bool
@@ -347,32 +347,32 @@ modm::Mcp2515<SPI, CS, INT>::mcp2515SendMessage(const can::Message& message)
347347
using namespace modm::mcp2515;
348348
RF_BEGIN();
349349

350-
statusBufferS_ = RF_CALL(readStatus(READ_STATUS));
350+
statusBufferS = RF_CALL(readStatus(READ_STATUS));
351351

352-
addressBufferS_ = static_cast<uint8_t>(false);
352+
addressBufferS = static_cast<uint8_t>(false);
353353

354354
// /// send if ready, else return that nothing was sent
355-
if(mcp2515IsReadyToSend(statusBufferS_))
355+
if(mcp2515IsReadyToSend(statusBufferS))
356356
{
357-
if ((statusBufferS_ & TXB0CNTRL_TXREQ) == 0)
357+
if ((statusBufferS & TXB0CNTRL_TXREQ) == 0)
358358
{
359-
addressBufferS_ = 0x00; // TXB0SIDH
360-
} else if ((statusBufferS_ & TXB1CNTRL_TXREQ) == 0)
359+
addressBufferS = 0x00; // TXB0SIDH
360+
} else if ((statusBufferS & TXB1CNTRL_TXREQ) == 0)
361361
{
362-
addressBufferS_ = 0x02; // TXB1SIDH
363-
} else if ((statusBufferS_ & TXB2CNTRL_TXREQ) == 0)
362+
addressBufferS = 0x02; // TXB1SIDH
363+
} else if ((statusBufferS & TXB2CNTRL_TXREQ) == 0)
364364
{
365-
addressBufferS_ = 0x04; // TXB2SIDH
365+
addressBufferS = 0x04; // TXB2SIDH
366366
} else
367367
{
368368
// all buffer are in use => could not send the message
369369
}
370370

371-
if (addressBufferS_ == 0x00 || addressBufferS_ == 0x02 || addressBufferS_ == 0x04)
371+
if (addressBufferS == 0x00 || addressBufferS == 0x02 || addressBufferS == 0x04)
372372
{
373373
RF_WAIT_UNTIL(this->acquireMaster());
374374
chipSelect.reset();
375-
RF_CALL(spi.transfer(WRITE_TX | addressBufferS_));
375+
RF_CALL(spi.transfer(WRITE_TX | addressBufferS));
376376
RF_CALL(writeIdentifier(message.identifier, message.flags.extended));
377377

378378
// if the message is a rtr-frame, is has a length but no attached data
@@ -383,24 +383,24 @@ modm::Mcp2515<SPI, CS, INT>::mcp2515SendMessage(const can::Message& message)
383383
{
384384
RF_CALL(spi.transfer(message.length));
385385

386-
for (i_ = 0; i_ < message.length; ++i_) {
387-
RF_CALL(spi.transfer(message.data[i_]));
386+
for (i = 0; i < message.length; ++i) {
387+
RF_CALL(spi.transfer(message.data[i]));
388388
}
389389
}
390-
delayS_.restart(1ms);
390+
delayS.restart(1ms);
391391
chipSelect.set();
392-
RF_WAIT_UNTIL(delayS_.isExpired());
392+
RF_WAIT_UNTIL(delayS.isExpired());
393393

394394
// send message via RTS command
395395
chipSelect.reset();
396-
addressBufferS_ = (addressBufferS_ == 0) ? 1 : addressBufferS_; // 0 2 4 => 1 2 4
397-
RF_CALL(spi.transfer(RTS | addressBufferS_));
396+
addressBufferS = (addressBufferS == 0) ? 1 : addressBufferS; // 0 2 4 => 1 2 4
397+
RF_CALL(spi.transfer(RTS | addressBufferS));
398398
RF_WAIT_UNTIL(this->releaseMaster());
399399
chipSelect.set();
400400
}
401401
}
402402

403-
RF_END_RETURN(static_cast<bool>(addressBufferS_));
403+
RF_END_RETURN(static_cast<bool>(addressBufferS));
404404
}
405405

406406
// ----------------------------------------------------------------------------
@@ -449,11 +449,11 @@ modm::Mcp2515<SPI, CS, INT>::readStatus(uint8_t type)
449449
RF_WAIT_UNTIL(this->acquireMaster());
450450
chipSelect.reset();
451451
RF_CALL(spi.transfer(type));
452-
statusBuffer_ = RF_CALL(spi.transfer(0xff));
452+
statusBuffer = RF_CALL(spi.transfer(0xff));
453453
RF_WAIT_UNTIL(this->releaseMaster());
454454
chipSelect.set();
455455

456-
RF_END_RETURN(statusBuffer_);
456+
RF_END_RETURN(statusBuffer);
457457
}
458458

459459
// ----------------------------------------------------------------------------
@@ -474,10 +474,10 @@ modm::Mcp2515<SPI, CS, INT>::writeIdentifier(const uint32_t& identifier,
474474
RF_CALL(spi.transfer(*((uint16_t *)ptr + 1) >> 5));
475475

476476
// calculate the next values
477-
a_ = (*((uint8_t *)ptr + 2) << 3) & 0xe0;
478-
a_ |= MCP2515_IDE;
479-
a_ |= (*((uint8_t *)ptr + 2)) & 0x03;
480-
RF_CALL(spi.transfer(a_));
477+
a = (*((uint8_t *)ptr + 2) << 3) & 0xe0;
478+
a |= MCP2515_IDE;
479+
a |= (*((uint8_t *)ptr + 2)) & 0x03;
480+
RF_CALL(spi.transfer(a));
481481
RF_CALL(spi.transfer(*((uint8_t *)ptr + 1)));
482482
RF_CALL(spi.transfer(*((uint8_t *)ptr)));
483483
} else
@@ -502,36 +502,36 @@ modm::Mcp2515<SPI, CS, INT>::readIdentifier(uint32_t &identifier)
502502
RF_BEGIN();
503503

504504
RF_WAIT_UNTIL(this->acquireMaster());
505-
a_ = RF_CALL(spi.transfer(0xff));
506-
b_ = RF_CALL(spi.transfer(0xff));
505+
a = RF_CALL(spi.transfer(0xff));
506+
b = RF_CALL(spi.transfer(0xff));
507507

508-
temp_ = false;
508+
temp = false;
509509

510-
if (b_ & MCP2515_IDE)
510+
if (b & MCP2515_IDE)
511511
{
512-
*((uint16_t *)ptr + 1) = (uint16_t)a_ << 5;
512+
*((uint16_t *)ptr + 1) = (uint16_t)a << 5;
513513
*((uint8_t *)ptr + 1) = RF_CALL(spi.transfer(0xff));
514514

515-
*((uint8_t *)ptr + 2) |= (b_ >> 3) & 0x1C;
516-
*((uint8_t *)ptr + 2) |= b_ & 0x03;
515+
*((uint8_t *)ptr + 2) |= (b >> 3) & 0x1C;
516+
*((uint8_t *)ptr + 2) |= b & 0x03;
517517

518518
*((uint8_t *)ptr) = RF_CALL(spi.transfer(0xff));
519519

520-
temp_ = true;
520+
temp = true;
521521
} else
522522
{
523523
RF_CALL(spi.transfer(0xff));
524524

525525
*((uint8_t *)ptr + 3) = 0;
526526
*((uint8_t *)ptr + 2) = 0;
527527

528-
*((uint16_t *)ptr) = (uint16_t)a_ << 3;
528+
*((uint16_t *)ptr) = (uint16_t)a << 3;
529529

530530
RF_CALL(spi.transfer(0xff));
531531

532-
*((uint8_t *)ptr) |= b_ >> 5;
532+
*((uint8_t *)ptr) |= b >> 5;
533533
}
534534
RF_WAIT_UNTIL(this->releaseMaster());
535535

536-
RF_END_RETURN(temp_);
536+
RF_END_RETURN(temp);
537537
}

0 commit comments

Comments
 (0)