Recent

Author Topic: Setting up Xtensa-FreeRTOS for ESP8266  (Read 21807 times)

botster

  • New Member
  • *
  • Posts: 39
Setting up Xtensa-FreeRTOS for ESP8266
« on: January 04, 2022, 02:46:46 am »
I am trying to set up FPC to use the Xtensa-FreeRTOS for ESP8266 by following the instructions at the Xtensa Wiki page.

I have installed the ESP8266-RTOS-SDK and successfully compiled and executed the hello_world example.

But, I get to the Build FPC section and read, "Change into the fpc directory [..]" What fpc directory? I don't see anywhere in the instructions where I was told to create one.

Reading further, in the Compile and run test program section, I read, "Create a hello world program [...]" How do I do that? Is there an example somewhere? Would it be something as simple as:
Code: Pascal  [Select][+][-]
  1. program Hello;
  2. begin
  3.   writeln ('Hello, world.');
  4. end.
  5.  
?
Or, how would I translate the FreeRTOS example (main program attached) to FreePascal? Is there an API reference?

Maybe I'm just dense because I find the instructions quite confusing.
Lee

ccrause

  • Hero Member
  • *****
  • Posts: 688
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #1 on: January 04, 2022, 06:59:42 am »
I am trying to set up FPC to use the Xtensa-FreeRTOS for ESP8266 by following the instructions at the Xtensa Wiki page.

I have installed the ESP8266-RTOS-SDK and successfully compiled and executed the hello_world example.

But, I get to the Build FPC section and read, "Change into the fpc directory [..]" What fpc directory? I don't see anywhere in the instructions where I was told to create one.

Indeed not clear, I agree. Xtensa is only supported in the development version of fpc, so a recent copy of the main branch is required. See https://www.freepascal.org/develop.html for different ways to get the development version source - I suggest using git as this way updating the source is relatively easy once configured. Or use fpcupdeluxe which automates the whole process of downloading fpc source and building the cross compiler.
Quote

Reading further, in the Compile and run test program section, I read, "Create a hello world program [...]" How do I do that? Is there an example somewhere? Would it be something as simple as:
Code: Pascal  [Select][+][-]
  1. program Hello;
  2. begin
  3.   writeln ('Hello, world.');
  4. end.
  5.  
?

Yes, as easy as that.
Quote

Or, how would I translate the FreeRTOS example (main program attached) to FreePascal? Is there an API reference?

Maybe I'm just dense because I find the instructions quite confusing.
Some of the SDK has been translated in this project. Have a look at the examples to see what is possible at the moment. Not fully updated, so feel free to post issues against the project.

af0815

  • Hero Member
  • *****
  • Posts: 1106
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #2 on: January 04, 2022, 09:32:21 am »
We have here a similar sounding thread https://forum.lazarus.freepascal.org/index.php/topic,57704.msg429396.html#msg429396

Actual i can compile with fpc on Windows 10 but not linking.

Edit:
Quote
Or use fpcupdeluxe which automates the whole process of downloading fpc source and building the cross compiler.
fpcupdeluxe load his own (older ?) toolchain and this have some problems with the esp8266. It looks it is more tested for the stm32, not for esp8266. Because it will use the stm32 crosscompiler (lx6) instead of the lx106 crosscompiler.
« Last Edit: January 04, 2022, 09:38:41 am by af0815 »
regards
Andreas

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1594
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #3 on: January 04, 2022, 09:53:31 am »
 I can confirm this issue for lx106. Will look into it.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1594
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #4 on: January 04, 2022, 10:10:24 am »
Update.

This issue cannot be resolved for Windows at the moment unfortunately.
Precompiled tools from GitHub are used: https://github.com/michael-ring/espsdk4fpc/releases
And these tools are not available for Windows [yet].

However. It should be possible to install the xtensa tools yourself from the appropriate sources on the Web.
And point FPC to use these tools.
https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/get-started/windows-setup.html

