Replies: 3 comments 17 replies
-
Lines like these |
Beta Was this translation helpful? Give feedback.
-
Hi
I had no problem with the wiring and the SDA/SCL pinning was correct
from the beginning or I would not have been able to scan the bus.
The info about data from a bytearray being seen as an integer solved the
problem - Using the type command I spent some time checking the
difference between Buf[0] and Buf[0:1] ;-)
The chip is an 8 bit eeprom and the address being read/written is split
into 2 bytes and sent MSB followed by LSB
Here the current version:
```py
# MIT License
# part of the code Copyright (c) 2018 Mike Causer
# The wait routine is Copyright (c) 2019 Peter Hinch
# Slightly modified
import machine, time
from machine import SoftI2C
i2c= SoftI2C(machine.Pin(1), machine.Pin(0), freq=100000, timeout =
50000) #max clock @ 3.3V
100 kHz
i2c_addr = 0x57
pages = 128
bpp = 32 # bytes per page
WriteByte = 2*i2c_addr + 0 # this is the device address including the
write bit as LSB (=0)
ReadByte = 2*i2c_addr + 1 # and this is the device address with a
Read bit (=1)
WriteBytes = bytearray(3)
Buf1 = bytearray(1)
devices = i2c.scan()
print('Number of units found: ',len(devices))
for device in devices:
print("Decimal address: ",device," | Hex address: ",hex(device))
def Wait4ready(): # After a write, wait for device to become ready
Buf1[0] = 0
while True:
try:
if i2c.writeto(i2c_addr, Buf1): # Poll ACK
break
except OSError:
pass
finally:
time.sleep_ms(1)
def read(addr, nbytes):
RollOver = ((addr + nbytes) > 4094) #if we pass the 4 k boundary,
the address-counter will roll over and start at address 0
"""Read one or more bytes from the EEPROM starting from a specific
address"""
WriteBytes[0] = WriteByte
WriteBytes[1] = addr >> 8 # first MSB is being sent out
WriteBytes[2] = addr & 0xff # the LSb is sent
i2c.start() # start a dummy write to set the address counter in the
eeprom
i2c.write(WriteBytes) # make a dummy write request (no STOP)
Res = i2c.readfrom(i2c_addr, nbytes) # return data
return Res
def write(addr, buf):
"""Write one or more bytes to the EEPROM respecting the block
boundaries """
RollOver = addr + len(buf) > 4095
offset = addr % bpp
partial = 0
# partial page write
if offset > 0:
partial = bpp - offset
WriteBytes[2] = addr & 0xff #isolate LSB of addr
WriteBytes[1] = (addr >> 8) & 0xff #and MSB
WriteBytes[0] = WriteByte #controlword for addressing
EEPROM for write operations
i2c.start()
i2c.write(WriteBytes + buf[0:partial])
i2c.stop()
#Now wait for the eeprom to finish the write cyclus
Wait4ready()
#time.sleep_ms(20)
addr += partial
# full page write
for i in range(partial, len(buf), bpp):
WriteBytes[2] = (addr + i-partial) & 0xff #isolate
LSB of addr
WriteBytes[1] = ((addr + i - partial) >> 8) & 0xff #and MSB
WriteBytes[0] = WriteByte #controlword for
addressing EEPROM for write operations
i2c.start()
i2c.write(WriteBytes + buf[i:i+bpp])
i2c.stop()
Wait4ready()
#time.sleep_ms(20)
return RollOver
EEprom = read(0,8)
print('8 bytes read before write takes place: ', EEprom)
Rollover = write(0,'Sed ut perspiciatis unde omnis iste natus error sit
voluptatem accusantium doloremque laudantium ')
print('Rollover during write operation? ', Rollover)
EEprom = read(0,128)
print('128 byte read: ', EEprom)
```
which results in the following output:
Number of units found: 3
Decimal address: 60 | Hex address: 0x3c
Decimal address: 87 | Hex address: 0x57
Decimal address: 104 | Hex address: 0x68
8 bytes read before write takes place: b'Sed ut p'
Rollover during write operation? False
128 byte read: b'Sed ut perspiciatis unde omnis iste natus error sit
voluptatem accusantium doloremque laudantium
cdefghijklmnopqrstuvwxyz012345'
Thanks for all the good answers and amount of work invested. It's very
much appreciated!
Regards
|
Beta Was this translation helpful? Give feedback.
-
Issue: Description: MicroPython Version: Hardware Details:
Code:
Observations:
Troubleshooting Performed
Request:
Any insights would be appreciated. |
Beta Was this translation helpful? Give feedback.
-
Hello
I am working on a project where I need to access (read/write) data to an Artmel 24C32n eeprom. I have tried different libraries (mcauser and a modified version of peterhinch') but none of them work for me so I decided to use I2C primitives and develop a simple driver based on the datasheet from Atmel.
I read that only SoftI2C supports primitive operations which is the reason I import and use it.
I use Thonny 4.0.1 and micropython 1.19.1 running on a Raspberry Pi Pico W.
Here a snippet where I only read/write one byte:
When I try to run this snippet, I receive an error
Traceback (most recent call last):
File "", line 36, in
File "", line 13, in EEWriteByte
TypeError: object with buffer protocol required
The GO LE keys on my keyboard almost are worn out and still I have not been able to find the reason.
Any hint which can make this work ?
Regards
Beta Was this translation helpful? Give feedback.
All reactions