How to use "pp.I2C_GetDeviceList()" correctly in Python with MiniProg3?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
ghariman
Level 2
Level 2
First solution authored First reply posted First question asked

Hi,

First of all I have confirmed using Bridge Control Panel that I can do "List" and Bridge Control Panel is able to find my Slave device and show the 8bit and 7 bit address correctly (0xC2 for 8-bit or 0x61 for 7-bit respectively).

Now I am trying to use Python to interact with MiniProg3 to do the same thing.
I started from the example file: "C:\Program Files (x86)\Cypress\Programmer\Examples\Protocols\I2C\Python_Ex\Python_Ex.py" file.

There are codes that is suppose to scan the I2C bus and get the slave address:
hResult = pp.I2C_GetDeviceList()
devices = hResult[1]

I expect that the contents of the 2nd item in hResult that came from pp.I2C_GetDeviceList() to be some sort of string or character or integer that is a representation of my slave device address.

However the contents of hResult is like this (I am using Spyder3 on Anaconda):
In [19]: hResult
Out[19]: (0, <memory at 0x0000025D302D9708>, '')

As can be seen above, the 2nd item in hResult is a "memoryView" object.

Question: what am I doing wrong?
How is this <memory at 0x0000025D302D9708> memoryView related to my Slave device address?

Please help. Thank you.

0 Likes
1 Solution
ghariman
Level 2
Level 2
First solution authored First reply posted First question asked

After talking to a Python expert, I finally figured it out.
The original "Python_Ex.py" code provided in the PSoC Programmer installation Example folder for I2C, had a code that was operating on "memoryview" objects incorrectly.

For example (trying to find a slave with address 0x61):

Original code from Python_Ex.py:
hResult = pp.I2C_GetDevices()
devices = hResult[1]
ord(devices[0]) => this resulted in a syntax error because ord() does not accept memoryview object

Instead I needed to modify it:
hResult = pp.I2C_GetDevices()
devices = hResult[1]
print(ord(bytes([devices[0]]))) => no errors and the return value = 97 which is the decimal for 0x61 which is the correct slave address.

There are many useful videos talking about memoryview() and bytes() and bytearray() on the internet. Reading and watching them also helped me figure this out.

View solution in original post

0 Likes
3 Replies
ghariman
Level 2
Level 2
First solution authored First reply posted First question asked

I want to add that today I tried another API with python and I am seeing the same "strange" return from it.


Today I am just trying something very simple to read the first register from a I2C slave with address = 0x61 and the contents should be = 0x9E.

Below you can see that with Bridge Control Panel, it gives the right result.

ghariman_0-1652986992518.png

 

 

Then I tried to use Python scripting.
First I ran (part of the) Python_Ex.py example code to setup the MiniProg3 (eg. OpenPort(), SetProtocol, etc). This was okay as I can set the clock frequency, etc of the MiniProg3 I2C.

 

Then I typed this code into my console: pp.I2C_ReadData(0x61,1)

I checked the oscilloscope and the slave ACK correctly and the waveforms show the correct 0x9E being read out. Exactly the same waveform as when I used the Bridge Control Panel.

So the actual python command went through when I typed pp.I2C_ReadData(0x61,1).

The problem is not that the command does not work, but rather the problem is about the format of the return of the command.

 

In the python console window, the output from that command came back and shows like this (OUT line below):

In[26]: pp.I2C_ReadData(0x61,1)

Out[26]: (0, <memory at 0x000001DFEBB19B88>, '')

 

As you can see, the I2C_ReadData() returns 3 items, instead of just the expected contents of the register:

  • The first one it is an integer = 0
  • The second one it is a “memoryView” object = <memory at 0x000001DFEBB19B88>,  (also note the value of this memory at changes every time)
  • The third one is an empty string ''

 

I am stuck because I don’t know how to interpret those 3 items and why it does not correspond to the expected contents of the register which is supposed to be = 0x9E.

It should be a very obvious thing. But I just don’t get it.

 

Note: I also tried pp.I2C_ReadDataFromReg() and it gives same 3 items return.

 

Also, the PDF documentation “PSoC Programmer CLI User Guide.pdf” does not explain much about the “I2C_ReadData()”. It just says this API should return the read contents.

ghariman_1-1652986992520.png

 

 

0 Likes
ghariman
Level 2
Level 2
First solution authored First reply posted First question asked

After talking to a Python expert, I finally figured it out.
The original "Python_Ex.py" code provided in the PSoC Programmer installation Example folder for I2C, had a code that was operating on "memoryview" objects incorrectly.

For example (trying to find a slave with address 0x61):

Original code from Python_Ex.py:
hResult = pp.I2C_GetDevices()
devices = hResult[1]
ord(devices[0]) => this resulted in a syntax error because ord() does not accept memoryview object

Instead I needed to modify it:
hResult = pp.I2C_GetDevices()
devices = hResult[1]
print(ord(bytes([devices[0]]))) => no errors and the return value = 97 which is the decimal for 0x61 which is the correct slave address.

There are many useful videos talking about memoryview() and bytes() and bytearray() on the internet. Reading and watching them also helped me figure this out.

0 Likes
AlenAn14
Moderator
Moderator
Moderator
500 replies posted 100 solutions authored 250 replies posted

Hi @ghariman ,

Glad your query is resolved! and thank you for sharing the solution with the community!
Please feel free to post any queries or issues you may have on Infineon products in the community and we will be happy to help.

Thanks and Regards,
Alen
Cypress Semiconductor Corporation
An Infineon Technologies Company

0 Likes