-
-
Save hkaji/4454c558bf5f59f168337480f4efd005 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
#include <Arduino.h> | |
#include <ESP32CAN.h> | |
#include <CAN_config.h> | |
CAN_device_t CAN_cfg; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("esp32 CAN demo...kaji tech"); | |
CAN_cfg.speed=CAN_SPEED_1000KBPS; | |
CAN_cfg.tx_pin_id = GPIO_NUM_5; | |
CAN_cfg.rx_pin_id = GPIO_NUM_4; | |
CAN_cfg.rx_queue = xQueueCreate(10,sizeof(CAN_frame_t)); | |
//start CAN Module | |
ESP32Can.CANInit(); | |
} | |
void loop() { | |
CAN_frame_t rx_frame; | |
//receive next CAN frame from queue | |
if(xQueueReceive(CAN_cfg.rx_queue,&rx_frame, 3*portTICK_PERIOD_MS)==pdTRUE){ | |
//do stuff! | |
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\n",rx_frame.MsgID, rx_frame.FIR.B.DLC); | |
for(int i = 0; i < 8; i++){ | |
printf("%c\t", (char)rx_frame.data.u8[i]); | |
} | |
printf("\n"); | |
} | |
} | |
else | |
{ | |
rx_frame.FIR.B.FF = CAN_frame_std; | |
rx_frame.MsgID = 1; | |
rx_frame.FIR.B.DLC = 8; | |
rx_frame.data.u8[0] = 'h'; | |
rx_frame.data.u8[1] = 'e'; | |
rx_frame.data.u8[2] = 'l'; | |
rx_frame.data.u8[3] = 'l'; | |
rx_frame.data.u8[4] = 'o'; | |
rx_frame.data.u8[5] = 'c'; | |
rx_frame.data.u8[6] = 'a'; | |
rx_frame.data.u8[7] = 'n'; | |
ESP32Can.CANWriteFrame(&rx_frame); | |
delay(1000); | |
printf("send frame\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment