Skip to content

Commit 5ac11f4

Browse files
committed
fixed some things related to comments from @rleh; cleanup of exmaple code; minor style changes to mcp2515 driver impl
1 parent dadd55f commit 5ac11f4

File tree

4 files changed

+55
-58
lines changed

4 files changed

+55
-58
lines changed

examples/nucleo_f439zi/can_m2515/main.cpp

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include <modm/processing/protothread.hpp>
1818
#include <modm/driver/can/mcp2515.hpp>
1919

20-
#define SENDER 1
21-
2220
// Set the log level
2321
#undef MODM_LOG_LEVEL
2422
#define MODM_LOG_LEVEL modm::log::DEBUG
@@ -36,27 +34,28 @@ using SpiMaster = SpiMaster1;
3634

3735
// Default filters to receive any extended CAN frame
3836
FLASH_STORAGE(uint8_t canFilter[]) = {
39-
MCP2515_FILTER_EXTENDED(0), // Filter 0
40-
MCP2515_FILTER_EXTENDED(0), // Filter 1
37+
MCP2515_FILTER_EXTENDED(0), // Filter 0
38+
MCP2515_FILTER_EXTENDED(0), // Filter 1
4139

42-
MCP2515_FILTER_EXTENDED(0), // Filter 2
43-
MCP2515_FILTER_EXTENDED(0), // Filter 3
44-
MCP2515_FILTER_EXTENDED(0), // Filter 4
45-
MCP2515_FILTER_EXTENDED(0), // Filter 5
40+
MCP2515_FILTER(0), // Filter 2
41+
MCP2515_FILTER(0), // Filter 3
42+
MCP2515_FILTER(0), // Filter 4
43+
MCP2515_FILTER(0), // Filter 5
4644

47-
MCP2515_FILTER_EXTENDED(0), // Mask 0
48-
MCP2515_FILTER_EXTENDED(0), // Mask 1
45+
MCP2515_MASK_EXTENDED(0), // Mask 0
46+
MCP2515_MASK(0), // Mask 1
4947
};
5048

5149
modm::Mcp2515<SpiMaster, Cs, Int> mcp2515;
5250

