spi_5W_master.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef APP_SPI_MASTER_H
  41. #define APP_SPI_MASTER_H
  42. #include <stdint.h>
  43. #include <stdlib.h>
  44. #include "boards.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. #define _SPI_5W_
  49. /**@brief Struct containing configuration parameters of the SPI master. */
  50. typedef struct
  51. {
  52. uint32_t SPI_Freq; /**< SPI frequency. */
  53. uint32_t SPI_Pin_SCK; /**< SCK pin number. */
  54. uint32_t SPI_Pin_MISO; /**< MISO pin number. */
  55. uint32_t SPI_Pin_MOSI; /**< MOSI pin number .*/
  56. uint32_t SPI_Pin_SS; /**< Slave select pin number. */
  57. uint8_t SPI_ORDER; /**< Bytes order MSBFIRST or LSBFIRST. */
  58. uint8_t SPI_CPOL; /**< Serial clock polarity ACTIVEHIGH or ACTIVELOW. */
  59. uint8_t SPI_CPHA; /**< Serial clock phase LEADING or TRAILING. */
  60. } spi_master_config_t;
  61. /**@brief SPI master driver events types. */
  62. typedef enum
  63. {
  64. SPI_MASTER_EVT_TRANSFER_STARTED = 0, /**< An event indicating that transfer has been started */
  65. SPI_MASTER_EVT_TRANSFER_COMPLETED, /**< An event indicating that transfer has been completed */
  66. SPI_MASTER_EVT_TRANSFER_ABORTED, /**< An event indicating that transfer has been aborted */
  67. SPI_MASTER_EVT_TRANSFER_RESTARTED, /**< An event indicating that transfer has been resumed */
  68. SPI_MASTER_EVT_FIRST_BYTE_RECEIVED, /**< An event indicating end of one byte transfer */
  69. SPI_MASTER_EVT_TYPE_MAX /**< Enumeration upper bound. */
  70. } spi_master_evt_type_t;
  71. /**@brief Struct containing parameters of the SPI MASTER event */
  72. typedef struct
  73. {
  74. spi_master_evt_type_t type; /**< Type of an event */
  75. uint16_t data; /**< event data - context dependent */
  76. } spi_master_evt_t;
  77. /**@brief SPI MASTER internal states types. */
  78. typedef enum
  79. {
  80. SPI_MASTER_STATE_DISABLED, /**< A state indicating that SPI master is disabled. */
  81. SPI_MASTER_STATE_BUSY, /**< A state indicating that SPI master is sending now. */
  82. SPI_MASTER_STATE_ABORTED,
  83. SPI_MASTER_STATE_IDLE /**< A state indicating that SPI master is idle now. */
  84. } spi_master_state_t;
  85. /**@brief Instances of SPI master module. */
  86. typedef enum
  87. {
  88. #ifdef SPI_MASTER_0_ENABLE
  89. SPI_MASTER_0, /**< A instance of SPI master 0. */
  90. #endif
  91. #ifdef SPI_MASTER_1_ENABLE
  92. SPI_MASTER_1, /**< A instance of SPI master 1. */
  93. #endif
  94. SPI_MASTER_HW_ENABLED_COUNT /**< A number of enabled instances of SPI master. */
  95. } spi_master_hw_instance_t;
  96. /**@brief Type of generic callback function handler to be used by all SPI MASTER driver events.
  97. *
  98. * @param[in] spi_master_evt SPI MASTER driver event.
  99. */
  100. typedef void (*spi_master_event_handler_t) (spi_master_evt_t spi_master_evt);
  101. /**@brief Function for opening and initializing a SPI master driver.
  102. *
  103. * @note Function initializes SPI master hardware and internal module states, unregister events callback.
  104. *
  105. * @warning If the function has been already called, the function @ref spi_master_close has to be
  106. * called before spi_master_open can be called again.
  107. *
  108. * @param[in] spi_master_hw_instance Instance of SPI master module.
  109. * @param[in] p_spi_master_config Pointer to configuration structure which will be used
  110. * to initialize SPI MASTER hardware.
  111. *
  112. * @retval NRF_SUCCESS Operation success.
  113. * @retval NRF_ERROR_INVALID_STATE Operation failure. The function has been already called.
  114. * To call it again the function @ref spi_master_close
  115. * has to be called previously.
  116. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  117. */
  118. uint32_t spi_master_open(const spi_master_hw_instance_t spi_master_hw_instance,
  119. spi_master_config_t const * const p_spi_master_config);
  120. /**@brief Function for closing a SPI MASTER driver.
  121. *
  122. * @note Function disable hardware, reset internal module states and unregister events callback
  123. * function.
  124. *
  125. * @param[in] spi_master_hw_instance A instance of SPI master.
  126. */
  127. void spi_master_close(const spi_master_hw_instance_t spi_master_hw_instance);
  128. /**@brief Function for transferring data between SPI master and SPI slave
  129. *
  130. * @note Function registers buffers pointed by p_tx_buf and p_rx_buf parameters, after that starts transmission.
  131. * Function generates an event of type @ref SPI_MASTER_EVT_TRANSFER_STARTED when transfer has been started
  132. * and @ref SPI_MASTER_EVT_TRANSFER_COMPLETED when transfer has been completed.
  133. *
  134. * @param[in] spi_master_hw_instance Instance of SPI master module.
  135. * @param[in] p_tx_buf Pointer to a transmit buffer.
  136. * @param[in] tx_buf_len Number of octets to the transfer.
  137. * @param[out] p_rx_buf Pointer to a receive buffer.
  138. * @param[in] rx_buf_len Number of octets to be received.
  139. *
  140. * @retval NRF_SUCCESS Operation success. Packet was registered to the transmission
  141. * and event will be send upon transmission completion.
  142. * @retval NRF_ERROR_BUSY Operation failure. Transmitting of a data is in progress.
  143. */
  144. uint32_t spi_master_send_recv(const spi_master_hw_instance_t spi_master_hw_instance,
  145. uint8_t * const p_tx_buf, const uint16_t tx_buf_len,
  146. uint8_t * const p_rx_buf, const uint16_t rx_buf_len);
  147. /**@brief Function for registration event handler.
  148. *
  149. * @note Function registers a event handler to be used by SPI MASTER driver for sending events.
  150. * @ref SPI_MASTER_EVT_TRANSFER_STARTED and @ref SPI_MASTER_EVT_TRANSFER_COMPLETED.
  151. *
  152. * @param[in] spi_master_hw_instance Instance of SPI master module.
  153. * @param[in] event_handler Generic callback function handler to be used
  154. * by all SPI master driver events.
  155. */
  156. void spi_master_evt_handler_reg(const spi_master_hw_instance_t spi_master_hw_instance,
  157. spi_master_event_handler_t event_handler);
  158. /**@brief Function for getting current state of the SPI master driver.
  159. *
  160. * @note Function gets current state of the SPI master driver.
  161. *
  162. * @param[in] spi_master_hw_instance Instance of SPI master module.
  163. *
  164. * @retval SPI_MASTER_STATE_DISABLED SPI MASTER is disabled.
  165. * @retval SPI_MASTER_STATE_BUSY SPI_MASTER is sending now.
  166. * @retval SPI_MASTER_STATE_IDLE SPI_MASTER is idle now.
  167. */
  168. spi_master_state_t spi_master_get_state(const spi_master_hw_instance_t spi_master_hw_instance);
  169. #ifdef _SPI_5W_
  170. uint32_t spi_master_abort(const spi_master_hw_instance_t spi_master_hw_instance);
  171. uint32_t spi_master_restart(const spi_master_hw_instance_t spi_master_hw_instance);
  172. void spi_5W_master_evt_handler_reg(const spi_master_hw_instance_t spi_master_hw_instance,
  173. spi_master_event_handler_t event_handler);
  174. #endif
  175. #ifdef __cplusplus
  176. }
  177. #endif
  178. #endif