MDX-20用の改造

Dependencies:   FatFileSystemCpp

Revision:
8:6e23bf867e38
Parent:
1:ad941ffd64cb
--- a/USBHostLite/usbhost_lpc17xx.cpp	Mon Dec 12 02:02:11 2016 +0000
+++ b/USBHostLite/usbhost_lpc17xx.cpp	Sun Dec 18 08:29:59 2016 +0000
@@ -15,7 +15,7 @@
 *
 **************************************************************************************************************
 */
- 
+
 /*
 **************************************************************************************************************
 *                                            INCLUDE HEADER FILES
@@ -39,8 +39,8 @@
 volatile  HCED        *EDBulkOut;                 /* BulkOut endpoint descriptor structure                  */
 volatile  HCTD        *TDHead;                    /* Head transfer descriptor structure                     */
 volatile  HCTD        *TDTail;                    /* Tail transfer descriptor structure                     */
-volatile  HCCA        *Hcca;                      /* Host Controller Communications Area structure          */ 
-          USB_INT16U  *TDBufNonVol;               /* Identical to TDBuffer just to reduce compiler warnings */
+volatile  HCCA        *Hcca;                      /* Host Controller Communications Area structure          */
+USB_INT16U  *TDBufNonVol;               /* Identical to TDBuffer just to reduce compiler warnings */
 volatile  USB_INT08U  *TDBuffer;                  /* Current Buffer Pointer of transfer descriptor          */
 
 // USB host structures
@@ -117,7 +117,7 @@
 *
 * Arguments  : None
 *
-* Returns    : 
+* Returns    :
 *
 **************************************************************************************************************
 */
@@ -125,7 +125,7 @@
 {
     PRINT_Log("In Host_Init\n");
     NVIC_DisableIRQ(USB_IRQn);                           /* Disable the USB interrupt source           */
-    
+
     // turn on power for USB
     LPC_SC->PCONP       |= (1UL<<31);
     // Enable USB host clock, port selection and AHB clock
@@ -133,7 +133,7 @@
     // Wait for clocks to become available
     while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK)
         ;
-    
+
     // it seems the bits[0:1] mean the following
     // 0: U1=device, U2=host
     // 1: U1=host, U2=host
@@ -142,35 +142,35 @@
     // NB: this register is only available if OTG clock (aka "port select") is enabled!!
     // since we don't care about port 2, set just bit 0 to 1 (U1=host)
     LPC_USB->OTGStCtrl |= 1;
-    
+
     // now that we've configured the ports, we can turn off the portsel clock
     LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN;
-    
+
     // power pins are not connected on mbed, so we can skip them
     /* P1[18] = USB_UP_LED, 01 */
     /* P1[19] = /USB_PPWR,     10 */
     /* P1[22] = USB_PWRD, 10 */
     /* P1[27] = /USB_OVRCR, 10 */
-    /*LPC_PINCON->PINSEL3 &= ~((3<<4) | (3<<6) | (3<<12) | (3<<22));  
+    /*LPC_PINCON->PINSEL3 &= ~((3<<4) | (3<<6) | (3<<12) | (3<<22));
     LPC_PINCON->PINSEL3 |=  ((1<<4)|(2<<6) | (2<<12) | (2<<22));   // 0x00802080
     */
 
     // configure USB D+/D- pins
     /* P0[29] = USB_D+, 01 */
     /* P0[30] = USB_D-, 01 */
-    LPC_PINCON->PINSEL1 &= ~((3<<26) | (3<<28));  
+    LPC_PINCON->PINSEL1 &= ~((3<<26) | (3<<28));
     LPC_PINCON->PINSEL1 |=  ((1<<26)|(1<<28));     // 0x14000000
-        
+
     PRINT_Log("Initializing Host Stack\n");
 
     Hcca       = (volatile  HCCA       *)(HostBuf+0x000);
     TDHead     = (volatile  HCTD       *)(HostBuf+0x100);
     TDTail     = (volatile  HCTD       *)(HostBuf+0x110);
