test_esp32s2.c 12 KB

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