[SOLVED] #include "tusb.h" not working

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
hippy
Posts: 15
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

[SOLVED] #include "tusb.h" not working

Post by hippy » Wed Apr 21, 2021 1:36 pm

Using recent builds I have found that using #include "tusb.h" in any 'modXXX.c' module fails -

Code: Select all

[  5%] Generating genhdr/qstr.i.last
/home/pi/pico/micropython/ports/rp2ffs/moduos.c:33:10: fatal error: tusb.h: No such file or directory
 #include "tusb.h"
          ^~~~~~~~
compilation terminated.
Traceback (most recent call last):
  File "/home/pi/pico/micropython/py/makeqstrdefs.py", line 188, in <module>
    preprocess()
    ... snip ...
The same #include "tusb.h" in 'main.c' works, and it works in 'modXXX.c' files if I use a quite old '.cmake' file, so this seems to be a CMake related issue.

Does anyone know what I need to add to allow #include "tusb.h" to be used in modules as before ?
Last edited by hippy on Wed Apr 21, 2021 4:32 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 2642
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: #include "tusb.h" not working

Post by Roberthh » Wed Apr 21, 2021 1:54 pm

You have to update the submodules.

hippy
Posts: 15
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: #include "tusb.h" not working

Post by hippy » Wed Apr 21, 2021 2:05 pm

I am already doing as below. Is there more that needs to be done ...?

Code: Select all

cd ~/pico/micropython
git pull
git submodule update --init lib/pico-sdk lib/tinyusb
That #include "tusb.h" works in 'main.c' but not in 'modXXX.c' suggests to me there is something beyond a submodule issue.

User avatar
Roberthh
Posts: 2642
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: #include "tusb.h" not working

Post by Roberthh » Wed Apr 21, 2021 2:14 pm

AFAIK, I had to deinit these submodules first and then update them again.

hippy
Posts: 15
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: #include "tusb.h" not working

Post by hippy » Wed Apr 21, 2021 2:37 pm

Did a 'deinit', it reported both 'lib/pico-sdk' and 'lib/tinyusb' as cleared. Did an 'update' and both checked-out. Same issue as before.

In the crash during "Generating genhdr/qstr.i.last" it reports "subprocess.CalledProcessError: Command '['/usr/bin/arm-none-eabi-gcc', '-E', '-I/home/pi/pico/micropython', '-I/home/pi/pico/micropython/lib/berkeley-db-1.xx/PORT/include', '-I/home/pi/pico/micropython/ports/rp2ffs/boards/PICO', ..." a long list of '-I' directories.

There is a '-DTINYUSB_DEVICE_LINKED=1', amongst that but no '/lib/tinyusb' only '/lib/berkeley-db-1.xx', '/lib/pico-sdk', 'lib/oofatfs', '/lib/utils'.

My guess would be 'gcc -E ...' isn't getting told it should be looking in '/lib/tinyusb' so cannot find 'tusb.h'.

Update : I think that is it ...

The only "tusb.h" I can find in the MicroPython tree is -

/home/pi/pico/micropython/lib/tinyusb/src/tusb.h

What seems to invoke "Generating genhdr/qstr.i.last" is -

/home/pi/pico/micropython/py/mkrules.cmake

With, line 72 -

COMMAND ${Python3_EXECUTABLE} ${MICROPY_PY_DIR}/makeqstrdefs.py pp ${CMAKE_C_COMPILER} -E output ${MICROPY_GENHDR_DIR}/qstr.i.last cflags ${MICROPY_CPP_FLAGS} -DNO_QSTR cxxflags ${MICROPY_CPP_FLAGS} -DNO_QSTR sources ${MICROPY_SOURCE_QSTR}

I added "-I/home/pi/pico/micropython/lib/tinyusb/src" to the end of that line

That allowed "Generating genhdr/qstr.i.last" and the build to complete with no errors. So, it seems to me, TinyUSB isn't included in {MICROPY_SOURCE_QSTR} when it needs to be

I'm not currently sure where that list is created but I think we are heading in the direction of where the issue lies.
Last edited by hippy on Wed Apr 21, 2021 3:19 pm, edited 2 times in total.

User avatar
Roberthh
Posts: 2642
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: #include "tusb.h" not working

Post by Roberthh » Wed Apr 21, 2021 3:17 pm

I can build that without any changes to scripts. So maybe you start over with:

make clean

and

make

I had that problem too initially, but that went away after Feb 12 with the. I cannot find the discussion I had about that any more.
maybe related:
https://github.com/micropython/micropython/pull/6791
https://github.com/micropython/micropython/pull/6835

hippy
Posts: 15
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: #include "tusb.h" not working

Post by hippy » Wed Apr 21, 2021 3:24 pm

Roberthh wrote:
Wed Apr 21, 2021 3:17 pm
I can build that without any changes to scripts.
But are you adding any #include "tusb.h" ? My builds are fine if I don't, it's only when I do I have problems.

Perhaps you could add #include "tusb.h" to 'moduos.c' and see if it still builds or fails as it does for me.

I am pretty certain it's not a cleaning or caching issue as I am using the nuclear option every time -

Code: Select all

rm -r build
mkdir build
cd build
cmake ..
make
It seems that {MICROPY_SOURCE_QSTR} is created on a per port basis, from 'CMakeLists.txt' ...

Code: Select all

set(MICROPY_SOURCE_QSTR
    ${MICROPY_SOURCE_PY}
    ${MICROPY_SOURCE_EXTMOD}
    ${MICROPY_SOURCE_USERMOD}
    ${MICROPY_DIR}/lib/utils/mpirq.c
    ${MICROPY_DIR}/lib/utils/sys_stdio_mphal.c
    ${PROJECT_SOURCE_DIR}/machine_adc.c
    ${PROJECT_SOURCE_DIR}/machine_i2c.c
    ... snip ...
    ${PROJECT_SOURCE_DIR}/rp2_pio.c
)
I don't know how that would need to be updated to include TinyUSB.

Added : I'm now thinking it's not that, not MICROPY_SOURCE_QSTR per se; that's a list of where QSTR are used, not a list of directories for '-I'. I am not really that knowledgeable about the MicroPython build steps.

User avatar
Roberthh
Posts: 2642
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: #include "tusb.h" not working

Post by Roberthh » Wed Apr 21, 2021 4:03 pm

I simply call make in the ports/rp2 directory. By default there is a Makefile as wrapper to cmake.

Edit: I do not add any include "tusb.h". No need to fo so.

hippy
Posts: 15
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: #include "tusb.h" not working

Post by hippy » Wed Apr 21, 2021 4:32 pm

Roberthh wrote:
Wed Apr 21, 2021 4:03 pm
Edit: I do not add any include "tusb.h". No need to fo so.
There absolutely is that need if adding or updating a MicroPython C Extension Module to use functions which only TinyUSB provides - Which is what I am doing

I do finally have a fix for the RP2 port so I can leave the rest alone, just one line to add -

~/pico/micropython/ports/rp2/CMakeLists.txt - Line 176

Code: Select all

target_include_directories(${MICROPY_TARGET} PRIVATE
    # Added the line below ...
    ${MICROPY_DIR}/lib/tinyusb/src
    ${MICROPY_INC_CORE}
So that means I can get on with what I'm doing without changes to any other MicroPython CMake files. I will leave it to others to decide if there is an issue or not, how it should be properly resolved if there is.

Post Reply