Skip to content

Commit 10b0e46

Browse files
committed
added example; small line cleanup
1 parent 148e788 commit 10b0e46

File tree

3 files changed

+118
-3
lines changed

3 files changed

+118
-3
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2013, Kevin Läufer
3+
* Copyright (c) 2013-2017, Niklas Hauser
4+
* Copyright (c) 2016, Raphael Lehmann
5+
*
6+
* This file is part of the modm project.
7+
*
8+
* This Source Code Form is subject to the terms of the Mozilla Public
9+
* License, v. 2.0. If a copy of the MPL was not distributed with this
10+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
*/
12+
// ----------------------------------------------------------------------------
13+
14+
#include <modm/board.hpp>
15+
#include <modm/driver/can/mcp2515.hpp>
16+
#include <modm/processing/protothread.hpp>
17+
18+
// If you use a different SPI instance, you may have to also choose different
19+
// GPIOs to connect to.
20+
using Int = GpioOutputB11;
21+
using Cs = GpioOutputB12;
22+
using Sck = GpioOutputB13;
23+
using Mosi = GpioOutputB15;
24+
using Miso = GpioInputB14;
25+
using SpiMaster = SpiMaster2;
26+
// Note that you can also use a bit-banged SPI driver as a drop-in replacement
27+
// using SpiMaster = BitBangSpiMaster<Sck, Mosi, Miso>;
28+
29+
// Default filters to receive any extended CAN frame
30+
FLASH_STORAGE(uint8_t canFilter[]) = {
31+
MCP2515_FILTER_EXTENDED(0), // Filter 0
32+
MCP2515_FILTER_EXTENDED(0), // Filter 1
33+
34+
MCP2515_FILTER_EXTENDED(0), // Filter 2
35+
MCP2515_FILTER_EXTENDED(0), // Filter 3
36+
MCP2515_FILTER_EXTENDED(0), // Filter 4
37+
MCP2515_FILTER_EXTENDED(0), // Filter 5
38+
39+
MCP2515_FILTER_EXTENDED(0), // Mask 0
40+
MCP2515_FILTER_EXTENDED(0), // Mask 1
41+
};
42+
43+
modm::Mcp2515<SpiMaster, Cs, Int> mcp2515;
44+
45+
class MyTask : modm::pt::Protothread
46+
{
47+
public:
48+
MyTask() : message_{0x123456}
49+
{
50+
mcp2515.initialize<8_MHz, 125_kbps>();
51+
mcp2515.setFilter(modm::accessor::asFlash(canFilter));
52+
}
53+
54+
bool
55+
run()
56+
{
57+
PT_BEGIN();
58+
while (true)
59+
{
60+
// send a new message
61+
message_.length = 2;
62+
message_.data[0] = 0xab;
63+
message_.data[1] = 0xcd;
64+
mcp2515.sendMessage(message_);
65+
66+
if (mcp2515.isMessageAvailable())
67+
{
68+
mcp2515.getMessage(message_);
69+
MODM_LOG_INFO << "Received message: " << message_.identifier << modm::endl;
70+
}
71+
}
72+
PT_END();
73+
}
74+
75+
private:
76+
modm::can::Message message_;
77+
};
78+
79+
MyTask task;
80+
81+
int
82+
main()
83+
{
84+
Board::initialize();
85+
86+
// Initialize SPI interface and the other pins
87+
// needed by the MCP2515
88+
SpiMaster::connect<Miso::Miso, Mosi::Mosi, Sck::Sck>();
89+
/// we initialize a higher baud rate then n the avr example, dunnow hats the mcp2515 is capable
90+
/// of
91+
SpiMaster::initialize<Board::SystemClock, 20_MHz>();
92+
Cs::setOutput();
93+
Int::setInput(Gpio::InputType::PullUp);
94+
95+
// Configure MCP2515 and set the filters
96+
mcp2515.initialize<8_MHz, 125_kbps>();
97+
98+
while (true) { task.run(); }
99+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<library>
2+
<extends>modm:disco-f407vg</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/stm32f4_discovery/spi</option>
5+
</options>
6+
<modules>
7+
<module>modm:platform:gpio</module>
8+
<module>modm:platform:spi.bitbang</module>
9+
<module>modm:platform:spi:2</module>
10+
<module>modm:build:scons</module>
11+
12+
13+
<module>modm:processing:protothread</module>
14+
<module>modm:processing:timer</module>
15+
<module>modm:driver:mcp2515</module>
16+
17+
</modules>
18+
</library>

src/modm/driver/can/mcp2515_impl.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ modm::Mcp2515<SPI, CS, INT>::initialize()
124124
using Timings = modm::CanBitTimingMcp2515<externalClockFrequency, bitrate>;
125125

126126
// initialize interrrupt on INT pin
127-
128127
modm::platform::Exti::connect<INT>(modm::platform::Exti::Trigger::FallingEdge, [](uint8_t){
129128
mcp2515interrupt();
130129
});
@@ -146,8 +145,7 @@ modm::Mcp2515<SPI, CS, INT>::setFilter(accessor::Flash<uint8_t> filter)
146145

147146
// change to configuration mode
148147
bitModify(CANCTRL, 0xe0, REQOP2);
149-
while ((readRegister(CANSTAT) & 0xe0) != REQOP2)
150-
;
148+
while ((readRegister(CANSTAT) & 0xe0) != REQOP2);
151149

152150
writeRegister(RXB0CTRL, BUKT);
153151
writeRegister(RXB1CTRL, 0);

0 commit comments

Comments
 (0)