driver_spi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * @File name : driver_spi.c
  3. * @Author : Bluetrum IOT Team
  4. * @Date : 2023-02-15
  5. * @Description : This file provides functions to manage the most functionalities
  6. * of the SPI peripheral.
  7. *
  8. * Copyright (c) by Bluetrum, All Rights Reserved.
  9. */
  10. #include "driver_spi.h"
  11. /**
  12. * @brief Initializes the spix peripheral according to the specified
  13. * parameters in the spi_init_struct.
  14. * @param spix: where x can be (0..1) to select the SPI peripheral.
  15. * @param spi_init_struct: pointer to a spi_init_typedef structure that
  16. * contains the configuration information for the specified SPI peripheral.
  17. * @retval None
  18. */
  19. void spi_init(spi_typedef* spix, spi_init_typedef* spi_init_struct)
  20. {
  21. uint32_t temp = 0;
  22. /* SPI_CLK freq configuration */
  23. spix->baud = sys_clk_nhz_get() / spi_init_struct->baud - 1;
  24. temp |= (uint32_t)(spi_init_struct->bus_mode & SPIxCON_BUSMODE);
  25. temp |= (uint32_t)(spi_init_struct->role_mode & SPIxCON_SPISM);
  26. temp |= (uint32_t)(spi_init_struct->output_data_edge & SPIxCON_SMPS);
  27. temp |= (uint32_t)(spi_init_struct->sample_clk_edge & SPIxCON_SPIOSS);
  28. temp |= (uint32_t)(spi_init_struct->clkids & SPIxCON_CLKIDS);
  29. spix->con = temp;
  30. }
  31. /**
  32. * @brief De-initialize the specified SPI peripheral.
  33. * @param spix: where x can be (0..1) to select the SPI peripheral.
  34. * @retval None
  35. */
  36. void spi_deinit(spi_typedef* spix)
  37. {
  38. if (spix == SPI0) {
  39. /* Assert: This release disables disabling SPI0 peripherals using this API */
  40. } else if (spix == SPI1) {
  41. spix->con = 0;
  42. clk_gate1_cmd(CLK_GATE1_SPI1, CLK_DIS);
  43. }
  44. }
  45. /**
  46. * @brief Enable or disable the specified SPI peripheral.
  47. * @param spix: where x can be (0..1) to select the SPI peripheral.
  48. * @param state: state of the SPIx peripheral.
  49. This parameter can be: ENABLE or DISABLE.
  50. * @retval None
  51. */
  52. void spi_cmd(spi_typedef* spix, FUNCTIONAL_STATE state)
  53. {
  54. if (state) {
  55. spix->con |= (uint32_t)SPIxCON_SPIEN;
  56. } else {
  57. spix->con &= (uint32_t)~SPIxCON_SPIEN;
  58. }
  59. }
  60. /**
  61. * @brief Select the specified SPI direction when in DMA mode or 2-wire mode.
  62. * @param spix: where x can be (0..1) to select the SPI peripheral.
  63. * @param direction: transmit or receive.
  64. This parameter can be: SPI_DIR_TX or SPI_DIR_RX.
  65. * @retval None
  66. */
  67. void spi_dir_sel(spi_typedef* spix, SPI_DIR_TYPEDEF direction)
  68. {
  69. if(direction == SPI_DIR_RX) {
  70. spix->con |= BIT(4);
  71. } else {
  72. spix->con &= ~BIT(4);
  73. }
  74. }
  75. /**
  76. * @brief Enable or disable the specified SPI interrupts.
  77. * @param spix: where x can be (0..1) to select the SPI peripheral.
  78. * @param state: state of the SPIx interrupts.
  79. This parameter can be: ENABLE or DISABLE.
  80. * @retval None
  81. */
  82. void spi_it_cmd(spi_typedef* spix, FUNCTIONAL_STATE state)
  83. {
  84. if (state) {
  85. spix->con |= (uint32_t)SPIxCON_SPIIE;
  86. } else {
  87. spix->con &= (uint32_t)~SPIxCON_SPIIE;
  88. }
  89. }
  90. /**
  91. * @brief Enable or disable the specified SPI LFSR.
  92. * @param spix: where x can be (0..1) to select the SPI peripheral.
  93. * @param state: state of the SPIx interrupts.
  94. This parameter can be: ENABLE or DISABLE.
  95. * @retval None
  96. */
  97. void spi_lfsr_cmd(spi_typedef* spix, FUNCTIONAL_STATE state)
  98. {
  99. if (state) {
  100. spix->con |= (uint32_t)SPIxCON_SPILF_EN;
  101. } else {
  102. spix->con &= (uint32_t)~SPIxCON_SPILF_EN;
  103. }
  104. }
  105. /**
  106. * @brief Enable or disable the specified SPI multiple bit bus.
  107. * @param spix: where x can be (0..1) to select the SPI peripheral.
  108. * @param state: state of the SPIx interrupts.
  109. This parameter can be: ENABLE or DISABLE.
  110. * @retval None
  111. */
  112. void spi_multi_bit_bus_cmd(spi_typedef* spix, FUNCTIONAL_STATE state)
  113. {
  114. if (state) {
  115. spix->con |= (uint32_t)SPIxCON_SPIMBEN;
  116. } else {
  117. spix->con &= (uint32_t)~SPIxCON_SPIMBEN;
  118. }
  119. }
  120. /**
  121. * @brief Enable or disable the specified SPI hold when bt rx.
  122. * @param spix: where x can be (0..1) to select the SPI peripheral.
  123. * @param state: state of the SPIx interrupts.
  124. This parameter can be: ENABLE or DISABLE.
  125. * @retval None
  126. */
  127. void spi_hold_rx_cmd(spi_typedef* spix, FUNCTIONAL_STATE state)
  128. {
  129. if (state) {
  130. spix->con |= (uint32_t)SPIxCON_HOLDENRX;
  131. } else {
  132. spix->con &= (uint32_t)~SPIxCON_HOLDENRX;
  133. }
  134. }
  135. /**
  136. * @brief Enable or disable the specified SPI hold when bt tx.
  137. * @param spix: where x can be (0..1) to select the SPI peripheral.
  138. * @param state: state of the SPIx interrupts.
  139. This parameter can be: ENABLE or DISABLE.
  140. * @retval None
  141. */
  142. void spi_hold_tx_cmd(spi_typedef* spix, FUNCTIONAL_STATE state)
  143. {
  144. if (state) {
  145. spix->con |= (uint32_t)SPIxCON_HOLDENTX;
  146. } else {
  147. spix->con &= (uint32_t)~SPIxCON_HOLDENTX;
  148. }
  149. }
  150. /**
  151. * @brief Enable or disable the specified SPI software hold.
  152. * @param spix: where x can be (0..1) to select the SPI peripheral.
  153. * @param state: state of the SPIx interrupts.
  154. This parameter can be: ENABLE or DISABLE.
  155. * @retval None
  156. */
  157. void spi_soft_hold_cmd(spi_typedef* spix, FUNCTIONAL_STATE state)
  158. {
  159. if (state) {
  160. spix->con |= (uint32_t)SPIxCON_HOLDENSW;
  161. } else {
  162. spix->con &= (uint32_t)~SPIxCON_HOLDENSW;
  163. }
  164. }
  165. /**
  166. * @brief Get the SPI pending.
  167. * @param spix: where x can be (0..1) to select the SPI peripheral.
  168. * @param spi_flag: specifies the flag to set.
  169. * this parameter can be one of the following values:
  170. * @arg SPI_FLAG_PENDING: SPI(0..1) SPI pending.
  171. * @retval The state of spi_flag (SET or RESET).
  172. */
  173. AT(.com_periph.spi.get)
  174. FLAG_STATE spi_get_flag(spi_typedef* spix, uint32_t spi_flag)
  175. {
  176. if ((spix->con & spi_flag) != RESET) {
  177. return SET;
  178. } else {
  179. return RESET;
  180. }
  181. }
  182. /**
  183. * @brief Clear the SPI pending.
  184. * @param spix: where x can be (0..1) to select the SPI peripheral.
  185. * @param spi_flag: specifies the flag to clear.
  186. * this parameter can be one of the following values:
  187. * @arg SPI_FLAG_PENDING: SPI(0..1) SPI pending.
  188. * @retval None
  189. */
  190. AT(.com_periph.spi.clear)
  191. void spi_clear_flag(spi_typedef* spix, uint32_t spi_flag)
  192. {
  193. spix->con |= (uint32_t)spi_flag;
  194. }
  195. /**
  196. * @brief Set the SPI counter register value.
  197. * @param spix: where x can be (0..1) to select the SPI peripheral.
  198. * @param cnt: specifies the counter register new value.
  199. * @retval None
  200. */
  201. void spi_set_dma_cnt(spi_typedef* spix, uint32_t cnt)
  202. {
  203. spix->dma_cnt = cnt;
  204. }
  205. /**
  206. * @brief Set the SPI DMA address register value.
  207. * @param spix: where x can be (0..1) to select the SPI peripheral.
  208. * @param addr: specifies the SPI DMA address register new value.
  209. * @retval None
  210. */
  211. void spi_set_dma_addr(spi_typedef* spix, uint32_t addr)
  212. {
  213. spix->dma_adr = (uint32_t)addr;
  214. }
  215. /**
  216. * @brief Get the spi receive data.
  217. * @param spix: where x can be (0..1) to select the SPI peripheral.
  218. * @retval spi receive data.
  219. */
  220. uint8_t spi_receive_data(spi_typedef* spix)
  221. {
  222. return (uint8_t)spix->buf;
  223. }
  224. /**
  225. * @brief Send the spi receive data.
  226. * @param spix: where x can be (0..1) to select the SPI peripheral.
  227. * @param data: specifies the SPI data to be sent.
  228. * @retval None.
  229. */
  230. void spi_send_data(spi_typedef* spix, uint8_t data)
  231. {
  232. spix->buf = data;
  233. }