From 0265f09a5ccf301cc776d9c3fb48d3d088087062 Mon Sep 17 00:00:00 2001
From: dak664 <dak664>
Date: Mon, 15 Mar 2010 18:52:55 +0000
Subject: [PATCH] Add RS232  port option to jackdaw USB stick

---
 cpu/avr/Makefile.avr                   |  4 +-
 cpu/avr/dev/rs232.c                    | 51 +++++++++++++--
 cpu/avr/dev/rs232_at90usb1287.h        | 88 +++++++++++++++++++++++++-
 cpu/avr/dev/usb/usb_specific_request.c | 10 +--
 4 files changed, 139 insertions(+), 14 deletions(-)

diff --git a/cpu/avr/Makefile.avr b/cpu/avr/Makefile.avr
index 59d148313..225de907d 100644
--- a/cpu/avr/Makefile.avr
+++ b/cpu/avr/Makefile.avr
@@ -1,4 +1,4 @@
-# $Id: Makefile.avr,v 1.21 2010/02/16 21:56:15 dak664 Exp $
+# $Id: Makefile.avr,v 1.22 2010/03/15 18:52:55 dak664 Exp $
 
 ### Check if we are running under Windows
 
@@ -31,7 +31,7 @@ ifdef USB
 ### Add the directories for the USB stick and remove the default rs232 driver
 CONTIKI_CPU_DIRS            += dev/usb dev/usb/serial dev/usb/rndis dev/usb/storage
 CONTIKI_TARGET_SOURCEFILES  += $(USB)
-AVR        = clock.c mtarch.c eeprom.c flash.c leds-arch.c watchdog.c rtimer-arch.c
+//AVR        = clock.c mtarch.c eeprom.c flash.c leds-arch.c watchdog.c rtimer-arch.c
 endif
 
 #For a coffee file system, the application makefile can define COFFEE_FILES=n
diff --git a/cpu/avr/dev/rs232.c b/cpu/avr/dev/rs232.c
index d84865605..728b350c6 100644
--- a/cpu/avr/dev/rs232.c
+++ b/cpu/avr/dev/rs232.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * @(#)$Id: rs232.c,v 1.5 2009/03/17 20:32:22 adamdunkels Exp $
+ * @(#)$Id: rs232.c,v 1.6 2010/03/15 18:52:55 dak664 Exp $
  */
 
 #include <stdio.h>
@@ -80,10 +80,6 @@ static rs232_t rs232_ports[2] = {
     NULL
   }
 };
-#else
-#error Please define the UART registers for your MCU!
-#endif
-
 /*---------------------------------------------------------------------------*/
 ISR(USART0_TX_vect)
 {
@@ -101,7 +97,6 @@ ISR(USART0_RX_vect)
     rs232_ports[RS232_PORT_0].input_handler(c);
   }
 }
-
 /*---------------------------------------------------------------------------*/
 ISR(USART1_TX_vect)
 {
@@ -120,6 +115,50 @@ ISR(USART1_RX_vect)
   }
 }
 
+#elif defined (__AVR_AT90USB1287__)
+/* Has only UART1, map it to port 0 */
+typedef struct {
+  volatile uint8_t * UDR;
+  volatile uint8_t * UBRRH;
+  volatile uint8_t * UBRRL;
+  volatile uint8_t * UCSRB;
+  volatile uint8_t * UCSRC;
+  volatile uint8_t txwait;
+  int (* input_handler)(unsigned char);
+} rs232_t;
+
+static rs232_t rs232_ports[1] = {
+  {  // UART1
+    &UDR1,
+    &UBRR1H,
+    &UBRR1L,
+    &UCSR1B,
+    &UCSR1C,
+    0,
+    NULL
+  }
+};
+/*---------------------------------------------------------------------------*/
+ISR(USART1_TX_vect)
+{
+  rs232_ports[RS232_PORT_0].txwait = 0;
+}
+
+/*---------------------------------------------------------------------------*/
+ISR(USART1_RX_vect)
+{
+  unsigned char c;
+
+  c = *(rs232_ports[RS232_PORT_0].UDR);
+
+  if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
+    rs232_ports[RS232_PORT_0].input_handler(c);
+  }
+}
+#else
+#error Please define the UART registers for your MCU!
+#endif
+
 /*---------------------------------------------------------------------------*/
 void
 rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt)
diff --git a/cpu/avr/dev/rs232_at90usb1287.h b/cpu/avr/dev/rs232_at90usb1287.h
index 87e138b31..cb3439e7f 100644
--- a/cpu/avr/dev/rs232_at90usb1287.h
+++ b/cpu/avr/dev/rs232_at90usb1287.h
@@ -41,20 +41,104 @@
 
 #ifndef __RS232_AT90USB1287__
 #define __RS232_AT90USB1287__
+
 /******************************************************************************/
 /***   Includes                                                               */
 /******************************************************************************/
 #include <avr/io.h>
 
 /******************************************************************************/
-/***   RS232 ports                                                            */
+/***   RS232 ports  - Has only UART1, map it into port 0                      */
 /******************************************************************************/
 #define RS232_PORT_0 0
-#define RS232_PORT_1 1
 
 /******************************************************************************/
 /***   Baud rates                                                             */
 /******************************************************************************/
