16#ifndef MB85RS64V_MB85RS64V_HPP_
17#define MB85RS64V_MB85RS64V_HPP_
102 MB85RS64V(SPI_HandleTypeDef *hspi, GPIO_TypeDef *csPort, uint16_t csPin, uint32_t timeoutMs = 100,
103 uint8_t maxAttempts = 3);
109 bool read(uint16_t address, uint8_t *buffer,
size_t length);
111 bool write(uint16_t address,
const uint8_t *data,
size_t length);
142 SPI_HandleTypeDef *_spi;
143 GPIO_TypeDef *_csPort;
146 uint8_t _maxAttempts;
147 HAL_StatusTypeDef _lastStatus = HAL_OK;
148 bool _lastRetryable =
false;
152 bool spiConfigOk()
const;
153 bool transmit(
const uint8_t *data, uint16_t length);
154 bool receive(uint8_t *data, uint16_t length);
155 bool sendCommand(uint8_t opcode);
156 bool isTransient(HAL_StatusTypeDef status)
const;
166 template <
typename Command>
167 bool run(Command command) {
168 for (uint8_t attempt = 0; attempt < _maxAttempts; attempt++) {
173 if (!_lastRetryable) {
Driver for one MB85RS64V SPI FRAM (8192 bytes) on an STM32 HAL SPI bus.
bool writeDisable()
Clears the write enable latch (WRDI, 04h).
bool writeStatus(uint8_t status)
Writes the status register (WREN, then WRSR, 01h); a retry repeats both steps.
static constexpr uint8_t STATUS_WEL
Write enable latch (read-only through WRSR).
bool read(uint16_t address, uint8_t *buffer, size_t length)
Reads bytes from the FRAM array (READ, 03h).
BlockProtect
Areas of the array that the block-protect bits can lock against writes.
@ UpperQuarter
0x1800 to 0x1FFF protected.
@ All
0x0000 to 0x1FFF protected.
@ UpperHalf
0x1000 to 0x1FFF protected.
static constexpr uint16_t SIZE_BYTES
Capacity in bytes (8192; addresses 0x0000 to 0x1FFF).
static constexpr size_t ID_LENGTH
Number of bytes returned by readId().
bool readStatus(uint8_t *status)
Reads the status register (RDSR, 05h).
HAL_StatusTypeDef lastStatus() const
HAL status of the most recent failed SPI call (HAL_OK if none has failed). Because a failed command i...
bool setBlockProtect(BlockProtect area)
Sets which part of the array the block-protect bits lock against writes, keeping the other writable s...
bool write(uint16_t address, const uint8_t *data, size_t length)
Writes bytes to the FRAM array (WREN, then WRITE, 02h); a retry repeats both steps.
static constexpr uint8_t STATUS_BP1
Block protect bit 1.
bool begin()
Checks the SPI setup and the chip's identity, and leaves writes disabled.
MB85RS64V(SPI_HandleTypeDef *hspi, GPIO_TypeDef *csPort, uint16_t csPin, uint32_t timeoutMs=100, uint8_t maxAttempts=3)
Creates the driver object; does not touch the hardware (call begin() for that).
static constexpr uint8_t STATUS_WPEN
Status register write protect enable.
bool writeEnable()
Sets the write enable latch (WREN, 06h). Normally not needed; write() does it.
static constexpr uint8_t STATUS_BP0
Block protect bit 0.
bool readId(uint8_t *id)
Reads the four-byte device ID (RDID, 9Fh).
: Header for main.c file. This file contains the common defines of the application.