53-
class MyTask : modm::pt::Protothread
51+
class CanThread : modm::pt::Protothread
5452
{
5553
public:
56-
MyTask() :
57-
message_{0x1337},
58-
i_{0},
59-
j_{0}
54+
CanThread() :
55+
message_{},
56+
wait_{1s},
57+
i{0},
58+
j{0}
6059
{}
6160

6261
bool
@@ -65,75 +64,77 @@ class MyTask : modm::pt::Protothread
6564
PT_BEGIN();
6665

6766
MODM_LOG_INFO << "Initializing mcp2515 ..." << modm::endl;
68-
// Configure MCP2515 and set the filters
69-
initialized_ = mcp2515.initialize<8_MHz, 500_kbps>();
70-
MODM_LOG_INFO << "Success: " << initialized_ << modm::endl;
67+
// Configure MCP2515
68+
while (!mcp2515.initialize<8_MHz, 500_kbps>()){
69+
PT_WAIT_UNTIL(wait_.isExpired());
70+
wait_.restart();
71+
}
72+
/// Set filters of MCP2515
73+
MODM_LOG_INFO << "Setting filters of mcp2515 ..." << modm::endl;
7174
mcp2515.setFilter(modm::accessor::asFlash(canFilter));
7275
MODM_LOG_INFO << "Running ... " << modm::endl;
73-
#if SENDER == 0
74-
while (initialized_)
76+
while (true)
7577
{
7678
// receive messages
7779
if (mcp2515.isMessageAvailable())
7880
{
7981
MODM_LOG_INFO << "Message Available ... " << modm::endl;
8082
mcp2515.getMessage(message_);
81-
for(i_ = 0; i_ < message_.length; ++i_){
82-
MODM_LOG_INFO << modm::hex<< " 0x" << message_.data[i_];
83+
MODM_LOG_INFO << "Received message: " << modm::hex << message_.identifier << modm::endl;
84+
for(i = 0; i < message_.length; ++i){
85+
MODM_LOG_INFO << modm::hex<< " 0x" << message_.data[i];
8386
}
8487
MODM_LOG_INFO << modm::endl;
85-
MODM_LOG_INFO << "Received message [" << j_ << "]: " << modm::hex << message_.identifier << modm::endl;
86-
j_+=1;
8788
MODM_LOG_INFO << modm::endl;
8889
}
89-
PT_CALL(mcp2515.update());
90-
}
91-
#else
92-
while (initialized_)
93-
{
94-
wait_.restart(1000ms);
95-
PT_WAIT_UNTIL(wait_.isExpired());
96-
message_.length = 2;
97-
message_.data[0] = i_++;
98-
message_.data[1] = i_++;
99-
MODM_LOG_INFO << "Sending Message ... "<< modm::endl;
100-
for(j_ = 0; j_ < message_.length; ++j_){
101-
MODM_LOG_INFO << modm::hex<< " 0x" << message_.data[j_];
90+
91+
if(wait_.isExpired())
92+
{
93+
wait_.restart(1000ms);
94+
message_.identifier = 0xAA;
95+
message_.length = 2;
96+
message_.data[0] = 13;
97+
message_.data[1] = 37;
98+
MODM_LOG_INFO << "Sending Message ... "<< modm::endl;
99+
for(j = 0; j < message_.length; ++j){
100+
MODM_LOG_INFO << modm::hex<< " 0x" << message_.data[j];
101+
}
102+
MODM_LOG_INFO << modm::endl;
103+
MODM_LOG_INFO << "Success: " << mcp2515.sendMessage(message_) << modm::endl;
102104
}
103-
MODM_LOG_INFO << modm::endl;
104-
mcp2515.sendMessage(message_);
105+
106+
/// process internal mcp2515 queues
105107
PT_CALL(mcp2515.update());
108+
PT_YIELD();
106109
}
107-
#endif
108110

109111
PT_END();
110112
}
111113

112114
private:
113115
modm::can::Message message_;
114-
bool initialized_;
115116
modm::ShortTimeout wait_;
116-
uint8_t i_, j_;
117+
uint8_t i, j;
117118
};
118119

119-
MyTask task;
120+
CanThread canThread;
120121

121122
int
122123
main()
123124
{
124125
Board::initialize();
125126

126-
MODM_LOG_INFO << "Hello" << modm::endl;
127+
MODM_LOG_INFO << "Mcp2515 Example" << modm::endl;
127128
// Initialize SPI interface and the other pins
128129
// needed by the MCP2515
129130
SpiMaster::connect<Miso::Miso, Mosi::Mosi, Sck::Sck>();
130131
/// we initialize a higher baud rate then n the avr example, dunnow hats the mcp2515 is capable
131132
/// of
132-
SpiMaster::initialize<Board::SystemClock, 20_MHz>();
133+
SpiMaster::initialize<Board::SystemClock, 10_MHz>();
133134
Cs::setOutput();
134135
Int::setInput(Gpio::InputType::PullUp);
135136

136137
while (true) {
137-
task.run();
138+
canThread.run();
138139
}
139140
}

src/modm/driver/can/mcp2515.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ namespace modm
260260
uint8_t a_ = 0;
261261
uint8_t b_ = 0;
262262
uint8_t data_ = 0;
263-
bool tempR_ = false;
263+
bool readTemp_ = false;
264264
bool tempS_ = false;
265265
bool temp_ = false;
266266
bool receiveSuccess_ = false;

src/modm/driver/can/mcp2515.lb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def prepare(module, options):
3131
":architecture:can",
3232
":architecture:clock",
3333
":architecture:delay",
34-
":architecture:interrupt",
3534
":architecture:spi.device",
3635
":processing:protothread",
3736
":processing:timer",

src/modm/driver/can/mcp2515_impl.hpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,12 @@ 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;
220219
if (not modm_assert_continue_ignore(txQueue.push(message), "mcp2515.can.tx",
221220
"CAN transmit software buffer overflowed!", 1)) {
222221
/// buffer full, could not send
223-
success = false;
222+
return false;
224223
}
225-
return success;
224+
return true;
226225
}
227226

228227
// ----------------------------------------------------------------------------
@@ -237,18 +236,18 @@ modm::Mcp2515<SPI, CS, INT>::mcp2515ReadMessage()
237236
// read status flag of the device
238237
statusBufferR_ = RF_CALL(readStatus(RX_STATUS));
239238

240-
tempR_ = true;
239+
readTemp_ = true;
241240
if (statusBufferR_ & FLAG_RXB0_FULL) {
242241
addressBufferR_ = READ_RX; // message in buffer 0
243242
}
244243
else if (statusBufferR_ & FLAG_RXB1_FULL) {
245244
addressBufferR_ = READ_RX | 0x04; // message in buffer 1 (RXB1SIDH)
246245
}
247246
else {
248-
tempR_ = false; // Error: no message available
247+
readTemp_ = false; // Error: no message available
249248
}
250249

251-
if(tempR_)
250+
if(readTemp_)
252251
{
253252
RF_WAIT_UNTIL(this->acquireMaster());
254253
chipSelect.reset();
@@ -274,7 +273,7 @@ modm::Mcp2515<SPI, CS, INT>::mcp2515ReadMessage()
274273
// RX0IF or RX1IF respectivly were already cleared automatically by rising CS.
275274
// See section 12.4 in datasheet.
276275

277-
RF_END_RETURN(tempR_);
276+
RF_END_RETURN(readTemp_);
278277
}
279278

280279
template <typename SPI, typename CS, typename INT>
@@ -290,9 +289,7 @@ modm::Mcp2515<SPI, CS, INT>::update(){
290289
if(RF_CALL(mcp2515ReadMessage()))
291290
{
292291
if(not modm_assert_continue_ignore(rxQueue.push(messageBuffer_), "mcp2515.can.tx",
293-
"CAN transmit software buffer overflowed!", 1)){
294-
/// ignore
295-
}
292+
"CAN transmit software buffer overflowed!", 1)){}
296293
}
297294
}
298295

0 commit comments

Comments
 (0)