Close

...it works!

A project log for Heartbeat Logger

A portable device that logs a snippet of your heart at the push of a button.

Ole Andreas UtstumoOle Andreas Utstumo 05/19/2019 at 18:230 Comments

I think I need a drink...

From setting up the USB and mass storage controller components in CubeMX to successfully mounting the device in Windows it took 15 minutes, where I spent 10 of them just looking at the generated files. I expected far more hazzle than this.

I ended up setting the block size equal to the sector size of the FatFs file system, 4096 bytes, and using the same interfacing functions to read and write as FatFs.

In my custom driver include file, spi_flash.h:

#define FLASH_SECTOR_SIZE        4096
#define FLASH_SECTOR_COUNT            511
#define FLASH_BLOCK_SIZE        FLASH_SECTOR_SIZE
#define FLASH_BLOCK_COUNT        FLASH_SECTOR_COUNT

...

//FatFS interface functions
DSTATUS MMC_disk_status(void);
DSTATUS MMC_disk_initialize(void);
DRESULT MMC_disk_write(BYTE *pBuffer, DWORD sector, UINT count);
DRESULT MMC_disk_read(BYTE *pBuffer, DWORD sector, UINT count);

 in usbd_storage_if.c:

#define STORAGE_LUN_NBR                  1
#define STORAGE_BLK_NBR                  FLASH_BLOCK_COUNT
#define STORAGE_BLK_SIZ                  FLASH_BLOCK_SIZE

...

int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
  /* USER CODE BEGIN 3 */

  *block_num  = STORAGE_BLK_NBR;
  *block_size = STORAGE_BLK_SIZ;
  return (USBD_OK);
  /* USER CODE END 3 */
}

...

int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
  /* USER CODE BEGIN 6 */

    //Shared call with FatFs works out of the box as long as block size == sector size
    MMC_disk_read(buf, blk_addr, blk_len);

    return (USBD_OK);
  /* USER CODE END 6 */
}

/**
  * @brief  .
  * @param  lun: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
  /* USER CODE BEGIN 7 */

        //Shared call with FatFs works out of the box as long as block size == sector size
    MMC_disk_write(buf, blk_addr, blk_len);

  return (USBD_OK);
  /* USER CODE END 7 */
}

I'll share the complete code when I'm done with the project. 

Next up: Verifying the file system integrity and making sure the figurative keys  to the file system will be handed to the USB MSC middle ware when connected to a PC, then back to my own code when disconnected. 

Discussions