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