Last active
September 18, 2019 13:04
-
-
Save yoshimax/c0d5888134ed03856ba1025bf31542bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <CAN.h> | |
// https://github.com/sandeepmistry/arduino-CAN | |
// For DO IT Dev On board LED | |
// MCP2551 VCC5V-DOITDEVBoaredVin CTX-5 CRX-5 | |
#include "freertos/task.h" | |
#define ONBLED 2 // For DO IT Dev On board LED | |
#define ON 1 | |
#define OFF 0 | |
/* Multi Task */ | |
#define TSK1STACK 4096 // Task1 Stack size | |
#define TSK2STACK 4096 // Task2 Stack size | |
void task0(void* param) { | |
const portTickType yDelay = 200UL; | |
while(1) { // Task Must Need Inf Lool | |
// Serial.printf("Task0 on core %d done\n",xPortGetCoreID()); | |
digitalWrite( ONBLED, ( digitalRead(ONBLED) ) ? LOW : HIGH ); | |
vTaskDelay(yDelay); // Delay SystemTic | |
} | |
} | |
void task1(void* param) { | |
const portTickType yDelay = 1UL; | |
Serial.println("CAN Receiver Callback ###"); | |
while(1) { // Task Must Need Inf Lool | |
// Serial.printf("Task0 on core %d done\n",xPortGetCoreID()); | |
if (Serial.available() > 0) { | |
String s; | |
s = Serial.readStringUntil('\n'); | |
Serial.println(s); | |
Serial.println("START SEND ###"); | |
// Some Serial Input Send to Can world | |
CAN.beginExtendedPacket(0xabcdef); | |
CAN.write('w'); | |
CAN.write('o'); | |
CAN.write('r'); | |
CAN.write('l'); | |
CAN.write('d'); | |
CAN.endPacket(); | |
Serial.println("END SEND ###"); | |
} | |
vTaskDelay(yDelay); // | |
} | |
} | |
void init_tasks() { | |
xTaskCreatePinnedToCore(task0, "Task0", TSK1STACK, NULL, 1, NULL, 0); | |
xTaskCreatePinnedToCore(task1, "Task1", TSK2STACK, NULL, 1, NULL, 1); | |
} | |
void init_console() { | |
// Set Up Serial ------ | |
Serial.begin(115200); | |
delay(100); | |
Serial.print("Start\n"); | |
// Set Up CAN ------ | |
Serial.println("CAN INIT ###"); | |
if (!CAN.begin(500E3)) { | |
Serial.println("Starting CAN failed!"); | |
while (1); | |
} | |
CAN.onReceive(onReceive); | |
} | |
void init_hard() { | |
pinMode(ONBLED,OUTPUT); | |
} | |
void setup() { | |
init_hard(); // Hard ware initial | |
init_console(); // Console initial for Debug | |
init_tasks(); // Dual Core assigned TASK | |
} | |
void loop() { | |
// Serial.printf("Loop on core %d done\n",xPortGetCoreID()); | |
delay(1); | |
} | |
// Receive CAN DATA -------- | |
void onReceive(int packetSize) { | |
// received a packet | |
Serial.print("Received "); | |
if (CAN.packetExtended()) { | |
Serial.print("extended "); | |
} | |
if (CAN.packetRtr()) { | |
// Remote transmission request, packet contains no data | |
Serial.print("RTR "); | |
} | |
Serial.print("packet with id 0x"); | |
Serial.print(CAN.packetId(), HEX); | |
if (CAN.packetRtr()) { | |
Serial.print(" and requested length "); | |
Serial.println(CAN.packetDlc()); | |
} else { | |
Serial.print(" and length "); | |
Serial.println(packetSize); | |
// only print packet data for non-RTR packets | |
while (CAN.available()) { | |
Serial.print((char)CAN.read()); | |
} | |
Serial.println(); | |
} | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment