Skip to content

Commit 3db3bcd

Browse files
committed
[examples] Add MCP2515 example
1 parent db9f10e commit 3db3bcd

File tree

6 files changed

+142
-13
lines changed

6 files changed

+142
-13
lines changed

examples/avr/can/mcp2515/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef SpiMaster SPI;
3030
modm::Mcp2515<SPI, Cs, Int> mcp2515;
3131

3232
// Default filters to receive any extended CAN frame
33-
FLASH_STORAGE(uint8_t canFilter[]) =
33+
const uint8_t canFilter[] =
3434
{
3535
MCP2515_FILTER_EXTENDED(0), // Filter 0
3636
MCP2515_FILTER_EXTENDED(0), // Filter 1
@@ -56,7 +56,7 @@ main()
5656

5757
// Configure MCP2515 and set the filters
5858
mcp2515.initialize<8_MHz, 125_kbps>();
59-
mcp2515.setFilter(modm::accessor::asFlash(canFilter));
59+
mcp2515.setFilter(canFilter);
6060

6161
// Create a new message
6262
modm::can::Message message(0x123456);

examples/avr/can/mcp2515_uart/main.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
#include <modm/platform.hpp>
1515
#include <modm/driver/can/mcp2515.hpp>
16-
#include <modm/processing/timer.hpp>
16+
#include <modm/processing.hpp>
17+
#include <modm/io.hpp>
1718

1819
using namespace modm::platform;
1920
using namespace modm::literals;
@@ -34,7 +35,7 @@ typedef BitBangSpiMaster<Sclk, Mosi, Miso> SPI;
3435
modm::Mcp2515<SPI, Cs, Int> mcp2515;
3536

3637
// Default filters to receive any extended CAN frame
37-
FLASH_STORAGE(uint8_t canFilter[]) =
38+
const uint8_t canFilter[] =
3839
{
3940
MCP2515_FILTER_EXTENDED(0), // Filter 0
4041
MCP2515_FILTER_EXTENDED(0), // Filter 1
@@ -48,6 +49,10 @@ FLASH_STORAGE(uint8_t canFilter[]) =
4849
MCP2515_FILTER_EXTENDED(0), // Mask 1
4950
};
5051

52+
// Create a IOStream for complex formatting tasks
53+
modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull > device;
54+
modm::IOStream stream(device);
55+
5156
int
5257
main()
5358
{
@@ -58,10 +63,6 @@ main()
5863
Uart0::connect<GpioD1::Txd, GpioD0::Rxd>();
5964
Uart0::initialize<SystemClock, 115200_Bd>();
6065

61-
// Create a IOStream for complex formatting tasks
62-
modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull > device;
63-
modm::IOStream stream(device);
64-
6566
// enable interrupts
6667
enableInterrupts();
6768

@@ -76,7 +77,7 @@ main()
7677

7778
// Configure MCP2515 and set the filters
7879
mcp2515.initialize<8_MHz, 125_kbps>();
79-
mcp2515.setFilter(modm::accessor::asFlash(canFilter));
80+
mcp2515.setFilter(canFilter);
8081

8182
// Create a new message
8283
modm::can::Message message(0x123456);

examples/avr/xpcc/receiver/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Postman postman;
6363
xpcc::Dispatcher dispatcher(&connector, &postman);
6464

6565
// Default filters to receive any extended CAN frame
66-
FLASH_STORAGE(uint8_t canFilter[]) =
66+
const uint8_t canFilter[] =
6767
{
6868
MCP2515_FILTER_EXTENDED(0), // Filter 0
6969
MCP2515_FILTER_EXTENDED(0), // Filter 1
@@ -98,7 +98,7 @@ main()
9898

9999
// Configure MCP2515 and set the filters
100100
device.initialize<8_MHz, 125_kbps>();
101-
device.setFilter(modm::accessor::asFlash(canFilter));
101+
device.setFilter(canFilter);
102102

103103
// Enable Interrupts
104104
enableInterrupts();

examples/avr/xpcc/sender/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Postman postman;
6363
xpcc::Dispatcher dispatcher(&connector, &postman);
6464

6565
// Default filters to receive any extended CAN frame
66-
FLASH_STORAGE(uint8_t canFilter[]) =
66+
const uint8_t canFilter[] =
6767
{
6868
MCP2515_FILTER_EXTENDED(0), // Filter 0
6969
MCP2515_FILTER_EXTENDED(0), // Filter 1
@@ -98,7 +98,7 @@ main()
9898

9999
// Configure MCP2515 and set the filters
100100
device.initialize<8_MHz, 125_kbps>();
101-
device.setFilter(modm::accessor::asFlash(canFilter));
101+
device.setFilter(canFilter);
102102

103103
// Enable Interrupts
104104
enableInterrupts();
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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/processing.hpp>
16+
#include <modm/driver/can/mcp2515.hpp>
17+
18+
// Set the log level
19+
#undef MODM_LOG_LEVEL
20+
#define MODM_LOG_LEVEL modm::log::DEBUG
21+
22+
// If you use a different SPI instance, you may have to also choose different
23+
// GPIOs to connect to.
24+
using Cs = GpioOutputA4;
25+
using Mosi = GpioOutputB5;
26+
using Miso = GpioInputB4;
27+
using Sck = GpioOutputB3;
28+
using Int = GpioInputC7;
29+
using SpiMaster = SpiMaster1;
30+
// Note that you can also use a bit-banged SPI driver as a drop-in replacement
31+
// using SpiMaster = BitBangSpiMaster<Sck, Mosi, Miso>;
32+
33+
// Default filters to receive any extended CAN frame
34+
const uint8_t canFilter[] =
35+
{
36+
MCP2515_FILTER_EXTENDED(0), // Filter 0
37+
MCP2515_FILTER_EXTENDED(0), // Filter 1
38+
39+
MCP2515_FILTER(0), // Filter 2
40+
MCP2515_FILTER(0), // Filter 3
41+
MCP2515_FILTER(0), // Filter 4
42+
MCP2515_FILTER(0), // Filter 5
43+
44+
MCP2515_MASK_EXTENDED(0), // Mask 0
45+
MCP2515_MASK(0), // Mask 1
46+
};
47+
48+
modm::Mcp2515<SpiMaster, Cs, Int> mcp2515;
49+
50+
modm::Fiber fiber_mcp2515([]
51+
{
52+
MODM_LOG_INFO << "Initializing mcp2515 ..." << modm::endl;
53+
mcp2515.initialize<8_MHz, 500_kbps>();
54+
55+
while(1)
56+
{
57+
mcp2515.update();
58+
modm::this_fiber::yield();
59+
}
60+
});
61+
62+
modm::Fiber fiber_can([]
63+
{
64+
modm::can::Message msg;
65+
modm::ShortPeriodicTimer tmr{1s};
66+
uint8_t i, j;
67+
68+
/// Set filters of MCP2515
69+
MODM_LOG_INFO << "Setting filters of mcp2515 ..." << modm::endl;
70+
mcp2515.setFilter(canFilter);
71+
MODM_LOG_INFO << "Running ... " << modm::endl;
72+
while (true)
73+
{
74+
// receive messages
75+
if (mcp2515.isMessageAvailable())
76+
{
77+
MODM_LOG_INFO << "Message Available ... " << modm::endl;
78+
mcp2515.getMessage(msg);
79+
MODM_LOG_INFO << "Received message: " << modm::hex << msg.identifier << modm::endl;
80+
for(i = 0; i < msg.length; ++i){
81+
MODM_LOG_INFO << modm::hex << " 0x" << msg.data[i];
82+
}
83+
MODM_LOG_INFO << modm::endl << modm::endl;
84+
}
85+
86+
if(tmr.execute())
87+
{
88+
msg.identifier = 0xAA;
89+
msg.length = 2;
90+
msg.data[0] = 13;
91+
msg.data[1] = 37;
92+
MODM_LOG_INFO << "Sending Message ... "<< modm::endl;
93+
for(j = 0; j < msg.length; ++j){
94+
MODM_LOG_INFO << modm::hex<< " 0x" << msg.data[j];
95+
}
96+
MODM_LOG_INFO << modm::endl;
97+
MODM_LOG_INFO << "Success: " << mcp2515.sendMessage(msg) << modm::endl;
98+
}
99+
100+
modm::this_fiber::yield();
101+
}
102+
});
103+
104+
int
105+
main()
106+
{
107+
Board::initialize();
108+
SpiMaster::connect<Miso::Miso, Mosi::Mosi, Sck::Sck>();
109+
SpiMaster::initialize<Board::SystemClock, 10_MHz>();
110+
111+
MODM_LOG_INFO << "Mcp2515 Example" << modm::endl;
112+
113+
modm::fiber::Scheduler::run();
114+
return 0;
115+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<library>
2+
<extends>modm:nucleo-f439zi</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo-f439zi/mcp2515</option>
5+
</options>
6+
<modules>
7+
<module>modm:platform:spi:1</module>
8+
<module>modm:build:scons</module>
9+
<module>modm:processing:protothread</module>
10+
<module>modm:processing:timer</module>
11+
<module>modm:driver:mcp2515</module>
12+
</modules>
13+
</library>

0 commit comments

Comments
 (0)