-    EDCtrl     = (volatile  HCED       *)(HostBuf+0x120); 
+    EDCtrl     = (volatile  HCED       *)(HostBuf+0x120);
     EDBulkIn   = (volatile  HCED       *)(HostBuf+0x130);
     EDBulkOut  = (volatile  HCED       *)(HostBuf+0x140);
     TDBuffer   = (volatile  USB_INT08U *)(HostBuf+0x150);
-    
+
     /* Initialize all the TDs, EDs and HCCA to 0  */
     Host_EDInit(EDCtrl);
     Host_EDInit(EDBulkIn);
@@ -178,27 +178,27 @@
     Host_TDInit(TDHead);
     Host_TDInit(TDTail);
     Host_HCCAInit(Hcca);
-    
+
     Host_DelayMS(50);                                   /* Wait 50 ms before apply reset              */
     LPC_USB->HcControl       = 0;                       /* HARDWARE RESET                             */
     LPC_USB->HcControlHeadED = 0;                       /* Initialize Control list head to Zero       */
     LPC_USB->HcBulkHeadED    = 0;                       /* Initialize Bulk list head to Zero          */
-    
-                                                        /* SOFTWARE RESET                             */
+
+    /* SOFTWARE RESET                             */
     LPC_USB->HcCommandStatus = OR_CMD_STATUS_HCR;
     LPC_USB->HcFmInterval    = DEFAULT_FMINTERVAL;      /* Write Fm Interval and Largest Data Packet Counter */
 
-                                                        /* Put HC in operational state                */
+    /* Put HC in operational state                */
     LPC_USB->HcControl  = (LPC_USB->HcControl & (~OR_CONTROL_HCFS)) | OR_CONTROL_HC_OPER;
     LPC_USB->HcRhStatus = OR_RH_STATUS_LPSC;            /* Set Global Power                           */
-    
+
     LPC_USB->HcHCCA = (USB_INT32U)Hcca;
     LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus;                   /* Clear Interrrupt Status                    */
 
 
     LPC_USB->HcInterruptEnable  = OR_INTR_ENABLE_MIE |
-                         OR_INTR_ENABLE_WDH |
-                         OR_INTR_ENABLE_RHSC;
+                                  OR_INTR_ENABLE_WDH |
+                                  OR_INTR_ENABLE_RHSC;
 
     NVIC_SetPriority(USB_IRQn, 0);       /* highest priority */
     /* Enable the USB Interrupt */
@@ -208,7 +208,7 @@
 
 /*
 **************************************************************************************************************
-*                                         INTERRUPT SERVICE ROUTINE
+*                                         INTERRUPT SERVICE ROUTINE // この割り込み関数で、USBデバイスの抜き差しを検出管理する
 *
 * Description: This function services the interrupt caused by host controller
 *
@@ -219,14 +219,13 @@
 **************************************************************************************************************
 */
 
-void USB_IRQHandler (void) __irq
-{
+void USB_IRQHandler (void) __irq {
     USB_INT32U   int_status;
     USB_INT32U   ie_status;
 
     int_status    = LPC_USB->HcInterruptStatus;                          /* Read Interrupt Status                */
     ie_status     = LPC_USB->HcInterruptEnable;                          /* Read Interrupt enable status         */
- 
+
     if (!(int_status & ie_status)) {
         return;
     } else {
@@ -240,8 +239,7 @@
                      * means a remote wakeup event.
                     */
                     HOST_RhscIntr = 1;// JUST SOMETHING FOR A BREAKPOINT
-                }
-                else {
+                } else {
                     /*
                      * When DRWE is off, Connect Status Change
                      * is NOT a remote wakeup event
@@ -252,16 +250,14 @@
                             HOST_WdhIntr = 0;
                             HOST_RhscIntr = 1;
                             gUSBConnected = 1;
-                        }
-                        else
+                        } else
                             PRINT_Log("Spurious status change (connected)?\n");
                     } else {
                         if (gUSBConnected) {
                             LPC_USB->HcInterruptEnable = 0; // why do we get multiple disc. rupts???
                             HOST_RhscIntr = 0;
                             gUSBConnected = 0;
-                        }
-                        else
+                        } else
                             PRINT_Log("Spurious status change (disconnected)?\n");
                     }
                 }
@@ -274,7 +270,7 @@
         if (int_status & OR_INTR_STATUS_WDH) {                  /* Writeback Done Head interrupt        */
             HOST_WdhIntr = 1;
             HOST_TDControlStatus = (TDHead->Control >> 28) & 0xf;
-        }            
+        }
         LPC_USB->HcInterruptStatus = int_status;                         /* Clear interrupt status register      */
     }
     return;
