A while back I was working on I2C and came across the compiler error " 'NRFX_TWI0_INST_IDX' undeclared ", which seemed to be related to NRF drivers vs NRFX drivers confusion. I recall researching here and not finding anything. I am sorry I don't recall every little detail, but still wanted to share:
I wonder whether there is a bug in apply_old_config.h line 1116 in SDK16.0.0
It reads "#if defined(TWI_ENABLED)", and in this case #undef's a number of NRFX related defines.
Since in the sdk_config.h file (at least in many examples), TWI_ENABLED is almost always #defined (just sometimes a 0), the above statement is almost always true.
I believe the line should read something along the lines of
#if defined(TWI_ENABLED) && (TWI_ENABLED==1)
I also recall, that apply_old_config.h is unconditionally included through many levels, starting with #include "nrfx_twi.h" in my main.c and cascading into nrfx_glue.h . Ie, you can't avoid the line above in apply_old_config.h .
An alternate solution for me was, to change the sdk_config.h line
// <e> TWI_ENABLED - nrf_drv_twi - TWI/TWIM peripheral driver - legacy layer//==========================================================#ifndef TWI_ENABLED#define TWI_ENABLED 0#endif
//#define TWI_ENABLED 0
Hope this is of help to someone. And maybe Nordic can review the #define in apply_old_config.h
I think the intention is that the legacy definitions should be the dominant ones, meaning that if both TWI_ENABLED and NRFX_TWI_ENABLED are defined, it should always choose TWI_ENABLED. No matter if TWI_ENABLED is set to 0 or 1.
That is the reason #if defined(TWI_ENABLED) is used. It will check if it is defined, and change the NRFX_TWI_ENABLED accordingly.
I had the impression that the way to configure sdk_config.h was to enable and disable functionality. Even example applications contain both the NRF and the NRFX defines in there. The fact that the pure presence of a define (even if defined as 0) has implications was unexpected to me. I feel example apps should then either contain NRF or NRFX defines. My 2 cents.
Take a look at the answer given by Simonr in this thread, where he provides an explanation of why both the NRFX and the legacy layer are present. Einar Thorsrud elaborates on this as well in this thread.
Hopefully, this answers your questions.
So if I read the links you provided below, and in particular the definition of "legacy", is the Nordic suggested way then to
* use nrf_drv_twi.h, the "legacy" driver, and to
* include nrfx_twi.c in the project
Ie, to NOT use nrfx_twi.h directly ?
(and similarly use nrf_drv_spi.h together with nrfx_spi.c) ? Or am I confused now ?
Yes, you have understood it correctly, don't use the NRFX layer directly. The NRFX source files should be included in the project since they are used by the legacy functions.
You can see that the NRFX layer is used similarly in the nRF Connect SDK (one of our newest projects), where Zephyr functions are used as the top layer.