[SOLVED] Update mbed source code and use it with platformio

I am developing with platformio on eclipse with following configuration

[env:nucleo_f401re]
platform = ststm32
framework = mbed
board = nucleo_f401re

In my project i am using RTC functions with clock backed up by a battery so that time is remembered after power off. It seems like stm mbed has problems in such case (refer to the last text on this page How to enable Nucleo's internal lowspeed crystal? - Question | Mbed).

…STM mbed code all reset the RTC upon initializing the RTC the first time in a program…

This causes the RTC to not be remembered and start from the beginning.

The solution to that is following fix Nucleo_RTC_battery_bkup_pwr_off_okay - In the past, you need modify rtc_api.c in mbed-de… | Mbed.

I can confirm that the fix works since i tested it with the mbed online compiler. The problem is that for the fix to work one should depend on mbed-dev (mbed-dev - mbed library sources. Supersedes mbed-src. | Mbed) which supersedes mbed-src and needs to delete the file rtc_api.c and replace it with (in my case) rtc_api_F4xx.c.

The problem is i do not know how to achieve this with platformio.

  1. How can i use mbed-dev with platformio, or is this version of mbed already used?
    EDIT: as per following links
    Access to the target folders of the mbed library - Question | Mbed
    Out of date? - Question | Mbed
    MBED-SRC is not updated after updating the MBED compiled library. - Question | Mbed
    How do I find "all" MBED library sources. - Question | Mbed
    i can see that the mbed-dev (and not anymore the mbed-src) is the official source against which the mbed library is build, so i think platformio is already using this version. Hence the only opened question is now the second question beneath.
  2. How to replace the file and build with the updated version?

Thanks

mbed version 122 is due to be released soon, does this fix exist in release branch?

I use unreleased version by manually compiling mbed library from GitHub - ARMmbed/mbed-os: Arm Mbed OS is a platform operating system designed for the internet of things and placing it manually in my ~/.platformio/packages/framework-mbed.

Hi Zgoda,

thanks for your reply. Can you explain me the steps how do you manually compile mbed, and what do you replace?

Thanks

Clone mbed repo from GitHub. Install Python and other required things as described in mbed-os/README.md at master · ARMmbed/mbed-os · GitHub. Handbook covers build system in mbed tools - Handbook | Mbed but there have been some changes recently, you have to activate mbed virtualenv and then run tools/build.py specifying target platform and toolchain (-m and -t command line parameters). You will find compiled library in .build subdirectory. Copy content of .build/mbed to appropriate variant directory in your .platformio/packages/framework-mbed/variant (you may want to empty this directory before).

Hi zgoda,

thanks for the great help. As per your instructions i could sucesfully build mbed from the changed source. The compiler i used:

from the file settings.py
# GCC ARM
GCC_ARM_PATH = "/usr/local/Cellar/gcc-arm-none-eabi/20140805/bin"

and could build with
tools/build.py -m NUCLEO_F401RE -t GCC_ARM

after that i deleted .platformio/packages/framework-mbed/variant/NUCLEO_F401RE/mbed

and pasted under
.platformio/packages/framework-mbed/variant/NUCLEO_F401RE/

the mbed folder that was generated as you said under
/mbed/.build/mbed having the same folder structure as before under .platformio

Now when i run i project in eclipse with platformio i get

src/main.cpp:43:1: error: 'Serial' does not name a type
Serial pc(USBTX, USBRX);
^

Did you get same errors? What could be the problem here?

Thanks

Hey @valeros,

Could you explain @mbed-dev how to use our mbed package builder?

Thanks.

Note that this version of script will work only with mbed rev. 121 (current) and earlier. There are changes to build tools and directory structure in rev. 122.

I think you mean the script mbed_to_package.py.

What is this script for and how can i use it so to fix the problem i had? I am still stuck on this thing. Thanks

To make it run with rev. 122 replace all occurences of “workspace_tools” with “tools” and create empty file __init__.py in mbed source root. Then run:

$ python scripts/mbed_to_package.py --mbed=<mbed_src_dir> --output=<output_dir>

This takes long time because code for all boards is built.

EDIT: the board filter does not seem to work as I expected, I guess we have to build framework for all boards anyway.

Hi zgoda,

i tried as follows:

  1. cloned git from GitHub - ARMmbed/mbed-os: Arm Mbed OS is a platform operating system designed for the internet of things (under
    Releases · ARMmbed/mbed-os · GitHub i see that mbed_lib_rev121 was released on 26 May so i think what i cloned is revision 122. Please correct me if i am wrong)

2.cloned platformio from GitHub - platformio/platformio-core: Your Gateway to Embedded Software Development Excellence 👽

3.created a folder named ~/project2 inside which i started a python virtual environment. Everything OK

4.as before i update ~/git-repos/mbed-src/tools/settings.py with following line: GCC_ARM_PATH = "/usr/local/Cellar/gcc-arm-none-eabi/20140805/bin"

5.inside the virtual environment i run pip install -r ~/git-repos/mbed-src/requirements.txt. Everything OK

6.changed ~/git-repos/platformio-src/scripts/mbed_to_package.py as you said plus i changed also the folder named “build” to “.build” in lines 69 and 71 becuase of the ERROR 1 beneath. And created the empty file ~/git-repos/mbed-src/__init__.py

7.i did run than python ~/git-repos/platformio-src/scripts/mbed_to_package.py --mbed=~/git-repos/mbed-src --output=~/project2/built_result

8.i saw often similiar lines like
LPC810 doesn’t support --rtos library!
LPC810 doesn’t support --eth library!
LPC810 doesn’t support --usb library!

but i think that is ok right? Nevertheles the build worked or at least it did not yield any errors.

now i got as result the folders structure

~/project2/
    -boards.txt (EMPTY FILE)
    -libs
    -variant <-- THIS FOLDER IS EMPTY

and under

~/git-repos/mbed-src/.build/
    -export
    -mbed (this folder structure is a little bit different from "~/.platformio/packages/framework-mbed/variant/NUCLEO_F401RE/mbed" 
           or "~/.platformio/packages/framework-mbed/variant" 
           so i do not know what to copy if i am supposed to
    -net (EMPTY FOLDER  WHICH I THINK IS OK SINCE HERE WAS eth WHICH WAS COPIED OVER TO ~/project2/built_result/libs)

The question is where is the thing i am missing?

ERROR 1 (referenced from above)

Moving generated libraries to framework dir...
Traceback (most recent call last):
  File "~/git-repos/platformio-src/scripts/mbed_to_package.py", line 120, in <module>
    sys_exit(main(args["mbed"], args["output"]))
  File "~/git-repos/platformio-src/scripts/mbed_to_package.py", line 107, in main
    copylibs(mbed_dir, output_dir)
  File "~/git-repos/platformio-src/scripts/mbed_to_package.py", line 71, in copylibs
    move(join(mbed_dir, "build", lib), libs_dir)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: '~/git-repos/mbed-src/build/dsp'

Yea, I found this script does not work with rev. 122, “variants” directory is empty, as well as file boards.txt. I guess the warnings “Skipped board: …” are related to this.

i tried also with revision 121.
i downloaded and extracted “mbed_lib_rev121.zip” from Releases · ARMmbed/mbed-os · GitHub. I changed “tools” to “workspace_tools” as it was in the original version of “mbed_to_package.py” but i let the folder “.build” stay as without the point “.” i got same error as above.

Than i did the same steps as above ending with the same result.

I know platformio works well with mbed since i am using it in the original version and have no problems at all, but how come that i can not build the mbed framework from the sources? Are you using other scripts that builds the framework or is “mbed_to_package.py” the one that platformio also uses for itsself?

while building i do not see any errors. i just get lines like

Processing board: SAML21J18A

  • SAML21J18A doesn't support --rtos library!
    
  • SAML21J18A doesn't support --eth library!
    
  • SAML21J18A doesn't support --usb library!
    

Warning! Skipped board: SAML21J18A
Processing board: SAMR21G18A

  • SAMR21G18A doesn't support --rtos library!
    
  • SAMR21G18A doesn't support --eth library!
    
  • SAMR21G18A doesn't support --usb library!
    

Warning! Skipped board: SAMR21G18A
Processing board: NRF51_DONGLE

  • NRF51_DONGLE doesn't support --rtos library!
    
  • NRF51_DONGLE doesn't support --eth library!
    
  • NRF51_DONGLE doesn't support --usb library!
    

Warning! Skipped board: NRF51_DONGLE
Processing board: LPC11U35_401

  • LPC11U35_401 doesn't support --eth library!
    

Warning! Skipped board: LPC11U35_401

and at the end i can read

Moving generated libraries to framework dir…
Complete!

Hi @anteo.c!
Sorry for the late reply. mbed_to_package.py should work without errors with 121 revision and the output above looks good to me. Is output directory still empty with 121 revision? Could you please try to specify the full path to src and output dirs without ~ symbol?

python mbed_to_package.py --mbed=/home/user/git-repos/mbed-src --output=/home/user/framework-mbed

If it still fails I can generate variant for your board with rtc_api_F4xx.c file.
Sorry again for the inconvenience.

Hi @valeros,

so to make sure i tried everything again with following folder structure

  1. unziped mbed rev121 under /Users/caliqi/dev/mbed-updated/mbed-mbed_lib_rev121
  2. cloned platformio under /Users/caliqi/dev/mbed-updated/platformio-src
  3. created a new folder /Users/caliqi/dev/mbed-updated/mbed-new/built_result where the result should be copied
  4. updated /Users/caliqi/dev/mbed-updated/platformio-src/scripts/mbed_to_package.py only lines 69 and 71 “build” → “.build” (it seems like the missing point is a bug maybe !)
  5. created empty file “__init__.py” inside /Users/caliqi/dev/mbed-updated/mbed-mbed_lib_rev121
  6. updated /Users/caliqi/dev/mbed-updated/mbed-mbed_lib_rev121/workspace_tools/settings.py line 54 with GCC_ARM_PATH = "/usr/local/Cellar/gcc-arm-none-eabi/20140805/bin"

i do not know if steps 5 and 6 are needed for this kind of build but i did it based on zgoda’s advice

folder structure is now

-mbed-updated
   -mbed-mbed_lib_rev121
   -mbed-new
        -built_result
   -platformio-src

here all the commands that i run afterwards:

  1. cd dev/mbed-updated/mbed-new/
  2. virtualenv venv --distribute --system-site-packages
  3. source venv/bin/activate
  4. pip install -r /Users/caliqi/dev/mbed-updated/mbed-mbed_lib_rev121/requirements.txt
  5. python /Users/caliqi/dev/mbed-updated/platformio-src/scripts/mbed_to_package.py --mbed=/Users/caliqi/dev/mbed-updated/mbed-mbed_lib_rev121 --output=/Users/caliqi/dev/mbed-updated/mbed-new/built_result

at the end

Moving generated libraries to framework dir…
Complete!

but the folder "/Users/caliqi/dev/mbed-updated/mbed-new/built_result/variant" is empty and the file "/Users/caliqi/dev/mbed-updated/mbed-new/built_result/boards.txt" also.

If you could generate the variant with the updated file that would be great. Please do not forget to update the first line of rtc_api_F4xx.c

I would also want to be able to generate it from my computer so if i am doing something wrong please inform me

Here is the prepared variant folder for your nucleo_f401re. Could you test it, please?

Here is what I did without waiting for all boards (everything from mbed virtual env and in workspace folder):

  • Copy rtc_api_F4xx.c to /mbed-dir/targets/hal/TARGET_STM/TARGET_STM32F4/
  • Delete first and the last line in this file.
  • Delete rtc_api.c file.
  • Run python build.py -m NUCLEO_F401RE -t GCC_ARM
  • Run python project.py -m NUCLEO_F401RE -i gcc_arm -p 3 -b
  • You will have file MBED_A4_gcc_arm_NUCLEO_F401RE.zip in /mbed-dir/.build/export/
  • Copy folder mbed to you variant.

Hi @valeros, @zgoda, @ivankravets

thanks for the great help. I can confirm the steps from @valeros post above work.

For the others who may read this post:
I did not run python project.py -m NUCLEO_F401RE -i gcc_arm -p 3 -b but just copied the mbed folder from the “.build” directory.

This is also what i tried the first time to build mbed. The only difference was that i was trying with last code from mbed git repository (maybe rev 122) and i was trying to run the commands not from workspace_tools folder (in that revision only “tools” folder) but from another folder outside git source. Nevertheless the build worked only platformio could not recognize Serial as i posted in one of my posts above

src/main.cpp:43:1: error: ‘Serial’ does not name a type
Serial pc(USBTX, USBRX);
^

i think this was due to the mbed revision.

Anyway thanks for the help. I will mark this as solved.

EDIT: where can i mark this as solved? is this possible at all :wink:

You can edit the subject and add prefix [solved]. However, that is not required step.

P.S: I hope that we will find time and write own native builder for ARM mbed from the source code without pre-compilation.

Happy coding!