RISC-V/V203/usb/usart/test/main.cpp

68 lines
1.9 KiB
C++
Raw Normal View History

2024-10-15 19:26:19 +02:00
#include "usart.h"
#include <cstdio>
#include <cstring>
2024-10-17 13:36:01 +02:00
#include <cstdlib>
2024-10-15 19:26:19 +02:00
#include <signal.h>
2024-10-17 13:36:01 +02:00
#include <unistd.h>
#include <chrono>
using namespace std::chrono;
2024-10-15 19:26:19 +02:00
class Top : public BaseLayer {
static constexpr int max = 1024;
2024-10-17 13:36:01 +02:00
unsigned tx_cnt, rx_cnt;
2024-10-15 19:26:19 +02:00
volatile unsigned index;
int passcnt;
char buffer [max];
public:
2024-10-17 13:36:01 +02:00
explicit Top () : BaseLayer(), tx_cnt(0u), rx_cnt(0u), index(0u), passcnt(0) {}
2024-10-15 19:26:19 +02:00
void puts (const char * str) {
const unsigned l = strlen(str);
2024-10-17 13:36:01 +02:00
tx_cnt += Down(str, l);
2024-10-15 19:26:19 +02:00
}
uint32_t Up(const char * data, const uint32_t len) override {
2024-10-17 13:36:01 +02:00
rx_cnt += len;
2024-10-15 19:26:19 +02:00
for (unsigned n=0; n<len; n++) {
const char c = data [n];
buffer [index++] = c;
if (c == '\n') {
buffer [index - 2] = '\0';
2024-10-17 13:36:01 +02:00
printf("Rx (%d) %s \r", passcnt++, buffer);
fflush(stdout);
2024-10-15 19:26:19 +02:00
index = 0u;
}
}
return len;
}
2024-10-17 13:36:01 +02:00
void result (const int t) {
const double real_br = 1.0e6 * (double) tx_cnt / (double) t;
printf("\nTx %d, Rx %d, tx = %g bytes/s\n", tx_cnt, rx_cnt, real_br);
}
2024-10-15 19:26:19 +02:00
};
static const char * TestString = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'S BACK 1234567890\r\n";
static volatile bool loop = false;
static void Handler (int) {
loop = false;
}
2024-10-17 13:36:01 +02:00
int main (int argc, char * argv[]) {
// 4 MBd je maximum.
int baud = 4'000'000;
if (argc > 1) {
baud = atoi (argv[1]);
}
2024-10-15 19:26:19 +02:00
loop = true;
signal (SIGINT, Handler);
2024-10-17 13:36:01 +02:00
//UsartClass usart ("/dev/ttyACM0", baud); // obecně
UsartClass usart ("/dev/serial/by-id/usb-Kizarm_Labs._USB__=__USART_0002-if00", baud);
2024-10-15 19:26:19 +02:00
Top top;
top += usart;
2024-10-17 13:36:01 +02:00
auto start = high_resolution_clock::now();
2024-10-15 19:26:19 +02:00
while (loop) {
top.puts(TestString);
}
2024-10-17 13:36:01 +02:00
auto stop = high_resolution_clock::now();
usleep(100'000);
int duration = duration_cast<microseconds>(stop - start).count();
top.result(duration);
2024-10-15 19:26:19 +02:00
return 0;
}