1 // Copyright 2015-2019 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 /*******************************************************************************
16 * NOTICE
17 * The hal is not public api, don't use in application code.
18 * See readme.md in hal/include/hal/readme.md
19 ******************************************************************************/
20
21 // The LL layer for ESP32 SPI register operations
22
23 #pragma once
24
25 #include "hal/hal_defs.h"
26 #include "soc/spi_periph.h"
27 #include "esp32/rom/lldesc.h"
28 #include <string.h>
29 #include <esp_types.h>
30 #include <stdlib.h> //for abs()
31
32 #if defined(__ZEPHYR__)
33 #include "stubs.h"
34 #endif
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 /// Registers to reset during initialization. Don't use in app.
41 #define SPI_LL_DMA_FIFO_RST_MASK (SPI_AHBM_RST | SPI_AHBM_FIFO_RST)
42 /// Interrupt not used. Don't use in app.
43 #define SPI_LL_UNUSED_INT_MASK (SPI_INT_EN | SPI_SLV_WR_STA_DONE | SPI_SLV_RD_STA_DONE | SPI_SLV_WR_BUF_DONE | SPI_SLV_RD_BUF_DONE)
44 /// Swap the bit order to its correct place to send
45 #define HAL_SPI_SWAP_DATA_TX(data, len) HAL_SWAP32((uint32_t)data<<(32-len))
46 /// This is the expected clock frequency
47 #define SPI_LL_PERIPH_CLK_FREQ (80 * 1000000)
48 #define SPI_LL_GET_HW(ID) ((ID)==0? &SPI1:((ID)==1? &SPI2 : &SPI3))
49
50 /**
51 * The data structure holding calculated clock configuration. Since the
52 * calculation needs long time, it should be calculated during initialization and
53 * stored somewhere to be quickly used.
54 */
55 typedef uint32_t spi_ll_clock_val_t;
56
57 //On ESP32-S2 and earlier chips, DMA registers are part of SPI registers. So set the registers of SPI peripheral to control DMA.
58 typedef spi_dev_t spi_dma_dev_t;
59
60 /** IO modes supported by the master. */
61 typedef enum {
62 SPI_LL_IO_MODE_NORMAL = 0, ///< 1-bit mode for all phases
63 SPI_LL_IO_MODE_DIO, ///< 2-bit mode for address and data phases, 1-bit mode for command phase
64 SPI_LL_IO_MODE_DUAL, ///< 2-bit mode for data phases only, 1-bit mode for command and address phases
65 SPI_LL_IO_MODE_QIO, ///< 4-bit mode for address and data phases, 1-bit mode for command phase
66 SPI_LL_IO_MODE_QUAD, ///< 4-bit mode for data phases only, 1-bit mode for command and address phases
67 } spi_ll_io_mode_t;
68
69
70 /*------------------------------------------------------------------------------
71 * Control
72 *----------------------------------------------------------------------------*/
73 /**
74 * Initialize SPI peripheral (master).
75 *
76 * @param hw Beginning address of the peripheral registers.
77 */
spi_ll_master_init(spi_dev_t * hw)78 static inline void spi_ll_master_init(spi_dev_t *hw)
79 {
80 //Reset timing
81 hw->ctrl2.val = 0;
82
83 //use all 64 bytes of the buffer
84 hw->user.usr_miso_highpart = 0;
85 hw->user.usr_mosi_highpart = 0;
86
87 //Disable unneeded ints
88 hw->slave.val &= ~SPI_LL_UNUSED_INT_MASK;
89 }
90
91 /**
92 * Initialize SPI peripheral (slave).
93 *
94 * @param hw Beginning address of the peripheral registers.
95 */
spi_ll_slave_init(spi_dev_t * hw)96 static inline void spi_ll_slave_init(spi_dev_t *hw)
97 {
98 //Configure slave
99 hw->clock.val = 0;
100 hw->user.val = 0;
101 hw->ctrl.val = 0;
102 hw->slave.wr_rd_buf_en = 1; //no sure if needed
103 hw->user.doutdin = 1; //we only support full duplex
104 hw->user.sio = 0;
105 hw->slave.slave_mode = 1;
106 hw->slave.sync_reset = 1;
107 hw->slave.sync_reset = 0;
108 //use all 64 bytes of the buffer
109 hw->user.usr_miso_highpart = 0;
110 hw->user.usr_mosi_highpart = 0;
111
112 //Disable unneeded ints
113 hw->slave.val &= ~SPI_LL_UNUSED_INT_MASK;
114 }
115
116 /**
117 * Check whether user-defined transaction is done.
118 *
119 * @param hw Beginning address of the peripheral registers.
120 *
121 * @return true if transaction is done, otherwise false.
122 */
spi_ll_usr_is_done(spi_dev_t * hw)123 static inline bool spi_ll_usr_is_done(spi_dev_t *hw)
124 {
125 return hw->slave.trans_done;
126 }
127
128 /**
129 * Trigger start of user-defined transaction for master.
130 *
131 * @param hw Beginning address of the peripheral registers.
132 */
spi_ll_master_user_start(spi_dev_t * hw)133 static inline void spi_ll_master_user_start(spi_dev_t *hw)
134 {
135 hw->cmd.usr = 1;
136 }
137
138 /**
139 * Trigger start of user-defined transaction for slave.
140 *
141 * @param hw Beginning address of the peripheral registers.
142 */
spi_ll_slave_user_start(spi_dev_t * hw)143 static inline void spi_ll_slave_user_start(spi_dev_t *hw)
144 {
145 hw->cmd.usr = 1;
146 }
147
148 /**
149 * Get current running command bit-mask. (Preview)
150 *
151 * @param hw Beginning address of the peripheral registers.
152 *
153 * @return Bitmask of running command, see ``SPI_CMD_REG``. 0 if no in-flight command.
154 */
spi_ll_get_running_cmd(spi_dev_t * hw)155 static inline uint32_t spi_ll_get_running_cmd(spi_dev_t *hw)
156 {
157 return hw->cmd.val;
158 }
159
160 /**
161 * Reset SPI CPU TX FIFO
162 *
163 * @param hw Beginning address of the peripheral registers.
164 */
spi_ll_cpu_tx_fifo_reset(spi_dev_t * hw)165 static inline void spi_ll_cpu_tx_fifo_reset(spi_dev_t *hw)
166 {
167 //This is not used in esp32
168 }
169
170 /**
171 * Reset SPI DMA FIFO
172 *
173 * @param hw Beginning address of the peripheral registers.
174 */
spi_ll_cpu_rx_fifo_reset(spi_dev_t * hw)175 static inline void spi_ll_cpu_rx_fifo_reset(spi_dev_t *hw)
176 {
177 //This is not used in esp32
178 }
179
180 /**
181 * Reset SPI DMA TX FIFO
182 *
183 * On ESP32, this function is not seperated
184 *
185 * @param hw Beginning address of the peripheral registers.
186 */
spi_ll_dma_tx_fifo_reset(spi_dev_t * hw)187 static inline void spi_ll_dma_tx_fifo_reset(spi_dev_t *hw)
188 {
189 hw->dma_conf.val |= SPI_LL_DMA_FIFO_RST_MASK;
190 hw->dma_conf.val &= ~SPI_LL_DMA_FIFO_RST_MASK;
191 }
192
193 /**
194 * Reset SPI DMA RX FIFO
195 *
196 * On ESP32, this function is not seperated
197 *
198 * @param hw Beginning address of the peripheral registers.
199 */
spi_ll_dma_rx_fifo_reset(spi_dev_t * hw)200 static inline void spi_ll_dma_rx_fifo_reset(spi_dev_t *hw)
201 {
202 hw->dma_conf.val |= SPI_LL_DMA_FIFO_RST_MASK;
203 hw->dma_conf.val &= ~SPI_LL_DMA_FIFO_RST_MASK;
204 }
205
206 /**
207 * Clear in fifo full error
208 *
209 * @param hw Beginning address of the peripheral registers.
210 */
spi_ll_infifo_full_clr(spi_dev_t * hw)211 static inline void spi_ll_infifo_full_clr(spi_dev_t *hw)
212 {
213 //This is not used in esp32
214 }
215
216 /**
217 * Clear out fifo empty error
218 *
219 * @param hw Beginning address of the peripheral registers.
220 */
spi_ll_outfifo_empty_clr(spi_dev_t * hw)221 static inline void spi_ll_outfifo_empty_clr(spi_dev_t *hw)
222 {
223 //This is not used in esp32
224 }
225
226 /*------------------------------------------------------------------------------
227 * SPI configuration for DMA
228 *----------------------------------------------------------------------------*/
229
230 /**
231 * Enable/Disable RX DMA (Peripherals->DMA->RAM)
232 *
233 * @param hw Beginning address of the peripheral registers.
234 * @param enable 1: enable; 2: disable
235 */
spi_ll_dma_rx_enable(spi_dev_t * hw,bool enable)236 static inline void spi_ll_dma_rx_enable(spi_dev_t *hw, bool enable)
237 {
238 //This is not used in esp32
239 }
240
241 /**
242 * Enable/Disable TX DMA (RAM->DMA->Peripherals)
243 *
244 * @param hw Beginning address of the peripheral registers.
245 * @param enable 1: enable; 2: disable
246 */
spi_ll_dma_tx_enable(spi_dev_t * hw,bool enable)247 static inline void spi_ll_dma_tx_enable(spi_dev_t *hw, bool enable)
248 {
249 //This is not used in esp32
250 }
251
252 /**
253 * Configuration of RX DMA EOF interrupt generation way
254 *
255 * @param hw Beginning address of the peripheral registers.
256 * @param enable 1: spi_dma_inlink_eof is set when the number of dma pushed data bytes is equal to the value of spi_slv/mst_dma_rd_bytelen[19:0] in spi dma transition. 0: spi_dma_inlink_eof is set by spi_trans_done in non-seg-trans or spi_dma_seg_trans_done in seg-trans.
257 */
spi_ll_dma_set_rx_eof_generation(spi_dev_t * hw,bool enable)258 static inline void spi_ll_dma_set_rx_eof_generation(spi_dev_t *hw, bool enable)
259 {
260 //This is not used in esp32
261 }
262
263 /*------------------------------------------------------------------------------
264 * Buffer
265 *----------------------------------------------------------------------------*/
266 /**
267 * Write to SPI buffer.
268 *
269 * @param hw Beginning address of the peripheral registers.
270 * @param buffer_to_send Data address to copy to the buffer.
271 * @param bitlen Length to copy, in bits.
272 */
spi_ll_write_buffer(spi_dev_t * hw,const uint8_t * buffer_to_send,size_t bitlen)273 static inline void spi_ll_write_buffer(spi_dev_t *hw, const uint8_t *buffer_to_send, size_t bitlen)
274 {
275 for (size_t x = 0; x < bitlen; x += 32) {
276 //Use memcpy to get around alignment issues for txdata
277 uint32_t word;
278 memcpy(&word, &buffer_to_send[x / 8], 4);
279 hw->data_buf[(x / 32)] = word;
280 }
281 }
282
283 /**
284 * Read from SPI buffer.
285 *
286 * @param hw Beginning address of the peripheral registers.
287 * @param buffer_to_rcv Address to copy buffer data to.
288 * @param bitlen Length to copy, in bits.
289 */
spi_ll_read_buffer(spi_dev_t * hw,uint8_t * buffer_to_rcv,size_t bitlen)290 static inline void spi_ll_read_buffer(spi_dev_t *hw, uint8_t *buffer_to_rcv, size_t bitlen)
291 {
292 for (size_t x = 0; x < bitlen; x += 32) {
293 //Do a memcpy to get around possible alignment issues in rx_buffer
294 uint32_t word = hw->data_buf[x / 32];
295 int len = bitlen - x;
296 if (len > 32) {
297 len = 32;
298 }
299 memcpy(&buffer_to_rcv[x / 8], &word, (len + 7) / 8);
300 }
301 }
302
303 /*------------------------------------------------------------------------------
304 * Configs: mode
305 *----------------------------------------------------------------------------*/
306 /**
307 * Enable/disable the postive-cs feature.
308 *
309 * @param hw Beginning address of the peripheral registers.
310 * @param cs One of the CS (0-2) to enable/disable the feature.
311 * @param pos_cs true to enable the feature, otherwise disable (default).
312 */
spi_ll_master_set_pos_cs(spi_dev_t * hw,int cs,uint32_t pos_cs)313 static inline void spi_ll_master_set_pos_cs(spi_dev_t *hw, int cs, uint32_t pos_cs)
314 {
315 if (pos_cs) {
316 hw->pin.master_cs_pol |= (1 << cs);
317 } else {
318 hw->pin.master_cs_pol &= ~(1 << cs);
319 }
320 }
321
322 /**
323 * Enable/disable the LSBFIRST feature for TX data.
324 *
325 * @param hw Beginning address of the peripheral registers.
326 * @param lsbfirst true if LSB of TX data to be sent first, otherwise MSB is sent first (default).
327 */
spi_ll_set_tx_lsbfirst(spi_dev_t * hw,bool lsbfirst)328 static inline void spi_ll_set_tx_lsbfirst(spi_dev_t *hw, bool lsbfirst)
329 {
330 hw->ctrl.wr_bit_order = lsbfirst;
331 }
332
333 /**
334 * Enable/disable the LSBFIRST feature for RX data.
335 *
336 * @param hw Beginning address of the peripheral registers.
337 * @param lsbfirst true if first bit received as LSB, otherwise as MSB (default).
338 */
spi_ll_set_rx_lsbfirst(spi_dev_t * hw,bool lsbfirst)339 static inline void spi_ll_set_rx_lsbfirst(spi_dev_t *hw, bool lsbfirst)
340 {
341 hw->ctrl.rd_bit_order = lsbfirst;
342 }
343
344 /**
345 * Set SPI mode for the peripheral as master.
346 *
347 * @param hw Beginning address of the peripheral registers.
348 * @param mode SPI mode to work at, 0-3.
349 */
spi_ll_master_set_mode(spi_dev_t * hw,uint8_t mode)350 static inline void spi_ll_master_set_mode(spi_dev_t *hw, uint8_t mode)
351 {
352 //Configure polarity
353 if (mode == 0) {
354 hw->pin.ck_idle_edge = 0;
355 hw->user.ck_out_edge = 0;
356 } else if (mode == 1) {
357 hw->pin.ck_idle_edge = 0;
358 hw->user.ck_out_edge = 1;
359 } else if (mode == 2) {
360 hw->pin.ck_idle_edge = 1;
361 hw->user.ck_out_edge = 1;
362 } else if (mode == 3) {
363 hw->pin.ck_idle_edge = 1;
364 hw->user.ck_out_edge = 0;
365 }
366 }
367
368 /**
369 * Set SPI mode for the peripheral as slave.
370 *
371 * @param hw Beginning address of the peripheral registers.
372 * @param mode SPI mode to work at, 0-3.
373 */
spi_ll_slave_set_mode(spi_dev_t * hw,const int mode,bool dma_used)374 static inline void spi_ll_slave_set_mode(spi_dev_t *hw, const int mode, bool dma_used)
375 {
376 if (mode == 0) {
377 //The timing needs to be fixed to meet the requirements of DMA
378 hw->pin.ck_idle_edge = 1;
379 hw->user.ck_i_edge = 0;
380 hw->ctrl2.miso_delay_mode = 0;
381 hw->ctrl2.miso_delay_num = 0;
382 hw->ctrl2.mosi_delay_mode = 2;
383 hw->ctrl2.mosi_delay_num = 2;
384 } else if (mode == 1) {
385 hw->pin.ck_idle_edge = 1;
386 hw->user.ck_i_edge = 1;
387 hw->ctrl2.miso_delay_mode = 2;
388 hw->ctrl2.miso_delay_num = 0;
389 hw->ctrl2.mosi_delay_mode = 0;
390 hw->ctrl2.mosi_delay_num = 0;
391 } else if (mode == 2) {
392 //The timing needs to be fixed to meet the requirements of DMA
393 hw->pin.ck_idle_edge = 0;
394 hw->user.ck_i_edge = 1;
395 hw->ctrl2.miso_delay_mode = 0;
396 hw->ctrl2.miso_delay_num = 0;
397 hw->ctrl2.mosi_delay_mode = 1;
398 hw->ctrl2.mosi_delay_num = 2;
399 } else if (mode == 3) {
400 hw->pin.ck_idle_edge = 0;
401 hw->user.ck_i_edge = 0;
402 hw->ctrl2.miso_delay_mode = 1;
403 hw->ctrl2.miso_delay_num = 0;
404 hw->ctrl2.mosi_delay_mode = 0;
405 hw->ctrl2.mosi_delay_num = 0;
406 }
407
408 /* Silicon issues exists in mode 0 and 2 with DMA, change clock phase to
409 * avoid dma issue. This will cause slave output to appear at most half a
410 * spi clock before
411 */
412 if (dma_used) {
413 if (mode == 0) {
414 hw->pin.ck_idle_edge = 0;
415 hw->user.ck_i_edge = 1;
416 hw->ctrl2.miso_delay_mode = 0;
417 hw->ctrl2.miso_delay_num = 2;
418 hw->ctrl2.mosi_delay_mode = 0;
419 hw->ctrl2.mosi_delay_num = 3;
420 } else if (mode == 2) {
421 hw->pin.ck_idle_edge = 1;
422 hw->user.ck_i_edge = 0;
423 hw->ctrl2.miso_delay_mode = 0;
424 hw->ctrl2.miso_delay_num = 2;
425 hw->ctrl2.mosi_delay_mode = 0;
426 hw->ctrl2.mosi_delay_num = 3;
427 }
428 }
429 }
430
431 /**
432 * Set SPI to work in full duplex or half duplex mode.
433 *
434 * @param hw Beginning address of the peripheral registers.
435 * @param half_duplex true to work in half duplex mode, otherwise in full duplex mode.
436 */
spi_ll_set_half_duplex(spi_dev_t * hw,bool half_duplex)437 static inline void spi_ll_set_half_duplex(spi_dev_t *hw, bool half_duplex)
438 {
439 hw->user.doutdin = !half_duplex;
440 }
441
442 /**
443 * Set SPI to work in SIO mode or not.
444 *
445 * SIO is a mode which MOSI and MISO share a line. The device MUST work in half-duplexmode.
446 *
447 * @param hw Beginning address of the peripheral registers.
448 * @param sio_mode true to work in SIO mode, otherwise false.
449 */
spi_ll_set_sio_mode(spi_dev_t * hw,int sio_mode)450 static inline void spi_ll_set_sio_mode(spi_dev_t *hw, int sio_mode)
451 {
452 hw->user.sio = sio_mode;
453 }
454
455 /**
456 * Configure the io mode for the master to work at.
457 *
458 * @param hw Beginning address of the peripheral registers.
459 * @param io_mode IO mode to work at, see ``spi_ll_io_mode_t``.
460 */
spi_ll_master_set_io_mode(spi_dev_t * hw,spi_ll_io_mode_t io_mode)461 static inline void spi_ll_master_set_io_mode(spi_dev_t *hw, spi_ll_io_mode_t io_mode)
462 {
463 hw->ctrl.val &= ~(SPI_FREAD_DUAL | SPI_FREAD_QUAD | SPI_FREAD_DIO | SPI_FREAD_QIO);
464 hw->user.val &= ~(SPI_FWRITE_DUAL | SPI_FWRITE_QUAD | SPI_FWRITE_DIO | SPI_FWRITE_QIO);
465 switch (io_mode) {
466 case SPI_LL_IO_MODE_DIO:
467 hw->ctrl.fread_dio = 1;
468 hw->user.fwrite_dio = 1;
469 break;
470 case SPI_LL_IO_MODE_DUAL:
471 hw->ctrl.fread_dual = 1;
472 hw->user.fwrite_dual = 1;
473 break;
474 case SPI_LL_IO_MODE_QIO:
475 hw->ctrl.fread_qio = 1;
476 hw->user.fwrite_qio = 1;
477 break;
478 case SPI_LL_IO_MODE_QUAD:
479 hw->ctrl.fread_quad = 1;
480 hw->user.fwrite_quad = 1;
481 break;
482 default:
483 break;
484 };
485 if (io_mode != SPI_LL_IO_MODE_NORMAL) {
486 hw->ctrl.fastrd_mode = 1;
487 }
488 }
489
490 /**
491 * Select one of the CS to use in current transaction.
492 *
493 * @param hw Beginning address of the peripheral registers.
494 * @param cs_id The cs to use, 0-2, otherwise none of them is used.
495 */
spi_ll_master_select_cs(spi_dev_t * hw,int cs_id)496 static inline void spi_ll_master_select_cs(spi_dev_t *hw, int cs_id)
497 {
498 hw->pin.cs0_dis = (cs_id == 0) ? 0 : 1;
499 hw->pin.cs1_dis = (cs_id == 1) ? 0 : 1;
500 hw->pin.cs2_dis = (cs_id == 2) ? 0 : 1;
501 }
502
503 /*------------------------------------------------------------------------------
504 * Configs: parameters
505 *----------------------------------------------------------------------------*/
506 /**
507 * Set the clock for master by stored value.
508 *
509 * @param hw Beginning address of the peripheral registers.
510 * @param val stored clock configuration calculated before (by ``spi_ll_cal_clock``).
511 */
spi_ll_master_set_clock_by_reg(spi_dev_t * hw,const spi_ll_clock_val_t * val)512 static inline void spi_ll_master_set_clock_by_reg(spi_dev_t *hw, const spi_ll_clock_val_t *val)
513 {
514 hw->clock.val = *(uint32_t *)val;
515 }
516
517 /**
518 * Get the frequency of given dividers. Don't use in app.
519 *
520 * @param fapb APB clock of the system.
521 * @param pre Pre devider.
522 * @param n main divider.
523 *
524 * @return Frequency of given dividers.
525 */
spi_ll_freq_for_pre_n(int fapb,int pre,int n)526 static inline int spi_ll_freq_for_pre_n(int fapb, int pre, int n)
527 {
528 return (fapb / (pre * n));
529 }
530
531 /**
532 * Calculate the nearest frequency avaliable for master.
533 *
534 * @param fapb APB clock of the system.
535 * @param hz Frequncy desired.
536 * @param duty_cycle Duty cycle desired.
537 * @param out_reg Output address to store the calculated clock configurations for the return frequency.
538 *
539 * @return Actual (nearest) frequency.
540 */
spi_ll_master_cal_clock(int fapb,int hz,int duty_cycle,spi_ll_clock_val_t * out_reg)541 static inline int spi_ll_master_cal_clock(int fapb, int hz, int duty_cycle, spi_ll_clock_val_t *out_reg)
542 {
543 typeof(SPI1.clock) reg;
544 int eff_clk;
545
546 //In hw, n, h and l are 1-64, pre is 1-8K. Value written to register is one lower than used value.
547 if (hz > ((fapb / 4) * 3)) {
548 //Using Fapb directly will give us the best result here.
549 reg.clkcnt_l = 0;
550 reg.clkcnt_h = 0;
551 reg.clkcnt_n = 0;
552 reg.clkdiv_pre = 0;
553 reg.clk_equ_sysclk = 1;
554 eff_clk = fapb;
555 } else {
556 //For best duty cycle resolution, we want n to be as close to 32 as possible, but
557 //we also need a pre/n combo that gets us as close as possible to the intended freq.
558 //To do this, we bruteforce n and calculate the best pre to go along with that.
559 //If there's a choice between pre/n combos that give the same result, use the one
560 //with the higher n.
561 int pre, n, h, l;
562 int bestn = -1;
563 int bestpre = -1;
564 int besterr = 0;
565 int errval;
566 for (n = 2; n <= 64; n++) { //Start at 2: we need to be able to set h/l so we have at least one high and one low pulse.
567 //Effectively, this does pre=round((fapb/n)/hz).
568 pre = ((fapb / n) + (hz / 2)) / hz;
569 if (pre <= 0) {
570 pre = 1;
571 }
572 if (pre > 8192) {
573 pre = 8192;
574 }
575 errval = abs(spi_ll_freq_for_pre_n(fapb, pre, n) - hz);
576 if (bestn == -1 || errval <= besterr) {
577 besterr = errval;
578 bestn = n;
579 bestpre = pre;
580 }
581 }
582
583 n = bestn;
584 pre = bestpre;
585 l = n;
586 //This effectively does round((duty_cycle*n)/256)
587 h = (duty_cycle * n + 127) / 256;
588 if (h <= 0) {
589 h = 1;
590 }
591
592 reg.clk_equ_sysclk = 0;
593 reg.clkcnt_n = n - 1;
594 reg.clkdiv_pre = pre - 1;
595 reg.clkcnt_h = h - 1;
596 reg.clkcnt_l = l - 1;
597 eff_clk = spi_ll_freq_for_pre_n(fapb, pre, n);
598 }
599 if (out_reg != NULL) {
600 *(uint32_t *)out_reg = reg.val;
601 }
602 return eff_clk;
603 }
604
605 /**
606 * Calculate and set clock for SPI master according to desired parameters.
607 *
608 * This takes long, suggest to calculate the configuration during
609 * initialization by ``spi_ll_master_cal_clock`` and store the result, then
610 * configure the clock by stored value when used by
611 * ``spi_ll_msater_set_clock_by_reg``.
612 *
613 * @param hw Beginning address of the peripheral registers.
614 * @param fapb APB clock of the system.
615 * @param hz Frequncy desired.
616 * @param duty_cycle Duty cycle desired.
617 *
618 * @return Actual frequency that is used.
619 */
spi_ll_master_set_clock(spi_dev_t * hw,int fapb,int hz,int duty_cycle)620 static inline int spi_ll_master_set_clock(spi_dev_t *hw, int fapb, int hz, int duty_cycle)
621 {
622 spi_ll_clock_val_t reg_val;
623 int freq = spi_ll_master_cal_clock(fapb, hz, duty_cycle, ®_val);
624 spi_ll_master_set_clock_by_reg(hw, ®_val);
625 return freq;
626 }
627
628 /**
629 * Enable/disable the CK sel feature for a CS pin.
630 *
631 * CK sel is a feature to toggle the CS line along with the clock.
632 *
633 * @param hw Beginning address of the peripheral registers.
634 * @param cs CS pin to enable/disable the feature, 0-2.
635 * @param cksel true to enable the feature, otherwise false.
636 */
spi_ll_master_set_cksel(spi_dev_t * hw,int cs,uint32_t cksel)637 static inline void spi_ll_master_set_cksel(spi_dev_t *hw, int cs, uint32_t cksel)
638 {
639 if (cksel) {
640 hw->pin.master_ck_sel |= (1 << cs);
641 } else {
642 hw->pin.master_ck_sel &= ~(1 << cs);
643 }
644 }
645
646 /**
647 * Set the mosi delay after the output edge to the signal. (Preview)
648 *
649 * The delay mode/num is a Espressif conception, may change in the new chips.
650 *
651 * @param hw Beginning address of the peripheral registers.
652 * @param delay_mode Delay mode, see TRM.
653 * @param delay_num APB clocks to delay.
654 */
spi_ll_set_mosi_delay(spi_dev_t * hw,int delay_mode,int delay_num)655 static inline void spi_ll_set_mosi_delay(spi_dev_t *hw, int delay_mode, int delay_num)
656 {
657 hw->ctrl2.mosi_delay_mode = delay_mode;
658 hw->ctrl2.mosi_delay_num = delay_num;
659 }
660
661 /**
662 * Set the miso delay applied to the input signal before the internal peripheral. (Preview)
663 *
664 * The delay mode/num is a Espressif conception, may change in the new chips.
665 *
666 * @param hw Beginning address of the peripheral registers.
667 * @param delay_mode Delay mode, see TRM.
668 * @param delay_num APB clocks to delay.
669 */
spi_ll_set_miso_delay(spi_dev_t * hw,int delay_mode,int delay_num)670 static inline void spi_ll_set_miso_delay(spi_dev_t *hw, int delay_mode, int delay_num)
671 {
672 hw->ctrl2.miso_delay_mode = delay_mode;
673 hw->ctrl2.miso_delay_num = delay_num;
674 }
675
676 /**
677 * Set dummy clocks to output before RX phase (master), or clocks to skip
678 * before the data phase and after the address phase (slave).
679 *
680 * Note this phase is also used to compensate RX timing in half duplex mode.
681 *
682 * @param hw Beginning address of the peripheral registers.
683 * @param dummy_n Dummy cycles used. 0 to disable the dummy phase.
684 */
spi_ll_set_dummy(spi_dev_t * hw,int dummy_n)685 static inline void spi_ll_set_dummy(spi_dev_t *hw, int dummy_n)
686 {
687 hw->user.usr_dummy = dummy_n ? 1 : 0;
688 hw->user1.usr_dummy_cyclelen = dummy_n - 1;
689 }
690
691 /**
692 * Set the delay of SPI clocks before the CS inactive edge after the last SPI clock.
693 *
694 * @param hw Beginning address of the peripheral registers.
695 * @param hold Delay of SPI clocks after the last clock, 0 to disable the hold phase.
696 */
spi_ll_master_set_cs_hold(spi_dev_t * hw,int hold)697 static inline void spi_ll_master_set_cs_hold(spi_dev_t *hw, int hold)
698 {
699 hw->ctrl2.hold_time = hold;
700 hw->user.cs_hold = hold ? 1 : 0;
701 }
702
703 /**
704 * Set the delay of SPI clocks before the first SPI clock after the CS active edge.
705 *
706 * Note ESP32 doesn't support to use this feature when command/address phases
707 * are used in full duplex mode.
708 *
709 * @param hw Beginning address of the peripheral registers.
710 * @param setup Delay of SPI clocks after the CS active edge, 0 to disable the setup phase.
711 */
spi_ll_master_set_cs_setup(spi_dev_t * hw,uint8_t setup)712 static inline void spi_ll_master_set_cs_setup(spi_dev_t *hw, uint8_t setup)
713 {
714 hw->ctrl2.setup_time = setup - 1;
715 hw->user.cs_setup = setup ? 1 : 0;
716 }
717
718 /*------------------------------------------------------------------------------
719 * Configs: data
720 *----------------------------------------------------------------------------*/
721 /**
722 * Set the input length (master).
723 *
724 * @param hw Beginning address of the peripheral registers.
725 * @param bitlen input length, in bits.
726 */
spi_ll_set_miso_bitlen(spi_dev_t * hw,size_t bitlen)727 static inline void spi_ll_set_miso_bitlen(spi_dev_t *hw, size_t bitlen)
728 {
729 hw->miso_dlen.usr_miso_dbitlen = bitlen - 1;
730 }
731
732 /**
733 * Set the output length (master).
734 *
735 * @param hw Beginning address of the peripheral registers.
736 * @param bitlen output length, in bits.
737 */
spi_ll_set_mosi_bitlen(spi_dev_t * hw,size_t bitlen)738 static inline void spi_ll_set_mosi_bitlen(spi_dev_t *hw, size_t bitlen)
739 {
740 hw->mosi_dlen.usr_mosi_dbitlen = bitlen - 1;
741 }
742
743 /**
744 * Set the maximum input length (slave).
745 *
746 * @param hw Beginning address of the peripheral registers.
747 * @param bitlen input length, in bits.
748 */
spi_ll_slave_set_rx_bitlen(spi_dev_t * hw,size_t bitlen)749 static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen)
750 {
751 hw->slv_wrbuf_dlen.bit_len = bitlen - 1;
752 }
753
754 /**
755 * Set the maximum output length (slave).
756 *
757 * @param hw Beginning address of the peripheral registers.
758 * @param bitlen output length, in bits.
759 */
spi_ll_slave_set_tx_bitlen(spi_dev_t * hw,size_t bitlen)760 static inline void spi_ll_slave_set_tx_bitlen(spi_dev_t *hw, size_t bitlen)
761 {
762 hw->slv_rdbuf_dlen.bit_len = bitlen - 1;
763 }
764
765 /**
766 * Set the length of command phase.
767 *
768 * When in 4-bit mode, the SPI cycles of the phase will be shorter. E.g. 16-bit
769 * command phases takes 4 cycles in 4-bit mode.
770 *
771 * @param hw Beginning address of the peripheral registers.
772 * @param bitlen Length of command phase, in bits. 0 to disable the command phase.
773 */
spi_ll_set_command_bitlen(spi_dev_t * hw,int bitlen)774 static inline void spi_ll_set_command_bitlen(spi_dev_t *hw, int bitlen)
775 {
776 hw->user2.usr_command_bitlen = bitlen - 1;
777 hw->user.usr_command = bitlen ? 1 : 0;
778 }
779
780 /**
781 * Set the length of address phase.
782 *
783 * When in 4-bit mode, the SPI cycles of the phase will be shorter. E.g. 16-bit
784 * address phases takes 4 cycles in 4-bit mode.
785 *
786 * @param hw Beginning address of the peripheral registers.
787 * @param bitlen Length of address phase, in bits. 0 to disable the address phase.
788 */
spi_ll_set_addr_bitlen(spi_dev_t * hw,int bitlen)789 static inline void spi_ll_set_addr_bitlen(spi_dev_t *hw, int bitlen)
790 {
791 hw->user1.usr_addr_bitlen = bitlen - 1;
792 hw->user.usr_addr = bitlen ? 1 : 0;
793 }
794
795 /**
796 * Set the address value in an intuitive way.
797 *
798 * The length and lsbfirst is required to shift and swap the address to the right place.
799 *
800 * @param hw Beginning address of the peripheral registers.
801 * @param address Address to set
802 * @param addrlen Length of the address phase
803 * @param lsbfirst whether the LSB first feature is enabled.
804 */
spi_ll_set_address(spi_dev_t * hw,uint64_t addr,int addrlen,uint32_t lsbfirst)805 static inline void spi_ll_set_address(spi_dev_t *hw, uint64_t addr, int addrlen, uint32_t lsbfirst)
806 {
807 if (lsbfirst) {
808 /* The output address start from the LSB of the highest byte, i.e.
809 * addr[24] -> addr[31]
810 * ...
811 * addr[0] -> addr[7]
812 * slv_wr_status[24] -> slv_wr_status[31]
813 * ...
814 * slv_wr_status[0] -> slv_wr_status[7]
815 * So swap the byte order to let the LSB sent first.
816 */
817 addr = HAL_SWAP64(addr);
818 hw->addr = addr >> 32;
819 hw->slv_wr_status = addr;
820 } else {
821 // shift the address to MSB of addr (and maybe slv_wr_status) register.
822 // output address will be sent from MSB to LSB of addr register, then comes the MSB to LSB of slv_wr_status register.
823 if (addrlen > 32) {
824 hw->addr = addr >> (addrlen - 32);
825 hw->slv_wr_status = addr << (64 - addrlen);
826 } else {
827 hw->addr = addr << (32 - addrlen);
828 }
829 }
830 }
831
832 /**
833 * Set the command value in an intuitive way.
834 *
835 * The length and lsbfirst is required to shift and swap the command to the right place.
836 *
837 * @param hw Beginning command of the peripheral registers.
838 * @param command Command to set
839 * @param addrlen Length of the command phase
840 * @param lsbfirst whether the LSB first feature is enabled.
841 */
spi_ll_set_command(spi_dev_t * hw,uint16_t cmd,int cmdlen,bool lsbfirst)842 static inline void spi_ll_set_command(spi_dev_t *hw, uint16_t cmd, int cmdlen, bool lsbfirst)
843 {
844 if (lsbfirst) {
845 // The output command start from bit0 to bit 15, kept as is.
846 hw->user2.usr_command_value = cmd;
847 } else {
848 /* Output command will be sent from bit 7 to 0 of command_value, and
849 * then bit 15 to 8 of the same register field. Shift and swap to send
850 * more straightly.
851 */
852 hw->user2.usr_command_value = HAL_SPI_SWAP_DATA_TX(cmd, cmdlen);
853
854 }
855 }
856
857 /**
858 * Enable/disable the RX data phase.
859 *
860 * @param hw Beginning address of the peripheral registers.
861 * @param enable true if RX phase exist, otherwise false.
862 */
spi_ll_enable_miso(spi_dev_t * hw,int enable)863 static inline void spi_ll_enable_miso(spi_dev_t *hw, int enable)
864 {
865 hw->user.usr_miso = enable;
866 }
867
868 /**
869 * Enable/disable the TX data phase.
870 *
871 * @param hw Beginning address of the peripheral registers.
872 * @param enable true if TX phase exist, otherwise false.
873 */
spi_ll_enable_mosi(spi_dev_t * hw,int enable)874 static inline void spi_ll_enable_mosi(spi_dev_t *hw, int enable)
875 {
876 hw->user.usr_mosi = enable;
877 }
878
879 /**
880 * Reset the slave peripheral before next transaction.
881 *
882 * @param hw Beginning address of the peripheral registers.
883 */
spi_ll_slave_reset(spi_dev_t * hw)884 static inline void spi_ll_slave_reset(spi_dev_t *hw)
885 {
886 hw->slave.sync_reset = 1;
887 hw->slave.sync_reset = 0;
888 }
889
890 /**
891 * Get the received bit length of the slave.
892 *
893 * @param hw Beginning address of the peripheral registers.
894 *
895 * @return Received bits of the slave.
896 */
spi_ll_slave_get_rcv_bitlen(spi_dev_t * hw)897 static inline uint32_t spi_ll_slave_get_rcv_bitlen(spi_dev_t *hw)
898 {
899 return hw->slv_rd_bit.slv_rdata_bit;
900 }
901
902 /*------------------------------------------------------------------------------
903 * Interrupts
904 *----------------------------------------------------------------------------*/
905 /**
906 * Disable the trans_done interrupt.
907 *
908 * @param hw Beginning address of the peripheral registers.
909 */
spi_ll_disable_int(spi_dev_t * hw)910 static inline void spi_ll_disable_int(spi_dev_t *hw)
911 {
912 hw->slave.trans_inten = 0;
913 }
914
915 /**
916 * Clear the trans_done interrupt.
917 *
918 * @param hw Beginning address of the peripheral registers.
919 */
spi_ll_clear_int_stat(spi_dev_t * hw)920 static inline void spi_ll_clear_int_stat(spi_dev_t *hw)
921 {
922 hw->slave.trans_done = 0;
923 }
924
925 /**
926 * Set the trans_done interrupt.
927 *
928 * @param hw Beginning address of the peripheral registers.
929 */
spi_ll_set_int_stat(spi_dev_t * hw)930 static inline void spi_ll_set_int_stat(spi_dev_t *hw)
931 {
932 hw->slave.trans_done = 1;
933 }
934
935 /**
936 * Enable the trans_done interrupt.
937 *
938 * @param hw Beginning address of the peripheral registers.
939 */
spi_ll_enable_int(spi_dev_t * hw)940 static inline void spi_ll_enable_int(spi_dev_t *hw)
941 {
942 hw->slave.trans_inten = 1;
943 }
944
945 /*------------------------------------------------------------------------------
946 * DMA:
947 * RX DMA (Peripherals->DMA->RAM)
948 * TX DMA (RAM->DMA->Peripherals)
949 *----------------------------------------------------------------------------*/
950 /**
951 * Reset RX DMA which stores the data received from a peripheral into RAM.
952 *
953 * @param dma_in Beginning address of the DMA peripheral registers which stores the data received from a peripheral into RAM.
954 * @param channel DMA channel, for chip version compatibility, not used.
955 */
spi_dma_ll_rx_reset(spi_dma_dev_t * dma_in,uint32_t channel)956 static inline void spi_dma_ll_rx_reset(spi_dma_dev_t *dma_in, uint32_t channel)
957 {
958 //Reset RX DMA peripheral
959 dma_in->dma_conf.in_rst = 1;
960 dma_in->dma_conf.in_rst = 0;
961 }
962
963 /**
964 * Start RX DMA.
965 *
966 * @param dma_in Beginning address of the DMA peripheral registers which stores the data received from a peripheral into RAM.
967 * @param channel DMA channel, for chip version compatibility, not used.
968 * @param addr Address of the beginning DMA descriptor.
969 */
spi_dma_ll_rx_start(spi_dma_dev_t * dma_in,uint32_t channel,lldesc_t * addr)970 static inline void spi_dma_ll_rx_start(spi_dma_dev_t *dma_in, uint32_t channel, lldesc_t *addr)
971 {
972 dma_in->dma_in_link.addr = (int) addr & 0xFFFFF;
973 dma_in->dma_in_link.start = 1;
974 }
975
976 /**
977 * Enable DMA RX channel burst for data
978 *
979 * @param dma_in Beginning address of the DMA peripheral registers which stores the data received from a peripheral into RAM.
980 * @param channel DMA channel, for chip version compatibility, not used.
981 * @param enable True to enable, false to disable
982 */
spi_dma_ll_rx_enable_burst_data(spi_dma_dev_t * dma_in,uint32_t channel,bool enable)983 static inline void spi_dma_ll_rx_enable_burst_data(spi_dma_dev_t *dma_in, uint32_t channel, bool enable)
984 {
985 //This is not supported in esp32
986 }
987
988 /**
989 * Enable DMA RX channel burst for descriptor
990 *
991 * @param dma_in Beginning address of the DMA peripheral registers which stores the data received from a peripheral into RAM.
992 * @param channel DMA channel, for chip version compatibility, not used.
993 * @param enable True to enable, false to disable
994 */
spi_dma_ll_rx_enable_burst_desc(spi_dma_dev_t * dma_in,uint32_t channel,bool enable)995 static inline void spi_dma_ll_rx_enable_burst_desc(spi_dma_dev_t *dma_in, uint32_t channel, bool enable)
996 {
997 dma_in->dma_conf.indscr_burst_en = enable;
998 }
999
1000 /**
1001 * Reset TX DMA which transmits the data from RAM to a peripheral.
1002 *
1003 * @param dma_out Beginning address of the DMA peripheral registers which transmits the data from RAM to a peripheral.
1004 * @param channel DMA channel, for chip version compatibility, not used.
1005 */
spi_dma_ll_tx_reset(spi_dma_dev_t * dma_out,uint32_t channel)1006 static inline void spi_dma_ll_tx_reset(spi_dma_dev_t *dma_out, uint32_t channel)
1007 {
1008 //Reset TX DMA peripheral
1009 dma_out->dma_conf.out_rst = 1;
1010 dma_out->dma_conf.out_rst = 0;
1011 }
1012
1013 /**
1014 * Start TX DMA.
1015 *
1016 * @param dma_out Beginning address of the DMA peripheral registers which transmits the data from RAM to a peripheral.
1017 * @param channel DMA channel, for chip version compatibility, not used.
1018 * @param addr Address of the beginning DMA descriptor.
1019 */
spi_dma_ll_tx_start(spi_dma_dev_t * dma_out,uint32_t channel,lldesc_t * addr)1020 static inline void spi_dma_ll_tx_start(spi_dma_dev_t *dma_out, uint32_t channel, lldesc_t *addr)
1021 {
1022 dma_out->dma_out_link.addr = (int) addr & 0xFFFFF;
1023 dma_out->dma_out_link.start = 1;
1024 }
1025
1026 /**
1027 * Enable DMA TX channel burst for data
1028 *
1029 * @param dma_out Beginning address of the DMA peripheral registers which transmits the data from RAM to a peripheral.
1030 * @param channel DMA channel, for chip version compatibility, not used.
1031 * @param enable True to enable, false to disable
1032 */
spi_dma_ll_tx_enable_burst_data(spi_dma_dev_t * dma_out,uint32_t channel,bool enable)1033 static inline void spi_dma_ll_tx_enable_burst_data(spi_dma_dev_t *dma_out, uint32_t channel, bool enable)
1034 {
1035 dma_out->dma_conf.out_data_burst_en = enable;
1036 }
1037
1038 /**
1039 * Enable DMA TX channel burst for descriptor
1040 *
1041 * @param dma_out Beginning address of the DMA peripheral registers which transmits the data from RAM to a peripheral.
1042 * @param channel DMA channel, for chip version compatibility, not used.
1043 * @param enable True to enable, false to disable
1044 */
spi_dma_ll_tx_enable_burst_desc(spi_dma_dev_t * dma_out,uint32_t channel,bool enable)1045 static inline void spi_dma_ll_tx_enable_burst_desc(spi_dma_dev_t *dma_out, uint32_t channel, bool enable)
1046 {
1047 dma_out->dma_conf.outdscr_burst_en = enable;
1048 }
1049
1050 /**
1051 * Configuration of OUT EOF flag generation way
1052 *
1053 * @param dma_out Beginning address of the DMA peripheral registers which transmits the data from RAM to a peripheral.
1054 * @param channel DMA channel, for chip version compatibility, not used.
1055 * @param enable 1: when dma pop all data from fifo 0:when ahb push all data to fifo.
1056 */
spi_dma_ll_set_out_eof_generation(spi_dma_dev_t * dma_out,uint32_t channel,bool enable)1057 static inline void spi_dma_ll_set_out_eof_generation(spi_dma_dev_t *dma_out, uint32_t channel, bool enable)
1058 {
1059 dma_out->dma_conf.out_eof_mode = enable;
1060 }
1061
1062 /**
1063 * Enable automatic outlink-writeback
1064 *
1065 * @param dma_out Beginning address of the DMA peripheral registers which transmits the data from RAM to a peripheral.
1066 * @param channel DMA channel, for chip version compatibility, not used.
1067 * @param enable True to enable, false to disable
1068 */
spi_dma_ll_enable_out_auto_wrback(spi_dma_dev_t * dma_out,uint32_t channel,bool enable)1069 static inline void spi_dma_ll_enable_out_auto_wrback(spi_dma_dev_t *dma_out, uint32_t channel, bool enable)
1070 {
1071 //does not configure it in ESP32
1072 }
1073
1074 #undef SPI_LL_RST_MASK
1075 #undef SPI_LL_UNUSED_INT_MASK
1076
1077 #ifdef __cplusplus
1078 }
1079 #endif
1080