1
0
mirror of git://projects.qi-hardware.com/ben-blinkenlights.git synced 2024-12-25 21:04:34 +02:00

swuart/: split general UART functions from application

... in preparation for making it a library.
This commit is contained in:
Werner Almesberger 2012-12-17 18:43:51 -03:00
parent b4530ffc4d
commit 2e749f901c
4 changed files with 117 additions and 46 deletions

View File

@ -4,7 +4,7 @@ LDFLAGS = -static
LDLIBS = -L../libubb -lubb
MAIN = swuart
OBJS = swuart.o
OBJS = test.o swuart.o
.PHONY: all clean spotless

View File

@ -12,17 +12,13 @@
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ubb/ubb.h>
#include <ubb/regs4740.h>
#include "swuart.h"
#define RX UBB_DAT0
#define TX UBB_DAT1
#define TIMER 7
@ -121,11 +117,7 @@ static void enable_interrupts(void)
/* ----- Transmit a block -------------------------------------------------- */
struct rx_err {
unsigned glitch; /* unstable start bit */
unsigned framing; /* stop bit wasn't "1" */
unsigned overflow; /* buffer overflow */
} rx_err;
static struct swuart_err errors;
static int trx(uint8_t *out, int out_size, uint8_t *in, int in_size,
@ -182,11 +174,11 @@ full:
rx = (rx >> 1) | (PIN(rx_mask) ? 0x200 : 0);
if (!--rx_bits) {
if (rx & 1) {
rx_err.glitch++;
errors.glitch++;
} else if (!(rx & 0x200)) {
rx_err.framing++;
errors.framing++;
} else if (in_len == in_size) {
rx_err.overflow++;
errors.overflow++;
} else {
*in++ = rx >> 1;
in_len++;
@ -217,6 +209,25 @@ int swuart_trx(void *out, int out_size, void *in, int in_size,
}
/* ----- Access error counters --------------------------------------------- */
unsigned swuart_get_errors(struct swuart_err *res)
{
unsigned sum = errors.glitch+errors.framing+errors.overflow;
if (res)
*res = errors;
return sum;
}
void swuart_clear_errors(void)
{
memset(&errors, 0, sizeof(errors));
}
/* ----- Open/close the software UART -------------------------------------- */
@ -242,35 +253,3 @@ void swuart_close(void)
{
ubb_close(0);
}
/* ----- Main -------------------------------------------------------------- */
static void at_exit(void)
{
swuart_close();
}
int main(int argc, char **argv)
{
uint8_t buf[40];
int got, i;
swuart_open(TX, RX, atoi(argv[1]));
atexit(at_exit);
while (1) {
got = swuart_trx(argv[2], strlen(argv[2]), buf, sizeof(buf),
10000, 100);
printf("%d (%d %d %d): ", got,
rx_err.glitch, rx_err.framing, rx_err.overflow);
for (i = 0; i != got; i++)
if (buf[i] >= ' ' && buf[i] <= '~')
printf("%c", buf[i]);
else
printf("\\%02o", buf[i]);
printf("\n");
usleep(100);
}
}

35
swuart/swuart.h Normal file
View File

@ -0,0 +1,35 @@
/*
* include/ubb/swuart.h - Software-implemented UART for UBB
*
* Written 2012 by Werner Almesberger
* Copyright 2012 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef UBB_SWUART_H
#define UBB_SWUART_H
#include <stdint.h>
struct swuart_err {
unsigned glitch; /* unstable start bit */
unsigned framing; /* stop bit wasn't "1" */
unsigned overflow; /* buffer overflow */
};
int swuart_trx(void *out, int out_size, void *in, int in_size,
int wait_bits, int idle_bits);
unsigned swuart_get_errors(struct swuart_err *res);
void swuart_clear_errors(void);
int swuart_open(uint32_t tx, uint32_t rx, int bps);
void swuart_close(void);
#endif /* !UBB_SWUART_H */

57
swuart/test.c Normal file
View File

@ -0,0 +1,57 @@
/*
* swuart/test.c - Software-implemented UART for UBB
*
* Written 2012 by Werner Almesberger
* Copyright 2012 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ubb/ubb.h>
#include "swuart.h"
#define RX UBB_DAT0
#define TX UBB_DAT1
static void at_exit(void)
{
swuart_close();
}
int main(int argc, char **argv)
{
uint8_t buf[40];
struct swuart_err err;
int got, i;
swuart_open(TX, RX, atoi(argv[1]));
atexit(at_exit);
while (1) {
got = swuart_trx(argv[2], strlen(argv[2]), buf, sizeof(buf),
10000, 100);
swuart_get_errors(&err);
printf("%d (%d %d %d): ", got,
err.glitch, err.framing, err.overflow);
for (i = 0; i != got; i++)
if (buf[i] >= ' ' && buf[i] <= '~')
printf("%c", buf[i]);
else
printf("\\%02o", buf[i]);
printf("\n");
usleep(100);
}
}