ch32v30x_spi.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v30x_spi.h
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2021/06/06
  6. * Description : This file contains all the functions prototypes for the
  7. * SPI firmware library.
  8. *********************************************************************************
  9. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  10. * Attention: This software (modified or not) and binary are used for
  11. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  12. *******************************************************************************/
  13. #ifndef __CH32V30x_SPI_H
  14. #define __CH32V30x_SPI_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "ch32v30x.h"
  19. /* SPI Init structure definition */
  20. typedef struct
  21. {
  22. uint16_t SPI_Direction; /* Specifies the SPI unidirectional or bidirectional data mode.
  23. This parameter can be a value of @ref SPI_data_direction */
  24. uint16_t SPI_Mode; /* Specifies the SPI operating mode.
  25. This parameter can be a value of @ref SPI_mode */
  26. uint16_t SPI_DataSize; /* Specifies the SPI data size.
  27. This parameter can be a value of @ref SPI_data_size */
  28. uint16_t SPI_CPOL; /* Specifies the serial clock steady state.
  29. This parameter can be a value of @ref SPI_Clock_Polarity */
  30. uint16_t SPI_CPHA; /* Specifies the clock active edge for the bit capture.
  31. This parameter can be a value of @ref SPI_Clock_Phase */
  32. uint16_t SPI_NSS; /* Specifies whether the NSS signal is managed by
  33. hardware (NSS pin) or by software using the SSI bit.
  34. This parameter can be a value of @ref SPI_Slave_Select_management */
  35. uint16_t SPI_BaudRatePrescaler; /* Specifies the Baud Rate prescaler value which will be
  36. used to configure the transmit and receive SCK clock.
  37. This parameter can be a value of @ref SPI_BaudRate_Prescaler.
  38. @note The communication clock is derived from the master
  39. clock. The slave clock does not need to be set. */
  40. uint16_t SPI_FirstBit; /* Specifies whether data transfers start from MSB or LSB bit.
  41. This parameter can be a value of @ref SPI_MSB_LSB_transmission */
  42. uint16_t SPI_CRCPolynomial; /* Specifies the polynomial used for the CRC calculation. */
  43. }SPI_InitTypeDef;
  44. /* I2S Init structure definition */
  45. typedef struct
  46. {
  47. uint16_t I2S_Mode; /* Specifies the I2S operating mode.
  48. This parameter can be a value of @ref I2S_Mode */
  49. uint16_t I2S_Standard; /* Specifies the standard used for the I2S communication.
  50. This parameter can be a value of @ref I2S_Standard */
  51. uint16_t I2S_DataFormat; /* Specifies the data format for the I2S communication.
  52. This parameter can be a value of @ref I2S_Data_Format */
  53. uint16_t I2S_MCLKOutput; /* Specifies whether the I2S MCLK output is enabled or not.
  54. This parameter can be a value of @ref I2S_MCLK_Output */
  55. uint32_t I2S_AudioFreq; /* Specifies the frequency selected for the I2S communication.
  56. This parameter can be a value of @ref I2S_Audio_Frequency */
  57. uint16_t I2S_CPOL; /* Specifies the idle state of the I2S clock.
  58. This parameter can be a value of @ref I2S_Clock_Polarity */
  59. }I2S_InitTypeDef;
  60. /* SPI_data_direction */
  61. #define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000)
  62. #define SPI_Direction_2Lines_RxOnly ((uint16_t)0x0400)
  63. #define SPI_Direction_1Line_Rx ((uint16_t)0x8000)
  64. #define SPI_Direction_1Line_Tx ((uint16_t)0xC000)
  65. /* SPI_mode */
  66. #define SPI_Mode_Master ((uint16_t)0x0104)
  67. #define SPI_Mode_Slave ((uint16_t)0x0000)
  68. /* SPI_data_size */
  69. #define SPI_DataSize_16b ((uint16_t)0x0800)
  70. #define SPI_DataSize_8b ((uint16_t)0x0000)
  71. /* SPI_Clock_Polarity */
  72. #define SPI_CPOL_Low ((uint16_t)0x0000)
  73. #define SPI_CPOL_High ((uint16_t)0x0002)
  74. /* SPI_Clock_Phase */
  75. #define SPI_CPHA_1Edge ((uint16_t)0x0000)
  76. #define SPI_CPHA_2Edge ((uint16_t)0x0001)
  77. /* SPI_Slave_Select_management */
  78. #define SPI_NSS_Soft ((uint16_t)0x0200)
  79. #define SPI_NSS_Hard ((uint16_t)0x0000)
  80. /* SPI_BaudRate_Prescaler */
  81. #define SPI_BaudRatePrescaler_2 ((uint16_t)0x0000)
  82. #define SPI_BaudRatePrescaler_4 ((uint16_t)0x0008)
  83. #define SPI_BaudRatePrescaler_8 ((uint16_t)0x0010)
  84. #define SPI_BaudRatePrescaler_16 ((uint16_t)0x0018)
  85. #define SPI_BaudRatePrescaler_32 ((uint16_t)0x0020)
  86. #define SPI_BaudRatePrescaler_64 ((uint16_t)0x0028)
  87. #define SPI_BaudRatePrescaler_128 ((uint16_t)0x0030)
  88. #define SPI_BaudRatePrescaler_256 ((uint16_t)0x0038)
  89. /* SPI_MSB_LSB_transmission */
  90. #define SPI_FirstBit_MSB ((uint16_t)0x0000)
  91. #define SPI_FirstBit_LSB ((uint16_t)0x0080)
  92. /* I2S_Mode */
  93. #define I2S_Mode_SlaveTx ((uint16_t)0x0000)
  94. #define I2S_Mode_SlaveRx ((uint16_t)0x0100)
  95. #define I2S_Mode_MasterTx ((uint16_t)0x0200)
  96. #define I2S_Mode_MasterRx ((uint16_t)0x0300)
  97. /* I2S_Standard */
  98. #define I2S_Standard_Phillips ((uint16_t)0x0000)
  99. #define I2S_Standard_MSB ((uint16_t)0x0010)
  100. #define I2S_Standard_LSB ((uint16_t)0x0020)
  101. #define I2S_Standard_PCMShort ((uint16_t)0x0030)
  102. #define I2S_Standard_PCMLong ((uint16_t)0x00B0)
  103. /* I2S_Data_Format */
  104. #define I2S_DataFormat_16b ((uint16_t)0x0000)
  105. #define I2S_DataFormat_16bextended ((uint16_t)0x0001)
  106. #define I2S_DataFormat_24b ((uint16_t)0x0003)
  107. #define I2S_DataFormat_32b ((uint16_t)0x0005)
  108. /* I2S_MCLK_Output */
  109. #define I2S_MCLKOutput_Enable ((uint16_t)0x0200)
  110. #define I2S_MCLKOutput_Disable ((uint16_t)0x0000)
  111. /* I2S_Audio_Frequency */
  112. #define I2S_AudioFreq_192k ((uint32_t)192000)
  113. #define I2S_AudioFreq_96k ((uint32_t)96000)
  114. #define I2S_AudioFreq_48k ((uint32_t)48000)
  115. #define I2S_AudioFreq_44k ((uint32_t)44100)
  116. #define I2S_AudioFreq_32k ((uint32_t)32000)
  117. #define I2S_AudioFreq_22k ((uint32_t)22050)
  118. #define I2S_AudioFreq_16k ((uint32_t)16000)
  119. #define I2S_AudioFreq_11k ((uint32_t)11025)
  120. #define I2S_AudioFreq_8k ((uint32_t)8000)
  121. #define I2S_AudioFreq_Default ((uint32_t)2)
  122. /* I2S_Clock_Polarity */
  123. #define I2S_CPOL_Low ((uint16_t)0x0000)
  124. #define I2S_CPOL_High ((uint16_t)0x0008)
  125. /* SPI_I2S_DMA_transfer_requests */
  126. #define SPI_I2S_DMAReq_Tx ((uint16_t)0x0002)
  127. #define SPI_I2S_DMAReq_Rx ((uint16_t)0x0001)
  128. /* SPI_NSS_internal_software_management */
  129. #define SPI_NSSInternalSoft_Set ((uint16_t)0x0100)
  130. #define SPI_NSSInternalSoft_Reset ((uint16_t)0xFEFF)
  131. /* SPI_CRC_Transmit_Receive */
  132. #define SPI_CRC_Tx ((uint8_t)0x00)
  133. #define SPI_CRC_Rx ((uint8_t)0x01)
  134. /* SPI_direction_transmit_receive */
  135. #define SPI_Direction_Rx ((uint16_t)0xBFFF)
  136. #define SPI_Direction_Tx ((uint16_t)0x4000)
  137. /* SPI_I2S_interrupts_definition */
  138. #define SPI_I2S_IT_TXE ((uint8_t)0x71)
  139. #define SPI_I2S_IT_RXNE ((uint8_t)0x60)
  140. #define SPI_I2S_IT_ERR ((uint8_t)0x50)
  141. #define SPI_I2S_IT_OVR ((uint8_t)0x56)
  142. #define SPI_IT_MODF ((uint8_t)0x55)
  143. #define SPI_IT_CRCERR ((uint8_t)0x54)
  144. #define I2S_IT_UDR ((uint8_t)0x53)
  145. /* SPI_I2S_flags_definition */
  146. #define SPI_I2S_FLAG_RXNE ((uint16_t)0x0001)
  147. #define SPI_I2S_FLAG_TXE ((uint16_t)0x0002)
  148. #define I2S_FLAG_CHSIDE ((uint16_t)0x0004)
  149. #define I2S_FLAG_UDR ((uint16_t)0x0008)
  150. #define SPI_FLAG_CRCERR ((uint16_t)0x0010)
  151. #define SPI_FLAG_MODF ((uint16_t)0x0020)
  152. #define SPI_I2S_FLAG_OVR ((uint16_t)0x0040)
  153. #define SPI_I2S_FLAG_BSY ((uint16_t)0x0080)
  154. void SPI_I2S_DeInit(SPI_TypeDef* SPIx);
  155. void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);
  156. void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct);
  157. void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct);
  158. void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct);
  159. void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  160. void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  161. void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);
  162. void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);
  163. void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data);
  164. uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx);
  165. void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);
  166. void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  167. void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize);
  168. void SPI_TransmitCRC(SPI_TypeDef* SPIx);
  169. void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState);
  170. uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC);
  171. uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx);
  172. void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction);
  173. FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
  174. void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
  175. ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
  176. void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #endif