hpm_spi_drv.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_SPI_DRV_H
  8. #define HPM_SPI_DRV_H
  9. #include "hpm_spi_regs.h"
  10. #include "hpm_soc_feature.h"
  11. /**
  12. * @brief SPI driver APIs
  13. * @defgroup spi_interface SPI driver APIs
  14. * @ingroup io_interfaces
  15. * @{
  16. */
  17. /**
  18. * @brief spi dma enable
  19. */
  20. typedef enum {
  21. spi_tx_dma_enable = SPI_CTRL_TXDMAEN_MASK,
  22. spi_rx_dma_enable = SPI_CTRL_RXDMAEN_MASK
  23. } spi_dma_enable_t;
  24. /**
  25. * @brief spi interrupt mask
  26. */
  27. typedef enum {
  28. spi_rx_fifo_overflow_int = SPI_INTREN_RXFIFOORINTEN_MASK,
  29. spi_tx_fifo_underflow_int = SPI_INTREN_TXFIFOURINTEN_MASK,
  30. spi_rx_fifo_threshold_int = SPI_INTREN_RXFIFOINTEN_MASK,
  31. spi_tx_fifo_threshold_int = SPI_INTREN_TXFIFOINTEN_MASK,
  32. spi_end_int = SPI_INTREN_ENDINTEN_MASK,
  33. spi_slave_cmd_int = SPI_INTREN_SLVCMDEN_MASK,
  34. } spi_interrupt_t;
  35. /**
  36. * @brief spi mode selection
  37. */
  38. typedef enum {
  39. spi_master_mode = 0,
  40. spi_slave_mode
  41. } spi_mode_selection_t;
  42. /**
  43. * @brief spi clock polarity
  44. */
  45. typedef enum {
  46. spi_sclk_low_idle = 0,
  47. spi_sclk_high_idle
  48. } spi_sclk_idle_state_t;
  49. /**
  50. * @brief spi clock phase
  51. */
  52. typedef enum {
  53. spi_sclk_sampling_odd_clk_edges = 0,
  54. spi_sclk_sampling_even_clk_edges
  55. } spi_sclk_sampling_clk_edges_t;
  56. /**
  57. * @brief spi cs to sclk edge duration
  58. */
  59. typedef enum {
  60. spi_cs2sclk_half_sclk_1 = 0,
  61. spi_cs2sclk_half_sclk_2,
  62. spi_cs2sclk_half_sclk_3,
  63. spi_cs2sclk_half_sclk_4
  64. } spi_cs2sclk_duration_t;
  65. /**
  66. * @brief spi cs high level duration
  67. */
  68. typedef enum {
  69. spi_csht_half_sclk_1 = 0,
  70. spi_csht_half_sclk_2,
  71. spi_csht_half_sclk_3,
  72. spi_csht_half_sclk_4,
  73. spi_csht_half_sclk_5,
  74. spi_csht_half_sclk_6,
  75. spi_csht_half_sclk_7,
  76. spi_csht_half_sclk_8,
  77. spi_csht_half_sclk_9,
  78. spi_csht_half_sclk_10,
  79. spi_csht_half_sclk_11,
  80. spi_csht_half_sclk_12,
  81. spi_csht_half_sclk_13,
  82. spi_csht_half_sclk_14,
  83. spi_csht_half_sclk_15,
  84. spi_csht_half_sclk_16,
  85. } spi_csht_duration_t;
  86. /**
  87. * @brief spi address phase format
  88. */
  89. typedef enum {
  90. spi_address_phase_format_single_io_mode = 0,
  91. spi_address_phase_format_dualquad_io_mode
  92. } spi_addr_phase_format_t;
  93. /**
  94. * @brief spi transfer mode
  95. */
  96. typedef enum {
  97. spi_trans_write_read_together = 0,
  98. spi_trans_write_only,
  99. spi_trans_read_only,
  100. spi_trans_write_read,
  101. spi_trans_read_write,
  102. spi_trans_write_dummy_read,
  103. spi_trans_read_dummy_write,
  104. spi_trans_no_data,
  105. spi_trans_dummy_write,
  106. spi_trans_dummy_read
  107. } spi_trans_mode_t;
  108. /**
  109. * @brief spi data phase format
  110. */
  111. typedef enum {
  112. spi_single_io_mode = 0,
  113. spi_dual_io_mode,
  114. spi_quad_io_mode,
  115. } spi_data_phase_format_t;
  116. /**
  117. * @brief spi token value
  118. */
  119. typedef enum {
  120. spi_token_value_0x00 = 0,
  121. spi_token_value_0x69
  122. } spi_token_value_t;
  123. /**
  124. * @brief spi dummy count
  125. */
  126. typedef enum {
  127. spi_dummy_count_1 = 0,
  128. spi_dummy_count_2,
  129. spi_dummy_count_3,
  130. spi_dummy_count_4
  131. } spi_dummy_count_t;
  132. /**
  133. * @brief spi master interface timing config structure
  134. */
  135. typedef struct {
  136. uint32_t clk_src_freq_in_hz;
  137. uint32_t sclk_freq_in_hz;
  138. uint8_t cs2sclk;
  139. uint8_t csht;
  140. } spi_master_timing_config_t;
  141. /**
  142. * @brief spi interface timing config structure
  143. */
  144. typedef struct {
  145. spi_master_timing_config_t master_config;
  146. } spi_timing_config_t;
  147. /**
  148. * @brief spi master transfer format config structure
  149. */
  150. typedef struct {
  151. uint8_t addr_len_in_bytes;
  152. } spi_master_format_config_t;
  153. /**
  154. * @brief spi common format config structure
  155. */
  156. typedef struct {
  157. uint8_t data_len_in_bits;
  158. bool data_merge;
  159. bool mosi_bidir;
  160. bool lsb;
  161. uint8_t mode;
  162. uint8_t cpol;
  163. uint8_t cpha;
  164. } spi_common_format_config_t;
  165. /**
  166. * @brief spi format config structure
  167. */
  168. typedef struct {
  169. spi_master_format_config_t master_config;
  170. spi_common_format_config_t common_config;
  171. } spi_format_config_t;
  172. /**
  173. * @brief spi master transfer control config structure
  174. */
  175. typedef struct {
  176. bool cmd_enable;
  177. bool addr_enable;
  178. uint8_t addr_phase_fmt;
  179. bool token_enable;
  180. uint8_t token_value;
  181. } spi_master_control_config_t;
  182. /**
  183. * @brief spi slave transfer control config structure
  184. */
  185. typedef struct {
  186. bool slave_data_only;
  187. } spi_slave_control_config_t;
  188. /**
  189. * @brief spi common transfer control config structure
  190. */
  191. typedef struct {
  192. bool tx_dma_enable;
  193. bool rx_dma_enable;
  194. uint8_t trans_mode;
  195. uint8_t data_phase_fmt;
  196. uint8_t dummy_cnt;
  197. } spi_common_control_config_t;
  198. /**
  199. * @brief spi control config structure
  200. */
  201. typedef struct {
  202. spi_master_control_config_t master_config;
  203. spi_slave_control_config_t slave_config;
  204. spi_common_control_config_t common_config;
  205. } spi_control_config_t;
  206. #if defined(__cplusplus)
  207. extern "C" {
  208. #endif /* __cplusplus */
  209. /**
  210. * @brief spi master get default timing config
  211. *
  212. * @param [out] config spi_timing_config_t
  213. */
  214. void spi_master_get_default_timing_config(spi_timing_config_t *config);
  215. /**
  216. * @brief spi master get default format config
  217. *
  218. * @param [out] config spi_format_config_t
  219. */
  220. void spi_master_get_default_format_config(spi_format_config_t *config);
  221. /**
  222. * @brief spi master get default control config
  223. *
  224. * @param [out] config spi_control_config_t
  225. */
  226. void spi_master_get_default_control_config(spi_control_config_t *config);
  227. /**
  228. * @brief spi slave get default format config
  229. *
  230. * @param [out] config spi_format_config_t
  231. */
  232. void spi_slave_get_default_format_config(spi_format_config_t *config);
  233. /**
  234. * @brief spi slave get default control config
  235. *
  236. * @param [out] config spi_control_config_t
  237. */
  238. void spi_slave_get_default_control_config(spi_control_config_t *config);
  239. /**
  240. * @brief spi master timing initialization
  241. *
  242. * @param [in] ptr SPI base address
  243. * @param [in] config spi_timing_config_t
  244. * @retval hpm_stat_t status_invalid_argument or status_success
  245. */
  246. hpm_stat_t spi_master_timing_init(SPI_Type *ptr, spi_timing_config_t *config);
  247. /**
  248. * @brief spi format initialization
  249. *
  250. * @param [in] ptr SPI base address
  251. * @param [in] config spi_format_config_t
  252. */
  253. void spi_format_init(SPI_Type *ptr, spi_format_config_t *config);
  254. /**
  255. * @brief spi transfer
  256. *
  257. * @param [in] ptr SPI base address
  258. * @param [in] config spi_control_config_t
  259. * @param [in,out] cmd spi transfer command address
  260. * @param [in] addr spi transfer target address
  261. * @param [in] wbuff spi sent data buff address
  262. * @param [in] wcount spi sent data count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  263. * @param [out] rbuff spi receive data buff address
  264. * @param [in] rcount spi receive data count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  265. * @retval hpm_stat_t status_success if spi transfer without any error
  266. */
  267. hpm_stat_t spi_transfer(SPI_Type *ptr,
  268. spi_control_config_t *config,
  269. uint8_t *cmd, uint32_t *addr,
  270. uint8_t *wbuff, uint32_t wcount, uint8_t *rbuff, uint32_t rcount);
  271. /**
  272. * @brief spi setup dma transfer
  273. *
  274. * @param [in] ptr SPI base address
  275. * @param [in] config spi_control_config_t
  276. * @param [in] cmd spi transfer command address
  277. * @param [in] addr spi transfer target address
  278. * @param [in] wcount spi sent data count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  279. * @param [in] rcount spi receive data count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  280. * @retval hpm_stat_t status_success if spi setup dma transfer without any error
  281. */
  282. hpm_stat_t spi_setup_dma_transfer(SPI_Type *ptr,
  283. spi_control_config_t *config,
  284. uint8_t *cmd, uint32_t *addr,
  285. uint32_t wcount, uint32_t rcount);
  286. /**
  287. * @brief spi wait for idle status
  288. *
  289. * @note on slave mode, if CS signal is asserted, take it as busy; if SPI CS signal is de-asserted, take it as idle.
  290. *
  291. * @param [in] ptr SPI base address
  292. * @retval hpm_stat_t status_success if spi in idle status
  293. */
  294. hpm_stat_t spi_wait_for_idle_status(SPI_Type *ptr);
  295. /**
  296. * @brief spi wait for busy status
  297. *
  298. * @note on slave mode, if CS signal is asserted, take it as busy; if SPI CS signal is de-asserted, take it as idle.
  299. *
  300. * @param [in] ptr SPI base address
  301. * @retval hpm_stat_t status_success if spi in busy status
  302. */
  303. hpm_stat_t spi_wait_for_busy_status(SPI_Type *ptr);
  304. /**
  305. * @brief SPI set TX FIFO threshold
  306. *
  307. * This function configures SPI TX FIFO threshold.
  308. *
  309. * @param ptr SPI base address.
  310. * @param threshold The FIFO threshold value, the value should not greater than FIFO size.
  311. */
  312. static inline void spi_set_tx_fifo_threshold(SPI_Type *ptr, uint32_t threshold)
  313. {
  314. ptr->CTRL = (ptr->CTRL & ~SPI_CTRL_TXTHRES_MASK) | SPI_CTRL_TXTHRES_SET(threshold);
  315. }
  316. /**
  317. * @brief SPI set RX FIFO threshold
  318. *
  319. * This function configures SPI RX FIFO threshold.
  320. *
  321. * @param ptr SPI base address.
  322. * @param threshold The FIFO threshold value, the value should not greater than FIFO size.
  323. */
  324. static inline void spi_set_rx_fifo_threshold(SPI_Type *ptr, uint32_t threshold)
  325. {
  326. ptr->CTRL = (ptr->CTRL & ~SPI_CTRL_RXTHRES_MASK) | SPI_CTRL_RXTHRES_SET(threshold);
  327. }
  328. /**
  329. * @brief Enables the SPI DMA request.
  330. *
  331. * This function configures the Rx and Tx DMA mask of the SPI. The parameters are base and a DMA mask.
  332. *
  333. * @param ptr SPI base address.
  334. * @param mask The dma enable mask; Use the spi_dma_enable_t.
  335. */
  336. static inline void spi_enable_dma(SPI_Type *ptr, uint32_t mask)
  337. {
  338. ptr->CTRL |= mask;
  339. }
  340. /*!
  341. * @brief Disables the SPI DMA request.
  342. *
  343. * This function configures the Rx and Tx DMA mask of the SPI. The parameters are base and a DMA mask.
  344. *
  345. * @param ptr SPI base address.
  346. * @param mask The dma enable mask; Use the spi_dma_enable_t.
  347. */
  348. static inline void spi_disable_dma(SPI_Type *ptr, uint32_t mask)
  349. {
  350. ptr->CTRL &= ~mask;
  351. }
  352. /**
  353. * @brief Get the SPI interrupt status.
  354. *
  355. * This function gets interrupt status of the SPI.
  356. *
  357. * @param ptr SPI base address.
  358. * @retval SPI interrupt status register value
  359. */
  360. static inline uint32_t spi_get_interrupt_status(SPI_Type *ptr)
  361. {
  362. return ptr->INTRST;
  363. }
  364. /**
  365. * @brief Clear the SPI interrupt status.
  366. *
  367. * This function clears interrupt status of the SPI.
  368. *
  369. * @param ptr SPI base address.
  370. * @param mask The interrupt mask; Use the spi_interrupt_t.
  371. *
  372. */
  373. static inline void spi_clear_interrupt_status(SPI_Type *ptr, uint32_t mask)
  374. {
  375. /* write 1 to clear */
  376. ptr->INTRST = mask;
  377. }
  378. /**
  379. * @brief Enables the SPI interrupt.
  380. *
  381. * This function configures interrupt of the SPI. The parameters are base and a interrupt mask.
  382. *
  383. * @param ptr SPI base address.
  384. * @param mask The interrupt mask; Use the spi_interrupt_t.
  385. */
  386. static inline void spi_enable_interrupt(SPI_Type *ptr, uint32_t mask)
  387. {
  388. ptr->INTREN |= mask;
  389. }
  390. /*!
  391. * @brief Disables the SPI interrupt.
  392. *
  393. * This function configures interrupt of the SPI. The parameters are base and a interrupt mask.
  394. *
  395. * @param ptr SPI base address.
  396. * @param mask The interrupt mask; Use the spi_interrupt_t.
  397. */
  398. static inline void spi_disable_interrupt(SPI_Type *ptr, uint32_t mask)
  399. {
  400. ptr->INTREN &= ~mask;
  401. }
  402. /**
  403. * @brief spi write and read data
  404. *
  405. * @note Call this function after SPI CONTROL is configured by spi_control_init.
  406. * The order of reading and writing is controlled by spi_control_init.
  407. *
  408. * @param [in] ptr SPI base address
  409. * @param [in] data_len_in_bytes data length in bytes
  410. * @param [in] wbuff spi sent data buff address
  411. * @param [in] wcount spi sent data count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  412. * @param [out] rbuff spi receive data buff address
  413. * @param [in] rcount spi receive data count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  414. * @retval hpm_stat_t status_success if spi transfer without any error
  415. */
  416. hpm_stat_t spi_write_read_data(SPI_Type *ptr, uint8_t data_len_in_bytes, uint8_t *wbuff, uint32_t wcount, uint8_t *rbuff, uint32_t rcount);
  417. /**
  418. * @brief spi read data
  419. *
  420. * @note Call this function after SPI CONTROL is configured by spi_control_init.
  421. *
  422. * @param [in] ptr SPI base address
  423. * @param [in] data_len_in_bytes data length in bytes
  424. * @param [out] buff spi receive data buff address
  425. * @param [in] count spi receive data count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  426. * @retval hpm_stat_t status_success if spi transfer without any error
  427. */
  428. hpm_stat_t spi_read_data(SPI_Type *ptr, uint8_t data_len_in_bytes, uint8_t *buff, uint32_t count);
  429. /**
  430. * @brief spi write data
  431. *
  432. * @note Call this function after SPI CONTROL is configured by spi_control_init.
  433. *
  434. * @param [in] ptr SPI base address
  435. * @param [in] data_len_in_bytes data length in bytes
  436. * @param [in] buff spi sent data buff address
  437. * @param [in] count spi sent data count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  438. * @retval hpm_stat_t status_success if spi transfer without any error
  439. */
  440. hpm_stat_t spi_write_data(SPI_Type *ptr, uint8_t data_len_in_bytes, uint8_t *buff, uint32_t count);
  441. /**
  442. * @brief spi write command
  443. *
  444. * Writing operations on this register will trigger SPI transfers, call this function on master mode.
  445. *
  446. * @param [in] ptr SPI base address
  447. * @param [in] mode spi mode, use the spi_mode_selection_t
  448. * @param [in] config point to spi_control_config_t
  449. * @param [in] cmd command data address
  450. * @retval hpm_stat_t status_success if spi transfer without any error
  451. */
  452. hpm_stat_t spi_write_command(SPI_Type *ptr, spi_mode_selection_t mode, spi_control_config_t *config, uint8_t *cmd);
  453. /**
  454. * @brief spi read command
  455. *
  456. * On slave mode, the command field of the last received SPI transaction is stored in this SPI Command Register
  457. *
  458. * @param [in] ptr SPI base address
  459. * @param [in] mode spi mode, use the spi_mode_selection_t
  460. * @param [in] config point to spi_control_config_t
  461. * @param [out] cmd command data address
  462. * @retval hpm_stat_t status_success if spi transfer without any error
  463. */
  464. hpm_stat_t spi_read_command(SPI_Type *ptr, spi_mode_selection_t mode, spi_control_config_t *config, uint8_t *cmd);
  465. /**
  466. * @brief spi write address
  467. *
  468. * @note Call this function on master mode.
  469. *
  470. * @param [in] ptr SPI base address
  471. * @param [in] mode spi mode, use the spi_mode_selection_t
  472. * @param [in] config point to spi_control_config_t
  473. * @param [in] addr point to address
  474. * @retval hpm_stat_t status_success if spi transfer without any error
  475. */
  476. hpm_stat_t spi_write_address(SPI_Type *ptr, spi_mode_selection_t mode, spi_control_config_t *config, uint32_t *addr);
  477. /**
  478. * @brief spi control initialization
  479. *
  480. * @param [in] ptr SPI base address
  481. * @param [in] config point to spi_control_config_t
  482. * @param [in] wcount spi sent data count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  483. * @param [in] rcount spi receive count, not greater than SPI_SOC_TRANSFER_COUNT_MAX
  484. * @retval hpm_stat_t status_success if spi transfer without any error
  485. */
  486. hpm_stat_t spi_control_init(SPI_Type *ptr, spi_control_config_t *config, uint32_t wcount, uint32_t rcount);
  487. /**
  488. * @brief Get the SPI data length in bits.
  489. *
  490. * @param ptr SPI base address.
  491. * @retval SPI data length in bits
  492. */
  493. static inline uint8_t spi_get_data_length_in_bits(SPI_Type *ptr)
  494. {
  495. return ((ptr->TRANSFMT & SPI_TRANSFMT_DATALEN_MASK) >> SPI_TRANSFMT_DATALEN_SHIFT) + 1;
  496. }
  497. /**
  498. * @brief Get the SPI data length in bytes.
  499. *
  500. * @param ptr SPI base address.
  501. * @retval SPI data length in bytes
  502. */
  503. static inline uint8_t spi_get_data_length_in_bytes(SPI_Type *ptr)
  504. {
  505. return ((spi_get_data_length_in_bits(ptr) + 7U) / 8U);
  506. }
  507. /**
  508. * @brief SPI get active status.
  509. *
  510. * @param ptr SPI base address.
  511. * @retval bool true for active, false for inactive
  512. */
  513. static inline bool spi_is_active(SPI_Type *ptr)
  514. {
  515. return ((ptr->STATUS & SPI_STATUS_SPIACTIVE_MASK) == SPI_STATUS_SPIACTIVE_MASK) ? true : false;
  516. }
  517. /**
  518. * @brief SPI enable tx dma
  519. *
  520. * @param ptr SPI base address
  521. */
  522. static inline void spi_enable_tx_dma(SPI_Type *ptr)
  523. {
  524. ptr->CTRL |= SPI_CTRL_TXDMAEN_MASK;
  525. }
  526. /**
  527. * @brief SPI disable tx dma
  528. *
  529. * @param ptr SPI base address
  530. */
  531. static inline void spi_disable_tx_dma(SPI_Type *ptr)
  532. {
  533. ptr->CTRL &= ~SPI_CTRL_TXDMAEN_MASK;
  534. }
  535. /**
  536. * @brief SPI enable rx dma
  537. *
  538. * @param ptr SPI base address
  539. */
  540. static inline void spi_enable_rx_dma(SPI_Type *ptr)
  541. {
  542. ptr->CTRL |= SPI_CTRL_RXDMAEN_MASK;
  543. }
  544. /**
  545. * @brief SPI disable rx dma
  546. *
  547. * @param ptr SPI base address
  548. */
  549. static inline void spi_disable_rx_dma(SPI_Type *ptr)
  550. {
  551. ptr->CTRL &= ~SPI_CTRL_RXDMAEN_MASK;
  552. }
  553. /**
  554. * @brief SPI slave get sent data count
  555. *
  556. * @param ptr SPI base address
  557. * @retval uint32_t data count
  558. */
  559. static inline uint32_t spi_slave_get_sent_data_count(SPI_Type *ptr)
  560. {
  561. return SPI_SLVDATACNT_WCNT_GET(ptr->SLVDATACNT);
  562. }
  563. /**
  564. * @brief SPI slave get received data count
  565. *
  566. * @param ptr SPI base address
  567. * @retval uint32_t data count
  568. */
  569. static inline uint32_t spi_slave_get_received_data_count(SPI_Type *ptr)
  570. {
  571. return SPI_SLVDATACNT_RCNT_GET(ptr->SLVDATACNT);
  572. }
  573. /**
  574. * @brief set spi clock phase
  575. *
  576. * @param [in] ptr SPI base address
  577. * @param [in] clock_phase clock phase enum
  578. */
  579. static inline void spi_set_clock_phase(SPI_Type *ptr, spi_sclk_sampling_clk_edges_t clock_phase)
  580. {
  581. ptr->TRANSCTRL |= SPI_TRANSFMT_CPHA_SET(clock_phase);
  582. }
  583. /**
  584. * @brief get spi clock phase
  585. *
  586. * @param [in] ptr SPI base address
  587. * @retval spi_sclk_sampling_clk_edges_t spi_sclk_sampling_odd_clk_edges if CPHA is 0
  588. */
  589. static inline spi_sclk_sampling_clk_edges_t spi_get_clock_phase(SPI_Type *ptr)
  590. {
  591. return SPI_TRANSFMT_CPHA_GET(ptr->TRANSCTRL);
  592. }
  593. /**
  594. * @brief set spi clock polarity
  595. *
  596. * @param [in] ptr SPI base address
  597. * @param [in] clock_polarity clock polarity enum
  598. */
  599. static inline void spi_set_clock_polarity(SPI_Type *ptr, spi_sclk_idle_state_t clock_polarity)
  600. {
  601. ptr->TRANSCTRL |= SPI_TRANSFMT_CPOL_SET(clock_polarity);
  602. }
  603. /**
  604. * @brief get spi clock phase
  605. *
  606. * @param [in] ptr SPI base address
  607. * @retval spi_sclk_idle_state_t spi_sclk_low_idle if CPOL is 0
  608. */
  609. static inline spi_sclk_idle_state_t spi_get_clock_polarity(SPI_Type *ptr)
  610. {
  611. return SPI_TRANSFMT_CPOL_GET(ptr->TRANSCTRL);
  612. }
  613. /**
  614. * @brief set spi the length of each data unit in bits
  615. *
  616. * @param [in] nbit the actual bits number of a data
  617. * @retval hpm_stat_t status_success if spi transfer without any error
  618. */
  619. static inline hpm_stat_t spi_set_data_bits(SPI_Type *ptr, uint8_t nbits)
  620. {
  621. if (nbits > 32) {
  622. return status_invalid_argument;
  623. } else {
  624. ptr->TRANSFMT |= SPI_TRANSFMT_DATALEN_SET(nbits - 1);
  625. return status_success;
  626. }
  627. }
  628. /**
  629. * @brief SPI transmit fifo reset
  630. *
  631. * @param ptr SPI base address
  632. */
  633. static inline void spi_transmit_fifo_reset(SPI_Type *ptr)
  634. {
  635. ptr->CTRL |= SPI_CTRL_TXFIFORST_MASK;
  636. }
  637. /**
  638. * @brief SPI receive fifo reset
  639. *
  640. * @param ptr SPI base address
  641. */
  642. static inline void spi_receive_fifo_reset(SPI_Type *ptr)
  643. {
  644. ptr->CTRL |= SPI_CTRL_RXFIFORST_MASK;
  645. }
  646. /**
  647. * @brief SPI reset
  648. *
  649. * @param ptr SPI base address
  650. */
  651. static inline void spi_reset(SPI_Type *ptr)
  652. {
  653. ptr->CTRL |= SPI_CTRL_SPIRST_MASK;
  654. }
  655. /**
  656. * @}
  657. */
  658. #if defined(__cplusplus)
  659. }
  660. #endif /* __cplusplus */
  661. #endif /* HPM_SPI_DRV_H */