How to use hal external module with Zephyr?

I'm trying to create a HAL library to use it with zephyr application

From what I have read I need to create another module .

I can't figure out how to set up the module and how to compile it.

for the moment I have only 1 module to add with the following files hal_flash.c and hal_flash.h 

How can I add cmake files kconfig files to make it work with zephyr application

note: app on nRF9160

Parents
  • I used this project as reference https://github.com/zephyrproject-rtos/hal_nrfx

    I made those changes:

    and added flash.h file to "nrfx/drivers/include" and flash.c to "nrfx/drivers/src/"

    and changed the CMakeLists and added this line

    zephyr_sources_ifdef(CONFIG_NRFX_FLASH   nrfx/drivers/src/hal_flash.c)

    and for the test I created an app folder with this structure

    hal_nrfx/app
    ├── CMakeLists.txt
    ├── prj.conf
    └── src
        └── main.c
    

    in main i include 

    #include <hal_flash.h>

    In the prj.conf I added 

    CONFIG_NRFX_FLASH=y
    but I keep getting this error when I try to build with "west build -p auto -b nrf9160dk_nrf9160_ns ."
  • Hello

    Any reason you're trying to put your driver into the nrfx tree?

    If I wanted to include my own driver in a project I would just put the files under src/ in the application folder.

    Then you'll just need to add your drivers as sources in CMakeLists, see for example the Asset Tracker v2 sample in NCS as an example of how this is done.

    The error you're getting means that your CONFIG_NRFX_FLASH symbol isn't defined properly.

    I'm not entirely sure what you'll have to do to make Kconfig recognize your config symbol, but I can look into it.

    Best regards,

    Einar

  • Hi Einarh

    Thanks for helping

    I want to create external module with my own hal files.

    then when creating an application I just want only to include the hal

    #include <hal_xxx.h>

    and add this in prj.conf

    CONFIG_hal_xxx=y

  • In your application you can include a header file from your src directory as

    #include "hal_xxx.h"

    Look at how it's done in one of the more complicated samples such as for example the Asset Tracker v2 application.

    -Einar

  • it will be used in many application so it's more practical to make it as module

  • Is you intention to make an official Zephyr module? In that case you can find info about how to do that here:

    https://docs.zephyrproject.org/latest/develop/modules.html

    If not, I would recommend simply including your library/driver files in the src directory in each of your projects.

    You can find most of the info you'll need about the configuration- and build system here:

    https://docs.zephyrproject.org/latest/develop/application/index.html

    -Einar

  • yes my intention is the first option

    I still get some problem,

    here is what I did:

    added module to the west.yml file

    and this is the structure of the module:

    <module_repo>
    ├── CMakeLists.txt
    ├── prj.conf
    ├── zephyr
        └── module.yml
    └── drivers
        └── foo
            ├── hal_flash.c
            ├── CMakeLists.txt
            ├── Kconfig
            └──include
               └──hal_flash.h

    module_repo/CMakeLists  contains

    set(ZEPHYR_EXTRA_MODULES drivers/foo)
    zephyr_include_directories(${ZEPHYR_EXTRA_MODULES})
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

    module_repo/prj.conf contains

    CONFIG_FOO=y

    in module_repo/drivers/foo/CMakeLists.txt : 

    zephyr_library()
    zephyr_include_directories(include)
    zephyr_library_sources_ifdef(CONFIG_FOO hal_flash.c)

    in module_repo/drivers/foo/Kconfig : 

    menuconfig FOO
            bool "foo driver"
            default y
            help
              Enable support for foo driver
    
    if FOO
    
    module = FOO
    module-str = foo
    source "subsys/logging/Kconfig.template.log_config"
    
    endif

    and module_repo/zephyr/module.yml : 

    build:
      cmake: .
      kconfig: drivers/foo/Kconfig
    

    and when I test my application (with CONF_FOO=y in prj.conf of the app) I got this error : 

    src/main.c:9: undefined reference to `hal_flash_flush'

Reply
  • yes my intention is the first option

    I still get some problem,

    here is what I did:

    added module to the west.yml file

    and this is the structure of the module:

    <module_repo>
    ├── CMakeLists.txt
    ├── prj.conf
    ├── zephyr
        └── module.yml
    └── drivers
        └── foo
            ├── hal_flash.c
            ├── CMakeLists.txt
            ├── Kconfig
            └──include
               └──hal_flash.h

    module_repo/CMakeLists  contains

    set(ZEPHYR_EXTRA_MODULES drivers/foo)
    zephyr_include_directories(${ZEPHYR_EXTRA_MODULES})
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

    module_repo/prj.conf contains

    CONFIG_FOO=y

    in module_repo/drivers/foo/CMakeLists.txt : 

    zephyr_library()
    zephyr_include_directories(include)
    zephyr_library_sources_ifdef(CONFIG_FOO hal_flash.c)

    in module_repo/drivers/foo/Kconfig : 

    menuconfig FOO
            bool "foo driver"
            default y
            help
              Enable support for foo driver
    
    if FOO
    
    module = FOO
    module-str = foo
    source "subsys/logging/Kconfig.template.log_config"
    
    endif

    and module_repo/zephyr/module.yml : 

    build:
      cmake: .
      kconfig: drivers/foo/Kconfig
    

    and when I test my application (with CONF_FOO=y in prj.conf of the app) I got this error : 

    src/main.c:9: undefined reference to `hal_flash_flush'

Children
Related