Re: How to write a own I2C(slave) driver? #i2s #slave #driver

Stefan Jaritz

Hej ho

I like to do polish my stuff before I push it and for that I am asking for your help.

So far I created a driver for an audio codec. This codec is controlled via I2C. Setting up like a common driver is easy(via DEVICE_AND_API_INIT).

Now I like to put it on the next level my connecting my driver with my device tree. I am struggling to get all steps rightfully together.

I added in my custom board device tree file:


&i2c1 {
    status = "ok";
    clock-frequency = <I2C_BITRATE_FAST>;
    tlv320aic3204@48 {
        compatible = "ti,tlv320aic3204";
        reg = <0x48>;
        label = "tlv320aic3204";

Now I like to know how to make zephyr to connect my driver with the dts configuration. I checked the "/drivers/sensor/hts221" driver and sample "/samples/sensor/hts221"

but I can@t figure out how thsi works excatly.

Compiling my dts file via cmake works. My questions are:

1.) How does my driver gets the I2C address of the tlv320aic3204 slave?

2.) were are the dts settings generated as a C-File? (guess somewhere in my build dir)

3.) How and when does the init of the tlv320aic3204 driver is called?

4.) How can I access the tlv320aic3204 driver? (guess via device_get_binding function)



On 13/12/2018 16:18, Rodrigo Peixoto wrote:


Make it available and share the link with us. It will be enough for now.

"code dump" (+1)

Rodrigo Peixoto

On Thu, 13 Dec 2018 at 13:07 Stefan Jaritz <stefan@...> wrote:


I took the tlv320dac310x audio codec driver as template. Done a very specific driver for the tlv320aic3204 from TI. It is not streamlined with the zephyr audio api. I can send it to you or put it on my github repro.

Maybe it is worth thinking about adding a "code dump" place, were people can share there code. This codes is may not fulfilling the zephyr quality standards but can be a good starting point for everyone who tries to create drivers etc.

Best regards,


On 12/12/2018 11:38, Rodrigo Peixoto wrote:
Stefan, Is that IC a sensor? 
Do you need to use interrupt on it?

I would suggest you to start taking a look at the code from the sensor This is a simple one and it implements the Sensor Driver API. This API is described here If you need interrupts on it, you need to write the proper code for that. You can take a look at the sensor driver

Good luck and let us know if you face any issue. 

Best regards,
Rodrigo Peixoto
Co-founder and Technical advisor

+55 (82) 98144-8585 | Skype: rodrigopex

Em seg, 10 de dez de 2018 às 15:00, Stefan Jaritz <stefan@...> escreveu:

I am currently checking the documentation for a "How to do an I2C slave
driver" and I did not find some tutorial.

Hopefully some guys can quickly tell me how to do it.

My hardware setup: custom stm32f412 board + IC connected via I2C_1 (Pin
B9 & B8)

I created an out of tree board and added at the dts file:

&i2c1 {
     status = "ok";
     clock-frequency = <I2C_BITRATE_FAST>;

I setup the pins in the pinmux.c with these two lines:

#ifdef CONFIG_I2C_1

Think form this side the I2C should be rightfully configured.

Now I like to access the IC connected via I2C_1. How to do that?

How I link my code to the I2C_1 device, write the protocoll functions
and build an API around that?

Any ideas examples are welcome!


Rodrigo Peixoto
Co-founder and Technical guru

+55 (82) 98144-8585 | Skype: rodrigopex


Join to automatically receive all group messages.