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