test_esp32s2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 dac 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/soc.h"
  27. #include "soc/spi_reg.h"
  28. #include "soc/adc_periph.h"
  29. #include "soc/dac_periph.h"
  30. #include "soc/spi_periph.h"
  31. #include "test/test_common_adc.h"
  32. #include "driver/dac.h"
  33. #include "soc/system_reg.h"
  34. #include "esp32s2/rom/lldesc.h"
  35. #include "test/test_adc_dac_dma.h"
  36. static const char *TAG = "test_adc";
  37. #define PLATFORM_SELECT (1) //0: pxp; 1: chip
  38. #if (PLATFORM_SELECT == 0) //PXP platform
  39. #include "soc/syscon_reg.h"
  40. #define SET_BREAK_POINT(flag) REG_WRITE(SYSCON_DATE_REG, flag)
  41. //PXP clk is slower.
  42. #define SYS_DELAY_TIME_MOM (1/40)
  43. #define RTC_SLOW_CLK_FLAG 1 // Slow clock is 32KHz.
  44. static void test_pxp_deinit_io(void)
  45. {
  46. for (int i = 0; i < 22; i++) {
  47. rtc_gpio_init(i);
  48. }
  49. }
  50. #else
  51. //PXP clk is slower.
  52. #define SET_BREAK_POINT(flag)
  53. #define SYS_DELAY_TIME_MOM (1)
  54. #define RTC_SLOW_CLK_FLAG 0 // Slow clock is 32KHz.
  55. #endif
  56. #define SAR_SIMPLE_NUM 512 // Set out number of enabled unit.
  57. typedef struct dma_msg {
  58. uint32_t int_msk;
  59. uint8_t *data;
  60. uint32_t data_len;
  61. } dac_dma_event_t;
  62. static QueueHandle_t que_dac = NULL;
  63. static uint8_t link_buf[2][SAR_SIMPLE_NUM*2] = {0};
  64. static lldesc_t dma1 = {0};
  65. static lldesc_t dma2 = {0};
  66. /*******************************************/
  67. /** DAC-DMA INIT CODE */
  68. /*******************************************/
  69. /**
  70. * DMA liner initialization and start.
  71. * @param is_loop
  72. * - true: The two dma linked lists are connected end to end, with no end mark (eof).
  73. * - false: The two dma linked lists are connected end to end, with end mark (eof).
  74. * @param int_mask DMA interrupt types.
  75. */
  76. uint32_t dac_dma_linker_init(bool is_alter, bool is_loop)
  77. {
  78. /* The DAC output is a sawtooth wave. */
  79. if (is_alter) {
  80. for(int i=0; i<SAR_SIMPLE_NUM*2; i++) {
  81. if(i%2){
  82. link_buf[0][i] = i%256;
  83. }else{
  84. link_buf[0][i] = 256-i%256;
  85. }
  86. if(i%2){
  87. link_buf[1][i] = i%256;
  88. }else{
  89. link_buf[1][i] = 256-i%256;
  90. }
  91. }
  92. } else {
  93. for(int i=0; i<SAR_SIMPLE_NUM; i++) {
  94. link_buf[0][i] = i%256;
  95. link_buf[1][i] = i%256;
  96. }
  97. }
  98. dma1 = (lldesc_t) {
  99. .size = (is_alter) ? SAR_SIMPLE_NUM*2 : SAR_SIMPLE_NUM,
  100. .length = (is_alter) ? SAR_SIMPLE_NUM*2 : SAR_SIMPLE_NUM,
  101. .eof = 0,
  102. .owner = 1,
  103. .buf = &link_buf[0][0],
  104. .qe.stqe_next = &dma2,
  105. };
  106. dma2 = (lldesc_t) {
  107. .size = (is_alter) ? SAR_SIMPLE_NUM*2 : SAR_SIMPLE_NUM,
  108. .length = (is_alter) ? SAR_SIMPLE_NUM*2 : SAR_SIMPLE_NUM,
  109. .owner = 1,
  110. .buf = &link_buf[1][0],
  111. };
  112. if (is_loop) {
  113. dma2.eof = 0;
  114. dma2.qe.stqe_next = &dma1;
  115. } else {
  116. dma2.eof = 1;
  117. dma2.qe.stqe_next = NULL;
  118. }
  119. return (uint32_t)&dma1;
  120. }
  121. /** ADC-DMA ISR handler. */
  122. static IRAM_ATTR void dac_dma_isr(void * arg)
  123. {
  124. uint32_t int_st = REG_READ(SPI_DMA_INT_ST_REG(3));
  125. int task_awoken = pdFALSE;
  126. dac_dma_event_t adc_evt;
  127. adc_evt.int_msk = int_st;
  128. REG_WRITE(SPI_DMA_INT_CLR_REG(3), int_st);
  129. xQueueSendFromISR(que_dac, &adc_evt, &task_awoken);
  130. ESP_EARLY_LOGV(TAG, "int msk%x, raw%x", int_st, REG_READ(SPI_DMA_INT_RAW_REG(3)));
  131. if (task_awoken == pdTRUE) {
  132. portYIELD_FROM_ISR();
  133. }
  134. }
  135. /**
  136. * Testcase: Check the interrupt types of DAC-DMA.
  137. */
  138. void test_dac_dig_dma_intr_check(dac_digi_convert_mode_t mode)
  139. {
  140. ESP_LOGI(TAG, " >> %s - dac mode %d<< ", __func__, mode);
  141. dac_dma_event_t evt;
  142. dac_digi_init();
  143. const dac_digi_config_t cfg = {
  144. .mode = mode,
  145. .interval = 100,
  146. .dig_clk.use_apll = false, // APB clk
  147. .dig_clk.div_num = 79,
  148. .dig_clk.div_b = 1,
  149. .dig_clk.div_a = 0,
  150. };
  151. dac_digi_controller_config(&cfg);
  152. dac_output_enable(DAC_CHANNEL_1);
  153. dac_output_enable(DAC_CHANNEL_2);
  154. /* DAC-DMA linker init */
  155. if (que_dac == NULL) {
  156. que_dac = xQueueCreate(5, sizeof(dac_dma_event_t));
  157. } else {
  158. xQueueReset(que_dac);
  159. }
  160. uint32_t int_mask = SPI_OUT_DONE_INT_ENA | SPI_OUT_EOF_INT_ENA | SPI_OUT_TOTAL_EOF_INT_ENA;
  161. uint32_t dma_addr = dac_dma_linker_init(mode, false);
  162. adc_dac_dma_isr_register(dac_dma_isr, NULL, int_mask);
  163. adc_dac_dma_linker_start(DMA_ONLY_DAC_OUTLINK, (void *)dma_addr, int_mask);
  164. /* ADC-DMA start output */
  165. dac_digi_start();
  166. /* Check interrupt type */
  167. while (int_mask) {
  168. TEST_ASSERT_EQUAL( xQueueReceive(que_dac, &evt, 2000 / portTICK_RATE_MS), pdTRUE );
  169. ESP_LOGI(TAG, "DAC-DMA intr type 0x%x", evt.int_msk);
  170. if (evt.int_msk & int_mask) {
  171. int_mask &= (~evt.int_msk);
  172. }
  173. }
  174. ESP_LOGI(TAG, "DAC-DMA intr test over");
  175. adc_dac_dma_linker_deinit();
  176. adc_dac_dma_isr_deregister(dac_dma_isr, NULL);
  177. TEST_ESP_OK( dac_digi_deinit() );
  178. }
  179. TEST_CASE("DAC-DMA interrupt test", "[dac]")
  180. {
  181. test_dac_dig_dma_intr_check(DAC_CONV_NORMAL);
  182. test_dac_dig_dma_intr_check(DAC_CONV_ALTER);
  183. }
  184. /*******************************************/
  185. /** SPI DMA INIT CODE */
  186. /*******************************************/
  187. #include "sys/queue.h"
  188. static bool adc_dac_dma_isr_flag = false;
  189. /*---------------------------------------------------------------
  190. INTERRUPT HANDLER
  191. ---------------------------------------------------------------*/
  192. typedef struct adc_dac_dma_isr_handler_ {
  193. uint32_t mask;
  194. intr_handler_t handler;
  195. void* handler_arg;
  196. SLIST_ENTRY(adc_dac_dma_isr_handler_) next;
  197. } adc_dac_dma_isr_handler_t;
  198. static SLIST_HEAD(adc_dac_dma_isr_handler_list_, adc_dac_dma_isr_handler_) s_adc_dac_dma_isr_handler_list =
  199. SLIST_HEAD_INITIALIZER(s_adc_dac_dma_isr_handler_list);
  200. portMUX_TYPE s_isr_handler_list_lock = portMUX_INITIALIZER_UNLOCKED;
  201. static intr_handle_t s_adc_dac_dma_isr_handle;
  202. static IRAM_ATTR void adc_dac_dma_isr_default(void* arg)
  203. {
  204. uint32_t status = REG_READ(SPI_DMA_INT_ST_REG(3));
  205. adc_dac_dma_isr_handler_t* it;
  206. portENTER_CRITICAL_ISR(&s_isr_handler_list_lock);
  207. SLIST_FOREACH(it, &s_adc_dac_dma_isr_handler_list, next) {
  208. if (it->mask & status) {
  209. portEXIT_CRITICAL_ISR(&s_isr_handler_list_lock);
  210. (*it->handler)(it->handler_arg);
  211. portENTER_CRITICAL_ISR(&s_isr_handler_list_lock);
  212. }
  213. }
  214. portEXIT_CRITICAL_ISR(&s_isr_handler_list_lock);
  215. REG_WRITE(SPI_DMA_INT_CLR_REG(3), status);
  216. }
  217. static esp_err_t adc_dac_dma_isr_ensure_installed(void)
  218. {
  219. esp_err_t err = ESP_OK;
  220. portENTER_CRITICAL(&s_isr_handler_list_lock);
  221. if (s_adc_dac_dma_isr_handle) {
  222. goto out;
  223. }
  224. REG_WRITE(SPI_DMA_INT_ENA_REG(3), 0);
  225. REG_WRITE(SPI_DMA_INT_CLR_REG(3), UINT32_MAX);
  226. err = esp_intr_alloc(ETS_SPI3_DMA_INTR_SOURCE, 0, &adc_dac_dma_isr_default, NULL, &s_adc_dac_dma_isr_handle);
  227. if (err != ESP_OK) {
  228. goto out;
  229. }
  230. out:
  231. portEXIT_CRITICAL(&s_isr_handler_list_lock);
  232. return err;
  233. }
  234. esp_err_t adc_dac_dma_isr_register(intr_handler_t handler, void* handler_arg, uint32_t intr_mask)
  235. {
  236. esp_err_t err = adc_dac_dma_isr_ensure_installed();
  237. if (err != ESP_OK) {
  238. return err;
  239. }
  240. adc_dac_dma_isr_handler_t* item = malloc(sizeof(*item));
  241. if (item == NULL) {
  242. return ESP_ERR_NO_MEM;
  243. }
  244. item->handler = handler;
  245. item->handler_arg = handler_arg;
  246. item->mask = intr_mask;
  247. portENTER_CRITICAL(&s_isr_handler_list_lock);
  248. SLIST_INSERT_HEAD(&s_adc_dac_dma_isr_handler_list, item, next);
  249. portEXIT_CRITICAL(&s_isr_handler_list_lock);
  250. return ESP_OK;
  251. }
  252. esp_err_t adc_dac_dma_isr_deregister(intr_handler_t handler, void* handler_arg)
  253. {
  254. adc_dac_dma_isr_handler_t* it;
  255. adc_dac_dma_isr_handler_t* prev = NULL;
  256. bool found = false;
  257. portENTER_CRITICAL(&s_isr_handler_list_lock);
  258. SLIST_FOREACH(it, &s_adc_dac_dma_isr_handler_list, next) {
  259. if (it->handler == handler && it->handler_arg == handler_arg) {
  260. if (it == SLIST_FIRST(&s_adc_dac_dma_isr_handler_list)) {
  261. SLIST_REMOVE_HEAD(&s_adc_dac_dma_isr_handler_list, next);
  262. } else {
  263. SLIST_REMOVE_AFTER(prev, next);
  264. }
  265. found = true;
  266. free(it);
  267. break;
  268. }
  269. prev = it;
  270. }
  271. portEXIT_CRITICAL(&s_isr_handler_list_lock);
  272. return found ? ESP_OK : ESP_ERR_INVALID_STATE;
  273. }
  274. void adc_dac_dma_linker_start(spi_dma_link_type_t type, void *dma_addr, uint32_t int_msk)
  275. {
  276. REG_SET_BIT(DPORT_PERIP_CLK_EN_REG, DPORT_APB_SARADC_CLK_EN_M);
  277. REG_SET_BIT(DPORT_PERIP_CLK_EN_REG, DPORT_SPI3_DMA_CLK_EN_M);
  278. REG_SET_BIT(DPORT_PERIP_CLK_EN_REG, DPORT_SPI3_CLK_EN);
  279. REG_CLR_BIT(DPORT_PERIP_RST_EN_REG, DPORT_SPI3_DMA_RST_M);
  280. REG_CLR_BIT(DPORT_PERIP_RST_EN_REG, DPORT_SPI3_RST_M);
  281. REG_WRITE(SPI_DMA_INT_CLR_REG(3), 0xFFFFFFFF);
  282. REG_WRITE(SPI_DMA_INT_ENA_REG(3), int_msk | REG_READ(SPI_DMA_INT_ENA_REG(3)));
  283. if (type & DMA_ONLY_ADC_INLINK) {
  284. REG_SET_BIT(SPI_DMA_IN_LINK_REG(3), SPI_INLINK_STOP);
  285. REG_CLR_BIT(SPI_DMA_IN_LINK_REG(3), SPI_INLINK_START);
  286. SET_PERI_REG_BITS(SPI_DMA_IN_LINK_REG(3), SPI_INLINK_ADDR, (uint32_t)dma_addr, 0);
  287. REG_SET_BIT(SPI_DMA_CONF_REG(3), SPI_IN_RST);
  288. REG_CLR_BIT(SPI_DMA_CONF_REG(3), SPI_IN_RST);
  289. REG_CLR_BIT(SPI_DMA_IN_LINK_REG(3), SPI_INLINK_STOP);
  290. REG_SET_BIT(SPI_DMA_IN_LINK_REG(3), SPI_INLINK_START);
  291. }
  292. if (type & DMA_ONLY_DAC_OUTLINK) {
  293. REG_SET_BIT(SPI_DMA_OUT_LINK_REG(3), SPI_OUTLINK_STOP);
  294. REG_CLR_BIT(SPI_DMA_OUT_LINK_REG(3), SPI_OUTLINK_START);
  295. SET_PERI_REG_BITS(SPI_DMA_OUT_LINK_REG(3), SPI_OUTLINK_ADDR, (uint32_t)dma_addr, 0);
  296. REG_SET_BIT(SPI_DMA_CONF_REG(3), SPI_OUT_RST);
  297. REG_CLR_BIT(SPI_DMA_CONF_REG(3), SPI_OUT_RST);
  298. REG_CLR_BIT(SPI_DMA_OUT_LINK_REG(3), SPI_OUTLINK_STOP);
  299. REG_SET_BIT(SPI_DMA_OUT_LINK_REG(3), SPI_OUTLINK_START);
  300. }
  301. }
  302. void adc_dac_dma_linker_stop(spi_dma_link_type_t type)
  303. {
  304. if (type & DMA_ONLY_ADC_INLINK) {
  305. REG_SET_BIT(SPI_DMA_IN_LINK_REG(3), SPI_INLINK_STOP);
  306. REG_CLR_BIT(SPI_DMA_IN_LINK_REG(3), SPI_INLINK_START);
  307. }
  308. if (type & DMA_ONLY_DAC_OUTLINK) {
  309. REG_SET_BIT(SPI_DMA_OUT_LINK_REG(3), SPI_OUTLINK_STOP);
  310. REG_CLR_BIT(SPI_DMA_OUT_LINK_REG(3), SPI_OUTLINK_START);
  311. }
  312. }
  313. void adc_dac_dma_linker_deinit(void)
  314. {
  315. adc_dac_dma_linker_stop(DMA_BOTH_ADC_DAC);
  316. REG_WRITE(SPI_DMA_INT_CLR_REG(3), 0xFFFFFFFF);
  317. REG_WRITE(SPI_DMA_INT_ENA_REG(3), 0);
  318. adc_dac_dma_isr_flag = false;
  319. }
  320. /*******************************************/
  321. /** SPI DMA INIT CODE END */
  322. /*******************************************/
  323. #endif // CONFIG_IDF_TARGET_ESP32S2