+#if MCU_MHZ == 16
+/* Single speed operation (U2X = 0)*/
+#warning 16MHz
+#define USART_BAUD_2400 416
+#define USART_BAUD_4800 207
+#define USART_BAUD_9600 103
+#define USART_BAUD_14400 68
+#define USART_BAUD_19200 51
+#define USART_BAUD_28800 34
+#define USART_BAUD_38400 25
+#define USART_BAUD_57600 16
+#define USART_BAUD_76800 12
+#define USART_BAUD_115200 8
+#define USART_BAUD_230400 3
+#define USART_BAUD_250000 3
+#define USART_BAUD_500000 1
+#define USART_BAUD_1000000 0
+#elif MCU_MHZ == 8
+/* Single speed operation (U2X = 0)*/
+#define USART_BAUD_2400 207
+#define USART_BAUD_4800 103
+#define USART_BAUD_9600 51
+#define USART_BAUD_14400 34
+#define USART_BAUD_19200 25
+#define USART_BAUD_28800 16
+#define USART_BAUD_38400 12
+#define USART_BAUD_57600 8
+#define USART_BAUD_76800 6
+#define USART_BAUD_115200 3
+#define USART_BAUD_230400 1
+#define USART_BAUD_250000 1
+#define USART_BAUD_500000 0
+#else
+#error "Please define the baud rates for your CPU clock: ATmega128 handbook p. \
+195-198 or set the rate in contiki-conf.h"
+#endif
+
+
+/******************************************************************************/
+/***   Interrupt settings                                                     */
+/******************************************************************************/
+#define USART_INTERRUPT_RX_COMPLETE _BV (RXCIE1)
+#define USART_INTERRUPT_TX_COMPLETE _BV (TXCIE1)
+#define USART_INTERRUPT_DATA_REG_EMPTY _BV (UDRIE1)
+
+/******************************************************************************/
+/***   Receiver / transmitter                                                 */
+/******************************************************************************/
+#define USART_RECEIVER_ENABLE _BV (RXEN1)
+#define USART_TRANSMITTER_ENABLE _BV (TXEN1)
+
+/******************************************************************************/
+/***   Mode select                                                            */
+/******************************************************************************/
+#define USART_MODE_ASYNC 0x00
+#define USART_MODE_SYNC _BV (UMSEL00)
+
+/******************************************************************************/
+/***   Parity                                                                 */
+/******************************************************************************/
+#define USART_PARITY_NONE 0x00
+#define USART_PARITY_EVEN _BV (UPM01)
+#define USART_PARITY_ODD  _BV (UPM01) | _BV (UPM00)
+
+/******************************************************************************/
+/***   Stop bits                                                              */
+/******************************************************************************/
+#define USART_STOP_BITS_1 0x00
+#define USART_STOP_BITS_2 _BV (USBS)
+
+/******************************************************************************/
+/***   Character size                                                         */
+/******************************************************************************/
+#define USART_DATA_BITS_5 0x00
+#define USART_DATA_BITS_6 _BV (UCSZ10)
+#define USART_DATA_BITS_7 _BV (UCSZ11)
+#define USART_DATA_BITS_8 _BV (UCSZ11) | _BV (UCSZ10)
+// #define USART_DATA_BITS_9 (needs also UCSZ2 bit in UCSRnB)
+
+/******************************************************************************/
+/***   Clock polarity                                                         */
+/******************************************************************************/
+#define USART_RISING_XCKN_EDGE 0x00
+#define USART_FALLING_XCKN_EDGE _BV (UCPOL0)
 
 
 #endif /* #ifndef __RS232_AT90USB1287__ */
diff --git a/cpu/avr/dev/usb/usb_specific_request.c b/cpu/avr/dev/usb/usb_specific_request.c
index d2c9873a1..912e854df 100644
--- a/cpu/avr/dev/usb/usb_specific_request.c
+++ b/cpu/avr/dev/usb/usb_specific_request.c
@@ -109,7 +109,7 @@ Bool usb_user_read_request(U8 type, U8 request)
 				return get_encapsulated_command();
       			break;
 
-
+#if USB_CONF_STORAGE
         case MASS_STORAGE_RESET:
          		Usb_ack_receive_setup();
          		Usb_send_control_in();
@@ -124,8 +124,9 @@ Bool usb_user_read_request(U8 type, U8 request)
          		ms_multiple_drive = 1;
          		return TRUE;
          		break;
+#endif /* USB_CONF_STORAGE */
 
-	
+#if USB_CONF_CDC	
 	/* We don't have a real serial port - so these aren't applicable. We
 	   advertise that we support nothing, so shouldn't get them anyway */
 		case GET_LINE_CODING:
@@ -142,6 +143,7 @@ Bool usb_user_read_request(U8 type, U8 request)
 				cdc_set_control_line_state();
       			return TRUE;
       			break;
+#endif /* USB_CONF_CDC */
      	default:
 				break;
 
@@ -315,7 +317,7 @@ void usb_user_endpoint_init(U8 conf_nb)
 
 }
 
-
+#if USB_CONF_CDC
 /******************** Virtual Serial Port ************************/
 
 extern S_line_coding   line_coding;
@@ -377,4 +379,4 @@ void cdc_set_control_line_state (void)
 	Usb_send_control_in();
   	while(!(Is_usb_read_control_enabled()));
 }
-
+#endif /* USB_CONF_CDC */