iot_spi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (c) 2020 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /**
  16. * @addtogroup IotHardware
  17. * @{
  18. *
  19. * @brief Provides APIs for operating devices,
  20. * including flash, GPIO, I2C, PWM, UART, and watchdog APIs.
  21. *
  22. *
  23. *
  24. * @since 2.2
  25. * @version 2.2
  26. */
  27. /**
  28. * @file iot_spi.h
  29. *
  30. * @brief Declares flash functions.
  31. *
  32. * These functions are used to initialize or deinitialize a flash device,
  33. * and read data from or write data to a flash memory. \n
  34. *
  35. * @since 2.2
  36. * @version 2.2
  37. */
  38. #ifndef IOT_SPI_H
  39. #define IOT_SPI_H
  40. /**
  41. * @brief Indicates the SPI callback, which is used in {@link SpiRegisterUsrFunc}.
  42. */
  43. typedef void (*SpiIsrFunc)(void);
  44. /**
  45. * @brief Enumerates SPI channel IDs.
  46. */
  47. typedef enum {
  48. /** Channel 0 */
  49. IOT_SPI_ID_0 = 0,
  50. /** Channel 1 */
  51. IOT_SPI_ID_1,
  52. } IotSpiIdx;
  53. /**
  54. * @brief Enumerates communication polarities.
  55. */
  56. typedef enum {
  57. /** Polarity 0 */
  58. IOT_SPI_CFG_CLOCK_CPOL_0,
  59. /** Polarity 1 */
  60. IOT_SPI_CFG_CLOCK_CPOL_1,
  61. }IotSpiCfgClockCpol;
  62. /**
  63. * @brief Enumerates communication phases.
  64. */
  65. typedef enum {
  66. /** Phase 0 */
  67. IOT_SPI_CFG_CLOCK_CPHA_0,
  68. /** Phase 1 */
  69. IOT_SPI_CFG_CLOCK_CPHA_1,
  70. } IotSpiCfgClockCpha;
  71. /**
  72. * @brief Enumerates communication protocols.
  73. */
  74. typedef enum {
  75. /** Motorola protocol */
  76. IOT_SPI_CFG_FRAM_MODE_MOTOROLA,
  77. /** Texas Instruments protocol */
  78. IOT_SPI_CFG_FRAM_MODE_TI,
  79. /** Microwire protocol */
  80. IOT_SPI_CFG_FRAM_MODE_MICROWIRE,
  81. } IotSpiCfgFramMode;
  82. /**
  83. * @brief Enumerates the communication data width, that is,
  84. * the number of valid bits in each frame.
  85. *
  86. */
  87. typedef enum {
  88. /** 4 bits */
  89. IOT_SPI_CFG_DATA_WIDTH_E_4BIT = 0x3,
  90. /** 5 bits */
  91. IOT_SPI_CFG_DATA_WIDTH_E_5BIT,
  92. /** 6 bits */
  93. IOT_SPI_CFG_DATA_WIDTH_E_6BIT,
  94. /** 7 bits */
  95. IOT_SPI_CFG_DATA_WIDTH_E_7BIT,
  96. /** 8 bits */
  97. IOT_SPI_CFG_DATA_WIDTH_E_8BIT,
  98. /** 9 bits */
  99. IOT_SPI_CFG_DATA_WIDTH_E_9BIT,
  100. /** 10 bits */
  101. IOT_SPI_CFG_DATA_WIDTH_E_10BIT,
  102. /** 11 bits */
  103. IOT_SPI_CFG_DATA_WIDTH_E_11BIT,
  104. /** 12 bits */
  105. IOT_SPI_CFG_DATA_WIDTH_E_12BIT,
  106. /** 13 bits */
  107. IOT_SPI_CFG_DATA_WIDTH_E_13BIT,
  108. /** 14 bits */
  109. IOT_SPI_CFG_DATA_WIDTH_E_14BIT,
  110. /** 15 bits */
  111. IOT_SPI_CFG_DATA_WIDTH_E_15BIT,
  112. /** 16 bits */
  113. IOT_SPI_CFG_DATA_WIDTH_E_16BIT,
  114. } IotSpiCfgDataWidth;
  115. /**
  116. * @brief Enumerates the endian mode of each frame.
  117. */
  118. typedef enum {
  119. /** Little-endian */
  120. IOT_SPI_CFG_ENDIAN_LITTLE,
  121. /** Big-endian */
  122. IOT_SPI_CFG_ENDIAN_BIG,
  123. } IotSpiCfgEndian;
  124. /**
  125. * @brief Defines data communication parameters.
  126. */
  127. typedef struct {
  128. /** Communication polarity. For details about available values,
  129. * see {@link IotSpiCfgClockCpol}.
  130. */
  131. unsigned int cpol : 1;
  132. /** Communication phase.
  133. * For details about available values, see {@link IotSpiCfgClockCpha}.
  134. */
  135. unsigned int cpha : 1;
  136. /** Communication protocol.
  137. * For details about available values, see {@link IotSpiCfgFramMode}.
  138. */
  139. unsigned int framMode : 2;
  140. /** Communication data width.
  141. * For details about available values, see {@link IotSpiCfgDataWidth}.
  142. */
  143. unsigned int dataWidth : 4;
  144. /** Endian mode. For details about available values, see {@link IotSpiCfgEndian}. */
  145. unsigned int endian : 1;
  146. /** Padding bit */
  147. unsigned int pad : 23;
  148. /** Communication frequency. The value ranges from 2460 Hz to 40 MHz. */
  149. unsigned int freq;
  150. } IotSpiCfgBasicInfo;
  151. /**
  152. * @brief Specifies whether a device is a master or slave device.
  153. *
  154. * @since 1.0
  155. * @version 1.0
  156. */
  157. typedef struct {
  158. /** Whether the device is a slave device */
  159. unsigned int isSlave : 1;
  160. /** Padding bit */
  161. unsigned int pad : 31;
  162. } IotSpiCfgInitParam;
  163. /**
  164. * @brief Sends data in SPI slave mode.
  165. *
  166. * In SPI slave mode, this function sends data of the length
  167. * specified by <b>byteLen</b> in <b>writeData</b> through
  168. * the channel specified by <b>spiId</b> within the duration <b>timeOutMs</b>.
  169. *
  170. * @param spiId Indicates the SPI channel ID.
  171. * @param writeData Indicates the pointer to the data to send.
  172. * @param byteLen Indicates the length of the data to send.
  173. * @param timeOutMs Indicates the timeout interval.
  174. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  175. * returns an error code defined in {@link iot_errno.h} otherwise.
  176. * @since 1.0
  177. * @version 1.0
  178. */
  179. unsigned int IoTSpiSlaveWrite(IotSpiIdx spiId, char *writeData, unsigned int byteLen, unsigned int timeOutMs);
  180. /**
  181. * @brief Reads data in SPI slave mode.
  182. *
  183. * In SPI slave mode, this function reads data of the length
  184. * specified by <b>byteLen</b> in <b>readData</b> through the channel
  185. * specified by <b>spiId</b> within the duration <b>timeOutMs</b>.
  186. *
  187. * @param spiId Indicates the SPI channel ID.
  188. * @param readData Indicates the pointer to the data to read.
  189. * @param byteLen Indicates the length of the data to read.
  190. * @param timeOutMs Indicates the timeout interval.
  191. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  192. * returns an error code defined in {@link iot_errno.h} otherwise.
  193. * @since 1.0
  194. * @version 1.0
  195. */
  196. unsigned int IoTSpiSlaveRead(IotSpiIdx spiId, char *readData, unsigned int byteLen, unsigned int timeOutMs);
  197. /**
  198. * @brief Sends data in half-duplex SPI master mode.
  199. *
  200. * In SPI master mode, this function sends data of the length
  201. * specified by <b>byteLen</b> in <b>writeData</b>
  202. * through the channel specified by <b>spiId</b>.
  203. *
  204. * @param spiId Indicates the SPI channel ID.
  205. * @param writeData Indicates the pointer to the data to send.
  206. * @param byteLen Indicates the length of the data to send.
  207. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  208. * returns an error code defined in {@link iot_errno.h} otherwise.
  209. * @since 1.0
  210. * @version 1.0
  211. */
  212. unsigned int IoTSpiHostWrite(IotSpiIdx spiId, char *writeData, unsigned int byteLen);
  213. /**
  214. * @brief Reads data in half-duplex SPI master mode.
  215. *
  216. * In SPI master mode, this function reads data of the length
  217. * specified by <b>byteLen</b> in <b>readData</b>
  218. * through the channel specified by <b>spiId</b>.
  219. *
  220. * @param spiId Indicates the SPI channel ID.
  221. * @param readData Indicates the pointer to the data to read.
  222. * @param byteLen Indicates the length of the data to read.
  223. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  224. * returns an error code defined in {@link iot_errno.h} otherwise.
  225. * @since 1.0
  226. * @version 1.0
  227. */
  228. unsigned int IoTSpiHostRead(IotSpiIdx spiId, char *readData, unsigned int byteLen);
  229. /**
  230. * @brief Sends and reads data in full-duplex SPI master mode.
  231. *
  232. * In SPI master mode, this function sends data in <b>writeData</b> and
  233. * reads data of the length specified by <b>byteLen</b> in <b>readData</b>
  234. * both through the channel specified by <b>spiId</b>.
  235. *
  236. * @param spiId Indicates the SPI channel ID.
  237. * @param writeData Indicates the pointer to the data to send.
  238. * @param readData Indicates the pointer to the data to read.
  239. * @param byteLen Indicates the length of the data to read.
  240. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  241. * returns an error code defined in {@link iot_errno.h} otherwise.
  242. * @since 1.0
  243. * @version 1.0
  244. */
  245. unsigned int IoTSpiHostWriteread(IotSpiIdx spiId, char *writeData, char *readData, unsigned int byteLen);
  246. /**
  247. * @brief Sets the SPI channel parameter.
  248. *
  249. *
  250. *
  251. * @param spiId Indicates the SPI channel ID.
  252. * @param param Indicates the pointer to the SPI parameter to set.
  253. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  254. * returns an error code defined in {@link iot_errno.h} otherwise.
  255. * @since 1.0
  256. * @version 1.0
  257. */
  258. unsigned int IoTSpiSetBasicInfo(IotSpiIdx spiId, const IotSpiCfgBasicInfo *param);
  259. /**
  260. * @brief Initializes an SPI device.
  261. *
  262. * This function initializes the device with the channel ID <b>spiId</b>,
  263. * device type <b>initParam</b>, and device parameter <b>param</b>.
  264. *
  265. * @param spiId Indicates the SPI channel ID.
  266. * @param initParam Specifies whether the device is a slave one.
  267. * @param param Indicates the pointer to the SPI device parameter.
  268. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  269. * returns an error code defined in {@link iot_errno.h} otherwise.
  270. * @since 1.0
  271. * @version 1.0
  272. */
  273. unsigned int IoTSpiInit(IotSpiIdx spiId, IotSpiCfgInitParam initParam, const IotSpiCfgBasicInfo *param);
  274. /**
  275. * @brief Deinitializes an SPI device.
  276. *
  277. * @param spiId Indicates the SPI channel ID.
  278. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  279. * returns an error code defined in {@link iot_errno.h} otherwise.
  280. * @since 1.0
  281. * @version 1.0
  282. */
  283. unsigned int IoTSpiDeinit(IotSpiIdx spiId);
  284. /**
  285. * @brief Sets whether to enable the interrupt request (IRQ) mode for an SPI device.
  286. *
  287. *
  288. *
  289. * @param spiId Indicates the SPI channel ID.
  290. * @param irqEn Specifies whether to enable IRQ.
  291. * The value <b>1</b> means to enable IRQ, and <b>0</b> means to disable IRQ.
  292. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  293. * returns an error code defined in {@link iot_errno.h} otherwise.
  294. * @since 1.0
  295. * @version 1.0
  296. */
  297. unsigned int IoTSpiSetIrqMode(IotSpiIdx spiId, unsigned char irqEn);
  298. /**
  299. * @brief Sets whether to enable DMA to transfer data for an SPI device in slave mode.
  300. *
  301. *
  302. *
  303. * @param spiId Indicates the SPI channel ID.
  304. * @param dmaEn Specifies whether to enable DMA.
  305. * The value <b>1</b> means to enable DMA, and <b>0</b> means to disable DMA.
  306. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  307. * returns an error code defined in {@link iot_errno.h} otherwise.
  308. * @since 1.0
  309. * @version 1.0
  310. */
  311. unsigned int IoTSpiSetDmaMode(IotSpiIdx spiId, unsigned char dmaEn);
  312. /**
  313. * @brief Registers the data TX preparation/recovery function.
  314. *
  315. * This function registers the functions
  316. * registered by <b>prepareF</b> and <b>restoreF</b> for
  317. * an SPI device with a channel specified by <b>spiId</b>.
  318. *
  319. * @param spiId Indicates the SPI channel ID.
  320. * @param prepareF Indicates the function used for data preparation.
  321. * @param restoreF Indicates the function used for data recovery.
  322. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  323. * returns an error code defined in {@link iot_errno.h} otherwise.
  324. * @since 1.0
  325. * @version 1.0
  326. */
  327. unsigned int IoTSpiRegisterUsrFunc(IotSpiIdx spiId, SpiIsrFunc prepareF, SpiIsrFunc restoreF);
  328. /**
  329. * @brief Sets whether to enable loopback test for an SPI device.
  330. *
  331. *
  332. *
  333. * @param spiId Indicates the SPI channel ID.
  334. * @param lbEn Specifies whether to enable loopback test. The value <b>1</b>
  335. * means to enable loopback test, and <b>0</b> means to disable loopback test.
  336. * @return Returns {@link IOT_SUCCESS} if the operation is successful;
  337. * returns an error code defined in {@link iot_errno.h} otherwise.
  338. * @since 1.0
  339. * @version 1.0
  340. */
  341. unsigned int IoTSpiSetLoopBackMode(IotSpiIdx spiId, unsigned char lbEn);
  342. #endif
  343. /** @} */