From 32614537238ea442a62a4acc9ba2c9f36615288a Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Wed, 21 Aug 2013 11:17:11 +0900
Subject: example update

---
 example-cdc/sys.c           |  2 +-
 example-cdc/usb-cdc.c       | 60 ++++++++++++++++++---------------------------
 example-cdc/usb_lld.h       |  1 -
 example-cdc/usb_stm32f103.c | 19 +++++---------
 4 files changed, 31 insertions(+), 51 deletions(-)

(limited to 'example-cdc')

diff --git a/example-cdc/sys.c b/example-cdc/sys.c
index 8bb9ae0..36184b7 100644
--- a/example-cdc/sys.c
+++ b/example-cdc/sys.c
@@ -566,7 +566,7 @@ reset (void)
   /* Never reach here. */
   /* Artificial entry to refer FT0, FT1, and FT2.  */
   asm volatile (""
-		: : "r" (&FT0), "r" (&FT1), "r" (&FT2));
+		: : "r" (FT0), "r" (FT1), "r" (FT2));
 }
 
 typedef void (*handler)(void);
diff --git a/example-cdc/usb-cdc.c b/example-cdc/usb-cdc.c
index b44cb89..cd6e2d1 100644
--- a/example-cdc/usb-cdc.c
+++ b/example-cdc/usb-cdc.c
@@ -158,6 +158,7 @@ static const uint8_t vcom_string3[28] = {
 #define NUM_INTERFACES 2
 
 uint32_t bDeviceState = UNCONNECTED; /* USB device status */
+uint8_t connected;
 
 void
 usb_cb_device_reset (void)
@@ -179,11 +180,18 @@ void
 usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, uint16_t value,
 			  uint16_t index, uint16_t len)
 {
-  (void)req;
-  (void)req_no;
-  (void)value;
-  (void)index;
-  (void)len;
+  uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
+
+  if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT)
+      && index == 0 && USB_SETUP_SET (req) && len == 0
+      && req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
+    {
+      /* Open/close the connection.  */
+      chopstx_mutex_lock (&usb_mtx);
+      connected = (value != 0)? 1 : 0;
+      chopstx_cond_signal (&cnd_usb);
+      chopstx_mutex_unlock (&usb_mtx);
+    }
 }
 
 struct line_coding
@@ -192,7 +200,7 @@ struct line_coding
   uint8_t format;
   uint8_t paritytype;
   uint8_t datatype;
-};
+}  __attribute__((packed));
 
 static struct line_coding line_coding = {
   115200, /* baud rate: 115200    */
@@ -201,50 +209,30 @@ static struct line_coding line_coding = {
   0x08    /* bits:      8         */
 };
 
-uint8_t connected;
 
 static int
-vcom_port_data_setup (uint8_t req, uint8_t req_no, uint16_t value)
+vcom_port_data_setup (uint8_t req, uint8_t req_no, uint16_t value, uint16_t len)
 {
+  (void)value;
   if (USB_SETUP_GET (req))
     {
-      if (req_no == USB_CDC_REQ_GET_LINE_CODING)
+      if (req_no == USB_CDC_REQ_GET_LINE_CODING
+	  && len == sizeof (line_coding))
 	{
-	  usb_lld_set_data_to_send (&line_coding, sizeof(line_coding));
+	  usb_lld_set_data_to_send (&line_coding, sizeof (line_coding));
 	  return USB_SUCCESS;
 	}
     }
   else  /* USB_SETUP_SET (req) */
     {
-      if (req_no == USB_CDC_REQ_SET_LINE_CODING)
+      if (req_no == USB_CDC_REQ_SET_LINE_CODING
+	  && len == sizeof (line_coding))
 	{
-	  usb_lld_set_data_to_recv (&line_coding, sizeof(line_coding));
+	  usb_lld_set_data_to_recv (&line_coding, sizeof (line_coding));
 	  return USB_SUCCESS;
 	}
       else if (req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
-	{
-	  uint8_t connected_saved = connected;
-
-	  if (value != 0)
-	    {
-	      if (connected == 0)
-		/* It's Open call */
-		connected++;
-	    }
-	  else
-	    {
-	      if (connected)
-		/* Close call */
-		connected = 0;
-	    }
-
-	  chopstx_mutex_lock (&usb_mtx);
-	  if (connected != connected_saved)
-	    chopstx_cond_signal (&cnd_usb);
-	  chopstx_mutex_unlock (&usb_mtx);
-
-	  return USB_SUCCESS;
-	}
+	return USB_SUCCESS;
     }
 
   return USB_UNSUPPORT;
@@ -259,7 +247,7 @@ usb_cb_setup (uint8_t req, uint8_t req_no,
   (void)len;
   if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT))
     if (index == 0)
-      return vcom_port_data_setup (req, req_no, value);
+      return vcom_port_data_setup (req, req_no, value, len);
 
   return USB_UNSUPPORT;
 }
diff --git a/example-cdc/usb_lld.h b/example-cdc/usb_lld.h
index b98d267..8dc00b9 100644
--- a/example-cdc/usb_lld.h
+++ b/example-cdc/usb_lld.h
@@ -88,7 +88,6 @@ enum DEVICE_STATE
   CONFIGURED
 };
 
-extern uint32_t bDeviceState;
 extern const uint8_t usb_initial_feature;
 
 #define STM32_USB_IRQ_PRIORITY     11
diff --git a/example-cdc/usb_stm32f103.c b/example-cdc/usb_stm32f103.c
index 887d69f..b75dfdb 100644
--- a/example-cdc/usb_stm32f103.c
+++ b/example-cdc/usb_stm32f103.c
@@ -1,16 +1,9 @@
-#ifdef FREE_STANDING
 #include <stdint.h>
 #include <stdlib.h>
+
 #define TRUE  1
 #define FALSE 0
 
-#define NULL  0
-
-#define     __IO    volatile
-#else
-#include "ch.h"
-#include "hal.h"
-#endif
 #include "sys.h"
 #include "usb_lld.h"
 
@@ -90,15 +83,15 @@ static struct DATA_INFO *const data_p = &data_info;
 #define PMA_ADDR  (0x40006000UL) /* USB_IP Packet Memory Area base address   */
 
 /* Control register */
-#define CNTR    ((__IO uint16_t *)(REG_BASE + 0x40))
+#define CNTR    ((volatile uint16_t *)(REG_BASE + 0x40))
 /* Interrupt status register */
-#define ISTR    ((__IO uint16_t *)(REG_BASE + 0x44))
+#define ISTR    ((volatile uint16_t *)(REG_BASE + 0x44))
 /* Frame number register */
-#define FNR     ((__IO uint16_t *)(REG_BASE + 0x48))
+#define FNR     ((volatile uint16_t *)(REG_BASE + 0x48))
 /* Device address register */
-#define DADDR   ((__IO uint16_t *)(REG_BASE + 0x4C))
+#define DADDR   ((volatile uint16_t *)(REG_BASE + 0x4C))
 /* Buffer Table address register */
-#define BTABLE  ((__IO uint16_t *)(REG_BASE + 0x50))
+#define BTABLE  ((volatile uint16_t *)(REG_BASE + 0x50))
 
 #define ISTR_CTR    (0x8000) /* Correct TRansfer (clear-only bit) */
 #define ISTR_DOVR   (0x4000) /* DMA OVeR/underrun (clear-only bit) */
-- 
cgit v1.2.3