Quickstart¶
Run the driver on your PC first, with no hardware; then read the chip from your MCU: wire it, write a small port class, call two driver functions.
Datasheet sections this page relies on
From Microchip (Atmel) Atmel-8707 F:
- Pins and packages: 1 Pin Configurations, Table 1-1, p. 2
All of them, with the driver API for each: Datasheet map.
Try it on your PC¶
examples/host/ runs the real driver against a simulated AT25040B, through a host
port. From the repository root, with CMake and a C++20 compiler:
cmake -S examples/host -B build-example
cmake --build build-example
./build-example/at25040b_host_example
AT25040B driver 0.1.0 on the host
at25040b: initialised
wrote 12 bytes at 0x0FC in 2 write cycles
read back: 6B 6F 6B 65 62 2D 61 74 32 35 00 01
at25040b: status reads 0xFF: nothing drives MISO
after unplugging: NoDevice, status byte 0xFF
The example checks the chip, writes 12 bytes across a page boundary (two write cycles), reads them back, then
unplugs the simulated module and probes again. The calls in main.cpp are the ones
below; only the port differs on an MCU. CI builds and runs this example on every pull request.
1. Wire it¶
| MCU | AT25040B (SOIC-8, TSSOP-8) | Notes |
|---|---|---|
| SPI SCK | SCK (pin 6) | Mode 0 or 3; 1 MHz for bring-up on jumper wires |
| SPI MOSI | SI (pin 5) | |
| SPI MISO | SO (pin 2) | High impedance while CS is high, so the bus can be shared |
| A GPIO or the SPI CS | CS (pin 1) | Low for one instruction, high between instructions |
| A GPIO, or VCC | WP (pin 3) | Low blocks every write. A GPIO lets the driver release it only while it writes |
| VCC | HOLD (pin 7) | Tie high. Never strap it to CS |
| 1.8 V to 5.5 V | VCC (pin 8) | 100 nF to GND at the pin |
| GND | GND (pin 4) |
Hardware has the clock limits per supply voltage and the other packages.
2. Write the port¶
The driver calls three hooks: bring the bus up, move bytes, wait.
// Your board's bus settings, checked against what the chip needs (handbook KB-CPP-123).
inline constexpr uint32_t kMySupplyMv = 3300; // lowest VCC the board sees
inline constexpr uint32_t kMyClockHz = 8'000'000; // what platform_spi_init() programs
inline constexpr uint8_t kMySpiMode = 0; // CPOL 0, CPHA 0
inline constexpr bool kMyMsbFirst = true; // the SDK's bit order
inline constexpr uint32_t kMyCsGapNs = 1000; // CS high between two transfers, as measured
static_assert(kMyClockHz <= at25040b::BusTimingAt(kMySupplyMv).max_clock_hz,
"SCK above f_SCK max at this supply (Table 4-3)");
static_assert(kMySpiMode == at25040b::kSpiMode || kMySpiMode == at25040b::kSpiModeAlternate,
"the chip samples in SPI mode 0 or 3");
static_assert(kMyMsbFirst == at25040b::kMsbFirst, "the chip takes MSB first");
static_assert(kMyCsGapNs >= at25040b::BusTimingAt(kMySupplyMv).min_cs_high_ns,
"CS high time below t_CS (Table 4-3)");
class MyPort : public at25040b::SpiInterface<MyPort> {
public:
// Bring the bus up once; later calls return true at once.
bool EnsureInitialized() noexcept {
if (!ready_) {
ready_ = platform_spi_init();
}
return ready_;
}
// One transfer, CS held low for all `len` bytes. `rx` is nullptr when no reply is needed.
bool Transfer(const uint8_t* tx, uint8_t* rx, std::size_t len) noexcept {
return platform_spi_transfer(tx, rx, len);
}
// Wait at least `us` microseconds: the driver polls the status every 100 us during a write.
void DelayUs(uint32_t us) noexcept {
platform_delay_us(us);
}
private:
bool ready_ = false;
};
Porting has the whole contract, the optional WP and log hooks, and the ESP-IDF port.
3. Read the chip¶
#include <array>
#include <cstdint>
#include "at25040b.hpp"
int read_board_id(MyPort& port, std::array<uint8_t, 8>& id) {
at25040b::AT25040B<MyPort> eeprom(port); // AT25040B, identity check on; no I/O yet
if (auto r = eeprom.EnsureInitialized(); !r) { // 1. bus up, identity check
return static_cast<int>(r.error); // NoDevice: nothing on MISO. DeviceIdMismatch: wiring, WP
}
if (auto r = eeprom.Read(0, id); !r) { // 2. eight bytes from address 0
return static_cast<int>(r.error); // Timeout: a write cycle that never ends
}
return 0; // 3. id holds what the chip sent: one READ, so check its CRC
}
EnsureInitialized() is the only call that talks to the chip before the start: it checks that an AT25xxxB
answers (see Contract). Every other call returns Error::NotInitialized until it
has succeeded. If it fails, GetLastStatusByte() shows what the bus returned: 0xFF is a floating MISO, 0x05 an
echo of the opcode. Troubleshooting has the rest.
Next: API guide for writes, block protection and the handler pattern.