Try to isolate some core files of Teensy for development

I’m trying to isolate a few files in the Teensy core code for some development in one project, while trying not to change them for other projects. Say the usb.c code, I want to add some features to it. Can I copy it to my src folder and make changes? Will this file in my src block the copy in core to be compiled? If I only worked on one computer and do it only locally, it would be OK to just change this file in the core folder. But I work on multiple computers and keep my project on git. So I need a way to keep the core code change in git as well. Thanks.

Advanced scripting can do that. Have a look at Build Middlewares — PlatformIO latest documentation with regards to the

Import("env")
def replace_node_with_another(node):
    return env.File("path/to/patched/RtosTimer.cpp")

env.AddBuildMiddleware(
    replace_node_with_another,
    "framework-mbed/rtos/RtosTimer.cpp"
)

code.

Thanks @maxgerhardt I kind of get the idea of the replacement but am just too new to this to implement it. Where do you store this python script, with platformio.ini? When I kept it in src, build complains not finding it so I moved it to project top folder with platformio.ini and the warning of not finding the file goes away.

Here is what I added to my platformio.ini

extra_scripts =
  pre:pre_extra_script.py

Here is what I wrote:

Import("env")

def replace_node_with_another(node):

    return env.File("../libs/USBDevice/usb_desc.h")

env.AddBuildMiddleware(

    replace_node_with_another,

    "framework-arduinoteensy/cores/teensy4/usb_desc.h"

)

I placed a #warning in this header that should be triggered when it is processed but it never printed out so I worry that the original file in the core folder was used. I mocked up the source, which starts with framwork- from the doc you pointed to so if this is not right. I also tried adding more path such as packages in front of the framework- and still the #warning wasn’t triggered. So I wonder maybe this mechanism doesn’t work with .h files. So I tried usb_desc.c file with a #warning in it. No luck. It’s never printed out. So I’m down to my last resort, move the core folder inside my project repo and create a link called core to point to the files in my repo. Finally it worked. Saw both #warning messages for .h and .c. I guess the teensy arduino core build system is just that it always looks for these files in the core folder regardless the extra file stuff you put in. Maybe for another platform it’d be different.

Mhm… The files that are built / compiled (“nodes”) to object files are .c / .cpp files, not header files. Replacing header files like that won’t work. You would have to use env.AddBuildMiddleware with every .c / .cpp file that uses usb_desc.h.

Since this may be unfeasable due to the amount of files, I suggest you redirect the framework-arduinoteensy package to be within your project using platform_packages. That is, e.g. create a new folder modified_core and inside there copy the current (unmodified) contents of the <home folder>/.platformio/packages/framework-arduinoteensy folder. Then add platform_packages = framework-arduinoteensy@file://modified_core to the platformio.ini so that PlatformIO uses the Arduino core stored inside modified_core. Adapt the needed files in modified_core/ and it should work.

Thanks! I’ll try that on a separate computer to make sure it work before changing on my main dev machine. Also there are 3600 files in that package so it’s a bit of a burden to include it in my repo. As of now, I only symlinked to the necessary core folder to the actual folder inside my repo, a much smaller folder inside the package.