Issue with I2C while writing and reading from buffer
Posted: Fri May 18, 2018 12:55 pm
Hey,
I am currently interfacing, I2C with Stm32f407disc board. problem here is when i use bytearray to read small length of data it works fine. But when i increase the bytearray size, and try to read it gives me garbage data.
Here goes my code to send array data of 6 elements. This program works fine and give me the following result, given below.
Now if i increase the size of the transmitted array,
I am currently interfacing, I2C with Stm32f407disc board. problem here is when i use bytearray to read small length of data it works fine. But when i increase the bytearray size, and try to read it gives me garbage data.
Here goes my code to send array data of 6 elements. This program works fine and give me the following result, given below.
Resultfrom pyb import I2C, UART
import utime as time
#I2C Initialisation
i2c = I2C(1,I2C.MASTER, baudrate = 100000) #master mode
#Buffer to transmit and Recieve data
Tx_buf = bytearray(5) #array of 7bytes
Rx_buf = bytearray(5) #array of 7 bytes
#Uart Initialisaion
uart = UART(4, 9600)
#Temp list to hold Read bytearray
temp = [0] * len(Rx_buf)
#Main Function
def main():
for x in range (5):
Tx_buf[x] = x #Sequence number stored in array
#Write Tx_Buf array to slave address 80
#@param ( Data or Data buffer, Addr, mem_addr, timeout, address size )
i2c.mem_write(Tx_buf, 80, 1, timeout = 5000, addr_size = 8)
time.sleep_ms(100) #Delay to write to EEPROM
#Recieve data into Recieve Buffer
#@param (No of bytes/buff, Addr, mem_addr, timeout, address size )
i2c.mem_read(Rx_buf, 80, 1, timeout = 5000, addr_size = 8)
uart.write("Read Array : {} \r\n".format(Rx_buf)) #Transmiting Read byte on UART4
main()
Read Array : bytearray(b'\x00\x01\x02\x03\x04')
Now if i increase the size of the transmitted array,
It gives the garbage data given below.
Tx_buf = bytearray(100) #array of 7bytes
Rx_buf = bytearray(100) #array of 7 bytes
can anyone suggest how to write 256 bytes and read 256 byte of data ?bytearray(b'`abcTUVWXYZ[\\]^\x0e\x0f\x10\x11\x12\x13\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x04\x050233456869:;<=>?\x0e\x0f\x10\x11\x12\x13\x04\x05\x06\x07\x08\t\n\x0b\x0c\rPQRSTUVWXYZ[\\]^_`bdcd')