Permalink
Browse files

more usarts

  • Loading branch information...
1 parent 2209716 commit 6dc329a581624dfb865c6e7c8d1af368ae866269 @embedit committed Jul 18, 2011
Showing with 441 additions and 0 deletions.
  1. +3 −0 README
  2. +121 −0 lib/usart2.c
  3. +97 −0 lib/usart2.h
  4. +122 −0 lib/usart3.c
  5. +98 −0 lib/usart3.h
View
3 README
@@ -11,3 +11,6 @@ History
* utils library added
* usart library added
* usart1 library added
+ * usart2 library added
+ * usart3 library added
+
View
@@ -0,0 +1,121 @@
+/*------------------------------------------------------------------------------
+ * Copyright 1994-2008 The FreeBSD Project. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *----------------------------------------------------------------------------*/
+
+
+#include "usart2.h"
+
+/* Static Variables */
+static char USART2_RxBuf[USART2_RX_BUFFER_SIZE];
+static volatile char USART2_RxHead;
+static volatile char USART2_RxTail;
+static char USART2_TxBuf[USART2_TX_BUFFER_SIZE];
+static volatile char USART2_TxHead;
+static volatile char USART2_TxTail;
+
+/* Initialize USART */
+void usart2_init( void )
+{
+ #undef BAUD // avoid compiler warning
+ #define BAUD BAUD2
+ #include <util/setbaud.h>
+ UBRR2L = UBRRL_VALUE;
+ UBRR2H = UBRRH_VALUE;
+ UCSR2B = ( ( 1 << RXCIE2 ) | ( 1 << RXEN2 ) | ( 1 << TXEN2 ) );
+ UCSR2C = (1<<UCSZ21)|(1<<UCSZ20);
+#if USE2_2X
+ UCSR2A |= (1 << U2X2);
+#endif /*USE_2X*/
+ USART2_RxTail = 0;
+ USART2_RxHead = 0;
+ USART2_TxTail = 0;
+ USART2_TxHead = 0;
+
+/* Init direction Pin for RS485 Driver, if RS485 is used */
+#ifdef RS485_2_PORT
+ UCSR2B |= (1 << TXCIE2);
+ RS485_2_PORT &= ~(1<<RS485_2_BIT);
+ RS485_2_DDR |= (1<<RS485_2_BIT);
+#endif /*RS485_1_PORT*/
+}
+
+char usart2_getc( void )
+{
+ unsigned char tmp;
+
+ while ( USART2_RxHead == USART2_RxTail );
+ tmp = ( USART2_RxTail + 1 ) & USART2_RX_BUFFER_MASK;
+ USART2_RxTail = tmp;
+ return USART2_RxBuf[tmp];
+}
+
+void usart2_putc( char data )
+{
+ unsigned char tmp;
+ tmp = ( USART2_TxHead + 1 ) & USART2_TX_BUFFER_MASK;
+ while ( tmp == USART2_TxTail );
+ USART2_TxBuf[tmp] = data;
+ USART2_TxHead = tmp;
+ UCSR2B |= (1<<UDRIE2);
+}
+
+char usart2_data_received( void )
+{
+ return ( USART2_RxHead != USART2_RxTail );
+}
+
+ISR(USART2_RX_vect)
+{
+ char data;
+ unsigned char tmp;
+ data = UDR2;
+ tmp = ( USART2_RxHead + 1 ) & USART2_RX_BUFFER_MASK;
+ USART2_RxHead = tmp;
+ USART2_RxBuf[tmp] = data;
+}
+
+ISR(USART2_UDRE_vect)
+{
+ unsigned char tmp;
+
+ if ( USART2_TxHead != USART2_TxTail )
+ {
+ #ifdef RS485_2_PORT
+ RS485_2_PORT |= (1 << RS485_2_BIT);
+ #endif /*RS485_2_PORT*/
+ tmp = ( USART2_TxTail + 1 ) & USART2_TX_BUFFER_MASK;
+ USART2_TxTail = tmp;
+ UDR2 = USART2_TxBuf[tmp];
+ }
+ else
+ {
+ UCSR2B &= ~(1<<UDRIE2);
+ }
+}
+
+#ifdef RS485_2_PORT
+ISR(UART2_TXC_IRQ)
+{
+ RS485_2_PORT &= ~(1<<RS485_2_BIT);
+}
+#endif /*RS485_2_PORT*/
View
@@ -0,0 +1,97 @@
+#ifndef USART2_H
+#define USART2_H 1
+/*------------------------------------------------------------------------------
+ * Copyright 1994-2008 The FreeBSD Project. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *----------------------------------------------------------------------------*/
+
+#include <avr/version.h>
+
+#if __AVR_LIBC_VERSION__ < 10602UL
+#error The USART Module needs avr-libc Version 1.6.2 or later
+#endif
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <config.h>
+
+#ifndef BAUD2
+ #error "The Parameter BAUD2 is not defined in the Project.\
+ Please add the Parameter to config.h\
+ You can find an Example on wiki.elektronik-projekt.de"
+#endif /*BAUD2*/
+
+/* UART Buffer Defines */
+#ifndef USART2_RX_BUFFER_SIZE
+ #error "The Parameter USART2_RX_BUFFER_SIZE is not defined in the Project.\
+ Please add the Parameter to config.h.\
+ You can find an Example on wiki.elektronik-projekt.de"
+#endif /*USART2_RX_BUFFER_SIZE*/
+
+#ifndef USART2_TX_BUFFER_SIZE
+ #error "The Parameter USART2_TX_BUFFER_SIZE is not defined in the Project.\
+ Please add the Parameter to config.h.\
+ You can find an Example on wiki.elektronik-projekt.de"
+#endif /*USART2_TX_BUFFER_SIZE*/
+
+#define USART2_RX_BUFFER_MASK ( USART2_RX_BUFFER_SIZE - 1 )
+#define USART2_TX_BUFFER_MASK ( USART2_TX_BUFFER_SIZE - 1 )
+#if ( USART2_RX_BUFFER_SIZE & USART2_RX_BUFFER_MASK )
+ #error RX buffer size is not a power of 2
+#endif
+#if ( USART2_TX_BUFFER_SIZE & USART2_TX_BUFFER_MASK )
+ #error TX buffer size is not a power of 2
+#endif
+
+/**----------------------------------
+ * Prototypes
+ *----------------------------------*/
+
+/**----------------------------------
+ * @brief Initialize USART
+ * @param void
+ * @return void
+ *----------------------------------*/
+void usart2_init( void );
+
+/**----------------------------------
+ * @brief Receives Char from USART Buffer
+ * @param void
+ * @return char
+ *----------------------------------*/
+char usart2_getc( void );
+
+/**----------------------------------
+ * @brief Sends Char to USART Buffer
+ * @param Char to send
+ * @return void
+ *----------------------------------*/
+void usart2_putc( char data );
+
+/**----------------------------------
+ * @brief Test if received char is in Buffer
+ * @param void
+ * @return Return 0 (FALSE) if the receive buffer is empty
+ *----------------------------------*/
+char usart2_data_received( void );
+
+#endif /* USART2_H */
View
@@ -0,0 +1,122 @@
+/*------------------------------------------------------------------------------
+ * Copyright 1994-2008 The FreeBSD Project. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *----------------------------------------------------------------------------*/
+
+
+#include "usart3.h"
+
+/* Static Variables */
+static char USART3_RxBuf[USART3_RX_BUFFER_SIZE];
+static volatile char USART3_RxHead;
+static volatile char USART3_RxTail;
+static char USART3_TxBuf[USART3_TX_BUFFER_SIZE];
+static volatile char USART3_TxHead;
+static volatile char USART3_TxTail;
+
+/* Initialize USART */
+void usart3_init( void )
+{
+ #undef BAUD // avoid compiler warning
+ #define BAUD BAUD3
+ #include <util/setbaud.h>
+ UBRR3L = UBRRL_VALUE;
+ UBRR3H = UBRRH_VALUE;
+ UCSR3B = ( ( 1 << RXCIE3 ) | ( 1 << RXEN3 ) | ( 1 << TXEN3 ) );
+ UCSR3C = (1<<UCSZ31)|(1<<UCSZ30);
+#if USE3_2X
+ UCSR3A |= (1 << U2X3);
+#endif /*USE_2X*/
+ USART3_RxTail = 0;
+ USART3_RxHead = 0;
+ USART3_TxTail = 0;
+ USART3_TxHead = 0;
+
+/* Init direction Pin for RS485 Driver, if RS485 is used */
+#ifdef RS485_3_PORT
+ UCSR3B |= (1 << TXCIE3);
+ RS485_3_PORT &= ~(1<<RS485_3_BIT);
+ RS485_3_DDR |= (1<<RS485_3_BIT);
+#endif /*RS485_1_PORT*/
+}
+
+char usart3_getc( void )
+{
+ unsigned char tmp;
+
+ while ( USART3_RxHead == USART3_RxTail );
+ tmp = ( USART3_RxTail + 1 ) & USART3_RX_BUFFER_MASK;
+ USART3_RxTail = tmp;
+ return USART3_RxBuf[tmp];
+}
+
+void usart3_putc( char data )
+{
+ unsigned char tmp;
+ tmp = ( USART3_TxHead + 1 ) & USART3_TX_BUFFER_MASK;
+ while ( tmp == USART3_TxTail );
+ USART3_TxBuf[tmp] = data;
+ USART3_TxHead = tmp;
+ UCSR3B |= (1<<UDRIE3);
+}
+
+char usart3_data_received( void )
+{
+ return ( USART3_RxHead != USART3_RxTail );
+}
+
+
+ISR(USART3_RX_vect)
+{
+ char data;
+ unsigned char tmp;
+ data = UDR3;
+ tmp = ( USART3_RxHead + 1 ) & USART3_RX_BUFFER_MASK;
+ USART3_RxHead = tmp;
+ USART3_RxBuf[tmp] = data;
+}
+
+ISR(USART3_UDRE_vect)
+{
+ unsigned char tmp;
+
+ if ( USART3_TxHead != USART3_TxTail )
+ {
+ #ifdef RS485_3_PORT
+ RS485_3_PORT |= (1 << RS485_3_BIT);
+ #endif /*RS485_3_PORT*/
+ tmp = ( USART3_TxTail + 1 ) & USART3_TX_BUFFER_MASK;
+ USART3_TxTail = tmp;
+ UDR3 = USART3_TxBuf[tmp];
+ }
+ else
+ {
+ UCSR3B &= ~(1<<UDRIE3);
+ }
+}
+
+#ifdef RS485_3_PORT
+ISR(UART2_TXC_IRQ)
+{
+ RS485_3_PORT &= ~(1<<RS485_3_BIT);
+}
+#endif /*RS485_3_PORT*/
Oops, something went wrong.

0 comments on commit 6dc329a

Please sign in to comment.