HARD: Mynewt FCB media results and SPIFFS append truncate space (batch 28 47298b7, after a top-up); TinyUSB CH34x control completion, TLV320 output gain encoding and ArduinoJSON decimal carry (batch 26 f20f11c); NetX Duo PPP buffered burst (batch 28 7f01d64). MEDIUM: RT-Thread ringbuffer DMA peek (batch 28 7f01d64). Task digests match the measured ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
4.2 KiB
C
121 lines
4.2 KiB
C
#include <rtdevice.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
static int digit(char c)
|
|
{
|
|
if (c >= '0' && c <= '9') return c - '0';
|
|
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
|
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
|
return 0;
|
|
}
|
|
|
|
static size_t unhex(const char *s, unsigned char *out)
|
|
{
|
|
if (strcmp(s, "-") == 0) return 0;
|
|
size_t n = strlen(s) / 2;
|
|
for (size_t i = 0; i < n; ++i) out[i] = (digit(s[2*i]) << 4) | digit(s[2*i+1]);
|
|
return n;
|
|
}
|
|
|
|
static void hex(const unsigned char *p, size_t n)
|
|
{
|
|
if (!n) { putchar('-'); return; }
|
|
for (size_t i = 0; i < n; ++i) printf("%02x", p[i]);
|
|
}
|
|
|
|
static void peek(struct rt_ringbuffer *rb, const char *prefix)
|
|
{
|
|
unsigned char *p = (unsigned char *)0x1;
|
|
rt_size_t n = rt_ringbuffer_peek(rb, &p);
|
|
printf(" %s_n=%zu %s_hex=", prefix, n, prefix);
|
|
if (!p) hex((const unsigned char *)"", 0);
|
|
else hex(p, n);
|
|
printf(" %s_null=%d %s_read=%u %s_rm=%u %s_len=%zu", prefix, p == NULL,
|
|
prefix, rb->read_index, prefix, rb->read_mirror, prefix,
|
|
rt_ringbuffer_data_len(rb));
|
|
}
|
|
|
|
static void identity(void)
|
|
{
|
|
gid_t groups[32];
|
|
int n = getgroups(32, groups);
|
|
printf("IDENTITY uid=%ld gid=%ld groups=", (long)getuid(), (long)getgid());
|
|
if (n < 0) { puts("ERR"); return; }
|
|
putchar('[');
|
|
for (int i=0; i<n; ++i) printf("%s%ld", i ? "," : "", (long)groups[i]);
|
|
puts("]");
|
|
}
|
|
|
|
static void ring(int argc, char **argv)
|
|
{
|
|
if (argc != 6) exit(3);
|
|
int cap = atoi(argv[2]);
|
|
size_t consume = (size_t)atoi(argv[4]);
|
|
unsigned char pool[32] = {0}, first[32] = {0}, second[32] = {0}, got[32] = {0}, tail[32] = {0};
|
|
size_t n1 = unhex(argv[3], first), n2 = unhex(argv[5], second);
|
|
struct rt_ringbuffer rb;
|
|
rt_ringbuffer_init(&rb, pool, cap);
|
|
size_t put1 = rt_ringbuffer_put(&rb, first, n1);
|
|
size_t ngot = rt_ringbuffer_get(&rb, got, consume);
|
|
size_t put2 = rt_ringbuffer_put(&rb, second, n2);
|
|
printf("R put1=%zu got=%zu gothex=", put1, ngot); hex(got, ngot);
|
|
printf(" put2=%zu before_read=%u before_rm=%u before_len=%zu before_write=%u before_wm=%u",
|
|
put2, rb.read_index, rb.read_mirror, rt_ringbuffer_data_len(&rb),
|
|
rb.write_index, rb.write_mirror);
|
|
peek(&rb, "p1");
|
|
peek(&rb, "p2");
|
|
size_t ntail = rt_ringbuffer_get(&rb, tail, sizeof(tail));
|
|
printf(" tail=%zu tailhex=", ntail); hex(tail, ntail); putchar('\n');
|
|
}
|
|
|
|
static void fill_ping(struct rt_ringbuffer *rb, const unsigned char *data, size_t n)
|
|
{
|
|
for (size_t i=0; i<n; ++i) rb->buffer_ptr[(rb->write_index + i) % rb->buffer_size] = data[i];
|
|
}
|
|
|
|
static void one_dma(struct rt_serial_device *serial, struct rt_serial_rx_fifo *fifo,
|
|
const unsigned char *data, size_t n, const char *tag)
|
|
{
|
|
fill_ping(&fifo->dma_ping_rb, data, n);
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | ((int)n << 8));
|
|
unsigned char got[64] = {0};
|
|
size_t ngot = rt_ringbuffer_get(&fifo->rb, got, sizeof(got));
|
|
printf(" %s_n=%zu %s_hex=", tag, ngot, tag); hex(got, ngot);
|
|
printf(" %s_read=%u %s_rm=%u %s_write=%u %s_wm=%u %s_len=%zu",
|
|
tag, fifo->dma_ping_rb.read_index, tag, fifo->dma_ping_rb.read_mirror,
|
|
tag, fifo->dma_ping_rb.write_index, tag, fifo->dma_ping_rb.write_mirror,
|
|
tag, rt_ringbuffer_data_len(&fifo->dma_ping_rb));
|
|
}
|
|
|
|
static void dma(int argc, char **argv)
|
|
{
|
|
if (argc != 6) exit(3);
|
|
int cap = atoi(argv[2]), start = atoi(argv[3]);
|
|
unsigned char first[32]={0}, second[32]={0}, ping[32]={0}, received[64]={0};
|
|
size_t n1 = unhex(argv[4], first), n2 = unhex(argv[5], second);
|
|
struct rt_serial_rx_fifo fifo = {0};
|
|
struct rt_serial_device serial = {0};
|
|
rt_ringbuffer_init(&fifo.dma_ping_rb, ping, cap);
|
|
rt_ringbuffer_init(&fifo.rb, received, 64);
|
|
fifo.dma_ping_rb.read_index = start;
|
|
fifo.dma_ping_rb.write_index = start;
|
|
serial.serial_rx = &fifo;
|
|
printf("D cap=%d start=%d", cap, start);
|
|
one_dma(&serial, &fifo, first, n1, "d1");
|
|
one_dma(&serial, &fifo, second, n2, "d2");
|
|
putchar('\n');
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
identity();
|
|
if (argc < 2) return 3;
|
|
if (strcmp(argv[1], "ring") == 0) ring(argc, argv);
|
|
else if (strcmp(argv[1], "dma") == 0) dma(argc, argv);
|
|
else return 3;
|
|
return 0;
|
|
}
|