stm8l15x_spi.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_spi.h
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file contains all the functions prototypes for the SPI firmware
  8. * library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  13. *
  14. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15. * You may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at:
  17. *
  18. * http://www.st.com/software_license_agreement_liberty_v2
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. ******************************************************************************
  27. */
  28. /* Define to prevent recursive inclusion -------------------------------------*/
  29. #ifndef __STM8L15x_SPI_H
  30. #define __STM8L15x_SPI_H
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "stm8l15x.h"
  33. /** @addtogroup STM8L15x_StdPeriph_Driver
  34. * @{
  35. */
  36. /** @addtogroup SPI
  37. * @{
  38. */
  39. /* Exported types ------------------------------------------------------------*/
  40. /** @defgroup SPI_Exported_Types
  41. * @{
  42. */
  43. /** @defgroup SPI_Direction_Mode
  44. * @brief element values correspond to BDM, BDOE, RXONLY bits position
  45. * @{
  46. */
  47. typedef enum {
  48. SPI_Direction_2Lines_FullDuplex = (uint8_t)0x00, /*!< 2-line uni-directional data mode enable */
  49. SPI_Direction_2Lines_RxOnly = (uint8_t)0x04, /*!< Receiver only in 2 line uni-directional data mode */
  50. SPI_Direction_1Line_Rx = (uint8_t)0x80, /*!< Receiver only in 1 line bi-directional data mode */
  51. SPI_Direction_1Line_Tx = (uint8_t)0xC0 /*!< Transmit only in 1 line bi-directional data mode */
  52. } SPI_DirectionMode_TypeDef;
  53. /**
  54. * @}
  55. */
  56. /** @defgroup SPI_SlaveSelect_Management
  57. * @brief element values correspond to LSBFIRST bit position
  58. * @{
  59. */
  60. typedef enum
  61. {
  62. SPI_NSS_Soft = (uint8_t)0x02, /*!< Software slave management disabled */
  63. SPI_NSS_Hard = (uint8_t)0x00 /*!< Software slave management enabled */
  64. } SPI_NSS_TypeDef;
  65. /**
  66. * @}
  67. */
  68. /** @defgroup SPI_Direction
  69. * @{
  70. */
  71. typedef enum
  72. {
  73. SPI_Direction_Rx = (uint8_t)0x00, /*!< Select Rx receive direction in bi-directional mode */
  74. SPI_Direction_Tx = (uint8_t)0x01 /*!< Select Tx transmission direction in bi-directional mode */
  75. } SPI_Direction_TypeDef;
  76. /**
  77. * @}
  78. */
  79. /** @defgroup SPI_Mode
  80. * @{
  81. */
  82. typedef enum
  83. {
  84. SPI_Mode_Master = (uint8_t)0x04, /*!< SPI Master configuration */
  85. SPI_Mode_Slave = (uint8_t)0x00 /*!< SPI Slave configuration */
  86. } SPI_Mode_TypeDef;
  87. /**
  88. * @}
  89. */
  90. /** @defgroup SPI_BaudRate_Prescaler
  91. * @{
  92. */
  93. typedef enum {
  94. SPI_BaudRatePrescaler_2 = (uint8_t)0x00, /*!< SPI frequency = frequency(CPU)/2 */
  95. SPI_BaudRatePrescaler_4 = (uint8_t)0x08, /*!< SPI frequency = frequency(CPU)/4 */
  96. SPI_BaudRatePrescaler_8 = (uint8_t)0x10, /*!< SPI frequency = frequency(CPU)/8 */
  97. SPI_BaudRatePrescaler_16 = (uint8_t)0x18, /*!< SPI frequency = frequency(CPU)/16 */
  98. SPI_BaudRatePrescaler_32 = (uint8_t)0x20, /*!< SPI frequency = frequency(CPU)/32 */
  99. SPI_BaudRatePrescaler_64 = (uint8_t)0x28, /*!< SPI frequency = frequency(CPU)/64 */
  100. SPI_BaudRatePrescaler_128 = (uint8_t)0x30, /*!< SPI frequency = frequency(CPU)/128 */
  101. SPI_BaudRatePrescaler_256 = (uint8_t)0x38 /*!< SPI frequency = frequency(CPU)/256 */
  102. } SPI_BaudRatePrescaler_TypeDef;
  103. /**
  104. * @}
  105. */
  106. /** @defgroup SPI_Clock_Polarity
  107. * @{
  108. */
  109. typedef enum
  110. {
  111. SPI_CPOL_Low = (uint8_t)0x00, /*!< Clock to 0 when idle */
  112. SPI_CPOL_High = (uint8_t)0x02 /*!< Clock to 1 when idle */
  113. } SPI_CPOL_TypeDef;
  114. /**
  115. * @}
  116. */
  117. /** @defgroup SPI_Clock_Phase
  118. * @{
  119. */
  120. typedef enum
  121. {
  122. SPI_CPHA_1Edge = (uint8_t)0x00, /*!< The first clock transition is the first data capture edge */
  123. SPI_CPHA_2Edge = (uint8_t)0x01 /*!< The second clock transition is the first data capture edge */
  124. } SPI_CPHA_TypeDef;
  125. /**
  126. * @}
  127. */
  128. /** @defgroup SPI_Frame_Format
  129. * @{
  130. */
  131. typedef enum
  132. {
  133. SPI_FirstBit_MSB = (uint8_t)0x00, /*!< MSB bit will be transmitted first */
  134. SPI_FirstBit_LSB = (uint8_t)0x80 /*!< LSB bit will be transmitted first */
  135. } SPI_FirstBit_TypeDef;
  136. /**
  137. * @}
  138. */
  139. /** @defgroup SPI_DMA_requests
  140. * @{
  141. */
  142. typedef enum {
  143. SPI_DMAReq_RX = (uint8_t)0x01, /*!< SPI DMA Rx transfer requests */
  144. SPI_DMAReq_TX = (uint8_t)0x02 /*!< SPI DMA Tx transfer requests */
  145. } SPI_DMAReq_TypeDef;
  146. /**
  147. * @}
  148. */
  149. /** @defgroup SPI_CRC
  150. * @{
  151. */
  152. typedef enum {
  153. SPI_CRC_RX = (uint8_t)0x00, /*!< Select Tx CRC register */
  154. SPI_CRC_TX = (uint8_t)0x01 /*!< Select Rx CRC register */
  155. } SPI_CRC_TypeDef;
  156. /**
  157. * @}
  158. */
  159. /** @defgroup SPI_Flags
  160. * @{
  161. */
  162. typedef enum {
  163. SPI_FLAG_BSY = (uint8_t)0x80, /*!< Busy flag */
  164. SPI_FLAG_OVR = (uint8_t)0x40, /*!< Overrun flag */
  165. SPI_FLAG_MODF = (uint8_t)0x20, /*!< Mode fault */
  166. SPI_FLAG_CRCERR = (uint8_t)0x10, /*!< CRC error flag */
  167. SPI_FLAG_WKUP = (uint8_t)0x08, /*!< Wake-up flag */
  168. SPI_FLAG_TXE = (uint8_t)0x02, /*!< Transmit buffer empty */
  169. SPI_FLAG_RXNE = (uint8_t)0x01 /*!< Receive buffer empty */
  170. } SPI_FLAG_TypeDef;
  171. /**
  172. * @}
  173. */
  174. /** @defgroup SPI_Interrupts
  175. * @brief SPI_IT possible values
  176. * Elements values convention: 0xYX
  177. * X: Position of the corresponding Interrupt
  178. * Y: ITPENDINGBIT position
  179. * @{
  180. */
  181. typedef enum
  182. {
  183. SPI_IT_WKUP = (uint8_t)0x34, /*!< Wake-up interrupt*/
  184. SPI_IT_OVR = (uint8_t)0x65, /*!< Overrun interrupt*/
  185. SPI_IT_MODF = (uint8_t)0x55, /*!< Mode fault interrupt*/
  186. SPI_IT_CRCERR = (uint8_t)0x45, /*!< CRC error interrupt*/
  187. SPI_IT_TXE = (uint8_t)0x17, /*!< Transmit buffer empty interrupt*/
  188. SPI_IT_RXNE = (uint8_t)0x06, /*!< Receive buffer not empty interrupt*/
  189. SPI_IT_ERR = (uint8_t)0x05 /*!< Error interrupt*/
  190. } SPI_IT_TypeDef;
  191. /**
  192. * @}
  193. */
  194. /**
  195. * @}
  196. */
  197. /* Exported constants --------------------------------------------------------*/
  198. /* Exported macros -----------------------------------------------------------*/
  199. /** @defgroup SPI_Exported_Macros
  200. * @brief Macros used by the assert_param function to check the different functions parameters.
  201. * @{
  202. */
  203. /**
  204. * @brief Macro used by the assert_param function in order to check the data direction mode values
  205. */
  206. #define IS_SPI_DATA_DIRECTION(Mode) (((Mode) == SPI_Direction_2Lines_FullDuplex) || \
  207. ((Mode) == SPI_Direction_2Lines_RxOnly) || \
  208. ((Mode) == SPI_Direction_1Line_Rx) || \
  209. ((Mode) == SPI_Direction_1Line_Tx))
  210. /**
  211. * @brief Macro used by the assert_param function in order to check the mode half duplex data direction values
  212. */
  213. #define IS_SPI_DIRECTION(Direction) (((Direction) == SPI_Direction_Rx) || \
  214. ((Direction) == SPI_Direction_Tx))
  215. /**
  216. * @brief Macro used by the assert_param function in order to check the NSS management values
  217. */
  218. #define IS_SPI_SLAVEMANAGEMENT(NSS) (((NSS) == SPI_NSS_Soft) || \
  219. ((NSS) == SPI_NSS_Hard))
  220. /**
  221. * @brief Macro used by the assert_param function in order to check the different
  222. * sensitivity values for the CRC polynomial
  223. */
  224. #define IS_SPI_CRC_POLYNOMIAL(Polynomial) ((Polynomial) > (uint8_t)0x00)
  225. /**
  226. * @brief Macro used by the assert_param function in order to check the SPI Mode values
  227. */
  228. #define IS_SPI_MODE(Mode) (((Mode) == SPI_Mode_Master) || \
  229. ((Mode) == SPI_Mode_Slave))
  230. /**
  231. * @brief Macro used by the assert_param function in order to check the baudrate values
  232. */
  233. #define IS_SPI_BAUDRATE_PRESCALER(Prescaler) (((Prescaler) == SPI_BaudRatePrescaler_2) || \
  234. ((Prescaler) == SPI_BaudRatePrescaler_4) || \
  235. ((Prescaler) == SPI_BaudRatePrescaler_8) || \
  236. ((Prescaler) == SPI_BaudRatePrescaler_16) || \
  237. ((Prescaler) == SPI_BaudRatePrescaler_32) || \
  238. ((Prescaler) == SPI_BaudRatePrescaler_64) || \
  239. ((Prescaler) == SPI_BaudRatePrescaler_128) || \
  240. ((Prescaler) == SPI_BaudRatePrescaler_256))
  241. /**
  242. * @brief Macro used by the assert_param function in order to check the polarity values
  243. */
  244. #define IS_SPI_POLARITY(ClkPol) (((ClkPol) == SPI_CPOL_Low) || \
  245. ((ClkPol) == SPI_CPOL_High))
  246. /**
  247. * @brief Macro used by the assert_param function in order to check the phase values
  248. */
  249. #define IS_SPI_PHASE(ClkPha) (((ClkPha) == SPI_CPHA_1Edge) || \
  250. ((ClkPha) == SPI_CPHA_2Edge))
  251. /**
  252. * @brief Macro used by the assert_param function in order to check the first bit
  253. * to be transmitted values
  254. */
  255. #define IS_SPI_FIRSTBIT(Bit) (((Bit) == SPI_FirstBit_MSB) || \
  256. ((Bit) == SPI_FirstBit_LSB))
  257. /**
  258. * @brief Macro used by the assert_param function in order to check the CRC Transmit/Receive
  259. */
  260. #define IS_SPI_CRC(CRC) (((CRC) == SPI_CRC_TX) || \
  261. ((CRC) == SPI_CRC_RX))
  262. /**
  263. * @brief Macro used by the assert_param function in order to check the DMA transfer requests
  264. */
  265. #define IS_SPI_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFC) == 0x00) && ((DMAREQ) != 0x00))
  266. /**
  267. * @brief Macro used by the assert_param function in order to check the different flags values
  268. */
  269. #define IS_SPI_FLAG(Flag) (((Flag) == SPI_FLAG_OVR) || \
  270. ((Flag) == SPI_FLAG_MODF) || \
  271. ((Flag) == SPI_FLAG_CRCERR) || \
  272. ((Flag) == SPI_FLAG_WKUP) || \
  273. ((Flag) == SPI_FLAG_TXE) || \
  274. ((Flag) == SPI_FLAG_RXNE) || \
  275. ((Flag) == SPI_FLAG_BSY))
  276. /**
  277. * @brief Macro used by the assert_param function in order to check the different
  278. * sensitivity values for the flag that can be cleared by writing 0
  279. */
  280. #define IS_SPI_CLEAR_FLAG(Flag) (((Flag) == SPI_FLAG_CRCERR) || \
  281. ((Flag) == SPI_FLAG_WKUP))
  282. /**
  283. * @brief Macro used by the assert_param function in order to check the different
  284. * sensitivity values for the Interrupts
  285. */
  286. #define IS_SPI_CONFIG_IT(Interrupt) (((Interrupt) == SPI_IT_TXE) || \
  287. ((Interrupt) == SPI_IT_RXNE) || \
  288. ((Interrupt) == SPI_IT_ERR) || \
  289. ((Interrupt) == SPI_IT_WKUP))
  290. /**
  291. * @brief Macro used by the assert_param function in order to check the different
  292. * sensitivity values for the pending bit
  293. */
  294. #define IS_SPI_GET_IT(ITPendingBit) (((ITPendingBit) == SPI_IT_OVR) || \
  295. ((ITPendingBit) == SPI_IT_MODF) || \
  296. ((ITPendingBit) == SPI_IT_CRCERR) || \
  297. ((ITPendingBit) == SPI_IT_WKUP) || \
  298. ((ITPendingBit) == SPI_IT_TXE) || \
  299. ((ITPendingBit) == SPI_IT_RXNE))
  300. /**
  301. * @brief Macro used by the assert_param function in order to check the different
  302. * sensitivity values for the pending bit that can be cleared by writing 0
  303. */
  304. #define IS_SPI_CLEAR_IT(ITPendingBit) (((ITPendingBit) == SPI_IT_CRCERR) || \
  305. ((ITPendingBit) == SPI_IT_WKUP))
  306. /**
  307. * @}
  308. */
  309. /* Exported functions ------------------------------------------------------- */
  310. /* Function used to set the SPI configuration to the default reset state *****/
  311. void SPI_DeInit(SPI_TypeDef* SPIx);
  312. /* Initialization and Configuration functions *********************************/
  313. void SPI_Init(SPI_TypeDef* SPIx, SPI_FirstBit_TypeDef SPI_FirstBit,
  314. SPI_BaudRatePrescaler_TypeDef SPI_BaudRatePrescaler,
  315. SPI_Mode_TypeDef SPI_Mode, SPI_CPOL_TypeDef SPI_CPOL,
  316. SPI_CPHA_TypeDef SPI_CPHA, SPI_DirectionMode_TypeDef SPI_Data_Direction,
  317. SPI_NSS_TypeDef SPI_Slave_Management, uint8_t CRCPolynomial);
  318. void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  319. void SPI_NSSInternalSoftwareCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  320. void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, SPI_Direction_TypeDef SPI_Direction);
  321. /* Data transfers functions ***************************************************/
  322. void SPI_SendData(SPI_TypeDef* SPIx, uint8_t Data);
  323. uint8_t SPI_ReceiveData(SPI_TypeDef* SPIx);
  324. /* Hardware CRC Calculation functions *****************************************/
  325. void SPI_TransmitCRC(SPI_TypeDef* SPIx);
  326. void SPI_CalculateCRCCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  327. uint8_t SPI_GetCRC(SPI_TypeDef* SPIx, SPI_CRC_TypeDef SPI_CRC);
  328. void SPI_ResetCRC(SPI_TypeDef* SPIx);
  329. uint8_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx);
  330. /* DMA transfer management functions *****************************************/
  331. void SPI_DMACmd(SPI_TypeDef* SPIx, SPI_DMAReq_TypeDef SPI_DMAReq, FunctionalState NewState);
  332. /* Interrupts and flags management functions **********************************/
  333. void SPI_ITConfig(SPI_TypeDef* SPIx, SPI_IT_TypeDef SPI_IT, FunctionalState NewState);
  334. FlagStatus SPI_GetFlagStatus(SPI_TypeDef* SPIx, SPI_FLAG_TypeDef SPI_FLAG);
  335. void SPI_ClearFlag(SPI_TypeDef* SPIx, SPI_FLAG_TypeDef SPI_FLAG);
  336. ITStatus SPI_GetITStatus(SPI_TypeDef* SPIx, SPI_IT_TypeDef SPI_IT);
  337. void SPI_ClearITPendingBit(SPI_TypeDef* SPIx, SPI_IT_TypeDef SPI_IT);
  338. #endif /* __STM8L15x_SPI_H */
  339. /**
  340. * @}
  341. */
  342. /**
  343. * @}
  344. */
  345. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/