Update USB demo.
authorgkostka <kostka.grzegorz@gmail.com>
Sun, 12 Jan 2014 16:25:13 +0000 (16:25 +0000)
committergkostka <kostka.grzegorz@gmail.com>
Sun, 12 Jan 2014 16:25:13 +0000 (16:25 +0000)
demos/stm32f429_disco/hw_init.c
demos/stm32f429_disco/hw_init.h
demos/stm32f429_disco/main.c
demos/stm32f429_disco/stm/lcd_utils/lcd_log_conf.h
demos/stm32f429_disco/stm/usb_user/usbh_usr.c
demos/stm32f429_disco/usb_msc_lwext4.c [new file with mode: 0644]
demos/stm32f429_disco/usb_msc_lwext4.h [new file with mode: 0644]

index bbf0a3d4c93c24e60d7b084a9d2edf8e98958300..9f0ff97c5b7b2401e335423189c4e4cadc601294 100644 (file)
 USB_OTG_CORE_HANDLE     USB_OTG_Core;\r
 USBH_HOST               USB_Host;\r
 \r
+static volatile uint32_t _systick_;\r
+\r
+void SysTick_Handler(void)\r
+{\r
+    _systick_++;\r
+}\r
+\r
 void hw_init(void)\r
 {\r
     pll_init();\r
@@ -39,6 +46,8 @@ void hw_init(void)
     STM_EVAL_LEDInit(LED3);\r
     STM_EVAL_LEDInit(LED4);\r
 \r
+    SysTick_Config(CFG_CCLK_FREQ / 1000);\r
+\r
     /*Init USB Host */\r
     USBH_Init(&USB_OTG_Core,\r
         USB_OTG_HS_CORE_ID,\r
@@ -66,6 +75,11 @@ bool hw_usb_connected(void)
     return HCD_IsDeviceConnected(&USB_OTG_Core);\r
 }\r
 \r
+bool hw_usb_enum_done(void)\r
+{\r
+    return USB_Host_Application_Ready;\r
+}\r
+\r
 void hw_led_red(bool on)\r
 {\r
     on ? STM_EVAL_LEDOn(LED4) : STM_EVAL_LEDOff(LED4);\r
@@ -75,3 +89,8 @@ void hw_led_green(bool on)
 {\r
     on ? STM_EVAL_LEDOn(LED3) : STM_EVAL_LEDOff(LED3);\r
 }\r
+\r
+uint32_t hw_get_ms(void)\r
+{\r
+    return _systick_;\r
+}\r
index 0664d98082da32ebcb30fcee0b9c59551e1ef101..6b7890c7979ad75869952bf9f5c623bc31ad6e26 100644 (file)
 \r
 #include <config.h>\r
 #include <stdbool.h>\r
+#include <stdint.h>\r
 \r
 void hw_init(void);\r
 void hw_usb_process(void);\r
 bool hw_usb_connected(void);\r
+bool hw_usb_enum_done(void);\r
 \r
 void hw_led_red(bool on);\r
 void hw_led_green(bool on);\r
-\r
+uint32_t hw_get_ms(void);\r
 \r
 #endif /* HW_INIT_H_ */\r
index 7075dc634a2669350c3e492d35e956f97c4d8c0a..d2f1b0752e80098d15ccab82548df277a65e93e3 100644 (file)
-/**\r
- * @file    main.c\r
- * @version 0.01\r
- * @date    Oct 2, 2012\r
- * @author  Grzegorz Kostka, kostka.grzegorz@gmail.com\r
- * @brief   ...\r
+/*\r
+ * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)\r
+ * All rights reserved.\r
  *\r
- * @note\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
  *\r
- * @addtogroup group\r
- * @{\r
- *      @addtogroup subgroup\r
- *      @{\r
- **********************************************************/\r
+ * - Redistributions of source code must retain the above copyright\r
+ *   notice, this list of conditions and the following disclaimer.\r
+ * - Redistributions in binary form must reproduce the above copyright\r
+ *   notice, this list of conditions and the following disclaimer in the\r
+ *   documentation and/or other materials provided with the distribution.\r
+ * - The name of the author may not be used to endorse or promote products\r
+ *   derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
 \r
 #include <config.h>\r
 #include <hw_init.h>\r
 \r
 #include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <unistd.h>\r
+#include <getopt.h>\r
+#include <stdbool.h>\r
+#include <time.h>\r
+#include <unistd.h>\r
+\r
+#include <usb_msc_lwext4.h>\r
+#include <ext4.h>\r
+\r
+/**@brief   Input stream name.*/\r
+char input_name[128] = "ext2";\r
+\r
+/**@brief   Read-write size*/\r
+#define READ_WRITE_SZIZE 1024 * 8\r
+\r
+/**@brief   Read-write size*/\r
+static int rw_szie  = READ_WRITE_SZIZE;\r
+\r
+/**@brief   Read-write size*/\r
+static int rw_count = 1000;\r
+\r
+/**@brief   Directory test count*/\r
+static int dir_cnt  = 10;\r
+\r
+/**@brief   Static or dynamic cache mode*/\r
+static bool cache_mode = false;\r
+\r
+/**@brief   Cleanup after test.*/\r
+static bool cleanup_flag = false;\r
+\r
+/**@brief   Block device stats.*/\r
+static bool bstat = false;\r
+\r
+/**@brief   Superblock stats.*/\r
+static bool sbstat = false;\r
+\r
+/**@brief   File write buffer*/\r
+static uint8_t wr_buff[READ_WRITE_SZIZE];\r
+\r
+/**@brief   File read buffer.*/\r
+static uint8_t rd_buff[READ_WRITE_SZIZE];\r
+\r
+/**@brief   Block device handle.*/\r
+static struct ext4_blockdev *bd;\r
+\r
+/**@brief   Block cache handle.*/\r
+static struct ext4_bcache   *bc;\r
+\r
+static char* entry_to_str(uint8_t type)\r
+{\r
+    switch(type){\r
+    case EXT4_DIRENTRY_UNKNOWN:\r
+        return "[UNK] ";\r
+    case EXT4_DIRENTRY_REG_FILE:\r
+        return "[FIL] ";\r
+    case EXT4_DIRENTRY_DIR:\r
+        return "[DIR] ";\r
+    case EXT4_DIRENTRY_CHRDEV:\r
+        return "[CHA] ";\r
+    case EXT4_DIRENTRY_BLKDEV:\r
+        return "[BLK] ";\r
+    case EXT4_DIRENTRY_FIFO:\r
+        return "[FIF] ";\r
+    case EXT4_DIRENTRY_SOCK:\r
+        return "[SOC] ";\r
+    case EXT4_DIRENTRY_SYMLINK:\r
+        return "[SYM] ";\r
+    default:\r
+        break;\r
+    }\r
+    return "[???]";\r
+}\r
+\r
+static void dir_ls(const char *path)\r
+{\r
+    int j = 0;\r
+    char sss[255];\r
+    ext4_dir d;\r
+    ext4_direntry *de;\r
+\r
+    printf("********************\n");\r
+\r
+    ext4_dir_open(&d, path);\r
+    de = ext4_dir_entry_get(&d, j++);\r
+    printf("ls %s\n", path);\r
+\r
+    while(de){\r
+        memcpy(sss, de->name, de->name_length);\r
+        sss[de->name_length] = 0;\r
+        printf("%s", entry_to_str(de->inode_type));\r
+        printf("%s", sss);\r
+        printf("\n");\r
+        de = ext4_dir_entry_get(&d, j++);\r
+    }\r
+    printf("********************\n");\r
+    ext4_dir_close(&d);\r
+}\r
+\r
+static void mp_stats(void)\r
+{\r
+    struct ext4_mount_stats stats;\r
+    ext4_mount_point_stats("/mp/", &stats);\r
+\r
+    printf("********************\n");\r
+    printf("ext4_mount_point_stats\n");\r
+    printf("inodes_count = %u\n", stats.inodes_count);\r
+    printf("free_inodes_count = %u\n", stats.free_inodes_count);\r
+    printf("blocks_count = %u\n", (uint32_t)stats.blocks_count);\r
+    printf("free_blocks_count = %u\n", (uint32_t)stats.free_blocks_count);\r
+    printf("block_size = %u\n", stats.block_size);\r
+    printf("block_group_count = %u\n", stats.block_group_count);\r
+    printf("blocks_per_group= %u\n", stats.blocks_per_group);\r
+    printf("inodes_per_group = %u\n", stats.inodes_per_group);\r
+    printf("volume_name = %s\n", stats.volume_name);\r
+\r
+    printf("********************\n");\r
+\r
+}\r
+\r
+static void block_stats(void)\r
+{\r
+    uint32_t i;\r
+\r
+    printf("********************\n");\r
+    printf("ext4 blockdev stats\n");\r
+    printf("bdev->bread_ctr = %u\n", bd->bread_ctr);\r
+    printf("bdev->bwrite_ctr = %u\n", bd->bwrite_ctr);\r
+\r
+\r
+    printf("bcache->ref_blocks = %u\n", bc->ref_blocks);\r
+    printf("bcache->max_ref_blocks = %u\n", bc->max_ref_blocks);\r
+    printf("bcache->lru_ctr = %u\n", bc->lru_ctr);\r
+\r
+    printf("\n");\r
+    for (i = 0; i < bc->cnt; ++i) {\r
+        printf("bcache->refctr[%d]= %u\n", i, bc->refctr[i]);\r
+    }\r
+\r
+    printf("\n");\r
+    for (i = 0; i < bc->cnt; ++i) {\r
+        printf("bcache->lru_id[%d] = %u\n", i, bc->lru_id[i]);\r
+    }\r
+\r
+    printf("\n");\r
+    for (i = 0; i < bc->cnt; ++i) {\r
+        printf("bcache->free_delay[%d] = %d\n", i, bc->free_delay[i]);\r
+    }\r
+\r
+    printf("\n");\r
+    for (i = 0; i < bc->cnt; ++i) {\r
+        printf("bcache->lba[%d] = %u\n", i, (uint32_t)bc->lba[i]);\r
+    }\r
+\r
+    printf("********************\n");\r
+}\r
+\r
+static clock_t get_ms(void)\r
+{\r
+    return hw_get_ms();\r
+}\r
+\r
+static bool dir_test(int len)\r
+{\r
+    ext4_file f;\r
+    int       r;\r
+    int       i;\r
+    char path[64];\r
+    clock_t diff;\r
+    clock_t stop;\r
+    clock_t start;\r
+    start = get_ms();\r
+\r
+    printf("Directory create: /mp/dir1\n");\r
+    r = ext4_dir_mk("/mp/dir1");\r
+    if(r != EOK){\r
+        printf("Unable to create directory: /mp/dir1\n");\r
+        return false;\r
+    }\r
+\r
+\r
+    printf("Add files to: /mp/dir1\n");\r
+    for (i = 0; i < len; ++i) {\r
+        sprintf(path, "/mp/dir1/f%d", i);\r
+        r = ext4_fopen(&f, path, "wb");\r
+        if(r != EOK){\r
+            printf("Unable to create file in directory: /mp/dir1\n");\r
+            return false;\r
+        }\r
+    }\r
+\r
+    stop =  get_ms();\r
+    diff = stop - start;\r
+    dir_ls("/mp/dir1");\r
+    printf("dir_test time: %d ms\n", (int)diff);\r
+    return true;\r
+}\r
+\r
+\r
+static bool file_test(void)\r
+{\r
+    int r;\r
+    uint32_t  size;\r
+    ext4_file f;\r
+    int i;\r
+    clock_t start;\r
+    clock_t stop;\r
+    clock_t diff;\r
+    uint32_t kbps;\r
+    uint64_t size_bytes;\r
+    /*Add hello world file.*/\r
+    r = ext4_fopen(&f, "/mp/hello.txt", "wb");\r
+    r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);\r
+    r = ext4_fclose(&f);\r
+\r
+\r
+    printf("ext4_fopen: test1\n");\r
+\r
+    start = get_ms();\r
+    r = ext4_fopen(&f, "/mp/test1", "wb");\r
+    if(r != EOK){\r
+        printf("ext4_fopen ERROR = %d\n", r);\r
+        return false;\r
+    }\r
+\r
+    printf("ext4_write: %d * %d ...\n" , rw_szie, rw_count);\r
+    for (i = 0; i < rw_count; ++i) {\r
+\r
+        memset(wr_buff, i % 10 + '0', rw_szie);\r
+\r
+        r = ext4_fwrite(&f, wr_buff, rw_szie, &size);\r
+\r
+        if((r != EOK) || (size != rw_szie))\r
+            break;\r
+\r
+        if(!(i % (rw_count >> 3))){\r
+            printf("*");\r
+            fflush(stdout);\r
+        }\r
+    }\r
+\r
+    if(i != rw_count){\r
+        printf("ERROR: rw_count = %d\n", i);\r
+        return false;\r
+    }\r
+\r
+    printf(" OK\n");\r
+    stop = get_ms();\r
+    diff = stop - start;\r
+    size_bytes = rw_szie * rw_count;\r
+    size_bytes = (size_bytes * 1000) / 1024;\r
+    kbps = (size_bytes) / (diff + 1);\r
+    printf("file_test write time: %d ms\n", (int)diff);\r
+    printf("file_test write speed: %d KB/s\n", (int)kbps);\r
+    r = ext4_fclose(&f);\r
+    printf("ext4_fopen: test1\n");\r
+\r
+\r
+    start = get_ms();\r
+    r = ext4_fopen(&f, "/mp/test1", "r+");\r
+    if(r != EOK){\r
+        printf("ext4_fopen ERROR = %d\n", r);\r
+        return false;\r
+    }\r
+\r
+    printf("ext4_read: %d * %d ...\n" , rw_szie, rw_count);\r
+\r
+    for (i = 0; i < rw_count; ++i) {\r
+        memset(wr_buff, i % 10 + '0', rw_szie);\r
+        r = ext4_fread(&f, rd_buff, rw_szie, &size);\r
+\r
+        if((r != EOK) || (size != rw_szie))\r
+            break;\r
+\r
+        if(memcmp(rd_buff, wr_buff, rw_szie))\r
+            break;\r
+\r
+\r
+        if(!(i % (rw_count >> 3)))\r
+            printf("*");\r
+    }\r
+    if(i != rw_count){\r
+        printf("ERROR: rw_count = %d\n", i);\r
+        return false;\r
+    }\r
+    printf(" OK\n");\r
+    stop = get_ms();\r
+    diff = stop - start;\r
+    size_bytes = rw_szie * rw_count;\r
+    size_bytes = (size_bytes * 1000) / 1024;\r
+    kbps = (size_bytes) / (diff + 1);\r
+    printf("file_test read time: %d ms\n", (int)diff);\r
+    printf("file_test read speed: %d KB/s\n", kbps);\r
+    r = ext4_fclose(&f);\r
+\r
+    return true;\r
+\r
+}\r
+static void cleanup(void)\r
+{\r
+    clock_t start;\r
+    clock_t stop;\r
+    clock_t diff;\r
+\r
+    ext4_fremove("/mp/hello.txt");\r
+\r
+    printf("cleanup: remove /mp/test1\n");\r
+    start = get_ms();\r
+    ext4_fremove("/mp/test1");\r
+    stop = get_ms();\r
+    diff = stop - start;\r
+    printf("cleanup: time: %d ms\n", (int)diff);\r
+\r
+\r
+    printf("cleanup: remove /mp/dir1\n");\r
+    start =get_ms();\r
+    ext4_dir_rm("/mp/dir1");\r
+    stop = get_ms();\r
+    diff = stop - start;\r
+    printf("cleanup: time: %d ms\n", (int)diff);\r
+}\r
+\r
+static bool open_filedev(void)\r
+{\r
+\r
+    bd = ext4_usb_msc_get();\r
+    bc = ext4_usb_msc_cache_get();\r
+    if(!bd || !bc){\r
+        printf("Block device ERROR\n");\r
+        return false;\r
+    }\r
+    return true;\r
+}\r
+\r
+static bool mount(void)\r
+{\r
+    int r;\r
+\r
+    if(!open_filedev())\r
+        return false;\r
+\r
+    ext4_dmask_set(EXT4_DEBUG_ALL);\r
+\r
+    r = ext4_device_register(bd, cache_mode ? 0 : bc, "ext4_filesim");\r
+    if(r != EOK){\r
+        printf("ext4_device_register ERROR = %d\n", r);\r
+        return false;\r
+    }\r
+\r
+    r = ext4_mount("ext4_filesim", "/mp/");\r
+    if(r != EOK){\r
+        printf("ext4_mount ERROR = %d\n", r);\r
+        return false;\r
+    }\r
+\r
+    return true;\r
+}\r
+\r
+static bool umount(void)\r
+{\r
+    int r = ext4_umount("/mp/");\r
+    if(r != EOK){\r
+        printf("ext4_umount: FAIL %d", r);\r
+        return false;\r
+    }\r
+    return true;\r
+}\r
+\r
 \r
 int main(void)\r
 {\r
-    volatile int count;\r
     hw_init();\r
 \r
+    setbuf(stdout, 0);\r
     printf("Connect USB drive...\n");\r
 \r
     while(!hw_usb_connected())\r
         hw_usb_process();\r
     printf("USB drive connected\n");\r
 \r
+    while(!hw_usb_enum_done())\r
+        hw_usb_process();\r
+    printf("USB drive enum done\n");\r
+\r
     hw_led_red(1);\r
 \r
-    int i = 0;\r
+    printf("Test conditions:\n");\r
+    printf("Imput name: %s\n", input_name);\r
+    printf("RW size: %d\n",  rw_szie);\r
+    printf("RW count: %d\n", rw_count);\r
+    printf("Cache mode: %s\n", cache_mode ? "dynamic" : "static");\r
+\r
+    if(!mount())\r
+        return EXIT_FAILURE;\r
+\r
+\r
+    cleanup();\r
+\r
+    if(sbstat)\r
+        mp_stats();\r
+\r
+    dir_ls("/mp/");\r
+    if(!dir_test(dir_cnt))\r
+        return EXIT_FAILURE;\r
+\r
+    if(!file_test())\r
+        return EXIT_FAILURE;\r
+\r
+    dir_ls("/mp/");\r
+\r
+    if(sbstat)\r
+        mp_stats();\r
+\r
+    if(cleanup_flag)\r
+        cleanup();\r
+\r
+    if(bstat)\r
+        block_stats();\r
+\r
+    if(!umount())\r
+        return EXIT_FAILURE;\r
+\r
+    printf("Test finish: OK\n");\r
+    printf("Press RESET to restart\n");\r
     while (1)\r
     {\r
+        volatile int count;\r
         for (count = 0; count < 1000000; count++);\r
         hw_led_green(1);\r
         for (count = 0; count < 1000000; count++);\r
         hw_led_green(0);\r
-        printf("%d\n", i++);\r
+\r
     }\r
 }\r
 \r
index 07d8d8a64fbbceaf660d5b03b854f3ef39b70c5a..b0b9ff7b2954d61b2a4229621d9cee5353cdf4e6 100644 (file)
@@ -55,7 +55,7 @@
 #define LCD_SCROLL_ENABLED\r
 \r
 /* Define the LCD default text color */\r
-#define LCD_LOG_DEFAULT_COLOR    LCD_COLOR_WHITE\r
+#define LCD_LOG_DEFAULT_COLOR    LCD_COLOR_GREEN\r
 \r
 /* Define the display window settings */\r
 #define YWINDOW_MIN              3\r
@@ -65,7 +65,7 @@
 /* Define the cache depth */\r
 #define CACHE_SIZE               50\r
 \r
-#define DEFAULT_FONT            Font8x12\r
+#define DEFAULT_FONT             Font8x12\r
 \r
 /** @defgroup LCD_LOG_CONF_Exported_TypesDefinitions\r
   * @{\r
index 59d5e3ae9c6bb203c9b7d2adff05bfc8a85880e5..8f88f0648d24252e3753abdc178daf358c10455b 100644 (file)
@@ -1,29 +1,29 @@
 /**\r
 ******************************************************************************\r
 * @file    usbh_usr.c\r
 * @author  MCD Application Team\r
 * @version V1.0.1\r
 * @date    11-November-2013\r
 * @brief   USB Host User Functions\r
 ******************************************************************************\r
 * @attention\r
 *\r
 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>\r
 *\r
 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");\r
 * You may not use this file except in compliance with the License.\r
 * You may obtain a copy of the License at:\r
 *\r
 *        http://www.st.com/software_license_agreement_liberty_v2\r
 *\r
-  * Unless required by applicable law or agreed to in writing, software \r
-  * distributed under the License is distributed on an "AS IS" BASIS, \r
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
 * See the License for the specific language governing permissions and\r
 * limitations under the License.\r
 *\r
 ******************************************************************************\r
 */\r
+ ******************************************************************************\r
+ * @file    usbh_usr.c\r
+ * @author  MCD Application Team\r
+ * @version V1.0.1\r
+ * @date    11-November-2013\r
+ * @brief   USB Host User Functions\r
+ ******************************************************************************\r
+ * @attention\r
+ *\r
+ * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>\r
+ *\r
+ * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");\r
+ * You may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at:\r
+ *\r
+ *        http://www.st.com/software_license_agreement_liberty_v2\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ ******************************************************************************\r
+ */\r
 \r
 /* Includes ------------------------------------------------------------------*/\r
 #include "usbh_usr.h"\r
@@ -45,26 +45,26 @@ extern USBH_HOST                      USB_Host;
 /*  The purpose of this register is to speed up the execution */\r
 \r
 USBH_Usr_cb_TypeDef USBH_USR_cb =\r
-  {\r
-    USBH_USR_Init,\r
-    USBH_USR_DeInit,\r
-    USBH_USR_DeviceAttached,\r
-    USBH_USR_ResetDevice,\r
-    USBH_USR_DeviceDisconnected,\r
-    USBH_USR_OverCurrentDetected,\r
-    USBH_USR_DeviceSpeedDetected,\r
-    USBH_USR_Device_DescAvailable,\r
-    USBH_USR_DeviceAddressAssigned,\r
-    USBH_USR_Configuration_DescAvailable,\r
-    USBH_USR_Manufacturer_String,\r
-    USBH_USR_Product_String,\r
-    USBH_USR_SerialNum_String,\r
-    USBH_USR_EnumerationDone,\r
-    USBH_USR_UserInput,\r
-    USBH_USR_MSC_Application,\r
-    USBH_USR_DeviceNotSupported,\r
-    USBH_USR_UnrecoveredError\r
-  };\r
+{\r
+        USBH_USR_Init,\r
+        USBH_USR_DeInit,\r
+        USBH_USR_DeviceAttached,\r
+        USBH_USR_ResetDevice,\r
+        USBH_USR_DeviceDisconnected,\r
+        USBH_USR_OverCurrentDetected,\r
+        USBH_USR_DeviceSpeedDetected,\r
+        USBH_USR_Device_DescAvailable,\r
+        USBH_USR_DeviceAddressAssigned,\r
+        USBH_USR_Configuration_DescAvailable,\r
+        USBH_USR_Manufacturer_String,\r
+        USBH_USR_Product_String,\r
+        USBH_USR_SerialNum_String,\r
+        USBH_USR_EnumerationDone,\r
+        USBH_USR_UserInput,\r
+        USBH_USR_MSC_Application,\r
+        USBH_USR_DeviceNotSupported,\r
+        USBH_USR_UnrecoveredError\r
+};\r
 \r
 \r
 uint32_t __IO USB_Host_Application_Ready;\r
@@ -92,88 +92,88 @@ const uint8_t MSG_UNREC_ERROR[]     = "[USB] UNRECOVERED ERROR STATE";
 /* Private functions ---------------------------------------------------------*/\r
 \r
 /**\r
 * @brief  Add the host lib initialization message to the console\r
 * @param  None\r
 * @retval None\r
 */\r
+ * @brief  Add the host lib initialization message to the console\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_Init(void)\r
 {\r
-  USB_Host_Application_Ready = 0;\r
+    USB_Host_Application_Ready = 0;\r
 }\r
 \r
 /**\r
 * @brief  Add the device attachement message to the console\r
 * @param  None\r
 * @retval None\r
 */\r
+ * @brief  Add the device attachement message to the console\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_DeviceAttached(void)\r
 {\r
 }\r
 \r
 \r
 /**\r
 * @brief  Add the unrecovered error message to the console\r
 * @param  None\r
 * @retval None\r
 */\r
+ * @brief  Add the unrecovered error message to the console\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_UnrecoveredError (void)\r
 {\r
 }\r
 \r
 \r
 /**\r
-  * @brief Add the device disconnection message to the console and free \r
 *        USB associated resources\r
 * @param  None\r
 * @retval None\r
 */\r
+ * @brief Add the device disconnection message to the console and free\r
+ *        USB associated resources\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_DeviceDisconnected (void)\r
 {\r
-  USB_Host_Application_Ready = 0;\r
-//  f_mount(0, NULL);\r
+    USB_Host_Application_Ready = 0;\r
+    //  f_mount(0, NULL);\r
 }\r
 /**\r
 * @brief  callback of the device reset event\r
 * @param  None\r
 * @retval None\r
 */\r
+ * @brief  callback of the device reset event\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_ResetDevice(void)\r
 {\r
 }\r
 \r
 \r
 /**\r
 * @brief  Add the device speed message to the console\r
 * @param  Device speed\r
 * @retval None\r
 */\r
+ * @brief  Add the device speed message to the console\r
+ * @param  Device speed\r
+ * @retval None\r
+ */\r
 void USBH_USR_DeviceSpeedDetected(uint8_t DeviceSpeed)\r
 {\r
 }\r
 \r
 /**\r
 * @brief  Add the USB device vendor and MFC Ids to the console\r
 * @param  device descriptor\r
 * @retval None\r
 */\r
+ * @brief  Add the USB device vendor and MFC Ids to the console\r
+ * @param  device descriptor\r
+ * @retval None\r
+ */\r
 void USBH_USR_Device_DescAvailable(void *DeviceDesc)\r
 {\r
 }\r
 \r
 /**\r
 * @brief  Device addressed event callbacak\r
 * @param  None\r
 * @retval None\r
 */\r
+ * @brief  Device addressed event callbacak\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_DeviceAddressAssigned(void)\r
 {\r
 }\r
 \r
 /**\r
 * @brief  Add the device class description to the console\r
 * @param  Configuration descriptor\r
 * @retval None\r
 */\r
+ * @brief  Add the device class description to the console\r
+ * @param  Configuration descriptor\r
+ * @retval None\r
+ */\r
 void USBH_USR_Configuration_DescAvailable(USBH_CfgDesc_TypeDef * cfgDesc,\r
     USBH_InterfaceDesc_TypeDef *itfDesc,\r
     USBH_EpDesc_TypeDef *epDesc)\r
@@ -181,28 +181,28 @@ void USBH_USR_Configuration_DescAvailable(USBH_CfgDesc_TypeDef * cfgDesc,
 }\r
 \r
 /**\r
 * @brief  Add the MFC String to the console\r
 * @param  Manufacturer String\r
 * @retval None\r
 */\r
+ * @brief  Add the MFC String to the console\r
+ * @param  Manufacturer String\r
+ * @retval None\r
+ */\r
 void USBH_USR_Manufacturer_String(void *ManufacturerString)\r
 {\r
 }\r
 \r
 /**\r
 * @brief  Add the Product String to the console\r
 * @param  Product String\r
 * @retval None\r
 */\r
+ * @brief  Add the Product String to the console\r
+ * @param  Product String\r
+ * @retval None\r
+ */\r
 void USBH_USR_Product_String(void *ProductString)\r
 {\r
 }\r
 \r
 /**\r
 * @brief  Add the Srial Number String to the console\r
 * @param  SerialNum_String\r
 * @retval None\r
 */\r
+ * @brief  Add the Srial Number String to the console\r
+ * @param  SerialNum_String\r
+ * @retval None\r
+ */\r
 void USBH_USR_SerialNum_String(void *SerialNumString)\r
 {\r
 }\r
@@ -210,89 +210,80 @@ void USBH_USR_SerialNum_String(void *SerialNumString)
 \r
 \r
 /**\r
 * @brief  Enumeration complete event callback\r
 * @param  None\r
 * @retval None\r
 */\r
+ * @brief  Enumeration complete event callback\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_EnumerationDone(void)\r
 {\r
 }\r
 \r
 /**\r
 * @brief  Device is not supported callback\r
 * @param  None\r
 * @retval None\r
 */\r
+ * @brief  Device is not supported callback\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_DeviceNotSupported(void)\r
 {\r
 }\r
 \r
 /**\r
 * @brief  User Action for application state entry callback\r
 * @param  None\r
 * @retval USBH_USR_Status : User response for key button\r
 */\r
+ * @brief  User Action for application state entry callback\r
+ * @param  None\r
+ * @retval USBH_USR_Status : User response for key button\r
+ */\r
 USBH_USR_Status USBH_USR_UserInput(void)\r
 {\r
-  return USBH_USR_RESP_OK;\r
+    return USBH_USR_RESP_OK;\r
 }\r
 \r
 /**\r
 * @brief  Over Current Detected on VBUS\r
 * @param  None\r
 * @retval Staus\r
 */\r
+ * @brief  Over Current Detected on VBUS\r
+ * @param  None\r
+ * @retval Staus\r
+ */\r
 void USBH_USR_OverCurrentDetected (void)\r
 {\r
 }\r
 \r
 \r
 /**\r
 * @brief  Mass storage application main handler\r
 * @param  None\r
 * @retval Staus\r
 */\r
+ * @brief  Mass storage application main handler\r
+ * @param  None\r
+ * @retval Staus\r
+ */\r
 int USBH_USR_MSC_Application(void)\r
 {\r
-  char temp[40];\r
-\r
-  if(USB_Host_Application_Ready == 0)\r
-  {\r
-    /* Initializes the File System*/\r
-    //if ( f_mount( 0, &USBH_fatfs ) != FR_OK )\r
-    if(1)\r
+    if(USB_Host_Application_Ready == 0)\r
     {\r
-      /* efs initialisation fails*/\r
-      USB_Host_Application_Ready = 1;\r
-      return -1;\r
-    }\r
-    sprintf (temp, "[FS] USB Disk capacity:%d MB", (int)((USBH_MSC_Param.MSCapacity * \\r
-      USBH_MSC_Param.MSPageLength) / 1024 / 1024));\r
 \r
-    USB_Host_Application_Ready = 1;\r
-  }\r
-  return(0);\r
+        uint64_t cap = (uint64_t)USBH_MSC_Param.MSCapacity * USBH_MSC_Param.MSPageLength;\r
+        printf ("USR_MSC size: %d MB\n", (int)(cap / (1024 * 1024)));\r
+\r
+        USB_Host_Application_Ready = 1;\r
+    }\r
+    return(0);\r
 }\r
 /**\r
 * @brief  De-init User state and associated variables\r
 * @param  None\r
 * @retval None\r
 */\r
