HAL_spi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /**
  2. ******************************************************************************
  3. * @file HAL_spi.h
  4. * @author AE Team
  5. * @version V1.1.0
  6. * @date 28/08/2019
  7. * @brief This file contains all the functions prototypes for the SPI firmware
  8. * library.
  9. ******************************************************************************
  10. * @copy
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2019 MindMotion</center></h2>
  20. */
  21. /* Define to prevent recursive inclusion -------------------------------------*/
  22. #ifndef __HAL_SPI_H
  23. #define __HAL_SPI_H
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "HAL_device.h"
  26. /** @addtogroup StdPeriph_Driver
  27. * @{
  28. */
  29. /** @addtogroup SPI
  30. * @{
  31. */
  32. /** @defgroup SPI_Exported_Types
  33. * @{
  34. */
  35. /**
  36. * @brief SPI Init structure definition
  37. */
  38. typedef struct
  39. {
  40. uint16_t SPI_Mode;
  41. uint16_t SPI_DataSize;
  42. uint16_t SPI_DataWidth;
  43. uint16_t SPI_CPOL;
  44. uint16_t SPI_CPHA;
  45. uint16_t SPI_NSS;
  46. uint16_t SPI_BaudRatePrescaler;
  47. uint16_t SPI_FirstBit;
  48. } SPI_InitTypeDef;
  49. /**
  50. * @}
  51. */
  52. /** @defgroup SPI_Exported_Constants
  53. * @{
  54. */
  55. #define IS_SPI_ALL_PERIPH(PERIPH) (((*(uint32_t*)&(PERIPH)) == SPI0_BASE) || \
  56. ((*(uint32_t*)&(PERIPH)) == SPI1_BASE))
  57. #define IS_SPI_2_PERIPH(PERIPH) ((*(uint32_t*)&(PERIPH)) == SPI1_BASE)
  58. /**
  59. * @}
  60. */
  61. /** @defgroup SPI_master_slave_mode
  62. * @{
  63. */
  64. #define SPI_Mode_Master ((uint16_t)0x0004)
  65. #define SPI_Mode_Slave ((uint16_t)0x0000)
  66. #define IS_SPI_MODE(MODE) (((MODE) == SPI_Mode_Master) || \
  67. ((MODE) == SPI_Mode_Slave))
  68. /**
  69. * @}
  70. */
  71. /** @defgroup SPI_data_size
  72. * @{
  73. */
  74. #define SPI_DataSize_32b ((uint16_t)0x0800)
  75. #define SPI_DataSize_8b ((uint16_t)0x0000)
  76. #define IS_SPI_DATASIZE(DATASIZE) (((DATASIZE) == SPI_DataSize_32b) || \
  77. ((DATASIZE) == SPI_DataSize_8b))
  78. /**
  79. * @}
  80. */
  81. /** @defgroup SPI_7bit_8bit data width
  82. * @{
  83. */
  84. #define SPI_DataWidth_1b ((uint16_t)0x0001)
  85. #define SPI_DataWidth_2b ((uint16_t)0x0002)
  86. #define SPI_DataWidth_3b ((uint16_t)0x0003)
  87. #define SPI_DataWidth_4b ((uint16_t)0x0004)
  88. #define SPI_DataWidth_5b ((uint16_t)0x0005)
  89. #define SPI_DataWidth_6b ((uint16_t)0x0006)
  90. #define SPI_DataWidth_7b ((uint16_t)0x0007)
  91. #define SPI_DataWidth_8b ((uint16_t)0x0008)
  92. #define SPI_DataWidth_9b ((uint16_t)0x0009)
  93. #define SPI_DataWidth_10b ((uint16_t)0x000a)
  94. #define SPI_DataWidth_11b ((uint16_t)0x000b)
  95. #define SPI_DataWidth_12b ((uint16_t)0x000c)
  96. #define SPI_DataWidth_13b ((uint16_t)0x000d)
  97. #define SPI_DataWidth_14b ((uint16_t)0x000e)
  98. #define SPI_DataWidth_15b ((uint16_t)0x000f)
  99. #define SPI_DataWidth_16b ((uint16_t)0x0010)
  100. #define SPI_DataWidth_17b ((uint16_t)0x0011)
  101. #define SPI_DataWidth_18b ((uint16_t)0x0012)
  102. #define SPI_DataWidth_19b ((uint16_t)0x0013)
  103. #define SPI_DataWidth_20b ((uint16_t)0x0014)
  104. #define SPI_DataWidth_21b ((uint16_t)0x0015)
  105. #define SPI_DataWidth_22b ((uint16_t)0x0016)
  106. #define SPI_DataWidth_23b ((uint16_t)0x0017)
  107. #define SPI_DataWidth_24b ((uint16_t)0x0018)
  108. #define SPI_DataWidth_25b ((uint16_t)0x0019)
  109. #define SPI_DataWidth_26b ((uint16_t)0x001a)
  110. #define SPI_DataWidth_27b ((uint16_t)0x001b)
  111. #define SPI_DataWidth_28b ((uint16_t)0x001c)
  112. #define SPI_DataWidth_29b ((uint16_t)0x001d)
  113. #define SPI_DataWidth_30b ((uint16_t)0x001e)
  114. #define SPI_DataWidth_31b ((uint16_t)0x001f)
  115. #define SPI_DataWidth_32b ((uint16_t)0x0000)
  116. #define IS_SPI_DATAWIDRH(WIDTH) (((WIDTH) == SPI_DataWidth_1b) || ((WIDTH) == SPI_DataWidth_2b)||\
  117. ((WIDTH) == SPI_DataWidth_3b)||((WIDTH) == SPI_DataWidth_4b)||\
  118. ((WIDTH) == SPI_DataWidth_5b)||((WIDTH) == SPI_DataWidth_6b)||\
  119. ((WIDTH) == SPI_DataWidth_7b)||((WIDTH) == SPI_DataWidth_8b)||\
  120. ((WIDTH) == SPI_DataWidth_9b)||((WIDTH) == SPI_DataWidth_10b)||\
  121. ((WIDTH) == SPI_DataWidth_11b)||((WIDTH) == SPI_DataWidth_12b)||\
  122. ((WIDTH) == SPI_DataWidth_13b)||((WIDTH) == SPI_DataWidth_14b)||\
  123. ((WIDTH) == SPI_DataWidth_15b)||((WIDTH) == SPI_DataWidth_16b)||\
  124. ((WIDTH) == SPI_DataWidth_17b)||((WIDTH) == SPI_DataWidth_18b)||\
  125. ((WIDTH) == SPI_DataWidth_19b)||((WIDTH) == SPI_DataWidth_20b)||\
  126. ((WIDTH) == SPI_DataWidth_21b)||((WIDTH) == SPI_DataWidth_22b)||\
  127. ((WIDTH) == SPI_DataWidth_23b)||((WIDTH) == SPI_DataWidth_24b)||\
  128. ((WIDTH) == SPI_DataWidth_25b)||((WIDTH) == SPI_DataWidth_26b)||\
  129. ((WIDTH) == SPI_DataWidth_27b)||((WIDTH) == SPI_DataWidth_28b)||\
  130. ((WIDTH) == SPI_DataWidth_29b)||((WIDTH) == SPI_DataWidth_30b)||\
  131. ((WIDTH) == SPI_DataWidth_31b)||((WIDTH) == SPI_DataWidth_32b)||)
  132. /**
  133. * @}
  134. */
  135. /** @defgroup SPI_Clock_Polarity
  136. * @{
  137. */
  138. #define SPI_CPOL_Low ((uint16_t)0x0000)
  139. #define SPI_CPOL_High ((uint16_t)0x0002)
  140. #define IS_SPI_CPOL(CPOL) (((CPOL) == SPI_CPOL_Low) || \
  141. ((CPOL) == SPI_CPOL_High))
  142. /**
  143. * @}
  144. */
  145. /** @defgroup SPI_Clock_Phase
  146. * @{
  147. */
  148. #define SPI_CPHA_1Edge ((uint16_t)0x0001)
  149. #define SPI_CPHA_2Edge ((uint16_t)0x0000)
  150. #define IS_SPI_CPHA(CPHA) (((CPHA) == SPI_CPHA_1Edge) || \
  151. ((CPHA) == SPI_CPHA_2Edge))
  152. /**
  153. * @}
  154. */
  155. /** @defgroup SPI_Slave_Select_management
  156. * @{
  157. */
  158. #define SPI_NSS_Soft ((uint16_t)0x0000)
  159. #define SPI_NSS_Hard ((uint16_t)0x0400)
  160. #define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_Soft) || \
  161. ((NSS) == SPI_NSS_Hard))
  162. /**
  163. * @}
  164. */
  165. /** @defgroup SPI_NSS_internal_software_mangement
  166. * @{
  167. */
  168. #define SPI_NSSInternalSoft_Set ((uint16_t)0x0001)
  169. #define SPI_NSSInternalSoft_Reset ((uint16_t)0xFFFE)
  170. #define IS_SPI_NSS_INTERNAL(INTERNAL) (((INTERNAL) == SPI_NSSInternalSoft_Set) || \
  171. ((INTERNAL) == SPI_NSSInternalSoft_Reset))
  172. /**
  173. * @}
  174. */
  175. /**
  176. * @}
  177. */
  178. /** @defgroup SPI_BaudRate_Prescaler_
  179. * @{
  180. */
  181. #define SPI_BaudRatePrescaler_2 ((uint16_t)0x0002)
  182. #define SPI_BaudRatePrescaler_4 ((uint16_t)0x0004)
  183. #define SPI_BaudRatePrescaler_8 ((uint16_t)0x0008)
  184. #define SPI_BaudRatePrescaler_16 ((uint16_t)0x0010)
  185. #define SPI_BaudRatePrescaler_32 ((uint16_t)0x0020)
  186. #define SPI_BaudRatePrescaler_64 ((uint16_t)0x0040)
  187. #define SPI_BaudRatePrescaler_128 ((uint16_t)0x0080)
  188. #define SPI_BaudRatePrescaler_256 ((uint16_t)0x0100)
  189. #define IS_SPI_BAUDRATE_PRESCALER(PRESCALER) (((PRESCALER) == SPI_BaudRatePrescaler_2) || \
  190. ((PRESCALER) == SPI_BaudRatePrescaler_4) || \
  191. ((PRESCALER) == SPI_BaudRatePrescaler_8) || \
  192. ((PRESCALER) == SPI_BaudRatePrescaler_16) || \
  193. ((PRESCALER) == SPI_BaudRatePrescaler_32) || \
  194. ((PRESCALER) == SPI_BaudRatePrescaler_64) || \
  195. ((PRESCALER) == SPI_BaudRatePrescaler_128) || \
  196. ((PRESCALER) == SPI_BaudRatePrescaler_256))
  197. /**
  198. * @}
  199. */
  200. /** @defgroup SPI_MSB_LSB_transmission
  201. * @{
  202. */
  203. #define SPI_FirstBit_MSB ((uint16_t)0x0000)
  204. #define SPI_FirstBit_LSB ((uint16_t)0x0004)
  205. #define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FirstBit_MSB) || \
  206. ((BIT) == SPI_FirstBit_LSB))
  207. /**
  208. * @}
  209. */
  210. /** @defgroup SPI_DMA_transfer_requests
  211. * @{
  212. */
  213. #define SPI_DMAReq_EN ((uint16_t)0x0200)
  214. #define IS_SPI_DMAREQ(DMAREQ) ((DMAREQ) == SPI_DMAReq_EN)
  215. /**
  216. * @}
  217. */
  218. /** @defgroup SPI TX Fifo and RX Fifo trigger level
  219. * @{
  220. */
  221. #define SPI_TXTLF ((uint16_t)0x0080)
  222. #define SPI_RXTLF ((uint16_t)0x0020)
  223. #define IS_SPI_FIFOTRIGGER(TRIGGER) (((TRIGGER) == SPI_TXTLF) && ((TRIGGER) == SPI_RXTLF))
  224. /**
  225. * @}
  226. */
  227. /** @defgroup SPI_NSS_internal_software_mangement
  228. * @{
  229. */
  230. #define SPI_CS_BIT0 ((uint16_t)0xfffe)
  231. #define SPI_CS_BIT1 ((uint16_t)0xfffd)
  232. #define SPI_CS_BIT2 ((uint16_t)0xfffb)
  233. #define SPI_CS_BIT3 ((uint16_t)0xfff7)
  234. #define SPI_CS_BIT4 ((uint16_t)0xffef)
  235. #define SPI_CS_BIT5 ((uint16_t)0xffdf)
  236. #define SPI_CS_BIT6 ((uint16_t)0xffbf)
  237. #define SPI_CS_BIT7 ((uint16_t)0xff7f)
  238. #define IS_SPI_CS(CS) (((CS) == SPI_CS_BIT0) || ((CS) == SPI_CS_BIT1)||\
  239. ((CS) == SPI_CS_BIT2) || ((CS) == SPI_CS_BIT3)||\
  240. ((CS) == SPI_CS_BIT4) || ((CS) == SPI_CS_BIT5)||\
  241. ((CS) == SPI_CS_BIT6) || ((CS) == SPI_CS_BIT7))
  242. /**
  243. * @}
  244. */
  245. /** @defgroup SPI_direction_transmit_receive
  246. * @{
  247. */
  248. #define SPI_Direction_Rx ((uint16_t)0x0010)
  249. #define SPI_Direction_Tx ((uint16_t)0x0008)
  250. #define SPI_Disable_Tx ((uint16_t)0xfff7)
  251. #define SPI_Disable_Rx ((uint16_t)0xffef)
  252. #define IS_SPI_DIRECTION(DIRECTION) (((DIRECTION) == SPI_Direction_Rx) || \
  253. ((DIRECTION) == SPI_Direction_Tx) || \
  254. ((DIRECTION) == SPI_Disable_Tx) || \
  255. ((DIRECTION) == SPI_Disable_Rx))
  256. /**
  257. * @}
  258. */
  259. /** @defgroup SPI_interrupts_definition
  260. * @{
  261. */
  262. #define SPI_INT_EN ((uint16_t)0x0002)
  263. #define SPI_IT_TX ((uint8_t)0x01)
  264. #define SPI_IT_RX ((uint8_t)0x02)
  265. #define IS_SPI_CONFIG_IT(IT) (((IT) == SPI_IT_TX) || \
  266. ((IT) == SPI_IT_RX))
  267. #define SPI_IT_UNDERRUN ((uint8_t)0x04)
  268. #define SPI_IT_RXOVER ((uint8_t)0x08)
  269. #define SPI_IT_RXMATCH ((uint8_t)0x10)
  270. #define SPI_IT_RXFULL ((uint8_t)0x20)
  271. #define SPI_IT_TXEPT ((uint8_t)0x40)
  272. #define IS_SPI_GET_IT(IT) (((IT) == SPI_IT_TX) || ((IT) == SPI_IT_RX) || \
  273. ((IT) == SPI_IT_UNDERRUN) || ((IT) == SPI_IT_RXOVER) || \
  274. ((IT) == SPI_IT_RXMATCH) || ((IT) == SPI_IT_RXFULL) || \
  275. ((IT) == SPI_IT_TXEPT))
  276. /**
  277. * @}
  278. */
  279. /** @defgroup SPI_flags_definition
  280. * @{
  281. */
  282. #define SPI_FLAG_RXAVL ((uint16_t)0x0002)
  283. #define SPI_FLAG_TXEPT ((uint16_t)0x0001)
  284. #define IS_SPI_GET_FLAG(FLAG) (((FLAG) == SPI_FLAG_RXAVL) || \
  285. ((FLAG) == SPI_FLAG_TXEPT))
  286. /**
  287. * @}
  288. */
  289. /** @defgroup SPI mode tx data transmit phase adjust set
  290. *in slave mode according to txedge bit of CCTL register
  291. * @{
  292. */
  293. #define SPI_SlaveAdjust_FAST ((uint16_t)0x0020)
  294. #define SPI_SlaveAdjust_LOW ((uint16_t)0xffdf)
  295. #define IS_SPI_SlaveAdjust(ADJUST) (((ADJUST) == SPI_SlaveAdjust_FAST) || \
  296. ((ADJUST) == SPI_SlaveAdjust_LOW))
  297. /**
  298. * @}
  299. */
  300. /** @defgroup SPI_Exported_Macros
  301. * @{
  302. */
  303. /**
  304. * @}
  305. */
  306. /** @defgroup SPI_Exported_Functions
  307. * @{
  308. */
  309. void SPI_DeInit(SPI_TypeDef* SPIx);
  310. void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);
  311. void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct);
  312. void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  313. void SPI_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_IT, FunctionalState NewState);
  314. void SPI_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_DMAReq, FunctionalState NewState);
  315. void SPI_FifoTrigger(SPI_TypeDef* SPIx, uint16_t SPI_FifoTriggerValue, FunctionalState NewState);
  316. void SPI_SendData(SPI_TypeDef* SPIx, uint32_t Data);
  317. uint32_t SPI_ReceiveData(SPI_TypeDef* SPIx);
  318. void SPI_CSInternalSelected(SPI_TypeDef* SPIx, uint16_t SPI_CSInternalSelected, FunctionalState NewState);
  319. void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);
  320. void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize);
  321. void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction);
  322. FlagStatus SPI_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_FLAG);
  323. ITStatus SPI_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_IT);
  324. void SPI_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_IT);
  325. void SPI_RxBytes(SPI_TypeDef* SPIx, uint16_t Number);
  326. void SPI_SlaveAdjust(SPI_TypeDef* SPIx, uint16_t AdjustValue);
  327. #endif /*__HAL_SPI_H */
  328. /**
  329. * @}
  330. */
  331. /**
  332. * @}
  333. */
  334. /**
  335. * @}
  336. */
  337. /*-------------------------(C) COPYRIGHT 2019 MindMotion ----------------------*/