F439_CPP_SPI_RA8875_TFT_LCD_04 1.0
STM32F439 SPI RA8875 7-inch TFT display with FRAM-backed touch calibration, built on the STM32_GFX, STM32_RA8875 and STM32_MB85RS64V libraries
Loading...
Searching...
No Matches
MB85RS64V.hpp
1/*
2 * MB85RS64V.hpp
3 *
4 * STM32 HAL driver for the Fujitsu (RAMXEED) MB85RS64V 64 Kbit (8 KB) SPI FRAM,
5 * as found on Adafruit's SPI Non-Volatile FRAM breakout (product 1897).
6 *
7 * Written from the MB85RS64V datasheet (DS501-00015-4v0-E): command set, timing
8 * and status register. Modeled on the shape of this site's other STM32 HAL
9 * drivers (an SPI handle plus a chip-select port and pin passed to the
10 * constructor). It is an independent implementation, not a port.
11 *
12 * Created on: Oct 3, 2026
13 * Author: johng
14 */
15
16#ifndef MB85RS64V_MB85RS64V_HPP_
17#define MB85RS64V_MB85RS64V_HPP_
18
19#include "main.h"
20#include <cstddef>
21#include <cstdint>
22
78class MB85RS64V {
79public:
81 static constexpr uint16_t SIZE_BYTES = 8192;
82
84 static constexpr size_t ID_LENGTH = 4;
85
88 static constexpr uint8_t STATUS_WEL = 0x02;
89 static constexpr uint8_t STATUS_BP0 = 0x04;
90 static constexpr uint8_t STATUS_BP1 = 0x08;
91 static constexpr uint8_t STATUS_WPEN = 0x80;
93
95 enum class BlockProtect : uint8_t {
96 None = 0,
100 };
101
102 MB85RS64V(SPI_HandleTypeDef *hspi, GPIO_TypeDef *csPort, uint16_t csPin, uint32_t timeoutMs = 100,
103 uint8_t maxAttempts = 3);
104
105 bool begin();
106
107 bool readId(uint8_t *id);
108
109 bool read(uint16_t address, uint8_t *buffer, size_t length);
110
111 bool write(uint16_t address, const uint8_t *data, size_t length);
112
113 bool writeEnable();
114
115 bool writeDisable();
116
117 bool readStatus(uint8_t *status);
118
119 bool writeStatus(uint8_t status);
120
121 bool setBlockProtect(BlockProtect area);
122
129 HAL_StatusTypeDef lastStatus() const { return _lastStatus; }
130
131private:
132 enum : uint8_t {
133 OP_WREN = 0x06,
134 OP_WRDI = 0x04,
135 OP_RDSR = 0x05,
136 OP_WRSR = 0x01,
137 OP_READ = 0x03,
138 OP_WRITE = 0x02,
139 OP_RDID = 0x9F
140 };
141
142 SPI_HandleTypeDef *_spi;
143 GPIO_TypeDef *_csPort;
144 uint16_t _csPin;
145 uint32_t _timeoutMs;
146 uint8_t _maxAttempts;
147 HAL_StatusTypeDef _lastStatus = HAL_OK;
148 bool _lastRetryable = false;
149
150 void csLow();
151 void csHigh();
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;
157
166 template <typename Command>
167 bool run(Command command) {
168 for (uint8_t attempt = 0; attempt < _maxAttempts; attempt++) {
169 if (command()) {
170 return true;
171 }
172 csHigh(); // end whatever was in progress before trying the whole command again
173 if (!_lastRetryable) {
174 break; // a retry cannot fix this one: fail right away
175 }
176 }
177 Error_Handler();
178 return false;
179 }
180};
181
182#endif /* MB85RS64V_MB85RS64V_HPP_ */
Driver for one MB85RS64V SPI FRAM (8192 bytes) on an STM32 HAL SPI bus.
Definition MB85RS64V.hpp:78
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).
Definition MB85RS64V.hpp:88
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.
Definition MB85RS64V.hpp:95
@ UpperQuarter
0x1800 to 0x1FFF protected.
@ None
No protection.
@ All
0x0000 to 0x1FFF protected.
@ UpperHalf
0x1000 to 0x1FFF protected.
static constexpr uint16_t SIZE_BYTES
Capacity in bytes (8192; addresses 0x0000 to 0x1FFF).
Definition MB85RS64V.hpp:81
static constexpr size_t ID_LENGTH
Number of bytes returned by readId().
Definition MB85RS64V.hpp:84
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.
Definition MB85RS64V.hpp:90
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).
Definition MB85RS64V.cpp:25
static constexpr uint8_t STATUS_WPEN
Status register write protect enable.
Definition MB85RS64V.hpp:91
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.
Definition MB85RS64V.hpp:89
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.