stm8s_spi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. ******************************************************************************
  3. * @file stm8s_spi.h
  4. * @author MCD Application Team
  5. * @version V2.2.0
  6. * @date 30-September-2014
  7. * @brief This file contains all functions prototype and macros for the SPI peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Define to prevent recursive inclusion -------------------------------------*/
  28. #ifndef __STM8S_SPI_H
  29. #define __STM8S_SPI_H
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm8s.h"
  32. /** @addtogroup STM8S_StdPeriph_Driver
  33. * @{
  34. */
  35. /** @addtogroup SPI_Exported_Types
  36. * @{
  37. */
  38. /**
  39. * @brief SPI data direction mode
  40. * Warning: element values correspond to BDM, BDOE, RXONLY bits position
  41. */
  42. typedef enum {
  43. SPI_DATADIRECTION_2LINES_FULLDUPLEX = (uint8_t)0x00, /*!< 2-line uni-directional data mode enable */
  44. SPI_DATADIRECTION_2LINES_RXONLY = (uint8_t)0x04, /*!< Receiver only in 2 line uni-directional data mode */
  45. SPI_DATADIRECTION_1LINE_RX = (uint8_t)0x80, /*!< Receiver only in 1 line bi-directional data mode */
  46. SPI_DATADIRECTION_1LINE_TX = (uint8_t)0xC0 /*!< Transmit only in 1 line bi-directional data mode */
  47. } SPI_DataDirection_TypeDef;
  48. /**
  49. * @brief SPI Slave Select management
  50. * Warning: element values correspond to LSBFIRST bit position
  51. */
  52. typedef enum
  53. {
  54. SPI_NSS_SOFT = (uint8_t)0x02, /*!< Software slave management disabled */
  55. SPI_NSS_HARD = (uint8_t)0x00 /*!< Software slave management enabled */
  56. } SPI_NSS_TypeDef;
  57. /**
  58. * @brief SPI direction transmit/receive
  59. */
  60. typedef enum {
  61. SPI_DIRECTION_RX = (uint8_t)0x00, /*!< Selects Rx receive direction in bi-directional mode */
  62. SPI_DIRECTION_TX = (uint8_t)0x01 /*!< Selects Tx transmission direction in bi-directional mode */
  63. } SPI_Direction_TypeDef;
  64. /**
  65. * @brief SPI master/slave mode
  66. * Warning: element values correspond to MSTR bit position
  67. */
  68. typedef enum {
  69. SPI_MODE_MASTER = (uint8_t)0x04, /*!< SPI Master configuration */
  70. SPI_MODE_SLAVE = (uint8_t)0x00 /*!< SPI Slave configuration */
  71. } SPI_Mode_TypeDef;
  72. /**
  73. * @brief SPI BaudRate Prescaler
  74. * Warning: element values correspond to BR bits position
  75. */
  76. typedef enum {
  77. SPI_BAUDRATEPRESCALER_2 = (uint8_t)0x00, /*!< SPI frequency = frequency(CPU)/2 */
  78. SPI_BAUDRATEPRESCALER_4 = (uint8_t)0x08, /*!< SPI frequency = frequency(CPU)/4 */
  79. SPI_BAUDRATEPRESCALER_8 = (uint8_t)0x10, /*!< SPI frequency = frequency(CPU)/8 */
  80. SPI_BAUDRATEPRESCALER_16 = (uint8_t)0x18, /*!< SPI frequency = frequency(CPU)/16 */
  81. SPI_BAUDRATEPRESCALER_32 = (uint8_t)0x20, /*!< SPI frequency = frequency(CPU)/32 */
  82. SPI_BAUDRATEPRESCALER_64 = (uint8_t)0x28, /*!< SPI frequency = frequency(CPU)/64 */
  83. SPI_BAUDRATEPRESCALER_128 = (uint8_t)0x30, /*!< SPI frequency = frequency(CPU)/128 */
  84. SPI_BAUDRATEPRESCALER_256 = (uint8_t)0x38 /*!< SPI frequency = frequency(CPU)/256 */
  85. } SPI_BaudRatePrescaler_TypeDef;
  86. /**
  87. * @brief SPI Clock Polarity
  88. * Warning: element values correspond to CPOL bit position
  89. */
  90. typedef enum {
  91. SPI_CLOCKPOLARITY_LOW = (uint8_t)0x00, /*!< Clock to 0 when idle */
  92. SPI_CLOCKPOLARITY_HIGH = (uint8_t)0x02 /*!< Clock to 1 when idle */
  93. } SPI_ClockPolarity_TypeDef;
  94. /**
  95. * @brief SPI Clock Phase
  96. * Warning: element values correspond to CPHA bit position
  97. */
  98. typedef enum {
  99. SPI_CLOCKPHASE_1EDGE = (uint8_t)0x00, /*!< The first clock transition is the first data capture edge */
  100. SPI_CLOCKPHASE_2EDGE = (uint8_t)0x01 /*!< The second clock transition is the first data capture edge */
  101. } SPI_ClockPhase_TypeDef;
  102. /**
  103. * @brief SPI Frame Format: MSB or LSB transmitted first
  104. * Warning: element values correspond to LSBFIRST bit position
  105. */
  106. typedef enum {
  107. SPI_FIRSTBIT_MSB = (uint8_t)0x00, /*!< MSB bit will be transmitted first */
  108. SPI_FIRSTBIT_LSB = (uint8_t)0x80 /*!< LSB bit will be transmitted first */
  109. } SPI_FirstBit_TypeDef;
  110. /**
  111. * @brief SPI CRC Transmit/Receive
  112. */
  113. typedef enum {
  114. SPI_CRC_RX = (uint8_t)0x00, /*!< Select Tx CRC register */
  115. SPI_CRC_TX = (uint8_t)0x01 /*!< Select Rx CRC register */
  116. } SPI_CRC_TypeDef;
  117. /**
  118. * @brief SPI flags definition - Warning : FLAG value = mapping position register
  119. */
  120. typedef enum {
  121. SPI_FLAG_BSY = (uint8_t)0x80, /*!< Busy flag */
  122. SPI_FLAG_OVR = (uint8_t)0x40, /*!< Overrun flag */
  123. SPI_FLAG_MODF = (uint8_t)0x20, /*!< Mode fault */
  124. SPI_FLAG_CRCERR = (uint8_t)0x10, /*!< CRC error flag */
  125. SPI_FLAG_WKUP = (uint8_t)0x08, /*!< Wake-up flag */
  126. SPI_FLAG_TXE = (uint8_t)0x02, /*!< Transmit buffer empty */
  127. SPI_FLAG_RXNE = (uint8_t)0x01 /*!< Receive buffer empty */
  128. } SPI_Flag_TypeDef;
  129. /**
  130. * @brief SPI_IT possible values
  131. * Elements values convention: 0xYX
  132. * X: Position of the corresponding Interrupt
  133. * Y: ITPENDINGBIT position
  134. */
  135. typedef enum
  136. {
  137. SPI_IT_WKUP = (uint8_t)0x34, /*!< Wake-up interrupt*/
  138. SPI_IT_OVR = (uint8_t)0x65, /*!< Overrun interrupt*/
  139. SPI_IT_MODF = (uint8_t)0x55, /*!< Mode fault interrupt*/
  140. SPI_IT_CRCERR = (uint8_t)0x45, /*!< CRC error interrupt*/
  141. SPI_IT_TXE = (uint8_t)0x17, /*!< Transmit buffer empty interrupt*/
  142. SPI_IT_RXNE = (uint8_t)0x06, /*!< Receive buffer not empty interrupt*/
  143. SPI_IT_ERR = (uint8_t)0x05 /*!< Error interrupt*/
  144. } SPI_IT_TypeDef;
  145. /**
  146. * @}
  147. */
  148. /* Private define ------------------------------------------------------------*/
  149. /** @addtogroup SPI_Private_Macros
  150. * @brief Macros used by the assert_param function to check the different functions parameters.
  151. * @{
  152. */
  153. /**
  154. * @brief Macro used by the assert_param function in order to check the data direction mode values
  155. */
  156. #define IS_SPI_DATA_DIRECTION_OK(MODE) (((MODE) == SPI_DATADIRECTION_2LINES_FULLDUPLEX) || \
  157. ((MODE) == SPI_DATADIRECTION_2LINES_RXONLY) || \
  158. ((MODE) == SPI_DATADIRECTION_1LINE_RX) || \
  159. ((MODE) == SPI_DATADIRECTION_1LINE_TX))
  160. /**
  161. * @brief Macro used by the assert_param function in order to check the mode
  162. * half duplex data direction values
  163. */
  164. #define IS_SPI_DIRECTION_OK(DIRECTION) (((DIRECTION) == SPI_DIRECTION_RX) || \
  165. ((DIRECTION) == SPI_DIRECTION_TX))
  166. /**
  167. * @brief Macro used by the assert_param function in order to check the NSS
  168. * management values
  169. */
  170. #define IS_SPI_SLAVEMANAGEMENT_OK(NSS) (((NSS) == SPI_NSS_SOFT) || \
  171. ((NSS) == SPI_NSS_HARD))
  172. /**
  173. * @brief Macro used by the assert_param function in order to check the different
  174. * sensitivity values for the CRC polynomial
  175. */
  176. #define IS_SPI_CRC_POLYNOMIAL_OK(POLYNOMIAL) ((POLYNOMIAL) > (uint8_t)0x00)
  177. /**
  178. * @brief Macro used by the assert_param function in order to check the SPI Mode values
  179. */
  180. #define IS_SPI_MODE_OK(MODE) (((MODE) == SPI_MODE_MASTER) || \
  181. ((MODE) == SPI_MODE_SLAVE))
  182. /**
  183. * @brief Macro used by the assert_param function in order to check the baudrate values
  184. */
  185. #define IS_SPI_BAUDRATE_PRESCALER_OK(PRESCALER) (((PRESCALER) == SPI_BAUDRATEPRESCALER_2) || \
  186. ((PRESCALER) == SPI_BAUDRATEPRESCALER_4) || \
  187. ((PRESCALER) == SPI_BAUDRATEPRESCALER_8) || \
  188. ((PRESCALER) == SPI_BAUDRATEPRESCALER_16) || \
  189. ((PRESCALER) == SPI_BAUDRATEPRESCALER_32) || \
  190. ((PRESCALER) == SPI_BAUDRATEPRESCALER_64) || \
  191. ((PRESCALER) == SPI_BAUDRATEPRESCALER_128) || \
  192. ((PRESCALER) == SPI_BAUDRATEPRESCALER_256))
  193. /**
  194. * @brief Macro used by the assert_param function in order to check the polarity values
  195. */
  196. #define IS_SPI_POLARITY_OK(CLKPOL) (((CLKPOL) == SPI_CLOCKPOLARITY_LOW) || \
  197. ((CLKPOL) == SPI_CLOCKPOLARITY_HIGH))
  198. /**
  199. * @brief Macro used by the assert_param function in order to check the phase values
  200. */
  201. #define IS_SPI_PHASE_OK(CLKPHA) (((CLKPHA) == SPI_CLOCKPHASE_1EDGE) || \
  202. ((CLKPHA) == SPI_CLOCKPHASE_2EDGE))
  203. /**
  204. * @brief Macro used by the assert_param function in order to check the first
  205. * bit to be transmited values
  206. */
  207. #define IS_SPI_FIRSTBIT_OK(BIT) (((BIT) == SPI_FIRSTBIT_MSB) || \
  208. ((BIT) == SPI_FIRSTBIT_LSB))
  209. /**
  210. * @brief Macro used by the assert_param function in order to check the CRC
  211. * Transmit/Receive
  212. */
  213. #define IS_SPI_CRC_OK(CRC) (((CRC) == SPI_CRC_TX) || \
  214. ((CRC) == SPI_CRC_RX))
  215. /**
  216. * @brief Macro used by the assert_param function in order to check the
  217. * different flags values
  218. */
  219. #define IS_SPI_FLAGS_OK(FLAG) (((FLAG) == SPI_FLAG_OVR) || \
  220. ((FLAG) == SPI_FLAG_MODF) || \
  221. ((FLAG) == SPI_FLAG_CRCERR) || \
  222. ((FLAG) == SPI_FLAG_WKUP) || \
  223. ((FLAG) == SPI_FLAG_TXE) || \
  224. ((FLAG) == SPI_FLAG_RXNE) || \
  225. ((FLAG) == SPI_FLAG_BSY))
  226. /**
  227. * @brief Macro used by the assert_param function in order to check the
  228. * different sensitivity values for the flag that can be cleared
  229. * by writing 0
  230. */
  231. #define IS_SPI_CLEAR_FLAGS_OK(FLAG) (((FLAG) == SPI_FLAG_CRCERR) || \
  232. ((FLAG) == SPI_FLAG_WKUP))
  233. /**
  234. * @brief Macro used by the assert_param function in order to check the
  235. * different sensitivity values for the Interrupts
  236. */
  237. #define IS_SPI_CONFIG_IT_OK(Interrupt) (((Interrupt) == SPI_IT_TXE) || \
  238. ((Interrupt) == SPI_IT_RXNE) || \
  239. ((Interrupt) == SPI_IT_ERR) || \
  240. ((Interrupt) == SPI_IT_WKUP))
  241. /**
  242. * @brief Macro used by the assert_param function in order to check the
  243. * different sensitivity values for the pending bit
  244. */
  245. #define IS_SPI_GET_IT_OK(ITPendingBit) (((ITPendingBit) == SPI_IT_OVR) || \
  246. ((ITPendingBit) == SPI_IT_MODF) || \
  247. ((ITPendingBit) == SPI_IT_CRCERR) || \
  248. ((ITPendingBit) == SPI_IT_WKUP) || \
  249. ((ITPendingBit) == SPI_IT_TXE) || \
  250. ((ITPendingBit) == SPI_IT_RXNE))
  251. /**
  252. * @brief Macro used by the assert_param function in order to check the
  253. * different sensitivity values for the pending bit that can be cleared
  254. * by writing 0
  255. */
  256. #define IS_SPI_CLEAR_IT_OK(ITPendingBit) (((ITPendingBit) == SPI_IT_CRCERR) || \
  257. ((ITPendingBit) == SPI_IT_WKUP))
  258. /**
  259. * @}
  260. */
  261. /** @addtogroup SPI_Exported_Functions
  262. * @{
  263. */
  264. void SPI_DeInit(void);
  265. void SPI_Init(SPI_FirstBit_TypeDef FirstBit,
  266. SPI_BaudRatePrescaler_TypeDef BaudRatePrescaler,
  267. SPI_Mode_TypeDef Mode, SPI_ClockPolarity_TypeDef ClockPolarity,
  268. SPI_ClockPhase_TypeDef ClockPhase,
  269. SPI_DataDirection_TypeDef Data_Direction,
  270. SPI_NSS_TypeDef Slave_Management, uint8_t CRCPolynomial);
  271. void SPI_Cmd(FunctionalState NewState);
  272. void SPI_ITConfig(SPI_IT_TypeDef SPI_IT, FunctionalState NewState);
  273. void SPI_SendData(uint8_t Data);
  274. uint8_t SPI_ReceiveData(void);
  275. void SPI_NSSInternalSoftwareCmd(FunctionalState NewState);
  276. void SPI_TransmitCRC(void);
  277. void SPI_CalculateCRCCmd(FunctionalState NewState);
  278. uint8_t SPI_GetCRC(SPI_CRC_TypeDef SPI_CRC);
  279. void SPI_ResetCRC(void);
  280. uint8_t SPI_GetCRCPolynomial(void);
  281. void SPI_BiDirectionalLineConfig(SPI_Direction_TypeDef SPI_Direction);
  282. FlagStatus SPI_GetFlagStatus(SPI_Flag_TypeDef SPI_FLAG);
  283. void SPI_ClearFlag(SPI_Flag_TypeDef SPI_FLAG);
  284. ITStatus SPI_GetITStatus(SPI_IT_TypeDef SPI_IT);
  285. void SPI_ClearITPendingBit(SPI_IT_TypeDef SPI_IT);
  286. /**
  287. * @}
  288. */
  289. #endif /* __STM8S_SPI_H */
  290. /**
  291. * @}
  292. */
  293. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/