Refactoring uart interface (#341)

* uart: Major cleanups
- Adds documentation to the uart API.
- Fixes a buffer overflow issue in `uart_receive`, where the maximum parameter was ignored.
- Splits the maximum length and bytes recieved variables in `uart_receive`.
- Downsizes the receive buffer to the minimum required, saving 16MiB of RAM at runtime.
- Refactors the POSIX and Win32 implementations of uart into separate files.
- Removes the unused `uart_{get,set}_parity` functions, which were not implemented on Win32.
This commit is contained in:
Michael Farrell 2017-07-06 04:22:02 +10:00 committed by pwpiwi
parent 52244230d3
commit 067bfc8b76
7 changed files with 315 additions and 270 deletions

View file

@ -57,26 +57,22 @@ struct receiver_arg {
int run;
};
byte_t rx[0x1000000];
byte_t rx[sizeof(UsbCommand)];
byte_t* prx = rx;
static void *uart_receiver(void *targ) {
struct receiver_arg *arg = (struct receiver_arg*)targ;
size_t rxlen;
size_t cmd_count;
while (arg->run) {
rxlen = sizeof(UsbCommand);
if (uart_receive(sp, prx, &rxlen)) {
rxlen = 0;
if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen)) {
prx += rxlen;
if (((prx-rx) % sizeof(UsbCommand)) != 0) {
if (prx-rx < sizeof(UsbCommand)) {
continue;
}
cmd_count = (prx-rx) / sizeof(UsbCommand);
for (size_t i = 0; i < cmd_count; i++) {
UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
}
UsbCommandReceived((UsbCommand*)rx);
}
prx = rx;