You must format before attempting to use with the computer. The Windows 10 computer could not format the device, when trying to format Windows think the W25Q64 has 4GB (would be great if this size were true!).
// 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;
}
}
}
// 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();
}
}