From 9615a6b4840ea073ca48c81a6d6e614c34a747ec Mon Sep 17 00:00:00 2001 From: LILYGO_L <135582120+Llgok@users.noreply.github.com> Date: Sat, 25 May 2024 15:05:31 +0800 Subject: [PATCH] Add ULP-FSM related examples --- README.md | 4 +- examples/ULP_ADC/ULP_ADC.ino | 80 ++++++++++++++++++++++++++++++++ examples/ULP_Count/ULP_Count.ino | 72 ++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 examples/ULP_ADC/ULP_ADC.ino create mode 100644 examples/ULP_Count/ULP_Count.ino diff --git a/README.md b/README.md index df9c1bd..3b5aa4b 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,9 @@ ├── sd # T-Display-TF Shield example ├── tft # TFT_eSPI example ├── touch_test # Capacitive touch test example -└── usb_hid_pad # Capacitive Touch Screen Simulation USB HID Example +├── usb_hid_pad # Capacitive Touch Screen Simulation USB HID Example +├── ULP_ADC # Example of ADC detection for ULP-FSM(arduino_esp32 version: 3.0.0-rc3) +└── ULP_Count # Example of register counting for ULP-FSM(arduino_esp32 version: 3.0.0-rc3) ``` ## 3️⃣ PlatformIO Quick Start (Recommended) diff --git a/examples/ULP_ADC/ULP_ADC.ino b/examples/ULP_ADC/ULP_ADC.ino new file mode 100644 index 0000000..c961df7 --- /dev/null +++ b/examples/ULP_ADC/ULP_ADC.ino @@ -0,0 +1,80 @@ +/* + * @Description: ULP检测ADC + * 适用arduino_esp32版本:3.0.0-rc3 + * @version: V1.0.0 + * @Author: None + * @Date: 2024-04-29 19:02:41 + * @LastEditors: LILYGO_L + * @LastEditTime: 2024-05-25 14:53:31 + * @License: GPL 3.0 + */ +#include +#include "driver/rtc_io.h" +#include "sdkconfig.h" +#include "soc/rtc_cntl_reg.h" +#include "esp32s3/ulp.h" + +void ULP_Initialization() +{ + memset(RTC_SLOW_MEM, 0, 100); + + const ulp_insn_t program[] = { + I_MOVI(R0, 0), + I_MOVI(R1, 0), + I_MOVI(R2, 0), + + M_LABEL(1), // do { + + // delay(1000); + I_MOVI(R0, 200), // R0 = n * 1000 / 5, where n is the number of seconds to delay, 200 = 1 s + M_LABEL(2), // do { + // delay (5); + // since ULP runs at 8 MHz + // 40000 cycles correspond to 5 ms (max possible delay is 65535 cycles or 8.19 ms) + I_DELAY(40000), + I_SUBI(R0, R0, 1), // R0 --; + M_BG(2, 1), // } while (R0 >= 1); ... jump to label 2 if R0 > 0 + + I_ADC(R2, 0, 0), // R2 ≈ adc1_get_raw (ADC1_CHANNEL_0); // GPIO 1 + I_ADDI(R1, R1, 1), // R1 ++ + // store sample count to RTC_SLOW_MEM [0] + I_ST(R1, 0, 0), // RTC_SLOW_MEM [1] = R1; + I_ST(R2, 0, 1), // RTC_SLOW_MEM [2] = R3; + + I_MOVR(R0, R1), // while (R1 < 100); + M_BL(1, 1000), // ... jump to label 1 + + I_END(), + }; + + size_t size = sizeof(program) / sizeof(ulp_insn_t); + ulp_process_macros_and_load(32, program, &size); + ulp_run(32); + + // in which it also prints the debug output mentioned earlier + // Serial.printf("ULP Mem in main app: %d\n", CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM); +} + +void setup() +{ + Serial.begin(115200); + Serial.println("Ciallo"); + + // pinMode(7, INPUT_PULLDOWN); + // analogReadResolution(12); + + // Serial.printf("IO7 ADC Voltage: %.03f V\n", ((float)analogReadMilliVolts(7)) / 1000.0); + + ULP_Initialization(); +} + +void loop() +{ + for (int i = 0; i < 10; i++) + { + Serial.printf("RTC_SLOW_MEM[%d]: %d\n", i, RTC_SLOW_MEM[i] & 0xFFF); + } + + Serial.printf("\n"); + delay(1000); +} diff --git a/examples/ULP_Count/ULP_Count.ino b/examples/ULP_Count/ULP_Count.ino new file mode 100644 index 0000000..e102a79 --- /dev/null +++ b/examples/ULP_Count/ULP_Count.ino @@ -0,0 +1,72 @@ +/* + * @Description: ULP计数程序 + * 适用arduino_esp32版本:3.0.0-rc3 + * @version: V1.0.0 + * @Author: None + * @Date: 2024-04-29 19:02:41 + * @LastEditors: LILYGO_L + * @LastEditTime: 2024-05-25 14:55:39 + * @License: GPL 3.0 + */ +#include +#include "driver/rtc_io.h" +#include "sdkconfig.h" +#include "soc/rtc_cntl_reg.h" +#include "esp32s3/ulp.h" + +void ULP_Initialization() +{ + memset(RTC_SLOW_MEM, 0, 100); + + const ulp_insn_t program[] = { + I_MOVI(R0, 0), + I_MOVI(R0, 0), // R0 =0 + I_MOVI(R1, 0), // R1 = 0; + + M_LABEL(1), // do { + + // delay(1000); + I_MOVI(R0, 200), // R0 = n * 1000 / 5, where n is the number of seconds to delay, 200 = 1 s + M_LABEL(2), // do { + // delay (5); + // since ULP runs at 8 MHz + // 40000 cycles correspond to 5 ms (max possible delay is 65535 cycles or 8.19 ms) + I_DELAY(40000), + I_SUBI(R0, R0, 1), // R0 --; + M_BG(2, 1), // } while (R0 >= 1); ... jump to label 2 if R0 > 0 + + I_ADDI(R1, R1, 1), // R1 ++ + I_ST(R1, 0, 1), // RTC_SLOW_MEM [2] = R1; + + I_MOVR(R0, R1), // M_BL函数根据R0来判断 所以这里将R0=R1 + M_BL(1, 100), // ... jump to label 1 + + I_END(), // 结束ULP进程 + }; + + size_t size = sizeof(program) / sizeof(ulp_insn_t); + ulp_process_macros_and_load(32, program, &size); + ulp_run(32); + + // in which it also prints the debug output mentioned earlier + // Serial.printf("ULP Mem in main app: %d\n", CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM); +} + +void setup() +{ + Serial.begin(115200); + Serial.println("Ciallo"); + + ULP_Initialization(); +} + +void loop() +{ + for (int i = 0; i < 10; i++) + { + Serial.printf("RTC_SLOW_MEM[%d]: %d\n", i, RTC_SLOW_MEM[i] & 0xFFFF); + } + + Serial.printf("\n"); + delay(1000); +} \ No newline at end of file