+ * @brief  De-init User state and associated variables\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_DeInit(void)\r
 {\r
-  USB_Host_Application_Ready = 0;\r
+    USB_Host_Application_Ready = 0;\r
 }\r
 \r
 /**\r
 * @brief  Handle Modules Background processes in the main task\r
 * @param  None\r
 * @retval None\r
-*/\r
+ * @brief  Handle Modules Background processes in the main task\r
+ * @param  None\r
+ * @retval None\r
+ */\r
 void USBH_USR_BackgroundProcess (void)\r
 {\r
-  if((USB_Host_Application_Ready == 0) || (HCD_IsDeviceConnected(&USB_OTG_Core) == 0))\r
-  {\r
-    USBH_Process(&USB_OTG_Core, &USB_Host);   \r
-  }\r
+    if((USB_Host_Application_Ready == 0) || (HCD_IsDeviceConnected(&USB_OTG_Core) == 0))\r
+    {\r
+        USBH_Process(&USB_OTG_Core, &USB_Host);\r
+    }\r
 }\r
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r
diff --git a/demos/stm32f429_disco/usb_msc_lwext4.c b/demos/stm32f429_disco/usb_msc_lwext4.c
new file mode 100644 (file)
index 0000000..181ff63
--- /dev/null
@@ -0,0 +1,161 @@
+\r
+#include <ext4_config.h>\r
+#include <ext4_blockdev.h>\r
+#include <ext4_errno.h>\r
+#include <stdio.h>\r
+#include <stdbool.h>\r
+#include <string.h>\r
+#include <fcntl.h>\r
+\r
+#include <hw_init.h>\r
+#include <usbh_msc_core.h>\r
+#include <usbh_usr.h>\r
+\r
+\r
+/**@brief   Block size.*/\r
+#define USB_MSC_BLOCK_SIZE          512\r
+\r
+extern USB_OTG_CORE_HANDLE          USB_OTG_Core;\r
+extern USBH_HOST                    USB_Host;\r
+\r
+/**@brief   MBR_block ID*/\r
+#define MBR_BLOCK_ID                0\r
+#define MBR_PART_TABLE_OFF          446\r
+\r
+struct part_tab_entry {\r
+    uint8_t  status;\r
+    uint8_t  chs1[3];\r
+    uint8_t  type;\r
+    uint8_t  chs2[3];\r
+    uint32_t first_lba;\r
+    uint32_t sectors;\r
+}__attribute__((packed));\r
+\r
+/**@brief   Partition block offset*/\r
+static uint32_t part_offset;\r
+\r
+/**********************BLOCKDEV INTERFACE**************************************/\r
+static int usb_msc_open(struct ext4_blockdev *bdev);\r
+static int usb_msc_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,\r
+    uint32_t blk_cnt);\r
+static int usb_msc_bwrite(struct ext4_blockdev *bdev, const void *buf,\r
+    uint64_t blk_id, uint32_t blk_cnt);\r
+static int usb_msc_close(struct  ext4_blockdev *bdev);\r
+\r
+\r
+/******************************************************************************/\r
+EXT4_BLOCKDEV_STATIC_INSTANCE(\r
+    _usb_msc,\r
+    USB_MSC_BLOCK_SIZE,\r
+    0,\r
+    usb_msc_open,\r
+    usb_msc_bread,\r
+    usb_msc_bwrite,\r
+    usb_msc_close\r
+);\r
+\r
+/******************************************************************************/\r
+EXT4_BCACHE_STATIC_INSTANCE(_usb_msc_cache, CONFIG_BLOCK_DEV_CACHE_SIZE, 1024);\r
+\r
+/******************************************************************************/\r
+\r
+static int usb_msc_open(struct ext4_blockdev *bdev)\r
+{\r
+    (void)bdev;\r
+\r
+    static uint8_t mbr[512];\r
+    struct part_tab_entry *part0;\r
+    uint8_t status;\r
+\r
+    if(!hw_usb_connected())\r
+        return EIO;\r
+    do\r
+    {\r
+        status = USBH_MSC_Read10(&USB_OTG_Core, mbr, 0, USB_MSC_BLOCK_SIZE);\r
+        USBH_MSC_HandleBOTXfer(&USB_OTG_Core ,&USB_Host);\r
+\r
+        if(!hw_usb_connected())\r
+            return EIO;\r
+\r
+    }\r
+    while(status == USBH_MSC_BUSY );\r
+\r
+    if(status != USBH_MSC_OK)\r
+        return EIO;\r
+\r
+    part0 = (struct part_tab_entry *)(mbr + MBR_PART_TABLE_OFF);\r
+\r
+    part_offset = part0->first_lba;\r
+    _usb_msc.ph_bcnt = USBH_MSC_Param.MSCapacity;\r
+\r
+    return hw_usb_connected() ? EOK : EIO;\r
+}\r
+\r
+static int usb_msc_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,\r
+    uint32_t blk_cnt)\r
+{\r
+    uint8_t status;\r
+\r
+    if(!hw_usb_connected())\r
+        return EIO;\r
+\r
+    do\r
+    {\r
+        status = USBH_MSC_Read10(&USB_OTG_Core, buf, blk_id + part_offset, _usb_msc.ph_bsize * blk_cnt);\r
+        USBH_MSC_HandleBOTXfer(&USB_OTG_Core ,&USB_Host);\r
+\r
+        if(!hw_usb_connected())\r
+            return EIO;\r
+    }\r
+    while(status == USBH_MSC_BUSY );\r
+\r
+    if(status != USBH_MSC_OK)\r
+        return EIO;\r
+\r
+    return EOK;\r
+\r
+}\r
+\r
+static int usb_msc_bwrite(struct ext4_blockdev *bdev, const void *buf,\r
+    uint64_t blk_id, uint32_t blk_cnt)\r
+{\r
+    uint8_t status;\r
+\r
+    if(!hw_usb_connected())\r
+        return EIO;\r
+\r
+    do\r
+    {\r
+        status = USBH_MSC_Write10(&USB_OTG_Core, (void *)buf, blk_id + part_offset, _usb_msc.ph_bsize * blk_cnt);\r
+        USBH_MSC_HandleBOTXfer(&USB_OTG_Core ,&USB_Host);\r
+\r
+        if(!hw_usb_connected())\r
+            return EIO;\r
+    }\r
+    while(status == USBH_MSC_BUSY );\r
+\r
+    if(status != USBH_MSC_OK)\r
+        return EIO;\r
+\r
+    return EOK;\r
+}\r
+\r
+static int usb_msc_close(struct  ext4_blockdev *bdev)\r
+{\r
+    (void)bdev;\r
+    return EOK;\r
+}\r
+\r
+/******************************************************************************/\r
+\r
+struct ext4_bcache*   ext4_usb_msc_cache_get(void)\r
+{\r
+    return &_usb_msc_cache;\r
+}\r
+\r
+\r
+struct ext4_blockdev* ext4_usb_msc_get(void)\r
+{\r
+    return &_usb_msc;\r
+}\r
+\r
diff --git a/demos/stm32f429_disco/usb_msc_lwext4.h b/demos/stm32f429_disco/usb_msc_lwext4.h
new file mode 100644 (file)
index 0000000..d82bbbe
--- /dev/null
@@ -0,0 +1,45 @@
+/*\r
+ * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ *\r
+ * - Redistributions of source code must retain the above copyright\r
+ *   notice, this list of conditions and the following disclaimer.\r
+ * - Redistributions in binary form must reproduce the above copyright\r
+ *   notice, this list of conditions and the following disclaimer in the\r
+ *   documentation and/or other materials provided with the distribution.\r
+ * - The name of the author may not be used to endorse or promote products\r
+ *   derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
+#ifndef USB_MSC_LWEXT4_H_\r
+#define USB_MSC_LWEXT4_H_\r
+\r
+#include <ext4_config.h>\r
+#include <ext4_blockdev.h>\r
+\r
+#include <stdint.h>\r
+#include <stdbool.h>\r
+\r
+/**@brief   USB MSC cache get.*/\r
+struct ext4_bcache*   ext4_usb_msc_cache_get(void);\r
+\r
+/**@brief   USB MSC blockdev get.*/\r
+struct ext4_blockdev* ext4_usb_msc_get(void);\r
+\r
+\r
+\r
+#endif /* USB_MSC_LWEXT4_H_ */\r