Unit tests: how to mock nrf_drv_gpiote.h ? (_WIN32 prevents nrf.h to include my platform bitfield header)

TimF gravatar image

asked 2017-09-08 15:20:28 +0200

updated 2017-09-13 15:04:18 +0200


I'm using Ceedling, Unity + CMock to implement unit tests but I'm running into issues when it comes to mocking nrf_drv_gpiote.h.

I'm trying to test a file, call it foo.c, which includes nrf_drv_gpiote.h, therefore in my test (test_foo.c) I'll include "mock_nrf_drv_gpiote.h".

The problem is that when Ceedling builds the test I have the following kind of errors :

components/drivers_nrf/hal/nrf_gpiote.h:71:32: error: 'GPIOTE_CONFIG_POLARITY_LoToHi' undeclared here (not in a function)

I understand where this error comes from but I don't know how to get rid of it. Basically, 'GPIOTE_CONFIG_POLARITY_LoToHi' is defined in nrf52840_bitfields.h, which is included by nrf.h if _WIN32 is not defined (see the code below), but _WIN32 is automatically defined when building on Windows. So how can I run my unit test on Windows if nrf.h doesn't allow me to include what's necessary to mock nrf_drv_gpiote.h ?

As a side note, I know that some people already requested this on this forum but if you could provide a minimalist Ceedling environment (or any other test framework) for unit tests it would be greatly appreciated !

Code in nrf.h that prevents from including nrf52840_bitfields.h :

    if defined(_WIN32)
    /* Do not include nrf specific files when building for PC host */
#elif defined(__unix)
    /* Do not include nrf specific files when building for PC host */
#elif defined(__APPLE__)
    /* Do not include nrf specific files when building for PC host */

    /* Device selection for device includes. */
    #if defined (NRF51)
        #include "nrf51.h"
        #include "nrf51_bitfields.h"
        #include "nrf51_deprecated.h"
    #elif defined (NRF52840_XXAA)
        #include "nrf52840.h"
        #include "nrf52840_bitfields.h"
        #include "nrf51_to_nrf52840.h"
        #include "nrf52_to_nrf52840.h"
    #elif defined (NRF52832_XXAA)
        #include "nrf52.h"
        #include "nrf52_bitfields.h"
        #include "nrf51_to_nrf52.h"
        #include "nrf52_name_change.h"
        #error "Device must be defined. See nrf.h."
    #endif /* NRF51, NRF52832_XXAA, NRF52840_XXAA */

    #include "compiler_abstraction.h"

#endif /* _WIN32 || __unix || __APPLE__ */
edit retag flag offensive close delete report spam

1 answer

Sort by » oldest newest most voted
MartinBL gravatar image

answered 2017-09-12 09:06:43 +0200

updated 2017-09-14 12:09:45 +0200


We have had this issue too and our current workaround is to make a new nrf.h file for the host and then include that in the unit tests. You will find the new file in the folder "mesh/test/include/device_wrappers" and it should also make it possible to mock peripherals through nrf_mesh_cmsis_mock.h.

edit flag offensive delete publish link more


Hi Martin, thank you for your answer. I couldn't find the folder "mesh/test/include/device_wrappers" nor the file nrf_mesh_cmsis_mock.h both in the SDK 13.0.0 and 14.0.0. Is it possible that those aren't distributed with the SDK but can be found on GitHub ?

Tim ( 2017-09-12 09:38:30 +0200 )editconvert to answer

It is in mesh SDK v 0.9.2.

Martin Børs-Lind ( 2017-09-12 10:04:13 +0200 )editconvert to answer

Thank you, that was a pain but I finally managed to get that working. I had not understood that the files in device_wrappers were used in addition to the SDK ones, I've tried to replace the ones from the SDKs with those and it obviously didn't work. I also had to add two defines : HOST and SUPPRESS_INLINE_IMPLEMENTATION and to replace the nRF51 includes in the new nrf.h with the ones for nrf52.

Tim ( 2017-09-14 11:27:14 +0200 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools

1 follower


Asked: 2017-09-08 15:20:28 +0200

Seen: 76 times

Last updated: Sep 14