hosal_spi.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef HAL_SPI_H
  31. #define HAL_SPI_H
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /** @addtogroup hosal_spi SPI
  36. * HOSAL SPI API
  37. *
  38. * @{
  39. */
  40. #include <stdint.h>
  41. #include <FreeRTOS.h>
  42. #include <event_groups.h>
  43. #define HOSAL_SPI_MODE_MASTER 0 /**< spi communication is master mode */
  44. #define HOSAL_SPI_MODE_SLAVE 1 /**< spi communication is slave mode */
  45. #define HOSAL_WAIT_FOREVER 0xFFFFFFFFU /**< DMA transmission timeout */
  46. typedef void (*hosal_spi_irq_t)(void *parg); /**< spi irq callback function */
  47. /**
  48. * @brief Define spi config args
  49. */
  50. typedef struct {
  51. uint8_t mode; /**< spi communication mode */
  52. uint8_t dma_enable; /**< enable dma tansmission or not */
  53. uint8_t polar_phase; /**< spi polar and phase */
  54. uint32_t freq; /**< communication frequency Hz */
  55. uint8_t pin_clk; /**< spi clk pin */
  56. uint8_t pin_mosi; /**< spi mosi pin */
  57. uint8_t pin_miso; /**< spi miso pin */
  58. } hosal_spi_config_t;
  59. /**
  60. * @brief Define spi dev handle
  61. */
  62. typedef struct {
  63. uint8_t port; /**< spi port */
  64. hosal_spi_config_t config; /**< spi config */
  65. hosal_spi_irq_t cb; /**< spi interrupt callback */
  66. void *p_arg; /**< arg pass to callback */
  67. void *priv; /**< priv data */
  68. } hosal_spi_dev_t;
  69. /**
  70. * @brief Initialises the SPI interface for a given SPI device
  71. *
  72. * @param[in] spi the spi device
  73. *
  74. * @return
  75. * - 0 : on success
  76. * - other : error
  77. */
  78. int hosal_spi_init(hosal_spi_dev_t *spi);
  79. /**
  80. * @brief Spi send
  81. *
  82. * @param[in] spi the spi device
  83. * @param[in] data spi send data
  84. * @param[in] size spi send data size
  85. * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
  86. * if you want to wait forever
  87. *
  88. * @return
  89. * - 0 : on success
  90. * - other : error
  91. */
  92. int hosal_spi_send(hosal_spi_dev_t *spi, const uint8_t *data, uint32_t size, uint32_t timeout);
  93. /**
  94. * @brief Spi recv
  95. *
  96. * @param[in] spi the spi device
  97. * @param[out] data spi recv data
  98. * @param[in] size spi recv data size
  99. * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
  100. * if you want to wait forever
  101. *
  102. * @return
  103. * - 0 : success
  104. * - other : error
  105. */
  106. int hosal_spi_recv(hosal_spi_dev_t *spi, uint8_t *data, uint16_t size, uint32_t timeout);
  107. /**
  108. * @brief spi send data and recv
  109. *
  110. * @param[in] spi the spi device
  111. * @param[in] tx_data spi send data
  112. * @param[out] rx_data spi recv data
  113. * @param[in] size spi data to be sent and recived
  114. * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
  115. * if you want to wait forever
  116. *
  117. * @return
  118. * - 0 : success
  119. * - other : error
  120. */
  121. int hosal_spi_send_recv(hosal_spi_dev_t *spi, uint8_t *tx_data, uint8_t *rx_data, uint16_t size, uint32_t timeout);
  122. /*
  123. * @brief set spi irq callback
  124. *
  125. * @param spi the spi device
  126. * @param pfn callback function
  127. * @param p_arg callback function parameter
  128. *
  129. * @return
  130. * - 0 : success
  131. * - othe : error
  132. */
  133. int hosal_spi_irq_callback_set(hosal_spi_dev_t *spi, hosal_spi_irq_t pfn, void *p_arg);
  134. /**
  135. * @brief spi software set cs pin high/low only for master device
  136. *
  137. * @param[in] pin cs pin
  138. * @param[in] value 0 or 1
  139. *
  140. * @return
  141. * - 0 : success
  142. * - other : error
  143. */
  144. int hosal_spi_set_cs(uint8_t pin, uint8_t value);
  145. /**
  146. * @brief De-initialises a SPI interface
  147. *
  148. *
  149. * @param[in] spi the SPI device to be de-initialised
  150. *
  151. * @return
  152. * - 0 : success
  153. * - other : error
  154. */
  155. int hosal_spi_finalize(hosal_spi_dev_t *spi);
  156. /** @} */
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif /* HAL_SPI_H */