I2C EEPROM Reading And Writing
We highly encourage everyone to use our new Python package which is packed with features. This library is still supported, but is not recommended for new design.
The example below demonstrates how to read from and write to an I2C EEPROM. This particular demo uses the SparkX Qwiic 512Kbit EEPROM from Sparkfun.
1
from binhoHostAdapter import binhoHostAdapter
2
3
from time import sleep
4
5
class Sparkfun_QwiicEEPROM:
6
7
def __init__(self, addr, pageWriteMs):
8
self.eeprom_address = addr;
9
self.eeprom_pageWriteTime_ms = pageWriteMs
10
11
def begin(self, adapter):
12
self.hostAdapter = adapter
13
14
def writePage(self, memAddress, pageData):
15
memAddressHigh = (memAddress >> 8)
16
memAddressLow = memAddress & 0xFF
17
18
fullAddress = [memAddressHigh, memAddressLow]
19
20
## load the address into the buffer
21
self.hostAdapter.writeToBuffer(0, 0, fullAddress)
22
23
## load the data into the buffer
24
self.hostAdapter.writeToBuffer(0, 2, pageData)
25
26
## I2C Start Transmission
27
self.hostAdapter.startI2C(0, self.eeprom_address)
28
29
## I2C Write Buffer
30
self.hostAdapter.writeFromBufferI2C(0, 34)
31
32
## I2C End Transmission
33
self.hostAdapter.endI2C(0)
34
35
## Clear the Buffer
36
self.hostAdapter.clearBuffer(0)
37
38
def readPage(self, memAddress):
39
memAddressHigh = (memAddress >> 8)
40
memAddressLow = memAddress & 0xFF
41
42
## Clear the Buffer
43
self.hostAdapter.clearBuffer(0)
44
45
## I2C Start Transmission
46
print(self.hostAdapter.startI2C(0, self.eeprom_address))
47
48
print(self.hostAdapter.writeByteI2C(0, memAddressHigh))
49
print(self.hostAdapter.writeByteI2C(0, memAddressLow))
50
51
## I2C End Transmission
52
print(self.hostAdapter.endI2C(0))
53
54
## Request the data
55
self.hostAdapter.readToBufferI2C(0, self.eeprom_address, 32)
56
57
print(self.hostAdapter.readBuffer(0, 32))
58
59
def pageWriteDelay(self):
60
return self.eeprom_pageWriteTime_ms / 1000.0
61
62
# Change this to match your COMPort
63
COMPORT = "COM27"
64
65
print("EEPROM Example using Binho Host Adapter")
66
print("v1.0 -- Jonathan Georgino <[email protected]>")
67
print()
68
69
# create the binhoHostAdapter object
70
binho = binhoHostAdapter.binhoHostAdapter(COMPORT)
71
eeprom = Sparkfun_QwiicEEPROM(0xA0, 1)
72
73
print("Connecting to host adapter...")
74
print(binho.getDeviceID())
75
print()
76
77
print("Configuring I2C settings...")
78
binho.setOperationMode(0, "I2C")
79
binho.setPullUpStateI2C(0, "EN")
80
binho.setClockI2C(0, 400000)
81
print("Ready!")
82
print()
83
84
85
eeprom.begin(binho)
86
87
currentMemoryAddress = 0x0000
88
89
print("Starting to write to the EEPROM ...")
90
91
for i in range(4):
92
93
print("Writing Page #" + str(i))
94
95
pageData = [0xAA+i, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0]
96
pageData += [0xAB+i, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71, 0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1]
97
98
print("PageData=")
99
print(pageData)
100
101
eeprom.writePage(currentMemoryAddress, pageData)
102
103
currentMemoryAddress += 32
104
105
sleep(eeprom.pageWriteDelay())
106
107
print()
108
print()
109
110
currentMemoryAddress = 0
111
112
print("Starting to read to the EEPROM ...")
113
114
for i in range(4):
115
116
print("Reading Page #" + str(i))
117
118
eeprom.readPage(currentMemoryAddress)
119
120
currentMemoryAddress += 32
121
122
print()
123
print()
124
125
binho.close()
Copied!
Last modified 8mo ago
Copy link