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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #if DISABLED(USE_ESP32_EXIO)
  25. #include "i2s.h"
  26. #include "../shared/Marduino.h"
  27. #include <driver/periph_ctrl.h>
  28. #include <rom/lldesc.h>
  29. #include <soc/i2s_struct.h>
  30. #include <freertos/queue.h>
  31. #include "../../module/stepper.h"
  32. #define DMA_BUF_COUNT 8 // number of DMA buffers to store data
  33. #define DMA_BUF_LEN 4092 // maximum size in bytes
  34. #define I2S_SAMPLE_SIZE 4 // 4 bytes, 32 bits per sample
  35. #define DMA_SAMPLE_COUNT DMA_BUF_LEN / I2S_SAMPLE_SIZE // number of samples per buffer
  36. typedef enum {
  37. I2S_NUM_0 = 0x0, /*!< I2S 0*/
  38. I2S_NUM_1 = 0x1, /*!< I2S 1*/
  39. I2S_NUM_MAX,
  40. } i2s_port_t;
  41. typedef struct {
  42. uint32_t **buffers;
  43. uint32_t *current;
  44. uint32_t rw_pos;
  45. lldesc_t **desc;
  46. xQueueHandle queue;
  47. } i2s_dma_t;
  48. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED};
  49. static i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0, &I2S1};
  50. static i2s_dma_t dma;
  51. // output value
  52. uint32_t i2s_port_data = 0;
  53. #define I2S_ENTER_CRITICAL() portENTER_CRITICAL(&i2s_spinlock[i2s_num])
  54. #define I2S_EXIT_CRITICAL() portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
  55. static inline void gpio_matrix_out_check(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv) {
  56. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  57. gpio_set_direction((gpio_num_t)gpio, (gpio_mode_t)GPIO_MODE_DEF_OUTPUT);
  58. gpio_matrix_out(gpio, signal_idx, out_inv, oen_inv);
  59. }
  60. static esp_err_t i2s_reset_fifo(i2s_port_t i2s_num) {
  61. I2S_ENTER_CRITICAL();
  62. I2S[i2s_num]->conf.rx_fifo_reset = 1;
  63. I2S[i2s_num]->conf.rx_fifo_reset = 0;
  64. I2S[i2s_num]->conf.tx_fifo_reset = 1;
  65. I2S[i2s_num]->conf.tx_fifo_reset = 0;
  66. I2S_EXIT_CRITICAL();
  67. return ESP_OK;
  68. }
  69. esp_err_t i2s_start(i2s_port_t i2s_num) {
  70. //start DMA link
  71. I2S_ENTER_CRITICAL();
  72. i2s_reset_fifo(i2s_num);
  73. //reset dma
  74. I2S[i2s_num]->lc_conf.in_rst = 1;
  75. I2S[i2s_num]->lc_conf.in_rst = 0;
  76. I2S[i2s_num]->lc_conf.out_rst = 1;
  77. I2S[i2s_num]->lc_conf.out_rst = 0;
  78. I2S[i2s_num]->conf.tx_reset = 1;
  79. I2S[i2s_num]->conf.tx_reset = 0;
  80. I2S[i2s_num]->conf.rx_reset = 1;
  81. I2S[i2s_num]->conf.rx_reset = 0;
  82. I2S[i2s_num]->int_clr.val = 0xFFFFFFFF;
  83. I2S[i2s_num]->out_link.start = 1;
  84. I2S[i2s_num]->conf.tx_start = 1;
  85. I2S_EXIT_CRITICAL();
  86. return ESP_OK;
  87. }
  88. esp_err_t i2s_stop(i2s_port_t i2s_num) {
  89. I2S_ENTER_CRITICAL();
  90. I2S[i2s_num]->out_link.stop = 1;
  91. I2S[i2s_num]->conf.tx_start = 0;
  92. I2S[i2s_num]->int_clr.val = I2S[i2s_num]->int_st.val; //clear pending interrupt
  93. I2S_EXIT_CRITICAL();
  94. return ESP_OK;
  95. }
  96. static void IRAM_ATTR i2s_intr_handler_default(void *arg) {
  97. int dummy;
  98. lldesc_t *finish_desc;
  99. portBASE_TYPE high_priority_task_awoken = pdFALSE;
  100. if (I2S0.int_st.out_eof) {
  101. // Get the descriptor of the last item in the linkedlist
  102. finish_desc = (lldesc_t*) I2S0.out_eof_des_addr;
  103. // If the queue is full it's because we have an underflow,
  104. // more than buf_count isr without new data, remove the front buffer
  105. if (xQueueIsQueueFullFromISR(dma.queue))
  106. xQueueReceiveFromISR(dma.queue, &dummy, &high_priority_task_awoken);
  107. xQueueSendFromISR(dma.queue, (void *)(&finish_desc->buf), &high_priority_task_awoken);
  108. }
  109. if (high_priority_task_awoken == pdTRUE) portYIELD_FROM_ISR();
  110. // clear interrupt
  111. I2S0.int_clr.val = I2S0.int_st.val; //clear pending interrupt
  112. }
  113. void stepperTask(void *parameter) {
  114. uint32_t nextMainISR = 0;
  115. #if ENABLED(LIN_ADVANCE)
  116. uint32_t nextAdvanceISR = Stepper::LA_ADV_NEVER;
  117. #endif
  118. for (;;) {
  119. xQueueReceive(dma.queue, &dma.current, portMAX_DELAY);
  120. dma.rw_pos = 0;
  121. while (dma.rw_pos < DMA_SAMPLE_COUNT) {
  122. // Fill with the port data post pulse_phase until the next step
  123. if (nextMainISR && TERN1(LIN_ADVANCE, nextAdvanceISR))
  124. i2s_push_sample();
  125. // i2s_push_sample() is also called from Stepper::pulse_phase_isr() and Stepper::advance_isr()
  126. // in a rare case where both are called, we need to double decrement the counters
  127. const uint8_t push_count = 1 + (!nextMainISR && TERN0(LIN_ADVANCE, !nextAdvanceISR));
  128. #if ENABLED(LIN_ADVANCE)
  129. if (!nextAdvanceISR) {
  130. Stepper::advance_isr();
  131. nextAdvanceISR = Stepper::la_interval;
  132. }
  133. else if (nextAdvanceISR == Stepper::LA_ADV_NEVER)
  134. nextAdvanceISR = Stepper::la_interval;
  135. #endif
  136. if (!nextMainISR) {
  137. Stepper::pulse_phase_isr();
  138. nextMainISR = Stepper::block_phase_isr();
  139. }
  140. nextMainISR -= push_count;
  141. TERN_(LIN_ADVANCE, nextAdvanceISR -= push_count);
  142. }
  143. }
  144. }
  145. int i2s_init() {
  146. periph_module_enable(PERIPH_I2S0_MODULE);
  147. /**
  148. * Each i2s transfer will take
  149. * fpll = PLL_D2_CLK -- clka_en = 0
  150. *
  151. * fi2s = fpll / N + b/a -- N = clkm_div_num
  152. * fi2s = 160MHz / 2
  153. * fi2s = 80MHz
  154. *
  155. * fbclk = fi2s / M -- M = tx_bck_div_num
  156. * fbclk = 80MHz / 2
  157. * fbclk = 40MHz
  158. *
  159. * fwclk = fbclk / 32
  160. *
  161. * for fwclk = 250kHz (4µS pulse time)
  162. * N = 10
  163. * M = 20
  164. */
  165. // Allocate the array of pointers to the buffers
  166. dma.buffers = (uint32_t **)malloc(sizeof(uint32_t*) * DMA_BUF_COUNT);
  167. if (!dma.buffers) return -1;
  168. // Allocate each buffer that can be used by the DMA controller
  169. for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) {
  170. dma.buffers[buf_idx] = (uint32_t*) heap_caps_calloc(1, DMA_BUF_LEN, MALLOC_CAP_DMA);
  171. if (dma.buffers[buf_idx] == nullptr) return -1;
  172. }
  173. // Allocate the array of DMA descriptors
  174. dma.desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * DMA_BUF_COUNT);
  175. if (!dma.desc) return -1;
  176. // Allocate each DMA descriptor that will be used by the DMA controller
  177. for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) {
  178. dma.desc[buf_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
  179. if (dma.desc[buf_idx] == nullptr) return -1;
  180. }
  181. // Initialize
  182. for (int buf_idx = 0; buf_idx < DMA_BUF_COUNT; buf_idx++) {
  183. dma.desc[buf_idx]->owner = 1;
  184. dma.desc[buf_idx]->eof = 1; // set to 1 will trigger the interrupt
  185. dma.desc[buf_idx]->sosf = 0;
  186. dma.desc[buf_idx]->length = DMA_BUF_LEN;
  187. dma.desc[buf_idx]->size = DMA_BUF_LEN;
  188. dma.desc[buf_idx]->buf = (uint8_t *) dma.buffers[buf_idx];
  189. dma.desc[buf_idx]->offset = 0;
  190. dma.desc[buf_idx]->empty = (uint32_t)((buf_idx < (DMA_BUF_COUNT - 1)) ? (dma.desc[buf_idx + 1]) : dma.desc[0]);
  191. }
  192. dma.queue = xQueueCreate(DMA_BUF_COUNT, sizeof(uint32_t *));
  193. // Set the first DMA descriptor
  194. I2S0.out_link.addr = (uint32_t)dma.desc[0];
  195. // stop i2s
  196. i2s_stop(I2S_NUM_0);
  197. // configure I2S data port interface.
  198. i2s_reset_fifo(I2S_NUM_0);
  199. //reset i2s
  200. I2S0.conf.tx_reset = 1;
  201. I2S0.conf.tx_reset = 0;
  202. I2S0.conf.rx_reset = 1;
  203. I2S0.conf.rx_reset = 0;
  204. //reset dma
  205. I2S0.lc_conf.in_rst = 1;
  206. I2S0.lc_conf.in_rst = 0;
  207. I2S0.lc_conf.out_rst = 1;
  208. I2S0.lc_conf.out_rst = 0;
  209. //Enable and configure DMA
  210. I2S0.lc_conf.check_owner = 0;
  211. I2S0.lc_conf.out_loop_test = 0;
  212. I2S0.lc_conf.out_auto_wrback = 0;
  213. I2S0.lc_conf.out_data_burst_en = 0;
  214. I2S0.lc_conf.outdscr_burst_en = 0;
  215. I2S0.lc_conf.out_no_restart_clr = 0;
  216. I2S0.lc_conf.indscr_burst_en = 0;
  217. I2S0.lc_conf.out_eof_mode = 1;
  218. I2S0.conf2.lcd_en = 0;
  219. I2S0.conf2.camera_en = 0;
  220. I2S0.pdm_conf.pcm2pdm_conv_en = 0;
  221. I2S0.pdm_conf.pdm2pcm_conv_en = 0;
  222. I2S0.fifo_conf.dscr_en = 0;
  223. I2S0.conf_chan.tx_chan_mod = TERN(I2S_STEPPER_SPLIT_STREAM, 4, 0);
  224. I2S0.fifo_conf.tx_fifo_mod = 0;
  225. I2S0.conf.tx_mono = 0;
  226. I2S0.conf_chan.rx_chan_mod = 0;
  227. I2S0.fifo_conf.rx_fifo_mod = 0;
  228. I2S0.conf.rx_mono = 0;
  229. I2S0.fifo_conf.dscr_en = 1; //connect dma to fifo
  230. I2S0.conf.tx_start = 0;
  231. I2S0.conf.rx_start = 0;
  232. I2S0.conf.tx_msb_right = 1;
  233. I2S0.conf.tx_right_first = 1;
  234. I2S0.conf.tx_slave_mod = 0; // Master
  235. I2S0.fifo_conf.tx_fifo_mod_force_en = 1;
  236. I2S0.pdm_conf.rx_pdm_en = 0;
  237. I2S0.pdm_conf.tx_pdm_en = 0;
  238. I2S0.conf.tx_short_sync = 0;
  239. I2S0.conf.rx_short_sync = 0;
  240. I2S0.conf.tx_msb_shift = 0;
  241. I2S0.conf.rx_msb_shift = 0;
  242. // set clock
  243. I2S0.clkm_conf.clka_en = 0; // Use PLL/2 as reference
  244. I2S0.clkm_conf.clkm_div_num = 10; // minimum value of 2, reset value of 4, max 256
  245. I2S0.clkm_conf.clkm_div_a = 0; // 0 at reset, what about divide by 0? (not an issue)
  246. I2S0.clkm_conf.clkm_div_b = 0; // 0 at reset
  247. // fbck = fi2s / tx_bck_div_num
  248. I2S0.sample_rate_conf.tx_bck_div_num = 2; // minimum value of 2 defaults to 6
  249. // Enable TX interrupts
  250. I2S0.int_ena.out_eof = 1;
  251. I2S0.int_ena.out_dscr_err = 0;
  252. I2S0.int_ena.out_total_eof = 0;
  253. I2S0.int_ena.out_done = 0;
  254. // Allocate and Enable the I2S interrupt
  255. intr_handle_t i2s_isr_handle;
  256. esp_intr_alloc(ETS_I2S0_INTR_SOURCE, 0, i2s_intr_handler_default, nullptr, &i2s_isr_handle);
  257. esp_intr_enable(i2s_isr_handle);
  258. // Create the task that will feed the buffer
  259. xTaskCreatePinnedToCore(stepperTask, "StepperTask", 10000, nullptr, 1, nullptr, CONFIG_ARDUINO_RUNNING_CORE); // run I2S stepper task on same core as rest of Marlin
  260. // Route the i2s pins to the appropriate GPIO
  261. // If a pin is not defined, no need to configure
  262. #if defined(I2S_DATA) && I2S_DATA >= 0
  263. gpio_matrix_out_check(I2S_DATA, I2S0O_DATA_OUT23_IDX, 0, 0);
  264. #endif
  265. #if defined(I2S_BCK) && I2S_BCK >= 0
  266. gpio_matrix_out_check(I2S_BCK, I2S0O_BCK_OUT_IDX, 0, 0);
  267. #endif
  268. #if defined(I2S_WS) && I2S_WS >= 0
  269. gpio_matrix_out_check(I2S_WS, I2S0O_WS_OUT_IDX, 0, 0);
  270. #endif
  271. // Start the I2S peripheral
  272. return i2s_start(I2S_NUM_0);
  273. }
  274. void i2s_write(uint8_t pin, uint8_t val) {
  275. #if ENABLED(I2S_STEPPER_SPLIT_STREAM)
  276. if (pin >= 16) {
  277. SET_BIT_TO(I2S0.conf_single_data, pin, val);
  278. return;
  279. }
  280. #endif
  281. SET_BIT_TO(i2s_port_data, pin, val);
  282. }
  283. uint8_t i2s_state(uint8_t pin) {
  284. #if ENABLED(I2S_STEPPER_SPLIT_STREAM)
  285. if (pin >= 16) return TEST(I2S0.conf_single_data, pin);
  286. #endif
  287. return TEST(i2s_port_data, pin);
  288. }
  289. void i2s_push_sample() {
  290. // Every 4µs (when space in DMA buffer) toggle each expander PWM output using
  291. // the current duty cycle/frequency so they sync with any steps (once
  292. // through the DMA/FIFO buffers). PWM signal inversion handled by other functions
  293. LOOP_L_N(p, MAX_EXPANDER_BITS) {
  294. if (hal.pwm_pin_data[p].pwm_duty_ticks > 0) { // pin has active pwm?
  295. if (hal.pwm_pin_data[p].pwm_tick_count == 0) {
  296. if (TEST32(i2s_port_data, p)) { // hi->lo
  297. CBI32(i2s_port_data, p);
  298. hal.pwm_pin_data[p].pwm_tick_count = hal.pwm_pin_data[p].pwm_cycle_ticks - hal.pwm_pin_data[p].pwm_duty_ticks;
  299. }
  300. else { // lo->hi
  301. SBI32(i2s_port_data, p);
  302. hal.pwm_pin_data[p].pwm_tick_count = hal.pwm_pin_data[p].pwm_duty_ticks;
  303. }
  304. }
  305. else
  306. hal.pwm_pin_data[p].pwm_tick_count--;
  307. }
  308. }
  309. dma.current[dma.rw_pos++] = i2s_port_data;
  310. }
  311. #endif // !USE_ESP32_EXIO
  312. #endif // ARDUINO_ARCH_ESP32