summaryrefslogtreecommitdiff
path: root/digital/avr/modules
diff options
context:
space:
mode:
authorNicolas Schodet2013-01-26 18:32:09 +0100
committerNicolas Schodet2013-01-26 18:32:09 +0100
commit3953c49f0bb0003f9b1b21bb228f25e4fa45206e (patch)
treed3ac0f6e9540f5dcfaeabfd4921983626dd4c02e /digital/avr/modules
parent62812c78e55ffa2c05e041188339f92f7125675b (diff)
digital/avr/modules: set uart speed at runtime
Diffstat (limited to 'digital/avr/modules')
-rw-r--r--digital/avr/modules/uart/uart.avr.c11
-rw-r--r--digital/avr/modules/uart/uart.h8
-rw-r--r--digital/avr/modules/uart/uart.txt3
-rw-r--r--digital/avr/modules/uart/uart_common.h1
4 files changed, 21 insertions, 2 deletions
diff --git a/digital/avr/modules/uart/uart.avr.c b/digital/avr/modules/uart/uart.avr.c
index ff735213..c28f2f22 100644
--- a/digital/avr/modules/uart/uart.avr.c
+++ b/digital/avr/modules/uart/uart.avr.c
@@ -245,9 +245,7 @@ void
uart_init (void)
{
/* Set baud rate. */
-#if (UBRR_VAL >> 8) != 0
UBRRH = UBRR_VAL >> 8;
-#endif
UBRRL = UBRR_VAL & 0xff;
UCSRA = 0;
/* Set format and enable uart. */
@@ -263,6 +261,15 @@ uart_init (void)
#endif
}
+/** Change uart speed after init. */
+void
+uart_set_speed (uint32_t speed)
+{
+ uint16_t ubrr_val = AC_FREQ / 16 / speed - 1;
+ UBRRH = ubrr_val >> 8;
+ UBRRL = ubrr_val & 0xff;
+}
+
/** Read a char. */
uint8_t
uart_getc (void)
diff --git a/digital/avr/modules/uart/uart.h b/digital/avr/modules/uart/uart.h
index f4d5c525..7cc93a7c 100644
--- a/digital/avr/modules/uart/uart.h
+++ b/digital/avr/modules/uart/uart.h
@@ -29,6 +29,10 @@
void
uart0_init (void);
+/** Change uart speed after init. */
+void
+uart0_set_speed (uint32_t speed);
+
/** Read a char. */
uint8_t
uart0_getc (void);
@@ -45,6 +49,10 @@ uart0_poll (void);
void
uart1_init (void);
+/** Change uart speed after init. */
+void
+uart1_set_speed (uint32_t speed);
+
/** Read a char. */
uint8_t
uart1_getc (void);
diff --git a/digital/avr/modules/uart/uart.txt b/digital/avr/modules/uart/uart.txt
index 30116732..966ae29c 100644
--- a/digital/avr/modules/uart/uart.txt
+++ b/digital/avr/modules/uart/uart.txt
@@ -40,6 +40,9 @@ Only one port can be configured to be used in the STDIO mode. In this mode,
there is an automatic conversion between carriage returns and line feeds (this
is also not good if you plan to use it for binary transfers.
+On AVR, there is also a ``uartN_set_speed`` to change speed after the UART was
+initialized.
+
API
===
diff --git a/digital/avr/modules/uart/uart_common.h b/digital/avr/modules/uart/uart_common.h
index 4704f59c..ffd49f19 100644
--- a/digital/avr/modules/uart/uart_common.h
+++ b/digital/avr/modules/uart/uart_common.h
@@ -40,6 +40,7 @@
#define uart_error_code uart (error_code)
#define uart_getc uart (getc)
#define uart_init uart (init)
+#define uart_set_speed uart (set_speed)
#define uart_poll uart (poll)
#define uart_putc uart (putc)
#define uart_recv_buffer uart (recv_buffer)