af0815

  • Hero Member
  • *****
  • Posts: 1106
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #5 on: January 04, 2022, 12:04:52 pm »
First step to build the crosscompiler is to patch fpcupdeluxe - m_any_to_freertosxtensa.pas
Code: Pascal  [Select][+][-]
  1. function TAny_FreeRTOSXtensa.GetBinUtils(Basepath:string): boolean;
  2. var
  3.   AsFile: string;
  4.   S,PresetBinPath:string;
  5. begin
  6.   result:=inherited;
  7.   if result then exit;
  8.  
  9.   if (FSubArch=TSUBARCH.lx6) then
  10.     FBinUtilsPrefix:=GetCPU(TargetCPU)+'-esp32-elf-';
  11.   if (FSubArch=TSUBARCH.lx106) then
  12.     FBinUtilsPrefix:=GetCPU(TargetCPU)+'-lx106-elf-';
  13.  
  14.   // Start with any names user may have given
  15.   AsFile:=FBinUtilsPrefix+'as'+GetExeExt;
  16.  
  17.  
I have to built the tools from espressif with their mysys32 enviroment.
« Last Edit: January 04, 2022, 12:07:01 pm by af0815 »
regards
Andreas

af0815

  • Hero Member
  • *****
  • Posts: 1106
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #6 on: January 04, 2022, 01:07:12 pm »
I have the helloworld in the SDK under MYSYS32 compiled. The full result is here https://drive.google.com/file/d/1TLg1UTOWFNlaaj-bzS5N5BBKKWivQ3yE/view?usp=sharing
This is the whole HelloWorld including all the files from build.
regards
Andreas

botster

  • New Member
  • *
  • Posts: 39
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #7 on: January 04, 2022, 07:51:44 pm »
Indeed not clear, I agree. Xtensa is only supported in the development version of fpc, so a recent copy of the main branch is required. See https://www.freepascal.org/develop.html for different ways to get the development version source - I suggest using git as this way updating the source is relatively easy once configured. Or use fpcupdeluxe which automates the whole process of downloading fpc source and building the cross compiler.
Ah, okay. So the "fpc directory" should be the "fpc source directory". Got it.

Thank you.

Quote
Some of the SDK has been translated in this project. Have a look at the examples to see what is possible at the moment. Not fully updated, so feel free to post issues against the project.
That looks very interesting. I'll have to delve into it when I have a bit more time.

I see that you have posted regarding an update to the wiki Xtensa instructions. I will comment further over there.
Lee

botster

  • New Member
  • *
  • Posts: 39
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #8 on: January 05, 2022, 12:58:03 am »
Using the updated instructions, I have made progress. Compiling ppcrossxtensa finished with no errors.

Now when I tried to compile the 'hello world' test program the first time, the linker complained that it could not find libc_fnano.a and libstdc++.a.

After removing the 'sysroot' directory (which does not exist) from the last compiler option:
Code: Pascal  [Select][+][-]
  1. -Fl~/esp/xtensa-lx106-elf/xtensa-lx106-elf/sysroot/lib/
The linker was able to find libstdc++.a, but still not libc_fnano.a.

libc_nano.a is in ~/esp/xtensa-lx106-elf/xtensa-lx106-elf/lib but not libc_fnano.a, and I don't know where that comes from as I do not find any ESP8266 or FPC source file that refers to it.

