Adapting a C program that reads from I2C bus to RIOT-OS

Hi everyone ! I am trying to access to I2C bus of STM32 nucleo board , where I have wired a AS7265x spectrometer sensor. I have found on github,com (see here - as7265x/src at master · jdesbonnet/as7265x · GitHub ) an interesting C program that does this task . So I have run it in a RIOT OS folder in order to adapt to RIOT but I found this error. ioctl(I2C_RDWR) in i2c_read: Bad file descriptor I supppose that the File descriptor of the slave device is not right. Can you help me to correct this error ? Thank you very much indeed. Bye

Sounds like you have build the application for the native board. There is no ioctl() in RIOT, but there is a native I2C driver for RIOT on Linux, so you can simulate your application on Linux using real I2C hardware.

Use BOARD=nucleo-… (with the proper name of your board) in your make commands to build and flash the STM32 board.

By native I2C driver for RIOT on Linuxx do you mean this in the following github rep ?

No I mean the driver for the native board that allows to run RIOT as a Linux process for simulation.

But turns out I2C has not been implemented here, only SPI and UART.

Where are you trying to call ioctl() and for which board do you build?

sorry perhaps I have been not clear in my question. since I want to make portable into RIOT OS (my board is nucleo-f401re board) the program written for Linux in the repository HERE I have arranged a single main.c with the main routines in RIOT OS application folder and created a Makefile . I have left intact the header file AS7265x.h but I had some problems with the I2c.c and I2c.h version files provided in this repository since they use ioctl to write into the registers and also use different names for routines writing/reading from register and also for the use of file descriptor of the device. what should I do for i2c.c and I2c.h ? should I adapt the original ones or should I use the ones provided in Riot os ?

As far the use of IOCTL can I use it in Riot OS ?

This driver is using the Linux I2C API with a custom wrapper. You’ll have to convert the driver to use the RIOT I2C API instead - e.g. by replacing i2c_register_write() with i2c_write_reg()

Maybe you are right. Anyway I think that something in the makefile include or Riot os setup os not correct. i would try to make some modifications before. Can You suggest me how to add the includes /usr/include/linux so that it can find these header files ? It is a try for now.

I think you got a bit confused. RIOT has nothing to do with Linux, it’s am operating system on it’s own. It can run as a Linux process for simulation purpose, but usually it will run on a microcontroller like your STM32 independent of any PC - including Linux headers won’t help you here.

OK. I agree. Now I will try to replace i2c_register_write() with i2c_write_reg() . OK .

What do you think if I use this code for i2c_write_reg and i2c_read_reg ?

I’m new to Riot-OS and trying to learn it. This discussion took my attention for another reason: On the Riot-Os home page it says “Program as you are used to. Save time with common environments. Benefit from POSIX APIs”.

Isn’t ioctl() a POSIX function? If Riot-OS claims “Benefit from POSIX APIs”, I think the ioctl() function should be supported for all boards, not just Native board. Am I wrong?

Could you please explain POSIX support in Riot-OS a bit on this occasion. Thanks.

I don’t see how ioctl() would help here - the arguments to ioctl() are still device and Operating System specific. e.g. you won’t find a struct gpioevent_request or struct i2c_rdwr_ioctl_data anywhere outside Linux.

Hi @benpicco

Thanks for your kind answer. Is there any documentation or example something about POSIX support in Riot-OS to get more familiar with it ?

Apart from POSIX functionality provided by the toolchain (please refer to newlib in that case), there is also some (but not complete) OS-specific support that can be found in our documentation.

1 Like