cmem7_spi.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. *****************************************************************************
  3. * @file cmem7_spi.h
  4. *
  5. * @brief CMEM7 SPI header file
  6. *
  7. *
  8. * @version V1.0
  9. * @date 3. September 2013
  10. *
  11. * @note
  12. *
  13. *****************************************************************************
  14. * @attention
  15. *
  16. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  17. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  18. * TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  19. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  20. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  21. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  22. *
  23. * <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
  24. *****************************************************************************
  25. */
  26. #ifndef __CMEM7_SPI_H
  27. #define __CMEM7_SPI_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "cmem7.h"
  33. #include "cmem7_conf.h"
  34. #define IS_SPI_ALL_PERIPH(PERIPH) (((PERIPH) == SPI0) || \
  35. ((PERIPH) == SPI1))
  36. /** @defgroup SPI_MODE
  37. * @{
  38. */
  39. #define SPI_MODE_CPOL_0_CPHA_0 0 /*!< CPOL : Idle clock level is low level.
  40. CPHA : Capture data at the first edge */
  41. #define SPI_MODE_CPOL_0_CPHA_1 1 /*!< CPOL : Idle clock level is low level.
  42. CPHA : Capture data at the second edge */
  43. #define SPI_MODE_CPOL_1_CPHA_0 2 /*!< CPOL : Idle clock level is high level.
  44. CPHA : Capture data at the first edge */
  45. #define SPI_MODE_CPOL_1_CPHA_1 3 /*!< CPOL : Idle clock level is high level.
  46. CPHA : Capture data at the first edge */
  47. #define IS_SPI_MODE(MODE) (((MODE) == SPI_MODE_CPOL_0_CPHA_0) || \
  48. ((MODE) == SPI_MODE_CPOL_0_CPHA_1) || \
  49. ((MODE) == SPI_MODE_CPOL_1_CPHA_0) || \
  50. ((MODE) == SPI_MODE_CPOL_1_CPHA_1))
  51. /**
  52. * @}
  53. */
  54. /** @defgroup SPI_INT
  55. * @{
  56. */
  57. #define SPI_INT_RX_FIFO_UNDERFLOW 0x00000001
  58. #define SPI_INT_RX_FIFO_OVERFLOW 0x00000002
  59. #define SPI_INT_RX_FIFO_ALMOST_FULL 0x00000004
  60. #define SPI_INT_TX_FIFO_UNDERFLOW 0x00000008
  61. #define SPI_INT_TX_FIFO_OVERFLOW 0x00000010
  62. #define SPI_INT_TX_FIFO_ALMOST_FULL 0x00000020
  63. #define SPI_INT_DONE 0x00000040
  64. #define SPI_INT_ALL 0x0000007F
  65. #define IS_SPI_INT(INT) (((INT) != 0) && (((INT) & ~SPI_INT_ALL) == 0))
  66. /**
  67. * @}
  68. */
  69. /**
  70. * @brief SPI initialization structure
  71. */
  72. typedef struct
  73. {
  74. uint8_t SPI_Mode; /*!< indicates SPI's CPOL and CPHA, ref as @ref SPI_MODE */
  75. BOOL SPI_RxEn; /*!< indicates if SPI receiver is enabled or not */
  76. uint8_t SPI_BitLength; /*!< bit length while transmitting and receiving */
  77. uint8_t SPI_Gap; /*!< cycle number between continuous data frame */
  78. uint8_t SPI_ClockDividor; /*!< SPI clock dividor, 1 / ((1 + DIV) * 2) */
  79. } SPI_InitTypeDef;
  80. /**
  81. * @}
  82. */
  83. /**
  84. * @brief SPI initialization
  85. * @note This function should be called at first before any other interfaces.
  86. * @param[in] SPIx SPI peripheral, which is SPI0 or SPI1
  87. * @param[in] init A pointer to structure SPI_InitTypeDef
  88. * @retval None
  89. */
  90. void SPI_Init(SPI0_Type* SPIx, SPI_InitTypeDef *init);
  91. /**
  92. * @brief Enable or disable SPI.
  93. * @param[in] SPIx SPI peripheral, which is SPI0 or SPI1
  94. * @param[in] Enable The bit indicates if the specific SPI is enable or not
  95. * @retval None
  96. */
  97. void SPI_Enable(SPI0_Type* SPIx, BOOL enable);
  98. /**
  99. * @brief Enable or disable SPI interrupt.
  100. * @param[in] SPIx SPI peripheral, which is SPI0 or SPI1
  101. * @param[in] Int interrupt mask bits, which can be the combination of @ref SPI_Int
  102. * @param[in] Enable The bit indicates if specific interrupts are enable or not
  103. * @retval None
  104. */
  105. void SPI_EnableInt(SPI0_Type* SPIx, uint32_t Int, BOOL enable);
  106. /**
  107. * @brief Check specific interrupts are set or not
  108. * @param[in] SPIx SPI peripheral, which is SPI0 or SPI1
  109. * @param[in] Int interrupt mask bits, which can be the combination of @ref SPI_Int
  110. * @retval BOOL The bit indicates if specific interrupts are set or not
  111. */
  112. BOOL SPI_GetIntStatus(SPI0_Type* SPIx, uint32_t Int);
  113. /**
  114. * @brief Clear specific interrupts
  115. * @param[in] SPIx SPI peripheral, which is SPI0 or SPI1
  116. * @param[in] Int interrupt mask bits, which can be the combination of @ref SPI_Int
  117. * @retval None
  118. */
  119. void SPI_ClearInt(SPI0_Type* SPIx, uint32_t Int);
  120. /**
  121. * @brief Read data from SPI FIFO
  122. * @param[in] SPIx SPI peripheral, which is SPI0 or SPI1
  123. * @param[in] size Expected data size to be read
  124. * @param[out] data A user-allocated buffer to fetch data to be read
  125. * @retval uint8_t Actual read data size
  126. */
  127. uint8_t SPI_ReadFifo(SPI0_Type* SPIx, uint8_t size, uint32_t* data);
  128. /**
  129. * @brief Write data to SPI FIFO
  130. * @param[in] SPIx SPI peripheral, which is SPI0 or SPI1
  131. * @param[in] size Expected data size to be written
  132. * @param[in] data A pointer to the data to be written
  133. * @retval uint8_t Actual written data size
  134. */
  135. uint8_t SPI_WriteFifo(SPI0_Type* SPIx, uint8_t Size, uint32_t* data);
  136. /**
  137. * @brief send a SPI transcation request
  138. * @param[in] SPIx SPI peripheral, which is SPI0 or SPI1
  139. * @param[in] size Expected data size to be written and read
  140. * @retval BOOL The bit indicates if the request is sent
  141. */
  142. BOOL SPI_Transcation(SPI0_Type* SPIx, uint8_t size);
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146. #endif /*__CMEM7_SPI_H */