From 6d494cb18309df787d119bce09abae3f18663891 Mon Sep 17 00:00:00 2001 From: Vincent Stakenburg Date: Wed, 2 Mar 2016 00:02:31 +0100 Subject: [PATCH] adds a bugfix for the Arduino Uno bootloader where Double Speed Asynchronous Mode is enabled by default. Also adds function to enable said mode again, if desired. --- README.md | 3 +-- lib/usart.c | 12 ++++++++++++ lib/usart.h | 3 +++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4c16286..c682fab 100644 --- a/README.md +++ b/README.md @@ -71,5 +71,4 @@ created by the compiler before linking) to the `OBJS` list in the Makefile. ## Notes -### USART -Note that if you are using an **Arduino Uno**, USART work a little different; If you set the baudrate to `57600` for example, the baudrate you'll have to listen for in your terminal will become double the rate you've set in the code, so that's `115200`. This is because the Uno runs on `Asynchronous Double Speed Mode` by default. \ No newline at end of file +- Since the Arduino Uno has a quirk where Double Speed Asynchronous Mode for USART is enabled by default, we made sure to disable this. If this mode is enabled it means that the USART baud rate is doubled, so if you were using a baud rate of `9200` in the code, you'd have to use a baud rate of 19200 in your terminal. We disabled this to keep things consistent, and because it has no real benefit. If you wish to turn this on however, you can do so by using `usart_set_2x(true);` \ No newline at end of file diff --git a/lib/usart.c b/lib/usart.c index e9e020a..0ca3783 100644 --- a/lib/usart.c +++ b/lib/usart.c @@ -19,10 +19,22 @@ void usart_init(uint16_t ubrr) // Enable Rx and Tx UCSR0B = (1 << RXEN0) | (1 << TXEN0); + // Clear U2X0 + cbi(UCSR0A, U2X0); + // 8-bit data, 1 stop bit UCSR0C = (0b11 << UCSZ00); } +void usart_set_2x(bool set) +{ + // Turn on Double Speed Asynchronous Mode + if (set) + sbi(UCSR0A, U2X0); + // Turn off Double Speed Asynchronous Mode + else + cbi(UCSR0A, U2X0); +} /** Send byte over USART */ void usart_tx(uint8_t data) diff --git a/lib/usart.h b/lib/usart.h index 5c325f7..6857e98 100644 --- a/lib/usart.h +++ b/lib/usart.h @@ -35,6 +35,9 @@ enum { /** Init UART with a UBRR value - can use the BAUD_* constants for 16 MHz */ void usart_init(uint16_t ubrr); +/** Set Double Speed Asynchronous mode on or off */ +void usart_set_2x(bool set); + /** Check if there's a byte in the RX register */ #define usart_rx_ready() bit_is_high(UCSR0A, RXC0)