From 55fe4bbdc907ae48697e1536c7d67ffc3217efe0 Mon Sep 17 00:00:00 2001 From: kkira Date: Thu, 18 Jun 2026 21:16:46 +0300 Subject: [PATCH] CAN ESP32 --- ESP32_ESP-IDF/main/can/can_receiver.cpp | 43 +++++++++++++++++++++++++ ESP32_ESP-IDF/main/can/can_receiver.hpp | 8 +++++ 2 files changed, 51 insertions(+) create mode 100644 ESP32_ESP-IDF/main/can/can_receiver.cpp create mode 100644 ESP32_ESP-IDF/main/can/can_receiver.hpp diff --git a/ESP32_ESP-IDF/main/can/can_receiver.cpp b/ESP32_ESP-IDF/main/can/can_receiver.cpp new file mode 100644 index 0000000..e424cdd --- /dev/null +++ b/ESP32_ESP-IDF/main/can/can_receiver.cpp @@ -0,0 +1,43 @@ +#include "can_receiver.hpp" + +#include "driver/twai.h" + +void CANReceiver::Init() +{ + twai_general_config_t g_config = + TWAI_GENERAL_CONFIG_DEFAULT( + GPIO_NUM_21, + GPIO_NUM_22, + TWAI_MODE_NORMAL + ); + + twai_timing_config_t t_config = + TWAI_TIMING_CONFIG_500KBITS(); + + twai_filter_config_t f_config = + TWAI_FILTER_CONFIG_ACCEPT_ALL(); + + twai_driver_install( + &g_config, + &t_config, + &f_config + ); + + twai_start(); +} + +bool CANReceiver::Read(uint16_t& ppm) +{ + twai_message_t rx; + + if(twai_receive(&rx, pdMS_TO_TICKS(1000)) == ESP_OK) + { + ppm = + (rx.data[0] << 8) | + rx.data[1]; + + return true; + } + + return false; +} \ No newline at end of file diff --git a/ESP32_ESP-IDF/main/can/can_receiver.hpp b/ESP32_ESP-IDF/main/can/can_receiver.hpp new file mode 100644 index 0000000..bbc3a6f --- /dev/null +++ b/ESP32_ESP-IDF/main/can/can_receiver.hpp @@ -0,0 +1,8 @@ +#pragma once + +class CANReceiver +{ +public: + static void Init(); + static bool Read(uint16_t& ppm); +}; \ No newline at end of file