Skip to content

Commit

Permalink
Issue #323: refactor and align flash drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
terrillmoore committed Nov 23, 2021
1 parent f8109ac commit 87b011f
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 78 deletions.
74 changes: 61 additions & 13 deletions src/Catena_Flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Name: Catena_Flash.h
*/

/// \file

#pragma once

#ifndef _catena_flash_h_
Expand All @@ -23,11 +25,29 @@ Name: Catena_Flash.h

namespace McciCatena {

///
/// \brief Abstract class for SPI flash devices.
///
/// \details
/// This base class is used as the interface for all flash drivers.
/// At the moment, we don't allow for a lot of code sharing between
/// various driver types, but there's no reason this couldn't be
/// extended to support that in the future.
///
class cFlash
{
public:
// default constructor
/// \brief default constructor
cFlash() {}

///
/// \brief the default destructor is public and virtual, as recommended
/// by cppreference.com under [desctructors](https://en.cppreference.com/w/cpp/language/destructor).
///
/// This allows destruction via a pointer to the base class.
///
virtual ~cFlash() {}

// neither copyable nor movable
cFlash(const cFlash&) = delete;
cFlash& operator=(const cFlash&) = delete;
Expand All @@ -36,38 +56,66 @@ class cFlash

// public methods

/// \brief initialize flash system
///
/// \brief initialize flash driver
///
/// \param[in] pSpi pointer to SPI bus for this flash device.
/// \param[in] ChipSelectPin pin to be used as chip-select for this flash device.
///
virtual bool begin(SPIClass *pSpi, uint8_t ChipSelectPin) = 0;

/// \brief stop the flash system
///
/// \brief get default chip-select pin
///
virtual uint8_t getDefaultChipSelectPin() const = 0;

///
/// \brief stop the flash driver
///
virtual void end() = 0;

/// \brief reset chip
/// \brief reset flash chip
virtual void reset(void) = 0;

///
/// \brief read JEDEC id
///
/// \param[out] pManufacturerId is set to the manufacturer ID read from the flash device.
/// \param[out] pDeviceId is set to the device ID.
///
virtual void readId(uint8_t *pManufacturerId, uint16_t *pDeviceId) = 0;

/// \brief chip erase
virtual void eraseChip(void) = 0;
///
/// \brief Issue chip erase.
///
virtual bool eraseChip(void) = 0;

/// \brief erase a 4k page
virtual void eraseSector(uint32_t SectorAddress) = 0;
///
/// \brief Erase a 4k page.
///
/// \param[in] SectorAddress base byte address of the page to be erased.
///
virtual bool eraseSector(uint32_t SectorAddress) = 0;

///
/// \brief erase a 32k page
virtual void eraseBlock32(uint32_t Block32Address) = 0;
///
/// \param[in] Block32Address base byte address of the block to be erased.
///
virtual bool eraseBlock32(uint32_t Block32Address) = 0;

///
/// \brief erase a 64k page
virtual void eraseBlock64(uint32_t Block64Address) = 0;
virtual bool eraseBlock64(uint32_t Block64Address) = 0;

// set protection -- CATENA_MX25V8035F_PL_xxx
virtual void setProtection(uint8_t protectionLevel) = 0;
// set protection is not portable,not part of API
// virtual void setProtection(uint8_t protectionLevel) = 0;

/// \brief read buffer
virtual void read(uint32_t Address, uint8_t *pBuffer, size_t nBuffer) = 0;

/// \brief program buffer
virtual void program(uint32_t Address, const uint8_t *pBuffer, size_t nBuffer) = 0;
virtual bool program(uint32_t Address, const uint8_t *pBuffer, size_t nBuffer) = 0;

/// \brief program at most one page.
/// \returns number of bytes not consumed from buffer.
Expand Down
75 changes: 47 additions & 28 deletions src/Catena_Flash_at25sf081.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ Copyright notice and License:

#include <Arduino.h>
#include <SPI.h>
#include <Catena_Flash.h>>

namespace McciCatena {

class cFlash_AT25SF081
class cFlash_AT25SF081 : public cFlash
{
public:
enum Commands : uint8_t
Expand Down Expand Up @@ -222,7 +223,14 @@ class cFlash_AT25SF081
}

public:
cFlash_AT25SF081() {};
///
/// \brief the default constructor must marks the object as uninitialized
/// and not yet registered.
///
cFlash_AT25SF081()
: m_Initialized(false)
, m_registered(false)
{}

// uses default destructor

Expand All @@ -233,49 +241,56 @@ class cFlash_AT25SF081
cFlash_AT25SF081& operator=(const cFlash_AT25SF081&&) = delete;

// set up and probe device
bool begin(SPIClass *pSpi, uint8_t ChipSelectPin = 5);
virtual bool begin(SPIClass *pSpi, uint8_t ChipSelectPin) override;

/// \brief the default chip select pin for this device is D5
virtual uint8_t getDefaultChipSelectPin() const override
{
return 5;
}

// stop using device; use begin() to restart.
void end(void);
virtual void end(void) override;

// reset device
void reset(void);
virtual void reset(void) override;

// read ID
void readId(uint8_t *pManufacturerId, uint16_t *pDeviceId);

// read data
void read(uint32_t Address, uint8_t *pBuffer, size_t nBuffer);

// program
bool program(uint32_t Address, const uint8_t *pBuffer, size_t nBuffer);

// program a page
size_t programPage(uint32_t Address, const uint8_t *pBuffer, size_t nBuffer);

// internal powerDown
void powerDown(void);

// internal powerUp.
void powerUp(void);
virtual void readId(uint8_t *pManufacturerId, uint16_t *pDeviceId) override;

// chip erase
bool eraseChip(void);
virtual bool eraseChip(void) override;

// erase a 4k sector
bool eraseSector(uint32_t SectorAddress);
virtual bool eraseSector(uint32_t SectorAddress) override;

// erase a 32k block
bool eraseBlock32(uint32_t Block32Address);
virtual bool eraseBlock32(uint32_t Block32Address) override;

// erase a 64k block
bool eraseBlock64(uint32_t Block64Address);
virtual bool eraseBlock64(uint32_t Block64Address) override;

// set protection
///
/// \brief set protection (non-portable)
///
/// \param[in] protectionLevel defines the level of protection.
///
bool setProtection(ProtectionBits protectionLevel);

// read status registers
void readStatus(uint8_t *pSr1, uint8_t *pSr2);
// read a buffer
virtual void read(uint32_t Address, uint8_t *pBuffer, size_t nBuffer) override;

// program a buffer
virtual bool program(uint32_t Address, const uint8_t *pBuffer, size_t nBuffer) override;

// program a page
virtual size_t programPage(uint32_t Address, const uint8_t *pBuffer, size_t nBuffer) override;

// internal powerDown
virtual void powerDown(void) override;

// internal powerUp.
virtual void powerUp(void) override;

protected:
void beginTransaction()
Expand Down Expand Up @@ -391,9 +406,13 @@ class cFlash_AT25SF081
uint32_t get_tBegin() { return this->m_tBegin; }
uint32_t get_tEnd() { return this->m_tEnd; }

// read status registers
void readStatus(uint8_t *pSr1, uint8_t *pSr2);

private:
SPISettings m_Settings;
bool m_Initialized;
bool m_registered;
bool m_PowerDown;
uint8_t m_CS;
SPIClass * m_pSpi;
Expand Down
45 changes: 29 additions & 16 deletions src/Catena_Mx25v8035f.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Catena_Mx25v8035f : public cFlash
Catena_Mx25v8035f(void)
: m_Initialized(false)
, m_registered(false)
{};
{}

// neither copyable nor movable
Catena_Mx25v8035f(const Catena_Mx25v8035f&) = delete;
Expand All @@ -144,35 +144,48 @@ class Catena_Mx25v8035f : public cFlash
Catena_Mx25v8035f& operator=(const Catena_Mx25v8035f&&) = delete;

// set up and probe device
virtual bool begin(SPIClass *pSpi, uint8_t ChipSelectPin = D19) override;
virtual bool begin(SPIClass *pSpi, uint8_t ChipSelectPin) override;
virtual void end(void) override;

/// \brief the default chip select pin for this type of flash is D19
virtual uint8_t getDefaultChipSelectPin() const override
{
return D19;
}

// reset chip
virtual void reset(void) override;

// read id
virtual void readId(uint8_t *pManufacturerId, uint16_t *pDeviceId) override;

/// \brief chip erase
virtual void eraseChip(void) override;
// chip erase
virtual bool eraseChip(void) override;

// erase sector, block32 and block64
virtual void eraseSector(uint32_t SectorAddress) override;
virtual void eraseBlock32(uint32_t Block32Address) override;
virtual void eraseBlock64(uint32_t Block64Address) override;

// set protection -- CATENA_MX25V8035F_PL_xxx
virtual void setProtection(uint8_t protectionLevel) override;

// read a buffer
virtual bool eraseSector(uint32_t SectorAddress) override;
virtual bool eraseBlock32(uint32_t Block32Address) override;
virtual bool eraseBlock64(uint32_t Block64Address) override;

///
/// \brief set protection (non-portable)
///
/// \param[in] protectionLevel is a bitmask chosen from CATENA_MX25V8035F_PL_xxx
///
void setProtection(uint8_t protectionLevel);

/// \brief read a buffer
virtual void read(uint32_t Address, uint8_t *pBuffer, size_t nBuffer) override;

// program a buffer
virtual void program(uint32_t Address, const uint8_t *pBuffer, size_t nBuffer) override;
/// \brief program a buffer
virtual bool program(uint32_t Address, const uint8_t *pBuffer, size_t nBuffer) override;
/// \brief program a page, returning number of bytes written
virtual size_t programPage(uint32_t Address, const uint8_t *pBuffer, size_t nBuffer) override;

// power management
/// \brief power down the flash chip
virtual void powerDown(void) override;

/// \brief power up the flash chip
virtual void powerUp(void) override;

private:
Expand All @@ -182,7 +195,7 @@ class Catena_Mx25v8035f : public cFlash
uint8_t m_CS;
SPIClass * m_pSpi;

void erase(uint32_t Address, uint8_t Command, uint32_t Delay);
bool erase(uint32_t Address, uint8_t Command, uint32_t Delay);
void setWel(void);
void pollWip(uint32_t pollMs);
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Catena_Flash_at25sf081.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ Name: cFlash_AT25SF081::eraseBlock64
This function erases a block (64KB).
Returns:
true for success, false for failure.
true, indicating success.
*/

bool cFlash_AT25SF081::eraseBlock64(
uint32_t Block64Address
)
{
this->erase(Block64Address, CMD_BE, Timing::tBE_64k);
return this->erase(Block64Address, CMD_BE, Timing::tBE_64k);
}

/*
Expand Down
Loading

0 comments on commit 87b011f

Please sign in to comment.