Started work on using TWAI instead of external CAN bus

This commit is contained in:
Dreaded_X 2022-06-20 19:41:53 +02:00
parent 2a727b66b0
commit 4bb95d2208
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
4 changed files with 65 additions and 7 deletions

View File

@ -1,3 +1,3 @@
idf_component_register(SRCS "src/main.cpp" "src/helper.cpp" "src/wav.cpp" "src/storage.cpp" "src/i2s.cpp" "src/bluetooth.cpp" "src/avrcp.cpp" "src/a2dp.cpp" "src/can.cpp"
idf_component_register(SRCS "src/main.cpp" "src/helper.cpp" "src/wav.cpp" "src/storage.cpp" "src/i2s.cpp" "src/bluetooth.cpp" "src/avrcp.cpp" "src/a2dp.cpp" "src/can.cpp" "src/twai.cpp"
INCLUDE_DIRS "include"
EMBED_FILES "assets/connect.wav" "assets/disconnect.wav")

View File

@ -0,0 +1,6 @@
#pragma once
namespace twai {
void init();
}

View File

@ -12,6 +12,7 @@
#include "avrcp.h"
#include "a2dp.h"
#include "can.h"
#include "twai.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@ -22,11 +23,12 @@ void task(void*) {
for (;;) {
vTaskDelay(5000 / portTICK_PERIOD_MS);
static uint8_t volume = 0;
volume = (volume + 1) % 31;
ESP_LOGI(APP_TAG, "Emulation radio with volume: %i", volume);
avrcp::set_volume(ceil(volume * 4.2f));
avrcp::seek_forward();
avrcp::seek_forward();
avrcp::seek_forward();
avrcp::seek_forward();
avrcp::seek_forward();
avrcp::seek_forward();
}
}
@ -45,6 +47,7 @@ extern "C" void app_main() {
/* xTaskCreate(task, "Task", 2048, nullptr, 0, nullptr); */
can::init();
/* can::init(); */
twai::init();
}

View File

@ -0,0 +1,49 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/twai.h"
#include "esp_log.h"
#include "twai.h"
#define TWAI_TAG "APP_TWAI"
static void listen(void*) {
for (;;) {
twai_message_t message;
if (twai_receive(&message, portMAX_DELAY) == ESP_OK) {
ESP_LOGI(TWAI_TAG, "Message received");
} else {
ESP_LOGI(TWAI_TAG, "Failed to receive message");
continue;
}
if (message.extd) {
ESP_LOGI(TWAI_TAG, "Message is in Extended Format");
} else {
ESP_LOGI(TWAI_TAG, "Message is in Standard Format");
}
ESP_LOGI(TWAI_TAG, "ID=%d, Length=%d", message.identifier, message.data_length_code);
}
}
void twai::init() {
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_5, GPIO_NUM_19, TWAI_MODE_LISTEN_ONLY);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS();
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
ESP_LOGI(TWAI_TAG, "Driver installed");
} else {
ESP_LOGI(TWAI_TAG, "Failed to install the driver");
return;
}
if (twai_start() == ESP_OK) {
ESP_LOGI(TWAI_TAG, "Driver started");
} else {
ESP_LOGI(TWAI_TAG, "Failed to start driver");
}
xTaskCreate(listen, "TWAI Listener", 2048, nullptr, 0, nullptr);
}