spi_ll.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. /*******************************************************************************
  14. * NOTICE
  15. * The hal is not public api, don't use in application code.
  16. * See readme.md in soc/include/hal/readme.md
  17. ******************************************************************************/
  18. // The LL layer for ESP32 SPI register operations
  19. #pragma once
  20. #include "hal/hal_defs.h"
  21. #include "soc/spi_periph.h"
  22. #include "esp32/rom/lldesc.h"
  23. #include <string.h>
  24. #include <esp_types.h>
  25. #include <stdlib.h> //for abs()
  26. /// Registers to reset during initialization. Don't use in app.
  27. #define SPI_LL_RST_MASK (SPI_OUT_RST | SPI_IN_RST | SPI_AHBM_RST | SPI_AHBM_FIFO_RST)
  28. /// Interrupt not used. Don't use in app.
  29. #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)
  30. /// Swap the bit order to its correct place to send
  31. #define HAL_SPI_SWAP_DATA_TX(data, len) HAL_SWAP32((uint32_t)data<<(32-len))
  32. /**
  33. * The data structure holding calculated clock configuration. Since the
  34. * calculation needs long time, it should be calculated during initialization and
  35. * stored somewhere to be quickly used.
  36. */
  37. typedef uint32_t spi_ll_clock_val_t;
  38. /** IO modes supported by the master. */
  39. typedef enum {
  40. SPI_LL_IO_MODE_NORMAL = 0, ///< 1-bit mode for all phases
  41. SPI_LL_IO_MODE_DIO, ///< 2-bit mode for address and data phases, 1-bit mode for command phase
  42. SPI_LL_IO_MODE_DUAL, ///< 2-bit mode for data phases only, 1-bit mode for command and address phases
  43. SPI_LL_IO_MODE_QIO, ///< 4-bit mode for address and data phases, 1-bit mode for command phase
  44. SPI_LL_IO_MODE_QUAD, ///< 4-bit mode for data phases only, 1-bit mode for command and address phases
  45. } spi_ll_io_mode_t;
  46. /*------------------------------------------------------------------------------
  47. * Control
  48. *----------------------------------------------------------------------------*/
  49. /**
  50. * Initialize SPI peripheral (master).
  51. *
  52. * @param hw Beginning address of the peripheral registers.
  53. */
  54. static inline void spi_ll_master_init(spi_dev_t *hw)
  55. {
  56. //Reset DMA
  57. hw->dma_conf.val |= SPI_LL_RST_MASK;
  58. hw->dma_out_link.start = 0;
  59. hw->dma_in_link.start = 0;
  60. hw->dma_conf.val &= ~SPI_LL_RST_MASK;
  61. //Reset timing
  62. hw->ctrl2.val = 0;
  63. //use all 64 bytes of the buffer
  64. hw->user.usr_miso_highpart = 0;
  65. hw->user.usr_mosi_highpart = 0;
  66. //Disable unneeded ints
  67. hw->slave.val &= ~SPI_LL_UNUSED_INT_MASK;
  68. }
  69. /**
  70. * Initialize SPI peripheral (slave).
  71. *
  72. * @param hw Beginning address of the peripheral registers.
  73. */
  74. static inline void spi_ll_slave_init(spi_dev_t *hw)
  75. {
  76. //Configure slave
  77. hw->clock.val = 0;
  78. hw->user.val = 0;
  79. hw->ctrl.val = 0;
  80. hw->slave.wr_rd_buf_en = 1; //no sure if needed
  81. hw->user.doutdin = 1; //we only support full duplex
  82. hw->user.sio = 0;
  83. hw->slave.slave_mode = 1;
  84. hw->dma_conf.val |= SPI_LL_RST_MASK;
  85. hw->dma_out_link.start = 0;
  86. hw->dma_in_link.start = 0;
  87. hw->dma_conf.val &= ~SPI_LL_RST_MASK;
  88. hw->slave.sync_reset = 1;
  89. hw->slave.sync_reset = 0;
  90. //use all 64 bytes of the buffer
  91. hw->user.usr_miso_highpart = 0;
  92. hw->user.usr_mosi_highpart = 0;
  93. //Disable unneeded ints
  94. hw->slave.val &= ~SPI_LL_UNUSED_INT_MASK;
  95. }
  96. /**
  97. * Reset TX and RX DMAs.
  98. *
  99. * @param hw Beginning address of the peripheral registers.
  100. */
  101. static inline void spi_ll_reset_dma(spi_dev_t *hw)
  102. {
  103. //Reset DMA peripheral
  104. hw->dma_conf.val |= SPI_LL_RST_MASK;
  105. hw->dma_out_link.start = 0;
  106. hw->dma_in_link.start = 0;
  107. hw->dma_conf.val &= ~SPI_LL_RST_MASK;
  108. hw->dma_conf.out_data_burst_en = 1;
  109. hw->dma_conf.indscr_burst_en = 1;
  110. hw->dma_conf.outdscr_burst_en = 1;
  111. }
  112. /**
  113. * Start RX DMA.
  114. *
  115. * @param hw Beginning address of the peripheral registers.
  116. * @param addr Address of the beginning DMA descriptor.
  117. */
  118. static inline void spi_ll_rxdma_start(spi_dev_t *hw, lldesc_t *addr)
  119. {
  120. hw->dma_in_link.addr = (int) addr & 0xFFFFF;
  121. hw->dma_in_link.start = 1;
  122. }
  123. /**
  124. * Start TX DMA.
  125. *
  126. * @param hw Beginning address of the peripheral registers.
  127. * @param addr Address of the beginning DMA descriptor.
  128. */
  129. static inline void spi_ll_txdma_start(spi_dev_t *hw, lldesc_t *addr)
  130. {
  131. hw->dma_out_link.addr = (int) addr & 0xFFFFF;
  132. hw->dma_out_link.start = 1;
  133. }
  134. /**
  135. * Write to SPI buffer.
  136. *
  137. * @param hw Beginning address of the peripheral registers.
  138. * @param buffer_to_send Data address to copy to the buffer.
  139. * @param bitlen Length to copy, in bits.
  140. */
  141. static inline void spi_ll_write_buffer(spi_dev_t *hw, const uint8_t *buffer_to_send, size_t bitlen)
  142. {
  143. for (int x = 0; x < bitlen; x += 32) {
  144. //Use memcpy to get around alignment issues for txdata
  145. uint32_t word;
  146. memcpy(&word, &buffer_to_send[x / 8], 4);
  147. hw->data_buf[(x / 32)] = word;
  148. }
  149. }
  150. /**
  151. * Read from SPI buffer.
  152. *
  153. * @param hw Beginning address of the peripheral registers.
  154. * @param buffer_to_rcv Address to copy buffer data to.
  155. * @param bitlen Length to copy, in bits.
  156. */
  157. static inline void spi_ll_read_buffer(spi_dev_t *hw, uint8_t *buffer_to_rcv, size_t bitlen)
  158. {
  159. for (int x = 0; x < bitlen; x += 32) {
  160. //Do a memcpy to get around possible alignment issues in rx_buffer
  161. uint32_t word = hw->data_buf[x / 32];
  162. int len = bitlen - x;
  163. if (len > 32) {
  164. len = 32;
  165. }
  166. memcpy(&buffer_to_rcv[x / 8], &word, (len + 7) / 8);
  167. }
  168. }
  169. /**
  170. * Check whether user-defined transaction is done.
  171. *
  172. * @param hw Beginning address of the peripheral registers.
  173. *
  174. * @return true if transaction is done, otherwise false.
  175. */
  176. static inline bool spi_ll_usr_is_done(spi_dev_t *hw)
  177. {
  178. return hw->slave.trans_done;
  179. }
  180. /**
  181. * Trigger start of user-defined transaction.
  182. *
  183. * @param hw Beginning address of the peripheral registers.
  184. */
  185. static inline void spi_ll_user_start(spi_dev_t *hw)
  186. {
  187. hw->cmd.usr = 1;
  188. }
  189. /**
  190. * Get current running command bit-mask. (Preview)
  191. *
  192. * @param hw Beginning address of the peripheral registers.
  193. *
  194. * @return Bitmask of running command, see ``SPI_CMD_REG``. 0 if no in-flight command.
  195. */
  196. static inline uint32_t spi_ll_get_running_cmd(spi_dev_t *hw)
  197. {
  198. return hw->cmd.val;
  199. }
  200. /**
  201. * Disable the trans_done interrupt.
  202. *
  203. * @param hw Beginning address of the peripheral registers.
  204. */
  205. static inline void spi_ll_disable_int(spi_dev_t *hw)
  206. {
  207. hw->slave.trans_inten = 0;
  208. }
  209. /**
  210. * Clear the trans_done interrupt.
  211. *
  212. * @param hw Beginning address of the peripheral registers.
  213. */
  214. static inline void spi_ll_clear_int_stat(spi_dev_t *hw)
  215. {
  216. hw->slave.trans_done = 0;
  217. }
  218. /**
  219. * Set the trans_done interrupt.
  220. *
  221. * @param hw Beginning address of the peripheral registers.
  222. */
  223. static inline void spi_ll_set_int_stat(spi_dev_t *hw)
  224. {
  225. hw->slave.trans_done = 1;
  226. }
  227. /**
  228. * Enable the trans_done interrupt.
  229. *
  230. * @param hw Beginning address of the peripheral registers.
  231. */
  232. static inline void spi_ll_enable_int(spi_dev_t *hw)
  233. {
  234. hw->slave.trans_inten = 1;
  235. }
  236. /*------------------------------------------------------------------------------
  237. * Configs: mode
  238. *----------------------------------------------------------------------------*/
  239. /**
  240. * Enable/disable the postive-cs feature.
  241. *
  242. * @param hw Beginning address of the peripheral registers.
  243. * @param cs One of the CS (0-2) to enable/disable the feature.
  244. * @param pos_cs true to enable the feature, otherwise disable (default).
  245. */
  246. static inline void spi_ll_master_set_pos_cs(spi_dev_t *hw, int cs, uint32_t pos_cs)
  247. {
  248. if (pos_cs) {
  249. hw->pin.master_cs_pol |= (1 << cs);
  250. } else {
  251. hw->pin.master_cs_pol &= ~(1 << cs);
  252. }
  253. }
  254. /**
  255. * Enable/disable the LSBFIRST feature for TX data.
  256. *
  257. * @param hw Beginning address of the peripheral registers.
  258. * @param lsbfirst true if LSB of TX data to be sent first, otherwise MSB is sent first (default).
  259. */
  260. static inline void spi_ll_set_tx_lsbfirst(spi_dev_t *hw, bool lsbfirst)
  261. {
  262. hw->ctrl.wr_bit_order = lsbfirst;
  263. }
  264. /**
  265. * Enable/disable the LSBFIRST feature for RX data.
  266. *
  267. * @param hw Beginning address of the peripheral registers.
  268. * @param lsbfirst true if first bit received as LSB, otherwise as MSB (default).
  269. */
  270. static inline void spi_ll_set_rx_lsbfirst(spi_dev_t *hw, bool lsbfirst)
  271. {
  272. hw->ctrl.rd_bit_order = lsbfirst;
  273. }
  274. /**
  275. * Set SPI mode for the peripheral as master.
  276. *
  277. * @param hw Beginning address of the peripheral registers.
  278. * @param mode SPI mode to work at, 0-3.
  279. */
  280. static inline void spi_ll_master_set_mode(spi_dev_t *hw, uint8_t mode)
  281. {
  282. //Configure polarity
  283. if (mode == 0) {
  284. hw->pin.ck_idle_edge = 0;
  285. hw->user.ck_out_edge = 0;
  286. } else if (mode == 1) {
  287. hw->pin.ck_idle_edge = 0;
  288. hw->user.ck_out_edge = 1;
  289. } else if (mode == 2) {
  290. hw->pin.ck_idle_edge = 1;
  291. hw->user.ck_out_edge = 1;
  292. } else if (mode == 3) {
  293. hw->pin.ck_idle_edge = 1;
  294. hw->user.ck_out_edge = 0;
  295. }
  296. }
  297. /**
  298. * Set SPI mode for the peripheral as slave.
  299. *
  300. * @param hw Beginning address of the peripheral registers.
  301. * @param mode SPI mode to work at, 0-3.
  302. */
  303. static inline void spi_ll_slave_set_mode(spi_dev_t *hw, const int mode, bool dma_used)
  304. {
  305. if (mode == 0) {
  306. //The timing needs to be fixed to meet the requirements of DMA
  307. hw->pin.ck_idle_edge = 1;
  308. hw->user.ck_i_edge = 0;
  309. hw->ctrl2.miso_delay_mode = 0;
  310. hw->ctrl2.miso_delay_num = 0;
  311. hw->ctrl2.mosi_delay_mode = 2;
  312. hw->ctrl2.mosi_delay_num = 2;
  313. } else if (mode == 1) {
  314. hw->pin.ck_idle_edge = 1;
  315. hw->user.ck_i_edge = 1;
  316. hw->ctrl2.miso_delay_mode = 2;
  317. hw->ctrl2.miso_delay_num = 0;
  318. hw->ctrl2.mosi_delay_mode = 0;
  319. hw->ctrl2.mosi_delay_num = 0;
  320. } else if (mode == 2) {
  321. //The timing needs to be fixed to meet the requirements of DMA
  322. hw->pin.ck_idle_edge = 0;
  323. hw->user.ck_i_edge = 1;
  324. hw->ctrl2.miso_delay_mode = 0;
  325. hw->ctrl2.miso_delay_num = 0;
  326. hw->ctrl2.mosi_delay_mode = 1;
  327. hw->ctrl2.mosi_delay_num = 2;
  328. } else if (mode == 3) {
  329. hw->pin.ck_idle_edge = 0;
  330. hw->user.ck_i_edge = 0;
  331. hw->ctrl2.miso_delay_mode = 1;
  332. hw->ctrl2.miso_delay_num = 0;
  333. hw->ctrl2.mosi_delay_mode = 0;
  334. hw->ctrl2.mosi_delay_num = 0;
  335. }
  336. /* Silicon issues exists in mode 0 and 2 with DMA, change clock phase to
  337. * avoid dma issue. This will cause slave output to appear at most half a
  338. * spi clock before
  339. */
  340. if (dma_used) {
  341. if (mode == 0) {
  342. hw->pin.ck_idle_edge = 0;
  343. hw->user.ck_i_edge = 1;
  344. hw->ctrl2.miso_delay_mode = 0;
  345. hw->ctrl2.miso_delay_num = 2;
  346. hw->ctrl2.mosi_delay_mode = 0;
  347. hw->ctrl2.mosi_delay_num = 3;
  348. } else if (mode == 2) {
  349. hw->pin.ck_idle_edge = 1;
  350. hw->user.ck_i_edge = 0;
  351. hw->ctrl2.miso_delay_mode = 0;
  352. hw->ctrl2.miso_delay_num = 2;
  353. hw->ctrl2.mosi_delay_mode = 0;
  354. hw->ctrl2.mosi_delay_num = 3;
  355. }
  356. }
  357. }
  358. /**
  359. * Set SPI to work in full duplex or half duplex mode.
  360. *
  361. * @param hw Beginning address of the peripheral registers.
  362. * @param half_duplex true to work in half duplex mode, otherwise in full duplex mode.
  363. */
  364. static inline void spi_ll_set_half_duplex(spi_dev_t *hw, bool half_duplex)
  365. {
  366. hw->user.doutdin = !half_duplex;
  367. }
  368. /**
  369. * Set SPI to work in SIO mode or not.
  370. *
  371. * SIO is a mode which MOSI and MISO share a line. The device MUST work in half-duplexmode.
  372. *
  373. * @param hw Beginning address of the peripheral registers.
  374. * @param sio_mode true to work in SIO mode, otherwise false.
  375. */
  376. static inline void spi_ll_set_sio_mode(spi_dev_t *hw, int sio_mode)
  377. {
  378. hw->user.sio = sio_mode;
  379. }
  380. /**
  381. * Configure the io mode for the master to work at.
  382. *
  383. * @param hw Beginning address of the peripheral registers.
  384. * @param io_mode IO mode to work at, see ``spi_ll_io_mode_t``.
  385. */
  386. static inline void spi_ll_master_set_io_mode(spi_dev_t *hw, spi_ll_io_mode_t io_mode)
  387. {
  388. hw->ctrl.val &= ~(SPI_FREAD_DUAL | SPI_FREAD_QUAD | SPI_FREAD_DIO | SPI_FREAD_QIO);
  389. hw->user.val &= ~(SPI_FWRITE_DUAL | SPI_FWRITE_QUAD | SPI_FWRITE_DIO | SPI_FWRITE_QIO);
  390. switch (io_mode) {
  391. case SPI_LL_IO_MODE_DIO:
  392. hw->ctrl.fread_dio = 1;
  393. hw->user.fwrite_dio = 1;
  394. break;
  395. case SPI_LL_IO_MODE_DUAL:
  396. hw->ctrl.fread_dual = 1;
  397. hw->user.fwrite_dual = 1;
  398. break;
  399. case SPI_LL_IO_MODE_QIO:
  400. hw->ctrl.fread_qio = 1;
  401. hw->user.fwrite_qio = 1;
  402. break;
  403. case SPI_LL_IO_MODE_QUAD:
  404. hw->ctrl.fread_quad = 1;
  405. hw->user.fwrite_quad = 1;
  406. break;
  407. default:
  408. break;
  409. };
  410. if (io_mode != SPI_LL_IO_MODE_NORMAL) {
  411. hw->ctrl.fastrd_mode = 1;
  412. }
  413. }
  414. /**
  415. * Select one of the CS to use in current transaction.
  416. *
  417. * @param hw Beginning address of the peripheral registers.
  418. * @param cs_id The cs to use, 0-2, otherwise none of them is used.
  419. */
  420. static inline void spi_ll_master_select_cs(spi_dev_t *hw, int cs_id)
  421. {
  422. hw->pin.cs0_dis = (cs_id == 0) ? 0 : 1;
  423. hw->pin.cs1_dis = (cs_id == 1) ? 0 : 1;
  424. hw->pin.cs2_dis = (cs_id == 2) ? 0 : 1;
  425. }
  426. /*------------------------------------------------------------------------------
  427. * Configs: parameters
  428. *----------------------------------------------------------------------------*/
  429. /**
  430. * Set the clock for master by stored value.
  431. *
  432. * @param hw Beginning address of the peripheral registers.
  433. * @param val stored clock configuration calculated before (by ``spi_ll_cal_clock``).
  434. */
  435. static inline void spi_ll_master_set_clock_by_reg(spi_dev_t *hw, spi_ll_clock_val_t *val)
  436. {
  437. hw->clock.val = *(uint32_t *)val;
  438. }
  439. /**
  440. * Get the frequency of given dividers. Don't use in app.
  441. *
  442. * @param fapb APB clock of the system.
  443. * @param pre Pre devider.
  444. * @param n main divider.
  445. *
  446. * @return Frequency of given dividers.
  447. */
  448. static inline int spi_ll_freq_for_pre_n(int fapb, int pre, int n)
  449. {
  450. return (fapb / (pre * n));
  451. }
  452. /**
  453. * Calculate the nearest frequency avaliable for master.
  454. *
  455. * @param fapb APB clock of the system.
  456. * @param hz Frequncy desired.
  457. * @param duty_cycle Duty cycle desired.
  458. * @param out_reg Output address to store the calculated clock configurations for the return frequency.
  459. *
  460. * @return Actual (nearest) frequency.
  461. */
  462. static inline int spi_ll_master_cal_clock(int fapb, int hz, int duty_cycle, spi_ll_clock_val_t *out_reg)
  463. {
  464. typeof(SPI1.clock) reg;
  465. int eff_clk;
  466. //In hw, n, h and l are 1-64, pre is 1-8K. Value written to register is one lower than used value.
  467. if (hz > ((fapb / 4) * 3)) {
  468. //Using Fapb directly will give us the best result here.
  469. reg.clkcnt_l = 0;
  470. reg.clkcnt_h = 0;
  471. reg.clkcnt_n = 0;
  472. reg.clkdiv_pre = 0;
  473. reg.clk_equ_sysclk = 1;
  474. eff_clk = fapb;
  475. } else {
  476. //For best duty cycle resolution, we want n to be as close to 32 as possible, but
  477. //we also need a pre/n combo that gets us as close as possible to the intended freq.
  478. //To do this, we bruteforce n and calculate the best pre to go along with that.
  479. //If there's a choice between pre/n combos that give the same result, use the one
  480. //with the higher n.
  481. int pre, n, h, l;
  482. int bestn = -1;
  483. int bestpre = -1;
  484. int besterr = 0;
  485. int errval;
  486. 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.
  487. //Effectively, this does pre=round((fapb/n)/hz).
  488. pre = ((fapb / n) + (hz / 2)) / hz;
  489. if (pre <= 0) {
  490. pre = 1;
  491. }
  492. if (pre > 8192) {
  493. pre = 8192;
  494. }
  495. errval = abs(spi_ll_freq_for_pre_n(fapb, pre, n) - hz);
  496. if (bestn == -1 || errval <= besterr) {
  497. besterr = errval;
  498. bestn = n;
  499. bestpre = pre;
  500. }
  501. }
  502. n = bestn;
  503. pre = bestpre;
  504. l = n;
  505. //This effectively does round((duty_cycle*n)/256)
  506. h = (duty_cycle * n + 127) / 256;
  507. if (h <= 0) {
  508. h = 1;
  509. }
  510. reg.clk_equ_sysclk = 0;
  511. reg.clkcnt_n = n - 1;
  512. reg.clkdiv_pre = pre - 1;
  513. reg.clkcnt_h = h - 1;
  514. reg.clkcnt_l = l - 1;
  515. eff_clk = spi_ll_freq_for_pre_n(fapb, pre, n);
  516. }
  517. if (out_reg != NULL) {
  518. *(uint32_t *)out_reg = reg.val;
  519. }
  520. return eff_clk;
  521. }
  522. /**
  523. * Calculate and set clock for SPI master according to desired parameters.
  524. *
  525. * This takes long, suggest to calculate the configuration during
  526. * initialization by ``spi_ll_master_cal_clock`` and store the result, then
  527. * configure the clock by stored value when used by
  528. * ``spi_ll_msater_set_clock_by_reg``.
  529. *
  530. * @param hw Beginning address of the peripheral registers.
  531. * @param fapb APB clock of the system.
  532. * @param hz Frequncy desired.
  533. * @param duty_cycle Duty cycle desired.
  534. *
  535. * @return Actual frequency that is used.
  536. */
  537. static inline int spi_ll_master_set_clock(spi_dev_t *hw, int fapb, int hz, int duty_cycle)
  538. {
  539. spi_ll_clock_val_t reg_val;
  540. int freq = spi_ll_master_cal_clock(fapb, hz, duty_cycle, &reg_val);
  541. spi_ll_master_set_clock_by_reg(hw, &reg_val);
  542. return freq;
  543. }
  544. /**
  545. * Enable/disable the CK sel feature for a CS pin.
  546. *
  547. * CK sel is a feature to toggle the CS line along with the clock.
  548. *
  549. * @param hw Beginning address of the peripheral registers.
  550. * @param cs CS pin to enable/disable the feature, 0-2.
  551. * @param cksel true to enable the feature, otherwise false.
  552. */
  553. static inline void spi_ll_master_set_cksel(spi_dev_t *hw, int cs, uint32_t cksel)
  554. {
  555. if (cksel) {
  556. hw->pin.master_ck_sel |= (1 << cs);
  557. } else {
  558. hw->pin.master_ck_sel &= ~(1 << cs);
  559. }
  560. }
  561. /**
  562. * Set the mosi delay after the output edge to the signal. (Preview)
  563. *
  564. * The delay mode/num is a Espressif conception, may change in the new chips.
  565. *
  566. * @param hw Beginning address of the peripheral registers.
  567. * @param delay_mode Delay mode, see TRM.
  568. * @param delay_num APB clocks to delay.
  569. */
  570. static inline void spi_ll_set_mosi_delay(spi_dev_t *hw, int delay_mode, int delay_num)
  571. {
  572. hw->ctrl2.mosi_delay_mode = delay_mode;
  573. hw->ctrl2.mosi_delay_num = delay_num;
  574. }
  575. /**
  576. * Set the miso delay applied to the input signal before the internal peripheral. (Preview)
  577. *
  578. * The delay mode/num is a Espressif conception, may change in the new chips.
  579. *
  580. * @param hw Beginning address of the peripheral registers.
  581. * @param delay_mode Delay mode, see TRM.
  582. * @param delay_num APB clocks to delay.
  583. */
  584. static inline void spi_ll_set_miso_delay(spi_dev_t *hw, int delay_mode, int delay_num)
  585. {
  586. hw->ctrl2.miso_delay_mode = delay_mode;
  587. hw->ctrl2.miso_delay_num = delay_num;
  588. }
  589. /**
  590. * Set dummy clocks to output before RX phase (master), or clocks to skip
  591. * before the data phase and after the address phase (slave).
  592. *
  593. * Note this phase is also used to compensate RX timing in half duplex mode.
  594. *
  595. * @param hw Beginning address of the peripheral registers.
  596. * @param dummy_n Dummy cycles used. 0 to disable the dummy phase.
  597. */
  598. static inline void spi_ll_set_dummy(spi_dev_t *hw, int dummy_n)
  599. {
  600. hw->user.usr_dummy = dummy_n ? 1 : 0;
  601. hw->user1.usr_dummy_cyclelen = dummy_n - 1;
  602. }
  603. /**
  604. * Set the delay of SPI clocks before the CS inactive edge after the last SPI clock.
  605. *
  606. * @param hw Beginning address of the peripheral registers.
  607. * @param hold Delay of SPI clocks after the last clock, 0 to disable the hold phase.
  608. */
  609. static inline void spi_ll_master_set_cs_hold(spi_dev_t *hw, int hold)
  610. {
  611. hw->ctrl2.hold_time = hold;
  612. hw->user.cs_hold = hold ? 1 : 0;
  613. }
  614. /**
  615. * Set the delay of SPI clocks before the first SPI clock after the CS active edge.
  616. *
  617. * Note ESP32 doesn't support to use this feature when command/address phases
  618. * are used in full duplex mode.
  619. *
  620. * @param hw Beginning address of the peripheral registers.
  621. * @param setup Delay of SPI clocks after the CS active edge, 0 to disable the setup phase.
  622. */
  623. static inline void spi_ll_master_set_cs_setup(spi_dev_t *hw, uint8_t setup)
  624. {
  625. hw->ctrl2.setup_time = setup - 1;
  626. hw->user.cs_setup = setup ? 1 : 0;
  627. }
  628. /*------------------------------------------------------------------------------
  629. * Configs: data
  630. *----------------------------------------------------------------------------*/
  631. /**
  632. * Set the input length (master).
  633. *
  634. * @param hw Beginning address of the peripheral registers.
  635. * @param bitlen input length, in bits.
  636. */
  637. static inline void spi_ll_set_miso_bitlen(spi_dev_t *hw, size_t bitlen)
  638. {
  639. hw->miso_dlen.usr_miso_dbitlen = bitlen - 1;
  640. }
  641. /**
  642. * Set the output length (master).
  643. *
  644. * @param hw Beginning address of the peripheral registers.
  645. * @param bitlen output length, in bits.
  646. */
  647. static inline void spi_ll_set_mosi_bitlen(spi_dev_t *hw, size_t bitlen)
  648. {
  649. hw->mosi_dlen.usr_mosi_dbitlen = bitlen - 1;
  650. }
  651. /**
  652. * Set the maximum input length (slave).
  653. *
  654. * @param hw Beginning address of the peripheral registers.
  655. * @param bitlen input length, in bits.
  656. */
  657. static inline void spi_ll_slave_set_rx_bitlen(spi_dev_t *hw, size_t bitlen)
  658. {
  659. hw->slv_wrbuf_dlen.bit_len = bitlen - 1;
  660. }
  661. /**
  662. * Set the maximum output length (slave).
  663. *
  664. * @param hw Beginning address of the peripheral registers.
  665. * @param bitlen output length, in bits.
  666. */
  667. static inline void spi_ll_slave_set_tx_bitlen(spi_dev_t *hw, size_t bitlen)
  668. {
  669. hw->slv_rdbuf_dlen.bit_len = bitlen - 1;
  670. }
  671. /**
  672. * Set the length of command phase.
  673. *
  674. * When in 4-bit mode, the SPI cycles of the phase will be shorter. E.g. 16-bit
  675. * command phases takes 4 cycles in 4-bit mode.
  676. *
  677. * @param hw Beginning address of the peripheral registers.
  678. * @param bitlen Length of command phase, in bits. 0 to disable the command phase.
  679. */
  680. static inline void spi_ll_set_command_bitlen(spi_dev_t *hw, int bitlen)
  681. {
  682. hw->user2.usr_command_bitlen = bitlen - 1;
  683. hw->user.usr_command = bitlen ? 1 : 0;
  684. }
  685. /**
  686. * Set the length of address phase.
  687. *
  688. * When in 4-bit mode, the SPI cycles of the phase will be shorter. E.g. 16-bit
  689. * address phases takes 4 cycles in 4-bit mode.
  690. *
  691. * @param hw Beginning address of the peripheral registers.
  692. * @param bitlen Length of address phase, in bits. 0 to disable the address phase.
  693. */
  694. static inline void spi_ll_set_addr_bitlen(spi_dev_t *hw, int bitlen)
  695. {
  696. hw->user1.usr_addr_bitlen = bitlen - 1;
  697. hw->user.usr_addr = bitlen ? 1 : 0;
  698. }
  699. /**
  700. * Set the address value in an intuitive way.
  701. *
  702. * The length and lsbfirst is required to shift and swap the address to the right place.
  703. *
  704. * @param hw Beginning address of the peripheral registers.
  705. * @param address Address to set
  706. * @param addrlen Length of the address phase
  707. * @param lsbfirst whether the LSB first feature is enabled.
  708. */
  709. static inline void spi_ll_set_address(spi_dev_t *hw, uint64_t addr, int addrlen, uint32_t lsbfirst)
  710. {
  711. if (lsbfirst) {
  712. /* The output address start from the LSB of the highest byte, i.e.
  713. * addr[24] -> addr[31]
  714. * ...
  715. * addr[0] -> addr[7]
  716. * slv_wr_status[24] -> slv_wr_status[31]
  717. * ...
  718. * slv_wr_status[0] -> slv_wr_status[7]
  719. * So swap the byte order to let the LSB sent first.
  720. */
  721. addr = HAL_SWAP64(addr);
  722. hw->addr = addr >> 32;
  723. hw->slv_wr_status = addr;
  724. } else {
  725. // shift the address to MSB of addr (and maybe slv_wr_status) register.
  726. // output address will be sent from MSB to LSB of addr register, then comes the MSB to LSB of slv_wr_status register.
  727. if (addrlen > 32) {
  728. hw->addr = addr >> (addrlen - 32);
  729. hw->slv_wr_status = addr << (64 - addrlen);
  730. } else {
  731. hw->addr = addr << (32 - addrlen);
  732. }
  733. }
  734. }
  735. /**
  736. * Set the command value in an intuitive way.
  737. *
  738. * The length and lsbfirst is required to shift and swap the command to the right place.
  739. *
  740. * @param hw Beginning command of the peripheral registers.
  741. * @param command Command to set
  742. * @param addrlen Length of the command phase
  743. * @param lsbfirst whether the LSB first feature is enabled.
  744. */
  745. static inline void spi_ll_set_command(spi_dev_t *hw, uint16_t cmd, int cmdlen, bool lsbfirst)
  746. {
  747. if (lsbfirst) {
  748. // The output command start from bit0 to bit 15, kept as is.
  749. hw->user2.usr_command_value = cmd;
  750. } else {
  751. /* Output command will be sent from bit 7 to 0 of command_value, and
  752. * then bit 15 to 8 of the same register field. Shift and swap to send
  753. * more straightly.
  754. */
  755. hw->user2.usr_command_value = HAL_SPI_SWAP_DATA_TX(cmd, cmdlen);
  756. }
  757. }
  758. /**
  759. * Enable/disable the RX data phase.
  760. *
  761. * @param hw Beginning address of the peripheral registers.
  762. * @param enable true if RX phase exist, otherwise false.
  763. */
  764. static inline void spi_ll_enable_miso(spi_dev_t *hw, int enable)
  765. {
  766. hw->user.usr_miso = enable;
  767. }
  768. /**
  769. * Enable/disable the TX data phase.
  770. *
  771. * @param hw Beginning address of the peripheral registers.
  772. * @param enable true if TX phase exist, otherwise false.
  773. */
  774. static inline void spi_ll_enable_mosi(spi_dev_t *hw, int enable)
  775. {
  776. hw->user.usr_mosi = enable;
  777. }
  778. /**
  779. * Reset the slave peripheral before next transaction.
  780. *
  781. * @param hw Beginning address of the peripheral registers.
  782. */
  783. static inline void spi_ll_slave_reset(spi_dev_t *hw)
  784. {
  785. hw->slave.sync_reset = 1;
  786. hw->slave.sync_reset = 0;
  787. }
  788. /**
  789. * Get the received bit length of the slave.
  790. *
  791. * @param hw Beginning address of the peripheral registers.
  792. *
  793. * @return Received bits of the slave.
  794. */
  795. static inline uint32_t spi_ll_slave_get_rcv_bitlen(spi_dev_t *hw)
  796. {
  797. return hw->slv_rd_bit.slv_rdata_bit;
  798. }
  799. #undef SPI_LL_RST_MASK
  800. #undef SPI_LL_UNUSED_INT_MASK