RISC-V/V203/usb/usart/test/main.cpp
2024-10-17 13:36:01 +02:00

67 lines
1.9 KiB
C++

#include "usart.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <signal.h>
#include <unistd.h>
#include <chrono>
using namespace std::chrono;
class Top : public BaseLayer {
static constexpr int max = 1024;
unsigned tx_cnt, rx_cnt;
volatile unsigned index;
int passcnt;
char buffer [max];
public:
explicit Top () : BaseLayer(), tx_cnt(0u), rx_cnt(0u), index(0u), passcnt(0) {}
void puts (const char * str) {
const unsigned l = strlen(str);
tx_cnt += Down(str, l);
}
uint32_t Up(const char * data, const uint32_t len) override {
rx_cnt += len;
for (unsigned n=0; n<len; n++) {
const char c = data [n];
buffer [index++] = c;
if (c == '\n') {
buffer [index - 2] = '\0';
printf("Rx (%d) %s \r", passcnt++, buffer);
fflush(stdout);
index = 0u;
}
}
return len;
}
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);
}
};
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;
}
int main (int argc, char * argv[]) {
// 4 MBd je maximum.
int baud = 4'000'000;
if (argc > 1) {
baud = atoi (argv[1]);
}
loop = true;
signal (SIGINT, Handler);
//UsartClass usart ("/dev/ttyACM0", baud); // obecně
UsartClass usart ("/dev/serial/by-id/usb-Kizarm_Labs._USB__=__USART_0002-if00", baud);
Top top;
top += usart;
auto start = high_resolution_clock::now();
while (loop) {
top.puts(TestString);
}
auto stop = high_resolution_clock::now();
usleep(100'000);
int duration = duration_cast<microseconds>(stop - start).count();
top.result(duration);
return 0;
}