mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
docs: update cn trans for upl docs
This commit is contained in:
parent
886c2d742d
commit
74d745a80b
@ -1,141 +0,0 @@
|
|||||||
Programming ULP coprocessor using C macros (legacy)
|
|
||||||
===================================================
|
|
||||||
|
|
||||||
In addition to the existing binutils port for the ESP32 ULP coprocessor, it is possible to generate programs for the ULP by embedding assembly-like macros into an ESP32 application. Here is an example how this can be done::
|
|
||||||
|
|
||||||
const ulp_insn_t program[] = {
|
|
||||||
I_MOVI(R3, 16), // R3 <- 16
|
|
||||||
I_LD(R0, R3, 0), // R0 <- RTC_SLOW_MEM[R3 + 0]
|
|
||||||
I_LD(R1, R3, 1), // R1 <- RTC_SLOW_MEM[R3 + 1]
|
|
||||||
I_ADDR(R2, R0, R1), // R2 <- R0 + R1
|
|
||||||
I_ST(R2, R3, 2), // R2 -> RTC_SLOW_MEM[R2 + 2]
|
|
||||||
I_HALT()
|
|
||||||
};
|
|
||||||
size_t load_addr = 0;
|
|
||||||
size_t size = sizeof(program)/sizeof(ulp_insn_t);
|
|
||||||
ulp_process_macros_and_load(load_addr, program, &size);
|
|
||||||
ulp_run(load_addr);
|
|
||||||
|
|
||||||
The ``program`` array is an array of ``ulp_insn_t``, i.e. ULP coprocessor instructions. Each ``I_XXX`` preprocessor define translates into a single 32-bit instruction. Arguments of these preprocessor defines can be register numbers (``R0 — R3``) and literal constants. See `ULP coprocessor instruction defines`_ section for descriptions of instructions and arguments they take.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
Because some of the instruction macros expand to inline function calls, defining such array in global scope will cause the compiler to produce an "initializer element is not constant" error. To fix this error, move the definition of instructions array into local scope.
|
|
||||||
|
|
||||||
Load and store instructions use addresses expressed in 32-bit words. Address 0 corresponds to the first word of ``RTC_SLOW_MEM`` (which is address 0x50000000 as seen by the main CPUs).
|
|
||||||
|
|
||||||
To generate branch instructions, special ``M_`` preprocessor defines are used. ``M_LABEL`` define can be used to define a branch target. Label identifier is a 16-bit integer. ``M_Bxxx`` defines can be used to generate branch instructions with target set to a particular label.
|
|
||||||
|
|
||||||
Implementation note: these ``M_`` preprocessor defines will be translated into two ``ulp_insn_t`` values: one is a token value which contains label number, and the other is the actual instruction. ``ulp_process_macros_and_load`` function resolves the label number to the address, modifies the branch instruction to use the correct address, and removes the the extra ``ulp_insn_t`` token which contains the label numer.
|
|
||||||
|
|
||||||
Here is an example of using labels and branches::
|
|
||||||
|
|
||||||
const ulp_insn_t program[] = {
|
|
||||||
I_MOVI(R0, 34), // R0 <- 34
|
|
||||||
M_LABEL(1), // label_1
|
|
||||||
I_MOVI(R1, 32), // R1 <- 32
|
|
||||||
I_LD(R1, R1, 0), // R1 <- RTC_SLOW_MEM[R1]
|
|
||||||
I_MOVI(R2, 33), // R2 <- 33
|
|
||||||
I_LD(R2, R2, 0), // R2 <- RTC_SLOW_MEM[R2]
|
|
||||||
I_SUBR(R3, R1, R2), // R3 <- R1 - R2
|
|
||||||
I_ST(R3, R0, 0), // R3 -> RTC_SLOW_MEM[R0 + 0]
|
|
||||||
I_ADDI(R0, R0, 1), // R0++
|
|
||||||
M_BL(1, 64), // if (R0 < 64) goto label_1
|
|
||||||
I_HALT(),
|
|
||||||
};
|
|
||||||
RTC_SLOW_MEM[32] = 42;
|
|
||||||
RTC_SLOW_MEM[33] = 18;
|
|
||||||
size_t load_addr = 0;
|
|
||||||
size_t size = sizeof(program)/sizeof(ulp_insn_t);
|
|
||||||
ulp_process_macros_and_load(load_addr, program, &size);
|
|
||||||
ulp_run(load_addr);
|
|
||||||
|
|
||||||
|
|
||||||
Application Example
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
Demonstration of entering into deep sleep mode and waking up using several wake up sources: :example:`system/deep_sleep`.
|
|
||||||
|
|
||||||
|
|
||||||
API Reference
|
|
||||||
-------------
|
|
||||||
|
|
||||||
Header File
|
|
||||||
^^^^^^^^^^^
|
|
||||||
|
|
||||||
.. list::
|
|
||||||
|
|
||||||
:esp32: - :component_file:`ulp/include/esp32/ulp.h`
|
|
||||||
:esp32s2: - :component_file:`ulp/include/esp32s2/ulp.h`
|
|
||||||
:esp32s3: - :component_file:`ulp/include/esp32s3/ulp.h`
|
|
||||||
|
|
||||||
Functions
|
|
||||||
^^^^^^^^^
|
|
||||||
|
|
||||||
.. doxygenfunction:: ulp_process_macros_and_load
|
|
||||||
.. doxygenfunction:: ulp_run
|
|
||||||
|
|
||||||
Error codes
|
|
||||||
^^^^^^^^^^^
|
|
||||||
|
|
||||||
.. doxygendefine:: ESP_ERR_ULP_BASE
|
|
||||||
.. doxygendefine:: ESP_ERR_ULP_SIZE_TOO_BIG
|
|
||||||
.. doxygendefine:: ESP_ERR_ULP_INVALID_LOAD_ADDR
|
|
||||||
.. doxygendefine:: ESP_ERR_ULP_DUPLICATE_LABEL
|
|
||||||
.. doxygendefine:: ESP_ERR_ULP_UNDEFINED_LABEL
|
|
||||||
.. doxygendefine:: ESP_ERR_ULP_BRANCH_OUT_OF_RANGE
|
|
||||||
|
|
||||||
ULP coprocessor registers
|
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
ULP co-processor has 4 16-bit general purpose registers. All registers have same functionality, with one exception. R0 register is used by some of the compare-and-branch instructions as a source register.
|
|
||||||
|
|
||||||
These definitions can be used for all instructions which require a register.
|
|
||||||
|
|
||||||
.. doxygengroup:: ulp_registers
|
|
||||||
:content-only:
|
|
||||||
|
|
||||||
ULP coprocessor instruction defines
|
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
.. doxygendefine:: I_DELAY
|
|
||||||
.. doxygendefine:: I_HALT
|
|
||||||
.. doxygendefine:: I_END
|
|
||||||
.. doxygendefine:: I_ST
|
|
||||||
.. doxygendefine:: I_LD
|
|
||||||
.. doxygendefine:: I_WR_REG
|
|
||||||
.. doxygendefine:: I_RD_REG
|
|
||||||
.. doxygendefine:: I_BL
|
|
||||||
.. doxygendefine:: I_BGE
|
|
||||||
.. doxygendefine:: I_BXR
|
|
||||||
.. doxygendefine:: I_BXI
|
|
||||||
.. doxygendefine:: I_BXZR
|
|
||||||
.. doxygendefine:: I_BXZI
|
|
||||||
.. doxygendefine:: I_BXFR
|
|
||||||
.. doxygendefine:: I_BXFI
|
|
||||||
.. doxygendefine:: I_ADDR
|
|
||||||
.. doxygendefine:: I_SUBR
|
|
||||||
.. doxygendefine:: I_ANDR
|
|
||||||
.. doxygendefine:: I_ORR
|
|
||||||
.. doxygendefine:: I_MOVR
|
|
||||||
.. doxygendefine:: I_LSHR
|
|
||||||
.. doxygendefine:: I_RSHR
|
|
||||||
.. doxygendefine:: I_ADDI
|
|
||||||
.. doxygendefine:: I_SUBI
|
|
||||||
.. doxygendefine:: I_ANDI
|
|
||||||
.. doxygendefine:: I_ORI
|
|
||||||
.. doxygendefine:: I_MOVI
|
|
||||||
.. doxygendefine:: I_LSHI
|
|
||||||
.. doxygendefine:: I_RSHI
|
|
||||||
.. doxygendefine:: M_LABEL
|
|
||||||
.. doxygendefine:: M_BL
|
|
||||||
.. doxygendefine:: M_BGE
|
|
||||||
.. doxygendefine:: M_BX
|
|
||||||
.. doxygendefine:: M_BXZ
|
|
||||||
.. doxygendefine:: M_BXF
|
|
||||||
|
|
||||||
Defines
|
|
||||||
^^^^^^^
|
|
||||||
|
|
||||||
.. doxygendefine:: RTC_SLOW_MEM
|
|
||||||
|
|
@ -289,10 +289,6 @@ union ulp_insn {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef union ulp_insn ulp_insn_t;
|
|
||||||
|
|
||||||
_Static_assert(sizeof(ulp_insn_t) == 4, "ULP coprocessor instruction size should be 4 bytes");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delay (nop) for a given number of cycles
|
* Delay (nop) for a given number of cycles
|
||||||
*/
|
*/
|
||||||
|
@ -255,10 +255,6 @@ union ulp_insn {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef union ulp_insn ulp_insn_t;
|
|
||||||
|
|
||||||
_Static_assert(sizeof(ulp_insn_t) == 4, "ULP coprocessor instruction size should be 4 bytes");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delay (nop) for a given number of cycles
|
* Delay (nop) for a given number of cycles
|
||||||
*/
|
*/
|
||||||
|
@ -255,9 +255,6 @@ union ulp_insn {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef union ulp_insn ulp_insn_t;
|
|
||||||
|
|
||||||
_Static_assert(sizeof(ulp_insn_t) == 4, "ULP coprocessor instruction size should be 4 bytes");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delay (nop) for a given number of cycles
|
* Delay (nop) for a given number of cycles
|
||||||
|
@ -194,10 +194,7 @@ INPUT = \
|
|||||||
$(PROJECT_PATH)/components/esp_rom/include/esp_rom_sys.h \
|
$(PROJECT_PATH)/components/esp_rom/include/esp_rom_sys.h \
|
||||||
$(PROJECT_PATH)/components/esp_system/include/esp_system.h \
|
$(PROJECT_PATH)/components/esp_system/include/esp_system.h \
|
||||||
$(PROJECT_PATH)/components/esp_common/include/esp_idf_version.h \
|
$(PROJECT_PATH)/components/esp_common/include/esp_idf_version.h \
|
||||||
$(PROJECT_PATH)/components/ulp/ulp_common/include/ulp_common.h \
|
$(PROJECT_PATH)/components/ulp/include/ulp_common.h \
|
||||||
$(PROJECT_PATH)/components/ulp/ulp_fsm/include/ulp_fsm_common.h \
|
|
||||||
$(PROJECT_PATH)/components/ulp/ulp_riscv/include/ulp_riscv.h \
|
|
||||||
$(PROJECT_PATH)/components/ulp/ulp_riscv/include/ulp_riscv_utils.h \
|
|
||||||
$(PROJECT_PATH)/components/app_trace/include/esp_app_trace.h \
|
$(PROJECT_PATH)/components/app_trace/include/esp_app_trace.h \
|
||||||
$(PROJECT_PATH)/components/app_trace/include/esp_sysview_trace.h \
|
$(PROJECT_PATH)/components/app_trace/include/esp_sysview_trace.h \
|
||||||
$(PROJECT_PATH)/components/esp_pm/include/esp_pm.h \
|
$(PROJECT_PATH)/components/esp_pm/include/esp_pm.h \
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
ULP-RISC-V Coprocessor programming
|
ULP RISC-V Coprocessor programming
|
||||||
==================================
|
==================================
|
||||||
:link_to_translation:`zh_CN:[中文]`
|
:link_to_translation:`zh_CN:[中文]`
|
||||||
|
|
||||||
The ULP RISC-V coprocessor is a variant of the ULP present in {IDF_TARGET_NAME}. Similar to ULP FSM, ULP RISC-V coprocessor can perform tasks such as sensor readings while the main CPU stays in low power modes. The main difference between ULP FSM and ULP RISC-V is that the later can be programmed in C using standard GNU tools. The ULP RISC-V coprocessor can access the RTC_SLOW_MEM memory region, and registers in RTC_CNTL, RTC_IO, and SARADC peripherals. The RISC-V processor is a 32-bit, fixed point machine. Its instruction set is based on RV32IMC which includes hardware multiplication and division, and compressed code.
|
The ULP RISC-V coprocessor is a variant of the ULP present in {IDF_TARGET_NAME}. Similar to ULP FSM, the ULP RISC-V coprocessor can perform tasks such as sensor readings while the main CPU stays in low power modes. The main difference between ULP FSM and ULP RISC-V is that the latter can be programmed in C using standard GNU tools. The ULP RISC-V coprocessor can access the RTC_SLOW_MEM memory region, and registers in RTC_CNTL, RTC_IO, and SARADC peripherals. The RISC-V processor is a 32-bit fixed point machine. Its instruction set is based on RV32IMC which includes hardware multiplication and division, and compressed code.
|
||||||
|
|
||||||
Installing the ULP-RISC-V Toolchain
|
Installing the ULP RISC-V Toolchain
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
|
||||||
The ULP RISC-V coprocessor code is written in C (assembly is also possible) and compiled using the RISC-V toolchain based on GCC.
|
The ULP RISC-V coprocessor code is written in C (assembly is also possible) and compiled using the RISC-V toolchain based on GCC.
|
||||||
|
|
||||||
If you have already set up ESP-IDF with CMake build system according to the :doc:`Getting Started Guide <../../../get-started/index>`, then the toolchain should already be installed.
|
If you have already set up ESP-IDF with CMake build system according to the :doc:`Getting Started Guide <../../get-started/index>`, then the toolchain should already be installed.
|
||||||
|
|
||||||
.. note: In earlier versions of ESP-IDF, RISC-V toolchain had a different prefix: `riscv-none-embed-gcc`.
|
.. note: In earlier versions of ESP-IDF, RISC-V toolchain had a different prefix: `riscv-none-embed-gcc`.
|
||||||
|
|
||||||
@ -18,9 +18,9 @@ Compiling the ULP RISC-V Code
|
|||||||
|
|
||||||
To compile the ULP RISC-V code as part of the component, the following steps must be taken:
|
To compile the ULP RISC-V code as part of the component, the following steps must be taken:
|
||||||
|
|
||||||
1. The ULP RISC-V code, written in C or assembly (must use the `.S` extension), must be placed in a separate directory inside the component directory, for instance `ulp/`.
|
1. The ULP RISC-V code, written in C or assembly (must use the `.S` extension), must be placed in a separate directory inside the component directory, for instance, `ulp/`.
|
||||||
|
|
||||||
.. note: When registering the component (via ``idf_component_register``), this directory should not be added to the ``SRC_DIRS`` argument as it is currently done for the ULP FSM. See the step below for how to properly add ULP source files
|
.. note: When registering the component (via ``idf_component_register``), this directory should not be added to the ``SRC_DIRS`` argument as it is currently done for the ULP FSM. See the step below for how to properly add ULP source files.
|
||||||
|
|
||||||
2. Call ``ulp_embed_binary`` from the component CMakeLists.txt after registration. For example::
|
2. Call ``ulp_embed_binary`` from the component CMakeLists.txt after registration. For example::
|
||||||
|
|
||||||
@ -33,13 +33,9 @@ To compile the ULP RISC-V code as part of the component, the following steps mus
|
|||||||
|
|
||||||
ulp_embed_binary(${ulp_app_name} "${ulp_sources}" "${ulp_exp_dep_srcs}")
|
ulp_embed_binary(${ulp_app_name} "${ulp_sources}" "${ulp_exp_dep_srcs}")
|
||||||
|
|
||||||
The first argument to ``ulp_embed_binary`` specifies the ULP binary name. The name specified here will also be used by other generated artifacts
|
The first argument to ``ulp_embed_binary`` specifies the ULP binary name. The name specified here will also be used by other generated artifacts such as the ELF file, map file, header file and linker export file. The second argument specifies the ULP source files. Finally, the third argument specifies the list of component source files which include the header file to be generated. This list is needed to build the dependencies correctly and ensure that the generated header file will be created before any of these files are compiled. See the section below for the concept of generated header files for ULP applications.
|
||||||
such as the ELF file, map file, header file and linker export file. The second argument specifies the ULP source files.
|
|
||||||
Finally, the third argument specifies the list of component source files which include the header file to be generated.
|
|
||||||
This list is needed to build the dependencies correctly and ensure that the generated header file will be created before any of these files are compiled.
|
|
||||||
See section below for the concept of generated header files for ULP applications.
|
|
||||||
|
|
||||||
3. Build the application as usual (e.g. `idf.py app`)
|
3. Build the application as usual (e.g. `idf.py app`).
|
||||||
|
|
||||||
Inside, the build system will take the following steps to build ULP program:
|
Inside, the build system will take the following steps to build ULP program:
|
||||||
|
|
||||||
@ -47,22 +43,22 @@ To compile the ULP RISC-V code as part of the component, the following steps mus
|
|||||||
|
|
||||||
2. **Run the linker script template through the C preprocessor.** The template is located in ``components/ulp/ld`` directory.
|
2. **Run the linker script template through the C preprocessor.** The template is located in ``components/ulp/ld`` directory.
|
||||||
|
|
||||||
4. **Link the object files into an output ELF file** (``ulp_app_name.elf``). The Map file (``ulp_app_name.map``) generated at this stage may be useful for debugging purposes.
|
3. **Link the object files into an output ELF file** (``ulp_app_name.elf``). The Map file (``ulp_app_name.map``) generated at this stage may be useful for debugging purposes.
|
||||||
|
|
||||||
5. **Dump the contents of the ELF file into a binary** (``ulp_app_name.bin``) which can then be embedded into the application.
|
4. **Dump the contents of the ELF file into a binary** (``ulp_app_name.bin``) which can then be embedded into the application.
|
||||||
|
|
||||||
6. **Generate a list of global symbols** (``ulp_app_name.sym``) in the ELF file using ``riscv32-esp-elf-nm``.
|
5. **Generate a list of global symbols** (``ulp_app_name.sym``) in the ELF file using ``riscv32-esp-elf-nm``.
|
||||||
|
|
||||||
7. **Create an LD export script and header file** (``ulp_app_name.ld`` and ``ulp_app_name.h``) containing the symbols from ``ulp_app_name.sym``. This is done using the ``esp32ulp_mapgen.py`` utility.
|
6. **Create an LD export script and a header file** (``ulp_app_name.ld`` and ``ulp_app_name.h``) containing the symbols from ``ulp_app_name.sym``. This is done using the ``esp32ulp_mapgen.py`` utility.
|
||||||
|
|
||||||
8. **Add the generated binary to the list of binary files** to be embedded into the application.
|
7. **Add the generated binary to the list of binary files** to be embedded into the application.
|
||||||
|
|
||||||
Accessing the ULP RISC-V Program Variables
|
Accessing the ULP RISC-V Program Variables
|
||||||
------------------------------------------
|
------------------------------------------
|
||||||
|
|
||||||
Global symbols defined in the ULP RISC-V program may be used inside the main program.
|
Global symbols defined in the ULP RISC-V program may be used inside the main program.
|
||||||
|
|
||||||
For example, the ULP RISC-V program may define a variable ``measurement_count`` which will define the number of ADC measurements the program needs to make before waking up the chip from deep sleep
|
For example, the ULP RISC-V program may define a variable ``measurement_count`` which will define the number of ADC measurements the program needs to make before waking up the chip from deep sleep.
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
@ -78,7 +74,7 @@ For example, the ULP RISC-V program may define a variable ``measurement_count``
|
|||||||
|
|
||||||
The main program can access the global ULP RISC-V program variables as the build system makes this possible by generating the ``${ULP_APP_NAME}.h`` and ``${ULP_APP_NAME}.ld`` files which define the global symbols present in the ULP RISC-V program. Each global symbol defined in the ULP RISC-V program is included in these files and are prefixed with ``ulp_``.
|
The main program can access the global ULP RISC-V program variables as the build system makes this possible by generating the ``${ULP_APP_NAME}.h`` and ``${ULP_APP_NAME}.ld`` files which define the global symbols present in the ULP RISC-V program. Each global symbol defined in the ULP RISC-V program is included in these files and are prefixed with ``ulp_``.
|
||||||
|
|
||||||
The header file contains the declaration of the symbol
|
The header file contains the declaration of the symbol:
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
@ -105,9 +101,9 @@ Starting the ULP RISC-V Program
|
|||||||
|
|
||||||
To run a ULP RISC-V program, the main application needs to load the ULP program into RTC memory using the :cpp:func:`ulp_riscv_load_binary` function, and then start it using the :cpp:func:`ulp_riscv_run` function.
|
To run a ULP RISC-V program, the main application needs to load the ULP program into RTC memory using the :cpp:func:`ulp_riscv_load_binary` function, and then start it using the :cpp:func:`ulp_riscv_run` function.
|
||||||
|
|
||||||
Note that `CONFIG_ULP_COPROC_ENABLED` and `CONFIG_ULP_COPROC_TYPE_RISCV` options must be enabled in menuconfig to work with ULP RISC-V. To reserve memory for the ULP, "RTC slow memory reserved for coprocessor" option must be set to a value big enough to store ULP RISC-V code and data. If the application components contain multiple ULP programs, then the size of the RTC memory must be sufficient to hold the largest one.
|
Note that `CONFIG_{IDF_TARGET_CFG_PREFIX}_ULP_COPROC_ENABLED` and `CONFIG_{IDF_TARGET_CFG_PREFIX}_ULP_COPROC_RISCV` options must be enabled in menuconfig to work with ULP RISC-V. To reserve memory for the ULP, the ``RTC slow memory reserved for coprocessor`` option must be set to a value big enough to store ULP RISC-V code and data. If the application components contain multiple ULP programs, then the size of the RTC memory must be sufficient to hold the largest one.
|
||||||
|
|
||||||
Each ULP RISC-V program is embedded into the ESP-IDF application as a binary blob. The application can reference this blob and load it in the following way (suppose ULP_APP_NAME was defined to ``ulp_app_name``)
|
Each ULP RISC-V program is embedded into the ESP-IDF application as a binary blob. The application can reference this blob and load it in the following way (suppose ULP_APP_NAME was defined to ``ulp_app_name``):
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
@ -119,7 +115,7 @@ Each ULP RISC-V program is embedded into the ESP-IDF application as a binary blo
|
|||||||
(bin_end - bin_start)) );
|
(bin_end - bin_start)) );
|
||||||
}
|
}
|
||||||
|
|
||||||
Once the program is loaded into RTC memory, the application can start it by calling the :cpp:func:`ulp_riscv_run` function
|
Once the program is loaded into RTC memory, the application can start it by calling the :cpp:func:`ulp_riscv_run` function:
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
@ -144,7 +140,6 @@ Application Examples
|
|||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
* ULP RISC-V Coprocessor polls GPIO while main CPU is in deep sleep: :example:`system/ulp_riscv/gpio`.
|
* ULP RISC-V Coprocessor polls GPIO while main CPU is in deep sleep: :example:`system/ulp_riscv/gpio`.
|
||||||
* ULP RISC-V Coprocessor reads external temperature sensor while main CPU is in deep sleep: :example:`system/ulp_riscv/ds18b20_onewire`.
|
|
||||||
|
|
||||||
API Reference
|
API Reference
|
||||||
-------------
|
-------------
|
@ -3,18 +3,18 @@ ULP Coprocessor programming
|
|||||||
|
|
||||||
:link_to_translation:`zh_CN:[中文]`
|
:link_to_translation:`zh_CN:[中文]`
|
||||||
|
|
||||||
The ULP (Ultra Low Power) coprocessor is a simple FSM (Finite State Machine) which is designed to perform measurements using the ADC, temperature sensor, and external I2C sensors, while the main processors are in deep sleep mode. The ULP coprocessor can access the RTC_SLOW_MEM memory region, and registers in the RTC_CNTL, RTC_IO, and SARADC peripherals. The ULP coprocessor uses fixed-width 32-bit instructions, 32-bit memory addressing, and has 4 general-purpose 16-bit registers. This coprocessor is referred to as `ULP FSM` in ESP-IDF.
|
The Ultra Low Power (ULP) coprocessor is a simple finite state machine (FSM) which is designed to perform measurements using the ADC, temperature sensor, and external I2C sensors, while the main processors are in deep sleep mode. The ULP coprocessor can access the RTC_SLOW_MEM memory region, and registers in the RTC_CNTL, RTC_IO, and SARADC peripherals. The ULP coprocessor uses fixed-width 32-bit instructions, 32-bit memory addressing, and has 4 general-purpose 16-bit registers. This coprocessor is referred to as `ULP FSM` in ESP-IDF.
|
||||||
|
|
||||||
.. only:: esp32s2 or esp32s3
|
.. only:: esp32s2 or esp32s3
|
||||||
|
|
||||||
{IDF_TARGET_NAME} provides a second type of ULP coprocessor which is based on a RISC-V instruction set architecture. For details regarding `ULP RISC-V` refer :doc:`ULP-RISC-V Coprocessor <../../../api-reference/system/ulp-risc-v>`.
|
{IDF_TARGET_NAME} provides a second type of ULP coprocessor which is based on a RISC-V instruction set architecture. For details regarding `ULP RISC-V` refer :doc:`ULP-RISC-V Coprocessor <./ulp-risc-v>`.
|
||||||
|
|
||||||
Installing the Toolchain
|
Installing the Toolchain
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
The ULP FSM coprocessor code is written in assembly and compiled using the `binutils-esp32ulp toolchain`_.
|
The ULP FSM coprocessor code is written in assembly and compiled using the `binutils-esp32ulp toolchain`_.
|
||||||
|
|
||||||
If you have already set up ESP-IDF with CMake build system according to the :doc:`Getting Started Guide <../../../get-started/index>`, then the ULP FSM toolchain will already be installed.
|
If you have already set up ESP-IDF with CMake build system according to the :doc:`Getting Started Guide <../../get-started/index>`, then the ULP FSM toolchain will already be installed.
|
||||||
|
|
||||||
.. only:: esp32
|
.. only:: esp32
|
||||||
|
|
||||||
@ -22,8 +22,8 @@ If you have already set up ESP-IDF with CMake build system according to the :doc
|
|||||||
|
|
||||||
Programming ULP FSM
|
Programming ULP FSM
|
||||||
-------------------
|
-------------------
|
||||||
The ULP FSM can be programmed using the supported instruction set. Alternatively, the ULP FSM coprocessor can also be programmed using C Macros on the main CPU.
|
|
||||||
Theses two methods are described in the following section:
|
The ULP FSM can be programmed using the supported instruction set. Alternatively, the ULP FSM coprocessor can also be programmed using C Macros on the main CPU. Theses two methods are described in the following section:
|
||||||
|
|
||||||
.. toctree::
|
.. toctree::
|
||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
@ -36,7 +36,7 @@ Compiling the ULP Code
|
|||||||
|
|
||||||
To compile the ULP FSM code as part of the component, the following steps must be taken:
|
To compile the ULP FSM code as part of the component, the following steps must be taken:
|
||||||
|
|
||||||
1. The ULP FSM code, written in assembly, must be added to one or more files with `.S` extension. These files must be placed into a separate directory inside the component directory, for instance `ulp/`.
|
1. The ULP FSM code, written in assembly, must be added to one or more files with `.S` extension. These files must be placed into a separate directory inside the component directory, for instance, `ulp/`.
|
||||||
|
|
||||||
.. note: When registering the component (via ``idf_component_register``), this directory should not be added to the ``SRC_DIRS`` argument. The logic behind this is that the ESP-IDF build system will compile files found in ``SRC_DIRS`` based on their extensions. For ``.S`` files, ``{IDF_TARGET_TOOLCHAIN_PREFIX}-as`` assembler is used. This is not desirable for ULP FSM assembly files, so the easiest way to achieve the distinction is by placing ULP FSM assembly files into a separate directory. The ULP FSM assembly source files should also **not** be added to ``SRCS`` for the same reason. See the step below for how to properly add ULP FSM assembly source files.
|
.. note: When registering the component (via ``idf_component_register``), this directory should not be added to the ``SRC_DIRS`` argument. The logic behind this is that the ESP-IDF build system will compile files found in ``SRC_DIRS`` based on their extensions. For ``.S`` files, ``{IDF_TARGET_TOOLCHAIN_PREFIX}-as`` assembler is used. This is not desirable for ULP FSM assembly files, so the easiest way to achieve the distinction is by placing ULP FSM assembly files into a separate directory. The ULP FSM assembly source files should also **not** be added to ``SRCS`` for the same reason. See the step below for how to properly add ULP FSM assembly source files.
|
||||||
|
|
||||||
@ -51,9 +51,9 @@ To compile the ULP FSM code as part of the component, the following steps must b
|
|||||||
|
|
||||||
ulp_embed_binary(${ulp_app_name} "${ulp_s_sources}" "${ulp_exp_dep_srcs}")
|
ulp_embed_binary(${ulp_app_name} "${ulp_s_sources}" "${ulp_exp_dep_srcs}")
|
||||||
|
|
||||||
The first argument to ``ulp_embed_binary`` specifies the ULP FSM binary name. The name specified here will also be used by other generated artifacts such as the ELF file, map file, header file and linker export file. The second argument specifies the ULP FSM assembly source files. Finally, the third argument specifies the list of component source files which include the header file to be generated. This list is needed to build the dependencies correctly and ensure that the generated header file will be created before any of these files are compiled. See section below for the concept of generated header files for ULP applications.
|
The first argument to ``ulp_embed_binary`` specifies the ULP FSM binary name. The name specified here will also be used by other generated artifacts such as the ELF file, map file, header file and linker export file. The second argument specifies the ULP FSM assembly source files. Finally, the third argument specifies the list of component source files which include the header file to be generated. This list is needed to build the dependencies correctly and ensure that the generated header file will be created before any of these files are compiled. See the section below for the concept of generated header files for ULP applications.
|
||||||
|
|
||||||
3. Build the application as usual (e.g. `idf.py app`)
|
3. Build the application as usual (e.g. `idf.py app`).
|
||||||
|
|
||||||
Inside, the build system will take the following steps to build ULP FSM program:
|
Inside, the build system will take the following steps to build ULP FSM program:
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ The first argument to ``ulp_embed_binary`` specifies the ULP FSM binary name. Th
|
|||||||
|
|
||||||
6. **Generate a list of global symbols** (``ulp_app_name.sym``) in the ELF file using ``esp32ulp-elf-nm``.
|
6. **Generate a list of global symbols** (``ulp_app_name.sym``) in the ELF file using ``esp32ulp-elf-nm``.
|
||||||
|
|
||||||
7. **Create an LD export script and header file** (``ulp_app_name.ld`` and ``ulp_app_name.h``) containing the symbols from ``ulp_app_name.sym``. This is done using the ``esp32ulp_mapgen.py`` utility.
|
7. **Create an LD export script and a header file** (``ulp_app_name.ld`` and ``ulp_app_name.h``) containing the symbols from ``ulp_app_name.sym``. This is done using the ``esp32ulp_mapgen.py`` utility.
|
||||||
|
|
||||||
8. **Add the generated binary to the list of binary files** to be embedded into the application.
|
8. **Add the generated binary to the list of binary files** to be embedded into the application.
|
||||||
|
|
||||||
@ -110,16 +110,16 @@ To access the ULP program variables from the main program, the generated header
|
|||||||
|
|
||||||
.. only:: esp32
|
.. only:: esp32
|
||||||
|
|
||||||
Note that the ULP FSM program can only use the lower 16 bits of each 32-bit word in RTC memory, because the registers are 16-bit, and there is no instruction to load from the high part of the word.
|
Note that the ULP FSM program can only use the lower 16 bits of each 32-bit word in RTC memory, because the registers are 16-bit, and there is no instruction to load from the high part of the word. Likewise, the ULP store instruction writes register values into the lower 16 bits of the 32-bit word in RTC memory. The upper 16 bits are written with a value which depends on the address of the store instruction, thus when reading variables written by the ULP coprocessor, the main application needs to mask the upper 16 bits, e.g.::
|
||||||
Likewise, the ULP store instruction writes register values into the lower 16 bits of the 32-bit word in RTC memory. The upper 16 bits are written with a value which depends on the address of the store instruction, thus when reading variables written by the ULP coprocessor, the main application needs to mask the upper 16 bits, e.g.::
|
|
||||||
printf("Last measurement value: %d\n", ulp_last_measurement & UINT16_MAX);
|
printf("Last measurement value: %d\n", ulp_last_measurement & UINT16_MAX);
|
||||||
|
|
||||||
Starting the ULP FSM Program
|
Starting the ULP FSM Program
|
||||||
----------------------------
|
----------------------------
|
||||||
|
|
||||||
To run a ULP FSM program, the main application needs to load the ULP program into RTC memory using the :cpp:func:`ulp_load_binary` function, and then start it using the :cpp:func:`ulp_run` function.
|
To run a ULP FSM program, the main application needs to load the ULP program into RTC memory using the :cpp:func:`ulp_load_binary` function, and then start it using the :cpp:func:`ulp_run` function.
|
||||||
|
|
||||||
Note that "Enable Ultra Low Power (ULP) Coprocessor" option must be enabled in menuconfig to work with ULP. To select the type of ULP to be used, the "ULP Co-processor type" option must be set. To reserve memory for the ULP, "RTC slow memory reserved for coprocessor" option must be set to a value sufficient enough to store ULP code and data. If the application components contain multiple ULP programs, then the size of the RTC memory must be sufficient to hold the largest one.
|
Note that the ``Enable Ultra Low Power (ULP) Coprocessor`` option must be enabled in menuconfig to work with ULP. To select the type of ULP to be used, the ``ULP Co-processor type`` option must be set. To reserve memory for the ULP, the ``RTC slow memory reserved for coprocessor`` option must be set to a value big enough to store ULP code and data. If the application components contain multiple ULP programs, then the size of the RTC memory must be sufficient to hold the largest one.
|
||||||
|
|
||||||
Each ULP program is embedded into the ESP-IDF application as a binary blob. The application can reference this blob and load it in the following way (suppose ULP_APP_NAME was defined to ``ulp_app_name``)::
|
Each ULP program is embedded into the ESP-IDF application as a binary blob. The application can reference this blob and load it in the following way (suppose ULP_APP_NAME was defined to ``ulp_app_name``)::
|
||||||
|
|
||||||
@ -184,8 +184,6 @@ Application Examples
|
|||||||
API Reference
|
API Reference
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
.. include-build-file:: inc/ulp_fsm_common.inc
|
|
||||||
.. include-build-file:: inc/ulp_common.inc
|
.. include-build-file:: inc/ulp_common.inc
|
||||||
.. include-build-file:: inc/ulp_common_defs.inc
|
|
||||||
|
|
||||||
.. _binutils-esp32ulp toolchain: https://github.com/espressif/binutils-esp32ulp
|
.. _binutils-esp32ulp toolchain: https://github.com/espressif/binutils-esp32ulp
|
@ -796,10 +796,10 @@ The detailed description of all instructions is presented below:
|
|||||||
Conditions *LE* and *GE* are implemented in the assembler using two **JUMPR** instructions::
|
Conditions *LE* and *GE* are implemented in the assembler using two **JUMPR** instructions::
|
||||||
|
|
||||||
// JUMPR target, threshold, LE is implemented as:
|
// JUMPR target, threshold, LE is implemented as:
|
||||||
|
|
||||||
JUMPR target, threshold, EQ
|
JUMPR target, threshold, EQ
|
||||||
JUMPR target, threshold, LT
|
JUMPR target, threshold, LT
|
||||||
|
|
||||||
// JUMPR target, threshold, GE is implemented as:
|
// JUMPR target, threshold, GE is implemented as:
|
||||||
|
|
||||||
JUMPR target, threshold, EQ
|
JUMPR target, threshold, EQ
|
File diff suppressed because it is too large
Load Diff
@ -185,7 +185,7 @@ Single Read mode ADC example can be found in :example:`peripherals/adc/single_re
|
|||||||
|
|
||||||
.. only:: SOC_ULP_SUPPORTED
|
.. only:: SOC_ULP_SUPPORTED
|
||||||
|
|
||||||
This API provides convenient way to configure ADC1 for reading from :doc:`ULP <../../api-reference/system/ulp>`. To do so, call function :cpp:func:`adc1_ulp_enable` and then set precision and attenuation as discussed above.
|
This API provides convenient way to configure ADC1 for reading from :doc:`ULP <../../api-guides/ulp>`. To do so, call function :cpp:func:`adc1_ulp_enable` and then set precision and attenuation as discussed above.
|
||||||
|
|
||||||
.. only:: esp32 or esp32s2
|
.. only:: esp32 or esp32s2
|
||||||
|
|
||||||
|
@ -809,7 +809,7 @@ Overview
|
|||||||
.. list::
|
.. list::
|
||||||
|
|
||||||
- In deep sleep
|
- In deep sleep
|
||||||
:SOC_ULP_SUPPORTED: - The :doc:`Ultra Low Power co-processor <../../api-reference/system/ulp>` is running
|
:SOC_ULP_SUPPORTED: - The :doc:`Ultra Low Power co-processor <../../api-guides/ulp>` is running
|
||||||
- Analog functions such as ADC/DAC/etc are in use.
|
- Analog functions such as ADC/DAC/etc are in use.
|
||||||
|
|
||||||
Application Example
|
Application Example
|
||||||
|
@ -149,7 +149,7 @@ DROM(数据存储在 flash 中)
|
|||||||
RTC Slow memory(RTC 慢速存储器)
|
RTC Slow memory(RTC 慢速存储器)
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
从 RTC 存储器运行的代码中使用的全局和静态变量必须放入 RTC Slow memory 中。例如 :doc:`深度睡眠 <deep-sleep-stub>` 变量可以放在 RTC Slow memory 中,而不是 RTC FAST memory,或者也可以放入由 :doc:`/api-reference/system/ulp` 访问的代码和变量。
|
从 RTC 存储器运行的代码中使用的全局和静态变量必须放入 RTC Slow memory 中。例如 :doc:`深度睡眠 <deep-sleep-stub>` 变量可以放在 RTC Slow memory 中,而不是 RTC FAST memory,或者也可以放入由 :doc:`/api-guides/ulp` 访问的代码和变量。
|
||||||
|
|
||||||
``RTC_NOINIT_ATTR`` 属性宏可以用来将数据放入 RTC Slow memory。放入此类型存储器的值从深度睡眠模式中醒来后会保持值不变。
|
``RTC_NOINIT_ATTR`` 属性宏可以用来将数据放入 RTC Slow memory。放入此类型存储器的值从深度睡眠模式中醒来后会保持值不变。
|
||||||
|
|
||||||
|
@ -1,37 +1,26 @@
|
|||||||
ULP-RISC-V 协处理器编程
|
ULP RISC-V 协处理器编程
|
||||||
==================================
|
==================================
|
||||||
:link_to_translation:`en:[English]`
|
:link_to_translation:`en:[English]`
|
||||||
|
|
||||||
.. only:: esp32s3
|
ULP RISC-V 协处理器是 ULP 的一种变体,用于 {IDF_TARGET_NAME}。与 ULP FSM 类似,ULP RISC-V 协处理器可以在主处理器处于低功耗模式时执行传感器读数等任务。其与 ULP FSM 的主要区别在于,ULP RISC-V 可以通过标准 GNU 工具使用 C 语言进行编程。ULP RISC-V 可以访问 RTC_SLOW_MEM 内存区域及 RTC_CNTL、RTC_IO、SARADC 等外设的寄存器。RISC-V 处理器是一种 32 位定点处理器,指令集基于 RV32IMC,包括硬件乘除法和压缩指令。
|
||||||
|
|
||||||
.. warning::
|
安装 ULP RISC-V 工具链
|
||||||
|
|
||||||
此功能不适用于 v4.4 版本。
|
|
||||||
|
|
||||||
|
|
||||||
.. toctree::
|
|
||||||
:maxdepth: 1
|
|
||||||
|
|
||||||
|
|
||||||
ULP-RISC-V 协处理器是 ULP 的一种变体,用于 ESP32-S2。与 ULP 类似,ULP-RISC-V 协处理器可以在主处理器处于低功耗模式时执行传感器读数等任务。与 ULP-FSM 不同,ULP-RISC-V 可以通过标准 GNU 工具使用 C 语言进行编程。ULP-RISC-V 可以访问 RTC_SLOW_MEM 内存区域及 RTC_CNTL、RTC_IO、SARADC 等外设的寄存器。RISC-V 处理器是一种 32 位定点处理器,指令集基于 RV32IMC,包括硬件乘除法和压缩指令。
|
|
||||||
|
|
||||||
安装 ULP-RISC-V 工具链
|
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
|
|
||||||
ULP-RISC-V 协处理器代码以 C 语言编写(也可能是汇编语言),使用基于 GCC 的 RISC-V 工具链进行编译。
|
ULP RISC-V 协处理器代码以 C 语言(或汇编语言)编写,使用基于 GCC 的 RISC-V 工具链进行编译。
|
||||||
|
|
||||||
如果你已依照 :doc:`快速入门指南 <../../../get-started/index>` 中的介绍安装好了 ESP-IDF 及其 CMake 构建系统,那么 ULP-RISC-V 工具链已经被默认安装到了你的开发环境中。
|
如果您已依照 :doc:`快速入门指南 <./../get-started/index>` 中的介绍安装好了 ESP-IDF 及其 CMake 构建系统,那么 ULP RISC-V 工具链已经被默认安装到了您的开发环境中。
|
||||||
|
|
||||||
.. note: 在早期版本的ESP-IDF中,RISC-V工具链具有不同的名称:`riscv-none-embed-gcc`。
|
.. note: 在早期版本的ESP-IDF中,RISC-V工具链具有不同的名称:`riscv-none-embed-gcc`。
|
||||||
|
|
||||||
编译 ULP-RISC-V 代码
|
编译 ULP RISC-V 代码
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
|
||||||
要将 ULP-RISC-V 代码编译为某组件的一部分,必须执行以下步骤:
|
要将 ULP RISC-V 代码编译为某组件的一部分,必须执行以下步骤:
|
||||||
|
|
||||||
1. ULP-RISC-V 代码以 C 语言或汇编语言编写(必须使用 `.S` 扩展名),必须放在组件目录中一个独立的目录中,例如 `ulp/`。
|
1. ULP RISC-V 代码以 C 语言或汇编语言编写(必须使用 `.S` 扩展名),必须放在组件目录中一个独立的目录中,例如 `ulp/`。
|
||||||
|
|
||||||
.. note: 当注册组件时(通过 ``idf_component_register``),该目录不应被添加至 ``SRC_DIRS`` 参数,因为目前 ULP-FSM 需要进行此步骤。如何正确添加 ULP 源文件,请见以下步骤。
|
.. note: 当注册组件时(通过 ``idf_component_register``),该目录不应被添加至 ``SRC_DIRS`` 参数,因为目前该步骤需用于 ULP FSM。如何正确添加 ULP 源文件,请见以下步骤。
|
||||||
|
|
||||||
2. 注册后从组件 CMakeLists.txt 中调用 ``ulp_embed_binary`` 示例如下::
|
2. 注册后从组件 CMakeLists.txt 中调用 ``ulp_embed_binary`` 示例如下::
|
||||||
|
|
||||||
@ -44,13 +33,9 @@ ULP-RISC-V 协处理器代码以 C 语言编写(也可能是汇编语言),
|
|||||||
|
|
||||||
ulp_embed_binary(${ulp_app_name} "${ulp_sources}" "${ulp_exp_dep_srcs}")
|
ulp_embed_binary(${ulp_app_name} "${ulp_sources}" "${ulp_exp_dep_srcs}")
|
||||||
|
|
||||||
``ulp_embed_binary`` 的第一个参数指定生成的 ULP 二进制文件名。生成的其他文件,
|
``ulp_embed_binary`` 的第一个参数指定生成的 ULP 二进制文件名。生成的其他文件,如 ELF 文件、.map 文件、头文件和链接器导出文件等也可使用此名称。第二个参数指定 ULP 源文件。最后,第三个参数指定组件源文件列表,其中包括生成的头文件。此列表用以正确构建依赖,并确保在构建过程中先生成后编译包含头文件的源文件。请参考下文,查看为 ULP 应用程序生成的头文件等相关概念。
|
||||||
如 ELF 文件、.map 文件、头文件和链接器导出文件等也可使用此名称。第二个参数指定 ULP 源文件。
|
|
||||||
最后,第三个参数指定组件源文件列表,其中包括生成的头文件。
|
|
||||||
此列表用以正确构建依赖,并确保在构建过程中先生成后编译包含头文件的源文件。
|
|
||||||
请参考下文,查看为 ULP 应用程序生成的头文件等相关概念。
|
|
||||||
|
|
||||||
3. 使用常规方法(例如 `idf.py app`)编译应用程序
|
3. 使用常规方法(例如 `idf.py app`)编译应用程序。
|
||||||
|
|
||||||
在内部,构建系统将按照以下步骤编译 ULP 程序:
|
在内部,构建系统将按照以下步骤编译 ULP 程序:
|
||||||
|
|
||||||
@ -58,22 +43,22 @@ ULP-RISC-V 协处理器代码以 C 语言编写(也可能是汇编语言),
|
|||||||
|
|
||||||
2. **通过 C 预处理器运行链接器脚本模版。** 模版位于 ``components/ulp/ld`` 目录中。
|
2. **通过 C 预处理器运行链接器脚本模版。** 模版位于 ``components/ulp/ld`` 目录中。
|
||||||
|
|
||||||
4. **将目标文件链接到 ELF 输出文件** (``ulp_app_name.elf``)。此步骤生成的 .Map 文件默认用于调试 (``ulp_app_name.map``)。
|
3. **将目标文件链接到 ELF 输出文件** (``ulp_app_name.elf``)。此步骤生成的 .map 文件默认用于调试 (``ulp_app_name.map``)。
|
||||||
|
|
||||||
5. **将 ELF 文件中的内容转储为二进制文件** (``ulp_app_name.bin``),以便嵌入到应用程序中。
|
4. **将 ELF 文件中的内容转储为二进制文件** (``ulp_app_name.bin``),以便嵌入到应用程序中。
|
||||||
|
|
||||||
6. 使用 ``riscv32-esp-elf-nm`` 在 ELF 文件中 **生成全局符号列表** (``ulp_app_name.sym``)。
|
5. 使用 ``riscv32-esp-elf-nm`` 在 ELF 文件中 **生成全局符号列表** (``ulp_app_name.sym``)。
|
||||||
|
|
||||||
7. **创建 LD 导出脚本和头文件** (``ulp_app_name.ld`` 和 ``ulp_app_name.h``),包含来自 ``ulp_app_name.sym`` 的符号。此步骤可借助 ``esp32ulp_mapgen.py`` 工具来完成。
|
6. **创建 LD 导出脚本和头文件** (``ulp_app_name.ld`` 和 ``ulp_app_name.h``),包含来自 ``ulp_app_name.sym`` 的符号。此步骤可借助 ``esp32ulp_mapgen.py`` 工具来完成。
|
||||||
|
|
||||||
8. **将生成的二进制文件添加到要嵌入应用程序的二进制文件列表中。**
|
7. **将生成的二进制文件添加到要嵌入应用程序的二进制文件列表中。**
|
||||||
|
|
||||||
访问 ULP-RISC-V 程序变量
|
访问 ULP RISC-V 程序变量
|
||||||
----------------------------
|
----------------------------
|
||||||
|
|
||||||
在 ULP-RISC-V 程序中定义的全局符号也可以在主程序中使用。
|
在 ULP RISC-V 程序中定义的全局符号也可以在主程序中使用。
|
||||||
|
|
||||||
例如,ULP-RISC-V 程序可以定义 ``measurement_count`` 变量,此变量可以定义程序从深度睡眠中唤醒芯片之前需要进行的 ADC 测量的次数。
|
例如,ULP RISC-V 程序可以定义 ``measurement_count`` 变量,此变量可以定义程序从深度睡眠中唤醒芯片之前需要进行的 ADC 测量的次数。
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
@ -87,21 +72,21 @@ ULP-RISC-V 协处理器代码以 C 语言编写(也可能是汇编语言),
|
|||||||
...do something.
|
...do something.
|
||||||
}
|
}
|
||||||
|
|
||||||
构建系统生成定义 ULP 编程中全局符号的 ``${ULP_APP_NAME}.h`` 和 ``${ULP_APP_NAME}.ld`` 文件,使主程序能够访问全局 ULP-RISC-V 程序变量。上述两个文件包含 ULP 程序中定义的所有全局符号,且这些符号均以 ``ulp_`` 开头,。
|
构建系统生成定义 ULP 编程中全局符号的 ``${ULP_APP_NAME}.h`` 和 ``${ULP_APP_NAME}.ld`` 文件,使主程序能够访问全局 ULP RISC-V 程序变量。上述两个文件包含 ULP RISC-V 程序中定义的所有全局符号,且这些符号均以 ``ulp_`` 开头。
|
||||||
|
|
||||||
头文件包含对此类符号的声明
|
头文件包含对此类符号的声明:
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
extern uint32_t ulp_measurement_count;
|
extern uint32_t ulp_measurement_count;
|
||||||
|
|
||||||
注意,所有符号(包括变量、数组、函数)均被声明为 ``uint32_t``。 函数和数组需要先获取符号地址、再转换为适当的类型。
|
注意,所有符号(包括变量、数组、函数)均被声明为 ``uint32_t``。函数和数组需要先获取符号地址,再转换为适当的类型。
|
||||||
|
|
||||||
生成的链接器文本定义了符号在 RTC_SLOW_MEM 中的位置::
|
生成的链接器文本定义了符号在 RTC_SLOW_MEM 中的位置::
|
||||||
|
|
||||||
PROVIDE ( ulp_measurement_count = 0x50000060 );
|
PROVIDE ( ulp_measurement_count = 0x50000060 );
|
||||||
|
|
||||||
要从主程序访问 ULP-RISC-V 程序变量,需使用 ``include`` 语句包含生成的头文件。这样,就可以像访问常规变量一样访问 ULP 程序变量。
|
要从主程序访问 ULP RISC-V 程序变量,需使用 ``include`` 语句包含生成的头文件。这样,就可以像访问常规变量一样访问 ULP RISC-V 程序变量。
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
@ -111,14 +96,14 @@ ULP-RISC-V 协处理器代码以 C 语言编写(也可能是汇编语言),
|
|||||||
ulp_measurement_count = 64;
|
ulp_measurement_count = 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
启动 ULP-RISC-V 程序
|
启动 ULP RISC-V 程序
|
||||||
-------------------------------
|
-------------------------------
|
||||||
|
|
||||||
要运行 ULP-RISC-V 程序,主程序需要调用 :cpp:func:`ulp_riscv_load_binary` 函数,将 ULP 程序加载到 RTC 内存中,然后调用 :cpp:func:`ulp_riscv_run` 函数,启动 ULP-RISC-V 程序。
|
要运行 ULP RISC-V 程序,主程序需要调用 :cpp:func:`ulp_riscv_load_binary` 函数,将 ULP 程序加载到 RTC 内存中,然后调用 :cpp:func:`ulp_riscv_run` 函数,启动 ULP RISC-V 程序。
|
||||||
|
|
||||||
注意,必须在 menuconfig 中启用 `CONFIG_ESP32S2_ULP_COPROC_ENABLED` 和 `CONFIG_ESP32S2_ULP_COPROC_RISCV` 选项,以便为 ULP 预留内存。"RTC slow memory reserved for coprocessor" 选项设置的值必须足够存储 ULP 代码和数据。如果应用程序组件包含多个 ULP 程序,RTC 内存必须足以容纳最大的程序。
|
注意,必须在 menuconfig 中启用 `CONFIG_{IDF_TARGET_CFG_PREFIX}_ULP_COPROC_ENABLED` 和 `CONFIG_{IDF_TARGET_CFG_PREFIX}_ULP_COPROC_RISCV` 选项,以便正常运行 ULP RISC-V 程序。``RTC slow memory reserved for coprocessor`` 选项设置的值必须足够存储 ULP RISC-V 代码和数据。如果应用程序组件包含多个 ULP 程序,RTC 内存必须足以容纳最大的程序。
|
||||||
|
|
||||||
每个 ULP-RISC-V 程序均以二进制 BLOB 的形式嵌入到 ESP-IDF 应用程序中。应用程序可以引用此 BLOB,并以下面的方式加载此 BLOB(假设 ULP_APP_NAME 已被定义为 ``ulp_app_name``):
|
每个 ULP RISC-V 程序均以二进制 BLOB 的形式嵌入到 ESP-IDF 应用程序中。应用程序可以引用此 BLOB,并以下面的方式加载此 BLOB(假设 ULP_APP_NAME 已被定义为 ``ulp_app_name``):
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
@ -130,26 +115,34 @@ ULP-RISC-V 协处理器代码以 C 语言编写(也可能是汇编语言),
|
|||||||
(bin_end - bin_start)) );
|
(bin_end - bin_start)) );
|
||||||
}
|
}
|
||||||
|
|
||||||
.. doxygenfunction:: ulp_riscv_load_binary()
|
|
||||||
|
|
||||||
一旦上述程序加载到 RTC 内存后,应用程序即可调用 :cpp:func:`ulp_riscv_run` 函数启动此程序:
|
一旦上述程序加载到 RTC 内存后,应用程序即可调用 :cpp:func:`ulp_riscv_run` 函数启动此程序:
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
ESP_ERROR_CHECK( ulp_riscv_run() );
|
ESP_ERROR_CHECK( ulp_riscv_run() );
|
||||||
|
|
||||||
.. doxygenfunction:: ulp_riscv_run()
|
ULP RISC-V 程序流
|
||||||
|
|
||||||
ULP-RISC-V 程序流
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
||||||
ULP-RISC-V 协处理器由定时器启动,调用 :cpp:func:`ulp_riscv_run` 即可启动定时器。定时器为 RTC_SLOW_CLK 的 Tick 事件计数(默认情况下,Tick 由内部 90 kHz RC 振荡器产生)。Tick 数值使用 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 寄存器设置。启用 ULP 时,使用 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 设置定时器 Tick 数值。
|
{IDF_TARGET_RTC_CLK_FRE:default="150kHz", esp32s2="90kHz", esp32s3="136kHz"}
|
||||||
|
|
||||||
|
ULP RISC-V 协处理器由定时器启动,调用 :cpp:func:`ulp_riscv_run` 即可启动定时器。定时器为 RTC_SLOW_CLK 的 Tick 事件计数(默认情况下,Tick 由内部 90 kHz RC 振荡器产生)。Tick 数值使用 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 寄存器设置。启用 ULP 时,使用 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 设置定时器 Tick 数值。
|
||||||
|
|
||||||
此应用程序可以调用 :cpp:func:`ulp_set_wakeup_period` 函数来设置 ULP 定时器周期值 (RTC_CNTL_ULP_CP_TIMER_1_REG)。
|
此应用程序可以调用 :cpp:func:`ulp_set_wakeup_period` 函数来设置 ULP 定时器周期值 (RTC_CNTL_ULP_CP_TIMER_1_REG)。
|
||||||
|
|
||||||
一旦定时器数到 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 寄存器中设置的 Tick 数,ULP 协处理器就会启动,并调用 :cpp:func:`ulp_riscv_run` 的入口点开始运行程序。
|
一旦定时器数到 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 寄存器中设置的 Tick 数,ULP RISC-V 协处理器就会启动,并调用 :cpp:func:`ulp_riscv_run` 的入口点开始运行程序。
|
||||||
|
|
||||||
程序保持运行,直至 ``RTC_CNTL_COCPU_CTRL_REG`` 寄存器中的 ``RTC_CNTL_COCPU_DONE`` 字段被置位或因非法处理器状态出现陷阱。一旦程序停止,ULP 协处理器会关闭电源,定时器再次启动。
|
程序保持运行,直至 ``RTC_CNTL_COCPU_CTRL_REG`` 寄存器中的 ``RTC_CNTL_COCPU_DONE`` 字段被置位或因非法处理器状态出现陷阱。一旦程序停止,ULP RISC-V 协处理器会关闭电源,定时器再次启动。
|
||||||
|
|
||||||
如需禁用定时器(有效防止 ULP 程序再次运行),请清除 ``RTC_CNTL_STATE0_REG`` 寄存器中的 ``RTC_CNTL_ULP_CP_SLP_TIMER_EN`` 位,此项操作可在 ULP 代码或主程序中进行。
|
如需禁用定时器(有效防止 ULP 程序再次运行),请清除 ``RTC_CNTL_STATE0_REG`` 寄存器中的 ``RTC_CNTL_ULP_CP_SLP_TIMER_EN`` 位,此项操作可在 ULP 代码或主程序中进行。
|
||||||
|
|
||||||
|
应用示例
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
* 主处理器处于 Deep-sleep 状态时,ULP RISC-V 协处理器轮询 GPIO::example:`system/ulp_riscv/gpio`。
|
||||||
|
* 主处理器处于 Deep-sleep 状态时,ULP RISC-V 协处理器读取外部温度传感器::example:`system/ulp_riscv/ds18b20_onewire`。
|
||||||
|
|
||||||
|
API 参考
|
||||||
|
-------------
|
||||||
|
|
||||||
|
.. include-build-file:: inc/ulp_riscv.inc
|
@ -3,34 +3,42 @@ ULP 协处理器编程
|
|||||||
|
|
||||||
:link_to_translation:`en:[English]`
|
:link_to_translation:`en:[English]`
|
||||||
|
|
||||||
.. toctree::
|
ULP(Ultra Low Power,超低功耗)协处理器是一种简单的有限状态机 (FSM),可以在主处理器处于深度睡眠模式时,使用 ADC、温度传感器和外部 I2C 传感器执行测量操作。ULP 协处理器可以访问 RTC_SLOW_MEM 内存区域及 RTC_CNTL、RTC_IO、SARADC 外设中的寄存器。ULP 协处理器使用 32 位固定宽度的指令,32 位内存寻址,配备 4 个 16 位通用寄存器。在 ESP-IDF 项目中,此协处理器被称作 `ULP FSM`。
|
||||||
:maxdepth: 1
|
|
||||||
|
|
||||||
{IDF_TARGET_NAME} ULP 指令集参考 <ulp_instruction_set>
|
.. only:: esp32s2 or esp32s3
|
||||||
使用宏进行编程(遗留) <ulp_macros>
|
|
||||||
|
|
||||||
|
{IDF_TARGET_NAME} 基于 RISC-V 指令集架构提供另一种 ULP 协处理器。关于 `ULP RISC-V` 的详细信息,请参考 :doc:`ULP-RISC-V Coprocessor <./ulp-risc-v>`。
|
||||||
ULP(Ultra Low Power 超低功耗)协处理器是一种简单的有限状态机 (FSM),可以在主处理器处于深度睡眠模式时,使用 ADC、温度传感器和外部 I2C 传感器执行测量操作。ULP 协处理器可以访问 RTC_SLOW_MEM 内存区域及 RTC_CNTL、RTC_IO、SARADC 外设中的寄存器。ULP 协处理器使用 32 位固定宽度的指令,32 位内存寻址,配备 4 个 16 位通用寄存器。
|
|
||||||
|
|
||||||
安装工具链
|
安装工具链
|
||||||
----------
|
----------
|
||||||
|
|
||||||
ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp 工具链`_ 进行编译。
|
ULP FSM 协处理器代码由汇编语言编写,使用 `binutils-esp32ulp 工具链`_ 进行编译。
|
||||||
|
|
||||||
如果你已经按照 :doc:`快速入门指南 <../../../get-started/index>` 中的介绍安装好了 ESP-IDF 及其 CMake 构建系统,那么 ULP 工具链已经被默认安装到了你的开发环境中。
|
如果您已经按照 :doc:`快速入门指南 <../../../get-started/index>` 中的介绍安装好了 ESP-IDF 及其 CMake 构建系统,那么 ULP 工具链已经被默认安装到了您的开发环境中。
|
||||||
|
|
||||||
|
编写 ULP FSM
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
使用受支持的指令集即可编写 ULP FSM 协处理器,此外也可使用主处理器上的 C 语言宏进行编程。以下小节分别介绍了这两种方法:
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 1
|
||||||
|
|
||||||
|
{IDF_TARGET_NAME} ULP 指令集参考 <ulp_instruction_set>
|
||||||
|
使用宏进行编程(遗留) <ulp_macros>
|
||||||
|
|
||||||
.. only:: esp32
|
.. only:: esp32
|
||||||
|
|
||||||
如果你的 ESP-IDF 仍在使用传统的基于 GNU Make 的构建系统,请参考 :doc:`ulp-legacy` 一文中的说明,完成工具链的安装。
|
如果你的 ESP-IDF 仍在使用传统的基于 GNU Make 的构建系统,请参考 :doc:`ulp-legacy` 一文中的说明,完成工具链的安装。
|
||||||
|
|
||||||
编译 ULP 代码
|
编译 ULP 代码
|
||||||
-------------
|
--------------
|
||||||
|
|
||||||
若需要将 ULP 代码编译为某组件的一部分,则必须执行以下步骤:
|
若需要将 ULP FSM 代码编译为某组件的一部分,则必须执行以下步骤:
|
||||||
|
|
||||||
1. 用汇编语言编写的 ULP 代码必须导入到一个或多个 `.S` 扩展文件中,且这些文件必须放在组件目录中一个独立的目录中,例如 `ulp/`。
|
1. 用汇编语言编写的 ULP FSM 代码必须导入到一个或多个 `.S` 扩展文件中,且这些文件必须放在组件目录中一个独立的目录中,例如 `ulp/`。
|
||||||
|
|
||||||
.. note: 在注册组件(通过 ``idf_component_register``)时,不应将该目录添加到 ``SRC_DIRS`` 参数中。因为 ESP-IDF 构建系统将基于文件扩展名编译在 ``SRC_DIRS`` 中搜索到的文件。对于 ``.S`` 文件,使用的是 ``{IDF_TARGET_TOOLCHAIN_PREFIX}-as`` 汇编器。但这并不适用于 ULP 程序集文件,因此体现这种区别最简单的方式就是将 ULP 程序集文件放到单独的目录中。同样,ULP 程序集源文件也 **不应该** 添加到 ``SRCS`` 中。请参考如下步骤,查看如何正确添加 ULP 程序集源文件。
|
.. note: 在注册组件(通过 ``idf_component_register``)时,不应将该目录添加到 ``SRC_DIRS`` 参数中。因为 ESP-IDF 构建系统将基于文件扩展名编译在 ``SRC_DIRS`` 中搜索到的文件。对于 ``.S`` 文件,使用的是 ``{IDF_TARGET_TOOLCHAIN_PREFIX}-as`` 汇编器。但这并不适用于 ULP FSM 程序集文件,因此体现这种区别最简单的方式就是将 ULP FSM 程序集文件放到单独的目录中。同样,ULP FSM 程序集源文件也 **不应该** 添加到 ``SRCS`` 中。请参考如下步骤,查看如何正确添加 ULP FSM 程序集源文件。
|
||||||
|
|
||||||
2. 注册后从组件 CMakeLists.txt 中调用 ``ulp_embed_binary`` 示例如下::
|
2. 注册后从组件 CMakeLists.txt 中调用 ``ulp_embed_binary`` 示例如下::
|
||||||
|
|
||||||
@ -43,11 +51,11 @@ ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp
|
|||||||
|
|
||||||
ulp_embed_binary(${ulp_app_name} "${ulp_s_sources}" "${ulp_exp_dep_srcs}")
|
ulp_embed_binary(${ulp_app_name} "${ulp_s_sources}" "${ulp_exp_dep_srcs}")
|
||||||
|
|
||||||
``ulp_embed_binary`` 的第一个参数为 ULP 二进制文件命名。指定的此名称也用于生成的其他文件,如:ELF 文件、.map 文件、头文件和链接器导出文件。第二个参数指定 ULP 程序集源文件。最后,第三个参数指定组件源文件列表,其中包括被生成的头文件。此列表用以建立正确的依赖项,并确保在编译这些文件之前先创建生成的头文件。有关 ULP 应用程序生成的头文件等相关概念,请参考下文。
|
``ulp_embed_binary`` 的第一个参数为 ULP 二进制文件命名。指定的此名称也用于生成的其他文件,如:ELF 文件、.map 文件、头文件和链接器导出文件。第二个参数指定 ULP FSM 程序集源文件。最后,第三个参数指定组件源文件列表,其中包括被生成的头文件。此列表用以建立正确的依赖项,并确保在编译这些文件之前先创建生成的头文件。有关 ULP FSM 应用程序生成的头文件等相关概念,请参考下文。
|
||||||
|
|
||||||
3. 使用常规方法(例如 `idf.py app`)编译应用程序
|
3. 使用常规方法(例如 `idf.py app`)编译应用程序。
|
||||||
|
|
||||||
在内部,构建系统将按照以下步骤编译 ULP 程序:
|
在内部,构建系统将按照以下步骤编译 ULP FSM 程序:
|
||||||
|
|
||||||
1. **通过 C 预处理器运行每个程序集文件 (foo.S)。** 此步骤在组件编译目录中生成预处理的程序集文件 (foo.ulp.S),同时生成依赖文件 (foo.ulp.d)。
|
1. **通过 C 预处理器运行每个程序集文件 (foo.S)。** 此步骤在组件编译目录中生成预处理的程序集文件 (foo.ulp.S),同时生成依赖文件 (foo.ulp.d)。
|
||||||
|
|
||||||
@ -55,7 +63,7 @@ ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp
|
|||||||
|
|
||||||
3. **通过 C 预处理器运行链接器脚本模板。** 模板位于 ``components/ulp/ld`` 目录中。
|
3. **通过 C 预处理器运行链接器脚本模板。** 模板位于 ``components/ulp/ld`` 目录中。
|
||||||
|
|
||||||
4. **将目标文件链接到 ELF 输出文件** (``ulp_app_name.elf``)。此步骤生成的.map 文件 (``ulp_app_name.map``) 默认用于调试。
|
4. **将目标文件链接到 ELF 输出文件** (``ulp_app_name.elf``)。此步骤生成的 .map 文件 (``ulp_app_name.map``) 默认用于调试。
|
||||||
|
|
||||||
5. **将 ELF 文件中的内容转储为二进制文件** (``ulp_app_name.bin``),以便嵌入到应用程序中。
|
5. **将 ELF 文件中的内容转储为二进制文件** (``ulp_app_name.bin``),以便嵌入到应用程序中。
|
||||||
|
|
||||||
@ -65,17 +73,17 @@ ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp
|
|||||||
|
|
||||||
8. **将生成的二进制文件添加到要嵌入应用程序的二进制文件列表中。**
|
8. **将生成的二进制文件添加到要嵌入应用程序的二进制文件列表中。**
|
||||||
|
|
||||||
访问 ULP 程序变量
|
访问 ULP FSM 程序变量
|
||||||
-----------------
|
------------------------
|
||||||
|
|
||||||
在 ULP 程序中定义的全局符号也可以在主程序中使用。
|
在 ULP FSM 程序中定义的全局符号也可以在主程序中使用。
|
||||||
|
|
||||||
例如,ULP 程序可以定义 ``measurement_count`` 变量,此变量可以定义程序从深度睡眠中唤醒芯片之前需要进行的 ADC 测量的次数::
|
例如,ULP FSM 程序可以定义 ``measurement_count`` 变量,此变量可以定义程序从深度睡眠中唤醒芯片之前需要进行的 ADC 测量的次数::
|
||||||
|
|
||||||
.global measurement_count
|
.global measurement_count
|
||||||
measurement_count: .long 0
|
measurement_count: .long 0
|
||||||
|
|
||||||
/* later, use measurement_count */
|
// later, use measurement_count
|
||||||
move r3, measurement_count
|
move r3, measurement_count
|
||||||
ld r3, r3, 0
|
ld r3, r3, 0
|
||||||
|
|
||||||
@ -100,18 +108,18 @@ ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp
|
|||||||
ulp_measurement_count = 64;
|
ulp_measurement_count = 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
注意,ULP 程序在 RTC 内存中只能使用 32 位字的低 16 位,因为寄存器是 16 位的,并且不具备从字的高位加载的指令。
|
.. only:: esp32
|
||||||
|
|
||||||
同样,ULP 储存指令将寄存器值写入 32 位字的低 16 位中。高 16 位写入的值取决于储存指令的地址,因此在读取 ULP 写的变量时,主应用程序需要屏蔽高 16 位,例如::
|
注意,ULP FSM 程序在 RTC 内存中只能使用 32 位字的低 16 位,因为寄存器是 16 位的,并且不具备从字的高位加载的指令。同样,ULP 储存指令将寄存器值写入 32 位字的低 16 位中。高 16 位写入的值取决于储存指令的地址,因此在读取 ULP 协处理器写的变量时,主应用程序需要屏蔽高 16 位,例如::
|
||||||
|
|
||||||
printf("Last measurement value: %d\n", ulp_last_measurement & UINT16_MAX);
|
printf("Last measurement value: %d\n", ulp_last_measurement & UINT16_MAX);
|
||||||
|
|
||||||
启动 ULP 程序
|
启动 ULP FSM 程序
|
||||||
-------------
|
--------------------
|
||||||
|
|
||||||
要运行 ULP 程序,主应用程序需要调用 ``ulp_load_binary`` 函数将 ULP 程序加载到 RTC 内存中,然后调用 ``ulp_run`` 函数,启动 ULP 程序。
|
要运行 ULP FSM 程序,主应用程序需要调用 :cpp:func:`ulp_load_binary` 函数将 ULP 程序加载到 RTC 内存中,然后调用 :cpp:func:`ulp_run` 函数,启动 ULP 程序。
|
||||||
|
|
||||||
注意,在 menuconfig 中必须启用 "Enable Ultra Low Power (ULP) Coprocessor" 选项,以便为 ULP 预留内存。"RTC slow memory reserved for coprocessor" 选项设置的值必须足够储存 ULP 代码和数据。如果应用程序组件包含多个 ULP 程序,则 RTC 内存必须足以容纳最大的程序。
|
注意,在 menuconfig 中必须启用 ``Enable Ultra Low Power (ULP) Coprocessor`` 选项,以便正常运行 ULP,并且必须设置 ``ULP Co-processor type`` 选项,以便选择要使用的 ULP 类型。 ``RTC slow memory reserved for coprocessor`` 选项设置的值必须足够储存 ULP 代码和数据。如果应用程序组件包含多个 ULP 程序,则 RTC 内存必须足以容纳最大的程序。
|
||||||
|
|
||||||
每个 ULP 程序均以二进制 BLOB 的形式嵌入到 ESP-IDF 应用程序中。应用程序可以引用此 BLOB,并以下面的方式加载此 BLOB(假设 ULP_APP_NAME 已被定义为 ``ulp_app_name``)::
|
每个 ULP 程序均以二进制 BLOB 的形式嵌入到 ESP-IDF 应用程序中。应用程序可以引用此 BLOB,并以下面的方式加载此 BLOB(假设 ULP_APP_NAME 已被定义为 ``ulp_app_name``)::
|
||||||
|
|
||||||
@ -120,38 +128,32 @@ ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp
|
|||||||
|
|
||||||
void start_ulp_program() {
|
void start_ulp_program() {
|
||||||
ESP_ERROR_CHECK( ulp_load_binary(
|
ESP_ERROR_CHECK( ulp_load_binary(
|
||||||
0 /* load address, set to 0 when using default linker scripts */,
|
0 // load address, set to 0 when using default linker scripts
|
||||||
bin_start,
|
bin_start,
|
||||||
(bin_end - bin_start) / sizeof(uint32_t)) );
|
(bin_end - bin_start) / sizeof(uint32_t)) );
|
||||||
}
|
}
|
||||||
|
|
||||||
.. doxygenfunction:: ulp_load_binary
|
|
||||||
|
|
||||||
一旦上述程序加载到 RTC 内存后,应用程序即可启动此程序,并将入口点的地址传递给 ``ulp_run`` 函数::
|
一旦上述程序加载到 RTC 内存后,应用程序即可启动此程序,并将入口点的地址传递给 ``ulp_run`` 函数::
|
||||||
|
|
||||||
ESP_ERROR_CHECK( ulp_run(&ulp_entry - RTC_SLOW_MEM) );
|
ESP_ERROR_CHECK( ulp_run(&ulp_entry - RTC_SLOW_MEM) );
|
||||||
|
|
||||||
.. doxygenfunction:: ulp_run
|
|
||||||
|
|
||||||
上述生成的头文件 ``${ULP_APP_NAME}.h`` 声明了入口点符号。在 ULP 应用程序的汇编源代码中,此符号必须标记为 ``.global``::
|
上述生成的头文件 ``${ULP_APP_NAME}.h`` 声明了入口点符号。在 ULP 应用程序的汇编源代码中,此符号必须标记为 ``.global``::
|
||||||
|
|
||||||
|
|
||||||
.global entry
|
.global entry
|
||||||
entry:
|
entry:
|
||||||
/* code starts here */
|
// code starts here
|
||||||
|
|
||||||
.. only:: esp32
|
.. only:: esp32
|
||||||
|
|
||||||
ESP32 ULP 程序流
|
ESP32 ULP 程序流
|
||||||
------------------
|
-------------------
|
||||||
|
|
||||||
ESP32 ULP 协处理器由定时器启动,而调用 ``ulp_run`` 则可启动此定时器。定时器为 RTC_SLOW_CLK 的 Tick 事件计数(默认情况下,Tick 由内部 150 KHz RC 振荡器生成)。使用 ``SENS_ULP_CP_SLEEP_CYCx_REG`` 寄存器 (x = 0..4) 设置 Tick 数值。第一次启动 ULP 时,使用 ``SENS_ULP_CP_SLEEP_CYC0_REG`` 设置定时器 Tick 数值,之后,ULP 程序可以使用 ``sleep`` 指令来选择另一个 ``SENS_ULP_CP_SLEEP_CYCx_REG`` 寄存器。
|
ESP32 ULP 协处理器由定时器启动,而调用 :cpp:func:`ulp_run` 则可启动此定时器。定时器为 RTC_SLOW_CLK 的 Tick 事件计数(默认情况下,Tick 由内部 150 KHz RC 振荡器生成)。使用 ``SENS_ULP_CP_SLEEP_CYCx_REG`` 寄存器 (x = 0..4) 设置 Tick 数值。第一次启动 ULP 时,使用 ``SENS_ULP_CP_SLEEP_CYC0_REG`` 设置定时器 Tick 数值,之后,ULP 程序可以使用 ``sleep`` 指令来选择另一个 ``SENS_ULP_CP_SLEEP_CYCx_REG`` 寄存器。
|
||||||
|
|
||||||
此应用程序可以调用 ``ulp_set_wakeup_period`` 函数来设置 ULP 定时器周期值 (SENS_ULP_CP_SLEEP_CYCx_REG, x = 0..4)。
|
此应用程序可以调用 ``ulp_set_wakeup_period`` 函数来设置 ULP 定时器周期值 (SENS_ULP_CP_SLEEP_CYCx_REG, x = 0..4)。
|
||||||
|
|
||||||
.. doxygenfunction:: ulp_set_wakeup_period
|
一旦定时器计数到 ``SENS_ULP_CP_SLEEP_CYCx_REG`` 寄存器设定的 Tick 数值,ULP 协处理器就会启动,并调用 :cpp:func:`ulp_run` 的入口点开始运行程序。
|
||||||
|
|
||||||
一旦定时器计数到 ``SENS_ULP_CP_SLEEP_CYCx_REG`` 寄存器设定的 Tick 数值,ULP 协处理器就会启动,并调用 ``ulp_run`` 的入口点开始运行程序。
|
|
||||||
|
|
||||||
程序保持运行,直到遇到 ``halt`` 指令或非法指令。一旦程序停止,ULP 协处理器电源关闭,定时器再次启动。
|
程序保持运行,直到遇到 ``halt`` 指令或非法指令。一旦程序停止,ULP 协处理器电源关闭,定时器再次启动。
|
||||||
|
|
||||||
@ -163,17 +165,25 @@ ULP 协处理器代码是用汇编语言编写的,并使用 `binutils-esp32ulp
|
|||||||
{IDF_TARGET_NAME} ULP 程序流
|
{IDF_TARGET_NAME} ULP 程序流
|
||||||
----------------------------
|
----------------------------
|
||||||
|
|
||||||
{IDF_TARGET_NAME} ULP 协处理器由定时器启动,调用 ``ulp_run`` 则可启动此定时器。定时器为 RTC_SLOW_CLK 的 Tick 事件计数(默认情况下,Tick 由内部 90 KHz RC 振荡器生成)。使用 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 寄存器设置 Tick 数值。
|
{IDF_TARGET_NAME} ULP 协处理器由定时器启动,调用 :cpp:func:`ulp_run` 则可启动此定时器。定时器为 RTC_SLOW_CLK 的 Tick 事件计数(默认情况下,Tick 由内部 90 KHz RC 振荡器生成)。使用 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 寄存器设置 Tick 数值。
|
||||||
|
|
||||||
此应用程序可以调用 ``ulp_set_wakeup_period`` 函数来设置 ULP 定时器周期值。
|
此应用程序可以调用 :cpp:func:`ulp_set_wakeup_period` 函数来设置 ULP 定时器周期值。
|
||||||
|
|
||||||
.. doxygenfunction:: ulp_set_wakeup_period
|
一旦定时器计数到 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 寄存器设定的 Tick 数值,ULP 协处理器就会启动,并调用 :cpp:func:`ulp_run` 的入口点开始运行程序。
|
||||||
|
|
||||||
一旦定时器计数到 ``RTC_CNTL_ULP_CP_TIMER_1_REG`` 寄存器设定的 Tick 数值,ULP 协处理器就会启动,并调用 ``ulp_run`` 的入口点开始运行程序。
|
|
||||||
|
|
||||||
程序保持运行,直到遇到 ``halt`` 指令或非法指令。一旦程序停止,ULP 协处理器电源关闭,定时器再次启动。
|
程序保持运行,直到遇到 ``halt`` 指令或非法指令。一旦程序停止,ULP 协处理器电源关闭,定时器再次启动。
|
||||||
|
|
||||||
如果想禁用定时器(有效防止 ULP 程序再次运行),可在 ULP 代码或主程序中清除 ``RTC_CNTL_STATE0_REG`` 寄存器中的 ``RTC_CNTL_ULP_CP_SLP_TIMER_EN`` 位。
|
如果想禁用定时器(有效防止 ULP 程序再次运行),可在 ULP 代码或主程序中清除 ``RTC_CNTL_ULP_CP_TIMER_REG`` 寄存器中的 ``RTC_CNTL_ULP_CP_SLP_TIMER_EN`` 位。
|
||||||
|
|
||||||
|
应用示例
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
* 主处理器处于 Deep-sleep 状态时,ULP FSM 协处理器对 IO 脉冲进行计数::example:`system/ulp_fsm/ulp`。
|
||||||
|
* 主处理器处于 Deep-sleep 状态时,ULP FSM 协处理器轮询 ADC::example:`system/ulp_fsm/ulp_adc`。
|
||||||
|
|
||||||
|
API 参考
|
||||||
|
-------------
|
||||||
|
|
||||||
|
.. include-build-file:: inc/ulp_common.inc
|
||||||
|
|
||||||
.. _binutils-esp32ulp 工具链: https://github.com/espressif/binutils-esp32ulp
|
.. _binutils-esp32ulp 工具链: https://github.com/espressif/binutils-esp32ulp
|
1
docs/zh_CN/api-guides/ulp_instruction_set.rst
Normal file
1
docs/zh_CN/api-guides/ulp_instruction_set.rst
Normal file
@ -0,0 +1 @@
|
|||||||
|
.. include:: ../../en/api-guides/ulp_instruction_set.rst
|
1
docs/zh_CN/api-guides/ulp_macros.rst
Normal file
1
docs/zh_CN/api-guides/ulp_macros.rst
Normal file
@ -0,0 +1 @@
|
|||||||
|
.. include:: ../../en/api-guides/ulp_macros.rst
|
@ -1 +0,0 @@
|
|||||||
.. include:: ../../en/api-guides/ulps2_instruction_set.rst
|
|
@ -1 +1 @@
|
|||||||
.. include:: ../../../en/api-reference/system/index.rst
|
.. include:: ../../../en/api-reference/system/index.rst
|
@ -1 +0,0 @@
|
|||||||
.. include:: ../../../en/api-reference/system/ulp_instruction_set.rst
|
|
@ -1 +0,0 @@
|
|||||||
.. include:: ../../../en/api-reference/system/ulp_macros.rst
|
|
Loading…
Reference in New Issue
Block a user