LowPowerLab / SPIFlash

Arduino library for read/write access to SPI flash memory chips
GNU General Public License v3.0
173 stars 91 forks source link

How to use the library for SPI2 and SPI3 or SPI_2 and SPI_3 #31

Open brightproject opened 1 year ago

brightproject commented 1 year ago

How to use this library for black pill STM32F401CCU6? The files SPIFlash.cpp and SPIFlash.h, as I understand it, are an "add-on" over the main SPI library from Arduino? I am using an arduino IDE and two cores to work with a microcontroller STM32F401CCU6. Each kernel folder has its own files with examples for working with SPI. Accordingly, I have three folders on the PC with the same file names in them:

  1. Arduino(C:\Users\Admin\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\SPI) SPI.cpp and SPI.h
  2. STM core for Arduino IDE(C:\Users\Admin\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.5.0\libraries\SPI) SPI.cpp and SPI.h
  3. Rogerclark core for Arduino IDE(C:\Users\Admin\Documents\Arduino\hardware\Arduino_STM32-master\STM32F4\libraries\SPI) SPI.cpp and SPI.h And I don't understand which of these three files to "load" from file SPIFlash.h #include <SPI.h> Or rather, as far as I understand - depending on the selected kernel through the plan menu, such a file will be loaded during compilation. But these questions do not remove questions about how to work with external flash memory through the SPI2 and SPI3 interfaces, but rather add questions and confuse even more.
brightproject commented 1 year ago

I added in SPIFlash.cpp into function SPIFlash::initialize() code:

  SPI.setMOSI(PB5);
  SPI.setMISO(PB4);
  SPI.setSCLK(PB3);

and got the following

boolean SPIFlash::initialize()
{
#if defined (SPCR) && defined (SPSR)
  _SPCR = SPCR;
  _SPSR = SPSR;
#endif
//reMAP pins from SPI to SPI3
  SPI.setMOSI(PB5);
  SPI.setMISO(PB4);
  SPI.setSCLK(PB3);
  pinMode(_slaveSelectPin, OUTPUT);
  //divider not work
  //SPI.setClockDivider(SPI_CLOCK_DIV4);
  SPI.begin();
#ifdef SPI_HAS_TRANSACTION
  _settings = SPISettings(10000000L, MSBFIRST, SPI_MODE0);// set 10 MHz(10000000 Hz), real 8 MHz
#endif

  unselect();
  wakeup();

  if (_jedecID == 0 || readDeviceId() == _jedecID) {
    command(SPIFLASH_STATUSWRITE, true); // Write Status Register
    SPI.transfer(0);                     // Global Unprotect
    unselect();
    return true;
  }
  return false;
}

SPI frequency not work by DIVIDER. Worked during transaction only

Code from example worked with core stm32duino - https://github.com/stm32duino/Arduino_Core_STM32 Logic analizer image SPI_Flash Max CLK frequency - 8 Mhz for SPI_3, then : _settings = SPISettings(10000000L, MSBFIRST, SPI_MODE0);// set 10 MHz(10000000 Hz), real 8 MHz