spi.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /****************************************************************************
  2. * include/nuttx/spi/spi.h
  3. *
  4. * Copyright(C) 2008-2013, 2015-2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __INCLUDE_NUTTX_SPI_SPI_H
  36. #define __INCLUDE_NUTTX_SPI_SPI_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <sys/types.h>
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <assert.h>
  45. #include <errno.h>
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. /* Configuration ************************************************************/
  50. /* These SPI configuration options affect the form of the SPI interface:
  51. *
  52. * CONFIG_SPI_EXCHANGE - Driver supports a single exchange method
  53. * (vs a recvblock() and sndblock ()methods).
  54. * CONFIG_SPI_CMDDATA - Devices on the SPI bus require out-of-band support
  55. * to distinguish command transfers from data transfers. Such devices
  56. * will often support either 9-bit SPI (yech) or 8-bit SPI and a GPIO
  57. * output that selects between command and data.
  58. * CONFIG_SPI_HWFEATURES - Include an interface method to support special,
  59. * hardware-specific SPI features.
  60. */
  61. /* Access macros ************************************************************/
  62. /****************************************************************************
  63. * Name: SPI_LOCK
  64. *
  65. * Description:
  66. * On SPI buses where there are multiple devices, it will be necessary to
  67. * lock SPI to have exclusive access to the buses for a sequence of
  68. * transfers. The bus should be locked before the chip is selected. After
  69. * locking the SPI bus, the caller should then also call the setfrequency,
  70. * setbits, and setmode methods to make sure that the SPI is properly
  71. * configured for the device. If the SPI bus is being shared, then it
  72. * may have been left in an incompatible state.
  73. *
  74. * Input Parameters:
  75. * dev - Device-specific state data
  76. * lock - true: Lock spi bus, false: unlock SPI bus
  77. *
  78. * Returned Value:
  79. * None
  80. *
  81. ****************************************************************************/
  82. #define SPI_LOCK(d,l) (d)->ops->lock(d,l)
  83. /****************************************************************************
  84. * Name: SPI_SELECT
  85. *
  86. * Description:
  87. * Enable/disable the SPI chip select. The implementation of this method
  88. * must include handshaking: If a device is selected, it must hold off
  89. * all other attempts to select the device until the device is deselected.
  90. * Required.
  91. *
  92. * Input Parameters:
  93. * dev - Device-specific state data
  94. * devid - Identifies the device to select
  95. * selected - true: slave selected, false: slave de-selected
  96. *
  97. * Returned Value:
  98. * None
  99. *
  100. ****************************************************************************/
  101. #define SPI_SELECT(d,id,s) ((d)->ops->select(d,id,s))
  102. /****************************************************************************
  103. * Name: SPI_SETFREQUENCY
  104. *
  105. * Description:
  106. * Set the SPI frequency. Required.
  107. *
  108. * Input Parameters:
  109. * dev - Device-specific state data
  110. * frequency - The SPI frequency requested
  111. *
  112. * Returned Value:
  113. * Returns the actual frequency selected
  114. *
  115. ****************************************************************************/
  116. #define SPI_SETFREQUENCY(d,f) ((d)->ops->setfrequency(d,f))
  117. /****************************************************************************
  118. * Name: SPI_SETDELAY
  119. *
  120. * Description:
  121. * Set the SPI Delays in nanoseconds. Optional.
  122. *
  123. * Input Parameters:
  124. * dev - Device-specific state data
  125. * startdelay - The delay between CS active and first CLK
  126. * stopdelay - The delay between last CLK and CS inactive
  127. * csdelay - The delay between CS inactive and CS active again
  128. *
  129. * Returned Value:
  130. * Returns zero (OK) on success; a negated errno value is return on any
  131. * failure.
  132. *
  133. ****************************************************************************/
  134. #ifdef CONFIG_SPI_CS_DELAY_CONTROL
  135. # define SPI_SETDELAY(d,a,b,c) ((d)->ops->setdelay(d,a,b,c))
  136. #endif
  137. /****************************************************************************
  138. * Name: SPI_SETMODE
  139. *
  140. * Description:
  141. * Set the SPI mode. Optional. See enum spi_mode_e for mode definitions.
  142. *
  143. * Input Parameters:
  144. * dev - Device-specific state data
  145. * mode - The SPI mode requested
  146. *
  147. * Returned Value:
  148. * none
  149. *
  150. ****************************************************************************/
  151. #define SPI_SETMODE(d,m) \
  152. do { if ((d)->ops->setmode) (d)->ops->setmode(d,m); } while (0)
  153. /****************************************************************************
  154. * Name: SPI_SETBITS
  155. *
  156. * Description:
  157. * Set the number if bits per word.
  158. *
  159. * Input Parameters:
  160. * dev - Device-specific state data
  161. * nbits - The number of bits in an SPI word.
  162. *
  163. * Returned Value:
  164. * none
  165. *
  166. ****************************************************************************/
  167. #define SPI_SETBITS(d,b) \
  168. do { if ((d)->ops->setbits) (d)->ops->setbits(d,b); } while (0)
  169. /****************************************************************************
  170. * Name: SPI_HWFEATURES
  171. *
  172. * Description:
  173. * Set hardware-specific feature flags.
  174. *
  175. * Input Parameters:
  176. * dev - Device-specific state data
  177. * features - H/W feature flags
  178. *
  179. * Returned Value:
  180. * Zero (OK) if the selected H/W features are enabled; A negated errno
  181. * value if any H/W feature is not supportable.
  182. *
  183. ****************************************************************************/
  184. #ifdef CONFIG_SPI_HWFEATURES
  185. /* If there are multiple SPI drivers, some may not support hardware
  186. * feature selection.
  187. */
  188. # define SPI_HWFEATURES(d,f) \
  189. (((d)->ops->hwfeatures) ? (d)->ops->hwfeatures(d,f) : ((f) == 0 ? OK : -ENOSYS))
  190. /* These are currently defined feature flags:
  191. *
  192. * Bit 0: HWFEAT_CRCGENERATION
  193. * Hardware CRC generation
  194. * Bit 1: HWFEAT_FORCE_CS_INACTIVE_AFTER_TRANSFER
  195. * CS rises after every Transmission, also if we provide new data
  196. * immediately
  197. * Bit 2: HWFEAT_FORCE_CS_ACTIVE_AFTER_TRANSFER
  198. * CS does not rise automatically after a transmission, also if
  199. * the spi runs out of data (for a long time)
  200. * Bit 3: HWFEAT_ESCAPE_LASTXFER
  201. * Do not set the LASTXFER-Bit at the last word of the next
  202. * exchange, Flag is auto-resetting after the next LASTXFER
  203. * condition. (see spi_exchange)
  204. * Bit 4: HWFEAT_LSBFIRST
  205. * Data transferred LSB first (default is MSB first)
  206. * Bit 5: Turn deferred trigger mode on or off. Primarily used for DMA
  207. * mode. If a transfer is deferred then the DMA will not actually
  208. * be triggered until a subsequent call to SPI_TRIGGER to set it
  209. * off.
  210. */
  211. # ifdef CONFIG_SPI_CRCGENERATION
  212. # define HWFEAT_CRCGENERATION (1 << 0)
  213. # endif
  214. # ifdef CONFIG_SPI_CS_CONTROL
  215. # define HWFEAT_FORCE_CS_CONTROL_MASK (7 << 1)
  216. # define HWFEAT_FORCE_CS_INACTIVE_AFTER_TRANSFER (1 << 1)
  217. # define HWFEAT_FORCE_CS_ACTIVE_AFTER_TRANSFER (1 << 2)
  218. # define HWFEAT_ESCAPE_LASTXFER (1 << 3)
  219. # endif
  220. # ifdef CONFIG_SPI_BITORDER
  221. # define HWFEAT_MSBFIRST (0 << 4)
  222. # define HWFEAT_LSBFIRST (1 << 4)
  223. # endif
  224. # ifdef CONFIG_SPI_TRIGGER
  225. # define HWFEAT_TRIGGER (1 << 5)
  226. # endif
  227. #else
  228. /* Any attempt to select hardware features with CONFIG_SPI_HWFEATURES
  229. * deselected will return an -ENOSYS error.
  230. */
  231. # define SPI_HWFEATURES(d,f) (((f) == 0) ? OK : -ENOSYS)
  232. #endif
  233. /****************************************************************************
  234. * Name: SPI_STATUS
  235. *
  236. * Description:
  237. * Get SPI/MMC status. Optional.
  238. *
  239. * Input Parameters:
  240. * dev - Device-specific state data
  241. * devid - Identifies the device to report status on
  242. *
  243. * Returned Value:
  244. * Returns a bitset of status values (see SPI_STATUS_* defines)
  245. *
  246. ****************************************************************************/
  247. #define SPI_STATUS(d,id) \
  248. ((d)->ops->status ? (d)->ops->status(d, id) : SPI_STATUS_PRESENT)
  249. /* SPI status bits -- Some dedicated for SPI MMC/SD support and may have no
  250. * relationship to SPI other than needed by the SPI MMC/SD interface
  251. */
  252. #define SPI_STATUS_PRESENT 0x01 /* Bit 0=1: MMC/SD card present */
  253. #define SPI_STATUS_WRPROTECTED 0x02 /* Bit 1=1: MMC/SD card write protected */
  254. /****************************************************************************
  255. * Name: SPI_CMDDATA
  256. *
  257. * Description:
  258. * Some devices require and additional out-of-band bit to specify if the
  259. * next word sent to the device is a command or data. This is typical, for
  260. * example, in "9-bit" displays where the 9th bit is the CMD/DATA bit.
  261. * This function provides selection of command or data.
  262. *
  263. * This "latches" the CMD/DATA state. It does not have to be called before
  264. * every word is transferred; only when the CMD/DATA state changes. This
  265. * method is required if CONFIG_SPI_CMDDATA is selected in the NuttX
  266. * configuration
  267. *
  268. * Input Parameters:
  269. * dev - Device-specific state data
  270. * cmd - TRUE: The following word is a command; FALSE: the following words
  271. * are data.
  272. *
  273. * Returned Value:
  274. * OK unless an error occurs. Then a negated errno value is returned
  275. *
  276. ****************************************************************************/
  277. #ifdef CONFIG_SPI_CMDDATA
  278. # define SPI_CMDDATA(d,id,cmd) ((d)->ops->cmddata(d,id,cmd))
  279. #endif
  280. /****************************************************************************
  281. * Name: SPI_SEND
  282. *
  283. * Description:
  284. * Exchange one word on SPI. Required.
  285. *
  286. * Input Parameters:
  287. * dev - Device-specific state data
  288. * wd - The word to send. the size of the data is determined by the
  289. * number of bits selected for the SPI interface.
  290. *
  291. * Returned Value:
  292. * Received value
  293. *
  294. ****************************************************************************/
  295. #define SPI_SEND(d,wd) ((d)->ops->send(d,(uint16_t)(wd)))
  296. /****************************************************************************
  297. * Name: SPI_SNDBLOCK
  298. *
  299. * Description:
  300. * Send a block of data on SPI. Required.
  301. *
  302. * Input Parameters:
  303. * dev - Device-specific state data
  304. * buffer - A pointer to the buffer of data to be sent
  305. * nwords - the length of data to send from the buffer in number of words.
  306. * The wordsize is determined by the number of bits-per-word
  307. * selected for the SPI interface. If nbits <= 8, the data is
  308. * packed into uint8_t's; if nbits >8, the data is packed into
  309. * uint16_t's
  310. *
  311. * Returned Value:
  312. * None
  313. *
  314. ****************************************************************************/
  315. #ifdef CONFIG_SPI_EXCHANGE
  316. # define SPI_SNDBLOCK(d,b,l) ((d)->ops->exchange(d,b,0,l))
  317. #else
  318. # define SPI_SNDBLOCK(d,b,l) ((d)->ops->sndblock(d,b,l))
  319. #endif
  320. /****************************************************************************
  321. * Name: SPI_RECVBLOCK
  322. *
  323. * Description:
  324. * Receive a block of data from SPI. Required.
  325. *
  326. * Input Parameters:
  327. * dev - Device-specific state data
  328. * buffer - A pointer to the buffer in which to receive data
  329. * nwords - the length of data that can be received in the buffer in number
  330. * of words. The wordsize is determined by the number of bits-
  331. * per-word selected for the SPI interface. If nbits <= 8, the
  332. * data is packed into uint8_t's; if nbits >8, the data is packed
  333. * into uint16_t's
  334. *
  335. * Returned Value:
  336. * None
  337. *
  338. ****************************************************************************/
  339. #ifdef CONFIG_SPI_EXCHANGE
  340. # define SPI_RECVBLOCK(d,b,l) ((d)->ops->exchange(d,0,b,l))
  341. #else
  342. # define SPI_RECVBLOCK(d,b,l) ((d)->ops->recvblock(d,b,l))
  343. #endif
  344. /****************************************************************************
  345. * Name: SPI_EXCHANGE
  346. *
  347. * Description:
  348. * Exchange a block of data from SPI. Required.
  349. *
  350. * Input Parameters:
  351. * dev - Device-specific state data
  352. * txbuffer - A pointer to the buffer of data to be sent
  353. * rxbuffer - A pointer to the buffer in which to receive data
  354. * nwords - the length of data that to be exchanged in units of words.
  355. * The wordsize is determined by the number of bits-per-word
  356. * selected for the SPI interface. If nbits <= 8, the data is
  357. * packed into uint8_t's; if nbits >8, the data is packed into
  358. * uint16_t's
  359. *
  360. * Returned Value:
  361. * None
  362. *
  363. ****************************************************************************/
  364. #ifdef CONFIG_SPI_EXCHANGE
  365. # define SPI_EXCHANGE(d,t,r,l) ((d)->ops->exchange(d,t,r,l))
  366. #endif
  367. /****************************************************************************
  368. * Name: SPI_REGISTERCALLBACK
  369. *
  370. * Description:
  371. * Register a callback that that will be invoked on any media status
  372. * change (i.e, anything that would be reported differently by SPI_STATUS).
  373. * Optional
  374. *
  375. * Input Parameters:
  376. * dev - Device-specific state data
  377. * callback - The function to call on the media change
  378. * arg - A caller provided value to return with the callback
  379. *
  380. * Returned Value:
  381. * 0 on success; negated errno on failure.
  382. *
  383. ****************************************************************************/
  384. #define SPI_REGISTERCALLBACK(d,c,a) \
  385. ((d)->ops->registercallback ? (d)->ops->registercallback(d,c,a) : -ENOSYS)
  386. /****************************************************************************
  387. * Name: SPI_TRIGGER
  388. *
  389. * Description:
  390. * Trigger a previously configured DMA transfer.
  391. *
  392. * Input Parameters:
  393. * dev - Device-specific state data
  394. *
  395. * Returned Value:
  396. * OK - Trigger was fired
  397. * -ENOSYS - Trigger not fired due to lack of DMA or low level support
  398. * -EIO - Trigger not fired because not previously primed
  399. *
  400. ****************************************************************************/
  401. # define SPI_TRIGGER(d) \
  402. (((d)->ops->trigger) ? ((d)->ops->trigger(d)) : -ENOSYS)
  403. /* SPI Device Macros ********************************************************/
  404. /* This builds a SPI devid from its type and index */
  405. #define SPIDEV_ID(type,index) ((((uint32_t)(type) & 0xffff) << 16) | \
  406. ((uint32_t)(index) & 0xffff))
  407. /* This retrieves the fields from a SPI devid */
  408. #define SPIDEVID_TYPE (devid) (((uint32_t)(devid) >> 16) & 0xffff)
  409. #define SPIDEVID_INDEX(devid) ((uint32_t)(devid) & 0xffff)
  410. /* These are standard definitions for the defined SPI device IDs. The index
  411. * argument, n, is the instance number. This should be zero if there is
  412. * only one instance of the SPI device on the SPI bus. Indices greater than
  413. * zero discriminate the additional devices of the same type on the SPI bus.
  414. */
  415. #define SPIDEV_NONE(n) SPIDEV_ID(SPIDEVTYPE_NONE, (n))
  416. #define SPIDEV_MMCSD(n) SPIDEV_ID(SPIDEVTYPE_MMCSD, (n))
  417. #define SPIDEV_FLASH(n) SPIDEV_ID(SPIDEVTYPE_FLASH, (n))
  418. #define SPIDEV_ETHERNET(n) SPIDEV_ID(SPIDEVTYPE_ETHERNET, (n))
  419. #define SPIDEV_DISPLAY(n) SPIDEV_ID(SPIDEVTYPE_DISPLAY, (n))
  420. #define SPIDEV_CAMERA(n) SPIDEV_ID(SPIDEVTYPE_CAMERA, (n))
  421. #define SPIDEV_WIRELESS(n) SPIDEV_ID(SPIDEVTYPE_WIRELESS, (n))
  422. #define SPIDEV_TOUCHSCREEN(n) SPIDEV_ID(SPIDEVTYPE_TOUCHSCREEN, (n))
  423. #define SPIDEV_EXPANDER(n) SPIDEV_ID(SPIDEVTYPE_EXPANDER, (n))
  424. #define SPIDEV_MUX(n) SPIDEV_ID(SPIDEVTYPE_MUX, (n))
  425. #define SPIDEV_AUDIO_DATA(n) SPIDEV_ID(SPIDEVTYPE_AUDIO_DATA, (n))
  426. #define SPIDEV_AUDIO_CTRL(n) SPIDEV_ID(SPIDEVTYPE_AUDIO_CTRL, (n))
  427. #define SPIDEV_EEPROM(n) SPIDEV_ID(SPIDEVTYPE_EEPROM, (n))
  428. #define SPIDEV_ACCELEROMETER(n) SPIDEV_ID(SPIDEVTYPE_ACCELEROMETER, (n))
  429. #define SPIDEV_BAROMETER(n) SPIDEV_ID(SPIDEVTYPE_BAROMETER, (n))
  430. #define SPIDEV_TEMPERATURE(n) SPIDEV_ID(SPIDEVTYPE_TEMPERATURE, (n))
  431. #define SPIDEV_IEEE802154(n) SPIDEV_ID(SPIDEVTYPE_IEEE802154, (n))
  432. #define SPIDEV_CONTACTLESS(n) SPIDEV_ID(SPIDEVTYPE_CONTACTLESS, (n))
  433. #define SPIDEV_CANBUS(n) SPIDEV_ID(SPIDEVTYPE_CANBUS, (n))
  434. #define SPIDEV_USBHOST(n) SPIDEV_ID(SPIDEVTYPE_USBHOST, (n))
  435. #define SPIDEV_LPWAN(n) SPIDEV_ID(SPIDEVTYPE_LPWAN, (n))
  436. #define SPIDEV_ADC(n) SPIDEV_ID(SPIDEVTYPE_ADC, (n))
  437. #define SPIDEV_USER(n) SPIDEV_ID(SPIDEVTYPE_USER, (n))
  438. /****************************************************************************
  439. * Public Types
  440. ****************************************************************************/
  441. /* The type of the media change callback function */
  442. typedef CODE void (*spi_mediachange_t)(FAR void *arg);
  443. /* If the board supports multiple SPI devices types, this enumeration
  444. * identifies which is selected or de-selected.
  445. * There may be more than one instance of each type on a bus, see below.
  446. */
  447. enum spi_devtype_e
  448. {
  449. SPIDEVTYPE_NONE = 0, /* Not a valid value */
  450. SPIDEVTYPE_MMCSD, /* Select SPI MMC/SD device */
  451. SPIDEVTYPE_FLASH, /* Select SPI FLASH device */
  452. SPIDEVTYPE_ETHERNET, /* Select SPI Ethernet device */
  453. SPIDEVTYPE_DISPLAY, /* Select SPI LCD/OLED display device */
  454. SPIDEVTYPE_CAMERA, /* Select SPI imaging device */
  455. SPIDEVTYPE_WIRELESS, /* Select SPI Wireless device */
  456. SPIDEVTYPE_TOUCHSCREEN, /* Select SPI touchscreen device */
  457. SPIDEVTYPE_EXPANDER, /* Select SPI I/O expander device */
  458. SPIDEVTYPE_MUX, /* Select SPI multiplexer device */
  459. SPIDEVTYPE_AUDIO_DATA, /* Select SPI audio codec device data port */
  460. SPIDEVTYPE_AUDIO_CTRL, /* Select SPI audio codec device control port */
  461. SPIDEVTYPE_EEPROM, /* Select SPI EEPROM device */
  462. SPIDEVTYPE_ACCELEROMETER, /* Select SPI Accelerometer device */
  463. SPIDEVTYPE_BAROMETER, /* Select SPI Pressure/Barometer device */
  464. SPIDEVTYPE_TEMPERATURE, /* Select SPI Temperature sensor device */
  465. SPIDEVTYPE_IEEE802154, /* Select SPI IEEE 802.15.4 wireless device */
  466. SPIDEVTYPE_CONTACTLESS, /* Select SPI Contactless device */
  467. SPIDEVTYPE_CANBUS, /* Select SPI CAN bus controller over SPI */
  468. SPIDEVTYPE_USBHOST, /* Select SPI USB host controller over SPI */
  469. SPIDEVTYPE_LPWAN, /* Select SPI LPWAN controller over SPI */
  470. SPIDEVTYPE_ADC, /* Select SPI ADC device */
  471. SPIDEVTYPE_USER /* Board-specific values start here
  472. * This must always be the last definition. */
  473. };
  474. /* Certain SPI devices may required different clocking modes */
  475. enum spi_mode_e
  476. {
  477. SPIDEV_MODE0 = 0, /* CPOL=0 CHPHA=0 */
  478. SPIDEV_MODE1, /* CPOL=0 CHPHA=1 */
  479. SPIDEV_MODE2, /* CPOL=1 CHPHA=0 */
  480. SPIDEV_MODE3, /* CPOL=1 CHPHA=1 */
  481. SPIDEV_MODETI, /* CPOL=0 CPHA=1 TI Synchronous Serial Frame Format */
  482. };
  483. #ifdef CONFIG_SPI_HWFEATURES
  484. /* This is a type wide enough to support all hardware features */
  485. typedef uint8_t spi_hwfeatures_t;
  486. #endif
  487. /* The SPI vtable */
  488. struct spi_dev_s;
  489. struct spi_ops_s
  490. {
  491. CODE int (*lock)(FAR struct spi_dev_s *dev, bool lock);
  492. CODE void (*select)(FAR struct spi_dev_s *dev, uint32_t devid,
  493. bool selected);
  494. CODE uint32_t (*setfrequency)(FAR struct spi_dev_s *dev,
  495. uint32_t frequency);
  496. #ifdef CONFIG_SPI_CS_DELAY_CONTROL
  497. CODE int (*setdelay)(FAR struct spi_dev_s *dev, uint32_t a,
  498. uint32_t b, uint32_t c);
  499. #endif
  500. CODE void (*setmode)(FAR struct spi_dev_s *dev, enum spi_mode_e mode);
  501. CODE void (*setbits)(FAR struct spi_dev_s *dev, int nbits);
  502. #ifdef CONFIG_SPI_HWFEATURES
  503. CODE int (*hwfeatures)(FAR struct spi_dev_s *dev,
  504. spi_hwfeatures_t features);
  505. #endif
  506. CODE uint8_t (*status)(FAR struct spi_dev_s *dev, uint32_t devid);
  507. #ifdef CONFIG_SPI_CMDDATA
  508. CODE int (*cmddata)(FAR struct spi_dev_s *dev, uint32_t devid,
  509. bool cmd);
  510. #endif
  511. CODE uint32_t (*send)(FAR struct spi_dev_s *dev, uint32_t wd);
  512. #ifdef CONFIG_SPI_EXCHANGE
  513. CODE void (*exchange)(FAR struct spi_dev_s *dev,
  514. FAR const void *txbuffer, FAR void *rxbuffer,
  515. size_t nwords);
  516. #else
  517. CODE void (*sndblock)(FAR struct spi_dev_s *dev,
  518. FAR const void *buffer, size_t nwords);
  519. CODE void (*recvblock)(FAR struct spi_dev_s *dev, FAR void *buffer,
  520. size_t nwords);
  521. #endif
  522. #ifdef CONFIG_SPI_TRIGGER
  523. CODE int (*trigger)(FAR struct spi_dev_s *dev);
  524. #endif
  525. CODE int (*registercallback)(FAR struct spi_dev_s *dev,
  526. spi_mediachange_t callback, void *arg);
  527. };
  528. /* SPI private data. This structure only defines the initial fields of the
  529. * structure visible to the SPI client. The specific implementation may
  530. * add additional, device specific fields
  531. */
  532. struct spi_dev_s
  533. {
  534. FAR const struct spi_ops_s *ops;
  535. };
  536. #undef EXTERN
  537. #if defined(__cplusplus)
  538. #define EXTERN extern "C"
  539. extern "C"
  540. {
  541. #else
  542. #define EXTERN extern
  543. #endif
  544. #undef EXTERN
  545. #if defined(__cplusplus)
  546. }
  547. #endif
  548. #endif /* __INCLUDE_NUTTX_SPI_SPI_H */