The content of the link*.res file in the project build directory (with paths normalized to the instructions) is:
Code: Pascal  [Select][+][-]
  1. SEARCH_DIR("~/esp/xtensa-lx106-elf-libs/")
  2. SEARCH_DIR("~/esp8266/ESP8266_RTOS_SDK/components/esp8266/lib/")
  3. SEARCH_DIR("~/esp/xtensa-lx106-elf/xtensa-lx106-elf/lib/")
  4. SEARCH_DIR("~/fpc/source/rtl/units/xtensa-freertos/")
  5. SEARCH_DIR("~/fpc/source/compiler/")
  6. INPUT (
  7. hello.o
  8. ~/fpc/source/rtl/units/xtensa-freertos/system.o
  9. ~/fpc/source/rtl/units/xtensa-freertos/esp8266.o
  10. ~/fpc/source/rtl/units/xtensa-freertos/consoleio.o
  11. ~/fpc/source/rtl/units/xtensa-freertos/heapmgr.o
  12. )
  13. GROUP(
  14. ~/esp/xtensa-lx106-elf-libs/libesp8266.a
  15. ~/esp/xtensa-lx106-elf-libs/liblog.a
  16. libc_fnano.a
  17. ~/esp/xtensa-lx106-elf-libs/libnewlib.a
  18. ~/esp/xtensa-lx106-elf-libs/libheap.a
  19. ~/esp/xtensa-lx106-elf-libs/libvfs.a
  20. ~/esp/xtensa-lx106-elf-libs/libesp_common.a
  21. ~/esp8266/ESP8266_RTOS_SDK/components/esp8266/lib/libcore.a
  22. ~/esp/xtensa-lx106-elf-libs/libfreertos.a
  23. ~/esp8266/ESP8266_RTOS_SDK/components/esp8266/lib/libphy.a
  24. ~/esp8266/ESP8266_RTOS_SDK/components/esp8266/lib/libnet80211.a
  25. ~/esp8266/ESP8266_RTOS_SDK/components/esp8266/lib/libhal.a
  26. ~/esp/xtensa-lx106-elf-libs/libnvs_flash.a
  27. ~/esp8266/ESP8266_RTOS_SDK/components/esp8266/lib/librtc.a
  28. ~/esp/xtensa-lx106-elf-libs/libspi_flash.a
  29. ~/esp/xtensa-lx106-elf-libs/libesp_ringbuf.a
  30. ~/esp8266/ESP8266_RTOS_SDK/components/esp8266/lib/libgcc.a
  31. ~/esp8266/ESP8266_RTOS_SDK/components/esp8266/lib/libpp.a
  32. ~/esp/xtensa-lx106-elf/xtensa-lx106-elf/lib/libstdc++.a
  33. ~/esp/xtensa-lx106-elf-libs/libpthread.a
  34. )
  35. SECTIONS
  36. {
  37.   .data :
  38.   {
  39.     KEEP (*(.fpc .fpc.n_version .fpc.n_links))
  40.   }
  41. }
  42.  

Is the 'f' in libc_fnano.a perhaps a typo that crept in somewhere?
What can I do to help diagnose this issue?
« Last Edit: January 05, 2022, 12:59:37 am by botster »
Lee

ccrause

  • Hero Member
  • *****
  • Posts: 688
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #9 on: January 05, 2022, 07:43:19 am »
Using the updated instructions, I have made progress. Compiling ppcrossxtensa finished with no errors.

Now when I tried to compile the 'hello world' test program the first time, the linker complained that it could not find libc_fnano.a and libstdc++.a.

After removing the 'sysroot' directory (which does not exist) from the last compiler option:
Code: Pascal  [Select][+][-]
  1. -Fl~/esp/xtensa-lx106-elf/xtensa-lx106-elf/sysroot/lib/
The linker was able to find libstdc++.a, but still not libc_fnano.a.

libc_nano.a is in ~/esp/xtensa-lx106-elf/xtensa-lx106-elf/lib but not libc_fnano.a, and I don't know where that comes from as I do not find any ESP8266 or FPC source file that refers to it.

This is a dependency change from esp8266-rtos-sdk v3.3 to v3.4, perhaps due to the newer gcc toolchain.  I'm busy testing a patch so that the compiler can generate the correct library requirements based on the SDK version.

Edit: proposed fix
« Last Edit: January 05, 2022, 08:03:12 am by ccrause »

botster

  • New Member
  • *
  • Posts: 39
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #10 on: January 05, 2022, 06:55:49 pm »
This is a dependency change from esp8266-rtos-sdk v3.3 to v3.4, perhaps due to the newer gcc toolchain.  I'm busy testing a patch so that the compiler can generate the correct library requirements based on the SDK version.

Edit: proposed fix

I looked at that merge request, and it said it had been merged. So, I re-cloned the repo and rebuilt ppcrossxtensa. Compilation of the hello test program failed again with the same linker error:
Code: Pascal  [Select][+][-]
  1. ~/esp/xtensa-lx106-elf/bin/xtensa-lx106-elf-ld: cannot find libc_fnano.a
Lee

ccrause

  • Hero Member
  • *****
  • Posts: 688
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #11 on: January 05, 2022, 08:38:04 pm »
Did you add the command line option -WP3.4?

botster

  • New Member
  • *
  • Posts: 39
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #12 on: January 05, 2022, 09:25:05 pm »
Did you add the command line option -WP3.4?

