spi_interface.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * ESPRSSIF MIT License
  3. *
  4. * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. /**
  25. * @file spi_interface.h
  26. * @brief Defines and Macros for the SPI.
  27. */
  28. #ifndef __SPI_INTERFACE_H__
  29. #define __SPI_INTERFACE_H__
  30. #include "driver/spi_register.h"
  31. #include "c_types.h"
  32. //*****************************************************************************
  33. //
  34. // Make sure all of the definitions in this header have a C binding.
  35. //
  36. //*****************************************************************************
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. /**
  42. * @brief Defines slave commands. Default value based on slave ESP8266.
  43. */
  44. #define MASTER_WRITE_DATA_TO_SLAVE_CMD 2
  45. #define MASTER_READ_DATA_FROM_SLAVE_CMD 3
  46. #define MASTER_WRITE_STATUS_TO_SLAVE_CMD 1
  47. #define MASTER_READ_STATUS_FROM_SLAVE_CMD 4
  48. /**
  49. * @brief Support HSPI and SPI module.
  50. *
  51. */
  52. typedef enum
  53. {
  54. SpiNum_SPI = 0,
  55. SpiNum_HSPI = 1,
  56. } SpiNum;
  57. /**
  58. * @brief The SPI module can work in either master or slave mode.
  59. *
  60. */
  61. typedef enum
  62. {
  63. SpiMode_Master = 0,
  64. SpiMode_Slave = 1,
  65. } SpiMode;
  66. /**
  67. * @brief SPI sub mode
  68. *
  69. * Support 4 sub modes based on SPI clock polarity and phase.
  70. * SPI_CPOL SPI_CPHA SubMode
  71. * 0 0 0
  72. * 0 1 1
  73. * 1 0 2
  74. * 1 1 3
  75. */
  76. typedef enum
  77. {
  78. SpiSubMode_0 = 0,
  79. SpiSubMode_1 = 1,
  80. SpiSubMode_2 = 2,
  81. SpiSubMode_3 = 3,
  82. } SpiSubMode;
  83. /**
  84. * @brief The SPI module working speed.
  85. *
  86. * @attention Max speed 80MHz
  87. *
  88. */
  89. typedef enum
  90. {
  91. SpiSpeed_0_5MHz = 160,
  92. SpiSpeed_1MHz = 80,
  93. SpiSpeed_2MHz = 40,
  94. SpiSpeed_5MHz = 16,
  95. SpiSpeed_8MHz = 10,
  96. SpiSpeed_10MHz = 8,
  97. } SpiSpeed;
  98. /**
  99. * @brief The SPI mode working speed.
  100. *
  101. */
  102. typedef enum
  103. {
  104. SpiBitOrder_MSBFirst = 0,
  105. SpiBitOrder_LSBFirst = 1,
  106. } SpiBitOrder;
  107. // @brief SPI interrupt soource defined.
  108. typedef enum
  109. {
  110. SpiIntSrc_TransDone = SPI_TRANS_DONE,
  111. SpiIntSrc_WrStaDone = SPI_SLV_WR_STA_DONE,
  112. SpiIntSrc_RdStaDone = SPI_SLV_RD_STA_DONE,
  113. SpiIntSrc_WrBufDone = SPI_SLV_WR_BUF_DONE,
  114. SpiIntSrc_RdBufDone = SPI_SLV_RD_BUF_DONE,
  115. } SpiIntSrc;
  116. // @brief SPI CS pin.
  117. typedef enum
  118. {
  119. SpiPinCS_0 = 1,
  120. SpiPinCS_1 = 2,
  121. SpiPinCS_2 = 4,
  122. } SpiPinCS;
  123. #pragma pack (1)
  124. /**
  125. * @brief SPI attribute
  126. */
  127. typedef struct
  128. {
  129. SpiMode mode; ///< Master or slave mode
  130. SpiSubMode subMode; ///< SPI SPI_CPOL SPI_CPHA mode
  131. SpiSpeed speed; ///< SPI Clock
  132. SpiBitOrder bitOrder; ///< SPI bit order
  133. } SpiAttr;
  134. /**
  135. * @brief SPI data package
  136. */
  137. typedef struct
  138. {
  139. uint16_t cmd; ///< Command value
  140. uint8_t cmdLen; ///< Command byte length
  141. uint32_t *addr; ///< Point to address value
  142. uint8_t addrLen; ///< Address byte length
  143. uint32_t *data; ///< Point to data buffer
  144. uint8_t dataLen; ///< Data byte length.
  145. } SpiData;
  146. /**
  147. * @brief SPI interrupt information
  148. */
  149. typedef struct
  150. {
  151. SpiIntSrc src; ///< Interrupt source
  152. void *isrFunc; ///< SPI interrupt callback function.
  153. } SpiIntInfo;
  154. #pragma upack (1)
  155. /**
  156. * @brief Initialize SPI module.
  157. *
  158. * @param [in] spiNum
  159. * Indicates which submode to be used, SPI or HSPI.
  160. * @param [in] pAttr
  161. * Pointer to a struct SpiAttr that indicates SPI working attribution.
  162. *
  163. * @return void.
  164. */
  165. void SPIInit(SpiNum spiNum, SpiAttr* pAttr);
  166. /**
  167. * @brief Set slave address value by master.
  168. *
  169. * @param [in] spiNum
  170. * Indicates which submode to be used, SPI or HSPI.
  171. * @param [in] addr
  172. * Slave address to be set.
  173. *
  174. * @return void.
  175. */
  176. void SPIMasterCfgAddr(SpiNum spiNum, uint32_t addr);
  177. /**
  178. * @brief Set command value by master.
  179. *
  180. * @param [in] spiNum
  181. * Indicates which submode to be used, SPI or HSPI.
  182. * @param [in] cmd
  183. * Command will be send to slave.
  184. *
  185. * @return void.
  186. */
  187. void SPIMasterCfgCmd(SpiNum spiNum, uint32_t cmd);
  188. /**
  189. * @brief Send data to slave from master.
  190. *
  191. * @param [in] spiNum
  192. * Indicates which submode to be used, SPI or HSPI.
  193. * @param [in] pInData
  194. * Pointer to a strcuture that will be send.
  195. *
  196. * @return int, -1:indicates failure,others indicates success.
  197. */
  198. int SPIMasterSendData(SpiNum spiNum, SpiData* pInData);
  199. /**
  200. * @brief Receive data from slave by master.
  201. *
  202. * @param [in] spiNum
  203. * Indicates which submode to be used, SPI or HSPI.
  204. * @param [in] pOutData
  205. * Point to data buffer.
  206. *
  207. * @return int, -1:indicates failure,others indicates success.
  208. *
  209. */
  210. int SPIMasterRecvData(SpiNum spiNum, SpiData* pOutData);
  211. /**
  212. * @brief Load data to slave send buffer.
  213. *
  214. * @param [in] spiNum
  215. * Indicates which submode to be used, SPI or HSPI.
  216. * @param [in] pInData
  217. * Point to data buffer.
  218. * @param [in] inLen
  219. * The number of bytes to be set.
  220. *
  221. * @return int, -1:indicates failure,others indicates success.
  222. */
  223. int SPISlaveSendData(SpiNum spiNum, uint32_t *pInData, uint8_t inLen);
  224. /**
  225. * @brief Receive data by slave.
  226. *
  227. * @param [in] spiNum
  228. * Indicates which submode to be used, SPI or HSPI.
  229. * @param [in] isrFunc
  230. * isrFunc is a pointer to the function to be called when the SPI interrupt occurs.
  231. *
  232. * @return int, -1:indicates failure,others indicates success.
  233. */
  234. int SPISlaveRecvData(SpiNum spiNum);
  235. /**
  236. * @brief Set slave status by master.
  237. *
  238. * @param [in] spiNum
  239. * Indicates which submode to be used, SPI or HSPI.
  240. * @param [in] data
  241. * Data will be write to slave SPI_WR_STATUS.
  242. *
  243. * @return void.
  244. *
  245. * @attention Just for ESP8266(slave) register of RD_STATUS or WR_STATUS.
  246. */
  247. void SPIMasterSendStatus(SpiNum spiNum, uint8_t data);
  248. /**
  249. * @brief Get salve status by master.
  250. *
  251. * @param [in] spiNum
  252. * Indicates which submode to be used, SPI or HSPI.
  253. *
  254. * @return int, -1: indicates failure; other value in slave status.
  255. *
  256. * @attention Just for ESP8266(slave) register of RD_STATUS or WR_STATUS.
  257. */
  258. int SPIMasterRecvStatus(SpiNum spiNum);
  259. /**
  260. * @brief Select SPI CS pin.
  261. *
  262. * @param [in] spiNum
  263. * Indicates which submode to be used, SPI or HSPI.
  264. * @param [in] pinCs
  265. * Indicates which SPI pin to choose.
  266. *
  267. * @return void.
  268. */
  269. void SPICsPinSelect(SpiNum spiNum, SpiPinCS pinCs);
  270. /**
  271. * @brief Set SPI module interrupt source and callback function.
  272. *
  273. * @param [in] spiNum
  274. * Indicates which submode to be used, SPI or HSPI.
  275. * @param [in] pIntInfo
  276. * Pointer to a struct SpiIntInfo that indicates SPI interrupt information.
  277. *
  278. * @return void.
  279. */
  280. void SPIIntCfg(SpiNum spiNum, SpiIntInfo *pIntInfo);
  281. /**
  282. * @brief Enable SPI module interrupt source.
  283. *
  284. * @param [in] spiNum
  285. * Indicates which submode to be used, SPI or HSPI.
  286. * @param [in] intSrc
  287. * Indicates which interrupt source to enable.
  288. *
  289. * @return void.
  290. */
  291. void SPIIntEnable(SpiNum spiNum, SpiIntSrc intSrc);
  292. /**
  293. * @brief Disable SPI module interrupt source.
  294. *
  295. * @param [in] spiNum
  296. * Indicates which submode to be used, SPI or HSPI.
  297. * @param [in] intSrc
  298. * Indicates which interrupt source to disable.
  299. *
  300. * @return void.
  301. */
  302. void SPIIntDisable(SpiNum spiNum, SpiIntSrc intSrc);
  303. /**
  304. * @brief Clear all of spi interrupt.
  305. *
  306. * @param [in] spiNum
  307. * Indicates which submode to be used, SPI or HSPI.
  308. *
  309. * @return void.
  310. */
  311. void SPIIntClear(SpiNum spiNum);
  312. #ifdef __cplusplus
  313. }
  314. #endif
  315. #endif // __SPI_INTERFACE_H__