LoPy4 - I2C - FRAM Memory - MicroPython Library



  • @jcaron said in LoPy4 - I2C - FRAM Memory - MicroPython Library:

    i2c.readfrom_mem(0x51,0x2,1,addrsize=16)

    Does
    this make any sense? What should I be using for baudrate?

    >>> from machine import I2C
    >>> i2c = I2C(0,I2C.MASTER, baudrate=100000)
    >>> 
    >>> i2c.scan()
    [81]
    >>> i2c.writeto_mem(0x51,0x2,bytearray(0x42),addrsize=16)
    66
    >>> i2c.readfrom_mem(0x51,0x2,1,addrsize=16)
    b'\x00'
    >>> 
    


  • @Eric-Tremblay An I2C address is 7 bits, in this case it is indeed 81 (0x51, 101 0001): 1010 fixed bits, 001 selected address. This is the address you should provide to the I2C functions, they will add the 0 or 1 to performs reads or writes.

    The memory address is 16 bits (2 x 8 bits), so you should use addrsize=16..

    This should write:

    i2c.writeto_mem(0x51,0x2,bytearray(0x42),addrsize=16)
    

    This should read back the data:

    i2c.readfrom_mem(0x51,0x2,1,addrsize=16)


  • @jcaron thanks for the pointer.
    I went back to the person who designed my board and I had an old unfinished copy of the wiring schematics, the pins are actually A0 to VDD, A1, and A2 to VSS. If I read the docs correctly should give me an address of 0101001(0or1). I took a multimeter to confirm everything was effectively like on the schematics and that all pins are ok I also check to make sure my write protect was in fact going from L to H and it seems ok. I'm pretty confident the hardware side is ok.
    when I scan the bus I still get an Int 81 back as an address (01010001). I try to pass in 01010011 but I get an I2C bus exception.

    i Tried different Baudrate. i wonder what i should be running.



  • @Eric-Tremblay the address pins on your FRAM module should not be left unconnected. That would result in unpredictable values.

    As specified in the datasheet they should be connected to either VSS (GND) or VDD (power supply) to achieve the right address. In your case they should probably all be connected to GND to achieve 000.



  • I was reading the docs for the FRAM module and saw that the first byte that needs to be sent after the start condition is met is 4 bits for the device address which are always 0101 then 3 bits to address multiple FRAM which mine are not connected. so they are 000. the last bit is 0 for a write operation and 1 for a read operation. when I scan the 12C bus I get back. int81 so 01010001. That's the address that I pass in my writeto_mem. I tried passing a 01010000 thinking maybe it would require a 0 to allow a write operation and it returns a 12C bus error.

    Eric



  • @Elizar Thank you for the pointer I was definitely overthinking this. Altho I am still in a bind
    when I call a writeto_mem() method It returns a number of bytes written like intended and then when call readfrom_mem() it returns me a byte array representing all 0, I played with baud rates and also with toggle my Write Protect Pin H to L with no success.
    I'm a bit puzzled but I am learning a lot about i2c so it's a good thing :)

    >>> i2c.init(I2C.MASTER, baudrate=20000)
    >>> i2c.scan()
    [81]
    >>> i2c.writeto_mem(81,3,4,addrsize=8)
    1
    >>> i2c.readfrom_mem(81,2,6,addrsize=8)
    b'\x00\x00\x00\x00\x00\x00'
    >>> i2c.writeto_mem(0x51,0x2,bytearray(1),addrsize=8)
    1
    >>> i2c.readfrom_mem(81,2,6,addrsize=8)
    b'\x00\x00\x00\x00\x00\x00'
    >>> 
    


  • There is no need for a "firmware" when it comes to using an FRAM. You just need to understand how an I²C bus operates.

    Reading or writing is a straight forward process:

    1. Create and i2c-Object (see example in the pycom docs)
    2. Write data by
    i2c.writeto_mem(ADDR_FRAM, Offset, bytearray(Data), addrsize=16)
    DataByteArray = i2c.readfrom_mem(ADDR_FRAM, Offset, Size, addrsize=16)
    

    Page 8 of the datasheet explains which bytes sequence has to be sent: two bytes for the target address, then the data byte(s).

    Then write yourself two functions WriteFRAM(Offset, Byte) and ReadFRAM(Offset) and you are done. The whole thing may finally have just about 20 lines of code.

    Don't spend too much time on searching for an already working program. Writing your own FRAM code is a very instructive task and will also lead to a better understanding of the possible pitfalls. Just as an example: how do you plan to react when a bus transmission fails due to a temporary electrical interference?


Log in to reply
 

Pycom on Twitter