changeset 169:61495fdd34c5 main

Add support for the mega16m1 UART (polling mode only).
author Bob Cook <bob@bobcookdev.com>
date Tue, 01 Jan 2013 11:33:25 -0800
parents cf51b02db081
children 9ebb7a1db87d
files main/packages/avr/device/uart.cpp
diffstat 1 files changed, 30 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/main/packages/avr/device/uart.cpp	Tue Jan 01 11:32:19 2013 -0800
+++ b/main/packages/avr/device/uart.cpp	Tue Jan 01 11:33:25 2013 -0800
@@ -358,10 +358,26 @@
 
 #elif defined( __AVR_ATmega16M1__ )
 
-#error TODO...
+    // reset the LIN/UART module
+
+    LINCR = ( 1 << LSWRES );
+    while ( LINSIR & ( 1 << LBUSY ) ) ;
+
+    // switch to UART mode, then enable TX & RX separately
+
+    LINCR = 0
+
+          | ( 0 << LCONF1 ) | ( 0 << LCONF0 )   // 8-bit, no parity, 1 stop bit
+          | ( 1 << LENA )                       // enable
+          | ( 1 << LCMD2 )                      // uart enable
+          ;
+
+    LINCR |= ( 1 << LCMD1 ) | ( 1 << LCMD0 );   // tx & rx output enable
 
 #endif  // #if defined( ...processor selection... )
 
+    //-------------------------------------------------------------------------------------
+
 #if defined( PRJ_UART0_INITIAL_BAUDRATE )
 
     uart0_set_baudrate( PRJ_UART0_INITIAL_BAUDRATE );
@@ -392,7 +408,11 @@
     
 #elif defined( __AVR_ATmega16M1__ )
 
-#error TODO...
+    // the original computation (for non-M1 devices) uses a divsor of 16 rather than 32,
+    // so divide the input value by 2
+
+    LINBTR = 32;
+    LINBRR = ( ( highbyte << 8 ) + ( lowbyte ) ) / 2;
 
 #endif
 
@@ -411,7 +431,7 @@
 
 #if defined( __AVR_ATmega8__ ) || defined( __AVR_ATmega16__ )
 
-        return ( UCSRA & ( 1 << RXC ) ) != 0;
+    return ( UCSRA & ( 1 << RXC ) ) != 0;
 
 
 #elif defined( __AVR_ATmega88__ ) \
@@ -419,11 +439,11 @@
       || defined( __AVR_ATmega328P__ ) \
       || defined( __AVR_ATmega128__ )
 
-        return ( UCSR0A & ( 1 << RXC0 ) ) != 0;
+    return ( UCSR0A & ( 1 << RXC0 ) ) != 0;
 
 #elif defined( __AVR_ATmega16M1__ )
 
-#error TODO...
+    return ( LINSIR & ( 1 << LRXOK ) ) != 0;
 
 #endif
 
@@ -457,7 +477,8 @@
 
 #elif defined( __AVR_ATmega16M1__ )
 
-#error TODO...
+    while ( ! ( LINSIR & ( 1 << LRXOK ) ) ) ;
+    return LINDAT;
 
 #endif
 
@@ -504,10 +525,6 @@
 
     UCSR0B |= ( 1 << UDRIE0 );
 
-#elif defined( __AVR_ATmega16M1__ )
-
-#error TODO...
-
 #endif
 
 #else   // defined( PRJ_UART0_USE_POLLED_MODE )
@@ -527,7 +544,9 @@
 
 #elif defined( __AVR_ATmega16M1__ )
 
-#error TODO...
+    while ( ( LINSIR & ( 1 << LBUSY ) ) ) ;
+    //while ( ! ( LINSIR & ( 1 << LTXOK ) ) ) ;
+    LINDAT = data;
 
 #endif