@@ -300,7 +296,7 @@
 USB_INT32S  Host_ProcessTD (volatile  HCED       *ed,
                             volatile  USB_INT32U  token,
                             volatile  USB_INT08U *buffer,
-                                      USB_INT32U  buffer_len)
+                            USB_INT32U  buffer_len)
 {
     volatile  USB_INT32U   td_toggle;
 
@@ -315,10 +311,10 @@
         td_toggle = 0;
     }
     TDHead->Control = (TD_ROUNDING    |
-                      token           |
-                      TD_DELAY_INT(0) |                           
-                      td_toggle       |
-                      TD_CC);
+                       token           |
+                       TD_DELAY_INT(0) |
+                       td_toggle       |
+                       TD_CC);
     TDTail->Control = 0;
     TDHead->CurrBufPtr   = (USB_INT32U) buffer;
     TDTail->CurrBufPtr   = 0;
@@ -339,18 +335,32 @@
         LPC_USB->HcBulkHeadED    = (USB_INT32U)ed;
         LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_BLF;
         LPC_USB->HcControl       = LPC_USB->HcControl       | OR_CONTROL_BLE;
-    }    
+    }
 
     Host_WDHWait();
 
 //    if (!(TDHead->Control & 0xF0000000)) {
     if (!HOST_TDControlStatus) {
         return (OK);
-    } else {      
+    } else {
         return (ERR_TD_FAIL);
     }
 }
 
+/** USBの存在確認
+ * @return 0:OK 1:NG
+ * @note 161218 ss 
+ */
+USB_INT32S  Host_Check (void)
+{
+//    USB_INT32S  rc;
+
+    PRINT_Log("Connect a Mass Storage device\n");
+    if(!HOST_RhscIntr) {        //@SS USB deviceがなければエラーで抜けるようにした
+        return (ERR_TD_FAIL);    //@SS 元は、deviceがつながるまで待っていた
+    }                       //@SS
+    return (OK);
+}
 /*
 **************************************************************************************************************
 *                                       ENUMERATE THE DEVICE
@@ -369,8 +379,11 @@
     USB_INT32S  rc;
 
     PRINT_Log("Connect a Mass Storage device\n");
-    while (!HOST_RhscIntr)
-        __WFI();
+    if(!HOST_RhscIntr) {        //@SS USB deviceがなければエラーで抜けるようにした
+        return (ERR_TD_FAIL);    //@SS 元は、deviceがつながるまで待っていた
+    }                       //@SS
+//@SS    while (!HOST_RhscIntr)
+    __WFI();
     Host_DelayMS(100);                             /* USB 2.0 spec says atleast 50ms delay beore port reset */
     LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRS; // Initiate port reset
     while (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_PRS)
@@ -379,7 +392,7 @@
     Host_DelayMS(200);                                                 /* Wait for 100 MS after port reset  */
 
     EDCtrl->Control = 8 << 16;                                         /* Put max pkt size = 8              */
-                                                                       /* Read first 8 bytes of device desc */
+    /* Read first 8 bytes of device desc */
     rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_DEVICE, 0, TDBuffer, 8);
     if (rc != OK) {
         PRINT_Err(rc);
@@ -393,13 +406,13 @@
     }
     Host_DelayMS(2);
     EDCtrl->Control = (EDCtrl->Control) | 1;                          /* Modify control pipe with address 1 */
