rmtuart cleanup

This commit is contained in:
informatic 2024-07-20 11:53:22 +02:00
parent 6239375dfa
commit 55566fe871
No known key found for this signature in database
6 changed files with 30 additions and 65 deletions

View file

@ -6,6 +6,7 @@
#include "baudot.h"
#include "esp_log.h"
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
static const char *TAG = "baudot";
/*
@ -152,11 +153,13 @@ size_t baudot_to_unicode(baudot_context_t *ctx, uint8_t *input, uint8_t input_le
if (c == BAUDOT_FIGS)
{
ctx->figures = 1;
ctx->tx_figures = 1;
ESP_LOGD(TAG, "figures switch: %d", ctx->figures);
}
else if (c == BAUDOT_LTRS)
{
ctx->figures = 0;
ctx->tx_figures = 0;
ESP_LOGD(TAG, "figures switch: %d", ctx->figures);
}
else
@ -281,16 +284,16 @@ size_t unicode_to_baudot(baudot_context_t *ctx, uint8_t *input, uint8_t input_le
for (int i = 0; i < sizeof(baudot_map); i++)
{
// start lookup from current part of baudot map
int x = i ^ (ctx->figures << 5);
int x = i ^ (ctx->tx_figures << 5);
if (baudot_map[x] == c)
{
// switch to figures + workaround for broken carriage return on our machine :(
if (ctx->figures != x >> 5 || (c == '\r' && !ctx->figures))
if (ctx->tx_figures != x >> 5 || (c == '\r' && !ctx->tx_figures))
{
output[out_pos++] = ctx->figures ? BAUDOT_LTRS : BAUDOT_FIGS;
ctx->figures = !ctx->figures;
ESP_LOGD(TAG, "figures switch: %d", ctx->figures);
output[out_pos++] = ctx->tx_figures ? BAUDOT_LTRS : BAUDOT_FIGS;
ctx->tx_figures = !ctx->tx_figures;
ESP_LOGD(TAG, "figures switch: %d", ctx->tx_figures);
}
ESP_LOGD(TAG, "emitting: %d", x & 0b11111);
output[out_pos++] = x & 0b11111;

View file

@ -6,6 +6,7 @@
typedef struct
{
uint8_t figures;
uint8_t tx_figures;
uint8_t unicode_len;
uint32_t unicode_codepoint;
} baudot_context_t;

View file

@ -126,6 +126,9 @@ void app_main(void)
{
ESP_LOGI(TAG, "starting up");
// rmtuart_echo_test();
// return;
rmtuart_start(&rmtuart);
network.rx_buffer = rmtuart.rx_buffer;

View file

@ -59,7 +59,11 @@ static void tcp_server_task(network_ctx_t *ctx)
ESP_LOGI(TAG, "Socket listening");
baudot_context_t unicode_ctx = {0};
baudot_context_t unicode_ctx = {};
unicode_ctx.figures = 0;
unicode_ctx.tx_figures = 0;
unicode_ctx.unicode_codepoint = 0;
unicode_ctx.unicode_len = 0;
while (1)
{
@ -95,6 +99,10 @@ static void tcp_server_task(network_ctx_t *ctx)
uint8_t rx_data[32] = {0};
size_t rx_data_len = xStreamBufferReceive(ctx->rx_buffer, &rx_data, sizeof(rx_data), pdMS_TO_TICKS(100));
for (int i = 0; i < rx_data_len; i++)
{
ESP_LOGI(TAG, "TTY->TCP: %02x", rx_data[i]);
}
uint8_t rx_unicode_data[64] = {1};
size_t rx_unicode_data_len = baudot_to_unicode(&unicode_ctx, rx_data, rx_data_len, rx_unicode_data, sizeof(rx_unicode_data));
@ -138,11 +146,16 @@ static void tcp_server_task(network_ctx_t *ctx)
}
else if (read_buffer_len > 0)
{
ESP_LOGI(TAG, "Sending %d bytes", read_buffer_len);
ESP_LOGI(TAG, "Sending %d bytes to TTY", read_buffer_len);
uint8_t tx_baudot_data[64] = {0};
size_t tx_baudot_data_len = unicode_to_baudot(&unicode_ctx, read_buffer, read_buffer_len, tx_baudot_data, sizeof(tx_baudot_data));
if (tx_baudot_data_len > 0)
{
for (int i = 0; i < tx_baudot_data_len; i++)
{
ESP_LOGI(TAG, "TCP->TTY: %02x", tx_baudot_data[i]);
}
xStreamBufferSend(ctx->tx_buffer, &tx_baudot_data, tx_baudot_data_len, pdMS_TO_TICKS(1000));
}
}
@ -150,7 +163,7 @@ static void tcp_server_task(network_ctx_t *ctx)
if (client_sockets[i] && rx_unicode_data_len > 0)
{
ESP_LOGI(TAG, "Sending %d bytes to %d: %s", rx_unicode_data_len, client_sockets[i], rx_unicode_data);
ESP_LOGI(TAG, "Sending %d bytes to TCP/%d: %s", rx_unicode_data_len, client_sockets[i], rx_unicode_data);
send(client_sockets[i], &rx_unicode_data, rx_unicode_data_len, 0);
}
}

View file

@ -173,20 +173,8 @@ void rmtuart_transmit_word(rmtuart_ctx_t *ctx, uint32_t word)
ESP_ERROR_CHECK(rmt_tx_wait_all_done(ctx->tx_chan, portMAX_DELAY));
}
typedef struct
{
int64_t ts;
uint8_t level;
} gpio_status_t;
static void IRAM_ATTR gpio_isr_handler(rmtuart_ctx_t *ctx)
{
/*gpio_status_t gpio_evt = {
esp_timer_get_time(),
gpio_get_level(ctx->rx_gpio),
};*/
// xQueueSendFromISR(ctx->gpio_evt_queue, &gpio_evt, NULL);
if (gpio_get_level(ctx->rx_gpio) == 1 && ctx->bit_cnt == 0)
{
ctx->bit_cnt = 1;
@ -210,41 +198,9 @@ static void gpio_bit_callback(rmtuart_ctx_t *ctx)
}
else
{
// uint8_t buf[32] = {ctx->buf};
// int res = xStreamBufferSendFromISR(ctx->rx_buffer, &ctx->buf, 1, 0);
gpio_status_t gpio_evt = {
0,
ctx->buf,
};
xQueueSendFromISR(ctx->gpio_evt_queue, &gpio_evt, NULL);
// int byte = ctx->buf;
// ctx->buf = 0;
uint8_t buf[32] = {ctx->buf};
xStreamBufferSendFromISR(ctx->rx_buffer, &buf, 1, 0);
ctx->bit_cnt = 0;
// ... push byte
}
}
void gpiouart_receive_words(rmtuart_ctx_t *ctx)
{
int64_t last_ts = 0;
while (true)
{
gpio_status_t evt;
if (xQueueReceive(ctx->gpio_evt_queue, &evt, pdMS_TO_TICKS(1000)))
{
ESP_LOGI(TAG, "%lld@%d", evt.ts, evt.level);
uint8_t payload[32] = {evt.level};
int res = xStreamBufferSend(ctx->rx_buffer, &payload, 1, pdMS_TO_TICKS(100));
last_ts = evt.ts;
}
else
{
ESP_LOGI(TAG, "...");
}
}
}
@ -281,8 +237,6 @@ void rmtuart_start(rmtuart_ctx_t *ctx)
}
else
{
ctx->gpio_evt_queue = xQueueCreate(10, sizeof(gpio_status_t));
ctx->bit_cnt = 0;
ctx->buf = 0;
@ -294,16 +248,12 @@ void rmtuart_start(rmtuart_ctx_t *ctx)
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
gpio_config(&io_conf);
// install gpio isr service
gpio_install_isr_service(0);
// hook isr handler for specific gpio pin
gpio_isr_handler_add(ctx->rx_gpio, (gpio_isr_t)gpio_isr_handler, (void *)ctx);
const esp_timer_create_args_t rx_bit_timer_config = {
.callback = &gpio_bit_callback,
.arg = ctx,
/* argument specified here will be passed to timer callback function */
.name = "gpio_rx_gpio",
};
@ -335,10 +285,6 @@ void rmtuart_start(rmtuart_ctx_t *ctx)
{
xTaskCreate((TaskFunction_t)rmtuart_receive_words, "rmtuart_receive_words", 4096, ctx, 40, NULL);
}
else
{
xTaskCreate((TaskFunction_t)gpiouart_receive_words, "gpiouart_receive_words", 4096, ctx, 40, NULL);
}
}
if (ctx->tx_gpio >= 0)

View file

@ -26,7 +26,6 @@ typedef struct
StreamBufferHandle_t rx_buffer;
StreamBufferHandle_t tx_buffer;
QueueHandle_t gpio_evt_queue;
rmt_clock_source_t clk_src;
int resolution_hz;