2021-06-26 15:24:07 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2019-10-05 19:00:10 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2019 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-10 18:26:04 +02:00
|
|
|
#ifndef SRC_SPIFLASH_HPP_
|
|
|
|
|
#define SRC_SPIFLASH_HPP_
|
|
|
|
|
|
|
|
|
|
#include <string>
|
2019-10-05 19:00:10 +02:00
|
|
|
|
2020-04-21 09:00:57 +02:00
|
|
|
#include "spiInterface.hpp"
|
2021-10-10 18:26:04 +02:00
|
|
|
#include "spiFlashdb.hpp"
|
2019-10-05 19:00:10 +02:00
|
|
|
|
|
|
|
|
class SPIFlash {
|
|
|
|
|
public:
|
2021-01-30 07:57:49 +01:00
|
|
|
SPIFlash(SPIInterface *spi, int8_t verbose);
|
2019-10-05 19:00:10 +02:00
|
|
|
/* power */
|
2021-07-08 20:51:51 +02:00
|
|
|
virtual void power_up();
|
|
|
|
|
virtual void power_down();
|
2019-10-05 19:00:10 +02:00
|
|
|
void reset();
|
|
|
|
|
/* protection */
|
2019-11-21 09:19:48 +01:00
|
|
|
int write_enable();
|
2019-10-05 19:00:10 +02:00
|
|
|
int write_disable();
|
2021-10-10 18:26:04 +02:00
|
|
|
/*!
|
|
|
|
|
* \brief disable protection for all sectors
|
|
|
|
|
* \return -1 if write enable or disabling failed
|
|
|
|
|
*/
|
2019-10-05 19:00:10 +02:00
|
|
|
int disable_protection();
|
2021-10-20 07:43:47 +02:00
|
|
|
/*!
|
|
|
|
|
* \brief enable protection for selected blocks
|
|
|
|
|
* \param[in] protect_code: bp + tb combinaison
|
|
|
|
|
* \return -1 if write enable or enabling failed
|
|
|
|
|
*/
|
|
|
|
|
int enable_protection(uint8_t protect_code = 0x1c);
|
|
|
|
|
/*!
|
|
|
|
|
* \brief enable protection for specified area
|
|
|
|
|
* \param[in] length: TODO
|
|
|
|
|
* \return -1 if write enable or enabling failed
|
|
|
|
|
*/
|
|
|
|
|
int enable_protection(int len);
|
2021-05-05 06:59:02 +02:00
|
|
|
/*!
|
|
|
|
|
* \brief unlock all sectors: specific to
|
|
|
|
|
* Microchip SST26VF032B / SST26VF032BA
|
|
|
|
|
* \return false if unlock fail
|
|
|
|
|
*/
|
|
|
|
|
bool global_unlock();
|
2019-10-05 19:00:10 +02:00
|
|
|
/* erase */
|
|
|
|
|
int bulk_erase();
|
2021-10-10 18:26:04 +02:00
|
|
|
/*!
|
|
|
|
|
* \brief erase one sector (64Kb)
|
|
|
|
|
*/
|
2019-10-05 19:00:10 +02:00
|
|
|
int sector_erase(int addr);
|
2021-10-10 18:26:04 +02:00
|
|
|
/*!
|
|
|
|
|
* \brief erase n sectors starting at base_addr
|
|
|
|
|
*/
|
2019-10-05 19:00:10 +02:00
|
|
|
int sectors_erase(int base_addr, int len);
|
|
|
|
|
/* write */
|
|
|
|
|
int write_page(int addr, uint8_t *data, int len);
|
2020-08-23 17:14:21 +02:00
|
|
|
/* read */
|
|
|
|
|
int read(int base_addr, uint8_t *data, int len);
|
2021-07-11 11:32:10 +02:00
|
|
|
/*!
|
|
|
|
|
* \brief read len Byte starting at base_addr and store
|
|
|
|
|
* into filename
|
|
|
|
|
* \param[in] filename: file name
|
|
|
|
|
* \param[in] base_addr: starting address in flash memory
|
|
|
|
|
* \param[in] len: length (in Byte)
|
|
|
|
|
* \param[in] rd_burst: size of packet to read
|
|
|
|
|
* \return false if read fails or filename can't be open, true otherwise
|
|
|
|
|
*/
|
|
|
|
|
bool dump(const std::string &filename, const int &base_addr,
|
|
|
|
|
const int &len, int rd_burst = 0);
|
2019-10-05 19:00:10 +02:00
|
|
|
/* combo flash + erase */
|
|
|
|
|
int erase_and_prog(int base_addr, uint8_t *data, int len);
|
2021-07-11 11:32:10 +02:00
|
|
|
/*!
|
|
|
|
|
* \brief check if area base_addr to base_addr + len match
|
|
|
|
|
* data content
|
|
|
|
|
* \param[in] base_addr: base address to read
|
|
|
|
|
* \param[in] data: theorical area content
|
|
|
|
|
* \param[in] len: length (in Byte) to area and data
|
|
|
|
|
* \param[in] rd_burst: size of packet to read
|
|
|
|
|
* \return false if read fails or content didn't match, true otherwise
|
|
|
|
|
*/
|
|
|
|
|
bool verify(const int &base_addr, const uint8_t *data,
|
|
|
|
|
const int &len, int rd_burst = 0);
|
2021-10-19 07:06:46 +02:00
|
|
|
/* return status register value */
|
2019-11-21 09:19:48 +01:00
|
|
|
uint8_t read_status_reg();
|
2021-10-19 07:06:46 +02:00
|
|
|
/* display/info */
|
|
|
|
|
void display_status_reg(uint8_t reg);
|
2021-07-08 20:51:51 +02:00
|
|
|
virtual void read_id();
|
2020-08-23 17:14:21 +02:00
|
|
|
uint16_t readNonVolatileCfgReg();
|
|
|
|
|
uint16_t readVolatileCfgReg();
|
2021-10-10 18:26:04 +02:00
|
|
|
|
2021-07-08 20:51:51 +02:00
|
|
|
protected:
|
2021-10-10 18:26:04 +02:00
|
|
|
/*!
|
2021-10-10 18:27:41 +02:00
|
|
|
* \brief convert block protect to len in byte
|
|
|
|
|
* \param[in] bp: block protection
|
2021-10-10 18:26:04 +02:00
|
|
|
* \return protect area in byte
|
|
|
|
|
*/
|
|
|
|
|
uint32_t bp_to_len(uint8_t bp);
|
|
|
|
|
/*!
|
2021-10-10 18:27:41 +02:00
|
|
|
* \brief convert len (in byte) to corresponding block protect
|
2021-10-10 18:26:04 +02:00
|
|
|
* \param[in] len: len in byte
|
2021-10-10 18:27:41 +02:00
|
|
|
* \return bp code (based on chip bp[x] position)
|
2021-10-10 18:26:04 +02:00
|
|
|
*/
|
|
|
|
|
uint8_t len_to_bp(uint32_t len);
|
|
|
|
|
|
2020-04-21 09:00:57 +02:00
|
|
|
SPIInterface *_spi;
|
2021-01-30 07:57:49 +01:00
|
|
|
int8_t _verbose;
|
2021-05-05 06:25:00 +02:00
|
|
|
uint32_t _jedec_id; /**< CHIP ID */
|
2021-10-10 18:26:04 +02:00
|
|
|
flash_t *_flash_model; /**< detect flash model */
|
2019-10-05 19:00:10 +02:00
|
|
|
};
|
|
|
|
|
|
2021-10-10 18:26:04 +02:00
|
|
|
#endif // SRC_SPIFLASH_HPP_
|