tests/periph_uart: added two UART driver tests

- test for UART driver in blocking mode
- test for UART driver in interrupt driven mode
This commit is contained in:
Hauke Petersen 2014-08-08 18:44:50 +02:00
parent 037820d6a6
commit 477bc5a73d
4 changed files with 274 additions and 0 deletions

View File

@ -0,0 +1,10 @@
export APPLICATION = periph_uart_blocking
include ../Makefile.tests_common
BOARD_BLACKLIST := chronos mbed_lpc1768 msb-430 msb-430h native qemu-i386 redbee-econotag telosb \
wsn430-v1_3b wsn430-v1_4 z1
# all listed boards: no periph_conf.h defined,
DISABLE_MODULE += auto_init
include $(RIOTBASE)/Makefile.include

View File

@ -0,0 +1,107 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief Test for low-level UART driver in blocking mode
*
* This test will test the functionality of all configured UART interface. While instructions are
* given on the STDIO UART interface, the test will ask the user to iteratively connect to every
* other UART interface and make some input there.
*
* The test makes only sense, if at least 2 UART devices are configured...
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include "board.h"
#include "periph/uart.h"
/* only build this test, if the target supports the UART driver interface */
#if UART_NUMOF
static int baudrates[] = {115200, 57600, 9600, 38400, 115200, 115200};
void uart_print(uart_t dev, char *str)
{
int i = 0;
while (str[i] != '\0') {
uart_write_blocking(dev, str[i++]);
}
}
int main(void)
{
char *hello = "Testing this UART device:\n";
char *ask = "Please enter a few chars with newline in the end\n";
char buf[128];
int p, i;
char tmp;
puts("Test UART driver in blocking mode\n");
puts("Setting up remaining UART devices:");
for (int i = UART_0; i < UART_NUMOF; i++) {
if (i != STDIO) {
printf("Setting up UART_%i @ %i", i, baudrates[i]);
if (uart_init_blocking(i, baudrates[i]) >= 0) {
puts(" ...ok");
}
else {
puts(" ...failed");
return 1;
}
}
}
printf("\n");
for (i = UART_0; i < UART_NUMOF; i++) {
if (i != STDIO) {
printf("Please Connect to UART_%i @ %i now, press return when done\n", i, baudrates[i]);
do {
tmp = getchar();
} while (tmp != '\n');
uart_print(i, hello);
uart_print(i, ask);
p = 0;
memset(buf, 0, 128);
printf("Input was: ");
do {
uart_read_blocking(i, &buf[p++]);
printf("%c", buf[p - 1]);
} while (buf[p - 1] != '\n');
buf[p] = '\0';
uart_print(i, buf);
printf("\n");
}
}
puts("If you were able to see your inputs on each UART device, the test was successful!");
return 0;
}
#else
int main(void)
{
puts("This board does not support the low-level UART driver interface.");
return 0;
}
#endif /* UART_NUMOF */

View File

@ -0,0 +1,11 @@
export APPLICATION = periph_uart_int
include ../Makefile.tests_common
BOARD_BLACKLIST := chronos mbed_lpc1768 msb-430 msb-430h native qemu-i386 redbee-econotag telosb \
wsn430-v1_3b wsn430-v1_4 z1
# all listed boards: no periph_conf.h defined,
USEMODULE += lib
USEMODULE += vtimer
include $(RIOTBASE)/Makefile.include

View File

@ -0,0 +1,146 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief Test for low-level UART driver in blocking mode
*
* This application tests the interrupt driven mode for the low-level UART driver.
*
* The test will read characters from the UART into a receiving buffer, until a newline is received.
* When this happens, the received string will be printed to stdout.
*
* In the same time, a string is written to the UART every 2 seconds, to make sure the transmission
* is working as well.
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include "cpu.h"
#include "msg.h"
#include "kernel.h"
#include "thread.h"
#include "board.h"
#include "vtimer.h"
#include "ringbuffer.h"
#include "periph/uart.h"
#include "periph_conf.h"
/* only build this test if the UART driver is supported */
#if UART_NUMOF
#define DEV UART_0
#define BAUD 115200
static volatile int main_pid;
static char uart_stack[KERNEL_CONF_STACKSIZE_MAIN];
static char rx_mem[128];
static char tx_mem[128];
static ringbuffer_t rx_buf;
static ringbuffer_t tx_buf;
void rx(void *ptr, char data)
{
msg_t msg;
ringbuffer_add_one(&rx_buf, data);
if (data == '\n') {
msg_send(&msg, main_pid, 1);
}
}
int tx(void *ptr)
{
char data;
if (tx_buf.avail > 0) {
data = ringbuffer_get_one(&tx_buf);
uart_write(DEV, data);
return 1;
}
return 0;
}
void *uart_thread(void *arg)
{
(void)arg;
char *status = "I am written to the UART every 2 seconds\n";
while (1) {
ringbuffer_add(&tx_buf, status, strlen(status));
uart_tx_begin(DEV);
vtimer_usleep(2000 * 1000);
}
return 0;
}
int main(void)
{
char buf[128];
int res;
msg_t msg;
main_pid = thread_getpid();
puts("\nTesting interrupt driven mode of UART driver\n");
puts("Setting up buffers...");
ringbuffer_init(&rx_buf, rx_mem, 128);
ringbuffer_init(&tx_buf, tx_mem, 128);
printf("Initializing UART @ %i", BAUD);
if (uart_init(DEV, BAUD, rx, tx, 0) >= 0) {
puts(" ...done");
}
else {
puts(" ...failed");
return 1;
}
puts("Starting timer thread that triggers UART output...");
thread_create(uart_stack, KERNEL_CONF_STACKSIZE_MAIN, PRIORITY_MAIN - 1,
0, uart_thread, 0, "uart");
while (1) {
msg_receive(&msg);
printf("RECEIVED INPUT: ");
res = ringbuffer_get(&rx_buf, buf, rx_buf.avail);
buf[res] = '\0';
printf("%s", buf);
}
return 0;
}
#else
int main(void)
{
puts("This platform does not support the low-level UART driver");
return 0;
}
#endif /* UART_NUMOF */