Používání paměti EEPROM AT24C256 nebo FT24C256A je velmi jednoduché, paměť komunikuje po \(I^2C\) sběrnici, \(I^2C\) adresa je obvykle 0x50. Stačí inicializovat sběrnici a už se to může používat.

Vlastnosti AT24C128 a AT24C256:

  • Pracuje při nízkém \(V_{cc} = 1.8V - 3.6V\) i normálním napětí \(V_{cc} = 2.7V - 5.5V\)

  • interně organizována 16384x8 bit a 32768x8 bit (náš modul)

  • Dvoudrátové sériové rozhraní (\(I^2C\)) adresa: 0x50

  • Vysoká spolehlivost: počet zápisů 1000000, trvanlivost dat 40 let

Modul EEPROM AT24CXX

IMG 20241102 052014

EEPROM FT24C256A

vlcsnap 2024 11 02 05h15m07s424

Knihovna a funkce

Pro použití s Pico SDK je potřeba vložit do projektu hlavičkový soubor hardware/i2c.h a do CMakeLists.txt knihovnu hardware_i2c a nastavit 3 věci. Které i2c budeme používat (buď i2c0 nebo i2c1), kam je připojen datový vodič (SDA) a kam jsou připojeny hodiny (SCL). To je všechno.

Konfigurace EEPROM
#include "hardware/i2c.h"

#define BYTE unsigned char
#define I2C             i2c1        (1)
#define I2C_FREQ        100000      (2)
#define EEPROM_ADDR     0x50        (3)
#define EEPROM_PAGE_LEN 64          (4)
#define EEPROM_WRITE_US 11000       (5)
#define EEPROM_XFER_US  100000      (6)

#define PIN_SDA         14          (7)
#define PIN_SCL         15          (8)
1 Instance I2C (buď i2c0 nebo i2c1), zde upravte podle vašeho zapojení.
2 Frekvence hodin I2C sběrnice 100 kHz.
3 I2C adresa paměti (obvykle bývá 0x50)
4 Délka stránky EEPROM paměti (jde vyčíst z datového listu).
5 Prodleva v mikrosekundách po zápisu.
6 Prodleva v mikrosekundách po přenosu dat
7 Připojení SDA (data) k příslušnému pinu, zde upravte podle vašeho zapojení.
8 Připojení SCL (hodiny) k příslušnému pinu, zde upravte podle vašeho zapojení.
Funkce
void i2c_eeprom_init(void) (1)
void i2c_eeprom_read(int addr, BYTE *data, int dlen) (2)
void i2c_eeprom_write(int addr, BYTE *data, int dlen) (3)
1 Inicializace EEPROM paměti
2 Čtení bajtů z EEPROM paměti. addr je adresa paměti, kam se bude zapisovat, *data je ukazatel na data a dlen je délka zapisovaných dat.
3 Zápis bajtů do EEPROM paměti. addr je adresa paměti, odkud se bude číst, *data je ukazatel na buffer, do kterého budou data přečtena a dlen je délka čtených dat.
eeprom2.c — příklad použití spolu s knihovnou
/* Jednoduchý program na zápis a čtení dat do EEPROM paměti AT24C256
(c) Jirka Chráska 2024 <jirka@lixis.cz>
BSD licence
*/

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include <string.h>

#define BYTE unsigned char
#define I2C             i2c1
#define I2C_FREQ        100000
#define EEPROM_ADDR     0x50
#define EEPROM_PAGE_LEN 64
#define EEPROM_WRITE_US 11000
#define EEPROM_XFER_US  100000

#define PIN_SDA         14
#define PIN_SCL         15

// Inicializace EEPROM I2C sběrnice
void i2c_eeprom_init(void)
{
    i2c_init(I2C, I2C_FREQ);
    gpio_set_function(PIN_SDA, GPIO_FUNC_I2C);
    gpio_set_function(PIN_SCL, GPIO_FUNC_I2C);
}

// Čtení z EEPROM
void i2c_eeprom_read(int addr, BYTE *data, int dlen)
{
    BYTE cmd[2] = {(BYTE)(addr >> 8), (BYTE)addr};

    i2c_write_timeout_us(I2C, EEPROM_ADDR, cmd, sizeof(cmd), 1, EEPROM_XFER_US);
    i2c_read_timeout_us(I2C, EEPROM_ADDR, data, dlen, 0, EEPROM_XFER_US);
}

// Zápis do EEPROM
void i2c_eeprom_write(int addr, BYTE *data, int dlen)
{
    BYTE cmd[EEPROM_PAGE_LEN+2];
    int n, i = 0, a = addr & (EEPROM_PAGE_LEN - 1);

    while (dlen > 0)
    {
        n = a + dlen > EEPROM_PAGE_LEN ? EEPROM_PAGE_LEN - a : dlen;
        cmd[0] = (BYTE)((addr + i) >> 8);
        cmd[1] = (BYTE)(addr + i);
        memcpy(&cmd[2], &data[i], n);
        i2c_write_timeout_us(I2C, EEPROM_ADDR, cmd, n + 2, 0, EEPROM_XFER_US);
        i += n;
        dlen -= n;
        a = 0;
        sleep_us(EEPROM_WRITE_US);
    }
}

// tiskne buffer jako znaky
void print_chars(const uint8_t *buf, size_t len)
{
    for (size_t i = 0; i < len; ++i) {
        printf("%c", buf[i]);
        if (i % 16 == 15)
            printf("\n");
        else
            printf(" ");
    }
}

// test zápisu a čtení 1024 bytů na EEPROM adresu 0.
int main( void )
{
uint8_t buffer[1025] =
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";

    stdio_init_all();
    printf("Stiskni klávesu.\n");
    getchar();
    printf("\nTest zápisu a čtení EEPROM AT24C256\n");
    i2c_eeprom_init();
    printf("Zapíšu: \n");
    print_chars(buffer,1024);
    i2c_eeprom_write(0, buffer, 1024);
    i2c_eeprom_read(0, buffer, 1024);
    printf("Přečetl jsem: \n");
    print_chars(buffer,1024);
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.12)

include(pico_sdk_import.cmake)

project(eeprom2 C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

#Initialize the SDK
pico_sdk_init()

add_executable(eeprom2 eeprom2.c)

target_link_libraries( eeprom2
    pico_stdlib
    hardware_i2c
)

pico_enable_stdio_usb(eeprom2 1)
pico_enable_stdio_uart(eeprom2 0)

pico_add_extra_outputs(eeprom2)
Moje zapojení EEPROM modulu na sběrnici I2C1 a piny 14 (SDA) a 15 (SDC), napájení 3.3V.

IMG 20241102 200431

Raspberry Pi Pico pinout

pico pinout

Testovací výstup z minicomu zápis a čtení 1024 bytů dat.
Test zápisu a čtení EEPROM AT24C256
Zapíšu:
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
Přečetl jsem:
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 1 2 3 4 5 6 7 8 9 a b c d e f

Zdroje a odkazy