Skip to content

Instantly share code, notes, and snippets.

@macchina
Last active September 3, 2021 21:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save macchina/ac0db2bc45774836d6fe2a5758b12e06 to your computer and use it in GitHub Desktop.
Save macchina/ac0db2bc45774836d6fe2a5758b12e06 to your computer and use it in GitHub Desktop.
// This is a VERY quick sketch as a proof of concept to see if we can start a car via SWCAN. This code won't work as-is, the data shown is dummy data.
#include <mcp_can.h>
#include <SPI.h>
#include "SamNonDuePin.h"
const int Red = 32;
const int Yellow1 = X0;
const int Yellow2 = 27;
const int Green = 23;
const int SPI_CS_PIN = 78;
SWcan SW_CAN(SPI_CS_PIN); // Set CS pin
int incomingByte; // a variable to read incoming serial data into
void setup() {
// initialize serial:
Serial.begin(9600);
SerialUSB.begin(115200);
pinMode(Red, OUTPUT);
pinModeNonDue(Yellow1, OUTPUT);
pinMode(Yellow2, OUTPUT);
pinMode(Green, OUTPUT);
digitalWrite(Red, HIGH);
digitalWriteNonDue(Yellow1, HIGH);
digitalWrite(Yellow2, HIGH);
digitalWrite(Green, HIGH);
SW_CAN.setupSW(0x00);
delay(100);
SW_CAN.mode(3); // 0 - Sleep, 1 - High Speed, 2 - High Voltage Wake-Up, 3 - Normal
delay(1000);
}
unsigned char wake[8] = {0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99};
unsigned char initalize[8] = {0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99};
unsigned char startup[3] = {0x99, 0x99, 0x99};
unsigned char stopdown[2] = {0x99, 0x99};
unsigned char lockmsg[2] = {0x99, 0x99};
unsigned char unlockmsg[2] = {0x99, 0x99};
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
SerialUSB.println(incomingByte);
if (incomingByte == '1') {
digitalWrite(Green, LOW);
start();
}
if (incomingByte == '2') {
digitalWrite(Red, LOW);
stop();
}
if (incomingByte == '3') {
digitalWriteNonDue(Yellow1, LOW);
lock();
}
if (incomingByte == '4') {
digitalWrite(Yellow2, LOW);
unlock();
}
if (incomingByte == '0') {
digitalWrite(Red, HIGH);
digitalWriteNonDue(Yellow1, HIGH);
digitalWrite(Yellow2, HIGH);
digitalWrite(Green, HIGH);
}
}
}
void start()
{
SW_CAN.mode(2); // High voltage wakup
delay(500);
SW_CAN.sendMsgBuf(0x999, 0, 8, wake); // send 11-bit non-extended messege
delay(500);
SW_CAN.sendMsgBuf(0x999, 0, 8, initalize);
delay(500);
SW_CAN.mode(3); // go to Normal mode
delay(500);
SW_CAN.sendMsgBuf(0x999999, 1, 3, startup); // send 29-bit extended messege
delay(500);
}
void stop()
{
SW_CAN.mode(2); // High voltage wakup
delay(500);
SW_CAN.sendMsgBuf(0x999, 0, 8, wake); // send 11-bit non-extended messege
delay(500);
SW_CAN.sendMsgBuf(0x999, 0, 8, initalize);
delay(500);
SW_CAN.mode(3); // go to Normal mode
delay(500);
SW_CAN.sendMsgBuf(0x999999, 1, 2, stopdown); // send 29-bit extended messege
delay(500);
}
void lock()
{
SW_CAN.mode(2); // High voltage wakup
delay(500);
SW_CAN.sendMsgBuf(0x999, 0, 8, wake); // send 11-bit non-extended messege
delay(500);
SW_CAN.sendMsgBuf(0x999, 0, 8, initalize);
delay(500);
SW_CAN.mode(3); // go to Normal mode
delay(500);
SW_CAN.sendMsgBuf(0x999999, 1, 2, lockmsg); // send 29-bit extended messege
delay(500);
}
void unlock()
{
SW_CAN.mode(2); // High voltage wakup
delay(500);
SW_CAN.sendMsgBuf(0x999, 0, 8, wake); // send 11-bit non-extended messege
delay(500);
SW_CAN.sendMsgBuf(0x999, 0, 8, initalize);
delay(500);
SW_CAN.mode(3); // go to Normal mode
delay(500);
SW_CAN.sendMsgBuf(0x999999, 1, 2, unlockmsg); // send 29-bit extended messege
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment