test_ulp.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2022 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 "driver/rtc_io.h"
  23. #include "sdkconfig.h"
  24. #include "esp_rom_sys.h"
  25. #include "ulp_test_app.h"
  26. extern const uint8_t ulp_test_app_bin_start[] asm("_binary_ulp_test_app_bin_start");
  27. extern const uint8_t ulp_test_app_bin_end[] asm("_binary_ulp_test_app_bin_end");
  28. #define HEX_DUMP_DEBUG 0
  29. static void hexdump(const uint32_t* src, size_t count) {
  30. #if HEX_DUMP_DEBUG
  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. #else
  39. (void)src;
  40. (void)count;
  41. #endif
  42. }
  43. TEST_CASE("ULP FSM addition test", "[ulp]")
  44. {
  45. #pragma GCC diagnostic push
  46. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  47. #pragma GCC diagnostic ignored "-Warray-bounds"
  48. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  49. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  50. #pragma GCC diagnostic pop
  51. /* ULP co-processor program to add data in 2 memory locations using ULP macros */
  52. const ulp_insn_t program[] = {
  53. I_MOVI(R3, 16), // r3 = 16
  54. I_LD(R0, R3, 0), // r0 = mem[r3 + 0]
  55. I_LD(R1, R3, 1), // r1 = mem[r3 + 1]
  56. I_ADDR(R2, R0, R1), // r2 = r0 + r1
  57. I_ST(R2, R3, 2), // mem[r3 + 2] = r2
  58. I_HALT() // halt
  59. };
  60. /* Load the memory regions used by the ULP co-processor */
  61. RTC_SLOW_MEM[16] = 10;
  62. RTC_SLOW_MEM[17] = 11;
  63. /* Calculate the size of the ULP co-processor binary, load it and run the ULP coprocessor */
  64. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  65. TEST_ASSERT_EQUAL(ESP_OK, ulp_process_macros_and_load(0, program, &size));
  66. TEST_ASSERT_EQUAL(ESP_OK, ulp_run(0));
  67. /* Wait for the ULP co-processor to finish up */
  68. esp_rom_delay_us(1000);
  69. hexdump(RTC_SLOW_MEM, 20);
  70. /* Verify the test results */
  71. TEST_ASSERT_EQUAL(10 + 11, RTC_SLOW_MEM[18] & 0xffff);
  72. }
  73. TEST_CASE("ULP FSM subtraction and branch test", "[ulp]")
  74. {
  75. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  76. #pragma GCC diagnostic push
  77. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  78. #pragma GCC diagnostic ignored "-Warray-bounds"
  79. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  80. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  81. #pragma GCC diagnostic pop
  82. /* ULP co-processor program to perform subtractions and branch to a label */
  83. const ulp_insn_t program[] = {
  84. I_MOVI(R0, 34), // r0 = 34
  85. M_LABEL(1), // define a label with label number as 1
  86. I_MOVI(R1, 32), // r1 = 32
  87. I_LD(R1, R1, 0), // r1 = mem[32 + 0]
  88. I_MOVI(R2, 33), // r2 = 33
  89. I_LD(R2, R2, 0), // r2 = mem[33 + 0]
  90. I_SUBR(R3, R1, R2), // r3 = r1 - r2
  91. I_ST(R3, R0, 0), // mem[r0 + 0] = r3
  92. I_ADDI(R0, R0, 1), // r0 = r0 + 1
  93. M_BL(1, 64), // branch to label 1 if r0 < 64
  94. I_HALT(), // halt
  95. };
  96. /* Load the memory regions used by the ULP co-processor */
  97. RTC_SLOW_MEM[32] = 42;
  98. RTC_SLOW_MEM[33] = 18;
  99. /* Calculate the size of the ULP co-processor binary, load it and run the ULP coprocessor */
  100. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  101. TEST_ASSERT_EQUAL(ESP_OK, ulp_process_macros_and_load(0, program, &size));
  102. TEST_ASSERT_EQUAL(ESP_OK, ulp_run(0));
  103. printf("\n\n");
  104. /* Wait for the ULP co-processor to finish up */
  105. esp_rom_delay_us(1000);
  106. hexdump(RTC_SLOW_MEM, 50);
  107. /* Verify the test results */
  108. for (int i = 34; i < 64; ++i) {
  109. TEST_ASSERT_EQUAL(42 - 18, RTC_SLOW_MEM[i] & 0xffff);
  110. }
  111. TEST_ASSERT_EQUAL(0, RTC_SLOW_MEM[64]);
  112. }
  113. TEST_CASE("ULP FSM JUMPS instruction test", "[ulp]")
  114. {
  115. /*
  116. * Load the ULP binary.
  117. *
  118. * This ULP program is written in assembly. Please refer associated .S file.
  119. */
  120. esp_err_t err = ulp_load_binary(0, ulp_test_app_bin_start,
  121. (ulp_test_app_bin_end - ulp_test_app_bin_start) / sizeof(uint32_t));
  122. TEST_ESP_OK(err);
  123. /* Clear ULP FSM raw interrupt */
  124. REG_CLR_BIT(RTC_CNTL_INT_RAW_REG, RTC_CNTL_ULP_CP_INT_RAW);
  125. /* Run the ULP coprocessor */
  126. TEST_ESP_OK(ulp_run(&ulp_test_jumps - RTC_SLOW_MEM));
  127. /* Wait for the ULP co-processor to finish up */
  128. esp_rom_delay_us(1000);
  129. /* Verify that ULP FSM issued an interrupt to wake up the main CPU */
  130. TEST_ASSERT_NOT_EQUAL(0, REG_GET_BIT(RTC_CNTL_INT_RAW_REG, RTC_CNTL_ULP_CP_INT_RAW));
  131. /* Verify the test results */
  132. TEST_ASSERT_EQUAL(0, ulp_jumps_fail & UINT16_MAX);
  133. TEST_ASSERT_EQUAL(1, ulp_jumps_pass & UINT16_MAX);
  134. }
  135. TEST_CASE("ULP FSM light-sleep wakeup test", "[ulp]")
  136. {
  137. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  138. #pragma GCC diagnostic push
  139. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  140. #pragma GCC diagnostic ignored "-Warray-bounds"
  141. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  142. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  143. #pragma GCC diagnostic pop
  144. /* ULP co-processor program to perform some activities and wakeup the main CPU from deep-sleep */
  145. const ulp_insn_t program[] = {
  146. I_MOVI(R1, 1024), // r1 = 1024
  147. M_LABEL(1), // define label 1
  148. I_DELAY(32000), // add a delay (NOP for 32000 cycles)
  149. I_SUBI(R1, R1, 1), // r1 = r1 - 1
  150. M_BXZ(3), // branch to label 3 if ALU value is 0. (r1 = 0)
  151. I_RSHI(R3, R1, 5), // r3 = r1 / 32
  152. I_ST(R1, R3, 16), // mem[r3 + 16] = r1
  153. M_BX(1), // loop to label 1
  154. M_LABEL(3), // define label 3
  155. I_MOVI(R2, 42), // r2 = 42
  156. I_MOVI(R3, 15), // r3 = 15
  157. I_ST(R2, R3, 0), // mem[r3 + 0] = r2
  158. I_WAKE(), // wake the SoC from deep-sleep
  159. I_END(), // stop ULP timer
  160. I_HALT() // halt
  161. };
  162. /* Calculate the size of the ULP co-processor binary, load it and run the ULP coprocessor */
  163. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  164. TEST_ASSERT_EQUAL(ESP_OK, ulp_process_macros_and_load(0, program, &size));
  165. TEST_ASSERT_EQUAL(ESP_OK, ulp_run(0));
  166. /* Setup wakeup triggers */
  167. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  168. /* Enter Light Sleep */
  169. TEST_ASSERT(esp_light_sleep_start() == ESP_OK);
  170. /* Wait for wakeup from ULP FSM Coprocessor */
  171. printf("cause %d\r\n", esp_sleep_get_wakeup_cause());
  172. TEST_ASSERT(esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_ULP);
  173. }
  174. TEST_CASE("ULP FSM deep-sleep wakeup test", "[ulp][reset=SW_CPU_RESET][ignore]")
  175. {
  176. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  177. #pragma GCC diagnostic push
  178. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  179. #pragma GCC diagnostic ignored "-Warray-bounds"
  180. /* Clearout the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  181. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  182. #pragma GCC diagnostic pop
  183. /* ULP co-processor program to perform some activities and wakeup the main CPU from deep-sleep */
  184. const ulp_insn_t program[] = {
  185. I_MOVI(R1, 1024), // r1 = 1024
  186. M_LABEL(1), // define label 1
  187. I_DELAY(32000), // add a delay (NOP for 32000 cycles)
  188. I_SUBI(R1, R1, 1), // r1 = r1 - 1
  189. M_BXZ(3), // branch to label 3 if ALU value is 0. (r1 = 0)
  190. I_RSHI(R3, R1, 5), // r3 = r1 / 32
  191. I_ST(R1, R3, 16), // mem[r3 + 16] = r1
  192. M_BX(1), // loop to label 1
  193. M_LABEL(3), // define label 3
  194. I_MOVI(R2, 42), // r2 = 42
  195. I_MOVI(R3, 15), // r3 = 15
  196. I_ST(R2, R3, 0), // mem[r3 + 0] = r2
  197. I_WAKE(), // wake the SoC from deep-sleep
  198. I_END(), // stop ULP timer
  199. I_HALT() // halt
  200. };
  201. /* Calculate the size of the ULP co-processor binary, load it and run the ULP coprocessor */
  202. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  203. TEST_ASSERT_EQUAL(ESP_OK, ulp_process_macros_and_load(0, program, &size));
  204. TEST_ASSERT_EQUAL(ESP_OK, ulp_run(0));
  205. /* Setup wakeup triggers */
  206. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  207. /* Enter Deep Sleep */
  208. esp_deep_sleep_start();
  209. UNITY_TEST_FAIL(__LINE__, "Should not get here!");
  210. }
  211. TEST_CASE("ULP FSM can write and read peripheral registers", "[ulp]")
  212. {
  213. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  214. /* Clear ULP timer */
  215. CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
  216. #pragma GCC diagnostic push
  217. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  218. #pragma GCC diagnostic ignored "-Warray-bounds"
  219. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  220. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  221. #pragma GCC diagnostic pop
  222. uint32_t rtc_store0 = REG_READ(RTC_CNTL_STORE0_REG);
  223. uint32_t rtc_store1 = REG_READ(RTC_CNTL_STORE1_REG);
  224. /* ULP co-processor program to read from and write to peripheral registers */
  225. const ulp_insn_t program[] = {
  226. I_MOVI(R1, 64), // r1 = 64
  227. I_RD_REG(RTC_CNTL_STORE1_REG, 0, 15), // r0 = REG_READ(RTC_CNTL_STORE1_REG[15:0])
  228. I_ST(R0, R1, 0), // mem[r1 + 0] = r0
  229. I_RD_REG(RTC_CNTL_STORE1_REG, 4, 11), // r0 = REG_READ(RTC_CNTL_STORE1_REG[11:4])
  230. I_ST(R0, R1, 1), // mem[r1 + 1] = r0
  231. I_RD_REG(RTC_CNTL_STORE1_REG, 16, 31), // r0 = REG_READ(RTC_CNTL_STORE1_REG[31:16])
  232. I_ST(R0, R1, 2), // mem[r1 + 2] = r0
  233. I_RD_REG(RTC_CNTL_STORE1_REG, 20, 27), // r0 = REG_READ(RTC_CNTL_STORE1_REG[27:20])
  234. I_ST(R0, R1, 3), // mem[r1 + 3] = r0
  235. I_WR_REG(RTC_CNTL_STORE0_REG, 0, 7, 0x89), // REG_WRITE(RTC_CNTL_STORE0_REG[7:0], 0x89)
  236. I_WR_REG(RTC_CNTL_STORE0_REG, 8, 15, 0xab), // REG_WRITE(RTC_CNTL_STORE0_REG[15:8], 0xab)
  237. I_WR_REG(RTC_CNTL_STORE0_REG, 16, 23, 0xcd), // REG_WRITE(RTC_CNTL_STORE0_REG[23:16], 0xcd)
  238. I_WR_REG(RTC_CNTL_STORE0_REG, 24, 31, 0xef), // REG_WRITE(RTC_CNTL_STORE0_REG[31:24], 0xef)
  239. I_LD(R0, R1, 4), // r0 = mem[r1 + 4]
  240. I_ADDI(R0, R0, 1), // r0 = r0 + 1
  241. I_ST(R0, R1, 4), // mem[r1 + 4] = r0
  242. I_END(), // stop ULP timer
  243. I_HALT() // halt
  244. };
  245. /* Set data in the peripheral register to be read by the ULP co-processor */
  246. REG_WRITE(RTC_CNTL_STORE1_REG, 0x89abcdef);
  247. /* Calculate the size of the ULP co-processor binary, load it and run the ULP coprocessor */
  248. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  249. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  250. TEST_ESP_OK(ulp_run(0));
  251. /* Wait for the ULP co-processor to finish up */
  252. vTaskDelay(100/portTICK_PERIOD_MS);
  253. /* Verify the test results */
  254. TEST_ASSERT_EQUAL_HEX32(0xefcdab89, REG_READ(RTC_CNTL_STORE0_REG));
  255. TEST_ASSERT_EQUAL_HEX16(0xcdef, RTC_SLOW_MEM[64] & 0xffff);
  256. TEST_ASSERT_EQUAL_HEX16(0xde, RTC_SLOW_MEM[65] & 0xffff);
  257. TEST_ASSERT_EQUAL_HEX16(0x89ab, RTC_SLOW_MEM[66] & 0xffff);
  258. TEST_ASSERT_EQUAL_HEX16(0x9a, RTC_SLOW_MEM[67] & 0xffff);
  259. TEST_ASSERT_EQUAL_HEX16(1, RTC_SLOW_MEM[68] & 0xffff);
  260. /* Restore initial calibration values */
  261. REG_WRITE(RTC_CNTL_STORE0_REG, rtc_store0);
  262. REG_WRITE(RTC_CNTL_STORE1_REG, rtc_store1);
  263. }
  264. TEST_CASE("ULP FSM I_WR_REG instruction test", "[ulp]")
  265. {
  266. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  267. #pragma GCC diagnostic push
  268. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  269. #pragma GCC diagnostic ignored "-Warray-bounds"
  270. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  271. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  272. #pragma GCC diagnostic pop
  273. /* Define the test set */
  274. typedef struct {
  275. int low;
  276. int width;
  277. } wr_reg_test_item_t;
  278. const wr_reg_test_item_t test_items[] = {
  279. {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}, {0, 8},
  280. {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8},
  281. {15, 1}, {15, 2}, {15, 3}, {15, 4}, {15, 5}, {15, 6}, {15, 7}, {15, 8},
  282. {16, 1}, {16, 2}, {16, 3}, {16, 4}, {16, 5}, {16, 6}, {16, 7}, {16, 8},
  283. {18, 1}, {18, 2}, {18, 3}, {18, 4}, {18, 5}, {18, 6}, {18, 7}, {18, 8},
  284. {24, 1}, {24, 2}, {24, 3}, {24, 4}, {24, 5}, {24, 6}, {24, 7}, {24, 8},
  285. };
  286. const size_t test_items_count =
  287. sizeof(test_items)/sizeof(test_items[0]);
  288. for (size_t i = 0; i < test_items_count; ++i) {
  289. const uint32_t mask = (uint32_t) (((1ULL << test_items[i].width) - 1) << test_items[i].low);
  290. const uint32_t not_mask = ~mask;
  291. printf("#%2d: low: %2d width: %2d mask: %08" PRIx32 " expected: %08" PRIx32 " ", i,
  292. test_items[i].low, test_items[i].width,
  293. mask, not_mask);
  294. /* Set all bits in RTC_CNTL_STORE0_REG and reset all bits in RTC_CNTL_STORE1_REG */
  295. uint32_t rtc_store0 = REG_READ(RTC_CNTL_STORE0_REG);
  296. uint32_t rtc_store1 = REG_READ(RTC_CNTL_STORE1_REG);
  297. REG_WRITE(RTC_CNTL_STORE0_REG, 0xffffffff);
  298. REG_WRITE(RTC_CNTL_STORE1_REG, 0x00000000);
  299. /* ULP co-processor program to write to peripheral registers */
  300. const ulp_insn_t program[] = {
  301. I_WR_REG(RTC_CNTL_STORE0_REG,
  302. test_items[i].low,
  303. test_items[i].low + test_items[i].width - 1,
  304. 0),
  305. I_WR_REG(RTC_CNTL_STORE1_REG,
  306. test_items[i].low,
  307. test_items[i].low + test_items[i].width - 1,
  308. 0xff & ((1 << test_items[i].width) - 1)),
  309. I_END(),
  310. I_HALT()
  311. };
  312. /* Calculate the size of the ULP co-processor binary, load it and run the ULP coprocessor */
  313. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  314. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  315. TEST_ESP_OK(ulp_run(0));
  316. /* Wait for the ULP co-processor to finish up */
  317. vTaskDelay(10/portTICK_PERIOD_MS);
  318. /* Verify the test results */
  319. uint32_t clear = REG_READ(RTC_CNTL_STORE0_REG);
  320. uint32_t set = REG_READ(RTC_CNTL_STORE1_REG);
  321. printf("clear: %08" PRIx32 " set: %08" PRIx32 "\n", clear, set);
  322. /* Restore initial calibration values */
  323. REG_WRITE(RTC_CNTL_STORE0_REG, rtc_store0);
  324. REG_WRITE(RTC_CNTL_STORE1_REG, rtc_store1);
  325. TEST_ASSERT_EQUAL_HEX32(not_mask, clear);
  326. TEST_ASSERT_EQUAL_HEX32(mask, set);
  327. }
  328. }
  329. TEST_CASE("ULP FSM controls RTC_IO", "[ulp][ignore]")
  330. {
  331. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  332. #pragma GCC diagnostic push
  333. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  334. #pragma GCC diagnostic ignored "-Warray-bounds"
  335. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  336. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  337. #pragma GCC diagnostic pop
  338. /* ULP co-processor program to toggle LED */
  339. const ulp_insn_t program[] = {
  340. I_MOVI(R0, 0), // r0 is LED state
  341. I_MOVI(R2, 16), // loop r2 from 16 down to 0
  342. M_LABEL(4), // define label 4
  343. I_SUBI(R2, R2, 1), // r2 = r2 - 1
  344. M_BXZ(6), // branch to label 6 if r2 = 0
  345. I_ADDI(R0, R0, 1), // r0 = (r0 + 1) % 2
  346. I_ANDI(R0, R0, 0x1),
  347. M_BL(0, 1), // if r0 < 1 goto 0
  348. M_LABEL(1), // define label 1
  349. I_WR_REG(RTC_GPIO_OUT_REG, 26, 27, 1), // RTC_GPIO12 = 1
  350. M_BX(2), // goto 2
  351. M_LABEL(0), // define label 0
  352. I_WR_REG(RTC_GPIO_OUT_REG, 26, 27, 0), // RTC_GPIO12 = 0
  353. M_LABEL(2), // define label 2
  354. I_MOVI(R1, 100), // loop R1 from 100 down to 0
  355. M_LABEL(3), // define label 3
  356. I_SUBI(R1, R1, 1), // r1 = r1 - 1
  357. M_BXZ(5), // branch to label 5 if r1 = 0
  358. I_DELAY(32000), // delay for a while
  359. M_BX(3), // goto 3
  360. M_LABEL(5), // define label 5
  361. M_BX(4), // loop back to label 4
  362. M_LABEL(6), // define label 6
  363. I_WAKE(), // wake up the SoC
  364. I_END(), // stop ULP program timer
  365. I_HALT()
  366. };
  367. /* Configure LED GPIOs */
  368. const gpio_num_t led_gpios[] = {
  369. GPIO_NUM_2,
  370. GPIO_NUM_0,
  371. GPIO_NUM_4
  372. };
  373. for (size_t i = 0; i < sizeof(led_gpios)/sizeof(led_gpios[0]); ++i) {
  374. rtc_gpio_init(led_gpios[i]);
  375. rtc_gpio_set_direction(led_gpios[i], RTC_GPIO_MODE_OUTPUT_ONLY);
  376. rtc_gpio_set_level(led_gpios[i], 0);
  377. }
  378. /* Calculate the size of the ULP co-processor binary, load it and run the ULP coprocessor */
  379. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  380. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  381. TEST_ESP_OK(ulp_run(0));
  382. /* Setup wakeup triggers */
  383. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  384. /* Enter Deep Sleep */
  385. esp_deep_sleep_start();
  386. UNITY_TEST_FAIL(__LINE__, "Should not get here!");
  387. }
  388. TEST_CASE("ULP FSM power consumption in deep sleep", "[ulp][ignore]")
  389. {
  390. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 4 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  391. #pragma GCC diagnostic push
  392. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  393. #pragma GCC diagnostic ignored "-Warray-bounds"
  394. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  395. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  396. #pragma GCC diagnostic pop
  397. /* Put the ULP coprocessor in halt state */
  398. ulp_insn_t insn = I_HALT();
  399. #pragma GCC diagnostic push
  400. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  401. #pragma GCC diagnostic ignored "-Warray-bounds"
  402. memcpy(&RTC_SLOW_MEM[0], &insn, sizeof(insn));
  403. #pragma GCC diagnostic pop
  404. /* Set ULP timer */
  405. ulp_set_wakeup_period(0, 0x8000);
  406. /* Run the ULP coprocessor */
  407. TEST_ESP_OK(ulp_run(0));
  408. /* Setup wakeup triggers */
  409. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  410. TEST_ASSERT(esp_sleep_enable_timer_wakeup(10 * 1000000) == ESP_OK);
  411. /* Enter Deep Sleep */
  412. esp_deep_sleep_start();
  413. UNITY_TEST_FAIL(__LINE__, "Should not get here!");
  414. }
  415. TEST_CASE("ULP FSM timer setting", "[ulp]")
  416. {
  417. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 32 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  418. #pragma GCC diagnostic push
  419. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  420. #pragma GCC diagnostic ignored "-Warray-bounds"
  421. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  422. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  423. #pragma GCC diagnostic pop
  424. /*
  425. * Run a simple ULP program which increments the counter, for one second.
  426. * Program calls I_HALT each time and gets restarted by the timer.
  427. * Compare the expected number of times the program runs with the actual.
  428. */
  429. const int offset = 6;
  430. const ulp_insn_t program[] = {
  431. I_MOVI(R1, offset), // r1 <- offset
  432. I_LD(R2, R1, 0), // load counter
  433. I_ADDI(R2, R2, 1), // counter += 1
  434. I_ST(R2, R1, 0), // save counter
  435. I_HALT(),
  436. };
  437. /* Calculate the size of the ULP co-processor binary, load it and run the ULP coprocessor */
  438. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  439. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  440. assert(offset >= size && "data offset needs to be greater or equal to program size");
  441. TEST_ESP_OK(ulp_run(0));
  442. /* Disable the ULP program timer — we will enable it later */
  443. ulp_timer_stop();
  444. /* Define the test data */
  445. const uint32_t cycles_to_test[] = { 10000, // 10 ms
  446. 20000, // 20 ms
  447. 50000, // 50 ms
  448. 100000, // 100 ms
  449. 200000, // 200 ms
  450. 500000, // 500 ms
  451. 1000000 }; // 1 sec
  452. const size_t tests_count = sizeof(cycles_to_test) / sizeof(cycles_to_test[0]);
  453. for (size_t i = 0; i < tests_count; ++i) {
  454. // zero out the counter
  455. RTC_SLOW_MEM[offset] = 0;
  456. // set the ulp timer period
  457. ulp_set_wakeup_period(0, cycles_to_test[i]);
  458. // enable the timer and wait for a second
  459. ulp_timer_resume();
  460. vTaskDelay(1000 / portTICK_PERIOD_MS);
  461. // stop the timer and get the counter value
  462. ulp_timer_stop();
  463. uint32_t counter = RTC_SLOW_MEM[offset] & 0xffff;
  464. // calculate the expected counter value and allow a tolerance of 15%
  465. uint32_t expected_counter = 1000000 / cycles_to_test[i];
  466. uint32_t tolerance = (expected_counter * 15 / 100);
  467. tolerance = tolerance ? tolerance : 1; // Keep a tolerance of at least 1 count
  468. printf("expected: %" PRIu32 "\t tolerance: +/- %" PRIu32 "\t actual: %" PRIu32 "\n", expected_counter, tolerance, counter);
  469. // Should be within 15%
  470. TEST_ASSERT_INT_WITHIN(tolerance, expected_counter, counter);
  471. }
  472. }
  473. #if !DISABLED_FOR_TARGETS(ESP32)
  474. TEST_CASE("ULP FSM can use temperature sensor (TSENS) in deep sleep", "[ulp][ignore]")
  475. {
  476. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  477. #pragma GCC diagnostic push
  478. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  479. #pragma GCC diagnostic ignored "-Warray-bounds"
  480. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  481. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  482. #pragma GCC diagnostic pop
  483. // Allow TSENS to be controlled by the ULP
  484. SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 10, SENS_TSENS_CLK_DIV_S);
  485. #if CONFIG_IDF_TARGET_ESP32S2
  486. SET_PERI_REG_BITS(SENS_SAR_POWER_XPD_SAR_REG, SENS_FORCE_XPD_SAR, SENS_FORCE_XPD_SAR_FSM, SENS_FORCE_XPD_SAR_S);
  487. SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL2_REG, SENS_TSENS_CLKGATE_EN);
  488. #elif CONFIG_IDF_TARGET_ESP32S3
  489. SET_PERI_REG_BITS(SENS_SAR_POWER_XPD_SAR_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);
  490. SET_PERI_REG_MASK(SENS_SAR_PERI_CLK_GATE_CONF_REG, SENS_TSENS_CLK_EN);
  491. #endif
  492. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
  493. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
  494. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE);
  495. // data start offset
  496. size_t offset = 20;
  497. // number of samples to collect
  498. RTC_SLOW_MEM[offset] = (CONFIG_ULP_COPROC_RESERVE_MEM) / 4 - offset - 8;
  499. // sample counter
  500. RTC_SLOW_MEM[offset + 1] = 0;
  501. /* ULP co-processor program to record temperature sensor readings */
  502. const ulp_insn_t program[] = {
  503. I_MOVI(R1, offset), // r1 <- offset
  504. I_LD(R2, R1, 1), // r2 <- counter
  505. I_LD(R3, R1, 0), // r3 <- length
  506. I_SUBI(R3, R3, 1), // end = length - 1
  507. I_SUBR(R3, R3, R2), // r3 = length - counter
  508. M_BXF(1), // if overflow goto 1:
  509. I_TSENS(R0, 16383), // r0 <- tsens
  510. I_ST(R0, R2, offset + 4), // mem[r2 + offset +4] <- r0
  511. I_ADDI(R2, R2, 1), // counter += 1
  512. I_ST(R2, R1, 1), // save counter
  513. I_HALT(), // enter sleep
  514. M_LABEL(1), // done with measurements
  515. I_END(), // stop ULP timer
  516. I_WAKE(), // initiate wakeup
  517. I_HALT()
  518. };
  519. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  520. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  521. assert(offset >= size);
  522. /* Run the ULP coprocessor */
  523. TEST_ESP_OK(ulp_run(0));
  524. /* Setup wakeup triggers */
  525. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  526. TEST_ASSERT(esp_sleep_enable_timer_wakeup(10 * 1000000) == ESP_OK);
  527. /* Enter Deep Sleep */
  528. esp_deep_sleep_start();
  529. UNITY_TEST_FAIL(__LINE__, "Should not get here!");
  530. }
  531. #endif //#if !DISABLED_FOR_TARGETS(ESP32)
  532. TEST_CASE("ULP FSM can use ADC in deep sleep", "[ulp][ignore]")
  533. {
  534. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 260 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  535. const int adc = 0;
  536. const int channel = 0;
  537. const int atten = 0;
  538. #pragma GCC diagnostic push
  539. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  540. #pragma GCC diagnostic ignored "-Warray-bounds"
  541. /* Clear the RTC_SLOW_MEM region for the ULP co-processor binary to be loaded */
  542. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  543. #pragma GCC diagnostic pop
  544. #if defined(CONFIG_IDF_TARGET_ESP32)
  545. // Configure SAR ADCn resolution
  546. SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_SAR1_BIT_WIDTH, 3, SENS_SAR1_BIT_WIDTH_S);
  547. SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_SAR2_BIT_WIDTH, 3, SENS_SAR2_BIT_WIDTH_S);
  548. SET_PERI_REG_BITS(SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_BIT, 0x3, SENS_SAR1_SAMPLE_BIT_S);
  549. SET_PERI_REG_BITS(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_BIT, 0x3, SENS_SAR2_SAMPLE_BIT_S);
  550. // SAR ADCn is started by ULP FSM
  551. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_FORCE);
  552. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_FORCE);
  553. // Use ULP FSM to power up SAR ADCn
  554. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);
  555. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_AMP, 2, SENS_FORCE_XPD_AMP_S);
  556. // SAR ADCn invert result
  557. SET_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR1_DATA_INV);
  558. SET_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR2_DATA_INV);
  559. // Set SAR ADCn pad enable bitmap to be controlled by ULP FSM
  560. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD_FORCE_M);
  561. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD_FORCE_M);
  562. #elif defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
  563. // SAR ADCn is started by ULP FSM
  564. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_START_FORCE);
  565. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS1_CTRL2_REG, SENS_MEAS1_START_FORCE);
  566. // Use ULP FSM to power up/down SAR ADCn
  567. SET_PERI_REG_BITS(SENS_SAR_POWER_XPD_SAR_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);
  568. SET_PERI_REG_BITS(SENS_SAR_MEAS1_CTRL1_REG, SENS_FORCE_XPD_AMP, 2, SENS_FORCE_XPD_AMP_S);
  569. // SAR1 invert result
  570. SET_PERI_REG_MASK(SENS_SAR_READER1_CTRL_REG, SENS_SAR1_DATA_INV);
  571. SET_PERI_REG_MASK(SENS_SAR_READER2_CTRL_REG, SENS_SAR2_DATA_INV);
  572. // Set SAR ADCn pad enable bitmap to be controlled by ULP FSM
  573. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS1_CTRL2_REG, SENS_SAR1_EN_PAD_FORCE_M);
  574. CLEAR_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_SAR2_EN_PAD_FORCE_M);
  575. // Enable SAR ADCn clock gate on esp32s3
  576. #if CONFIG_IDF_TARGET_ESP32S3
  577. SET_PERI_REG_MASK(SENS_SAR_PERI_CLK_GATE_CONF_REG, SENS_SARADC_CLK_EN);
  578. #endif
  579. #endif
  580. SET_PERI_REG_BITS(SENS_SAR_ATTEN1_REG, 3, atten, 2 * channel); //set SAR1 attenuation
  581. SET_PERI_REG_BITS(SENS_SAR_ATTEN2_REG, 3, atten, 2 * channel); //set SAR2 attenuation
  582. // data start offset
  583. size_t offset = 20;
  584. // number of samples to collect
  585. RTC_SLOW_MEM[offset] = (CONFIG_ULP_COPROC_RESERVE_MEM) / 4 - offset - 8;
  586. // sample counter
  587. RTC_SLOW_MEM[offset + 1] = 0;
  588. const ulp_insn_t program[] = {
  589. I_MOVI(R1, offset), // r1 <- offset
  590. I_LD(R2, R1, 1), // r2 <- counter
  591. I_LD(R3, R1, 0), // r3 <- length
  592. I_SUBI(R3, R3, 1), // end = length - 1
  593. I_SUBR(R3, R3, R2), // r3 = length - counter
  594. M_BXF(1), // if overflow goto 1:
  595. I_ADC(R0, adc, channel), // r0 <- ADC
  596. I_ST(R0, R2, offset + 4), // mem[r2 + offset +4] = r0
  597. I_ADDI(R2, R2, 1), // counter += 1
  598. I_ST(R2, R1, 1), // save counter
  599. I_HALT(), // enter sleep
  600. M_LABEL(1), // done with measurements
  601. I_END(), // stop ULP program timer
  602. I_HALT()
  603. };
  604. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  605. TEST_ESP_OK(ulp_process_macros_and_load(0, program, &size));
  606. assert(offset >= size);
  607. /* Run the ULP coprocessor */
  608. TEST_ESP_OK(ulp_run(0));
  609. /* Setup wakeup triggers */
  610. TEST_ASSERT(esp_sleep_enable_ulp_wakeup() == ESP_OK);
  611. TEST_ASSERT(esp_sleep_enable_timer_wakeup(10 * 1000000) == ESP_OK);
  612. /* Enter Deep Sleep */
  613. esp_deep_sleep_start();
  614. UNITY_TEST_FAIL(__LINE__, "Should not get here!");
  615. }