2

See also follow-up question about i2c addresses

I have this mikroe board wired up to an S2 Feather via the SDA and SCL pins. The Mikroe board is powered by the Feather via 3v3 and GND. I have 2k pullup resistors to 3v3 on the SDA and SCL pins.

Having run and I2C scan, I returned the following two addresses: 0x53 and 0x5b on the Mikroe board. At this point, feel free to have a look at the datasheet.

I want to read the EEPROM, so I have tried:

result = bytearray(8)
i2c.writeto_then_readfrom(0x5b, result, result)
print(result)

this returns:

bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00)

So then I think I need to send the address of the EEPROM read command to the mikroe board, which is listed as 0x01 in the datasheet. I try to load various commands from the datasheet into my writeto_then_readfrom_function such as 0x01 and 0xe5 (simple system status) in various ways.

I have tried to append the address to the buffer using:

result = bytearray.append(0x01)

But that returns

TypeError: argument has wrong type

So I tried to load it in as a byte, thinking that surely a bytearray can contain bytes:

test = bytes(0xe5)
result = bytearray.append(test)

But, sure enough, I have the same error. So how to I actually send the appropriate command to the Mikroe board so it transmits the data back to the buffer? Perhaps this isn't even the appropriate method? As an aside I have tried all of the examples above using the other address, 0x53 as well.

jonathanjo
  • 12,049
  • 3
  • 27
  • 60
  • 1
    You have three different problems. First basic usage of Python variables. Next is how to use the I2C module your MCU provides in Python. The last is how to access the memory data using the I2C module. Solve them one by one, there must be manuals and tutorials available - if not then how can anyone use that MCU board for any purpose. For example, I have no clue what exactly the writeto-readfrom method does even if I can guess, but you need to link to the documentation. – Justme Oct 12 '22 at 16:50
  • Thank you. Great point. I will mark this as solved because @jonathanjo worked out the basic usage in python. I will dig around in the I2c module and ask a more intelligent question next time. Thank you! – Z-Man Jones Oct 12 '22 at 16:56

1 Answers1

3

You're appending to the wrong thing. append is a method on bytearrays, and so is invoked on the object you want to append to, in order to pass that object in to the function as its first argumentNote.

I believe you meant this:

>>> result = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
>>> result.append(0x01)
>>> result
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x01')

Tested on Python 3.6.9

That's your Python problem solved: I see your follow-up question about i2c


Note: this is a common stumbling block in Python, and is either very opaque or completely obvious depending on your Python knowledge.

We think of a class function as being "the same as" the instance method, and they both look like attributes.

when C is a class which contains a definition for a function f(), and x is an instance of C, calling x.f(1) is equivalent to calling C.f(x, 1). -- Python Reference: Data Model

This is most visible by looking at the signatures:

import inspect # standard library for inspecting functions

>>> C = bytearray
>>> x = C()

>>> C.append
<method 'append' of 'bytearray' objects>
>>> inspect.signature(C.append)
<Signature (self, item, /)>

>>> x.append
<built-in method append of bytearray object at 0x7f840aeba998>
>>> inspect.signature(x.append)
<Signature (item, /)>
jonathanjo
  • 12,049
  • 3
  • 27
  • 60
  • 1
    Wow! What a simple mistake and great job catching it. That being said, when I fix it as you suggest the buffer actually returns "none" and as a result the i2c.writeto... method gets a type error that object with bufffer protocol is required. I don't know why appending to result changes the type. I tried "loading" into test as well but that returns error of can't convert bytes to int – Z-Man Jones Oct 12 '22 at 16:33