summaryrefslogtreecommitdiff
path: root/ucoolib/hal/uart
diff options
context:
space:
mode:
Diffstat (limited to 'ucoolib/hal/uart')
-rw-r--r--ucoolib/hal/uart/Config6
-rw-r--r--ucoolib/hal/uart/Module1
-rw-r--r--ucoolib/hal/uart/test/Config4
-rw-r--r--ucoolib/hal/uart/test/Makefile12
-rw-r--r--ucoolib/hal/uart/test/test_uart.cc70
-rw-r--r--ucoolib/hal/uart/test/test_uart_disc.cc128
-rw-r--r--ucoolib/hal/uart/uart.hh36
-rw-r--r--ucoolib/hal/uart/uart.stm32.cc221
-rw-r--r--ucoolib/hal/uart/uart.stm32.hh78
9 files changed, 0 insertions, 556 deletions
diff --git a/ucoolib/hal/uart/Config b/ucoolib/hal/uart/Config
deleted file mode 100644
index 0a67d63..0000000
--- a/ucoolib/hal/uart/Config
+++ /dev/null
@@ -1,6 +0,0 @@
-[hal/uart]
-# Size of reception buffer.
-rx_buffer = 32
-# Size of transmission buffer.
-tx_buffer = 32
-
diff --git a/ucoolib/hal/uart/Module b/ucoolib/hal/uart/Module
deleted file mode 100644
index a9d185e..0000000
--- a/ucoolib/hal/uart/Module
+++ /dev/null
@@ -1 +0,0 @@
-hal_uart_SOURCES = uart.stm32.cc
diff --git a/ucoolib/hal/uart/test/Config b/ucoolib/hal/uart/test/Config
deleted file mode 100644
index 4fd7571..0000000
--- a/ucoolib/hal/uart/test/Config
+++ /dev/null
@@ -1,4 +0,0 @@
-[hal/uart]
-rx_buffer = 32
-tx_buffer = 8
-
diff --git a/ucoolib/hal/uart/test/Makefile b/ucoolib/hal/uart/test/Makefile
deleted file mode 100644
index 6989dd2..0000000
--- a/ucoolib/hal/uart/test/Makefile
+++ /dev/null
@@ -1,12 +0,0 @@
-BASE = ../../../..
-
-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
-test_uart_disc_MODULES = $(MODULES) base/test hal/usb
-
-include $(BASE)/build/top.mk
diff --git a/ucoolib/hal/uart/test/test_uart.cc b/ucoolib/hal/uart/test/test_uart.cc
deleted file mode 100644
index 7752342..0000000
--- a/ucoolib/hal/uart/test/test_uart.cc
+++ /dev/null
@@ -1,70 +0,0 @@
-// ucoolib - Microcontroller object oriented library. {{{
-//
-// Copyright (C) 2013 Nicolas Schodet
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the "Software"),
-// to deal in the Software without restriction, including without limitation
-// the rights to use, copy, modify, merge, publish, distribute, sublicense,
-// and/or sell copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-// DEALINGS IN THE SOFTWARE.
-//
-// }}}
-#include "ucoolib/hal/uart/uart.hh"
-
-#include "ucoolib/arch/arch.hh"
-
-#if defined (TARGET_stm32)
-# include <libopencm3/stm32/f4/rcc.h>
-# include "ucoolib/hal/gpio/gpio.hh"
-#endif
-
-#include "ucoolib/common.hh"
-
-int
-main (int argc, const char **argv)
-{
- ucoo::arch_init (argc, argv);
-#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);
-#endif
- ucoo::Uart *u[] = { &u0, &u1 };
- char buf[64];
- while (1)
- {
- for (int i = 0; i < (int) lengthof (u); i++)
- {
- if (u[i]->poll ())
- {
- int len = u[i]->read (buf, sizeof (buf));
- u[i]->write (buf, len);
- }
- }
- ucoo::yield ();
- }
-}
-
diff --git a/ucoolib/hal/uart/test/test_uart_disc.cc b/ucoolib/hal/uart/test/test_uart_disc.cc
deleted file mode 100644
index 5e42613..0000000
--- a/ucoolib/hal/uart/test/test_uart_disc.cc
+++ /dev/null
@@ -1,128 +0,0 @@
-// ucoolib - Microcontroller object oriented library. {{{
-//
-// Copyright (C) 2012 Nicolas Schodet
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the "Software"),
-// to deal in the Software without restriction, including without limitation
-// the rights to use, copy, modify, merge, publish, distribute, sublicense,
-// and/or sell copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-// DEALINGS IN THE SOFTWARE.
-//
-// }}}
-#include "ucoolib/hal/uart/uart.hh"
-
-#include "ucoolib/arch/arch.hh"
-#include "ucoolib/hal/gpio/gpio.hh"
-#include "ucoolib/base/test/test.hh"
-
-#include <libopencm3/stm32/f4/rcc.h>
-
-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<int> (sizeof (buf)))
- buf[buf_i++] = c;
- break;
- }
- }
- }
-}
diff --git a/ucoolib/hal/uart/uart.hh b/ucoolib/hal/uart/uart.hh
deleted file mode 100644
index e2d99a6..0000000
--- a/ucoolib/hal/uart/uart.hh
+++ /dev/null
@@ -1,36 +0,0 @@
-#ifndef ucoolib_hal_uart_uart_hh
-#define ucoolib_hal_uart_uart_hh
-// ucoolib - Microcontroller object oriented library. {{{
-//
-// Copyright (C) 2012 Nicolas Schodet
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the "Software"),
-// to deal in the Software without restriction, including without limitation
-// the rights to use, copy, modify, merge, publish, distribute, sublicense,
-// and/or sell copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-// DEALINGS IN THE SOFTWARE.
-//
-// }}}
-
-#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"
-#endif
-
-#endif // ucoolib_hal_uart_uart_hh
diff --git a/ucoolib/hal/uart/uart.stm32.cc b/ucoolib/hal/uart/uart.stm32.cc
deleted file mode 100644
index 14dc849..0000000
--- a/ucoolib/hal/uart/uart.stm32.cc
+++ /dev/null
@@ -1,221 +0,0 @@
-// ucoolib - Microcontroller object oriented library. {{{
-//
-// Copyright (C) 2012 Nicolas Schodet
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the "Software"),
-// to deal in the Software without restriction, including without limitation
-// the rights to use, copy, modify, merge, publish, distribute, sublicense,
-// and/or sell copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-// DEALINGS IN THE SOFTWARE.
-//
-// }}}
-#include "uart.stm32.hh"
-
-#include <libopencm3/stm32/f4/usart.h>
-#include <libopencm3/stm32/f4/rcc.h>
-#include <libopencm3/cm3/nvic.h>
-
-#ifndef TARGET_stm32f4
-// Need RCC adaptations and USART6 different handling for F1.
-# error "it's a trap, only implemented for F4 for the moment"
-#endif
-
-namespace ucoo {
-
-static const int uart_nb = 6;
-
-/// Information on UART hardware structure.
-struct uart_hardware_t
-{
- /// UART base address.
- uint32_t base;
- /// APB number.
- int apb;
- /// RCC enable bit.
- uint32_t rcc_en;
- /// Corresponding IRQ.
- int irq;
-};
-
-/// Information on UART hardware array, this is zero indexed, USART1 is at
-/// index 0.
-static const uart_hardware_t uart_hardware[uart_nb] =
-{
- { USART1, 2, RCC_APB2ENR_USART1EN, NVIC_USART1_IRQ },
- { USART2, 1, RCC_APB1ENR_USART2EN, NVIC_USART2_IRQ },
- { USART3, 1, RCC_APB1ENR_USART3EN, NVIC_USART3_IRQ },
- { UART4, 1, RCC_APB1ENR_UART4EN, NVIC_UART4_IRQ },
- { UART5, 1, RCC_APB1ENR_UART5EN, NVIC_UART5_IRQ },
- { USART6, 2, RCC_APB2ENR_USART6EN, NVIC_USART6_IRQ },
-};
-
-static Uart *uart_instances[uart_nb];
-
-} // namespace ucoo
-
-extern "C" {
-
-void usart1_isr () { ucoo::Uart::isr (0); }
-
-void usart2_isr () { ucoo::Uart::isr (1); }
-
-void usart3_isr () { ucoo::Uart::isr (2); }
-
-void uart4_isr () { ucoo::Uart::isr (3); }
-
-void uart5_isr () { ucoo::Uart::isr (4); }
-
-void usart6_isr () { ucoo::Uart::isr (5); }
-
-}
-
-namespace ucoo {
-
-Uart::Uart (int n)
- : n_ (n), error_char_ (default_error_char), enabled_ (false)
-{
- assert (n < uart_nb);
- assert (!uart_instances[n]);
- uart_instances[n] = this;
-}
-
-Uart::~Uart ()
-{
- disable ();
- uart_instances[n_] = 0;
-}
-
-void
-Uart::enable (int speed, Parity parity, int stop_bits)
-{
- enabled_ = true;
- uint32_t base = uart_hardware[n_].base;
- // Turn on.
- rcc_peripheral_enable_clock
- (uart_hardware[n_].apb == 1 ? &RCC_APB1ENR : &RCC_APB2ENR,
- uart_hardware[n_].rcc_en);
- // Set speed, rounded to nearest.
- int apb_freq = uart_hardware[n_].apb == 1 ? rcc_ppre1_frequency
- : rcc_ppre2_frequency;
- USART_BRR (base) = (2 * apb_freq + speed) / (2 * speed);
- // Set parameters and enable.
- if (stop_bits == 1)
- USART_CR2 (base) = USART_CR2_STOPBITS_1;
- else if (stop_bits == 2)
- USART_CR2 (base) = USART_CR2_STOPBITS_2;
- else
- assert_unreachable ();
- USART_CR3 (base) = 0;
- uint32_t cr1 = USART_CR1_UE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
- if (parity != NONE)
- cr1 |= USART_CR1_M | USART_CR1_PCE;
- if (parity == ODD)
- cr1 |= USART_CR1_PS;
- USART_CR1 (base) = cr1;
- // Reset status.
- (void) USART_SR (base);
- (void) USART_DR (base);
- // Enable interrupts.
- nvic_enable_irq (uart_hardware[n_].irq);
-}
-
-void
-Uart::disable ()
-{
- if (enabled_)
- {
- enabled_ = false;
- uint32_t base = uart_hardware[n_].base;
- // Stop UART.
- nvic_disable_irq (uart_hardware[n_].irq);
- USART_CR1 (base) = 0;
- // Turn off.
- rcc_peripheral_disable_clock
- (uart_hardware[n_].apb == 1 ? &RCC_APB1ENR : &RCC_APB2ENR,
- uart_hardware[n_].rcc_en);
- }
-}
-
-void
-Uart::set_error_char (char c)
-{
- error_char_ = c;
-}
-
-int
-Uart::read (char *buf, int count)
-{
- assert (enabled_);
- if (block_)
- while (rx_fifo_.empty ())
- barrier ();
- return rx_fifo_.read (buf, count);
-}
-
-int
-Uart::write (const char *buf, int count)
-{
- assert (enabled_);
- int left = count;
- while (left)
- {
- int r = tx_fifo_.write (buf, left);
- if (r)
- {
- USART_CR1 (uart_hardware[n_].base) |= USART_CR1_TXEIE;
- buf += r;
- left -= r;
- }
- if (!block_)
- break;
- }
- return count - left;
-}
-
-int
-Uart::poll ()
-{
- return rx_fifo_.empty () ? 0 : 1;
-}
-
-void
-Uart::isr (int n)
-{
- uint32_t base = uart_hardware[n].base;
- uint32_t sr = USART_SR (base);
- uint32_t dr = USART_DR (base);
- assert (uart_instances[n]);
- Uart &uart = *uart_instances[n];
- if (sr & (USART_SR_ORE | USART_SR_NE | USART_SR_FE | USART_SR_PE))
- {
- dr = uart.error_char_;
- // Datasheet is not really clear about this when error bits are set.
- sr |= USART_SR_RXNE;
- }
- if (sr & USART_SR_RXNE)
- {
- if (!uart.rx_fifo_.full ())
- uart.rx_fifo_.push (dr);
- }
- if (sr & USART_SR_TXE)
- {
- if (!uart.tx_fifo_.empty ())
- USART_DR (base) = static_cast<uint8_t> (uart.tx_fifo_.pop ());
- if (uart.tx_fifo_.empty ())
- USART_CR1 (base) &= ~USART_CR1_TXEIE;
- }
-}
-
-} // namespace ucoo
diff --git a/ucoolib/hal/uart/uart.stm32.hh b/ucoolib/hal/uart/uart.stm32.hh
deleted file mode 100644
index db14536..0000000
--- a/ucoolib/hal/uart/uart.stm32.hh
+++ /dev/null
@@ -1,78 +0,0 @@
-#ifndef ucoolib_hal_uart_uart_stm32_hh
-#define ucoolib_hal_uart_uart_stm32_hh
-// ucoolib - Microcontroller object oriented library. {{{
-//
-// Copyright (C) 2012 Nicolas Schodet
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the "Software"),
-// to deal in the Software without restriction, including without limitation
-// the rights to use, copy, modify, merge, publish, distribute, sublicense,
-// and/or sell copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-// DEALINGS IN THE SOFTWARE.
-//
-// }}}
-#include "ucoolib/intf/stream.hh"
-#include "ucoolib/utils/fifo.hh"
-
-#include "config/hal/uart.hh"
-
-namespace ucoo {
-
-/// Universal asynchronous receiver transmitter (UART).
-///
-/// When an error is detected on RX, error character is inserted in receive
-/// FIFO.
-class Uart : public Stream
-{
- public:
- /// Parity setting.
- enum Parity { ODD, EVEN, NONE };
- /// Default error character.
- static const char default_error_char = '~';
- public:
- /// Constructor for the Nth UART.
- Uart (int n);
- /// Shutdown UART.
- ~Uart ();
- /// Enable and setup UART.
- void enable (int speed, Parity parity = NONE, int stop_bits = 1);
- /// Disable.
- void disable ();
- /// Change the error character.
- void set_error_char (char c);
- /// See Stream::read.
- int read (char *buf, int count);
- /// See Stream::write.
- int write (const char *buf, int count);
- /// See Stream::poll.
- int poll ();
- /// Handle interrupts.
- static void isr (int n);
- private:
- /// UART number.
- int n_;
- /// RX FIFO, filled by interrupt handler.
- Fifo<char, UCOO_CONFIG_HAL_UART_RX_BUFFER> rx_fifo_;
- /// TX FIFO, emptied by interrupt handler.
- Fifo<char, UCOO_CONFIG_HAL_UART_TX_BUFFER> tx_fifo_;
- /// Error character, inserted in case of error.
- char error_char_;
- /// Is it enabled?
- bool enabled_;
-};
-
-} // namespace ucoo
-
-#endif // ucoolib_hal_uart_uart_stm32_hh