PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
MCP2515_Demo.cpp
cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(MCP2515_Demo)
create_simple_executable(${PROJECT_NAME} MCP2515_Demo.cpp)
static const Port::Mask MOSI = Port::Mask::P0;
static const Port::Mask MISO = Port::Mask::P1;
static const Port::Mask SCLK = Port::Mask::P2;
static const Port::Mask CS = Port::Mask::P7;
static const Port::Mask CLOCK_MASK = Port::Mask::P8;
static const uint8_t messages[][6] = {
"Hello",
"David",
"Katie"
};
void handle(const PropWare::ErrorCode err) {
if (err) {
pwOut.printf("ERROR!!! %d\n", err);
while (1)
waitcnt(0);
}
}
void read(MCP2515 &can, const MCP2515::BufferNumber bufferNumber) {
if (can.check_receive_buffer(bufferNumber)) {
uint8_t buffer[MCP2515::MAX_DATA_BYTES];
uint8_t bytesRead;
can.read_message(bufferNumber, &bytesRead, buffer);
pwOut << "Message: `" << (const char *) buffer << "`\n";
} else {
pwOut << "No message\n";
}
}
int main() {
// Instantiate the SPI bus and CAN controller instances
const SPI spi(MOSI, MISO, SCLK);
MCP2515 can(spi, CS);
// We'll use the Propeller's hardware counters as a clock source for the MCP2515
const Pin clock(CLOCK_MASK, Pin::Dir::OUT);
clock.start_hardware_pwm(8000000);
// Start the MCP2515 running a 1 Mbaud and in "loopback" mode. This means that all messages "sent" will be
// immediately looped back into the receive buffers. This is great for testing your configuration while still at
// your desk, to ensure that the filters and masks have been correctly configured, along with any other
// configuration. Once ready, remove the optional `mode` parameter and connect to your live system.
handle(can.start(MCP2515::BaudRate::BAUD_1000KBPS, MCP2515::Mode::LOOPBACK));
// Set up the filters and masks so that only message ID 2 is allowed through
handle(can.set_mask(MCP2515::BufferNumber::BUFFER_0, PropWare::WORD_0));
handle(can.set_filter(MCP2515::FilterNumber::FILTER_0, 2));
pwOut << "Expected message received:\n";
handle(can.send_message(2, 6, messages[0]));
read(can, MCP2515::BufferNumber::BUFFER_0);
pwOut << "Message should _not_ be received!\n";
handle(can.send_message(3, 6, messages[1]));
read(can, MCP2515::BufferNumber::BUFFER_0);
pwOut << "Expected message received:\n";
handle(can.send_message(2, 6, messages[2]));
read(can, MCP2515::BufferNumber::BUFFER_0);
}
PropWare::MCP2515
Control and communicate with the Microchip MCP2515 CAN bus controller.
Definition: mcp2515.h:111
printer.h
PropWare::Printer::printf
void printf(const char fmt[], const T first, const Targs... remaining) const
Similar in functionality to the C-standard function printf.
Definition: printer.h:497
PropWare::SPI
SPI serial communications library; Core functionality comes from a dedicated assembly cog.
Definition: spi.h:43
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
mcp2515.h
PropWare::Port
Flexible port that can have any pin enabled or disabled. Pins are independent of each other.
Definition: port.h:38
main
int main(void)
Definition: GraphicsTest.c:20
PropWare.h
pwOut
PropWare::Printer pwOut
Most common use of printing in PropWare applications (not thread safe; see PropWare::pwSyncOut for mu...
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
SPI
Definition: SPI.h:11
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33