USBH: testhal example: improve support for multiple FAT devices
This commit is contained in:
parent
c0205baf7d
commit
c466db250c
|
@ -4,4 +4,4 @@ FATFSSRC = ${CHIBIOS_CONTRIB}/os/various/fatfs_bindings/fatfs_diskio.c \
|
|||
${CHIBIOS}/ext/fatfs/src/ff.c \
|
||||
${CHIBIOS}/ext/fatfs/src/option/unicode.c
|
||||
|
||||
FATFSINC = ${CHIBIOS}/ext/fatfs/src
|
||||
FATFSINC = ${CHIBIOS}/ext/fatfs/src ${CHIBIOS_CONTRIB}/os/various/fatfs_bindings
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio
|
||||
Copyright (C) 2015..2017 Diego Ismirlian, (dismirlian (at) google's mail)
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef FATFS_DEVICES_H_
|
||||
#define FATFS_DEVICES_H_
|
||||
|
||||
#include "hal.h"
|
||||
|
||||
#if HAL_USE_MMC_SPI && HAL_USE_SDC
|
||||
#error "cannot specify both MMC_SPI and SDC drivers"
|
||||
#endif
|
||||
|
||||
#if HAL_USE_MMC_SPI
|
||||
extern MMCDriver MMCD1;
|
||||
#elif HAL_USE_SDC
|
||||
extern SDCDriver SDCD1;
|
||||
#elif HAL_USBH_USE_MSD
|
||||
|
||||
#else
|
||||
#error "MMC_SPI, SDC or USBH_MSD driver must be specified"
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Correspondence between physical drive number and physical drive. */
|
||||
#if HAL_USE_MMC_SPI
|
||||
#define FATFSDEV_MMC 0
|
||||
#define FATFSDEV_MMC_DRIVE "0:"
|
||||
#endif
|
||||
|
||||
#if HAL_USE_SDC
|
||||
#define FATFSDEV_SDC 0
|
||||
#define FATFSDEV_SDC_DRIVE "0:"
|
||||
#endif
|
||||
|
||||
#if HAL_USBH_USE_MSD
|
||||
#if defined(FATFSDEV_MMC) || defined(FATFSDEV_SDC)
|
||||
#define FATFSDEV_MSDLUN0 1
|
||||
#define FATFSDEV_MSDLUN0_DRIVE "1:"
|
||||
#else
|
||||
#define FATFSDEV_MSDLUN0 0
|
||||
#define FATFSDEV_MSDLUN0_DRIVE "0:"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif /* FATFS_DEVICES_H_ */
|
|
@ -9,38 +9,7 @@
|
|||
#include "ffconf.h"
|
||||
#include "diskio.h"
|
||||
#include "usbh/dev/msd.h"
|
||||
|
||||
#if HAL_USE_MMC_SPI && HAL_USE_SDC
|
||||
#error "cannot specify both MMC_SPI and SDC drivers"
|
||||
#endif
|
||||
|
||||
#if HAL_USE_MMC_SPI
|
||||
extern MMCDriver MMCD1;
|
||||
#elif HAL_USE_SDC
|
||||
extern SDCDriver SDCD1;
|
||||
#elif HAL_USBH_USE_MSD
|
||||
|
||||
#else
|
||||
#error "MMC_SPI, SDC or USBH_MSD driver must be specified"
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Correspondence between physical drive number and physical drive. */
|
||||
#if HAL_USE_MMC_SPI
|
||||
#define MMC 0
|
||||
#endif
|
||||
|
||||
#if HAL_USE_SDC
|
||||
#define SDC 0
|
||||
#endif
|
||||
|
||||
#if HAL_USBH_USE_MSD
|
||||
#if defined(MMC) || defined(SDC)
|
||||
#define MSDLUN0 1
|
||||
#else
|
||||
#define MSDLUN0 0
|
||||
#endif
|
||||
#endif
|
||||
#include "fatfs_devices.h"
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Inidialize a Drive */
|
||||
|
@ -53,7 +22,7 @@ DSTATUS disk_initialize (
|
|||
|
||||
switch (pdrv) {
|
||||
#if HAL_USE_MMC_SPI
|
||||
case MMC:
|
||||
case FATFSDEV_MMC:
|
||||
stat = 0;
|
||||
/* It is initialized externally, just reads the status.*/
|
||||
if (blkGetDriverState(&MMCD1) != BLK_READY)
|
||||
|
@ -62,7 +31,7 @@ DSTATUS disk_initialize (
|
|||
stat |= STA_PROTECT;
|
||||
return stat;
|
||||
#elif HAL_USE_SDC
|
||||
case SDC:
|
||||
case FATFSDEV_SDC:
|
||||
stat = 0;
|
||||
/* It is initialized externally, just reads the status.*/
|
||||
if (blkGetDriverState(&SDCD1) != BLK_READY)
|
||||
|
@ -72,7 +41,7 @@ DSTATUS disk_initialize (
|
|||
return stat;
|
||||
#endif
|
||||
#if HAL_USBH_USE_MSD
|
||||
case MSDLUN0:
|
||||
case FATFSDEV_MSDLUN0:
|
||||
stat = 0;
|
||||
/* It is initialized externally, just reads the status.*/
|
||||
if (blkGetDriverState(&MSBLKD[0]) != BLK_READY)
|
||||
|
@ -96,7 +65,7 @@ DSTATUS disk_status (
|
|||
|
||||
switch (pdrv) {
|
||||
#if HAL_USE_MMC_SPI
|
||||
case MMC:
|
||||
case FATFSDEV_MMC:
|
||||
stat = 0;
|
||||
/* It is initialized externally, just reads the status.*/
|
||||
if (blkGetDriverState(&MMCD1) != BLK_READY)
|
||||
|
@ -105,7 +74,7 @@ DSTATUS disk_status (
|
|||
stat |= STA_PROTECT;
|
||||
return stat;
|
||||
#elif HAL_USE_SDC
|
||||
case SDC:
|
||||
case FATFSDEV_SDC:
|
||||
stat = 0;
|
||||
/* It is initialized externally, just reads the status.*/
|
||||
if (blkGetDriverState(&SDCD1) != BLK_READY)
|
||||
|
@ -115,7 +84,7 @@ DSTATUS disk_status (
|
|||
return stat;
|
||||
#endif
|
||||
#if HAL_USBH_USE_MSD
|
||||
case MSDLUN0:
|
||||
case FATFSDEV_MSDLUN0:
|
||||
stat = 0;
|
||||
/* It is initialized externally, just reads the status.*/
|
||||
if (blkGetDriverState(&MSBLKD[0]) != BLK_READY)
|
||||
|
@ -140,7 +109,7 @@ DRESULT disk_read (
|
|||
{
|
||||
switch (pdrv) {
|
||||
#if HAL_USE_MMC_SPI
|
||||
case MMC:
|
||||
case FATFSDEV_MMC:
|
||||
if (blkGetDriverState(&MMCD1) != BLK_READY)
|
||||
return RES_NOTRDY;
|
||||
if (mmcStartSequentialRead(&MMCD1, sector))
|
||||
|
@ -155,7 +124,7 @@ DRESULT disk_read (
|
|||
return RES_ERROR;
|
||||
return RES_OK;
|
||||
#elif HAL_USE_SDC
|
||||
case SDC:
|
||||
case FATFSDEV_SDC:
|
||||
if (blkGetDriverState(&SDCD1) != BLK_READY)
|
||||
return RES_NOTRDY;
|
||||
if (sdcRead(&SDCD1, sector, buff, count))
|
||||
|
@ -163,7 +132,7 @@ DRESULT disk_read (
|
|||
return RES_OK;
|
||||
#endif
|
||||
#if HAL_USBH_USE_MSD
|
||||
case MSDLUN0:
|
||||
case FATFSDEV_MSDLUN0:
|
||||
/* It is initialized externally, just reads the status.*/
|
||||
if (blkGetDriverState(&MSBLKD[0]) != BLK_READY)
|
||||
return RES_NOTRDY;
|
||||
|
@ -189,7 +158,7 @@ DRESULT disk_write (
|
|||
{
|
||||
switch (pdrv) {
|
||||
#if HAL_USE_MMC_SPI
|
||||
case MMC:
|
||||
case FATFSDEV_MMC:
|
||||
if (blkGetDriverState(&MMCD1) != BLK_READY)
|
||||
return RES_NOTRDY;
|
||||
if (mmcIsWriteProtected(&MMCD1))
|
||||
|
@ -206,7 +175,7 @@ DRESULT disk_write (
|
|||
return RES_ERROR;
|
||||
return RES_OK;
|
||||
#elif HAL_USE_SDC
|
||||
case SDC:
|
||||
case FATFSDEV_SDC:
|
||||
if (blkGetDriverState(&SDCD1) != BLK_READY)
|
||||
return RES_NOTRDY;
|
||||
if (sdcWrite(&SDCD1, sector, buff, count))
|
||||
|
@ -214,7 +183,7 @@ DRESULT disk_write (
|
|||
return RES_OK;
|
||||
#endif
|
||||
#if HAL_USBH_USE_MSD
|
||||
case MSDLUN0:
|
||||
case FATFSDEV_MSDLUN0:
|
||||
/* It is initialized externally, just reads the status.*/
|
||||
if (blkGetDriverState(&MSBLKD[0]) != BLK_READY)
|
||||
return RES_NOTRDY;
|
||||
|
@ -239,7 +208,7 @@ DRESULT disk_ioctl (
|
|||
{
|
||||
switch (pdrv) {
|
||||
#if HAL_USE_MMC_SPI
|
||||
case MMC:
|
||||
case FATFSDEV_MMC:
|
||||
switch (cmd) {
|
||||
case CTRL_SYNC:
|
||||
return RES_OK;
|
||||
|
@ -257,7 +226,7 @@ DRESULT disk_ioctl (
|
|||
return RES_PARERR;
|
||||
}
|
||||
#elif HAL_USE_SDC
|
||||
case SDC:
|
||||
case FATFSDEV_SDC:
|
||||
switch (cmd) {
|
||||
case CTRL_SYNC:
|
||||
return RES_OK;
|
||||
|
@ -282,7 +251,7 @@ DRESULT disk_ioctl (
|
|||
}
|
||||
#endif
|
||||
#if HAL_USBH_USE_MSD
|
||||
case MSDLUN0:
|
||||
case FATFSDEV_MSDLUN0:
|
||||
switch (cmd) {
|
||||
case CTRL_SYNC:
|
||||
return RES_OK;
|
||||
|
|
|
@ -125,7 +125,7 @@
|
|||
* @brief Enables the SDC subsystem.
|
||||
*/
|
||||
#if !defined(HAL_USE_SDC) || defined(__DOXYGEN__)
|
||||
#define HAL_USE_SDC TRUE
|
||||
#define HAL_USE_SDC FALSE
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
|
|
@ -297,6 +297,7 @@ start:
|
|||
#if HAL_USBH_USE_MSD
|
||||
#include "usbh/dev/msd.h"
|
||||
#include "ff.h"
|
||||
#include "fatfs_devices.h"
|
||||
|
||||
static FATFS MSDLUN0FS;
|
||||
|
||||
|
@ -388,14 +389,14 @@ start:
|
|||
|
||||
usbDbgPuts("FS: Block driver ready, try mount...");
|
||||
|
||||
res = f_mount(&MSDLUN0FS, "0:", 1);
|
||||
res = f_mount(&MSDLUN0FS, FATFSDEV_MSDLUN0_DRIVE, 1);
|
||||
if (res != FR_OK) {
|
||||
usbDbgPuts("FS: Can't mount. Check file system.");
|
||||
continue;
|
||||
}
|
||||
usbDbgPuts("FS: Mounted.");
|
||||
|
||||
res = f_getfree("0:", &clusters, &fsp);
|
||||
res = f_getfree(FATFSDEV_MSDLUN0_DRIVE, &clusters, &fsp);
|
||||
if (res != FR_OK) {
|
||||
usbDbgPuts("FS: f_getfree() failed");
|
||||
continue;
|
||||
|
@ -419,7 +420,7 @@ start:
|
|||
//write test
|
||||
if (1) {
|
||||
usbDbgPuts("FS: Write test (create file /test.dat, 1MB)");
|
||||
f_open(&file, "/test.dat", FA_CREATE_ALWAYS | FA_WRITE);
|
||||
f_open(&file, FATFSDEV_MSDLUN0_DRIVE "/test.dat", FA_CREATE_ALWAYS | FA_WRITE);
|
||||
src = start;
|
||||
st = chVTGetSystemTime();
|
||||
for (j = 0; j < 2048; j++) {
|
||||
|
@ -439,7 +440,7 @@ start:
|
|||
//read test
|
||||
if (1) {
|
||||
usbDbgPuts("FS: Read test (read file /test.dat, 1MB, compare)");
|
||||
f_open(&file, "/test.dat", FA_READ);
|
||||
f_open(&file, FATFSDEV_MSDLUN0_DRIVE "/test.dat", FA_READ);
|
||||
src = start;
|
||||
st = chVTGetSystemTime();
|
||||
for (j = 0; j < 2048; j++) {
|
||||
|
@ -463,7 +464,7 @@ start:
|
|||
//scan files test
|
||||
if (1) {
|
||||
usbDbgPuts("FS: Scan files test");
|
||||
fbuff[0] = 0;
|
||||
strcpy(fbuff, FATFSDEV_MSDLUN0_DRIVE);
|
||||
scan_files(chp, (char *)fbuff);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue