Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/avr/can/mcp2515/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef SpiMaster SPI;
modm::Mcp2515<SPI, Cs, Int> mcp2515;

// Default filters to receive any extended CAN frame
FLASH_STORAGE(uint8_t canFilter[]) =
const uint8_t canFilter[] =
{
MCP2515_FILTER_EXTENDED(0), // Filter 0
MCP2515_FILTER_EXTENDED(0), // Filter 1
Expand All @@ -56,7 +56,7 @@ main()

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

// Create a new message
modm::can::Message message(0x123456);
Expand Down
15 changes: 8 additions & 7 deletions examples/avr/can/mcp2515_uart/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

#include <modm/platform.hpp>
#include <modm/driver/can/mcp2515.hpp>
#include <modm/processing/timer.hpp>
#include <modm/processing.hpp>
#include <modm/io.hpp>

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

// Default filters to receive any extended CAN frame
FLASH_STORAGE(uint8_t canFilter[]) =
const uint8_t canFilter[] =
{
MCP2515_FILTER_EXTENDED(0), // Filter 0
MCP2515_FILTER_EXTENDED(0), // Filter 1
Expand All @@ -48,6 +49,10 @@ FLASH_STORAGE(uint8_t canFilter[]) =
MCP2515_FILTER_EXTENDED(0), // Mask 1
};

// Create a IOStream for complex formatting tasks
modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull > device;
modm::IOStream stream(device);

int
main()
{
Expand All @@ -58,10 +63,6 @@ main()
Uart0::connect<GpioD1::Txd, GpioD0::Rxd>();
Uart0::initialize<SystemClock, 115200_Bd>();

// Create a IOStream for complex formatting tasks
modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull > device;
modm::IOStream stream(device);

// enable interrupts
enableInterrupts();

Expand All @@ -76,7 +77,7 @@ main()

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

// Create a new message
modm::can::Message message(0x123456);
Expand Down
4 changes: 2 additions & 2 deletions examples/avr/xpcc/receiver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Postman postman;
xpcc::Dispatcher dispatcher(&connector, &postman);

// Default filters to receive any extended CAN frame
FLASH_STORAGE(uint8_t canFilter[]) =
const uint8_t canFilter[] =
{
MCP2515_FILTER_EXTENDED(0), // Filter 0
MCP2515_FILTER_EXTENDED(0), // Filter 1
Expand Down Expand Up @@ -98,7 +98,7 @@ main()

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

// Enable Interrupts
enableInterrupts();
Expand Down
4 changes: 2 additions & 2 deletions examples/avr/xpcc/sender/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Postman postman;
xpcc::Dispatcher dispatcher(&connector, &postman);

// Default filters to receive any extended CAN frame
FLASH_STORAGE(uint8_t canFilter[]) =
const uint8_t canFilter[] =
{
MCP2515_FILTER_EXTENDED(0), // Filter 0
MCP2515_FILTER_EXTENDED(0), // Filter 1
Expand Down Expand Up @@ -98,7 +98,7 @@ main()

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

// Enable Interrupts
enableInterrupts();
Expand Down
115 changes: 115 additions & 0 deletions examples/nucleo_f439zi/can_m2515/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2013, Kevin Läufer
* Copyright (c) 2013-2017, Niklas Hauser
* Copyright (c) 2016, Raphael Lehmann
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/processing.hpp>
#include <modm/driver/can/mcp2515.hpp>

// Set the log level
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG

// If you use a different SPI instance, you may have to also choose different
// GPIOs to connect to.
using Cs = GpioOutputA4;
using Mosi = GpioOutputB5;
using Miso = GpioInputB4;
using Sck = GpioOutputB3;
using Int = GpioInputC7;
using SpiMaster = SpiMaster1;
// Note that you can also use a bit-banged SPI driver as a drop-in replacement
// using SpiMaster = BitBangSpiMaster<Sck, Mosi, Miso>;

// Default filters to receive any extended CAN frame
const uint8_t canFilter[] =
{
MCP2515_FILTER_EXTENDED(0), // Filter 0
MCP2515_FILTER_EXTENDED(0), // Filter 1

MCP2515_FILTER(0), // Filter 2
MCP2515_FILTER(0), // Filter 3
MCP2515_FILTER(0), // Filter 4
MCP2515_FILTER(0), // Filter 5

MCP2515_MASK_EXTENDED(0), // Mask 0
MCP2515_MASK(0), // Mask 1
};

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

modm::Fiber fiber_mcp2515([]
{
MODM_LOG_INFO << "Initializing mcp2515 ..." << modm::endl;
mcp2515.initialize<8_MHz, 500_kbps>();

while(1)
{
mcp2515.update();
modm::this_fiber::yield();
}
});

modm::Fiber fiber_can([]
{
modm::can::Message msg;
modm::ShortPeriodicTimer tmr{1s};
uint8_t i, j;

/// Set filters of MCP2515
MODM_LOG_INFO << "Setting filters of mcp2515 ..." << modm::endl;
mcp2515.setFilter(canFilter);
MODM_LOG_INFO << "Running ... " << modm::endl;
while (true)
{
// receive messages
if (mcp2515.isMessageAvailable())
{
MODM_LOG_INFO << "Message Available ... " << modm::endl;
mcp2515.getMessage(msg);
MODM_LOG_INFO << "Received message: " << modm::hex << msg.identifier << modm::endl;
for(i = 0; i < msg.length; ++i){
MODM_LOG_INFO << modm::hex << " 0x" << msg.data[i];
}
MODM_LOG_INFO << modm::endl << modm::endl;
}

if(tmr.execute())
{
msg.identifier = 0xAA;
msg.length = 2;
msg.data[0] = 13;
msg.data[1] = 37;
MODM_LOG_INFO << "Sending Message ... "<< modm::endl;
for(j = 0; j < msg.length; ++j){
MODM_LOG_INFO << modm::hex<< " 0x" << msg.data[j];
}
MODM_LOG_INFO << modm::endl;
MODM_LOG_INFO << "Success: " << mcp2515.sendMessage(msg) << modm::endl;
}

modm::this_fiber::yield();
}
});

int
main()
{
Board::initialize();
SpiMaster::connect<Miso::Miso, Mosi::Mosi, Sck::Sck>();
SpiMaster::initialize<Board::SystemClock, 10_MHz>();

MODM_LOG_INFO << "Mcp2515 Example" << modm::endl;

modm::fiber::Scheduler::run();
return 0;
}
13 changes: 13 additions & 0 deletions examples/nucleo_f439zi/can_m2515/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<library>
<extends>modm:nucleo-f439zi</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo-f439zi/mcp2515</option>
</options>
<modules>
<module>modm:platform:spi:1</module>
<module>modm:build:scons</module>
<module>modm:processing:protothread</module>
<module>modm:processing:timer</module>
<module>modm:driver:mcp2515</module>
</modules>
</library>
40 changes: 0 additions & 40 deletions src/modm/architecture/interface/can.cpp

This file was deleted.

62 changes: 57 additions & 5 deletions src/modm/architecture/interface/can.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
*/
// ----------------------------------------------------------------------------

#ifndef MODM_INTERFACE_CAN_HPP
#define MODM_INTERFACE_CAN_HPP
#pragma once

#include <modm/architecture/interface/peripheral.hpp>
#include "can_message.hpp"
Expand Down Expand Up @@ -118,10 +117,63 @@ class Can : public ::modm::PeripheralDriver
#endif
};

/// Wraps an object CAN driver into the static CAN interface
/// @ingroup modm_architecture_can
modm::IOStream&
operator << (modm::IOStream& stream, const Can::BusState state);
template <class CanType, CanType &can>
struct StaticCanWrapper : public modm::Can
{
static bool
isMessageAvailable()
{ return can.isMessageAvailable(); }

static bool
getMessage(can::Message& message, uint8_t *filter_id=nullptr)
{
return can.getMessage(message, filter_id);
}

static bool
isReadyToSend()
{ return can.isReadyToSend(); }

static bool
sendMessage(const can::Message& message)
{ return can.sendMessage(message); }

static BusState
getBusState() { return can.getBusState(); }
};

} // namespace modm

#endif // MODM_INTERFACE_CAN_HPP
#if MODM_HAS_IOSTREAM
#include <modm/io/iostream.hpp>

namespace modm
{

/// @ingroup modm_architecture_can
inline modm::IOStream&
operator << (modm::IOStream& stream, const Can::BusState state)
{
switch (state)
{
case Can::BusState::Connected:
stream << "connected";
break;
case Can::BusState::ErrorWarning:
stream << "error warning";
break;
case Can::BusState::ErrorPassive:
stream << "error passive";
break;
case Can::BusState::Off:
stream << "off";
break;
}
return stream;
}

} // namespace modm

#endif
7 changes: 1 addition & 6 deletions src/modm/architecture/interface/can_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef MODM_CAN_FILTER_HPP
#define MODM_CAN_FILTER_HPP

#include <modm/io/iostream.hpp>
#pragma once

namespace modm::can
{
Expand Down Expand Up @@ -69,5 +66,3 @@ struct ExtendedMask
};

}

#endif
Loading
Loading