Using SerialFlash with STM32 Blue Pill

The SerialFlash library by PaulStoffregen is very popular for interfacing with FlashChips provided by manufacturers like Winbond and Micron. This library works very well for boards like Teensy and Arduino. However, a few changes are required when trying to use this library with STM32 Blue Pill (Arduino IDE, stm32duino package). These changes arise because the SPI library of Blue Pill is slightly different than the SPI libraries of Arduino or Teensy. The following are the steps you need to follow to adapt the PaulStoffregen’s SerialFlash library to STM32 Blue Pill:

  • Download the library and navigate to the SerialFlashChip.cpp file
  • Replace the SPI clock speed in SPISettings (line 33, as per the library in June 2020) from 50000000 to 18000000 (18MHz is just one of the many choices available. STM32 BluePill has a clock speed of 72 MHz, you can use any desired pre-scaler):
#define SPICONFIG SPISettings(18000000, MSBFIRST, SPI_MODE0)
  • In the SerialFlashChip::read function, replace SPIPORT.transfer with SPIPORT.read (line 161, as per the library in June 2020). This is because the SPI library of STM32 Blue Pill has no function called transfer(uint8 *buffer, uint32 length). The equivalent function, with the same arguments and performing the same operation is read(uint8 *buffer, uint32 length)
SPIPORT.transfer(p, rdlen);

That’s it. The SerialFlash library can now be used for interfacing STM32 BluePill and any of the supported flash chips. If you wish to use this library with Blue Pill and another board like Teensy, you may want to create a copy of the library and call the original library for the boards like Teensy and the modified library for STM32 Blue Pill.

For more posts related to firmware in general, checkout https://iotespresso.com/category/firmware/

1 comment

Leave a comment

Your email address will not be published.