From b70cf17e53dcf772c7a5da9e70c5bed93618e54e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 2 Mar 2016 01:33:25 +0100 Subject: [PATCH] some cleanup and formatting --- README.md | 10 +++++++--- avr-c-bp.pro | 12 ++++++------ lib/usart.c | 21 ++++++++------------- lib/usart.h | 3 +++ main.c | 2 +- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index c682fab..f4e7e73 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ You can compile it with `make` and flash with `make flash`. ### Before you can flash -First, check that the `avrdude` options in the file are correct for your system - especially +First, check that the `avrdude` options in the file are correct for your system - especially the device and speed. ```ini @@ -52,7 +52,7 @@ PROG_TYPE = arduino PROG_ARGS = -c $(PROG_TYPE) -p $(MCU) -b $(PROG_BAUD) -P $(PROG_DEV) ``` -- Adjust `PROG_DEV` to the device your board is connected to. On Linux it's usually +- Adjust `PROG_DEV` to the device your board is connected to. On Linux it's usually `/dev/ttyUSB0`, but it can also be `/dev/ttyACM0` or something else. On Mac, it'll be `/dev/cu.xxx`. On Windows it's some `COMx`.
Linux and Mac users can use `ls /dev` to see their devices. Windows users will find @@ -71,4 +71,8 @@ created by the compiler before linking) to the `OBJS` list in the Makefile. ## Notes -- 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 +- The **Arduino UNO** bootloader has a quirk where `Double Speed Asynchronous Mode` for USART + is enabled by default, so if you set your baud rate to 9600, you'd really get 19200.
+ We correct this in the `usart_init()` function to keep things consistent and to avoid + confusion.
+ *If you wish to turn this on* however, you can do so by using `usart_set_2x(true)`. diff --git a/avr-c-bp.pro b/avr-c-bp.pro index 8bfc897..485ac11 100644 --- a/avr-c-bp.pro +++ b/avr-c-bp.pro @@ -12,21 +12,21 @@ DEFINES += __AVR_ATmega328P__ F_CPU=16000000UL DISTFILES += \ style.astylerc \ Makefile \ - README.md \ - LICENSE + README.md \ + LICENSE HEADERS += \ lib/calc.h \ lib/iopins.h \ lib/usart.h \ - lib/nsdelay.h \ - lib/spi.h + lib/nsdelay.h \ + lib/spi.h SOURCES += \ lib/iopins.c \ main.c \ - lib/usart.c \ - lib/spi.c + lib/usart.c \ + lib/spi.c # === Flags for the Clang code model=== # diff --git a/lib/usart.c b/lib/usart.c index 0ca3783..8927dff 100644 --- a/lib/usart.c +++ b/lib/usart.c @@ -26,16 +26,14 @@ void usart_init(uint16_t ubrr) UCSR0C = (0b11 << UCSZ00); } -void usart_set_2x(bool set) + +/** Set Double Speed Asynchronous mode */ +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); + set_bit(UCSR0A, U2X0, set); } + /** Send byte over USART */ void usart_tx(uint8_t data) { @@ -59,8 +57,7 @@ uint8_t usart_rx(void) /** Send string over USART */ void usart_puts(const char* str) { - while (*str) - { + while (*str) { usart_tx(*str++); } } @@ -70,8 +67,7 @@ void usart_puts(const char* str) void usart_puts_P(const char* str) { char c; - while ((c = pgm_read_byte(str++))) - { + while ((c = pgm_read_byte(str++))) { usart_tx(c); } } @@ -81,8 +77,7 @@ void usart_puts_P(const char* str) void usart_flush_rx(void) { uint8_t dummy; - while (bit_is_high(UCSR0A, RXC0)) - { + while (bit_is_high(UCSR0A, RXC0)) { dummy = UDR0; } } diff --git a/lib/usart.h b/lib/usart.h index 6857e98..f03fda5 100644 --- a/lib/usart.h +++ b/lib/usart.h @@ -35,12 +35,15 @@ 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) + /** Check if USART is ready to accept new byte to send */ #define usart_tx_ready() bit_is_high(UCSR0A, UDRE0) diff --git a/main.c b/main.c index a158a6f..16861b1 100644 --- a/main.c +++ b/main.c @@ -26,7 +26,7 @@ ISR(USART_RX_vect) void main() { - usart_init(BAUD_9600); + usart_init(BAUD_115200); usart_isr_rx_enable(true); // enable RX interrupt handler // configure pins