test_esp32s2.c 11 KB

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