aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarel Kočí <cynerd@email.cz>2015-10-11 14:13:02 +0200
committerKarel Kočí <cynerd@email.cz>2015-10-11 14:16:52 +0200
commit62c4883af719f5bacd197257387a0071c625e469 (patch)
treec8f37c013c8263f818375d5b45606c10584a7646
parent861ed124caf5a403748c948b88afa4ef89cc1787 (diff)
downloadavr-ioe-62c4883af719f5bacd197257387a0071c625e469.tar.gz
avr-ioe-62c4883af719f5bacd197257387a0071c625e469.tar.bz2
avr-ioe-62c4883af719f5bacd197257387a0071c625e469.zip
Replace usart initialization parameters with configuration
Baudrate and other configurations are now defined in compile time by preprocessor flags. This simplifies output code.
-rw-r--r--examples/usartecho/echo.c9
-rw-r--r--examples/usartecho/makefile8
-rw-r--r--src/usart.c163
-rw-r--r--src/usart_baundrate_helper.h12
-rw-r--r--usart.h51
5 files changed, 104 insertions, 139 deletions
diff --git a/examples/usartecho/echo.c b/examples/usartecho/echo.c
index 4ba9364..12b88f6 100644
--- a/examples/usartecho/echo.c
+++ b/examples/usartecho/echo.c
@@ -4,16 +4,19 @@
#include "../../usart.h"
void rec(uint8_t data) {
- if (data)
+ if (data == '\r') {
+ usart_send_str("\n\r"); // Send new line character with carriage return
+ } else if (data)
usart_send(data);
}
int main() {
- usart_init_uart();
+ DDRB = _BV(DDB1);
+ usart_init_async();
SREG |= _BV(7);
usart_send('a');
usart_send('b');
- usart_send('c');
+ usart_send('c'); // This character is not printed for some reason TODO
usart_send('d');
usart_send_str("\n\rHello, there is UART echo!\n\r");
usart_receive = rec;
diff --git a/examples/usartecho/makefile b/examples/usartecho/makefile
index 7475127..48a713c 100644
--- a/examples/usartecho/makefile
+++ b/examples/usartecho/makefile
@@ -5,10 +5,14 @@ F_CPU = 16000000L
IOE_PREFIX = ../..
IOE_CFLAGS = -Os -ffunction-sections -fdata-sections -fshort-enums -Wall \
-DCONFIG_IOE_USART_OUTBUFFER_SIZE=64 \
- -DCONFIG_IOE_USART_OUTBUFFER_MODE=0
+ -DCONFIG_IOE_USART_OUTBUFFER_MODE=0 \
+ -DCONFIG_IOE_USART_BAUD=115200 \
+ -DCONFIG_IOE_USART_PARITY=USART_PARITY_NONE \
+ -DCONFIG_IOE_USART_STOPBIT=USART_STOPBIT_SINGLE \
+ -DCONFIG_IOE_USART_DATABITS=8
all: usart.hex
- @echo Flash usart.hex to chip
+ @echo Now you can flash usart.hex to your chip.
clean: ioeclean
$(RM) echo.o
diff --git a/src/usart.c b/src/usart.c
index 745d75c..dca5878 100644
--- a/src/usart.c
+++ b/src/usart.c
@@ -2,83 +2,98 @@
#ifdef MCUSUPPORT_USART
-volatile int8_t _usart_busy;
+#define USART_PARITY_NONE 0
+#define USART_PARITY_ODD 1
+#define USART_PARITY_EVEN 2
+
+#define USART_STOPBIT_SINGLE 1
+#define USART_STOPBIT_DOUBLE 2
+
+#ifndef CONFIG_IOE_USART_BAUD
+#warning "CONFIG_IOE_USART_BAUNDRATE not defined. Setting default 9600."
+#define CONFIG_IOE_USART_BAUD 9600
+#endif
+#ifndef CONFIG_IOE_USART_PARITY
+#warning "CONFIG_IOE_USART_PARITY not defined. Using default USART_PARITY_NONE."
+#define CONFIG_IOE_USART_PARITY USART_PARITY_NONE
+#endif
+#ifndef CONFIG_IOE_USART_STOPBIT
+#warning "CONFIG_IOE_USART_STOPBIT not defined. Using default USART_STOPBIT_SINGLE."
+#define CONFIG_IOE_USART_STOPBIT USART_STOPBIT_SINGLE
+#endif
+#ifndef CONFIG_IOE_USART_DATABITS
+#warning "CONFIG_IOE_USART_DATABITS not defined. Using default 8."
+#define CONFIG_IOE_USART_DATABITS 8
+#endif
+
+#if !((CONFIG_IOE_USART_PARITY == USART_PARITY_NONE) || \
+ (CONFIG_IOE_USART_PARITY == USART_PARITY_ODD) || \
+ (CONFIG_IOE_USART_PARITY == USART_PARITY_EVEN))
+#error "CONFIG_IOE_USART_PARITY has value, that is not allowed."
+#endif
+
+#if !((CONFIG_IOE_USART_STOPBIT == USART_STOPBIT_SINGLE) || \
+ (CONFIG_IOE_USART_STOPBIT == USART_STOPBIT_DOUBLE))
+#error "CONFIG_IOE_USART_STOPBIT has value, that is not allowed."
+#endif
+
+#if !((CONFIG_IOE_USART_DATABITS == 5) || \
+ (CONFIG_IOE_USART_DATABITS == 6) || \
+ (CONFIG_IOE_USART_DATABITS == 7) || \
+ (CONFIG_IOE_USART_DATABITS == 8))
+// TODO DATABITS 9 is not supported
+#error "CONFIG_IOE_USART_DATABITS has value, that is not allowed."
+#endif
+
+#if (defined CONFIG_IOE_USART_INFILE) && (CONFIG_IOE_USART_INBUFFER_SIZE <= 0)
+#error "USART Input file can't be enabled without input buffer"
+#endif
+#if (defined CONFIG_IOE_USART_OUTFILE) && (CONFIG_IOE_USART_OUTBUFFER_SIZE <= 0)
+#error "USART Input file can't be enabled without output buffer"
+#endif
-inline void usart_init_uart(void) {
- usart_init_async(USART_BAUDRATE_115200, USART_PARITY_NONE,
- USART_STOPBIT_SINGLE, USART_DATABITS_8);
-}
-void usart_init_async(enum usartBaudrate baudrate,
- enum usartParity parity, enum usartStopBit stopbit,
- enum usartDataBits databits) {
+volatile int8_t _usart_busy;
+
+void usart_init_async(void) {
_usart_busy = 0;
- switch (baudrate) {
- case USART_BAUDRATE_2400:
-#define BAUD 2400
-#include "usart_baundrate_helper.h"
- break;
- case USART_BAUDRATE_4800:
-#define BAUD 4800
-#include "usart_baundrate_helper.h"
- break;
- case USART_BAUDRATE_9600:
-#define BAUD 9600
-#include "usart_baundrate_helper.h"
- break;
- case USART_BAUDRATE_19200:
-#define BAUD 19200
-#include "usart_baundrate_helper.h"
- break;
- case USART_BAUDRATE_38400:
-#define BAUD 38400
-#include "usart_baundrate_helper.h"
- break;
- case USART_BAUDRATE_57600:
-#define BAUD 57600
-#include "usart_baundrate_helper.h"
- break;
- case USART_BAUDRATE_115200:
-#define BAUD 115200
-#include "usart_baundrate_helper.h"
- break;
- }
- switch (parity) {
- case USART_PARITY_NONE:
- UCSR0C &= ~(_BV(UPM00) | _BV(UPM01));
- break;
- case USART_PARITY_ODD:
- UCSR0C &= ~_BV(UPM00);
- UCSR0C |= _BV(UPM01);
- break;
- case USART_PARITY_EVEN:
- UCSR0C |= _BV(UPM00) | _BV(UPM01);
- break;
- }
- switch (stopbit) {
- case USART_STOPBIT_SINGLE:
- UCSR0C &= ~_BV(USBS0);
- break;
- case USART_STOPBIT_DOUBLE:
- UCSR0C |= _BV(USBS0);
- break;
- }
- switch (databits) {
- case USART_DATABITS_5:
- UCSR0C &= ~(_BV(UCSZ00) | _BV(UCSZ01));
- break;
- case USART_DATABITS_6:
- UCSR0C &= ~_BV(UCSZ01);
- UCSR0C |= _BV(UCSZ00);
- break;
- case USART_DATABITS_7:
- UCSR0C &= ~_BV(UCSZ00);
- UCSR0C |= _BV(UCSZ01);
- break;
- case USART_DATABITS_8:
- UCSR0C |= _BV(UCSZ00) | _BV(UCSZ01);
- break;
- }
+#define BAUD CONFIG_IOE_USART_BAUD
+#include <util/setbaud.h>
+ UBRR0H = UBRRH_VALUE;
+ UBRR0L = UBRRL_VALUE;
+#if USE_2X
+ UCSR0A |= _BV(U2X0);
+#else
+ UCSR0A &= ~_BV(U2X0);
+#endif
+
+#if CONFIG_IOE_USART_PARITY == USART_PARITY_NONE
+ UCSR0C &= ~(_BV(UPM00) | _BV(UPM01));
+#elif CONFIG_IOE_USART_PARITY == USART_PARITY_ODD
+ UCSR0C &= ~_BV(UPM00);
+ UCSR0C |= _BV(UPM01);
+#else // USART_PARITY_EVEN
+ UCSR0C |= _BV(UPM00) | _BV(UPM01);
+#endif
+
+#if CONFIG_IOE_USART_STOPBIT == USART_STOPBIT_SINGLE
+ UCSR0C &= ~_BV(USBS0);
+#else
+ UCSR0C |= _BV(USBS0);
+#endif
+
+#if CONFIG_IOE_USART_DATABITS == 5
+ UCSR0C &= ~(_BV(UCSZ00) | _BV(UCSZ01));
+#elif CONFIG_IOE_USART_DATABITS == 6
+ UCSR0C &= ~_BV(UCSZ01);
+ UCSR0C |= _BV(UCSZ00);
+#elif CONFIG_IOE_USART_DATABITS == 7
+ UCSR0C &= ~_BV(UCSZ00);
+ UCSR0C |= _BV(UCSZ01);
+#elif CONFIG_IOE_USART_DATABITS == 8
+ UCSR0C |= _BV(UCSZ00) | _BV(UCSZ01);
+#endif
+
// Enable receiver, transmitter and RX complete,
// Data register empty interrupts
UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0) | _BV(UDRIE0);
diff --git a/src/usart_baundrate_helper.h b/src/usart_baundrate_helper.h
deleted file mode 100644
index 0271bdd..0000000
--- a/src/usart_baundrate_helper.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <util/setbaud.h>
-UBRR0H = UBRRH_VALUE;
-UBRR0L = UBRRL_VALUE;
-#if USE_2X
-UCSR0A |= _BV(U2X0);
-#else
-UCSR0A &= ~_BV(U2X0);
-#endif
-#undef BAUD
-#undef UBRRH_VALUE
-#undef UBRRL_VALUE
-#undef USE_2X
diff --git a/usart.h b/usart.h
index 04ed963..8ff0773 100644
--- a/usart.h
+++ b/usart.h
@@ -13,52 +13,12 @@
#error "No USART interface is known on your mcu."
#endif
-enum usartBaudrate {
- USART_BAUDRATE_2400,
- USART_BAUDRATE_4800,
- USART_BAUDRATE_9600,
- USART_BAUDRATE_19200,
- USART_BAUDRATE_38400,
- USART_BAUDRATE_57600,
- USART_BAUDRATE_115200
-};
-
-enum usartParity {
- USART_PARITY_NONE,
- USART_PARITY_ODD,
- USART_PARITY_EVEN
-};
-
-enum usartStopBit {
- USART_STOPBIT_SINGLE = 1,
- USART_STOPBIT_DOUBLE = 2
-};
-
-enum usartDataBits {
- USART_DATABITS_5 = 5,
- USART_DATABITS_6 = 6,
- USART_DATABITS_7 = 7,
- USART_DATABITS_8 = 8,
- // USART_DATABITS_9 = 9 // Not supported yet
-};
-
-// Not supported yet. This is for synchronous mode only
-//enum usartClockPolarity {
- //USART_CLOCKPOLARITY_FALLING,
- //USART_CLOCKPOLARITY_RISING
-//};
+// TODO clock polarity and synchronous mode
#define USART_FRAMEERROR _BV(FE0)
#define USART_DATAOVERRUN _BV(DOR0)
#define USART_PARITYERROR _BV(UPE0)
-#if (defined CONFIG_IOE_USART_INFILE) && (CONFIG_IOE_USART_INBUFFER_SIZE <= 0)
-#error "USART Input file can't be enabled without input buffer"
-#endif
-#if (defined CONFIG_IOE_USART_OUTFILE) && (CONFIG_IOE_USART_OUTBUFFER_SIZE <= 0)
-#error "USART Input file can't be enabled without output buffer"
-#endif
-
#if CONFIG_IOE_USART_INBUFFER_SIZE > 0
#define _IOE_USART_INBUFFER
volatile IOEBUFFER(_ioe_usart_inbuffer, CONFIG_IOE_USART_INBUFFER_SIZE);
@@ -70,14 +30,9 @@ volatile IOEBUFFER(_ioe_usart_outbuffer, CONFIG_IOE_USART_OUTBUFFER_SIZE);
/*
- * Initialize USART device with 115200 baud rate, no parity, single stop bit,
- * and 8 data bits.
- * This function serves as fast default initialization.
+ * Initialize USART device.
*/
-void usart_init_uart(void);
-void usart_init_async(enum usartBaudrate,
- enum usartParity, enum usartStopBit,
- enum usartDataBits);
+void usart_init_async(void);
void usart_send(uint8_t data);
#ifdef _IOE_USART_OUTBUFFER
void usart_send_str(char *str);