hal_spi.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_spi.h
  15. * @brief SPI Driver macros and structures.
  16. *
  17. * @addtogroup SPI
  18. * @{
  19. */
  20. #ifndef HAL_SPI_H
  21. #define HAL_SPI_H
  22. #if (HAL_USE_SPI == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /**
  27. * @name Chip Select modes
  28. * @{
  29. */
  30. #define SPI_SELECT_MODE_NONE 0 /** @brief @p spiSelect() and
  31. @p spiUnselect() do
  32. nothing. */
  33. #define SPI_SELECT_MODE_PAD 1 /** @brief Legacy mode. */
  34. #define SPI_SELECT_MODE_PORT 2 /** @brief Fastest mode. */
  35. #define SPI_SELECT_MODE_LINE 3 /** @brief Packed mode. */
  36. #define SPI_SELECT_MODE_LLD 4 /** @brief LLD-defined mode.*/
  37. /** @} */
  38. /*===========================================================================*/
  39. /* Driver pre-compile time settings. */
  40. /*===========================================================================*/
  41. /**
  42. * @name SPI configuration options
  43. * @{
  44. */
  45. /**
  46. * @brief Enables synchronous APIs.
  47. * @note Disabling this option saves both code and data space.
  48. */
  49. #if !defined(SPI_USE_WAIT) || defined(__DOXYGEN__)
  50. #define SPI_USE_WAIT TRUE
  51. #endif
  52. /**
  53. * @brief Enables circular transfers APIs.
  54. * @note Disabling this option saves both code and data space.
  55. */
  56. #if !defined(SPI_USE_CIRCULAR) || defined(__DOXYGEN__)
  57. #define SPI_USE_CIRCULAR FALSE
  58. #endif
  59. /**
  60. * @brief Enables the @p spiAcquireBus() and @p spiReleaseBus() APIs.
  61. * @note Disabling this option saves both code and data space.
  62. */
  63. #if !defined(SPI_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
  64. #define SPI_USE_MUTUAL_EXCLUSION TRUE
  65. #endif
  66. /**
  67. * @brief Handling method for SPI CS line.
  68. * @note Disabling this option saves both code and data space.
  69. */
  70. #if !defined(SPI_SELECT_MODE) || defined(__DOXYGEN__)
  71. #define SPI_SELECT_MODE SPI_SELECT_MODE_PAD
  72. #endif
  73. /** @} */
  74. /*===========================================================================*/
  75. /* Derived constants and error checks. */
  76. /*===========================================================================*/
  77. #if (SPI_SELECT_MODE != SPI_SELECT_MODE_NONE) && \
  78. (SPI_SELECT_MODE != SPI_SELECT_MODE_PAD) && \
  79. (SPI_SELECT_MODE != SPI_SELECT_MODE_PORT) && \
  80. (SPI_SELECT_MODE != SPI_SELECT_MODE_LINE) && \
  81. (SPI_SELECT_MODE != SPI_SELECT_MODE_LLD)
  82. #error "invalid SPI_SELECT_MODE setting"
  83. #endif
  84. /* Some modes have a dependency on the PAL driver, making the required
  85. checks here.*/
  86. #if ((SPI_SELECT_MODE != SPI_SELECT_MODE_PAD) || \
  87. (SPI_SELECT_MODE != SPI_SELECT_MODE_PORT) || \
  88. (SPI_SELECT_MODE != SPI_SELECT_MODE_LINE)) && \
  89. (HAL_USE_PAL != TRUE)
  90. #error "current SPI_SELECT_MODE requires HAL_USE_PAL"
  91. #endif
  92. /*===========================================================================*/
  93. /* Driver data structures and types. */
  94. /*===========================================================================*/
  95. /**
  96. * @brief Driver state machine possible states.
  97. */
  98. typedef enum {
  99. SPI_UNINIT = 0, /**< Not initialized. */
  100. SPI_STOP = 1, /**< Stopped. */
  101. SPI_READY = 2, /**< Ready. */
  102. SPI_ACTIVE = 3, /**< Exchanging data. */
  103. SPI_COMPLETE = 4 /**< Asynchronous operation complete. */
  104. } spistate_t;
  105. /**
  106. * @brief Type of a structure representing an SPI driver.
  107. */
  108. typedef struct hal_spi_driver SPIDriver;
  109. /**
  110. * @brief Type of a SPI driver configuration structure.
  111. */
  112. typedef struct hal_spi_config SPIConfig;
  113. /**
  114. * @brief SPI notification callback type.
  115. *
  116. * @param[in] spip pointer to the @p SPIDriver object triggering the
  117. * callback
  118. */
  119. typedef void (*spicallback_t)(SPIDriver *spip);
  120. /* Including the low level driver header, it exports information required
  121. for completing types.*/
  122. #include "hal_spi_lld.h"
  123. /**
  124. * @brief Driver configuration structure.
  125. */
  126. struct hal_spi_config {
  127. #if (SPI_SUPPORTS_CIRCULAR == TRUE) || defined(__DOXYGEN__)
  128. /**
  129. * @brief Enables the circular buffer mode.
  130. */
  131. bool circular;
  132. #endif
  133. /**
  134. * @brief Operation complete callback or @p NULL.
  135. */
  136. spicallback_t end_cb;
  137. #if (SPI_SELECT_MODE == SPI_SELECT_MODE_LINE) || defined(__DOXYGEN__)
  138. /**
  139. * @brief The chip select line.
  140. */
  141. ioline_t ssline;
  142. #endif
  143. #if (SPI_SELECT_MODE == SPI_SELECT_MODE_PORT) || defined(__DOXYGEN__)
  144. /**
  145. * @brief The chip select port.
  146. */
  147. ioportid_t ssport;
  148. /**
  149. * @brief The chip select port mask.
  150. */
  151. ioportmask_t ssmask;
  152. #endif
  153. #if (SPI_SELECT_MODE == SPI_SELECT_MODE_PAD) || defined(__DOXYGEN__)
  154. /**
  155. * @brief The chip select port.
  156. */
  157. ioportid_t ssport;
  158. /**
  159. * @brief The chip select pad number.
  160. */
  161. uint_fast8_t sspad;
  162. #endif
  163. /* End of the mandatory fields.*/
  164. spi_lld_config_fields;
  165. };
  166. /**
  167. * @brief Structure representing an SPI driver.
  168. */
  169. struct hal_spi_driver {
  170. /**
  171. * @brief Driver state.
  172. */
  173. spistate_t state;
  174. /**
  175. * @brief Current configuration data.
  176. */
  177. const SPIConfig *config;
  178. #if (SPI_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  179. /**
  180. * @brief Waiting thread.
  181. */
  182. thread_reference_t thread;
  183. #endif /* SPI_USE_WAIT == TRUE */
  184. #if (SPI_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
  185. /**
  186. * @brief Mutex protecting the peripheral.
  187. */
  188. mutex_t mutex;
  189. #endif /* SPI_USE_MUTUAL_EXCLUSION == TRUE */
  190. #if defined(SPI_DRIVER_EXT_FIELDS)
  191. SPI_DRIVER_EXT_FIELDS
  192. #endif
  193. /* End of the mandatory fields.*/
  194. spi_lld_driver_fields;
  195. };
  196. /*===========================================================================*/
  197. /* Driver macros. */
  198. /*===========================================================================*/
  199. /**
  200. * @name Macro Functions
  201. * @{
  202. */
  203. /**
  204. * @brief Buffer state.
  205. * @note This function is meant to be called from the SPI callback only.
  206. *
  207. * @param[in] spip pointer to the @p SPIDriver object
  208. * @return The buffer state.
  209. * @retval false if the driver filled/sent the first half of the
  210. * buffer.
  211. * @retval true if the driver filled/sent the second half of the
  212. * buffer.
  213. *
  214. * @special
  215. */
  216. #define spiIsBufferComplete(spip) ((bool)((spip)->state == SPI_COMPLETE))
  217. #if (SPI_SELECT_MODE == SPI_SELECT_MODE_LLD) || defined(__DOXYGEN__)
  218. /**
  219. * @brief Asserts the slave select signal and prepares for transfers.
  220. *
  221. * @param[in] spip pointer to the @p SPIDriver object
  222. *
  223. * @iclass
  224. */
  225. #define spiSelectI(spip) \
  226. do { \
  227. spi_lld_select(spip); \
  228. } while (false)
  229. /**
  230. * @brief Deasserts the slave select signal.
  231. * @details The previously selected peripheral is unselected.
  232. *
  233. * @param[in] spip pointer to the @p SPIDriver object
  234. *
  235. * @iclass
  236. */
  237. #define spiUnselectI(spip) \
  238. do { \
  239. spi_lld_unselect(spip); \
  240. } while (false)
  241. #elif SPI_SELECT_MODE == SPI_SELECT_MODE_LINE
  242. #define spiSelectI(spip) \
  243. do { \
  244. palClearLine((spip)->config->ssline); \
  245. } while (false)
  246. #define spiUnselectI(spip) \
  247. do { \
  248. palSetLine((spip)->config->ssline); \
  249. } while (false)
  250. #elif SPI_SELECT_MODE == SPI_SELECT_MODE_PORT
  251. #define spiSelectI(spip) \
  252. do { \
  253. palClearPort((spip)->config->ssport, (spip)->config->ssmask); \
  254. } while (false)
  255. #define spiUnselectI(spip) \
  256. do { \
  257. palSetPort((spip)->config->ssport, (spip)->config->ssmask); \
  258. } while (false)
  259. #elif SPI_SELECT_MODE == SPI_SELECT_MODE_PAD
  260. #define spiSelectI(spip) \
  261. do { \
  262. palClearPad((spip)->config->ssport, (spip)->config->sspad); \
  263. } while (false)
  264. #define spiUnselectI(spip) \
  265. do { \
  266. palSetPad((spip)->config->ssport, (spip)->config->sspad); \
  267. } while (false)
  268. #elif SPI_SELECT_MODE == SPI_SELECT_MODE_NONE
  269. #define spiSelectI(spip)
  270. #define spiUnselectI(spip)
  271. #endif
  272. /**
  273. * @brief Ignores data on the SPI bus.
  274. * @details This asynchronous function starts the transmission of a series of
  275. * idle words on the SPI bus and ignores the received data.
  276. * @pre A slave must have been selected using @p spiSelect() or
  277. * @p spiSelectI().
  278. * @post At the end of the operation the configured callback is invoked.
  279. *
  280. * @param[in] spip pointer to the @p SPIDriver object
  281. * @param[in] n number of words to be ignored
  282. *
  283. * @iclass
  284. */
  285. #define spiStartIgnoreI(spip, n) { \
  286. (spip)->state = SPI_ACTIVE; \
  287. spi_lld_ignore(spip, n); \
  288. }
  289. /**
  290. * @brief Exchanges data on the SPI bus.
  291. * @details This asynchronous function starts a simultaneous transmit/receive
  292. * operation.
  293. * @pre A slave must have been selected using @p spiSelect() or
  294. * @p spiSelectI().
  295. * @post At the end of the operation the configured callback is invoked.
  296. * @note The buffers are organized as uint8_t arrays for data sizes below
  297. * or equal to 8 bits else it is organized as uint16_t arrays.
  298. *
  299. * @param[in] spip pointer to the @p SPIDriver object
  300. * @param[in] n number of words to be exchanged
  301. * @param[in] txbuf the pointer to the transmit buffer
  302. * @param[out] rxbuf the pointer to the receive buffer
  303. *
  304. * @iclass
  305. */
  306. #define spiStartExchangeI(spip, n, txbuf, rxbuf) { \
  307. (spip)->state = SPI_ACTIVE; \
  308. spi_lld_exchange(spip, n, txbuf, rxbuf); \
  309. }
  310. /**
  311. * @brief Sends data over the SPI bus.
  312. * @details This asynchronous function starts a transmit operation.
  313. * @pre A slave must have been selected using @p spiSelect() or
  314. * @p spiSelectI().
  315. * @post At the end of the operation the configured callback is invoked.
  316. * @note The buffers are organized as uint8_t arrays for data sizes below
  317. * or equal to 8 bits else it is organized as uint16_t arrays.
  318. *
  319. * @param[in] spip pointer to the @p SPIDriver object
  320. * @param[in] n number of words to send
  321. * @param[in] txbuf the pointer to the transmit buffer
  322. *
  323. * @iclass
  324. */
  325. #define spiStartSendI(spip, n, txbuf) { \
  326. (spip)->state = SPI_ACTIVE; \
  327. spi_lld_send(spip, n, txbuf); \
  328. }
  329. /**
  330. * @brief Receives data from the SPI bus.
  331. * @details This asynchronous function starts a receive operation.
  332. * @pre A slave must have been selected using @p spiSelect() or
  333. * @p spiSelectI().
  334. * @post At the end of the operation the configured callback is invoked.
  335. * @note The buffers are organized as uint8_t arrays for data sizes below
  336. * or equal to 8 bits else it is organized as uint16_t arrays.
  337. *
  338. * @param[in] spip pointer to the @p SPIDriver object
  339. * @param[in] n number of words to receive
  340. * @param[out] rxbuf the pointer to the receive buffer
  341. *
  342. * @iclass
  343. */
  344. #define spiStartReceiveI(spip, n, rxbuf) { \
  345. (spip)->state = SPI_ACTIVE; \
  346. spi_lld_receive(spip, n, rxbuf); \
  347. }
  348. /**
  349. * @brief Exchanges one frame using a polled wait.
  350. * @details This synchronous function exchanges one frame using a polled
  351. * synchronization method. This function is useful when exchanging
  352. * small amount of data on high speed channels, usually in this
  353. * situation is much more efficient just wait for completion using
  354. * polling than suspending the thread waiting for an interrupt.
  355. * @note This API is implemented as a macro in order to minimize latency.
  356. *
  357. * @param[in] spip pointer to the @p SPIDriver object
  358. * @param[in] frame the data frame to send over the SPI bus
  359. * @return The received data frame from the SPI bus.
  360. */
  361. #define spiPolledExchange(spip, frame) spi_lld_polled_exchange(spip, frame)
  362. /** @} */
  363. /**
  364. * @name Low level driver helper macros
  365. * @{
  366. */
  367. #if (SPI_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  368. /**
  369. * @brief Wakes up the waiting thread.
  370. *
  371. * @param[in] spip pointer to the @p SPIDriver object
  372. *
  373. * @notapi
  374. */
  375. #define _spi_wakeup_isr(spip) { \
  376. osalSysLockFromISR(); \
  377. osalThreadResumeI(&(spip)->thread, MSG_OK); \
  378. osalSysUnlockFromISR(); \
  379. }
  380. #else /* !SPI_USE_WAIT */
  381. #define _spi_wakeup_isr(spip)
  382. #endif /* !SPI_USE_WAIT */
  383. /**
  384. * @brief Common ISR code when circular mode is not supported.
  385. * @details This code handles the portable part of the ISR code:
  386. * - Callback invocation.
  387. * - Waiting thread wakeup, if any.
  388. * - Driver state transitions.
  389. * .
  390. * @note This macro is meant to be used in the low level drivers
  391. * implementation only.
  392. *
  393. * @param[in] spip pointer to the @p SPIDriver object
  394. *
  395. * @notapi
  396. */
  397. #define _spi_isr_code(spip) { \
  398. if ((spip)->config->end_cb) { \
  399. (spip)->state = SPI_COMPLETE; \
  400. (spip)->config->end_cb(spip); \
  401. if ((spip)->state == SPI_COMPLETE) \
  402. (spip)->state = SPI_READY; \
  403. } \
  404. else \
  405. (spip)->state = SPI_READY; \
  406. _spi_wakeup_isr(spip); \
  407. }
  408. /**
  409. * @brief Half buffer filled ISR code in circular mode.
  410. * @details This code handles the portable part of the ISR code:
  411. * - Callback invocation.
  412. * .
  413. * @note This macro is meant to be used in the low level drivers
  414. * implementation only.
  415. *
  416. * @param[in] spip pointer to the @p SPIDriver object
  417. *
  418. * @notapi
  419. */
  420. #define _spi_isr_half_code(spip) { \
  421. if ((spip)->config->end_cb) { \
  422. (spip)->config->end_cb(spip); \
  423. } \
  424. }
  425. /**
  426. * @brief Full buffer filled ISR code in circular mode.
  427. * @details This code handles the portable part of the ISR code:
  428. * - Callback invocation.
  429. * - Driver state transitions.
  430. * .
  431. * @note This macro is meant to be used in the low level drivers
  432. * implementation only.
  433. *
  434. * @param[in] spip pointer to the @p SPIDriver object
  435. *
  436. * @notapi
  437. */
  438. #define _spi_isr_full_code(spip) { \
  439. if ((spip)->config->end_cb) { \
  440. (spip)->state = SPI_COMPLETE; \
  441. (spip)->config->end_cb(spip); \
  442. if ((spip)->state == SPI_COMPLETE) \
  443. (spip)->state = SPI_ACTIVE; \
  444. } \
  445. }
  446. /** @} */
  447. /*===========================================================================*/
  448. /* External declarations. */
  449. /*===========================================================================*/
  450. #ifdef __cplusplus
  451. extern "C" {
  452. #endif
  453. void spiInit(void);
  454. void spiObjectInit(SPIDriver *spip);
  455. void spiStart(SPIDriver *spip, const SPIConfig *config);
  456. void spiStop(SPIDriver *spip);
  457. void spiSelect(SPIDriver *spip);
  458. void spiUnselect(SPIDriver *spip);
  459. void spiStartIgnore(SPIDriver *spip, size_t n);
  460. void spiStartExchange(SPIDriver *spip, size_t n,
  461. const void *txbuf, void *rxbuf);
  462. void spiStartSend(SPIDriver *spip, size_t n, const void *txbuf);
  463. void spiStartReceive(SPIDriver *spip, size_t n, void *rxbuf);
  464. #if SPI_SUPPORTS_CIRCULAR == TRUE
  465. void spiAbortI(SPIDriver *spip);
  466. void spiAbort(SPIDriver *spip);
  467. #endif
  468. #if SPI_USE_WAIT == TRUE
  469. void spiIgnore(SPIDriver *spip, size_t n);
  470. void spiExchange(SPIDriver *spip, size_t n, const void *txbuf, void *rxbuf);
  471. void spiSend(SPIDriver *spip, size_t n, const void *txbuf);
  472. void spiReceive(SPIDriver *spip, size_t n, void *rxbuf);
  473. #endif
  474. #if SPI_USE_MUTUAL_EXCLUSION == TRUE
  475. void spiAcquireBus(SPIDriver *spip);
  476. void spiReleaseBus(SPIDriver *spip);
  477. #endif
  478. #ifdef __cplusplus
  479. }
  480. #endif
  481. #endif /* HAL_USE_SPI == TRUE */
  482. #endif /* HAL_SPI_H */
  483. /** @} */