-                                                                      /* Get the configuration descriptor   */
+    /* Get the configuration descriptor   */
     rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, 9);
     if (rc != OK) {
         PRINT_Err(rc);
         return (rc);
     }
-                                                                       /* Get the first configuration data  */
+    /* Get the first configuration data  */
     rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, ReadLE16U(&TDBuffer[2]));
     if (rc != OK) {
         PRINT_Err(rc);
@@ -436,13 +449,13 @@
 *
 **************************************************************************************************************
 */
-   
+
 USB_INT32S  Host_CtrlRecv (         USB_INT08U   bm_request_type,
                                     USB_INT08U   b_request,
                                     USB_INT16U   w_value,
                                     USB_INT16U   w_index,
                                     USB_INT16U   w_length,
-                          volatile  USB_INT08U  *buffer)
+                                    volatile  USB_INT08U  *buffer)
 {
     USB_INT32S  rc;
 
@@ -479,7 +492,7 @@
                                      USB_INT16U   w_value,
                                      USB_INT16U   w_index,
                                      USB_INT16U   w_length,
-                           volatile  USB_INT08U  *buffer)
+                                     volatile  USB_INT08U  *buffer)
 {
     USB_INT32S  rc;
 
@@ -519,9 +532,9 @@
                       USB_INT16U   w_length)
 {
     int i;
-    for (i=0;i<w_length;i++)
+    for (i=0; i<w_length; i++)
         TDBuffer[i] = 0;
-    
+
     TDBuffer[0] = bm_request_type;
     TDBuffer[1] = b_request;
     WriteLE16U(&TDBuffer[2], w_value);
@@ -583,7 +596,7 @@
 *
 * Arguments  : Pointer to HCCA
 *
-* Returns    : 
+* Returns    :
 *
 **************************************************************************************************************
 */
@@ -617,10 +630,10 @@
 
 void  Host_WDHWait (void)
 {
-  while (!HOST_WdhIntr)
-      __WFI();
+    while (!HOST_WdhIntr)
+        __WFI();
 
-  HOST_WdhIntr = 0;
+    HOST_WdhIntr = 0;
 }
 
 /*
@@ -644,14 +657,14 @@
     return __REV(val);
 #else
     return val;
-#endif    
+#endif
 }
 
 /*
 **************************************************************************************************************
 *                                        WRITE LE 32U
 *
-* Description: This function is used to write an unsigned integer into a charecter buffer in the platform 
+* Description: This function is used to write an unsigned integer into a charecter buffer in the platform
 *              containing little endian processor.
 *
 * Arguments  : pmem    Pointer to the charecter buffer
@@ -663,7 +676,7 @@
 */
 
 void  WriteLE32U (volatile  USB_INT08U  *pmem,
-                            USB_INT32U   val)
+                  USB_INT32U   val)
 {
 #ifdef __BIG_ENDIAN
     *(USB_INT32U*)pmem = __REV(val);
@@ -693,7 +706,7 @@
     return __REV16(val);
 #else
     return val;
-#endif    
+#endif
 }
 
 /*
@@ -712,7 +725,7 @@
 */
 
 void  WriteLE16U (volatile  USB_INT08U  *pmem,
-                            USB_INT16U   val)
+                  USB_INT16U   val)
 {
 #ifdef __BIG_ENDIAN
     *(USB_INT16U*)pmem = (__REV16(val) & 0xFFFF);
@@ -761,7 +774,7 @@
 */
 
 void  WriteBE32U (volatile  USB_INT08U  *pmem,
-                            USB_INT32U   val)
+                  USB_INT32U   val)
 {
 #ifdef __BIG_ENDIAN
     *(USB_INT32U*)pmem = val;
@@ -791,7 +804,7 @@
     return val;
 #else
     return __REV16(val);
-#endif    
+#endif
 }
 
 /*
@@ -810,7 +823,7 @@
 */
 
 void  WriteBE16U (volatile  USB_INT08U  *pmem,
-                            USB_INT16U   val)
+                  USB_INT16U   val)
 {
 #ifdef __BIG_ENDIAN
     *(USB_INT16U*)pmem = val;