MDX-20用の改造

Dependencies:   FatFileSystemCpp

Files at this revision

API Documentation at this revision

Comitter:
suupen
Date:
Sun Dec 18 08:29:59 2016 +0000
Parent:
7:a6b950d25e29
Child:
9:280ae971e7cb
Commit message:
add USB check.disk_usbCheck() and Host_Check()

Changed in this revision

MSCFileSystem.cpp Show annotated file Show diff for this revision Revisions of this file
MSCFileSystem.h Show annotated file Show diff for this revision Revisions of this file
USBHostLite/usbhost_lpc17xx.cpp Show annotated file Show diff for this revision Revisions of this file
USBHostLite/usbhost_lpc17xx.h Show annotated file Show diff for this revision Revisions of this file
usbMemory.cpp Show annotated file Show diff for this revision Revisions of this file
usbMemory.h Show annotated file Show diff for this revision Revisions of this file
--- a/MSCFileSystem.cpp	Mon Dec 12 02:02:11 2016 +0000
+++ b/MSCFileSystem.cpp	Sun Dec 18 08:29:59 2016 +0000
@@ -78,7 +78,7 @@
 
     //print_clock();
     Host_Init();               /* Initialize the  host controller                                    */
-    rc = Host_EnumDev();       /* Enumerate the device connected                                            */
+    rc = Host_EnumDev();       /* Enumerate the device connected    ベースでは、USB memoryがつながるまで待っている (HOST_RhscIntr 1:deviceあり 0:なし)                                        */
     if (rc != OK) {
         fprintf(stderr, "Could not enumerate device: %d\n", rc);
         return rc;
@@ -97,19 +97,25 @@
     return _blkSize == 512 ? OK : 1;
 }
 
-int MSCFileSystem::disk_initialize()
+/** USBの存在確認
+ * @return 1:ない 0:ない 
+ */
+int MSCFileSystem::disk_usbCheck()
 {
-    if(MS_TestUnitReady == OK) {
+    if(Host_Check() != OK){
         return 1;
-    } else {
+        }
         return 0;
+
     }
-    /*
+
+
+int MSCFileSystem::disk_initialize()    // 元からある関数。複
+{
         if ( initialise_msc() != OK )
             return 1;
 
         return 0;
-    */
 }
 
 int MSCFileSystem::disk_write(const char *buffer, int block_number)
--- a/MSCFileSystem.h	Mon Dec 12 02:02:11 2016 +0000
+++ b/MSCFileSystem.h	Sun Dec 18 08:29:59 2016 +0000
@@ -32,6 +32,9 @@
      *  name - The name used to access the filesystem
      */
     MSCFileSystem(const char* name);
+
+     int disk_usbCheck();   //@ss 161218
+
     virtual int disk_initialize();
     virtual int disk_write(const char *buffer, int block_number);
     virtual int disk_read(char *buffer, int block_number);    
--- 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;
--- a/USBHostLite/usbhost_lpc17xx.h	Mon Dec 12 02:02:11 2016 +0000
+++ b/USBHostLite/usbhost_lpc17xx.h	Sun Dec 18 08:29:59 2016 +0000
@@ -199,6 +199,9 @@
 
 extern "C" void USB_IRQHandler(void)  __irq;
 
+USB_INT32S  Host_Check (void);
+
+
 USB_INT32S  Host_EnumDev  (void);
 
 USB_INT32S  Host_ProcessTD(volatile  HCED       *ed,
--- a/usbMemory.cpp	Mon Dec 12 02:02:11 2016 +0000
+++ b/usbMemory.cpp	Sun Dec 18 08:29:59 2016 +0000
@@ -27,9 +27,20 @@
 char buffer[50];
 
 
-void usbMemoryInitialize(void)
+/** usb check
+ * @return 0:OK 1:NG
+ */
+int usbMemoryCheck(void)
 {
-    msc.disk_initialize();
+    return(msc.disk_usbCheck());
+}
+
+/** usb memory initialize
+ * @return 0:OK 1:NG
+ */
+int usbMemoryInitialize(void)
+{
+    return(msc.disk_initialize());
 }
 
 /** File check
@@ -63,41 +74,56 @@
         }
         *fileNumberMax = numMax;
     }
+    closedir(d);
     return ans;
 }
 
-
-int32_t fileSelect(char *fileName, uint32_t *numberLine)
+/** 指示したfileの行数を取得 
+ * @param *fileName : 指示するfile name
+ * @param *numberLine : 取得した行数
+ * @return false:行数取得できず true:処理成功
+ */
+int32_t fileSelect(char *fileName, int32_t *numberLine)
 {
     uint32_t numMax = 0;
     int32_t ans = false;
 
-    DIR *d;
-    struct dirent *p;
+    /*
+      DIR *d;
+      struct dirent *p;
 
-    d = opendir("/" FSNAME);
+      d = opendir("/" FSNAME);
+    */
+    // file check
+    sprintf(buffer,"/msc/%s",fileName);
+    fp = fopen( buffer, "r");
 
-        // file check
-        sprintf(buffer,"/msc/%s",fileName);
-        fp = fopen( buffer, "r");
-        if ( fp != NULL ) {
-            char buf[256];
-            while ( NULL != fgets(buf, sizeof(buf), fp) ) {
-                DEBUG_PRINT("%s", buf);
-                numMax++;
-            }
-            *numberLine = numMax;
-            fclose(fp);
-            ans = true;
+    if ( fp != NULL ) {
+        char buf[256];
+        while ( NULL != fgets(buf, sizeof(buf), fp) ) {
+            DEBUG_PRINT("%s", buf);
+            numMax++;
+        }
+        *numberLine = numMax;
+        ans = true;
 
-        } else {
-            // Don't file open
-            // nothing
-        }
+    } else {
+        // Don't file open
+        // nothing
+    }
+
+    fclose(fp);
     return ans;
 }
 
-uint32_t fileOneLineRead(char *fileName, char *data, uint32_t dataNumber, uint32_t numberLine)
+/** 指定した行のデータを取得 
+ * @param *fileNmae データを取得するfile Name
+ * @param *data : 取得した行データを格納するバッファ
+ * @param dataNumber : *dataのbyte数
+ * @param numberLine : 取得する行数
+ * @return false:失敗 true:成功
+ */
+uint32_t fileOneLineRead(char *fileName, char *data, int32_t dataNumber, int32_t numberLine)
 {
 //    static FILE *fp;
     int32_t ans = false;
@@ -114,7 +140,6 @@
 
         if(NULL != fgets(data, dataNumber, fp)) {
             DEBUG_PRINT("line = %s\r\n", data);
-            fclose(fp);
             ans = true;
         } else {
             // nothing
@@ -124,6 +149,7 @@
         // nothing
     }
 
+    fclose(fp);
     return ans;
 }
 
--- a/usbMemory.h	Mon Dec 12 02:02:11 2016 +0000
+++ b/usbMemory.h	Sun Dec 18 08:29:59 2016 +0000
@@ -5,9 +5,12 @@
 #include "usbMemory.h"
 
 int32_t fileDetect(int32_t fileNumber, char *fileName, int32_t *fileNumberMax);
-int32_t fileSelect(char *fileName, uint32_t *numberLine);
-uint32_t fileOneLineRead(char *fileName, char *data, uint32_t dataNumber, uint32_t numberLine);
-void usbMemoryInitialize(void);
+int32_t fileSelect(char *fileName, int32_t *numberLine);
+uint32_t fileOneLineRead(char *fileName, char *data, int32_t dataNumber, int32_t numberLine);
+
+int usbMemoryCheck(void);
+
+int usbMemoryInitialize(void);