Is there a way to programmatically detect type, manufacturer, name and/or description of i2c device on raspberry pi - python-3.x

I have a raspberry pi connected with multiple i2c devices and want to be able to programmatically work out the type, manufacturer name, device name and/or description for each device on the i2c bus.
Is there any way to do this? Can it be done in python?
I can already get the ID numbers with python code the does essentially the same as i2cdetect, but it doesn't give any information that tells what each device is.
That means I have to manually dig through data sheets and then still have to disassemble the hardware and test with each individual device as the only one connected, to identify which device is at which ID, since some devices can be customized by altering their factory ID.
Putting it together in the first place was a huge job that I don't want to repeat, so I would rather have a way to identify each device programmatically.
Does anyone know how or if this can be done? Does i2c protocol lend any support for that?
Could it be done indirectly, ie. is it possible to power down or otherwise temporarily mute or disable an i2c compatible HAT while the Raspberry Pi is running and then rescan the i2c bus and identify which ID no longer shows up?

No, the I2C bus is not designed for this.
The only way to do something like this is to have the knowledge of the possible addresses for each type of device stored somewhere, together with a way of detecting whether it's really that particular device. Some devices (such as the BMP/BME sensor family) have a ID register that can be used for this, but others don't have that. There are even devices (such as displays) that don't even support reading any data from them.

Related

Linux USB Disable USB Power Only

I've have been looking for a while and though a lot of Exchange Q&A's have helped, I haven't exactly received the answer I'm looking for.
Going off the question asked here: How can I detect a USB port being used for charging in Linux?
I felt as if it needed it's own question. I have a USB Powered Hub for charging smart phones and I wish to control the power (On/Off only) to each of the ports individually.
Using a USB charging cable, Linux cannot detect that a device is plugged in, so using a USB data cable might be more useful to this scenario.
Essentially, I want Linux to be able to detect when a device is plugged in, however I do not want any data transmission to occur between the device and Linux. Simply only power. By detecting the device being plugged in, I want to be able to control whether power will flow to the device or not, but no data transmission.
I'm currently testing with uhubctl, however when enabling/disabling the port, unfortunately it controls both power/data where I wish to cut off data entirely and only allow power to the device.
Is it possible to using a USB Data Cable detect a device plugged in, then essentially disable the phone talking to the computer entirely, but still allow power to flow to the device?
If not, the other option I was thinking is if there is a USB Power Hub out there that can tell Linux if a new device has been plugged in or not and I can tell it whether to charge or not?
Any input would be greatly appreciated.

pcsc-lite : multiple ACR122T devices with no unique serial number - linux

I have multiple ACR122T smart card devices with no unique serial numbers (from ACS). I need to know which physical smart card I am connected to from within pcsc-lite. I know from using lsusb which USB Port a physical device is on, but how to map that USB port information with SCardGetAttribute(SCARD_ATTR_DEVICE_UNIT) ?
It seems the map should be at the OS level as I cant find a way to read USB Port info (lsusb results) from within the pscs-lite API.
This post mentions a method, Smartcard reader ACR122U has no unique serial number
but I dont understand the details behind "used that dev number to ensure I always used the right reader".
Any help appreciated.
Thanks, -Phil
Disclaimer: I'm working on Windows, not Linux.
The ACR122T (and ACR122U) devices not only have no serial number (other than the one printed on the back) but also have no USB serial number so that they can only be differentiated by the USB port they are plugged in. Additionally all the ACR122Ts and Us I have here (we bought a bunch) identify themselves as "ACS ACR122" to Windows. Using the proprietary API to get the firmware version both the ACR122T and the ACR122U identify themselves as a ACR122U (e.g. "ACR122U207" or "ACR122U214").
That means that the OS itself is not able to differentiate multiple devices in any other way then the USB port it's plugged in.
I also tried to make a connection between the pcsc device and the OS device, but apart from this function (which is only available starting Windows 8) I have not found anything, so it looks to me like there is no way to do that through pcsc calls.
The device unit is given out by the pcsc layer sequentially so you can differentiate between devices you are connected to but not identify a specific device.

How should I structure a linux driver that uses several chips in one device?

