test_esp32s2.c 22 KB

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