test_ulp_manual.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <freertos/FreeRTOS.h>
  9. #include <freertos/task.h>
  10. #include <freertos/semphr.h>
  11. #include "unity.h"
  12. #include "esp_attr.h"
  13. #include "esp_err.h"
  14. #include "esp_log.h"
  15. #include "esp_sleep.h"
  16. #include "ulp.h"
  17. #include "soc/soc.h"
  18. #include "soc/rtc.h"
  19. #include "soc/rtc_cntl_reg.h"
  20. #include "soc/sens_reg.h"
  21. #include "soc/rtc_io_reg.h"
  22. #include "hal/misc.h"
  23. #include "driver/rtc_io.h"
  24. #include "sdkconfig.h"
  25. #include "esp_rom_sys.h"
  26. #include "ulp_test_app.h"
  27. /* Test cases that require manual interaction, not run in CI */
  28. void ulp_fsm_controls_rtc_io(void)
  29. {
  30. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  31. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  32. hal_memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  33. /* ULP co-processor program to toggle LED */
  34. const ulp_insn_t program[] = {
  35. I_MOVI(R0, 0), // r0 is LED state
  36. I_MOVI(R2, 16), // loop r2 from 16 down to 0
  37. M_LABEL(4), // define label 4
  38. I_SUBI(R2, R2, 1), // r2 = r2 - 1
  39. M_BXZ(6), // branch to label 6 if r2 = 0
  40. I_ADDI(R0, R0, 1), // r0 = (r0 + 1) % 2
  41. I_ANDI(R0, R0, 0x1),
  42. M_BL(0, 1), // if r0 < 1 goto 0
  43. M_LABEL(1), // define label 1
  44. I_WR_REG(RTC_GPIO_OUT_REG, 26, 27, 1), // RTC_GPIO12 = 1
  45. M_BX(2), // goto 2
  46. M_LABEL(0), // define label 0
  47. I_WR_REG(RTC_GPIO_OUT_REG, 26, 27, 0), // RTC_GPIO12 = 0
  48. M_LABEL(2), // define label 2
  49. I_MOVI(R1, 100), // loop R1 from 100 down to 0
  50. M_LABEL(3), // define label 3
  51. I_SUBI(R1, R1, 1), // r1 = r1 - 1
  52. M_BXZ(5), // branch to label 5 if r1 = 0
  53. I_DELAY(32000), // delay for a while
  54. M_BX(3), // goto 3
  55. M_LABEL(5), // define label 5
  56. M_BX(4), // loop back to label 4
  57. M_LABEL(6), // define label 6
  58. I_WAKE(), // wake up the SoC
  59. I_END(), // stop ULP program timer
  60. I_HALT()
  61. };
  62. /* Configure LED GPIOs */
  63. const gpio_num_t led_gpios[] = {
  64. GPIO_NUM_2,
  65. GPIO_NUM_0,
  66. GPIO_NUM_4
  67. };
  68. for (size_t i = 0; i < sizeof(led_gpios)/sizeof(led_gpios[0]); ++i) {
  69. rtc_gpio_init(led_gpios[i]);
  70. rtc_gpio_set_direction(led_gpios[i], RTC_GPIO_MODE_OUTPUT_ONLY);
  71. rtc_gpio_set_level(led_gpios[i], 0);
  72. }
  73. /* Calculate the size of the ULP co-processor binary, load it and run the ULP coprocessor */
  74. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  75. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  76. TEST_ESP_OK(ulp_run(0));
  77. /* Setup wakeup triggers */
  78. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  79. /* Enter Deep Sleep */
  80. esp_deep_sleep_start();
  81. UNITY_TEST_FAIL(__LINE__, "Should not get here!");
  82. }
  83. void ulp_fsm_power_consumption(void)
  84. {
  85. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 4 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  86. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  87. hal_memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  88. /* Put the ULP coprocessor in halt state */
  89. ulp_insn_t insn = I_HALT();
  90. hal_memcpy(RTC_SLOW_MEM, &insn, sizeof(insn));
  91. /* Set ULP timer */
  92. ulp_set_wakeup_period(0, 0x8000);
  93. /* Run the ULP coprocessor */
  94. TEST_ESP_OK(ulp_run(0));
  95. /* Setup wakeup triggers */
  96. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  97. TEST_ASSERT(esp_sleep_enable_timer_wakeup(10 * 1000000) == ESP_OK);
  98. /* Enter Deep Sleep */
  99. esp_deep_sleep_start();
  100. UNITY_TEST_FAIL(__LINE__, "Should not get here!");
  101. }
  102. #if !DISABLED_FOR_TARGETS(ESP32)
  103. void ulp_fsm_temp_sens(void)
  104. {
  105. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  106. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  107. hal_memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  108. // Allow TSENS to be controlled by the ULP
  109. SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 10, SENS_TSENS_CLK_DIV_S);
  110. #if CONFIG_IDF_TARGET_ESP32S2
  111. SET_PERI_REG_BITS(SENS_SAR_POWER_XPD_SAR_REG, SENS_FORCE_XPD_SAR, SENS_FORCE_XPD_SAR_FSM, SENS_FORCE_XPD_SAR_S);
  112. SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL2_REG, SENS_TSENS_CLKGATE_EN);
  113. #elif CONFIG_IDF_TARGET_ESP32S3
  114. SET_PERI_REG_BITS(SENS_SAR_POWER_XPD_SAR_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);
  115. SET_PERI_REG_MASK(SENS_SAR_PERI_CLK_GATE_CONF_REG, SENS_TSENS_CLK_EN);
  116. #endif
  117. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
  118. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
  119. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE);
  120. // data start offset
  121. size_t offset = 20;
  122. // number of samples to collect
  123. RTC_SLOW_MEM[offset] = (CONFIG_ULP_COPROC_RESERVE_MEM) / 4 - offset - 8;
  124. // sample counter
  125. RTC_SLOW_MEM[offset + 1] = 0;
  126. /* ULP co-processor program to record temperature sensor readings */
  127. const ulp_insn_t program[] = {
  128. I_MOVI(R1, offset), // r1 <- offset
  129. I_LD(R2, R1, 1), // r2 <- counter
  130. I_LD(R3, R1, 0), // r3 <- length
  131. I_SUBI(R3, R3, 1), // end = length - 1
  132. I_SUBR(R3, R3, R2), // r3 = length - counter
  133. M_BXF(1), // if overflow goto 1:
  134. I_TSENS(R0, 16383), // r0 <- tsens
  135. I_ST(R0, R2, offset + 4), // mem[r2 + offset +4] <- r0
  136. I_ADDI(R2, R2, 1), // counter += 1
  137. I_ST(R2, R1, 1), // save counter
  138. I_HALT(), // enter sleep
  139. M_LABEL(1), // done with measurements
  140. I_END(), // stop ULP timer
  141. I_WAKE(), // initiate wakeup
  142. I_HALT()
  143. };
  144. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  145. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  146. assert(offset >= size);
  147. /* Run the ULP coprocessor */
  148. TEST_ESP_OK(ulp_run(0));
  149. /* Setup wakeup triggers */
  150. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  151. TEST_ASSERT(esp_sleep_enable_timer_wakeup(10 * 1000000) == ESP_OK);
  152. /* Enter Deep Sleep */
  153. esp_deep_sleep_start();
  154. UNITY_TEST_FAIL(__LINE__, "Should not get here!");
  155. }
  156. #endif //#if !DISABLED_FOR_TARGETS(ESP32)
  157. void ulp_fsm_adc(void)
  158. {
  159. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  160. const int adc = 0;
  161. const int channel = 0;
  162. const int atten = 0;
  163. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  164. hal_memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  165. #if defined(CONFIG_IDF_TARGET_ESP32)
  166. // Configure SAR ADCn resolution
  167. SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_SAR1_BIT_WIDTH, 3, SENS_SAR1_BIT_WIDTH_S);
  168. SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_SAR2_BIT_WIDTH, 3, SENS_SAR2_BIT_WIDTH_S);
  169. SET_PERI_REG_BITS(SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_BIT, 0x3, SENS_SAR1_SAMPLE_BIT_S);
  170. SET_PERI_REG_BITS(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_BIT, 0x3, SENS_SAR2_SAMPLE_BIT_S);
  171. // SAR ADCn is started by ULP FSM
  172. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_FORCE);
  173. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_FORCE);
  174. // Use ULP FSM to power up SAR ADCn
  175. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);
  176. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_AMP, 2, SENS_FORCE_XPD_AMP_S);
  177. // SAR ADCn invert result
  178. SET_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR1_DATA_INV);
  179. SET_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR2_DATA_INV);
  180. // Set SAR ADCn pad enable bitmap to be controlled by ULP FSM
  181. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD_FORCE_M);
  182. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD_FORCE_M);
  183. #elif defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
  184. // SAR ADCn is started by ULP FSM
  185. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_START_FORCE);
  186. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS1_CTRL2_REG, SENS_MEAS1_START_FORCE);
  187. // Use ULP FSM to power up/down SAR ADCn
  188. SET_PERI_REG_BITS(SENS_SAR_POWER_XPD_SAR_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);
  189. SET_PERI_REG_BITS(SENS_SAR_MEAS1_CTRL1_REG, SENS_FORCE_XPD_AMP, 2, SENS_FORCE_XPD_AMP_S);
  190. // SAR1 invert result
  191. SET_PERI_REG_MASK(SENS_SAR_READER1_CTRL_REG, SENS_SAR1_DATA_INV);
  192. SET_PERI_REG_MASK(SENS_SAR_READER2_CTRL_REG, SENS_SAR2_DATA_INV);
  193. // Set SAR ADCn pad enable bitmap to be controlled by ULP FSM
  194. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS1_CTRL2_REG, SENS_SAR1_EN_PAD_FORCE_M);
  195. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_SAR2_EN_PAD_FORCE_M);
  196. // Enable SAR ADCn clock gate on esp32s3
  197. #if CONFIG_IDF_TARGET_ESP32S3
  198. SET_PERI_REG_MASK(SENS_SAR_PERI_CLK_GATE_CONF_REG, SENS_SARADC_CLK_EN);
  199. #endif
  200. #endif
  201. SET_PERI_REG_BITS(SENS_SAR_ATTEN1_REG, 3, atten, 2 * channel); //set SAR1 attenuation
  202. SET_PERI_REG_BITS(SENS_SAR_ATTEN2_REG, 3, atten, 2 * channel); //set SAR2 attenuation
  203. // data start offset
  204. size_t offset = 20;
  205. // number of samples to collect
  206. RTC_SLOW_MEM[offset] = (CONFIG_ULP_COPROC_RESERVE_MEM) / 4 - offset - 8;
  207. // sample counter
  208. RTC_SLOW_MEM[offset + 1] = 0;
  209. const ulp_insn_t program[] = {
  210. I_MOVI(R1, offset), // r1 <- offset
  211. I_LD(R2, R1, 1), // r2 <- counter
  212. I_LD(R3, R1, 0), // r3 <- length
  213. I_SUBI(R3, R3, 1), // end = length - 1
  214. I_SUBR(R3, R3, R2), // r3 = length - counter
  215. M_BXF(1), // if overflow goto 1:
  216. I_ADC(R0, adc, channel), // r0 <- ADC
  217. I_ST(R0, R2, offset + 4), // mem[r2 + offset +4] = r0
  218. I_ADDI(R2, R2, 1), // counter += 1
  219. I_ST(R2, R1, 1), // save counter
  220. I_HALT(), // enter sleep
  221. M_LABEL(1), // done with measurements
  222. I_END(), // stop ULP program timer
  223. I_HALT()
  224. };
  225. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  226. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  227. assert(offset >= size);
  228. /* Run the ULP coprocessor */
  229. TEST_ESP_OK(ulp_run(0));
  230. /* Setup wakeup triggers */
  231. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  232. TEST_ASSERT(esp_sleep_enable_timer_wakeup(10 * 1000000) == ESP_OK);
  233. /* Enter Deep Sleep */
  234. esp_deep_sleep_start();
  235. UNITY_TEST_FAIL(__LINE__, "Should not get here!");
  236. }