I have a hardware device that consists of 3 separate chips on an I2C bus. I would like to group them together and expose them to userland as one logical device. The user would see the logical device represented by a single directory somewhere in /sys as well as the nodes you'd expect from the I2C chips under /sys/class/i2c-adapter/i2c-?/*.
One of the chips is an MCP23017 which as far as I can tell already has a driver (drivers/gpio/gpio-mcp23s08.c) and I'd like to reuse it. Another of the chips is a PCA9685 and I would like to contribute a driver for this chip that uses the PWM system in include/linux/pwm.h. The third chip is an MCU running custom firmware.
How should I structure the set of drivers? One idea I had is to register a platform driver to present the logical device, and use I2C drivers from within that. Is this a good way to go? Are there better ways?
The logical device is a motor driver board and IR receiver. I have a simple diagram of its structure.
I'm looking to create two interfaces. The first similar to /sys/class/gpio where motors can be 'exported' and then accessed via reading and writing attributes. This would be useful for shell script access and quick debugging of the mechanical parts of the system attached to the motors. The second a character device node in /dev where data can be read or written in binary format, more useful for application control.
it seems not usual design, are you sure you have access to I2C bus of all chips ?
I think you should be able to talk only to MCU, and MCU should manage other devices.
Otherwise, why MCU is there ?
However, I cannot see your diagram, perhaps link is wrong.

Is there a way to get the device node of a usb device using libusb APIs?

I am trying to get the device node (eg. /dev/sdb) of a usb device.
I was wondering if there is any libusb API that would give me the particular device node to which the USB device is associated with.
If there is no API, are there any other alternate means of achieving this? Any insight on this would prove REALLY helpful.
Thanks in advance.
On Linux, easiest method to explore attached USB devices and their properties is to simply scan directory /sys/bus/usb/devices. This virtual directory lists all attached devices. Each entry has very simple structure, and for every device that has slave connected (like device connected via hub) there is virtual subdirectory.
What is also nice that in general you do not need to be root to read a lot of device properties, like manufacturer or serial number.
Another very good property of this interface is that it is semi-stable. That is, every device has unique id like a-b.c.d.e:x.y (a - bus number, b,c - root hub, next hub, port, etc.., x,y - function, subfunction), and this device enumeration is not going to abruptly shift for all devices if one device is connected or disconnected.
You can also easily map these device ids into libusb-style bus/device numbers (but those are not stable).
Unfortunately, this is Linux specific, and does not seem to be available for other operating systems. I wish libusb had implemented something like this, but it does not.
Anyway, good luck!

Bluetooth UUID discovery

Does anyone know how a bluetooth device could pick up the discoverable devices' device IDs in range?
I am ideally looking for the simplest solution that involves the smallest implimentation of the bluetooth protocols.
A starting point would be good, I just wish to create a device that can store all the device ids of nearby bluetooth devices with minimal power consumption, preferably just using radio frequencies and not SDP and whatever else.
If you can't help me with this, please can you help me find good reading material for low level bluetooth (step by step) communication. The reading online is so high level that I cant work out what is actually sent, when.
Laalto nailed the answer from the Bluetooth spec/stack POV, but your question implies your looking for a stand-alone Bluetooth device - not just a laptop app scanning surrounding devices.
I can only speak for the BT chips that the company I work for manufactures (Cambridge Silicon Radio - CSR) but our chips can do that pretty much out of the box. Our chips have an on-board Virtual Machine sandbox that allows access to the firmware functions and Bluetooth stack of the chip. You can easily write a C code app to run in the virtual machine sandbox, on chip, that periodically scans for discoverable devices around, grab their ids and then download them when connected via USB or Serial, or maybe over BT when a device connects to the listener directly.
www.csr.com and www.csrsupport.com for chips, dev-kits, design references, etc.. etc...
You probably want a module with the extra HW (UARTs, USB etc...) as well as just the chip but you could implement this with something the size of a BlueTooth USB or probably smaller.
It would really help to know more about what your trying to achieve, why you want something that just scans the surrounding bluetooth devices and how big the device needs to be.
Sorry if this sounds like advertising. For balance: Broadcom make BT chips too!
The Bluetooth specs from http://www.bluetooth.org are a good starting place for low-level information. You need an account to access the specs, but you can create one for free.
Basically what you need to do is to go into Inquiry mode periodically and grab the response packets as they arrive. The more time you spend in Inquiry mode, the more likely you will discover devices in range: discoverable devices enter the Inquiry Scan mode only relatively rarely; it takes some time (10.24s at least with older Bluetooth versions) to scan all the possible frequencies in the Inquiry/Inquiry Scan frequency hopping schemes. And even then you can have suboptimal radio conditions.
For implementation I suggest you at least start with existing Bluetooth libraries such as BlueZ and do not attempt to create your own from scratch.

Resources