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
+ }
0 commit comments