Tuesday, August 16, 2016

Programmatic Add and Removal of USB devices in Linux

As a part of setting up my 9$ CHIP computer, I was running my chip from 500 mA/1000 mA power sources and sometimes the CHIP stops during startup due to high current consumption.

To stop chip from stopping CHIP during boot I had taken following steps:

  1. Ensure the power supply is good and at least 1000 mA
  2. Ensure that CHIP is connected to a LiPo battery, so that battery can supply burst current situations
  3. Added /etc/systemd/system/no-limit.service 
    1. This is nothing but execution of axp209 --no-limit during CHIP-booting by systemd
  4. Stopping USB hubs and USB devices during startup programatically
    1. Write systemd scripts for using above script -- TODO

To achieve the 4th step, one  has to programmatically detect  USB devices on USB Hub and stop them. Later once startup is finished, start the USB devices programmatically.

So I write one helper script that does this job automatically and this code can be read at github


This script might be used for non-USB devices(might need some modifications). Though tried and tested on CHIP, it might run on PCs and other Linux boxes with little modifications. If you have suggestions/modifications, please let me know or update in git.

Monday, August 15, 2016

Data mining by Python script to get data from 9$ Chip's battery health

I got finally my 9$ chip boards. Fortunately this boards comes with LiPo battery connector and charging IC (named axp209). One can query this chip's i2c subsystem by i2cget/ic2set commands to get the data about system's battery status like:

  • Battery voltage
  • Charging current
  • Battery connected
  • Battery charging
  • etc
NTC provides a shell script that fetches above data. But running this script in daemon mode and collecting data from multiple chips (which are in network) would put  strain on CPU/battery resources of chip as this script executes almost 50 unix processes for getting 10 sensor values and it also might drain battery (when main power goes off AND monitoring scripts should not drain resources).

As I am going to monitor system using other python scripts as well, I started porting the axp209 script to python script using i2c-tools/py-smbus module. But this porting does not work in  python as smbus python-module does not provide forceful read/write of i2c-device addresses (for safe reasons). 
Similar options were provided in command line tools of  i2c-tools (like i2cget, i2cset, etc). So I have hacked  i2c-modules/py-smbus code and changed/added 3 lines to get similar forceful opening in smbus module of python.


Changed i2c-tools/py-smbus code is at github

Using the above  modified smbus module, I wrote python script that collects 9$ chip data in daemon mode. Source code is at github


This changed i2c-tools/py-smbus code can be used for raspberry pi as well (one has to change i2c-bus and device addresses accordingly)

Warning: Developer should be aware that forceful read/write of i2c devices can lead to dangerous consequences (if done unknowingly/inappropriately/etc). So developer is responsible for using this changed code :)