I have been having difficulty getting a simple script setup in the Script Tool to read and write a sequence of registers to a device through I2C on my Analog Discovery 2. Most of the I2C configuration commands that correlate to the manual controls on the Protocol I2C window such as SCL, SDA pins assignments, Frequency, Address, ReadBytes, and WriteBytes are working properly.
However, the SubAddress does not seem to be functioning properly and does not update from the script like the other parameters. Perhaps there is a different function call or parameter to set instead of what I am trying to do. The last line of the following code is how I would expect the SubAddress to be changed. This code executes without error, but doesn't update the parameter. For example, If I set the Device Address value to 0x00 and the Sub Address (my device's register address to read) value to 0x01, then execute this script, I see the Address field of the Protocol tool change to 0x48, but the SubAddress field stays set to 0x01 when I expected it would be change to 0x02.
Protocol.I2C.Mode.value = 1; //Set the I2C Protocol tool into Master Mode
Protocol.I2C.SCL.value = 0; //Set the SCL pin to be DIO 0
Protocol.I2C.SDA.value = 1; //Set the SDA pin to be DIO 1
Protocol.I2C.SCL.Frequency.value = 100000; //Set the SCL clock frequency to 100 kHz
Protocol.I2C.Clockstretching.enable = 1; //Enable Clock Stretching
Protocol.I2C.ReadBytes.value = 1; //Set the Read Bytes number to 1 for reading one byte of data
Protocol.I2C.WriteBytes.value = 1; //Set the Write Bytes number to 1 for writing one byte of data
Protocol.I2C.Address.value = 0x48; //Set the I2C Protocol Device Address
//The following line of code executes but does not actually change the SubAddress parameter
Protocol.I2C.SubAddress.value = 0x02; //Set the SubAddress (Device register address)
Unfortunately there is no scripting information provided in the Help Reference Document for the Protocol tool, or any example code files for basic I2C (or SPI scripts) that simply read and write registers. But if I look at the I2C Custom scripting as an example, I would assume the commands for the Script Tool would be similar. There are 3 read functions available:
// 1.)Read(address) --> returns true when acknowledged otherwise false
Protocol.I2C.Read(0x48)
//Returns true for me
// 2.) Read(address, count of bytes) --> returns the read bytes array
Protocol.I2C.Read(0x48, 1)
//Returns the value of the register corresponding the the GUI's SubAddress that was manually entered on the Protocol page, but not the script assignment
// 3.) Read(address, [sub address byte1, byte 2,...], count of bytes) --> writes the subaddress bytes, repeated starts to read data bytes returns the read data bytes array
Protocol.I2C.Read(0x48, 0x01, 1)
//I would expect I could read one byte from register 0x01 of device 0x48. however, this function doesn't work properly. Is this the proper way to read a single 8-bit register? Since the SubAddress can't be set through the script, I hoped this would allow me to read the register value without having to manually changing the SubAddress in the GUI.
Could you please check to see if the SubAddress commands are working properly between the Script and Protocol tools and/or explain the proper way to script the Protocol I2C tool for writing and reading 8-bit registers if I am not approaching this properly. Thank You!
Question
Jonboy
I have been having difficulty getting a simple script setup in the Script Tool to read and write a sequence of registers to a device through I2C on my Analog Discovery 2. Most of the I2C configuration commands that correlate to the manual controls on the Protocol I2C window such as SCL, SDA pins assignments, Frequency, Address, ReadBytes, and WriteBytes are working properly.
However, the SubAddress does not seem to be functioning properly and does not update from the script like the other parameters. Perhaps there is a different function call or parameter to set instead of what I am trying to do. The last line of the following code is how I would expect the SubAddress to be changed. This code executes without error, but doesn't update the parameter. For example, If I set the Device Address value to 0x00 and the Sub Address (my device's register address to read) value to 0x01, then execute this script, I see the Address field of the Protocol tool change to 0x48, but the SubAddress field stays set to 0x01 when I expected it would be change to 0x02.
Protocol.I2C.Mode.value = 1; //Set the I2C Protocol tool into Master Mode Protocol.I2C.SCL.value = 0; //Set the SCL pin to be DIO 0 Protocol.I2C.SDA.value = 1; //Set the SDA pin to be DIO 1 Protocol.I2C.SCL.Frequency.value = 100000; //Set the SCL clock frequency to 100 kHz Protocol.I2C.Clockstretching.enable = 1; //Enable Clock Stretching Protocol.I2C.ReadBytes.value = 1; //Set the Read Bytes number to 1 for reading one byte of data Protocol.I2C.WriteBytes.value = 1; //Set the Write Bytes number to 1 for writing one byte of data Protocol.I2C.Address.value = 0x48; //Set the I2C Protocol Device Address //The following line of code executes but does not actually change the SubAddress parameter Protocol.I2C.SubAddress.value = 0x02; //Set the SubAddress (Device register address)
Unfortunately there is no scripting information provided in the Help Reference Document for the Protocol tool, or any example code files for basic I2C (or SPI scripts) that simply read and write registers. But if I look at the I2C Custom scripting as an example, I would assume the commands for the Script Tool would be similar. There are 3 read functions available:
// 1.)Read(address) --> returns true when acknowledged otherwise false Protocol.I2C.Read(0x48) //Returns true for me // 2.) Read(address, count of bytes) --> returns the read bytes array Protocol.I2C.Read(0x48, 1) //Returns the value of the register corresponding the the GUI's SubAddress that was manually entered on the Protocol page, but not the script assignment // 3.) Read(address, [sub address byte1, byte 2,...], count of bytes) --> writes the subaddress bytes, repeated starts to read data bytes returns the read data bytes array Protocol.I2C.Read(0x48, 0x01, 1) //I would expect I could read one byte from register 0x01 of device 0x48. however, this function doesn't work properly. Is this the proper way to read a single 8-bit register? Since the SubAddress can't be set through the script, I hoped this would allow me to read the register value without having to manually changing the SubAddress in the GUI.
Could you please check to see if the SubAddress commands are working properly between the Script and Protocol tools and/or explain the proper way to script the Protocol I2C tool for writing and reading 8-bit registers if I am not approaching this properly. Thank You!
Link to comment
Share on other sites
5 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.