test_esp32s2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. Tests for the adc device driver on ESP32-S2 only
  8. */
  9. #include "sdkconfig.h"
  10. #if CONFIG_IDF_TARGET_ESP32S2
  11. #include "esp_system.h"
  12. #include "esp_intr_alloc.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. #include "freertos/queue.h"
  16. #include "driver/adc.h"
  17. #include "driver/rtc_io.h"
  18. #include "driver/gpio.h"
  19. #include "unity.h"
  20. #include "esp_system.h"
  21. #include "esp_event.h"
  22. #include "esp_wifi.h"
  23. #include "esp_log.h"
  24. #include "nvs_flash.h"
  25. #include "test_utils.h"
  26. #include "soc/adc_periph.h"
  27. #include "test/test_common_adc.h"
  28. #include "esp_rom_sys.h"
  29. #include "driver/dac.h"
  30. #include "soc/system_reg.h"
  31. #include "soc/spi_reg.h"
  32. #include "soc/soc.h"
  33. #include "soc/lldesc.h"
  34. #include "test/test_adc_dac_dma.h"
  35. static const char *TAG = "test_adc";
  36. #define PLATFORM_SELECT (1) //0: pxp; 1: chip
  37. #if (PLATFORM_SELECT == 0) //PXP platform
  38. #include "soc/syscon_reg.h"
  39. #define SET_BREAK_POINT(flag) REG_WRITE(SYSCON_DATE_REG, flag)
  40. //PXP clk is slower.
  41. #define SYS_DELAY_TIME_MOM (1/40)
  42. #define RTC_SLOW_CLK_FLAG 1 // Slow clock is 32KHz.
  43. static void test_pxp_deinit_io(void)
  44. {
  45. for (int i = 0; i < 22; i++) {
  46. rtc_gpio_init(i);
  47. }
  48. }
  49. #else
  50. //PXP clk is slower.
  51. #define SET_BREAK_POINT(flag)
  52. #define SYS_DELAY_TIME_MOM (1)
  53. #define RTC_SLOW_CLK_FLAG 0 // Slow clock is 32KHz.
  54. #endif
  55. #define ADC_REG_BASE_TEST() ({ \
  56. TEST_ASSERT_EQUAL_UINT32(REG_GET_FIELD(APB_SARADC_APB_CTRL_DATE_REG, APB_SARADC_APB_CTRL_DATE), APB_SARADC.apb_ctrl_date); \
  57. TEST_ASSERT_EQUAL_UINT32(REG_GET_FIELD(SENS_SARDATE_REG, SENS_SAR_DATE), SENS.sardate.sar_date); \
  58. TEST_ASSERT_EQUAL_UINT32(REG_GET_FIELD(RTC_IO_DATE_REG, RTC_IO_IO_DATE), RTCIO.date.date); \
  59. })
  60. /** Sample rate = APB_CLK(80 MHz) / (CLK_DIV + 1) / TRIGGER_INTERVAL / 2. */
  61. #define TEST_ADC_TRIGGER_INTERVAL_DEFAULT (40)
  62. #define TEST_ADC_DIGI_CLK_DIV_DEFAULT (9)
  63. static uint8_t adc_test_num = 9;
  64. static adc_channel_t adc_list[SOC_ADC_PATT_LEN_MAX] = {
  65. ADC_CHANNEL_0,
  66. ADC_CHANNEL_1,
  67. ADC_CHANNEL_2,
  68. ADC_CHANNEL_3,
  69. ADC_CHANNEL_4,
  70. ADC_CHANNEL_5,
  71. ADC_CHANNEL_6,
  72. // ADC_CHANNEL_7, // Workaround: IO18 is pullup outside in ESP32S2-Saola Runner.
  73. ADC_CHANNEL_8,
  74. ADC_CHANNEL_9,
  75. };
  76. /* For ESP32S2, it should use same atten, or, it will have error. */
  77. #define TEST_ADC_ATTEN_DEFAULT (ADC_ATTEN_11db)
  78. extern esp_err_t adc_digi_reset(void);
  79. /* Work mode.
  80. * single: eof_num;
  81. * double: SAR_EOF_NUMBER/2;
  82. * alter: eof_num;
  83. * */
  84. #define SAR_SIMPLE_NUM 512 // Set sample number of enabled unit.
  85. /* Use two DMA linker to save ADC data. ADC sample 1 times -> 2 byte data -> 2 DMA link buf. */
  86. #define SAR_DMA_DATA_SIZE(unit, sample_num) (SAR_EOF_NUMBER(unit, sample_num))
  87. #define SAR_EOF_NUMBER(unit, sample_num) ((sample_num) * (unit))
  88. #define SAR_MEAS_LIMIT_NUM(unit, sample_num) (SAR_SIMPLE_NUM)
  89. #define SAR_SIMPLE_TIMEOUT_MS 1000
  90. typedef struct dma_msg {
  91. uint32_t int_msk;
  92. uint8_t *data;
  93. uint32_t data_len;
  94. } adc_dma_event_t;
  95. static uint8_t link_buf[2][SAR_DMA_DATA_SIZE(2, SAR_SIMPLE_NUM)] = {0};
  96. static lldesc_t dma1 = {0};
  97. static lldesc_t dma2 = {0};
  98. static QueueHandle_t que_adc = NULL;
  99. static adc_dma_event_t adc_evt;
  100. /** ADC-DMA ISR handler. */
  101. static IRAM_ATTR void adc_dma_isr(void *arg)
  102. {
  103. uint32_t int_st = REG_READ(SPI_DMA_INT_ST_REG(3));
  104. int task_awoken = pdFALSE;
  105. REG_WRITE(SPI_DMA_INT_CLR_REG(3), int_st);
  106. if (int_st & SPI_IN_SUC_EOF_INT_ST_M) {
  107. adc_evt.int_msk = int_st;
  108. xQueueSendFromISR(que_adc, &adc_evt, &task_awoken);
  109. }
  110. if (int_st & SPI_IN_DONE_INT_ST) {
  111. adc_evt.int_msk = int_st;
  112. xQueueSendFromISR(que_adc, &adc_evt, &task_awoken);
  113. }
  114. ESP_EARLY_LOGV(TAG, "int msk%x\n", int_st);
  115. if (task_awoken == pdTRUE) {
  116. portYIELD_FROM_ISR();
  117. }
  118. }
  119. /**
  120. * DMA liner initialization and start.
  121. * @param is_loop
  122. * - true: The two dma linked lists are connected end to end, with no end mark (eof).
  123. * - false: The two dma linked lists are connected end to end, with end mark (eof).
  124. */
  125. static uint32_t adc_dma_linker_init(adc_unit_t adc, bool is_loop)
  126. {
  127. dma1 = (lldesc_t) {
  128. .size = SAR_DMA_DATA_SIZE((adc > 2) ? 2 : 1, SAR_SIMPLE_NUM),
  129. .owner = 1,
  130. .buf = &link_buf[0][0],
  131. .qe.stqe_next = &dma2,
  132. };
  133. dma2 = (lldesc_t) {
  134. .size = SAR_DMA_DATA_SIZE((adc > 2) ? 2 : 1, SAR_SIMPLE_NUM),
  135. .owner = 1,
  136. .buf = &link_buf[1][0],
  137. };
  138. if (is_loop) {
  139. dma2.qe.stqe_next = &dma1;
  140. } else {
  141. dma2.qe.stqe_next = NULL;
  142. }
  143. return (uint32_t)&dma1;
  144. }
  145. #define DEBUG_CHECK_ENABLE 1
  146. #define DEBUG_PRINT_ENABLE 1
  147. #define DEBUG_CHECK_ERROR 10
  148. /**
  149. * Check the ADC-DMA data in linker buffer by input level.
  150. * ideal_level
  151. * - -1: Don't check data.
  152. * - 0: ADC channel voltage is 0v.
  153. * - 1: ADC channel voltage is 3.3v.
  154. * - 2: ADC channel voltage is 1.4v.
  155. */
  156. static esp_err_t adc_dma_data_check(adc_unit_t adc, int ideal_level)
  157. {
  158. int unit_old = 1;
  159. int ch_cnt = 0;
  160. for (int cnt = 0; cnt < 2; cnt++) {
  161. esp_rom_printf("\n[%s] link_buf[%d]: \n", __func__, cnt % 2);
  162. for (int i = 0; i < SAR_DMA_DATA_SIZE((adc > 2) ? 2 : 1, SAR_SIMPLE_NUM); i += 2) {
  163. uint8_t h = link_buf[cnt % 2][i + 1], l = link_buf[cnt % 2][i];
  164. uint16_t temp = (h << 8 | l);
  165. adc_digi_output_data_t *data = (adc_digi_output_data_t *)&temp;
  166. if (adc > ADC_UNIT_2) { //ADC_ENCODE_11BIT
  167. #if DEBUG_PRINT_ENABLE
  168. if (i % 16 == 0) {
  169. esp_rom_printf("\n");
  170. }
  171. esp_rom_printf("[%d_%d_%04x] ", data->type2.unit, data->type2.channel, data->type2.data);
  172. #endif
  173. #if DEBUG_CHECK_ENABLE
  174. if (ideal_level >= 0) {
  175. TEST_ASSERT_NOT_EQUAL(unit_old, data->type2.unit);
  176. unit_old = data->type2.unit;
  177. if (data->type2.channel > ADC_CHANNEL_MAX) {
  178. printf("Data invalid [%d]\n", data->type2.channel);
  179. continue;
  180. }
  181. int cur_ch = ((ch_cnt++ / 2) % adc_test_num);
  182. TEST_ASSERT_EQUAL( data->type2.channel, adc_list[cur_ch] );
  183. }
  184. if (ideal_level == 1) { // high level 3.3v
  185. TEST_ASSERT_EQUAL( 0x7FF, data->type2.data );
  186. } else if (ideal_level == 0) { // low level 0v
  187. TEST_ASSERT_LESS_THAN( 10, data->type2.data );
  188. } else if (ideal_level == 2) { // middle level 1.4v
  189. TEST_ASSERT_INT_WITHIN( 128, 1100, data->type2.data );
  190. } else if (ideal_level == 3) { // normal level
  191. } else { // no check
  192. }
  193. #endif
  194. } else { //ADC_ENCODE_12BIT
  195. #if DEBUG_PRINT_ENABLE
  196. if (i % 16 == 0) {
  197. esp_rom_printf("\n");
  198. }
  199. esp_rom_printf("[%d_%04x] ", data->type1.channel, data->type1.data);
  200. #endif
  201. #if DEBUG_CHECK_ENABLE
  202. if (ideal_level >= 0) {
  203. int cur_ch = ((ch_cnt++) % adc_test_num);
  204. TEST_ASSERT_EQUAL( adc_list[cur_ch], data->type1.channel );
  205. }
  206. if (ideal_level == 1) { // high level 3.3v
  207. TEST_ASSERT_EQUAL( 0XFFF, data->type1.data );
  208. } else if (ideal_level == 0) { // low level 0v
  209. TEST_ASSERT_LESS_THAN( 10, data->type1.data );
  210. } else if (ideal_level == 2) { // middle level 1.4v
  211. TEST_ASSERT_INT_WITHIN( 256, 2200, data->type1.data );
  212. } else if (ideal_level == 3) { // normal level
  213. } else { // no check
  214. }
  215. #endif
  216. }
  217. link_buf[cnt % 2][i] = 0;
  218. link_buf[cnt % 2][i + 1] = 0;
  219. }
  220. esp_rom_printf("\n");
  221. }
  222. return ESP_OK;
  223. }
  224. static esp_err_t adc_dma_data_multi_st_check(adc_unit_t adc, void *dma_addr, uint32_t int_mask)
  225. {
  226. adc_dma_event_t evt;
  227. ESP_LOGI(TAG, "adc IO normal, test ...");
  228. for (int i = 0; i < adc_test_num; i++) {
  229. adc_io_normal(adc, adc_list[i]);
  230. }
  231. TEST_ESP_OK( adc_digi_start() );
  232. while (1) {
  233. TEST_ASSERT_EQUAL( xQueueReceive(que_adc, &evt, SAR_SIMPLE_TIMEOUT_MS / portTICK_RATE_MS), pdTRUE );
  234. if (evt.int_msk & SPI_IN_SUC_EOF_INT_ENA) {
  235. break;
  236. }
  237. }
  238. TEST_ESP_OK( adc_digi_stop() );
  239. adc_dac_dma_linker_start(DMA_ONLY_ADC_INLINK, (void *)dma_addr, int_mask);
  240. adc_digi_reset();
  241. TEST_ESP_OK( adc_dma_data_check(adc, -1) ); // Don't check data.
  242. ESP_LOGI(TAG, "adc IO fake tie high, test ...");
  243. for (int i = 0; i < adc_test_num; i++) {
  244. adc_fake_tie_high(adc, adc_list[i]);
  245. }
  246. TEST_ESP_OK( adc_digi_start() );
  247. while (1) {
  248. TEST_ASSERT_EQUAL( xQueueReceive(que_adc, &evt, SAR_SIMPLE_TIMEOUT_MS / portTICK_RATE_MS), pdTRUE );
  249. if (evt.int_msk & SPI_IN_SUC_EOF_INT_ENA) {
  250. break;
  251. }
  252. }
  253. TEST_ESP_OK( adc_digi_stop() );
  254. adc_dac_dma_linker_start(DMA_ONLY_ADC_INLINK, (void *)dma_addr, int_mask);
  255. adc_digi_reset();
  256. TEST_ESP_OK( adc_dma_data_check(adc, 1) );
  257. ESP_LOGI(TAG, "adc IO fake tie low, test ...");
  258. for (int i = 0; i < adc_test_num; i++) {
  259. adc_fake_tie_low(adc, adc_list[i]);
  260. }
  261. TEST_ESP_OK( adc_digi_start() );
  262. while (1) {
  263. TEST_ASSERT_EQUAL( xQueueReceive(que_adc, &evt, SAR_SIMPLE_TIMEOUT_MS / portTICK_RATE_MS), pdTRUE );
  264. if (evt.int_msk & SPI_IN_SUC_EOF_INT_ENA) {
  265. break;
  266. }
  267. }
  268. TEST_ESP_OK( adc_digi_stop() );
  269. adc_dac_dma_linker_start(DMA_ONLY_ADC_INLINK, (void *)dma_addr, int_mask);
  270. adc_digi_reset();
  271. TEST_ESP_OK( adc_dma_data_check(adc, 0) );
  272. ESP_LOGI(TAG, "adc IO fake tie middle, test ...");
  273. for (int i = 0; i < adc_test_num; i++) {
  274. adc_fake_tie_middle(adc, adc_list[i]);
  275. }
  276. TEST_ESP_OK( adc_digi_start() );
  277. while (1) {
  278. TEST_ASSERT_EQUAL( xQueueReceive(que_adc, &evt, SAR_SIMPLE_TIMEOUT_MS / portTICK_RATE_MS), pdTRUE );
  279. if (evt.int_msk & SPI_IN_SUC_EOF_INT_ENA) {
  280. break;
  281. }
  282. }
  283. TEST_ESP_OK( adc_digi_stop() );
  284. adc_dac_dma_linker_start(DMA_ONLY_ADC_INLINK, (void *)dma_addr, int_mask);
  285. adc_digi_reset();
  286. TEST_ESP_OK( adc_dma_data_check(adc, 2) );
  287. return ESP_OK;
  288. }
  289. #include "soc/apb_saradc_struct.h"
  290. /**
  291. * Test the partten table setting. It's easy wrong.
  292. *
  293. * @param adc_n ADC unit.
  294. * @param in_partten_len The length of partten be set.
  295. * @param in_last_ch The channel number of the last message.
  296. */
  297. static esp_err_t adc_check_patt_table(adc_unit_t adc, uint32_t in_partten_len, adc_channel_t in_last_ch)
  298. {
  299. esp_err_t ret = ESP_FAIL;
  300. uint8_t index = (in_partten_len - 1) / 4;
  301. uint8_t offset = 24 - ((in_partten_len - 1) % 4) * 8;
  302. uint32_t temp = 0, len;
  303. if (adc & ADC_UNIT_1) {
  304. len = APB_SARADC.ctrl.sar1_patt_len + 1;
  305. temp = APB_SARADC.sar1_patt_tab[index];
  306. printf("patt1 len %d\n", len);
  307. printf("patt1 0x%08x\n", APB_SARADC.sar1_patt_tab[0]);
  308. printf("patt1 0x%08x\n", APB_SARADC.sar1_patt_tab[1]);
  309. printf("patt1 0x%08x\n", APB_SARADC.sar1_patt_tab[2]);
  310. printf("patt1 0x%08x\n", APB_SARADC.sar1_patt_tab[3]);
  311. if (in_partten_len == len) {
  312. if (in_last_ch == (((temp >> (offset + 4))) & 0xf)) {
  313. ret = ESP_OK;
  314. }
  315. }
  316. }
  317. if (adc & ADC_UNIT_2) {
  318. len = APB_SARADC.ctrl.sar2_patt_len + 1;
  319. temp = APB_SARADC.sar2_patt_tab[index];
  320. printf("patt2 len %d\n", len);
  321. printf("patt2 0x%08x\n", APB_SARADC.sar2_patt_tab[0]);
  322. printf("patt2 0x%08x\n", APB_SARADC.sar2_patt_tab[1]);
  323. printf("patt2 0x%08x\n", APB_SARADC.sar2_patt_tab[2]);
  324. printf("patt2 0x%08x\n", APB_SARADC.sar2_patt_tab[3]);
  325. if (in_partten_len == len) {
  326. if (in_last_ch == (((temp >> (offset + 4))) & 0xf)) {
  327. ret = ESP_OK;
  328. }
  329. }
  330. }
  331. return ret;
  332. }
  333. /**
  334. * Testcase: Check the base function of ADC-DMA. Include:
  335. * - Various conversion modes.
  336. * - Whether the channel and data are lost.
  337. * - Whether the data is the same as the channel voltage.
  338. */
  339. int test_adc_dig_dma_single_unit(adc_unit_t adc)
  340. {
  341. ESP_LOGI(TAG, " >> %s << ", __func__);
  342. ESP_LOGI(TAG, " >> adc unit: %x << ", adc);
  343. TEST_ESP_OK( adc_digi_init() );
  344. /* arbiter config */
  345. adc_arbiter_t arb_cfg = {
  346. .mode = ADC_ARB_MODE_FIX,
  347. .dig_pri = 0,
  348. .pwdet_pri = 2,
  349. .rtc_pri = 1,
  350. };
  351. TEST_ESP_OK( adc_arbiter_config(ADC_UNIT_2, &arb_cfg) ); // If you want use force
  352. adc_digi_config_t config = {
  353. .conv_limit_en = false,
  354. .conv_limit_num = 0,
  355. .interval = TEST_ADC_TRIGGER_INTERVAL_DEFAULT,
  356. .dig_clk.use_apll = 0, // APB clk
  357. .dig_clk.div_num = TEST_ADC_DIGI_CLK_DIV_DEFAULT,
  358. .dig_clk.div_b = 0,
  359. .dig_clk.div_a = 0,
  360. .dma_eof_num = SAR_EOF_NUMBER((adc > 2) ? 2 : 1, SAR_SIMPLE_NUM),
  361. };
  362. /* Config pattern table */
  363. adc_digi_pattern_table_t adc1_patt[SOC_ADC_PATT_LEN_MAX] = {0};
  364. adc_digi_pattern_table_t adc2_patt[SOC_ADC_PATT_LEN_MAX] = {0};
  365. if (adc & ADC_UNIT_1) {
  366. config.adc1_pattern_len = adc_test_num;
  367. config.adc1_pattern = adc1_patt;
  368. for (int i = 0; i < adc_test_num; i++) {
  369. adc1_patt[i].atten = TEST_ADC_ATTEN_DEFAULT;
  370. adc1_patt[i].channel = adc_list[i];
  371. adc_gpio_init(ADC_UNIT_1, adc_list[i]);
  372. }
  373. }
  374. if (adc & ADC_UNIT_2) {
  375. config.adc2_pattern_len = adc_test_num;
  376. config.adc2_pattern = adc2_patt;
  377. for (int i = 0; i < adc_test_num; i++) {
  378. adc2_patt[i].atten = TEST_ADC_ATTEN_DEFAULT;
  379. adc2_patt[i].channel = adc_list[i];
  380. adc_gpio_init(ADC_UNIT_2, adc_list[i]);
  381. }
  382. }
  383. if (adc == ADC_UNIT_1) {
  384. config.conv_mode = ADC_CONV_SINGLE_UNIT_1;
  385. config.format = ADC_DIGI_FORMAT_12BIT;
  386. } else if (adc == ADC_UNIT_2) {
  387. config.conv_mode = ADC_CONV_SINGLE_UNIT_2;
  388. config.format = ADC_DIGI_FORMAT_12BIT;
  389. } else if (adc == ADC_UNIT_BOTH) {
  390. config.conv_mode = ADC_CONV_BOTH_UNIT;
  391. config.format = ADC_DIGI_FORMAT_11BIT;
  392. } else if (adc == ADC_UNIT_ALTER) {
  393. config.conv_mode = ADC_CONV_ALTER_UNIT;
  394. config.format = ADC_DIGI_FORMAT_11BIT;
  395. }
  396. TEST_ESP_OK( adc_digi_controller_config(&config) );
  397. /* ADC-DMA linker init */
  398. if (que_adc == NULL) {
  399. que_adc = xQueueCreate(5, sizeof(adc_dma_event_t));
  400. } else {
  401. xQueueReset(que_adc);
  402. }
  403. uint32_t int_mask = SPI_IN_SUC_EOF_INT_ENA;
  404. uint32_t dma_addr = adc_dma_linker_init(adc, false);
  405. adc_dac_dma_isr_register(adc_dma_isr, NULL, int_mask);
  406. adc_dac_dma_linker_start(DMA_ONLY_ADC_INLINK, (void *)dma_addr, int_mask);
  407. TEST_ESP_OK( adc_check_patt_table(adc, adc_test_num, adc_list[adc_test_num - 1]) );
  408. adc_dma_data_multi_st_check(adc, (void *)dma_addr, int_mask);
  409. adc_dac_dma_linker_deinit();
  410. adc_dac_dma_isr_deregister(adc_dma_isr, NULL);
  411. TEST_ESP_OK( adc_digi_deinit() );
  412. vTaskDelay(10 / portTICK_RATE_MS);
  413. return 0;
  414. }
  415. TEST_CASE("ADC DMA single read", "[ADC]")
  416. {
  417. test_adc_dig_dma_single_unit(ADC_UNIT_BOTH);
  418. test_adc_dig_dma_single_unit(ADC_UNIT_ALTER);
  419. test_adc_dig_dma_single_unit(ADC_UNIT_1);
  420. test_adc_dig_dma_single_unit(ADC_UNIT_2);
  421. }
  422. #include "touch_scope.h"
  423. /**
  424. * 0: ADC1 channels raw data debug.
  425. * 1: ADC2 channels raw data debug.
  426. * 2: ADC1 one channel raw data debug.
  427. */
  428. #define SCOPE_DEBUG_TYPE 0
  429. #define SCOPE_DEBUG_CHANNEL_MAX (10)
  430. #define SCOPE_DEBUG_ENABLE (0)
  431. #define SCOPE_UART_BUADRATE (256000)
  432. #define SCOPE_DEBUG_FREQ_MS (50)
  433. #define SCOPE_OUTPUT_UART (0)
  434. static float scope_temp[SCOPE_DEBUG_CHANNEL_MAX] = {0}; // max scope channel is 10.
  435. int test_adc_dig_scope_debug_unit(adc_unit_t adc)
  436. {
  437. ESP_LOGI(TAG, " >> %s << ", __func__);
  438. ESP_LOGI(TAG, " >> adc unit: %x << ", adc);
  439. TEST_ESP_OK( adc_digi_init() );
  440. if (adc & ADC_UNIT_2) {
  441. /* arbiter config */
  442. adc_arbiter_t arb_cfg = {
  443. .mode = ADC_ARB_MODE_FIX,
  444. .dig_pri = 0,
  445. .pwdet_pri = 2,
  446. .rtc_pri = 1,
  447. };
  448. TEST_ESP_OK( adc_arbiter_config(ADC_UNIT_2, &arb_cfg) ); // If you want use force
  449. }
  450. adc_digi_config_t config = {
  451. .conv_limit_en = false,
  452. .conv_limit_num = 0,
  453. .interval = TEST_ADC_TRIGGER_INTERVAL_DEFAULT,
  454. .dig_clk.use_apll = 0, // APB clk
  455. .dig_clk.div_num = TEST_ADC_DIGI_CLK_DIV_DEFAULT,
  456. .dig_clk.div_a = 0,
  457. .dig_clk.div_b = 0,
  458. .dma_eof_num = SAR_EOF_NUMBER((adc > 2) ? 2 : 1, SAR_SIMPLE_NUM),
  459. };
  460. /* Config pattern table */
  461. adc_digi_pattern_table_t adc1_patt[SOC_ADC_PATT_LEN_MAX] = {0};
  462. adc_digi_pattern_table_t adc2_patt[SOC_ADC_PATT_LEN_MAX] = {0};
  463. if (adc & ADC_UNIT_1) {
  464. config.adc1_pattern_len = adc_test_num;
  465. config.adc1_pattern = adc1_patt;
  466. for (int i = 0; i < adc_test_num; i++) {
  467. adc1_patt[i].atten = TEST_ADC_ATTEN_DEFAULT;
  468. adc1_patt[i].channel = adc_list[i];
  469. adc_gpio_init(ADC_UNIT_1, adc_list[i]);
  470. }
  471. }
  472. if (adc & ADC_UNIT_2) {
  473. config.adc2_pattern_len = adc_test_num;
  474. config.adc2_pattern = adc2_patt;
  475. for (int i = 0; i < adc_test_num; i++) {
  476. adc2_patt[i].atten = TEST_ADC_ATTEN_DEFAULT;
  477. adc2_patt[i].channel = adc_list[i];
  478. adc_gpio_init(ADC_UNIT_2, adc_list[i]);
  479. }
  480. }
  481. if (adc == ADC_UNIT_1) {
  482. config.conv_mode = ADC_CONV_SINGLE_UNIT_1;
  483. config.format = ADC_DIGI_FORMAT_12BIT;
  484. } else if (adc == ADC_UNIT_2) {
  485. config.conv_mode = ADC_CONV_SINGLE_UNIT_2;
  486. config.format = ADC_DIGI_FORMAT_12BIT;
  487. } else if (adc == ADC_UNIT_BOTH) {
  488. config.conv_mode = ADC_CONV_BOTH_UNIT;
  489. config.format = ADC_DIGI_FORMAT_11BIT;
  490. } else if (adc == ADC_UNIT_ALTER) {
  491. config.conv_mode = ADC_CONV_ALTER_UNIT;
  492. config.format = ADC_DIGI_FORMAT_11BIT;
  493. }
  494. TEST_ESP_OK( adc_digi_controller_config(&config) );
  495. /* ADC-DMA linker init */
  496. if (que_adc == NULL) {
  497. que_adc = xQueueCreate(5, sizeof(adc_dma_event_t));
  498. } else {
  499. xQueueReset(que_adc);
  500. }
  501. uint32_t int_mask = SPI_IN_SUC_EOF_INT_ENA;
  502. uint32_t dma_addr = adc_dma_linker_init(adc, false);
  503. adc_dac_dma_isr_register(adc_dma_isr, NULL, int_mask);
  504. adc_dac_dma_linker_start(DMA_ONLY_ADC_INLINK, (void *)dma_addr, int_mask);
  505. ESP_LOGI(TAG, "adc IO fake tie middle, test ...");
  506. for (int i = 0; i < adc_test_num; i++) {
  507. adc_fake_tie_middle(adc, adc_list[i]);
  508. }
  509. return 0;
  510. }
  511. static void scope_output(int adc_num, int channel, int data)
  512. {
  513. /** can replace by uart log.*/
  514. #if SCOPE_OUTPUT_UART
  515. static int icnt = 0;
  516. if (icnt++ % 8 == 0) {
  517. esp_rom_printf("\n");
  518. }
  519. esp_rom_printf("[%d_%d_%04x] ", adc_num, channel, data);
  520. return;
  521. #endif
  522. #if SCOPE_DEBUG_TYPE == 0
  523. if (adc_num != 0) {
  524. return;
  525. }
  526. #elif SCOPE_DEBUG_TYPE == 1
  527. if (adc_num != 1) {
  528. return;
  529. }
  530. #endif
  531. int i;
  532. /* adc Read */
  533. for (i = 0; i < adc_test_num; i++) {
  534. if (adc_list[i] == channel && scope_temp[i] == 0) {
  535. scope_temp[i] = data;
  536. break;
  537. }
  538. }
  539. if (i == adc_test_num) {
  540. test_tp_print_to_scope(scope_temp, adc_test_num);
  541. vTaskDelay(SCOPE_DEBUG_FREQ_MS / portTICK_RATE_MS);
  542. for (int i = 0; i < adc_test_num; i++) {
  543. scope_temp[i] = 0;
  544. }
  545. }
  546. }
  547. /**
  548. * Manual test: Capture ADC-DMA data and display it on the serial oscilloscope. Used to observe the stability of the data.
  549. * Use step:
  550. * 1. Run this test from the unit test app.
  551. * 2. Use `ESP-Tuning Tool`(download from `www.espressif.com`) to capture.
  552. * 3. The readings of multiple channels will be displayed on the tool.
  553. */
  554. TEST_CASE("test_adc_digi_slope_debug", "[adc_dma][ignore]")
  555. {
  556. adc_dma_event_t evt;
  557. test_tp_scope_debug_init(0, -1, -1, SCOPE_UART_BUADRATE);
  558. adc_unit_t adc = ADC_CONV_BOTH_UNIT;
  559. test_adc_dig_scope_debug_unit(adc);
  560. while (1) {
  561. TEST_ESP_OK( adc_digi_start() );
  562. TEST_ASSERT_EQUAL( xQueueReceive(que_adc, &evt, portMAX_DELAY), pdTRUE );
  563. if (evt.int_msk & SPI_IN_SUC_EOF_INT_ST) {
  564. TEST_ESP_OK( adc_digi_stop() );
  565. adc_digi_reset();
  566. for (int cnt = 0; cnt < 2; cnt++) {
  567. esp_rom_printf("cnt%d\n", cnt);
  568. for (int i = 0; i < SAR_DMA_DATA_SIZE((adc > 2) ? 2 : 1, SAR_SIMPLE_NUM); i += 2) {
  569. uint8_t h = link_buf[cnt % 2][i + 1], l = link_buf[cnt % 2][i];
  570. uint16_t temp = (h << 8 | l);
  571. adc_digi_output_data_t *data = (adc_digi_output_data_t *)&temp;
  572. if (adc > ADC_UNIT_2) { //ADC_ENCODE_11BIT
  573. scope_output(data->type2.unit, data->type2.channel, data->type2.data);
  574. } else { //ADC_ENCODE_12BIT
  575. if (adc == ADC_UNIT_1) {
  576. scope_output(0, data->type1.channel, data->type1.data);
  577. } else if (adc == ADC_UNIT_2) {
  578. scope_output(1, data->type1.channel, data->type1.data);
  579. }
  580. }
  581. link_buf[cnt % 2][i] = 0;
  582. link_buf[cnt % 2][i + 1] = 0;
  583. }
  584. }
  585. }
  586. }
  587. }
  588. #endif // CONFIG_IDF_TARGET_ESP32S2