--- /dev/null
+/* uart.h */
+/*
+ This file is part of the AVR-Crypto-Lib.
+ Copyright (C) 2014 Daniel Otte (daniel.otte@rub.de)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef UART_H_
+#define UART_H_
+
+#include "config.h"
+
+#if UART_NI == 0
+# include "uart_i.h"
+#elif UART_NI == 1
+# include "uart_ni.h"
+#else
+# error "You have to set 'UART_NI' to '0' for interrupt driven uart o to '1' for a polling driven uart!"
+#endif
+
+#endif /* UART_H_ */
--- /dev/null
+/* uart_i.c */
+/*
+ This file is part of the AVR-uart_i.
+ Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * \file uart_i.c
+ * \email daniel.otte@rub.de
+ * \author Daniel Otte
+ * \date 2009-07-24
+ * \license GPLv3 or later
+ * \ingroup uart_i
+ * \brief implementation of interrupt based uart
+ */
+
+#include <stdlib.h>
+#include <avr/interrupt.h>
+#include "config.h"
+#include "uart.h"
+
+#define XON_VALUE 0x11
+#define XOFF_VALUE 0x13
+
+#if UART0_I
+
+#ifndef UART0_PARATY
+# warning "UART0: using default paraty: 'none'"
+# define UART0_PARATY UART_PARATY_NONE
+#endif
+
+#ifndef UART0_STOPBITS
+# warning "UART0: using default ammount of stop bits: '1'"
+# define UART0_STOPBITS UART_STOPBITS_1
+#endif
+
+#ifndef UART0_DATABITS
+# warning "UART0: using default ammount of data bits: '8'"
+# define UART0_DATABITS UART_DATABITS_8
+#endif
+
+#if UART0_DATABITS == UART_DATABITS_9
+# error "UART0: data bits==9 not supported"
+#endif
+
+
+#ifdef UDR
+# define OLD_UART
+# ifdef UDR0
+# error "can not decide which registernames to use, UDR and UDR0 are defined"
+# endif
+#endif
+
+#ifdef OLD_UART
+# define UCSR0A UCSRA
+# define UCSR0B UCSRB
+# define UCSR0C UCSRC
+# define UBRR0H UBRRH
+# define UBRR0L UBRRL
+# define UDR0 UDR
+# define TXEN0 TXEN
+# define RXEN0 RXEN
+# define UDRE0 UDRE
+# define RXC0 RXC
+# define TXB80 TXB8
+# define RXB80 RXB8
+#endif
+
+uart0_ctx_t uart0_ctx;
+uint8_t uart0_rxbuffer[UART0_RXBUFFER_SIZE];
+uint8_t uart0_txbuffer[UART0_TXBUFFER_SIZE];
+
+void uart0_init(void){
+ circularbytebuffer_init2(UART0_RXBUFFER_SIZE, &(uart0_ctx.rxb), uart0_rxbuffer);
+ circularbytebuffer_init2(UART0_TXBUFFER_SIZE, &(uart0_ctx.txb), uart0_txbuffer);
+#if UART0_HOOK
+ uart0_ctx.hook = NULL;
+ uart0_ctx.hook_running = 0;
+#endif
+#if UART0_SWFLOWCTRL
+ uart0_ctx.txon = 1;
+ uart0_ctx.rxon = 1;
+#endif
+ #define BAUD UART0_BAUD_RATE
+ #include <util/setbaud.h>
+ UBRR0H = UBRRH_VALUE;
+ UBRR0L = UBRRL_VALUE;
+ #if USE_2X
+ UCSR0A |= _BV(U2X0);
+ #else
+ UCSR0A &= ~_BV(U2X0);
+ #endif
+ UCSR0C = (UART0_PARATY<<4)|(UART0_STOPBITS<<3)|((UART0_DATABITS&3)<<1);
+ UCSR0B = _BV(RXCIE0) | _BV(UDRIE0) | _BV(RXEN0) | _BV(TXEN0) ; /* enable TX and RX and interrupts */
+ sei();
+}
+
+ISR(USART0_UDRE_vect){
+ uint16_t x;
+ x = circularbytebuffer_get_fifo(&(uart0_ctx.txb));
+ if(x==0xffff){
+ /* the transmit buffer is empty, disable interrupt */
+ UCSR0B &= (uint8_t)~_BV(UDRIE0);
+ return;
+ }
+#if UART0_SWFLOWCTRL
+ while(!uart0_ctx.txon)
+ ;
+#endif
+ UDR0 = x;
+}
+
+void uart0_putc (uint16_t c){
+#if UART0_SWFLOWCTRL
+ while(!uart0_ctx.txon)
+ ;
+#endif
+ while(circularbytebuffer_cnt(&(uart0_ctx.txb))==UART0_TXBUFFER_SIZE)
+ ;
+ cli();
+ circularbytebuffer_append((uint8_t)c, &(uart0_ctx.txb));
+ sei();
+ UCSR0B |= (uint8_t)_BV(UDRIE0);
+}
+
+ISR(USART0_RX_vect){
+ uint16_t c;
+ c = UDR0;
+#if UART0_SWFLOWCTRL
+ if(c==XON_VALUE){
+ uart0_ctx.txon = 1;
+ return;
+ }
+ if(c==XOFF_VALUE){
+ uart0_ctx.txon = 0;
+ return;
+ }
+#endif
+#if UART0_HOOK
+ if((!uart0_ctx.hook_running) && uart0_ctx.hook){
+ uart0_ctx.hook_running=1;
+ sei();
+ do{
+ uart0_ctx.hook(c);
+ }while((c=circularbytebuffer_get_fifo(&(uart0_ctx.rxb)))!=0xffff);
+ uart0_ctx.hook_running=0;
+ return;
+ }
+#endif
+ if(circularbytebuffer_cnt(&(uart0_ctx.rxb))==UART0_RXBUFFER_SIZE)
+ return;
+ circularbytebuffer_append(c, &(uart0_ctx.rxb));
+#if UART0_SWFLOWCTRL
+ if(circularbytebuffer_cnt(&(uart0_ctx.rxb))>UART0_THRESH_HIGH && uart0_ctx.rxon){
+ uart0_ctx.rxon = 0;
+ circularbytebuffer_push(XOFF_VALUE, &(uart0_ctx.txb));
+ UCSR0B |= (uint8_t)_BV(UDRIE0);
+ }
+ if(circularbytebuffer_cnt(&(uart0_ctx.rxb))<UART0_THRESH_LOW && !uart0_ctx.rxon){
+ uart0_ctx.rxon = 1;
+ circularbytebuffer_push(XON_VALUE, &(uart0_ctx.txb));
+ UCSR0B |= (uint8_t)_BV(UDRIE0);
+ }
+#endif
+}
+
+uint16_t uart0_getc(void){
+ uint8_t ret;
+ while(circularbytebuffer_cnt(&(uart0_ctx.rxb))==0)
+ ;
+ cli();
+ ret = circularbytebuffer_get_fifo(&(uart0_ctx.rxb));
+ sei();
+ return ret;
+}
+
+uint8_t uart0_dataavail(void){
+ return circularbytebuffer_cnt(&(uart0_ctx.rxb));
+}
+
+#if UART0_HOOK
+void uart0_sethook(void(*fpt)(uint8_t)){
+ uart0_ctx.hook = fpt;
+}
+#endif
+
+#endif /* UART0_I */
--- /dev/null
+/* uart_ni.c */
+/*
+ This file is part of the AVR-uart_ni.
+ Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+#include "config.h"
+#include <avr/io.h>
+#include "avr-asm-macros.S"
+#include "uart_defs.h"
+/******************************************************************************/
+
+#if UART0_NI
+
+#ifndef UART0_PARATY
+# warning "UART0: using default paraty: 'none'"
+# define UART0_PARATY UART_PARATY_NONE
+#endif
+
+#ifndef UART0_STOPBITS
+# warning "UART0: using default ammount of stop bits: '1'"
+# define UART0_STOPBITS UART_STOPBITS_1
+#endif
+
+#ifndef UART0_DATABITS
+# warning "UART0: using default ammount of data bits: '8'"
+# define UART0_DATABITS UART_DATABITS_8
+#endif
+
+
+#ifdef UDR
+# define OLD_UART
+# ifdef UDR0
+# error "can not decide which registernames to use, UDR and UDR0 are defined"
+# endif
+#endif
+
+#ifdef OLD_UART
+# define UCSR0A UCSRA
+# define UCSR0B UCSRB
+# define UCSR0C UCSRC
+# define UBRR0H UBRRH
+# define UBRR0L UBRRL
+# define UDR0 UDR
+# define TXEN0 TXEN
+# define RXEN0 RXEN
+# define UDRE0 UDRE
+# define RXC0 RXC
+# define TXB80 TXB8
+# define RXB80 RXB8
+#endif
+
+#define BAUD (UART0_BAUD_RATE)
+#include "setbaud_asm.inc"
+
+/******************************************************************************/
+
+.global uart0_init
+uart0_init:
+ ldi r24, UBRRH_VALUE
+ STORE_IO UBRR0H, r24
+ ldi r24, UBRRL_VALUE
+ STORE_IO UBRR0L, r24
+#if _SFR_IO_REG_P(UCSR0A)
+ #if USE_2X
+ sbi _SFR_IO_ADDR(UCSR0A), 1
+ #else
+ cbi _SFR_IO_ADDR(UCSR0A), 1
+ #endif
+#else
+ lds r24, _SFR_MEM_ADDR(UCSR0A)
+ #if USE_2X
+ ori r24, 0x02
+ #else
+ andi r24, ~0x02
+ #endif
+ sts _SFR_MEM_ADDR(UCSR0A), r24
+#endif
+ ldi r24, (UART0_PARATY<<4)|(UART0_STOPBITS<<3)|((UART0_DATABITS&3)<<1)
+ STORE_IO UCSR0C, r24
+ ldi r24, _BV(TXEN0)|_BV(RXEN0)|((UART0_DATABITS>>2)<<2)
+ STORE_IO UCSR0B, r24
+ ret
+
+/******************************************************************************/
+
+.global uart0_putc
+uart0_putc:
+#if _SFR_IO_REG_P(UCSR0A)
+ sbis _SFR_IO_ADDR(UCSR0A), UDRE0
+ rjmp uart0_putc
+#else
+ lds r25, _SFR_MEM_ADDR(UCSR0A)
+ sbrs r25, UDRE0
+ rjmp uart0_putc
+#endif
+#if UART0_DATABITS == UART_DATABITS_9
+# if _SFR_IO_REG_P(UCSR0B)
+ sbi UCSR0B, TXB80
+ sbrs r25, 0
+ cbi UCSR0B, TXB80
+# else
+ lds r23, _SFR_MEM_ADDR(UCSR0B)
+ bst r25, 0
+ bld r23, TXB80
+ sts _SFR_MEM_ADDR(UCSR0B), r23
+# endif
+#endif
+ STORE_IO UDR0, r24
+ ret
+
+/******************************************************************************/
+
+.global uart0_getc
+uart0_getc:
+#if _SFR_IO_REG_P(UCSR0A)
+ sbis _SFR_IO_ADDR(UCSR0A), RXC0
+ rjmp uart0_putc
+#else
+ lds r25, _SFR_MEM_ADDR(UCSR0A)
+ sbrs r25, RXC0
+ rjmp uart0_getc
+#endif
+
+#if UART0_DATABITS == UART_DATABITS_9
+ LOAD_IO r25, UCSR0B
+ lsr r25
+ andi r25, 1
+#else
+ clr r25
+#endif
+ LOAD_IO r24, UDR0
+ ret
+
+/******************************************************************************/
+
+.global uart0_dataavail
+uart0_dataavail:
+ LOAD_IO r24, UCSR0A
+ andi r24, _BV(RXC0)
+ clr r25
+ ret
+
+#endif /* UART0_NI */
+
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+
+#if UART1_NI
+
+#ifndef UDRE1
+# error "registernames for second UART not defined"
+#endif
+
+#ifndef UART1_PARATY
+# warning "UART1: using default paraty: 'none'"
+# define UART1_PARATY UART_PARATY_NONE
+#endif
+
+#ifndef UART1_STOPBITS
+# warning "UART1: using default ammount of stop bits: '1'"
+# define UART1_STOPBITS UART_STOPBITS_1
+#endif
+
+#ifndef UART1_DATABITS
+# warning "UART1: using default ammount of data bits: '8'"
+# define UART1_DATABITS UART_DATABITS_8
+#endif
+
+#ifdef BAUD
+# undef BAUD
+#endif
+
+#define BAUD (UART1_BAUD_RATE)
+
+#include "setbaud_asm.inc"
+
+/******************************************************************************/
+
+.global uart1_init
+uart1_init:
+ ldi r24, UBRRH_VALUE
+ STORE_IO UBRR1H, r24
+ ldi r24, UBRRL_VALUE
+ STORE_IO UBRR1L, r24
+#if _SFR_IO_REG_P(UCSR1A)
+ #if USE_2X
+ sbi _SFR_IO_ADDR(UCSR1A), 1
+ #else
+ cbi _SFR_IO_ADDR(UCSR1A), 1
+ #endif
+#else
+ lds r24, _SFR_MEM_ADDR(UCSR1A)
+ #if USE_2X
+ ori r24, 0x02
+ #else
+ andi r24, ~0x02
+ #endif
+ sts _SFR_MEM_ADDR(UCSR1A), r24
+#endif
+ ldi r24, (UART1_PARATY<<4)|(UART1_STOPBITS<<3)|((UART1_DATABITS&3)<<1)
+ STORE_IO UCSR1C, r24
+ ldi r24, _BV(TXEN1)|_BV(RXEN1)|((UART1_DATABITS>>2)<<2)
+ STORE_IO UCSR1B, r24
+ ret
+
+/******************************************************************************/
+
+.global uart1_putc
+uart1_putc:
+#if _SFR_IO_REG_P(UCSR1A)
+ sbis _SFR_IO_ADDR(UCSR1A), UDRE1
+ rjmp uart1_putc
+#else
+ lds r23, _SFR_MEM_ADDR(UCSR1A)
+ sbrs r23, UDRE1
+ rjmp uart1_putc
+#endif
+#if UART1_DATABITS == UART_DATABITS_9
+# if _SFR_IO_REG_P(UCSR1B)
+ sbi UCSR1B, TXB81
+ sbrs r25, 0
+ cbi UCSR1B, TXB81
+# else
+ lds r23, _SFR_MEM_ADDR(UCSR1B)
+ bst r25, 0
+ bld r23, TXB81
+ sts _SFR_MEM_ADDR(UCSR1B), r23
+# endif
+#endif
+ STOREIO UDR1, r24
+ ret
+
+/******************************************************************************/
+
+.global uart1_getc
+uart1_getc:
+#if _SFR_IO_REG_P(UCSR1A)
+ sbis _SFR_IO_ADDR(UCSR1A), RXC1
+ rjmp uart1_putc
+#else
+ lds r25, _SFR_MEM_ADDR(UCSR1A)
+ sbrs r25, RXC1
+ rjmp uart1_getc
+#endif
+#if UART1_DATABITS == UART_DATABITS_9
+ LOAD_IO r25, UCSR1B
+ lsr r25
+ andi r25, 1
+#else
+ clr r25
+#endif
+ LOAD_IO r24, UDR1
+ ret
+
+/******************************************************************************/
+
+.global uart1_dataavail
+uart1_dataavail:
+ LOAD_IO r24, UCSR1A
+ andi r24, _BV(RXC1)
+ clr r25
+ ret
+
+#endif /* UART1_NI */
--- /dev/null
+/* uart_ni.h */
+/*
+ This file is part of the AVR-uart_ni.
+ Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * \file uart_ni.h
+ * \email daniel.otte@rub.de
+ * \author Daniel Otte
+ * \date 2009-07-24
+ * \license GPLv3 or later
+ * \ingroup uart_ni
+ * \brief declaration for non-interrupt uart
+ */
+
+#ifndef UART_NI_H_
+#define UART_NI_H_
+
+#include "config.h"
+#include <stdint.h>
+
+#if UART0_NI
+
+/**
+ * \brief initialize uart0.
+ * This function initializes the first uart according to the parameter specified
+ * in config.h .
+ */
+void uart0_init(void);
+
+/**
+ * \brief send data through uart0.
+ * This function sends data through the first uart
+ * (the data size is defined in config.h).
+ * \param c data to send
+ */
+void uart0_putc(uint16_t c);
+
+/**
+ * \brief read data from uart0.
+ * This function reads data from the first uart
+ * (the data size is defined in config.h).
+ * \return data received by uart0
+ */
+uint16_t uart0_getc(void);
+
+/**
+ * \brief checks if data is available.
+ *
+ * This function checks the state of the input buffer of uart0 and
+ * returns if data is available or not.
+ * \return zero if no data is available else a value different from zero is returned
+ */
+uint8_t uart0_dataavail(void);
+
+#define uart0_flush()
+
+#endif /* UART0_NI */
+
+#if UART1_NI
+/**
+ * \brief initialize uart1.
+ * This function initializes the second uart according to the parameter specifyed
+ * in config.h .
+ */
+void uart1_init(void);
+
+/**
+ * \brief send data through uart1.
+ * This function sends data through the second uart
+ * (the data size is defined in config.h).
+ * \param c data to send
+ */
+void uart1_putc(uint16_t c);
+
+/**
+ * \brief read data from uart1.
+ * This function reads data from the second uart
+ * (the data size is defined in config.h).
+ * \return data received by uart1
+ */
+uint16_t uart1_getc(void);
+
+/**
+ * \brief checks if data is available.
+ * This function checks the state of the input buffer of uart1 and
+ * returns if data is available or not.
+ * \return zero if no data is available else a value different from zero is returned
+ */
+uint8_t uart1_dataavail(void);
+
+#define uart1_flush()
+
+#endif /* UART1_NI */
+
+#endif /* UART_NI_H_ */