Initial project setup

This commit is contained in:
2025-12-13 11:59:11 +02:00
commit 3218e6039f
2176 changed files with 355321 additions and 0 deletions

296
main/usb_cdc_cli.c Normal file
View File

@@ -0,0 +1,296 @@
#include "usb_cdc_cli.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dcdc_controller.h"
#include "esp_check.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "sdkconfig.h"
#include "tinyusb.h"
#include "tusb_cdc_acm.h"
#define CLI_LINE_MAX_LEN 128
#define CLI_QUEUE_LEN 8
#ifndef CONFIG_TINYUSB_CDC_RX_BUFSIZE
#define CONFIG_TINYUSB_CDC_RX_BUFSIZE 64
#endif
typedef struct {
size_t length;
uint8_t data[CONFIG_TINYUSB_CDC_RX_BUFSIZE];
} usb_cli_msg_t;
static const char *TAG = "usb_cli";
static QueueHandle_t s_cli_queue;
static bool s_cli_initialized;
static bool s_host_ready;
static void usb_cli_task(void *arg);
static void usb_cli_process_line(char *line);
static void usb_cli_prompt(void);
static void usb_cli_write_raw(const char *data, size_t len)
{
if (!s_host_ready || len == 0) {
return;
}
if (tinyusb_cdcacm_write_queue(TINYUSB_CDC_ACM_0, (uint8_t *)data, len) == ESP_OK) {
esp_err_t err = tinyusb_cdcacm_write_flush(TINYUSB_CDC_ACM_0, 0);
if (err != ESP_OK) {
ESP_LOGW(TAG, "CDC flush error: %s", esp_err_to_name(err));
}
}
}
static void usb_cli_printf(const char *fmt, ...)
{
if (!s_host_ready) {
return;
}
char buffer[192];
va_list args;
va_start(args, fmt);
int len = vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
if (len > 0) {
if (len >= (int)sizeof(buffer)) {
len = (int)sizeof(buffer) - 1;
}
usb_cli_write_raw(buffer, len);
}
}
static const char *PROMPT = "\r\nwatch> ";
static char *usb_cli_trim(char *str)
{
while (*str && isspace((unsigned char)*str)) {
str++;
}
char *end = str + strlen(str);
while (end > str && isspace((unsigned char)*(end - 1))) {
*(--end) = '\0';
}
return str;
}
static bool usb_cli_parse_channel(const char *token, dcdc_channel_t *channel)
{
if (!token) {
return false;
}
char *end = NULL;
long val = strtol(token, &end, 10);
if (end == token || val < 0 || val >= DCDC_CHANNEL_COUNT) {
return false;
}
*channel = (dcdc_channel_t)val;
return true;
}
static void usb_cli_print_status(void)
{
usb_cli_printf("\r\nСтан каналів:\r\n");
for (size_t i = 0; i < dcdc_channel_count(); ++i) {
dcdc_channel_t ch = (dcdc_channel_t)i;
usb_cli_printf(" - CH%u: %s (GPIO %d)\r\n",
(unsigned)i,
dcdc_get_state(ch) ? "ON" : "OFF",
(int)dcdc_get_gpio(ch));
}
}
static void usb_cli_prompt(void)
{
usb_cli_write_raw(PROMPT, strlen(PROMPT));
}
static void usb_cli_handle_switch(const char *cmd, char *args)
{
dcdc_channel_t channel;
char *ctx = NULL;
char *channel_str = strtok_r(args, " ", &ctx);
if (!usb_cli_parse_channel(channel_str, &channel)) {
usb_cli_printf("\r\nНевірний номер каналу\r\n");
return;
}
esp_err_t err = ESP_OK;
if (strcasecmp(cmd, "enable") == 0) {
err = dcdc_enable(channel);
} else if (strcasecmp(cmd, "disable") == 0) {
err = dcdc_disable(channel);
} else {
err = dcdc_toggle(channel);
}
if (err != ESP_OK) {
usb_cli_printf("\r\nПомилка керування каналом: %s\r\n", esp_err_to_name(err));
} else {
usb_cli_printf("\r\nКанал %d -> %s\r\n", channel,
dcdc_get_state(channel) ? "ON" : "OFF");
}
}
static void usb_cli_process_line(char *line)
{
char *trimmed = usb_cli_trim(line);
if (*trimmed == '\0') {
return;
}
char *save_ptr = NULL;
char *cmd = strtok_r(trimmed, " ", &save_ptr);
if (!cmd) {
return;
}
if (strcasecmp(cmd, "help") == 0) {
usb_cli_printf(
"\r\nДоступні команди:\r\n"
" help - показати довідку\r\n"
" status - стан всіх каналів\r\n"
" enable <n> - увімкнути канал n\r\n"
" disable <n> - вимкнути канал n\r\n"
" toggle <n> - перемкнути канал n\r\n");
} else if (strcasecmp(cmd, "status") == 0) {
usb_cli_print_status();
} else if (strcasecmp(cmd, "enable") == 0 ||
strcasecmp(cmd, "disable") == 0 ||
strcasecmp(cmd, "toggle") == 0) {
usb_cli_handle_switch(cmd, save_ptr);
} else {
usb_cli_printf("\r\nНевідома команда '%s'\r\n", cmd);
}
}
static void usb_cli_task(void *arg)
{
usb_cli_msg_t msg;
char line[CLI_LINE_MAX_LEN];
size_t line_len = 0;
while (xQueueReceive(s_cli_queue, &msg, portMAX_DELAY)) {
for (size_t i = 0; i < msg.length; ++i) {
char ch = (char)msg.data[i];
if (ch == '\r' || ch == '\n') {
if (line_len > 0) {
line[line_len] = '\0';
usb_cli_process_line(line);
line_len = 0;
}
usb_cli_prompt();
} else if (ch == 0x7F || ch == '\b') {
if (line_len > 0) {
line_len--;
}
} else if (isprint((unsigned char)ch)) {
if (line_len < CLI_LINE_MAX_LEN - 1) {
line[line_len++] = ch;
} else {
usb_cli_printf("\r\nРядок занадто довгий\r\n");
line_len = 0;
usb_cli_prompt();
}
}
}
}
}
static void usb_cdc_rx_callback(int itf, cdcacm_event_t *event)
{
if (!s_cli_queue) {
return;
}
usb_cli_msg_t msg = { 0 };
size_t rx_size = 0;
esp_err_t ret = tinyusb_cdcacm_read(itf, msg.data, sizeof(msg.data), &rx_size);
if (ret == ESP_OK && rx_size > 0) {
msg.length = rx_size;
if (xQueueSend(s_cli_queue, &msg, 0) != pdTRUE) {
ESP_LOGW(TAG, "CLI queue overflow, втрачено %u байт", (unsigned)rx_size);
}
}
}
static void usb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event)
{
const bool dtr = event->line_state_changed_data.dtr;
const bool rts = event->line_state_changed_data.rts;
ESP_LOGI(TAG, "CDC лінія %d змінилась: DTR=%d RTS=%d", itf, dtr, rts);
s_host_ready = dtr;
if (s_host_ready) {
usb_cli_printf("\r\nwatch-watch CLI готовий. Надрукуйте 'help'.\r\n");
usb_cli_prompt();
}
}
esp_err_t usb_cdc_cli_init(void)
{
if (s_cli_initialized) {
return ESP_OK;
}
s_cli_queue = xQueueCreate(CLI_QUEUE_LEN, sizeof(usb_cli_msg_t));
if (!s_cli_queue) {
return ESP_ERR_NO_MEM;
}
if (xTaskCreate(usb_cli_task, "usb_cli", 4096, NULL, 5, NULL) != pdPASS) {
vQueueDelete(s_cli_queue);
s_cli_queue = NULL;
return ESP_ERR_NO_MEM;
}
const tinyusb_config_t tusb_cfg = {
.device_descriptor = NULL,
.string_descriptor = NULL,
.external_phy = false,
#if (TUD_OPT_HIGH_SPEED)
.fs_configuration_descriptor = NULL,
.hs_configuration_descriptor = NULL,
.qualifier_descriptor = NULL,
#else
.configuration_descriptor = NULL,
#endif
};
ESP_RETURN_ON_ERROR(tinyusb_driver_install(&tusb_cfg), TAG, "TinyUSB init failed");
const tinyusb_config_cdcacm_t acm_cfg = {
.usb_dev = TINYUSB_USBDEV_0,
.cdc_port = TINYUSB_CDC_ACM_0,
.rx_unread_buf_sz = CONFIG_TINYUSB_CDC_RX_BUFSIZE,
.callback_rx = usb_cdc_rx_callback,
.callback_rx_wanted_char = NULL,
.callback_line_state_changed = NULL,
.callback_line_coding_changed = NULL,
};
ESP_RETURN_ON_ERROR(tusb_cdc_acm_init(&acm_cfg), TAG, "CDC init failed");
ESP_RETURN_ON_ERROR(
tinyusb_cdcacm_register_callback(TINYUSB_CDC_ACM_0,
CDC_EVENT_LINE_STATE_CHANGED,
usb_cdc_line_state_changed_callback),
TAG,
"Line state callback error");
s_cli_initialized = true;
ESP_LOGI(TAG, "USB CDC CLI ініціалізовано");
return ESP_OK;
}