My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i2s.cpp 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef ARDUINO_ARCH_ESP32
  23. #include "../../inc/MarlinConfigPre.h"
  24. #include "i2s.h"
  25. #include "../shared/Marduino.h"
  26. #include <driver/periph_ctrl.h>
  27. #include <rom/lldesc.h>
  28. #include <soc/i2s_struct.h>
  29. #include <freertos/queue.h>
  30. #include "../../module/stepper.h"
  31. #define DMA_BUF_COUNT 8 // number of DMA buffers to store data
  32. #define DMA_BUF_LEN 4092 // maximum size in bytes
  33. #define I2S_SAMPLE_SIZE 4 // 4 bytes, 32 bits per sample
  34. #define DMA_SAMPLE_COUNT DMA_BUF_LEN / I2S_SAMPLE_SIZE // number of samples per buffer
  35. typedef enum {
  36. I2S_NUM_0 = 0x0, /*!< I2S 0*/
  37. I2S_NUM_1 = 0x1, /*!< I2S 1*/
  38. I2S_NUM_MAX,
  39. } i2s_port_t;
  40. typedef struct {
  41. uint32_t **buffers;
  42. uint32_t *current;
  43. uint32_t rw_pos;
  44. lldesc_t **desc;
  45. xQueueHandle queue;
  46. } i2s_dma_t;
  47. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED};
  48. static i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0, &I2S1};
  49. static i2s_dma_t dma;
  50. // output value
  51. uint32_t i2s_port_data = 0;
  52. #define I2S_ENTER_CRITICAL() portENTER_CRITICAL(&i2s_spinlock[i2s_num])
  53. #define I2S_EXIT_CRITICAL() portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
  54. static inline void gpio_matrix_out_check(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv) {
  55. //if pin = -1, do not need to configure
  56. if (gpio != -1) {
  57. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  58. gpio_set_direction((gpio_num_t)gpio, (gpio_mode_t)GPIO_MODE_DEF_OUTPUT);
  59. gpio_matrix_out(gpio, signal_idx, out_inv, oen_inv);
  60. }
  61. }
  62. static esp_err_t i2s_reset_fifo(i2s_port_t i2s_num) {
  63. I2S_ENTER_CRITICAL();
  64. I2S[i2s_num]->conf.rx_fifo_reset = 1;
  65. I2S[i2s_num]->conf.rx_fifo_reset = 0;
  66. I2S[i2s_num]->conf.tx_fifo_reset = 1;
  67. I2S[i2s_num]->conf.tx_fifo_reset = 0;
  68. I2S_EXIT_CRITICAL();
  69. return ESP_OK;
  70. }
  71. esp_err_t i2s_start(i2s_port_t i2s_num) {
  72. //start DMA link
  73. I2S_ENTER_CRITICAL();
  74. i2s_reset_fifo(i2s_num);
  75. //reset dma
  76. I2S[i2s_num]->lc_conf.in_rst = 1;
  77. I2S[i2s_num]->lc_conf.in_rst = 0;
  78. I2S[i2s_num]->lc_conf.out_rst = 1;
  79. I2S[i2s_num]->lc_conf.out_rst = 0;
  80. I2S[i2s_num]->conf.tx_reset = 1;
  81. I2S[i2s_num]->conf.tx_reset = 0;
  82. I2S[i2s_num]->conf.rx_reset = 1;
  83. I2S[i2s_num]->conf.rx_reset = 0;
  84. I2S[i2s_num]->int_clr.val = 0xFFFFFFFF;
  85. I2S[i2s_num]->out_link.start = 1;
  86. I2S[i2s_num]->conf.tx_start = 1;
  87. I2S_EXIT_CRITICAL();
  88. return ESP_OK;
  89. }
  90. esp_err_t i2s_stop(i2s_port_t i2s_num) {
  91. I2S_ENTER_CRITICAL();
  92. I2S[i2s_num]->out_link.stop = 1;
  93. I2S[i2s_num]->conf.tx_start = 0;
  94. I2S[i2s_num]->int_clr.val = I2S[i2s_num]->int_st.val; //clear pending interrupt
  95. I2S_EXIT_CRITICAL();
  96. return ESP_OK;
  97. }
  98. static void IRAM_ATTR i2s_intr_handler_default(void *arg) {
  99. int dummy;
  100. lldesc_t *finish_desc;
  101. portBASE_TYPE high_priority_task_awoken = pdFALSE;
  102. if (I2S0.int_st.out_eof) {
  103. // Get the descriptor of the last item in the linkedlist
  104. finish_desc = (lldesc_t*) I2S0.out_eof_des_addr;
  105. // If the queue is full it's because we have an underflow,
  106. // more than buf_count isr without new data, remove the front buffer
  107. if (xQueueIsQueueFullFromISR(dma.queue))
  108. xQueueReceiveFromISR(dma.queue, &dummy, &high_priority_task_awoken);
  109. xQueueSendFromISR(dma.queue, (void *)(&finish_desc->buf), &high_priority_task_awoken);
  110. }
  111. if (high_priority_task_awoken == pdTRUE) portYIELD_FROM_ISR();
  112. // clear interrupt
  113. I2S0.int_clr.val = I2S0.int_st.val; //clear pending interrupt
  114. }
  115. void stepperTask(void* parameter) {
  116. uint32_t remaining = 0;
  117. while (1) {
  118. xQueueReceive(dma.queue, &dma.current, portMAX_DELAY);
  119. dma.rw_pos = 0;
  120. while (dma.rw_pos < DMA_SAMPLE_COUNT) {
  121. // Fill with the port data post pulse_phase until the next step
  122. if (remaining) {
  123. i2s_push_sample();
  124. remaining--;
  125. }
  126. else {
  127. Stepper::pulse_phase_isr();
  128. remaining = Stepper::block_phase_isr();
  129. }
  130. }
  131. }
  132. }
  133. int i2s_init() {
  134. periph_module_enable(PERIPH_I2S0_MODULE);
  135. /**
  136. * Each i2s transfer will take
  137. * fpll = PLL_D2_CLK -- clka_en = 0
  138. *
  139. * fi2s = fpll / N + b/a -- N = clkm_div_num
  140. * fi2s = 160MHz / 2
  141. * fi2s = 80MHz
  142. *
  143. * fbclk = fi2s / M -- M = tx_bck_div_num
  144. * fbclk = 80MHz / 2
  145. * fbclk = 40MHz
  146. *
  147. * fwclk = fbclk / 32
  148. *
  149. * for fwclk = 250kHz (4µS pulse time)
  150. * N = 10
  151. * M = 20
  152. */
  153. // Allocate the array of pointers to the buffers
  154. dma.buffers = (uint32_t **)malloc(sizeof(uint32_t*) * DMA_BUF_COUNT);
  155. if (dma.buffers == nullptr) return -1;
  156. // Allocate each buffer that can be used by the DMA controller
  157. for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) {
  158. dma.buffers[buf_idx] = (uint32_t*) heap_caps_calloc(1, DMA_BUF_LEN, MALLOC_CAP_DMA);
  159. if (dma.buffers[buf_idx] == nullptr) return -1;
  160. }
  161. // Allocate the array of DMA descriptors
  162. dma.desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * DMA_BUF_COUNT);
  163. if (dma.desc == nullptr) return -1;
  164. // Allocate each DMA descriptor that will be used by the DMA controller
  165. for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) {
  166. dma.desc[buf_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
  167. if (dma.desc[buf_idx] == nullptr) return -1;
  168. }
  169. // Initialize
  170. for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) {
  171. dma.desc[buf_idx]->owner = 1;
  172. dma.desc[buf_idx]->eof = 1; // set to 1 will trigger the interrupt
  173. dma.desc[buf_idx]->sosf = 0;
  174. dma.desc[buf_idx]->length = DMA_BUF_LEN;
  175. dma.desc[buf_idx]->size = DMA_BUF_LEN;
  176. dma.desc[buf_idx]->buf = (uint8_t *) dma.buffers[buf_idx];
  177. dma.desc[buf_idx]->offset = 0;
  178. dma.desc[buf_idx]->empty = (uint32_t)((buf_idx < (DMA_BUF_COUNT - 1)) ? (dma.desc[buf_idx + 1]) : dma.desc[0]);
  179. }
  180. dma.queue = xQueueCreate(DMA_BUF_COUNT, sizeof(uint32_t *));
  181. // Set the first DMA descriptor
  182. I2S0.out_link.addr = (uint32_t)dma.desc[0];
  183. // stop i2s
  184. i2s_stop(I2S_NUM_0);
  185. // configure I2S data port interface.
  186. i2s_reset_fifo(I2S_NUM_0);
  187. //reset i2s
  188. I2S0.conf.tx_reset = 1;
  189. I2S0.conf.tx_reset = 0;
  190. I2S0.conf.rx_reset = 1;
  191. I2S0.conf.rx_reset = 0;
  192. //reset dma
  193. I2S0.lc_conf.in_rst = 1;
  194. I2S0.lc_conf.in_rst = 0;
  195. I2S0.lc_conf.out_rst = 1;
  196. I2S0.lc_conf.out_rst = 0;
  197. //Enable and configure DMA
  198. I2S0.lc_conf.check_owner = 0;
  199. I2S0.lc_conf.out_loop_test = 0;
  200. I2S0.lc_conf.out_auto_wrback = 0;
  201. I2S0.lc_conf.out_data_burst_en = 0;
  202. I2S0.lc_conf.outdscr_burst_en = 0;
  203. I2S0.lc_conf.out_no_restart_clr = 0;
  204. I2S0.lc_conf.indscr_burst_en = 0;
  205. I2S0.lc_conf.out_eof_mode = 1;
  206. I2S0.conf2.lcd_en = 0;
  207. I2S0.conf2.camera_en = 0;
  208. I2S0.pdm_conf.pcm2pdm_conv_en = 0;
  209. I2S0.pdm_conf.pdm2pcm_conv_en = 0;
  210. I2S0.fifo_conf.dscr_en = 0;
  211. I2S0.conf_chan.tx_chan_mod = (
  212. #if ENABLED(I2S_STEPPER_SPLIT_STREAM)
  213. 4
  214. #else
  215. 0
  216. #endif
  217. );
  218. I2S0.fifo_conf.tx_fifo_mod = 0;
  219. I2S0.conf.tx_mono = 0;
  220. I2S0.conf_chan.rx_chan_mod = 0;
  221. I2S0.fifo_conf.rx_fifo_mod = 0;
  222. I2S0.conf.rx_mono = 0;
  223. I2S0.fifo_conf.dscr_en = 1; //connect dma to fifo
  224. I2S0.conf.tx_start = 0;
  225. I2S0.conf.rx_start = 0;
  226. I2S0.conf.tx_msb_right = 1;
  227. I2S0.conf.tx_right_first = 1;
  228. I2S0.conf.tx_slave_mod = 0; // Master
  229. I2S0.fifo_conf.tx_fifo_mod_force_en = 1;
  230. I2S0.pdm_conf.rx_pdm_en = 0;
  231. I2S0.pdm_conf.tx_pdm_en = 0;
  232. I2S0.conf.tx_short_sync = 0;
  233. I2S0.conf.rx_short_sync = 0;
  234. I2S0.conf.tx_msb_shift = 0;
  235. I2S0.conf.rx_msb_shift = 0;
  236. // set clock
  237. I2S0.clkm_conf.clka_en = 0; // Use PLL/2 as reference
  238. I2S0.clkm_conf.clkm_div_num = 10; // minimum value of 2, reset value of 4, max 256
  239. I2S0.clkm_conf.clkm_div_a = 0; // 0 at reset, what about divide by 0? (not an issue)
  240. I2S0.clkm_conf.clkm_div_b = 0; // 0 at reset
  241. // fbck = fi2s / tx_bck_div_num
  242. I2S0.sample_rate_conf.tx_bck_div_num = 2; // minimum value of 2 defaults to 6
  243. // Enable TX interrupts
  244. I2S0.int_ena.out_eof = 1;
  245. I2S0.int_ena.out_dscr_err = 0;
  246. I2S0.int_ena.out_total_eof = 0;
  247. I2S0.int_ena.out_done = 0;
  248. // Allocate and Enable the I2S interrupt
  249. intr_handle_t i2s_isr_handle;
  250. esp_intr_alloc(ETS_I2S0_INTR_SOURCE, 0, i2s_intr_handler_default, nullptr, &i2s_isr_handle);
  251. esp_intr_enable(i2s_isr_handle);
  252. // Create the task that will feed the buffer
  253. xTaskCreatePinnedToCore(stepperTask, "StepperTask", 10000, nullptr, 1, nullptr, CONFIG_ARDUINO_RUNNING_CORE); // run I2S stepper task on same core as rest of Marlin
  254. // Route the i2s pins to the appropriate GPIO
  255. gpio_matrix_out_check(I2S_DATA, I2S0O_DATA_OUT23_IDX, 0, 0);
  256. gpio_matrix_out_check(I2S_BCK, I2S0O_BCK_OUT_IDX, 0, 0);
  257. gpio_matrix_out_check(I2S_WS, I2S0O_WS_OUT_IDX, 0, 0);
  258. // Start the I2S peripheral
  259. return i2s_start(I2S_NUM_0);
  260. }
  261. void i2s_write(uint8_t pin, uint8_t val) {
  262. #if ENABLED(I2S_STEPPER_SPLIT_STREAM)
  263. if (pin >= 16) {
  264. SET_BIT_TO(I2S0.conf_single_data, pin, val);
  265. return;
  266. }
  267. #endif
  268. SET_BIT_TO(i2s_port_data, pin, val);
  269. }
  270. uint8_t i2s_state(uint8_t pin) {
  271. #if ENABLED(I2S_STEPPER_SPLIT_STREAM)
  272. if (pin >= 16) return TEST(I2S0.conf_single_data, pin);
  273. #endif
  274. return TEST(i2s_port_data, pin);
  275. }
  276. void i2s_push_sample() {
  277. dma.current[dma.rw_pos++] = i2s_port_data;
  278. }
  279. #endif // ARDUINO_ARCH_ESP32