test_ulp.c 18 KB

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