test_ulp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <freertos/FreeRTOS.h>
  17. #include <freertos/task.h>
  18. #include <freertos/semphr.h>
  19. #include <unity.h>
  20. #include "esp_attr.h"
  21. #include "esp_err.h"
  22. #include "esp_log.h"
  23. #include "esp_sleep.h"
  24. #include "esp32/ulp.h"
  25. #include "soc/soc.h"
  26. #include "soc/rtc.h"
  27. #include "soc/rtc_cntl_reg.h"
  28. #include "soc/sens_reg.h"
  29. #include "driver/rtc_io.h"
  30. #include "sdkconfig.h"
  31. #include "esp_rom_sys.h"
  32. static void hexdump(const uint32_t* src, size_t count) {
  33. for (size_t i = 0; i < count; ++i) {
  34. printf("%08x ", *src);
  35. ++src;
  36. if ((i + 1) % 4 == 0) {
  37. printf("\n");
  38. }
  39. }
  40. }
  41. TEST_CASE("ulp add test", "[ulp]")
  42. {
  43. memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
  44. const ulp_insn_t program[] = {
  45. I_MOVI(R3, 16),
  46. I_LD(R0, R3, 0),
  47. I_LD(R1, R3, 1),
  48. I_ADDR(R2, R0, R1),
  49. I_ST(R2, R3, 2),
  50. I_HALT()
  51. };
  52. RTC_SLOW_MEM[16] = 10;
  53. RTC_SLOW_MEM[17] = 11;
  54. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  55. TEST_ASSERT_EQUAL(ESP_OK, ulp_process_macros_and_load(0, program, &size));
  56. TEST_ASSERT_EQUAL(ESP_OK, ulp_run(0));
  57. esp_rom_delay_us(1000);
  58. hexdump(RTC_SLOW_MEM, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM / 4);
  59. TEST_ASSERT_EQUAL(10 + 11, RTC_SLOW_MEM[18] & 0xffff);
  60. }
  61. TEST_CASE("ulp branch test", "[ulp]")
  62. {
  63. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  64. memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
  65. const ulp_insn_t program[] = {
  66. I_MOVI(R0, 34), // r0 = dst
  67. M_LABEL(1),
  68. I_MOVI(R1, 32),
  69. I_LD(R1, R1, 0), // r1 = mem[33]
  70. I_MOVI(R2, 33),
  71. I_LD(R2, R2, 0), // r2 = mem[34]
  72. I_SUBR(R3, R1, R2), // r3 = r1 - r2
  73. I_ST(R3, R0, 0), // dst[0] = r3
  74. I_ADDI(R0, R0, 1),
  75. M_BL(1, 64),
  76. I_HALT(),
  77. };
  78. RTC_SLOW_MEM[32] = 42;
  79. RTC_SLOW_MEM[33] = 18;
  80. hexdump(RTC_SLOW_MEM, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM / 4);
  81. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  82. ulp_process_macros_and_load(0, program, &size);
  83. ulp_run(0);
  84. printf("\n\n");
  85. hexdump(RTC_SLOW_MEM, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM / 4);
  86. for (int i = 34; i < 64; ++i) {
  87. TEST_ASSERT_EQUAL(42 - 18, RTC_SLOW_MEM[i] & 0xffff);
  88. }
  89. TEST_ASSERT_EQUAL(0, RTC_SLOW_MEM[64]);
  90. }
  91. TEST_CASE("ulp wakeup test", "[ulp][ignore]")
  92. {
  93. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  94. memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
  95. const ulp_insn_t program[] = {
  96. I_MOVI(R1, 1024),
  97. M_LABEL(1),
  98. I_DELAY(32000),
  99. I_SUBI(R1, R1, 1),
  100. M_BXZ(3),
  101. I_RSHI(R3, R1, 5), // R3 = R1 / 32
  102. I_ST(R1, R3, 16),
  103. M_BX(1),
  104. M_LABEL(3),
  105. I_MOVI(R2, 42),
  106. I_MOVI(R3, 15),
  107. I_ST(R2, R3, 0),
  108. I_WAKE(),
  109. I_END(),
  110. I_HALT()
  111. };
  112. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  113. ulp_process_macros_and_load(0, program, &size);
  114. ulp_run(0);
  115. esp_sleep_enable_ulp_wakeup();
  116. esp_deep_sleep_start();
  117. }
  118. TEST_CASE("ulp can write and read peripheral registers", "[ulp]")
  119. {
  120. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  121. CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  122. memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
  123. REG_WRITE(RTC_CNTL_STORE1_REG, 0x89abcdef);
  124. const ulp_insn_t program[] = {
  125. I_MOVI(R1, 64),
  126. I_RD_REG(RTC_CNTL_STORE1_REG, 0, 15),
  127. I_ST(R0, R1, 0),
  128. I_RD_REG(RTC_CNTL_STORE1_REG, 4, 11),
  129. I_ST(R0, R1, 1),
  130. I_RD_REG(RTC_CNTL_STORE1_REG, 16, 31),
  131. I_ST(R0, R1, 2),
  132. I_RD_REG(RTC_CNTL_STORE1_REG, 20, 27),
  133. I_ST(R0, R1, 3),
  134. I_WR_REG(RTC_CNTL_STORE0_REG, 0, 7, 0x89),
  135. I_WR_REG(RTC_CNTL_STORE0_REG, 8, 15, 0xab),
  136. I_WR_REG(RTC_CNTL_STORE0_REG, 16, 23, 0xcd),
  137. I_WR_REG(RTC_CNTL_STORE0_REG, 24, 31, 0xef),
  138. I_LD(R0, R1, 4),
  139. I_ADDI(R0, R0, 1),
  140. I_ST(R0, R1, 4),
  141. I_END(),
  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. TEST_ESP_OK(ulp_run(0));
  147. vTaskDelay(100/portTICK_PERIOD_MS);
  148. TEST_ASSERT_EQUAL_HEX32(0xefcdab89, REG_READ(RTC_CNTL_STORE0_REG));
  149. TEST_ASSERT_EQUAL_HEX16(0xcdef, RTC_SLOW_MEM[64] & 0xffff);
  150. TEST_ASSERT_EQUAL_HEX16(0xde, RTC_SLOW_MEM[65] & 0xffff);
  151. TEST_ASSERT_EQUAL_HEX16(0x89ab, RTC_SLOW_MEM[66] & 0xffff);
  152. TEST_ASSERT_EQUAL_HEX16(0x9a, RTC_SLOW_MEM[67] & 0xffff);
  153. TEST_ASSERT_EQUAL_HEX32(1 | (15 << 21) | (1 << 16), RTC_SLOW_MEM[68]);
  154. }
  155. TEST_CASE("ULP I_WR_REG instruction test", "[ulp]")
  156. {
  157. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  158. memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
  159. typedef struct {
  160. int low;
  161. int width;
  162. } wr_reg_test_item_t;
  163. const wr_reg_test_item_t test_items[] = {
  164. {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}, {0, 8},
  165. {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8},
  166. {15, 1}, {15, 2}, {15, 3}, {15, 4}, {15, 5}, {15, 6}, {15, 7}, {15, 8},
  167. {16, 1}, {16, 2}, {16, 3}, {16, 4}, {16, 5}, {16, 6}, {16, 7}, {16, 8},
  168. {18, 1}, {18, 2}, {18, 3}, {18, 4}, {18, 5}, {18, 6}, {18, 7}, {18, 8},
  169. {24, 1}, {24, 2}, {24, 3}, {24, 4}, {24, 5}, {24, 6}, {24, 7}, {24, 8},
  170. };
  171. const size_t test_items_count =
  172. sizeof(test_items)/sizeof(test_items[0]);
  173. for (size_t i = 0; i < test_items_count; ++i) {
  174. const uint32_t mask = (uint32_t) (((1ULL << test_items[i].width) - 1) << test_items[i].low);
  175. const uint32_t not_mask = ~mask;
  176. printf("#%2d: low: %2d width: %2d mask: %08x expected: %08x ", i,
  177. test_items[i].low, test_items[i].width,
  178. mask, not_mask);
  179. REG_WRITE(RTC_CNTL_STORE0_REG, 0xffffffff);
  180. REG_WRITE(RTC_CNTL_STORE1_REG, 0x00000000);
  181. const ulp_insn_t program[] = {
  182. I_WR_REG(RTC_CNTL_STORE0_REG,
  183. test_items[i].low,
  184. test_items[i].low + test_items[i].width - 1,
  185. 0),
  186. I_WR_REG(RTC_CNTL_STORE1_REG,
  187. test_items[i].low,
  188. test_items[i].low + test_items[i].width - 1,
  189. 0xff & ((1 << test_items[i].width) - 1)),
  190. I_END(),
  191. I_HALT()
  192. };
  193. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  194. ulp_process_macros_and_load(0, program, &size);
  195. ulp_run(0);
  196. vTaskDelay(10/portTICK_PERIOD_MS);
  197. uint32_t clear = REG_READ(RTC_CNTL_STORE0_REG);
  198. uint32_t set = REG_READ(RTC_CNTL_STORE1_REG);
  199. printf("clear: %08x set: %08x\n", clear, set);
  200. TEST_ASSERT_EQUAL_HEX32(not_mask, clear);
  201. TEST_ASSERT_EQUAL_HEX32(mask, set);
  202. }
  203. }
  204. TEST_CASE("ulp controls RTC_IO", "[ulp][ignore]")
  205. {
  206. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  207. memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
  208. const ulp_insn_t program[] = {
  209. I_MOVI(R0, 0), // R0 is LED state
  210. I_MOVI(R2, 16), // loop R2 from 16 down to 0
  211. M_LABEL(4),
  212. I_SUBI(R2, R2, 1),
  213. M_BXZ(6),
  214. I_ADDI(R0, R0, 1), // R0 = (R0 + 1) % 2
  215. I_ANDI(R0, R0, 0x1),
  216. M_BL(0, 1), // if R0 < 1 goto 0
  217. M_LABEL(1),
  218. I_WR_REG(RTC_GPIO_OUT_REG, 26, 27, 1), // RTC_GPIO12 = 1
  219. M_BX(2), // goto 2
  220. M_LABEL(0), // 0:
  221. I_WR_REG(RTC_GPIO_OUT_REG, 26, 27, 0), // RTC_GPIO12 = 0
  222. M_LABEL(2), // 2:
  223. I_MOVI(R1, 100), // loop R1 from 100 down to 0
  224. M_LABEL(3),
  225. I_SUBI(R1, R1, 1),
  226. M_BXZ(5),
  227. I_DELAY(32000), // delay for a while
  228. M_BX(3),
  229. M_LABEL(5),
  230. M_BX(4),
  231. M_LABEL(6),
  232. I_WAKE(), // wake up the SoC
  233. I_END(), // stop ULP program timer
  234. I_HALT()
  235. };
  236. const gpio_num_t led_gpios[] = {
  237. GPIO_NUM_2,
  238. GPIO_NUM_0,
  239. GPIO_NUM_4
  240. };
  241. for (size_t i = 0; i < sizeof(led_gpios)/sizeof(led_gpios[0]); ++i) {
  242. rtc_gpio_init(led_gpios[i]);
  243. rtc_gpio_set_direction(led_gpios[i], RTC_GPIO_MODE_OUTPUT_ONLY);
  244. rtc_gpio_set_level(led_gpios[i], 0);
  245. }
  246. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  247. ulp_process_macros_and_load(0, program, &size);
  248. ulp_run(0);
  249. esp_sleep_enable_ulp_wakeup();
  250. esp_deep_sleep_start();
  251. }
  252. TEST_CASE("ulp power consumption in deep sleep", "[ulp][ignore]")
  253. {
  254. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 4 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  255. ulp_insn_t insn = I_HALT();
  256. memcpy(&RTC_SLOW_MEM[0], &insn, sizeof(insn));
  257. REG_WRITE(SENS_ULP_CP_SLEEP_CYC0_REG, 0x8000);
  258. ulp_run(0);
  259. esp_sleep_enable_ulp_wakeup();
  260. esp_sleep_enable_timer_wakeup(10 * 1000000);
  261. esp_deep_sleep_start();
  262. }
  263. TEST_CASE("ulp timer setting", "[ulp]")
  264. {
  265. /*
  266. * Run a simple ULP program which increments the counter, for one second.
  267. * Program calls I_HALT each time and gets restarted by the timer.
  268. * Compare the expected number of times the program runs with the actual.
  269. */
  270. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 32 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  271. memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
  272. const int offset = 6;
  273. const ulp_insn_t program[] = {
  274. I_MOVI(R1, offset), // r1 <- offset
  275. I_LD(R2, R1, 0), // load counter
  276. I_ADDI(R2, R2, 1), // counter += 1
  277. I_ST(R2, R1, 0), // save counter
  278. I_HALT(),
  279. };
  280. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  281. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  282. assert(offset >= size && "data offset needs to be greater or equal to program size");
  283. TEST_ESP_OK(ulp_run(0));
  284. // disable the ULP program timer — we will enable it later
  285. CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  286. const uint32_t cycles_to_test[] = {0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
  287. const size_t tests_count = sizeof(cycles_to_test) / sizeof(cycles_to_test[0]);
  288. for (size_t i = 0; i < tests_count; ++i) {
  289. // zero out the counter
  290. RTC_SLOW_MEM[offset] = 0;
  291. // set the number of slow clock cycles
  292. REG_WRITE(SENS_ULP_CP_SLEEP_CYC0_REG, cycles_to_test[i]);
  293. // enable the timer and wait for a second
  294. SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  295. vTaskDelay(1000 / portTICK_PERIOD_MS);
  296. // get the counter value and stop the timer
  297. uint32_t counter = RTC_SLOW_MEM[offset] & 0xffff;
  298. CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  299. // compare the actual and expected numbers of iterations of ULP program
  300. float expected_period = (cycles_to_test[i] + 16) / (float) rtc_clk_slow_freq_get_hz() + 5 / 8e6f;
  301. float error = 1.0f - counter * expected_period;
  302. printf("%u\t%u\t%.01f\t%.04f\n", cycles_to_test[i], counter, 1.0f / expected_period, error);
  303. // Should be within 15%
  304. TEST_ASSERT_INT_WITHIN(15, 0, (int) error * 100);
  305. }
  306. }
  307. TEST_CASE("ulp can use TSENS in deep sleep", "[ulp][ignore]")
  308. {
  309. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  310. hexdump(RTC_SLOW_MEM, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM / 4);
  311. printf("\n\n");
  312. memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
  313. // Allow TSENS to be controlled by the ULP
  314. SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 10, SENS_TSENS_CLK_DIV_S);
  315. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S);
  316. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
  317. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
  318. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE);
  319. // data start offset
  320. size_t offset = 20;
  321. // number of samples to collect
  322. RTC_SLOW_MEM[offset] = (CONFIG_ESP32_ULP_COPROC_RESERVE_MEM) / 4 - offset - 8;
  323. // sample counter
  324. RTC_SLOW_MEM[offset + 1] = 0;
  325. const ulp_insn_t program[] = {
  326. I_MOVI(R1, offset), // r1 <- offset
  327. I_LD(R2, R1, 1), // r2 <- counter
  328. I_LD(R3, R1, 0), // r3 <- length
  329. I_SUBI(R3, R3, 1), // end = length - 1
  330. I_SUBR(R3, R3, R2), // r3 = length - counter
  331. M_BXF(1), // if overflow goto 1:
  332. I_WR_REG(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_S, SENS_FORCE_XPD_SAR_S + 1, 3),
  333. I_TSENS(R0, 16383), // r0 <- tsens
  334. I_WR_REG(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_S, SENS_FORCE_XPD_SAR_S + 1, 0),
  335. I_ST(R0, R2, offset + 4),
  336. I_ADDI(R2, R2, 1), // counter += 1
  337. I_ST(R2, R1, 1), // save counter
  338. I_HALT(), // enter sleep
  339. M_LABEL(1), // done with measurements
  340. I_END(), // stop ULP timer
  341. I_WAKE(), // initiate wakeup
  342. I_HALT()
  343. };
  344. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  345. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  346. assert(offset >= size);
  347. TEST_ESP_OK(ulp_run(0));
  348. esp_sleep_enable_timer_wakeup(4000000);
  349. esp_sleep_enable_ulp_wakeup();
  350. esp_deep_sleep_start();
  351. }
  352. TEST_CASE("can use ADC in deep sleep", "[ulp][ignore]")
  353. {
  354. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  355. hexdump(RTC_SLOW_MEM, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM / 4);
  356. printf("\n\n");
  357. memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
  358. SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_SAR1_BIT_WIDTH, 3, SENS_SAR1_BIT_WIDTH_S);
  359. SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_SAR2_BIT_WIDTH, 3, SENS_SAR2_BIT_WIDTH_S);
  360. SET_PERI_REG_BITS(SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_BIT, 0x3, SENS_SAR1_SAMPLE_BIT_S);
  361. SET_PERI_REG_BITS(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_BIT, 0x3, SENS_SAR2_SAMPLE_BIT_S);
  362. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_FORCE);
  363. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_FORCE);
  364. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);
  365. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_AMP, 2, SENS_FORCE_XPD_AMP_S);
  366. // SAR1 invert result
  367. SET_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR1_DATA_INV);
  368. SET_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR2_DATA_INV);
  369. // const int adc = 1;
  370. // const int channel = 1;
  371. // const int atten = 3;
  372. // const int gpio_num = 0;
  373. const int adc = 0;
  374. const int channel = 0;
  375. const int atten = 0;
  376. const int gpio_num = 36;
  377. rtc_gpio_init(gpio_num);
  378. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD_FORCE_M);
  379. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD_FORCE_M);
  380. SET_PERI_REG_BITS(SENS_SAR_ATTEN1_REG, 3, atten, 2 * channel); //set SAR1 attenuation
  381. SET_PERI_REG_BITS(SENS_SAR_ATTEN2_REG, 3, atten, 2 * channel); //set SAR2 attenuation
  382. // data start offset
  383. size_t offset = 20;
  384. // number of samples to collect
  385. RTC_SLOW_MEM[offset] = (CONFIG_ESP32_ULP_COPROC_RESERVE_MEM) / 4 - offset - 8;
  386. // sample counter
  387. RTC_SLOW_MEM[offset + 1] = 0;
  388. const ulp_insn_t program[] = {
  389. I_MOVI(R1, offset), // r1 <- offset
  390. I_LD(R2, R1, 1), // r2 <- counter
  391. I_LD(R3, R1, 0), // r3 <- length
  392. I_SUBI(R3, R3, 1), // end = length - 1
  393. I_SUBR(R3, R3, R2), // r3 = length - counter
  394. M_BXF(1), // if overflow goto 1:
  395. I_ADC(R0, adc, channel), // r0 <- ADC
  396. I_ST(R0, R2, offset + 4),
  397. I_ADDI(R2, R2, 1), // counter += 1
  398. I_ST(R2, R1, 1), // save counter
  399. I_HALT(),
  400. M_LABEL(1), // done with measurements
  401. I_END(), // stop ULP program timer
  402. I_HALT()
  403. };
  404. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  405. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  406. assert(offset >= size);
  407. TEST_ESP_OK(ulp_run(0));
  408. esp_sleep_enable_timer_wakeup(4000000);
  409. esp_deep_sleep_start();
  410. }