Last active
May 10, 2019 19:55
-
-
Save rocketjosh/a092d722aeb2f85aabcf5c18c03aeb5a to your computer and use it in GitHub Desktop.
This file contains 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
//Modified from this library to blink LED: | |
//https://github.com/miwagner/ESP32-Arduino-CAN | |
// | |
//Follow these instructions to install ESP32 board files: | |
//https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/boards_manager.md | |
#include <ESP32CAN.h> | |
#include <CAN_config.h> | |
CAN_device_t CAN_cfg; // CAN Config | |
unsigned long previousMillis = 0; // will store last time a CAN Message was send | |
const int interval = 1000; // interval at which send CAN Messages (milliseconds) | |
const int rx_queue_size = 10; // Receive Queue size | |
int ledState = LOW; // ledState used to set the LED | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("Basic Demo - ESP32-Arduino-CAN"); | |
CAN_cfg.speed = CAN_SPEED_500KBPS; | |
CAN_cfg.tx_pin_id = GPIO_NUM_5; | |
CAN_cfg.rx_pin_id = GPIO_NUM_4; | |
CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t)); | |
// Init CAN Module | |
ESP32Can.CANInit(); | |
pinMode(13, OUTPUT); | |
} | |
void loop() { | |
CAN_frame_t rx_frame; | |
unsigned long currentMillis = millis(); | |
// Receive next CAN frame from queue | |
if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) { | |
if (rx_frame.FIR.B.FF == CAN_frame_std) { | |
printf("New standard frame"); | |
} | |
else { | |
printf("New extended frame"); | |
} | |
if (rx_frame.FIR.B.RTR == CAN_RTR) { | |
printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC); | |
} | |
else { | |
printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC); | |
for (int i = 0; i < rx_frame.FIR.B.DLC; i++) { | |
printf("0x%02X ", rx_frame.data.u8[i]); | |
} | |
printf("\n"); | |
} | |
} | |
// Send CAN Message | |
if (currentMillis - previousMillis >= interval) { | |
if (ledState == LOW) { | |
ledState = HIGH; | |
} else { | |
ledState = LOW; | |
} | |
digitalWrite(13, ledState); | |
previousMillis = currentMillis; | |
CAN_frame_t tx_frame; | |
tx_frame.FIR.B.FF = CAN_frame_std; | |
tx_frame.MsgID = 0x001; | |
tx_frame.FIR.B.DLC = 8; | |
tx_frame.data.u8[0] = 0x00; | |
tx_frame.data.u8[1] = 0x01; | |
tx_frame.data.u8[2] = 0x02; | |
tx_frame.data.u8[3] = 0x03; | |
tx_frame.data.u8[4] = 0x04; | |
tx_frame.data.u8[5] = 0x05; | |
tx_frame.data.u8[6] = 0x06; | |
tx_frame.data.u8[7] = 0x07; | |
ESP32Can.CANWriteFrame(&tx_frame);; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment