|
1 | 1 | ESP32 Dual Core |
2 | 2 | =============== |
3 | 3 |
|
| 4 | +.. highlight:: text |
| 5 | + |
4 | 6 | Basic example of using second core of Esp32 chip. |
5 | 7 |
|
6 | | -Code should be in IRAM and avoid accessing flash, especially the filing system. |
| 8 | +.. important:: |
| 9 | + |
| 10 | + Sming is NOT thread-safe! In general, only re-entrant code and code intended to be called from interrupt context is safe to use from multiple cores. |
| 11 | + |
| 12 | +If code is timing sensitive then it should be run from IRAM. |
| 13 | + |
| 14 | +Flash and filing system access must only be done from the main Sming application. |
| 15 | + |
| 16 | + |
| 17 | +Required steps |
| 18 | +-------------- |
| 19 | + |
| 20 | +- Override IDF SDK config settings as in ``esp-dual-core.cfg``. |
| 21 | +- Add line to project's ``component.mk``: *SDK_CUSTOM_CONFIG := esp32-dual-core.cfg* |
| 22 | +- Add application code to run on second core and call to start for that core |
| 23 | +- Run `make sdk-config-clean` to ensure custom configuration values are picked up |
| 24 | + |
| 25 | +The custom configuration enables dual-core operation (``CONFIG_FREERTOS_UNICORE=n``) and disables the idle task for the second core (``CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=n``). This ensures that code will run uninterrupted. |
| 26 | + |
| 27 | +Task stats shows this when running:: |
| 28 | + |
| 29 | + # | Core | Prio | Handle | Run Time | % Time | Name |
| 30 | + 1 | 0 | 24 | 3ffaee9c | 0 | 0% | ipc0 |
| 31 | + 3 | 0 | 22 | 3ffaf568 | 3226 | 0% | esp_timer |
| 32 | + 5 | 0 | 0 | 3ffb8974 | 1996774 | 49% | IDLE0 |
| 33 | + 2 | 1 | 24 | 3ffaf404 | 0 | 0% | ipc1 |
| 34 | + 8 | 1 | 18 | 3ffbdf44 | 33156 | 0% | Sming |
| 35 | + 7 | 1 | 5 | 3ffb9d24 | 1966844 | 49% | Sming2 |
| 36 | + 6 | 1 | 0 | 3ffb9118 | 0 | 0% | IDLE1 |
| 37 | + |
| 38 | + |
| 39 | +Bare Metal |
| 40 | +---------- |
| 41 | + |
| 42 | +Because we rely on the IDF and its dependency on FreeRTOS, the above approach also ensures that calls such as ``System.queueCallback`` will work. This is the recommended way to communicate between code running on different cores. |
| 43 | + |
| 44 | +The default mode for Sming runs without FreeRTOS on the second core (*CONFIG_FREERTOS_UNICORE=y*) which provides a smaller set of tasks and thus lower system memory usage:: |
| 45 | + |
| 46 | + # | Core | Prio | Handle | Run Time | % Time | Name |
| 47 | + 1 | 0 | 22 | 3ffaf470 | 29 | 0% | esp_timer |
| 48 | + 4 | 0 | 18 | 3ffb778c | 11016 | 0% | Sming |
| 49 | + 3 | 0 | 0 | 3ffafdb0 | 1988955 | 49% | IDLE |
7 | 50 |
|
8 | | -Sming is NOT thread-safe! |
| 51 | +In this state it is technically possible to get code running on the second core by hooking a low-level startup routine. The application would need to handle stack/heap allocation and code would be far more limited in what it can safely do. |
0 commit comments