Go Down

Topic: [Blue Pill STM32F103C8T6] USB Mass Storage Flash W28Q64 (Like a SD card Reader)  (Read 1 time) previous topic - next topic

rtek1000

 8)

Code: [Select]
// 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();
  }
}



Need Flash formater see:
https://github.com/arpruss/USBComposite_stm32f1/issues/28
Please avoid private messages, your question may be someone's answer in the future!

Go Up