Skip to content

Instantly share code, notes, and snippets.

@pdp7
Created November 2, 2019 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdp7/27651af3ede36c97714c05a66f83cbcf to your computer and use it in GitHub Desktop.
Save pdp7/27651af3ede36c97714c05a66f83cbcf to your computer and use it in GitHub Desktop.
Macchina M2 output from Due CAN
#include <SPI.h>
#include <MCP2515_sw_can.h>
#include "variant.h"
#include <due_can.h>
// Pin definitions specific to how the MCP2515 is wired up.
#ifdef MACCHINA_M2
#define CS_PIN SPI0_CS3
#define INT_PIN SWC_INT
#else
#define CS_PIN 34
#define INT_PIN 38
#endif
#define TEST1_CAN_COMM_MB_IDX 0
#define TEST1_CAN_TRANSFER_ID 0x05
#define TEST1_CAN0_TX_PRIO 15
#define CAN_MSG_DUMMY_DATA 0x00010203
// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN 8
// Message variable to be send
uint32_t CAN_MSG_1 = 0;
// Create CAN object with pins as defined
SWcan SCAN(CS_PIN, INT_PIN);
void CANHandler() {
SCAN.intHandler();
}
void setup() {
CAN_FRAME output;
SerialUSB.println("setup RX_Example...");
delay(5000);
SerialUSB.begin(115200);
SerialUSB.println("Initializing ...");
// Set up SPI Communication
// dataMode can be SPI_MODE0 or SPI_MODE3 only for MCP2515
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.begin();
// Initialize MCP2515 CAN controller at the specified speed and clock frequency
// (Note: This is the oscillator attached to the MCP2515, not the Arduino oscillator)
//speed in KHz, clock in MHz
SCAN.setupSW(33);
attachInterrupt(INT_PIN, CANHandler, FALLING);
SCAN.InitFilters(true);
SCAN.mode(3);
SerialUSB.println("Ready ...");
}
byte i = 0;
// CAN message frame (actually just the parts that are exposed by the MCP2515 RX/TX buffers)
CAN_FRAME message;
void loop() {
// Initialize CAN0 and CAN1, baudrate is 250kb/s
Can0.begin(CAN_BPS_250K);
CAN_FRAME output;
// Prepare transmit ID, data and data length in CAN0 mailbox 0
output.id = TEST1_CAN_TRANSFER_ID;
output.length = MAX_CAN_FRAME_DATA_LEN;
//Set first four bytes (32 bits) all at once
output.data.low = CAN_MSG_DUMMY_DATA;
//Set last four bytes (32 bits) all at once
output.data.high = CAN_MSG_DUMMY_DATA;
//Send out the frame on whichever mailbox is free or queue it for
//sending when there is an opening.
CAN.sendFrame(output);
SerialUSB.println("\nsend");
// Disable CAN0 Controller
Can0.disable();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment