arpruss / USBComposite_stm32f1

USB Composite library for STM32F1 (HID, Serial, MIDI and XBox360 controller)
Other
382 stars 76 forks source link

[Blue Pill STM32F103C8T6] USB Mass Storage W28Q64 (Like a SD card Reader) Failed to format #28

Open rtek1000 opened 5 years ago

rtek1000 commented 5 years ago

With the help of another library, it was possible to replace the SD card with a SPI flash memory.

But Windows cannot format the memory.

W25Q64 flash memory is only 8MB (64Mbit) and if the library's own formatter is used, Windows can correctly find the available 8MB space. But when trying to format in Windows 10 x64 Explorer, the size of 4GB appears and then the formatting does not end.

Formatter:

// Adafruit SPI Flash FatFs Format Example
// Author: Tony DiCola
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!  NOTE: YOU WILL ERASE ALL DATA BY RUNNING THIS SKETCH!  !!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
// Usage:
// - Modify the pins and type of fatfs object in the config
//   section below if necessary (usually not necessary).
// - Upload this sketch to your M0 express board.
// - Open the serial monitor at 115200 baud.  You should see a
//   prompt to confirm formatting.  If you don't see the prompt
//   close the serial monitor, press the board reset buttton,
//   wait a few seconds, then open the serial monitor again.
// - Type OK and enter to confirm the format when prompted.
// - Partitioning and formatting will take about 30-60 seconds.
//   Once formatted a message will be printed to notify you that
//   it is finished.
//
#include <SPI.h>
#include <SdFat.h>
#include <Adafruit_SPIFlash.h>

// Since SdFat doesn't fully support FAT12 such as format a new flash
// We will use Elm Cham's fatfs f_mkfs() to format
#include "ff.h"
#include "diskio.h"

// Use second SPI port
SPIClass SPI_2(2);

#define SS2 PB12

#if defined(__SAMD51__) || defined(NRF52840_XXAA)
Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS, PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
#else
//  #if (SPI_INTERFACES_COUNT == 1)
//    Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
//  #else
//    Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
//  #endif

Adafruit_FlashTransport_SPI flashTransport(SS2, &SPI_2);
#endif

Adafruit_SPIFlash flash(&flashTransport);

// file system object from SdFat
FatFileSystem fatfs;

void setup() {
  // Initialize serial port and wait for it to open before continuing.
  Serial.begin(115200);
  while (!Serial) {
    delay(100);
  }
  Serial.println("Adafruit SPI Flash FatFs Format Example");

  // Initialize flash library and check its chip ID.
  if (!flash.begin()) {
    Serial.println("Error, failed to initialize flash chip!");
    while(1);
  }
  Serial.print("Flash chip JEDEC ID: 0x"); Serial.println(flash.getJEDECID(), HEX);

  // Wait for user to send OK to continue.
  Serial.setTimeout(30000);  // Increase timeout to print message less frequently.
  do {
    Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    Serial.println("This sketch will ERASE ALL DATA on the flash chip and format it with a new filesystem!");
    Serial.println("Type OK (all caps) and press enter to continue.");
    Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  }
  while ( !Serial.find("OK"));

  // Call fatfs begin and passed flash object to initialize file system
  Serial.println("Creating and formatting FAT filesystem (this takes ~60 seconds)...");

  // Make filesystem.
  uint8_t buf[512] = {0};          // Working buffer for f_fdisk function.    
  FRESULT r = f_mkfs("", FM_FAT | FM_SFD, 0, buf, sizeof(buf));
  if (r != FR_OK) {
    Serial.print("Error, f_mkfs failed with error code: "); Serial.println(r, DEC);
    while(1);
  }

  // sync to make sure all data is written to flash
  flash.syncBlocks();

  Serial.println("Formatted flash!");

  // Check new filesystem
  if (!fatfs.begin(&flash)) {
    Serial.println("Error, failed to mount newly formatted filesystem!");
    while(1) delay(1);
  }

  // Done!
  Serial.println("Flash chip successfully formatted with new empty filesystem!");
}

void loop() {
  // Nothing to be done in the main loop.
}

//--------------------------------------------------------------------+
// fatfs diskio
//--------------------------------------------------------------------+
extern "C"
{

DSTATUS disk_status ( BYTE pdrv )
{
  (void) pdrv;
    return 0;
}

DSTATUS disk_initialize ( BYTE pdrv )
{
  (void) pdrv;
    return 0;
}

DRESULT disk_read (
    BYTE pdrv,      /* Physical drive nmuber to identify the drive */
    BYTE *buff,     /* Data buffer to store read data */
    DWORD sector,   /* Start sector in LBA */
    UINT count      /* Number of sectors to read */
)
{
  (void) pdrv;
    return flash.readBlocks(sector, buff, count) ? RES_OK : RES_ERROR;
}

DRESULT disk_write (
    BYTE pdrv,          /* Physical drive nmuber to identify the drive */
    const BYTE *buff,   /* Data to be written */
    DWORD sector,       /* Start sector in LBA */
    UINT count          /* Number of sectors to write */
)
{
  (void) pdrv;
  return flash.writeBlocks(sector, buff, count) ? RES_OK : RES_ERROR;
}

DRESULT disk_ioctl (
    BYTE pdrv,      /* Physical drive nmuber (0..) */
    BYTE cmd,       /* Control code */
    void *buff      /* Buffer to send/receive control data */
)
{
  (void) pdrv;

  switch ( cmd )
  {
    case CTRL_SYNC:
      flash.syncBlocks();
      return RES_OK;

    case GET_SECTOR_COUNT:
      *((DWORD*) buff) = flash.size()/512;
      return RES_OK;

    case GET_SECTOR_SIZE:
      *((WORD*) buff) = 512;
      return RES_OK;

    case GET_BLOCK_SIZE:
      *((DWORD*) buff) = 8;    // erase block size in units of sector size
      return RES_OK;

    default:
      return RES_PARERR;
  }
}

}

Source: https://github.com/adafruit/Adafruit_SPIFlash/tree/master/examples/SdFat_format

USB Reader:

// This uses the greiman sdfat library.
// To use SdFatEX, set ENABLE_EXTENDED_TRANSFER_CLASS to 1 in the library's
// src/SdFatConfig.h
#include <USBComposite.h>
#include <SPI.h>
#include "SdFat.h"
#include "Adafruit_SPIFlash.h"

USBMassStorage MassStorage;
USBCompositeSerial CompositeSerial;

#define LED_PIN PC13
#define PRODUCT_ID 0x29

// Use first SPI port
//SdFat sd;

// Use second SPI port
SPIClass SPI_2(2);
//SdFat sd(&SPI_2);

#define SS2 PB12

Adafruit_FlashTransport_SPI flashTransport(SS2, &SPI_2);

Adafruit_SPIFlash flash(&flashTransport);

//SdFatEX sd;

const uint32_t speed = SPI_CLOCK_DIV2 ;
//const uint8_t SD_CHIP_SELECT = SS2; //PA15; //SS;
bool enabled = false;
uint32 cardSize;

bool write(const uint8_t *writebuff, uint32_t startSector, uint16_t numSectors) {
  //  return sd.card()->writeBlocks(startSector, writebuff, numSectors);
  return flash.writeBlocks(startSector, writebuff, numSectors);
}

bool read(uint8_t *readbuff, uint32_t startSector, uint16_t numSectors) {
  //  return sd.card()->readBlocks(startSector, readbuff, numSectors);
  return flash.readBlocks(startSector, readbuff, numSectors);
}

void setup() {
  //  // Remap SPI 1
  //  // Source: https://community.platformio.org/t/pio-arduino-stm32f103cbt6-remap/6786/5
  //  afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY); // PB3 free
  //  afio_remap(AFIO_REMAP_SPI1);
  //
  //  gpio_set_mode (GPIOB, 3, GPIO_AF_OUTPUT_PP);
  //  gpio_set_mode (GPIOB, 4, GPIO_INPUT_FLOATING);
  //  gpio_set_mode (GPIOB, 5, GPIO_AF_OUTPUT_PP);

  USBComposite.setProductId(PRODUCT_ID);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, 1);
}

void initReader() {
  digitalWrite(LED_PIN, 0);
  //  cardSize = sd.card()->cardSize();
  cardSize = flash.size();
  MassStorage.setDriveData(0, cardSize, read, write);
  MassStorage.registerComponent();
  CompositeSerial.registerComponent();
  USBComposite.begin();
  enabled = true;
}

void loop() {
  if (!enabled) {
    //    if (sd.begin(SD_CHIP_SELECT)) {
    if (flash.begin()) {
      initReader();
    }
    else {
      delay(50);
    }
  }
  else {
    MassStorage.loop();
  }
}

Adafruit SPI Flash, for FAT filesystems on SPI flash chips: https://github.com/adafruit/Adafruit_SPIFlash

[Blue Pill STM32F103C8T6] Works!!! USB Mass Storage W28Q64 (Like a SD card Reader): https://github.com/adafruit/Adafruit_SPIFlash/issues/24#issue-496958474

joesphan commented 9 months ago

Currently attempting to implement USB mass storage on the STM32 with a spiflash. My understanding is W25QXX chips run at a page size of 256 bytes, meanwhile the standard assumption of sd cards (subsequently sdfat) is a sector size of 512 bytes. This means the read and write would end up with incomplete data.

bool write(const uint8_t *writebuff, uint32_t startSector, uint16_t numSectors) {
  //  return sd.card()->writeBlocks(startSector, writebuff, numSectors);
  return flash.writeBlocks(startSector, writebuff, numSectors);
}

bool read(uint8_t *readbuff, uint32_t startSector, uint16_t numSectors) {
  //  return sd.card()->readBlocks(startSector, readbuff, numSectors);
  return flash.readBlocks(startSector, readbuff, numSectors);
}

My idea is to have it read 2 pages and write 2 pages (note I've used different libraries, untested code):

bool write(const uint8_t *writebuff, uint32_t startSector, uint16_t numSectors) {
  flash.releasePowerDown();
  for (int i = 0; i < numSectors; i++)
  {
    for (int j = 0; j < 256; j++)
    {
      flash.write((startSector + i) * 2, j, *(writebuff + j));
    }
    for (int j = 0; j < 256; j++)
    {
      flash.write(((startSector + i) * 2) + 1, j, *(writebuff + j + 256));
    }

  }
  flash.powerDown();
  return true;
}

bool read(uint8_t *readbuff, uint32_t startSector, uint16_t numSectors) {

  flash.releasePowerDown();
  for (int i = 0; i < numSectors; i++)
  {
    for (int j = 0; j < 256; j++)
    {
      *(readbuff + j) = flash.read((startSector + i) * 2, j);
    }
    for (int j = 0; j < 256; j++)
    {
      *(readbuff + j + 256) = flash.read(((startSector + i) * 2) + 1, j);
    }

  }
  flash.powerDown();
  return true;
}

I will report back later.

Regarding the formatting, what is the issue with formatting via windows into a fat16? I don't have a very good understanding of how it all works, but isn't it just reading/writing raw bytes at the end of the day?