Search code examples
armstm32i2ccortex-mlibopencm3

Can I use libopencm3 to write an i2c program on my STM32L0?


I am trying to create a very simple i2c test program to run on my STM32L0 (discovery kit). I have modified the miniblink program in libopencm3-examples/examples/stm32/l0/stm32l0538-disco.

But if I just include the i2c header file:

#include <libopencm3/stm32/i2c.h>

And run make I get the error:

 ../../../../../libopencm3//include/libopencm3/stm32/i2c.h:36:9: error: #error "stm32 family not defined."

Upon investigating this file it appears that there are rules defined for each of the other models but not for the l0, why is this? Does libopencm3 not support i2c on the STM32L0 series?

#if defined(STM32F0)
#       include <libopencm3/stm32/f0/i2c.h>
#elif defined(STM32F1)
#       include <libopencm3/stm32/f1/i2c.h>
#elif defined(STM32F2)
#       include <libopencm3/stm32/f2/i2c.h>
#elif defined(STM32F3)
#       include <libopencm3/stm32/f3/i2c.h>
#elif defined(STM32F4)
#       include <libopencm3/stm32/f4/i2c.h>
#elif defined(STM32L1)
#       include <libopencm3/stm32/l1/i2c.h>
#else
#       error "stm32 family not defined."
#endif

I had a look at libopencm3/stm32/l1/i2c.h and all it seems to do is import the common i2c library anyway. Is there any way I can just use the i2c library for the l1?

Thanks


Solution

  • STM32L0 specific header need to be added.
    though (i2c common header file can probebly be used).
    the common header's are not intended to be directly included (for safty and less headache). (it has includer check)

    solution:
    you should check "i2c_common_all.h" if it is has correct register defination for l0 (probebly yes), then copy stm32/l1/i2c.h to stm32/l0/i2c.h and added (the last two lines) to stm32/i2c.h

    #elif defined(STM32L1)
    #       include <libopencm3/stm32/l1/i2c.h>
    #elif defined(STM32L0)
    #       include <libopencm3/stm32/l0/i2c.h>
    

    and make modification to lib/stm32/l0/Makefile OBJS += i2c_common_all.o

    if you want, send a pull request

    hack: (not recommended)

    #include <libopencm3/cm3/common.h>
    #include <libopencm3/stm32/memorymap.h>
    #define LIBOPENCM3_I2C_H
    #include <libopencm3/stm32/i2c_common_all.h>
    

    and include the source file lib/stm32/common/i2c_common_all.c manually.