From 2fb53ef2384f2217b370c673478598f42335f6e8 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sat, 2 Mar 2013 23:26:20 +0100 Subject: digital/ucoolib/ucoolib/hal/uart: add host support --- digital/ucoolib/ucoolib/hal/uart/test/Makefile | 7 +- digital/ucoolib/ucoolib/hal/uart/test/test_uart.cc | 114 +++++------------- .../ucoolib/hal/uart/test/test_uart_disc.cc | 128 +++++++++++++++++++++ digital/ucoolib/ucoolib/hal/uart/uart.hh | 5 +- 4 files changed, 165 insertions(+), 89 deletions(-) create mode 100644 digital/ucoolib/ucoolib/hal/uart/test/test_uart_disc.cc (limited to 'digital') diff --git a/digital/ucoolib/ucoolib/hal/uart/test/Makefile b/digital/ucoolib/ucoolib/hal/uart/test/Makefile index 5d84850c..6989dd2d 100644 --- a/digital/ucoolib/ucoolib/hal/uart/test/Makefile +++ b/digital/ucoolib/ucoolib/hal/uart/test/Makefile @@ -1,9 +1,12 @@ BASE = ../../../.. -TARGETS = stm32f4 +TARGETS = host stm32f4 PROGS = test_uart +stm32f4_PROGS = test_uart_disc test_uart_SOURCES = test_uart.cc +test_uart_disc_SOURCES = test_uart_disc.cc -MODULES = hal/uart base/test hal/usb +MODULES = hal/uart +test_uart_disc_MODULES = $(MODULES) base/test hal/usb include $(BASE)/build/top.mk diff --git a/digital/ucoolib/ucoolib/hal/uart/test/test_uart.cc b/digital/ucoolib/ucoolib/hal/uart/test/test_uart.cc index 217baf91..4e933044 100644 --- a/digital/ucoolib/ucoolib/hal/uart/test/test_uart.cc +++ b/digital/ucoolib/ucoolib/hal/uart/test/test_uart.cc @@ -1,6 +1,6 @@ // ucoolib - Microcontroller object oriented library. {{{ // -// Copyright (C) 2012 Nicolas Schodet +// Copyright (C) 2013 Nicolas Schodet // // APBTeam: // Web: http://apbteam.org/ @@ -24,105 +24,47 @@ #include "ucoolib/hal/uart/uart.hh" #include "ucoolib/arch/arch.hh" -#include "ucoolib/hal/gpio/gpio.hh" -#include "ucoolib/base/test/test.hh" -#include +#if defined (TARGET_stm32) +# include +# include "ucoolib/hal/gpio/gpio.hh" +#endif -static void -check_act (ucoo::Stream &ts, ucoo::Stream &u, char n) -{ - char buf[3 + 16 + 1]; - if (!u.poll ()) - return; - int r = u.read (buf + 3, 16); - if (r <= 0) - { - buf[3] = '#'; - r = 1; - } - buf[0] = '<'; - buf[1] = n; - buf[2] = ':'; - buf[3 + r] = '>'; - ts.write (buf, 3 + r + 1); -} +#include "ucoolib/common.hh" int main (int argc, const char **argv) { ucoo::arch_init (argc, argv); - ucoo::Stream &ts = ucoo::test_stream (); - ucoo::Uart u1 (0); - ucoo::Uart u3 (2); - ucoo::Uart u4 (3); +#if defined (TARGET_host) + ucoo::Uart u0, u1 ("uart1"); +#elif defined (TARGET_stm32) + // D8, D9: UART3 + // C12, D2: UART5 + rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN); + rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + gpio_mode_setup (GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO12); + gpio_mode_setup (GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2 | GPIO8 | GPIO9); + gpio_set_af (GPIOC, GPIO_AF8, GPIO12); + gpio_set_af (GPIOD, GPIO_AF8, GPIO2); + gpio_set_af (GPIOD, GPIO_AF7, GPIO8 | GPIO9); + ucoo::Uart u0 (2), u1 (4); + u0.enable (38400, ucoo::Uart::EVEN, 1); u1.enable (38400, ucoo::Uart::EVEN, 1); - u3.enable (38400, ucoo::Uart::EVEN, 1); - u4.enable (38400, ucoo::Uart::EVEN, 1); - // For this test, shorten B6 & B7 to have a loopback on UART1, shorten C10 - // & C11 to connect UART3 to UART4. - rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPBEN - | RCC_AHB1ENR_IOPCEN); - gpio_mode_setup (GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, - GPIO6 | GPIO7); - gpio_set_af (GPIOB, GPIO_AF7, GPIO6 | GPIO7); - gpio_mode_setup (GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, - GPIO10 | GPIO11); - gpio_set_af (GPIOC, GPIO_AF7, GPIO10); - gpio_set_af (GPIOC, GPIO_AF8, GPIO11); - // Loop to report any activity on ports and provide a simple UI. +#endif + ucoo::Uart *u[] = { &u0, &u1 }; char buf[64]; - int buf_i = 0; - ucoo::Uart *u = &u1; while (1) { - check_act (ts, u1, '1'); - check_act (ts, u3, '3'); - check_act (ts, u4, '4'); - while (ts.poll ()) + for (int i = 0; i < (int) lengthof (u); i++) { - char c = ts.getc (); - switch (c) + if (u[i]->poll ()) { - case '?': - static const char help[] = - "? - help\n" - "1, 3, 4 - set output uart\n" - ": - reset output buffer index\n" - "! - send output buffer\n" - "O, E, N - change parity to Odd, Even or None\n" - "any - fill output buffer\n"; - ts.write (help, sizeof (help)); - break; - case '1': - u = &u1; - break; - case '3': - u = &u3; - break; - case '4': - u = &u4; - break; - case ':': - buf_i = 0; - break; - case '!': - u->write (buf, buf_i); - break; - case 'O': - u->enable (38400, ucoo::Uart::ODD, 1); - break; - case 'E': - u->enable (38400, ucoo::Uart::EVEN, 1); - break; - case 'N': - u->enable (38400, ucoo::Uart::NONE, 1); - break; - default: - if (buf_i < static_cast (sizeof (buf))) - buf[buf_i++] = c; - break; + int len = u[i]->read (buf, sizeof (buf)); + u[i]->write (buf, len); } } + ucoo::yield (); } } + diff --git a/digital/ucoolib/ucoolib/hal/uart/test/test_uart_disc.cc b/digital/ucoolib/ucoolib/hal/uart/test/test_uart_disc.cc new file mode 100644 index 00000000..217baf91 --- /dev/null +++ b/digital/ucoolib/ucoolib/hal/uart/test/test_uart_disc.cc @@ -0,0 +1,128 @@ +// ucoolib - Microcontroller object oriented library. {{{ +// +// Copyright (C) 2012 Nicolas Schodet +// +// APBTeam: +// Web: http://apbteam.org/ +// Email: team AT apbteam DOT org +// +// 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. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "ucoolib/hal/uart/uart.hh" + +#include "ucoolib/arch/arch.hh" +#include "ucoolib/hal/gpio/gpio.hh" +#include "ucoolib/base/test/test.hh" + +#include + +static void +check_act (ucoo::Stream &ts, ucoo::Stream &u, char n) +{ + char buf[3 + 16 + 1]; + if (!u.poll ()) + return; + int r = u.read (buf + 3, 16); + if (r <= 0) + { + buf[3] = '#'; + r = 1; + } + buf[0] = '<'; + buf[1] = n; + buf[2] = ':'; + buf[3 + r] = '>'; + ts.write (buf, 3 + r + 1); +} + +int +main (int argc, const char **argv) +{ + ucoo::arch_init (argc, argv); + ucoo::Stream &ts = ucoo::test_stream (); + ucoo::Uart u1 (0); + ucoo::Uart u3 (2); + ucoo::Uart u4 (3); + u1.enable (38400, ucoo::Uart::EVEN, 1); + u3.enable (38400, ucoo::Uart::EVEN, 1); + u4.enable (38400, ucoo::Uart::EVEN, 1); + // For this test, shorten B6 & B7 to have a loopback on UART1, shorten C10 + // & C11 to connect UART3 to UART4. + rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPBEN + | RCC_AHB1ENR_IOPCEN); + gpio_mode_setup (GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, + GPIO6 | GPIO7); + gpio_set_af (GPIOB, GPIO_AF7, GPIO6 | GPIO7); + gpio_mode_setup (GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, + GPIO10 | GPIO11); + gpio_set_af (GPIOC, GPIO_AF7, GPIO10); + gpio_set_af (GPIOC, GPIO_AF8, GPIO11); + // Loop to report any activity on ports and provide a simple UI. + char buf[64]; + int buf_i = 0; + ucoo::Uart *u = &u1; + while (1) + { + check_act (ts, u1, '1'); + check_act (ts, u3, '3'); + check_act (ts, u4, '4'); + while (ts.poll ()) + { + char c = ts.getc (); + switch (c) + { + case '?': + static const char help[] = + "? - help\n" + "1, 3, 4 - set output uart\n" + ": - reset output buffer index\n" + "! - send output buffer\n" + "O, E, N - change parity to Odd, Even or None\n" + "any - fill output buffer\n"; + ts.write (help, sizeof (help)); + break; + case '1': + u = &u1; + break; + case '3': + u = &u3; + break; + case '4': + u = &u4; + break; + case ':': + buf_i = 0; + break; + case '!': + u->write (buf, buf_i); + break; + case 'O': + u->enable (38400, ucoo::Uart::ODD, 1); + break; + case 'E': + u->enable (38400, ucoo::Uart::EVEN, 1); + break; + case 'N': + u->enable (38400, ucoo::Uart::NONE, 1); + break; + default: + if (buf_i < static_cast (sizeof (buf))) + buf[buf_i++] = c; + break; + } + } + } +} diff --git a/digital/ucoolib/ucoolib/hal/uart/uart.hh b/digital/ucoolib/ucoolib/hal/uart/uart.hh index b5ed4df6..758176b5 100644 --- a/digital/ucoolib/ucoolib/hal/uart/uart.hh +++ b/digital/ucoolib/ucoolib/hal/uart/uart.hh @@ -24,7 +24,10 @@ // // }}} -#ifdef TARGET_stm32 +#if defined (TARGET_host) +# include "ucoolib/arch/host/host_stream.hh" +namespace ucoo { typedef HostStream Uart; } +#elif defined (TARGET_stm32) # include "uart.stm32.hh" #else # error "not implemented for this target" -- cgit v1.2.3