INA3221 driver missing. Translate from CircuitPython?

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
Roanyg
Posts: 7
Joined: Fri Apr 28, 2017 8:36 am

INA3221 driver missing. Translate from CircuitPython?

Post by Roanyg » Wed Dec 30, 2020 11:00 pm

INA3221 is a 3 channels current sensor. Driver for circuitpython is made, but I cannot manage to make it work with Micropython.

https://github.com/barbudor/CircuitPython_INA3221

Can anyoune helpe me fix that i2c translation job?

User avatar
pythoncoder
Posts: 4921
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: INA3221 driver missing. Translate from CircuitPython?

Post by pythoncoder » Thu Dec 31, 2020 6:40 am

The approach I use is to pass an initialised I2C bus to the constructor, in this case as the i2c_bus arg. This means that the caller must set baudrate and suchlike, but this facilitates sharing the bus with other devices which may use different args.

You'll need to rewrite the .write method to use i2c.writeto(self.i2c_addr, ...) and .read to use i2c.readfrominto(self.i2c_addr, buf).

In general, porting drivers from CircuitPython to MicroPython is not hard. Converting that to optimised MicroPython can be rather more involved, but may be unnecessary in this instance.

If you get this working you might want to post it on GitHub for others to use. It looks like a useful and impressive chip.
Peter Hinch

Roanyg
Posts: 7
Joined: Fri Apr 28, 2017 8:36 am

Re: INA3221 driver missing. Translate from CircuitPython?

Post by Roanyg » Fri Jan 01, 2021 8:50 am

I made this clone https://github.com/roarnyg/INA3221-Micropython. I have never ported any driver, so if I succseed, it will take some time / weeks. If you have any examples of ported I2C drivers from Circuitpython to Micropython, it could help me a lot.

User avatar
pythoncoder
Posts: 4921
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: INA3221 driver missing. Translate from CircuitPython?

Post by pythoncoder » Fri Jan 01, 2021 1:12 pm

You could look at this one.

A beauty of MicroPython and I2C is that you can test it at the REPL. Use scan() to see if your hardware is detected and then fire commands at it, reading the responses. Once you understand how MicroPython uses I2C, swapping out the Adafruit code is easy.
Peter Hinch

Roanyg
Posts: 7
Joined: Fri Apr 28, 2017 8:36 am

Re: INA3221 driver missing. Translate from CircuitPython?

Post by Roanyg » Fri Jan 01, 2021 1:28 pm

Thanx. I will investigate this scan() function even more.

Libraries forRaspberry Pi / Python and the sensor INA3221 is allso written, https://github.com/search?q=ina3221. Will it be easier to port from python to micropython rather than from circuitpython?

User avatar
pythoncoder
Posts: 4921
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: INA3221 driver missing. Translate from CircuitPython?

Post by pythoncoder » Sat Jan 02, 2021 5:42 am

My personal view is that CircuitPython is easier, but that may just be down to familiarity.
Peter Hinch

Post Reply