CAN ESP32

This commit is contained in:
kkira
2026-06-18 21:16:46 +03:00
parent 5197495371
commit 55fe4bbdc9
2 changed files with 51 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -0,0 +1,8 @@
#pragma once
class CANReceiver
{
public:
static void Init();
static bool Read(uint16_t& ppm);
};