2021-06-26 15:24:07 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-04-21 08:53:52 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2020 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_SPIINTERFACE_HPP_
|
|
|
|
|
#define SRC_SPIINTERFACE_HPP_
|
|
|
|
|
|
2023-04-19 22:27:09 +02:00
|
|
|
#include <cstdint>
|
2020-04-21 08:53:52 +02:00
|
|
|
#include <iostream>
|
2023-03-05 10:13:08 +01:00
|
|
|
#include <string>
|
2020-04-21 08:53:52 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \file SPIInterface.hpp
|
|
|
|
|
* \class SPIInterface
|
|
|
|
|
* \brief abstract class between spi implementation and converters
|
|
|
|
|
* \author Gwenhael Goavec-Merou
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class SPIInterface {
|
|
|
|
|
public:
|
2021-12-22 19:05:21 +01:00
|
|
|
SPIInterface();
|
2023-07-30 08:39:15 +02:00
|
|
|
SPIInterface(const std::string &filename, int8_t verbose,
|
2022-05-24 07:29:35 +02:00
|
|
|
uint32_t rd_burst, bool verify, bool skip_load_bridge = false,
|
|
|
|
|
bool skip_reset = false);
|
2020-04-21 08:53:52 +02:00
|
|
|
virtual ~SPIInterface() {}
|
|
|
|
|
|
2021-12-22 19:05:21 +01:00
|
|
|
bool protect_flash(uint32_t len);
|
|
|
|
|
bool unprotect_flash();
|
2022-03-10 08:51:54 +01:00
|
|
|
bool bulk_erase_flash();
|
2023-01-21 14:20:49 +01:00
|
|
|
void set_filename(const std::string &filename) {_spif_filename = filename;}
|
|
|
|
|
|
2021-12-22 19:05:21 +01:00
|
|
|
/*!
|
|
|
|
|
* \brief write len byte into flash starting at offset,
|
2022-12-18 23:29:21 +01:00
|
|
|
* optionally verify after write and unprotect
|
2021-12-22 19:05:21 +01:00
|
|
|
* blocks if required and allowed
|
|
|
|
|
* \param[in] offset: offset into flash
|
|
|
|
|
* \param[in] data: data to write
|
|
|
|
|
* \param[in] len: byte len to write
|
|
|
|
|
* \param[in] verify: verify flash after write
|
|
|
|
|
* \param[in] unprotect_flash: unprotect blocks if allowed and required
|
|
|
|
|
* \param[in] rd_burst: read flash by rd_burst bytes
|
|
|
|
|
* \param[in] verbose: verbose level
|
|
|
|
|
* \return false when something fails
|
|
|
|
|
*/
|
2023-08-08 15:54:27 +02:00
|
|
|
bool write(uint32_t offset, const uint8_t *data, uint32_t len,
|
2021-12-22 19:05:21 +01:00
|
|
|
bool unprotect_flash);
|
2023-03-05 10:13:08 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief read flash offset byte starting at base_addr and
|
|
|
|
|
* store into data buffer
|
|
|
|
|
* \param[in] data: buffer where to store
|
|
|
|
|
* \param[in] base_addr: offset into flash
|
|
|
|
|
* \param[in] len: byte len to read
|
|
|
|
|
* \return false when something fails
|
|
|
|
|
*/
|
|
|
|
|
bool read(uint8_t *data, uint32_t base_addr, uint32_t len);
|
|
|
|
|
|
2021-12-22 19:05:21 +01:00
|
|
|
/*!
|
|
|
|
|
* \brief read flash offset byte starting at base_addr and
|
|
|
|
|
* store into filename
|
|
|
|
|
* \param[in] filename: file to store
|
|
|
|
|
* \param[in] base_addr: offset into flash
|
|
|
|
|
* \param[in] len: byte len to read
|
|
|
|
|
* \param[in] rd_burst: read flash by rd_burst bytes
|
|
|
|
|
* \param[in] verbose: verbose level
|
|
|
|
|
* \return false when something fails
|
|
|
|
|
*/
|
|
|
|
|
bool dump(uint32_t base_addr, uint32_t len);
|
|
|
|
|
|
2020-04-21 08:53:52 +02:00
|
|
|
/*!
|
|
|
|
|
* \brief send a command, followed by len byte.
|
|
|
|
|
* \param[in] cmd: command/opcode to send
|
|
|
|
|
* \param[in] tx: buffer to send
|
|
|
|
|
* \param[in] rx: buffer for read access
|
|
|
|
|
* \param[in] len: number of byte to send/receive (cmd not comprise)
|
|
|
|
|
* to send only a cmd set len to 0
|
|
|
|
|
* \return 0 when success
|
|
|
|
|
*/
|
2023-08-08 15:54:27 +02:00
|
|
|
virtual int spi_put(uint8_t cmd, const uint8_t *tx, uint8_t *rx,
|
2020-08-23 17:16:21 +02:00
|
|
|
uint32_t len) = 0;
|
2020-04-21 08:53:52 +02:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief send a command, followed by len byte.
|
|
|
|
|
* \param[in] tx: buffer to send
|
|
|
|
|
* \param[in] rx: buffer for read access
|
|
|
|
|
* \param[in] len: number of byte to send/receive
|
|
|
|
|
* \return 0 when success
|
|
|
|
|
*/
|
2023-08-08 15:54:27 +02:00
|
|
|
virtual int spi_put(const uint8_t *tx, uint8_t *rx, uint32_t len) = 0;
|
2020-04-21 08:53:52 +02:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief wait until register content and mask match cond, or timeout
|
|
|
|
|
* \param[in] cmd: register to read
|
|
|
|
|
* \param[in] mask: mask used with read byte
|
|
|
|
|
* \param[in] cond: condition to wait
|
|
|
|
|
* \param[in] timeout: number of try before fail
|
|
|
|
|
* \return 0 when success, -ETIME when timeout occur
|
|
|
|
|
*/
|
|
|
|
|
virtual int spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond,
|
|
|
|
|
uint32_t timeout, bool verbose = false) = 0;
|
2021-12-22 19:05:21 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/*!
|
|
|
|
|
* \brief prepare SPI flash access
|
|
|
|
|
*/
|
|
|
|
|
virtual bool prepare_flash_access() {return false;}
|
|
|
|
|
/*!
|
|
|
|
|
* \brief end of SPI flash access
|
|
|
|
|
*/
|
|
|
|
|
virtual bool post_flash_access() {return false;}
|
|
|
|
|
|
2023-07-30 08:39:15 +02:00
|
|
|
int8_t _spif_verbose;
|
2021-12-22 19:05:21 +01:00
|
|
|
uint32_t _spif_rd_burst;
|
|
|
|
|
bool _spif_verify;
|
2022-05-11 14:29:08 +02:00
|
|
|
bool _skip_load_bridge;
|
2022-05-24 07:29:35 +02:00
|
|
|
bool _skip_reset; /*!< don't reset the device after write */
|
2023-03-05 10:13:08 +01:00
|
|
|
|
2021-12-22 19:05:21 +01:00
|
|
|
private:
|
|
|
|
|
std::string _spif_filename;
|
2020-04-21 08:53:52 +02:00
|
|
|
};
|
|
|
|
|
#endif // SRC_SPIINTERFACE_HPP_
|