[Answered] How much of newlib is implemented/mapped/usable on ESP32?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Answered] How much of newlib is implemented/mapped/usable on ESP32?

Postby kolban » Sun Oct 02, 2016 6:43 pm

Looking at the ESP-IDF, I am fascinated by the "newlib" component. How much of it is mapped/implemented/usable on the ESP32? As an example, the posix "pthread" APIs seem to be present. Are these usable in an ESP32 environment? Do these map to FreeRTOS?

To start, I'd be interested if we had a list of things in newlib that we can use and things in newlib that we should avoid using (at this time).

Update: 2016-10-14 - Espressif have published the newlib-esp32 source on git hub here:
https://github.com/espressif/newlib-esp32
Last edited by kolban on Fri Oct 14, 2016 5:36 pm, edited 2 times in total.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: How much of newlib is implemented/mapped/usable on ESP32?

Postby ESP_igrr » Tue Oct 04, 2016 3:22 am

Newlib implements C standard library plus some extensions.
Most of the standard library works on the ESP32. Things which don't work yet include:
- time() and clock() functions
- functions which read from standard input: scanf, getc, gets, and so on.
- functions which operate on FILEs, such as fopen, fread, fwrite, fflush, an so on, are not mapped to any filesystem.
- double precision math functions use software FP routines. There is no software support for DFP accelerator which we have in the CPU yet. This affects only double precision functions. Single precision math functions use the FPU.
- atomic functions (part of C11 standard) are not implemented.

All other C standard library features should work. If anything doesn't work, please open an issue in github tracker.

Regarding support for functions which aren't part of C standard library... functions which deal with strings and numbers will work. Functions which deal with the system, i.e. raise(), kill(), getpid(), and other similar POSIX functions will not work.
Pthreads library is not part of newlib, and currently we don't have a pthreads wrapper for FreeRTOS. Such wrapper would be nice to have though.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: How much of newlib is implemented/mapped/usable on ESP32?

Postby kolban » Tue Oct 04, 2016 3:55 am

Thank you as always for the fantastic reply. I'm going to digest what you have said. The reason I asked about "pthreads" was because right now I am studying from the code upwards until the docs catch up ... while looking through the ESP-IDF, I came across ...

https://github.com/espressif/esp-idf/bl ... /pthread.h

Which since it looked like Posix pthreads and was contained in the "newlib" component folder ... made me think that pthreads might have been part of newlib which might have been targeted for inclusion/support in ESP-IDF.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: How much of newlib is implemented/mapped/usable on ESP32?

Postby ESP_igrr » Tue Oct 04, 2016 2:47 pm

True, this header file is part of newlib, however all functions are within #if defined(_POSIX_THREADS) block, and _POSIX_THREADS option is not enabled when building newlib.

wangshuoran
Posts: 37
Joined: Fri Dec 24, 2021 12:40 pm

Re: [Answered] How much of newlib is implemented/mapped/usable on ESP32?

Postby wangshuoran » Sun Oct 09, 2022 2:00 am

在ESP-IDFv4.4.2中,经过测试,
-time()是可用的,应该clock()也是可用的
-scanf()仅仅能获得FIFO中的数据,无法阻塞,是可用的,但不建议使用
-fopen等API都是可用的
-原子类是可用的,并且真正有用

除原子类外,其他API都可以在文档中找到,原子类的测试代码和测试效果如下:
```c
#include <stdatomic.h>
#include <stdio.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
atomic_int acnt;
int b = 0;

void task1(void* arg) {
for (size_t i = 0; i < 1000000; i++) {
atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed);
b++;
}
vTaskDelete(NULL);
}

void task2(void* arg) {
for (size_t i = 0; i < 1000000; i++) {
atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed);
b++;
}
vTaskDelete(NULL);
}

void app_main(void) {
atomic_init(&acnt, 0);
xTaskCreate(task1, "1", 1024, NULL, 1, NULL);
xTaskCreate(task2, "2", 1024, NULL, 1, NULL);
xTaskCreatePinnedToCore(task1, "1", 1024, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(task2, "2", 1024, NULL, 1, NULL, 1);
vTaskDelay(pdMS_TO_TICKS(5000));
int a = atomic_load_explicit(&acnt, memory_order_relaxed);
printf("%d %d\n", a, b);
}
```
屏幕截图 2022-10-09 095941.jpg
屏幕截图 2022-10-09 095941.jpg (57.04 KiB) Viewed 2397 times
所以,newlibc现在已经被乐鑫完美移植,可以无限把ESP32==Linux进行开发。

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: [Answered] How much of newlib is implemented/mapped/usable on ESP32?

Postby ESP_igrr » Mon Oct 10, 2022 7:32 am

Adding to this, it's possible to use 'scanf' as well, however you first need to initialize the UART driver and reconfigure the console to use it. Please refer to this comment: https://github.com/espressif/esp-idf/is ... 1233893079.

Who is online

Users browsing this forum: Bing [Bot] and 279 guests