No, I did not. I didn't realize I needed to. Adding that option appears to have fixed the issue as it compiled and linked with no errors.
(BTW, are these cross-compiler options documented somewhere? All I can find is -Wp which defines "Minimum iOS deployment version" though I'm sure this is for plain-vanilla FPC.)

I proceeded to flash the device (a Devkit v0.9; 2M flash) with:
Code: Pascal  [Select][+][-]
  1. $IDF_PATH/components/esptool_py/esptool/esptool.py --chip esp8266 --port "/dev/ttyUSB1" --baud 921600 --before "default_reset" --after "hard_reset" write_flash -z --flash_mode "dio" --flash_freq "40m" --flash_size "2MB"   0x0 bootloader.bin 0x10000 hello.bin 0x8000 partitions_singleapp.bin

The result:
Code: Pascal  [Select][+][-]
  1. esptool.py v2.4.0
  2. Connecting.....
  3. Chip is ESP8266EX
  4. Features: WiFi
  5. MAC: [retracted]
  6. Uploading stub...
  7. Running stub...
  8. Stub running...
  9. Changing baud rate to 921600
  10. Changed.
  11. Configuring flash size...
  12.  
  13. A fatal error occurred: Invalid head of packet (0x01)
  14.  

Edit:
FYI, I copied partitions_singleapp.bin from ~/esp8266/hello_world/build/ and bootloader.bin from ~/esp8266/hello_world/build/bootloader/
« Last Edit: January 05, 2022, 09:31:37 pm by botster »
Lee

MarkMLl

  • Hero Member
  • *****
  • Posts: 5866
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #13 on: January 05, 2022, 09:56:22 pm »
> A fatal error occurred: Invalid head of packet (0x01)

Noting that your directory names imply that you're running "a Linux": watch out for ModemManager. I've had enormous problems with this when trying to bring up an Android 'phone from cold: in principle there's a way of telling it to ignore a specific port but in practice I found that I had to prevent it from starting.

Apart from that... watching the thread with interest.

MarkMLl

MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ccrause

  • Hero Member
  • *****
  • Posts: 688
Re: Setting up Xtensa-FreeRTOS for ESP8266
« Reply #14 on: January 05, 2022, 10:37:11 pm »
Did you add the command line option -WP3.4?

No, I did not. I didn't realize I needed to. Adding that option appears to have fixed the issue as it compiled and linked with no errors.
(BTW, are these cross-compiler options documented somewhere? All I can find is -Wp which defines "Minimum iOS deployment version" though I'm sure this is for plain-vanilla FPC.)

The -WP option is now also used for xtensa-freertos.  There is a note on the xtensa wiki page about this option, but it is not really prominent I guess. There is a merge request to add this option to the command line help output for the xtensa cross compiler.

Quote
I proceeded to flash the device (a Devkit v0.9; 2M flash) with:
Code: Pascal  [Select][+][-]
  1. $IDF_PATH/components/esptool_py/esptool/esptool.py --chip esp8266 --port "/dev/ttyUSB1" --baud 921600 --before "default_reset" --after "hard_reset" write_flash -z --flash_mode "dio" --flash_freq "40m" --flash_size "2MB"   0x0 bootloader.bin 0x10000 hello.bin 0x8000 partitions_singleapp.bin

The result:
Code: Pascal  [Select][+][-]
  1. esptool.py v2.4.0
  2. Connecting.....
  3. Chip is ESP8266EX
  4. Features: WiFi
  5. MAC: [retracted]
  6. Uploading stub...
  7. Running stub...
  8. Stub running...
  9. Changing baud rate to 921600
  10. Changed.
  11. Configuring flash size...
  12.  
  13. A fatal error occurred: Invalid head of packet (0x01)
  14.  

Edit:
FYI, I copied partitions_singleapp.bin from ~/esp8266/hello_world/build/ and bootloader.bin from ~/esp8266/hello_world/build/bootloader/

Nothing seems obviously wrong with your flash command.  Did you manage to flash the SDK example to your board (make flash)?  If so, just copy the parameters from there.

You can also try a lower baud rate, such as 115200.

 

TinyPortal © 2005-2018