Skip to content

Commit

Permalink
mio168 sd card support
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Dec 9, 2020
1 parent 9856f60 commit 79745a9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 83 deletions.
60 changes: 39 additions & 21 deletions src/eez/fs_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ static FATFS g_fatFS[3];
static bool g_diskDriverLinked[NUM_SLOTS];
#endif

////////////////////////////////////////////////////////////////////////////////

bool isDriverLinked(int slotIndex) {
#if defined(EEZ_PLATFORM_STM32)
int driverIndex = slotIndex + 1;
Expand Down Expand Up @@ -142,6 +144,8 @@ void UnLinkDriver(int slotIndex) {
DebugTrace("Slot %d disk driver unlinked\n", slotIndex + 1);
}

////////////////////////////////////////////////////////////////////////////////

int getDiskDrivesNum(bool includeUsbMassStorageDevice) {
int diskDrivesNum = 0;

Expand Down Expand Up @@ -189,7 +193,10 @@ int getDiskDriveIndex(int iterationIndex, bool includeUsbMassStorageDevice) {
return -1;
}

////////////////////////////////////////////////////////////////////////////////

#if defined(EEZ_PLATFORM_STM32)

DSTATUS DiskDriver_initialize(BYTE lun) {
ExecuteDiskDriveOperationParams params;

Expand Down Expand Up @@ -261,43 +268,52 @@ DRESULT DiskDriver_ioctl(BYTE lun, BYTE cmd, void *buff) {
return (DRESULT)params.result;
}

static int8_t UsbStorageFS_Init(uint8_t lun) {
int slotIndex = g_selectedMassStorageDevice - 1;
////////////////////////////////////////////////////////////////////////////////

ExecuteDiskDriveOperationParams params;

params.operation = DISK_DRIVER_OPERATION_USB_STORAGE_FS_INIT;

g_slots[slotIndex]->executeDiskDriveOperation(&params);

return (int8_t)params.result;
static int8_t UsbStorageFS_Init(uint8_t lun) {
return USBD_OK;
}

static int8_t UsbStorageFS_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size) {
int slotIndex = g_selectedMassStorageDevice - 1;

ExecuteDiskDriveOperationParams params;

params.operation = DISK_DRIVER_OPERATION_USB_STORAGE_FS_GET_CAPACITY;
params.operation = DISK_DRIVER_OPERATION_IOCTL;

params.blockNum = block_num;
params.blockSize = block_size;
// get block_num
params.cmd = GET_SECTOR_COUNT;
params.buff = (uint8_t *)block_num;

g_slots[slotIndex]->executeDiskDriveOperation(&params);

return (int8_t)params.result;
if ((DRESULT)params.result != RES_OK) {
return -1;
}

// get block_size
params.cmd = GET_SECTOR_SIZE;
params.buff = (uint8_t *)block_size;

g_slots[slotIndex]->executeDiskDriveOperation(&params);

if ((DRESULT)params.result != RES_OK) {
return -1;
}

return USBD_OK;
}

static int8_t UsbStorageFS_IsReady(uint8_t lun) {
int slotIndex = g_selectedMassStorageDevice - 1;

ExecuteDiskDriveOperationParams params;

params.operation = DISK_DRIVER_OPERATION_USB_STORAGE_FS_IS_READY;
params.operation = DISK_DRIVER_OPERATION_STATUS;

g_slots[slotIndex]->executeDiskDriveOperation(&params);

return (int8_t)params.result;
return (DSTATUS)params.result & STA_NOINIT ? -1 : USBD_OK;
}

static int8_t UsbStorageFS_IsWriteProtected(uint8_t lun) {
Expand All @@ -306,40 +322,40 @@ static int8_t UsbStorageFS_IsWriteProtected(uint8_t lun) {

static int8_t UsbStorageFS_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
if (blk_len > 1) {
return USBD_FAIL;
return -1;
}

int slotIndex = g_selectedMassStorageDevice - 1;

ExecuteDiskDriveOperationParams params;

params.operation = DISK_DRIVER_OPERATION_USB_STORAGE_FS_READ;
params.operation = DISK_DRIVER_OPERATION_READ;

params.buff = buf;
params.sector = blk_addr;

g_slots[slotIndex]->executeDiskDriveOperation(&params);

return (int8_t)params.result;
return (DRESULT)params.result == RES_OK ? USBD_OK : -1;
}

static int8_t UsbStorageFS_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
if (blk_len > 1) {
return USBD_FAIL;
return -1;
}

int slotIndex = g_selectedMassStorageDevice - 1;

ExecuteDiskDriveOperationParams params;

params.operation = DISK_DRIVER_OPERATION_USB_STORAGE_FS_WRITE;
params.operation = DISK_DRIVER_OPERATION_WRITE;

params.buff = buf;
params.sector = blk_addr;

g_slots[slotIndex]->executeDiskDriveOperation(&params);

return (int8_t)params.result;
return (DRESULT)params.result == RES_OK ? USBD_OK : -1;
}

static int8_t UsbStorageFS_GetMaxLun() {
Expand All @@ -351,6 +367,8 @@ static int8_t UsbStorageFS_GetMaxLun() {
} // namespace fs_driver
} // namespace eez

////////////////////////////////////////////////////////////////////////////////

#if defined(EEZ_PLATFORM_STM32)

extern const int8_t STORAGE_Inquirydata_FS[];
Expand Down
10 changes: 1 addition & 9 deletions src/eez/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,15 +725,7 @@ void Module::stopDlog(int subchannelIndex, int resourceIndex) {

#ifdef EEZ_PLATFORM_STM32
void Module::executeDiskDriveOperation(ExecuteDiskDriveOperationParams *params) {
if (
params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_INIT ||
params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_GET_CAPACITY ||
params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_IS_READY ||
params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_READ ||
params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_WRITE
) {
params->result = USBD_FAIL;
} else if (params->operation == DISK_DRIVER_OPERATION_INITIALIZE || params->operation == DISK_DRIVER_OPERATION_STATUS) {
if (params->operation == DISK_DRIVER_OPERATION_INITIALIZE || params->operation == DISK_DRIVER_OPERATION_STATUS) {
params->result = STA_NOINIT;
} else {
params->result = RES_ERROR;
Expand Down
8 changes: 1 addition & 7 deletions src/eez/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,7 @@ enum DiskDriverOperation {
DISK_DRIVER_OPERATION_STATUS,
DISK_DRIVER_OPERATION_READ,
DISK_DRIVER_OPERATION_WRITE,
DISK_DRIVER_OPERATION_IOCTL,

DISK_DRIVER_OPERATION_USB_STORAGE_FS_INIT,
DISK_DRIVER_OPERATION_USB_STORAGE_FS_GET_CAPACITY,
DISK_DRIVER_OPERATION_USB_STORAGE_FS_IS_READY,
DISK_DRIVER_OPERATION_USB_STORAGE_FS_READ,
DISK_DRIVER_OPERATION_USB_STORAGE_FS_WRITE,
DISK_DRIVER_OPERATION_IOCTL
};

struct ExecuteDiskDriveOperationParams {
Expand Down
48 changes: 3 additions & 45 deletions src/eez/modules/dib-mio168/dib-mio168.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ using namespace eez::psu;
using namespace eez::psu::gui;
using namespace eez::gui;

static uint32_t g_trtMrt1;
static uint32_t g_trtMrt2;
static uint32_t g_trtMrt3;
static uint32_t g_trtMrt4;
static uint32_t g_trtMrt5;
static uint32_t g_trtMrt6;
static uint32_t g_trtMrt7;
static uint32_t g_trtMrt8;
static uint32_t g_trtMrt9;
static uint32_t g_trtMrt10;
static uint32_t g_trtMrt11;

namespace eez {
namespace dib_mio168 {

Expand Down Expand Up @@ -1094,7 +1082,7 @@ struct Mio168Module : public Module {
data.diskDriverOperation.operation = diskOperationParams->operation;
data.diskDriverOperation.sector = diskOperationParams->sector;
data.diskDriverOperation.cmd = diskOperationParams->cmd;
if (data.diskDriverOperation.operation == DISK_DRIVER_OPERATION_WRITE || data.diskDriverOperation.operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_WRITE) {
if (data.diskDriverOperation.operation == DISK_DRIVER_OPERATION_WRITE) {
memcpy(data.diskDriverOperation.buffer, diskOperationParams->buff, 512);
} else if (data.diskDriverOperation.operation == DISK_DRIVER_OPERATION_IOCTL) {
memcpy(data.diskDriverOperation.buffer, diskOperationParams->buff, DISK_DRIVER_IOCTL_BUFFER_MAX_SIZE);
Expand Down Expand Up @@ -1199,13 +1187,10 @@ struct Mio168Module : public Module {

diskOperationParams->result = data.diskOperationResult;

if (diskOperationParams->operation == DISK_DRIVER_OPERATION_READ || diskOperationParams->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_READ) {
if (diskOperationParams->operation == DISK_DRIVER_OPERATION_READ) {
memcpy(diskOperationParams->buff, data.buffer, 512);
} else if (diskOperationParams->operation == DISK_DRIVER_OPERATION_IOCTL) {
memcpy(diskOperationParams->buff, data.buffer, DISK_DRIVER_IOCTL_BUFFER_MAX_SIZE);
} else if (diskOperationParams->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_GET_CAPACITY) {
*diskOperationParams->blockNum = ((uint32_t *)data.buffer)[0];
*diskOperationParams->blockSize = ((uint16_t *)data.buffer)[2];
}

diskOperationStatus = DISK_OPERATION_SUCCESSFULLY_FINISHED;
Expand All @@ -1214,15 +1199,7 @@ struct Mio168Module : public Module {
}

void setDiskOperationFailed() {
if (
diskOperationParams->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_INIT ||
diskOperationParams->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_GET_CAPACITY ||
diskOperationParams->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_IS_READY ||
diskOperationParams->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_READ ||
diskOperationParams->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_WRITE
) {
diskOperationParams->result = USBD_FAIL;
} else if (diskOperationParams->operation == DISK_DRIVER_OPERATION_INITIALIZE || diskOperationParams->operation == DISK_DRIVER_OPERATION_STATUS) {
if (diskOperationParams->operation == DISK_DRIVER_OPERATION_INITIALIZE || diskOperationParams->operation == DISK_DRIVER_OPERATION_STATUS) {
diskOperationParams->result = STA_NOINIT;
} else {
diskOperationParams->result = RES_ERROR;
Expand Down Expand Up @@ -2568,25 +2545,6 @@ struct Mio168Module : public Module {

#ifdef EEZ_PLATFORM_STM32
void executeDiskDriveOperation(ExecuteDiskDriveOperationParams *params) override {
if (params->operation == DISK_DRIVER_OPERATION_INITIALIZE) g_trtMrt1++;
else if (params->operation == DISK_DRIVER_OPERATION_STATUS) g_trtMrt2++;
else if (params->operation == DISK_DRIVER_OPERATION_READ) g_trtMrt3++;
else if (params->operation == DISK_DRIVER_OPERATION_WRITE) g_trtMrt4++;
else if (params->operation == DISK_DRIVER_OPERATION_IOCTL) g_trtMrt5++;
else if (params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_INIT) g_trtMrt6++;
else if (params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_GET_CAPACITY) g_trtMrt7++;
else if (params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_IS_READY) g_trtMrt8++;
else if (params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_READ) {
g_trtMrt9++;
g_trtMrt11 = params->sector;
}
else if (params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_WRITE) g_trtMrt10++;

if (params->operation == DISK_DRIVER_OPERATION_USB_STORAGE_FS_INIT) {
params->result = USBD_OK;
return;
}

diskOperationParams = params;

for (int nretry = 0; nretry < 10; nretry++) {
Expand Down
4 changes: 3 additions & 1 deletion src/eez/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ void highPriorityThreadMainLoop(const void *) {

void highPriorityThreadOneIter() {
osEvent event = osMessageGet(g_highPriorityMessageQueueId, 1);

WATCHDOG_RESET(WATCHDOG_HIGH_PRIORITY_THREAD);

if (event.status == osEventMessage) {
uint32_t message = event.value.v;
uint8_t type = QUEUE_MESSAGE_TYPE(message);
Expand All @@ -182,7 +185,6 @@ void highPriorityThreadOneIter() {
return;
}
#endif
WATCHDOG_RESET(WATCHDOG_HIGH_PRIORITY_THREAD);
for (int i = 0; i < NUM_SLOTS; i++) {
g_slots[i]->tick();
}
Expand Down
1 change: 1 addition & 0 deletions src/eez/tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ enum LowPriorityThreadMessage {
THREAD_MESSAGE_SOUND_TICK,
THREAD_MESSAGE_SELECT_USB_MODE,
THREAD_MESSAGE_SELECT_USB_DEVICE_CLASS,
THREAD_MESSAGE_SELECT_USB_MASS_STORAGE_DEVICE,
THREAD_MESSAGE_USBD_MSC_DATAIN,
THREAD_MESSAGE_USBD_MSC_DATAOUT,
THREAD_MESSAGE_GENERATE_ERROR,
Expand Down

0 comments on commit 79745a9

Please sign in to comment.