[BUG] unable to call spi_register() from C++ code due to inappropriate declaration
Description / Steps to reproduce the issue
In the spi_transfer.h header file, https://github.com/apache/nuttx/blob/c38042941fe984a7f89d3be68b410862c5b54398/include/nuttx/spi/spi_transfer.h#L163, the declaration of spi_register() function is not wrapped into extern "C" directives.
#ifdef CONFIG_SPI_DRIVER
int spi_register(FAR struct spi_dev_s *spi, int bus);
#endif
Therefore, when one tries to use spi_register() from within C++ source code file, linker does not find this function because it seeks for its mangled name, which is not present in the collection of nuttx object file which is provided to linker during link process. To reproduce, one can just create any C++ source file, add it to the CXXSRCS variable in Make file, and attempt to invoke spi_register() from it. It should be wrapped inside extern "C" directives
On which OS does this issue occur?
[OS: Linux]
What is the version of your OS?
Linux Debian
NuttX Version
master, 12.12, and previous versions
Issue Architecture
[Arch: all]
Issue Area
[Area: Drivers]
Host information
No response
Verification
- [x] I have verified before submitting the report.
@VitalyS83 Application code cannot call spi_register() directly, it is a violation of POSIX abstraction. You need to create an SPI driver on NuttX kernel and your application will open the device file.
Another alternative is exporting the SPI Bus as char device, look at nuttx-apps/system/spi to understand how it works.
In various examples under nuttx/boards/arm folder, function spi_register() is called from user-provided spi bus initialization source files, which are in C. For instance, nuttx/boards/arm/sam34/arduino-due/src/sam_bringup.c and others. Are these board initialization files, related to SPI setup, considered to be part of NuttX kernel? I would better treat them as not part of kernel, but part of application setup process. I wanted to do basically the same for my own board (device), but in C++. It seems illogical for me, that I must either write my own initialization file(s) in C or manually wrap #include <nuttx/spi/spi_transfer.h> into extern "C"
@VitalyS83 code inside nuttx/boards/ runs in kernel mode, not userspace. The board bring-up needs to be done by kernel, userspace cannot "see" the low level implementatation, otherwise your application will be board dependent and the idea of NuttX is abstract it. The same application should work in different boards if the equivalent drivers are enabled and initialized.
@acassis, thank you for clarifications. At the end, I must provide board initialization (bring-up) files for my own PCB (device), call spi_register() from them, but do not consider these board initialization code as actual part of my application code, but as part of board setup package code which runs in kernel mode. Ok, now I understand.