test_ulp.c 30 KB

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