hal_wspi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_wspi.h
  15. * @brief WSPI Driver macros and structures.
  16. *
  17. * @addtogroup WSPI
  18. * @{
  19. */
  20. #ifndef HAL_WSPI_H
  21. #define HAL_WSPI_H
  22. #if (HAL_USE_WSPI == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /*===========================================================================*/
  27. /* Driver pre-compile time settings. */
  28. /*===========================================================================*/
  29. /**
  30. * @name WSPI configuration options
  31. * @{
  32. */
  33. /**
  34. * @brief Enables synchronous APIs.
  35. * @note Disabling this option saves both code and data space.
  36. */
  37. #if !defined(WSPI_USE_WAIT) || defined(__DOXYGEN__)
  38. #define WSPI_USE_WAIT TRUE
  39. #endif
  40. /**
  41. * @brief Enables the @p wspiAcquireBus() and @p wspiReleaseBus() APIs.
  42. * @note Disabling this option saves both code and data space.
  43. */
  44. #if !defined(WSPI_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
  45. #define WSPI_USE_MUTUAL_EXCLUSION TRUE
  46. #endif
  47. /** @} */
  48. /*===========================================================================*/
  49. /* Derived constants and error checks. */
  50. /*===========================================================================*/
  51. /*===========================================================================*/
  52. /* Driver data structures and types. */
  53. /*===========================================================================*/
  54. /**
  55. * @brief Driver state machine possible states.
  56. */
  57. typedef enum {
  58. WSPI_UNINIT = 0, /**< Not initialized. */
  59. WSPI_STOP = 1, /**< Stopped. */
  60. WSPI_READY = 2, /**< Ready. */
  61. WSPI_ACTIVE = 3, /**< Exchanging data. */
  62. WSPI_COMPLETE = 4, /**< Asynchronous operation complete. */
  63. WSPI_MEMMAP = 5 /**< In memory mapped mode. */
  64. } wspistate_t;
  65. /**
  66. * @brief Type of a structure representing an WSPI driver.
  67. */
  68. typedef struct hal_wspi_driver WSPIDriver;
  69. /**
  70. * @brief Type of a structure representing an WSPI driver configuration.
  71. */
  72. typedef struct hal_wspi_config WSPIConfig;
  73. /**
  74. * @brief Type of a WSPI notification callback.
  75. *
  76. * @param[in] wspip pointer to the @p WSPIDriver object triggering the
  77. * callback
  78. */
  79. typedef void (*wspicallback_t)(WSPIDriver *wspip);
  80. /**
  81. * @brief Type of a WSPI command descriptor.
  82. */
  83. typedef struct {
  84. /**
  85. * @brief Transfer configuration field.
  86. */
  87. uint32_t cfg;
  88. /**
  89. * @brief Command phase data.
  90. */
  91. uint32_t cmd;
  92. /**
  93. * @brief Address phase data.
  94. */
  95. uint32_t addr;
  96. /**
  97. * @brief Alternate phase data.
  98. */
  99. uint32_t alt;
  100. /**
  101. * @brief Number of dummy cycles to be inserted.
  102. */
  103. uint32_t dummy;
  104. } wspi_command_t;
  105. /* Including the low level driver header, it exports information required
  106. for completing types.*/
  107. #include "hal_wspi_lld.h"
  108. #if !defined(WSPI_SUPPORTS_MEMMAP)
  109. #error "low level does not define WSPI_SUPPORTS_MEMMAP"
  110. #endif
  111. #if !defined(WSPI_DEFAULT_CFG_MASKS)
  112. #error "low level does not define WSPI_DEFAULT_CFG_MASKS"
  113. #endif
  114. /**
  115. * @brief Driver configuration structure.
  116. */
  117. struct hal_wspi_config {
  118. /**
  119. * @brief Operation complete callback or @p NULL.
  120. */
  121. wspicallback_t end_cb;
  122. /* End of the mandatory fields.*/
  123. wspi_lld_config_fields;
  124. };
  125. /**
  126. * @brief Structure representing an WSPI driver.
  127. */
  128. struct hal_wspi_driver {
  129. /**
  130. * @brief Driver state.
  131. */
  132. wspistate_t state;
  133. /**
  134. * @brief Current configuration data.
  135. */
  136. const WSPIConfig *config;
  137. #if (WSPI_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  138. /**
  139. * @brief Waiting thread.
  140. */
  141. thread_reference_t thread;
  142. #endif /* WSPI_USE_WAIT */
  143. #if (WSPI_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
  144. /**
  145. * @brief Mutex protecting the peripheral.
  146. */
  147. mutex_t mutex;
  148. #endif /* WSPI_USE_MUTUAL_EXCLUSION */
  149. #if defined(WSPI_DRIVER_EXT_FIELDS)
  150. WSPI_DRIVER_EXT_FIELDS
  151. #endif
  152. /* End of the mandatory fields.*/
  153. wspi_lld_driver_fields;
  154. };
  155. /*===========================================================================*/
  156. /* Driver macros. */
  157. /*===========================================================================*/
  158. #if (WSPI_DEFAULT_CFG_MASKS == TRUE) || defined(__DOXYGEN__)
  159. /**
  160. * @name Transfer options
  161. * @note The low level driver has the option to override the following
  162. * definitions and use its own ones. In must take care to use
  163. * the same name for the same function or compatibility is not
  164. * ensured.
  165. * @{
  166. */
  167. #define WSPI_CFG_CMD_MODE_MASK (7LU << 0LU)
  168. #define WSPI_CFG_CMD_MODE_NONE (0LU << 0LU)
  169. #define WSPI_CFG_CMD_MODE_ONE_LINE (1LU << 0LU)
  170. #define WSPI_CFG_CMD_MODE_TWO_LINES (2LU << 0LU)
  171. #define WSPI_CFG_CMD_MODE_FOUR_LINES (3LU << 0LU)
  172. #define WSPI_CFG_CMD_MODE_EIGHT_LINES (4LU << 0LU)
  173. #define WSPI_CFG_CMD_DTR (1LU << 3LU)
  174. #define WSPI_CFG_CMD_SIZE_MASK (3LU << 4LU)
  175. #define WSPI_CFG_CMD_SIZE_8 (0LU << 4LU)
  176. #define WSPI_CFG_CMD_SIZE_16 (1LU << 4LU)
  177. #define WSPI_CFG_CMD_SIZE_24 (2LU << 4LU)
  178. #define WSPI_CFG_CMD_SIZE_32 (3LU << 4LU)
  179. #define WSPI_CFG_ADDR_MODE_MASK (7LU << 8LU)
  180. #define WSPI_CFG_ADDR_MODE_NONE (0LU << 8LU)
  181. #define WSPI_CFG_ADDR_MODE_ONE_LINE (1LU << 8LU)
  182. #define WSPI_CFG_ADDR_MODE_TWO_LINES (2LU << 8LU)
  183. #define WSPI_CFG_ADDR_MODE_FOUR_LINES (3LU << 8LU)
  184. #define WSPI_CFG_ADDR_MODE_EIGHT_LINES (4LU << 8LU)
  185. #define WSPI_CFG_ADDR_DTR (1LU << 11LU)
  186. #define WSPI_CFG_ADDR_SIZE_MASK (3LU << 12LU)
  187. #define WSPI_CFG_ADDR_SIZE_8 (0LU << 12LU)
  188. #define WSPI_CFG_ADDR_SIZE_16 (1LU << 12LU)
  189. #define WSPI_CFG_ADDR_SIZE_24 (2LU << 12LU)
  190. #define WSPI_CFG_ADDR_SIZE_32 (3LU << 12LU)
  191. #define WSPI_CFG_ALT_MODE_MASK (7LU << 16LU)
  192. #define WSPI_CFG_ALT_MODE_NONE (0LU << 16LU)
  193. #define WSPI_CFG_ALT_MODE_ONE_LINE (1LU << 16LU)
  194. #define WSPI_CFG_ALT_MODE_TWO_LINES (2LU << 16LU)
  195. #define WSPI_CFG_ALT_MODE_FOUR_LINES (3LU << 16LU)
  196. #define WSPI_CFG_ALT_MODE_EIGHT_LINES (4LU << 16LU)
  197. #define WSPI_CFG_ALT_DTR (1LU << 19LU)
  198. #define WSPI_CFG_ALT_SIZE_MASK (3LU << 20LU)
  199. #define WSPI_CFG_ALT_SIZE_8 (0LU << 20LU)
  200. #define WSPI_CFG_ALT_SIZE_16 (1LU << 20LU)
  201. #define WSPI_CFG_ALT_SIZE_24 (2LU << 20LU)
  202. #define WSPI_CFG_ALT_SIZE_32 (3LU << 20LU)
  203. #define WSPI_CFG_DATA_MODE_MASK (7LU << 24LU)
  204. #define WSPI_CFG_DATA_MODE_NONE (0LU << 24LU)
  205. #define WSPI_CFG_DATA_MODE_ONE_LINE (1LU << 24LU)
  206. #define WSPI_CFG_DATA_MODE_TWO_LINES (2LU << 24LU)
  207. #define WSPI_CFG_DATA_MODE_FOUR_LINES (3LU << 24LU)
  208. #define WSPI_CFG_DATA_MODE_EIGHT_LINES (4LU << 24LU)
  209. #define WSPI_CFG_DATA_DTR (1LU << 27LU)
  210. #define WSPI_CFG_DQS_ENABLE (1LU << 29LU)
  211. #define WSPI_CFG_SIOO (1LU << 31LU)
  212. #define WSPI_CFG_ALL_DTR (WSPI_CFG_CMD_DTR | \
  213. WSPI_CFG_ADDR_DTR | \
  214. WSPI_CFG_ALT_DTR | \
  215. WSPI_CFG_DATA_DTR)
  216. /** @} */
  217. #endif /* WSPI_USE_DEFAULT_CFG_MASKS == TRUE */
  218. /**
  219. * @name Macro Functions
  220. * @{
  221. */
  222. /**
  223. * @brief Sends a command without data phase.
  224. * @post At the end of the operation the configured callback is invoked.
  225. *
  226. * @param[in] wspip pointer to the @p WSPIDriver object
  227. * @param[in] cmdp pointer to the command descriptor
  228. *
  229. * @iclass
  230. */
  231. #define wspiStartCommandI(wspip, cmdp) { \
  232. osalDbgAssert(((cmdp)->cfg & WSPI_CFG_DATA_MODE_MASK) == \
  233. WSPI_CFG_DATA_MODE_NONE, \
  234. "data mode specified"); \
  235. (wspip)->state = WSPI_ACTIVE; \
  236. wspi_lld_command(wspip, cmdp); \
  237. }
  238. /**
  239. * @brief Sends data over the WSPI bus.
  240. * @details This asynchronous function starts a transmit operation.
  241. * @post At the end of the operation the configured callback is invoked.
  242. *
  243. * @param[in] wspip pointer to the @p WSPIDriver object
  244. * @param[in] cmdp pointer to the command descriptor
  245. * @param[in] n number of bytes to send or zero if no data phase
  246. * @param[in] txbuf the pointer to the transmit buffer
  247. *
  248. * @iclass
  249. */
  250. #define wspiStartSendI(wspip, cmdp, n, txbuf) { \
  251. osalDbgAssert(((cmdp)->cfg & WSPI_CFG_DATA_MODE_MASK) != \
  252. WSPI_CFG_DATA_MODE_NONE, \
  253. "data mode required"); \
  254. (wspip)->state = WSPI_ACTIVE; \
  255. wspi_lld_send(wspip, cmdp, n, txbuf); \
  256. }
  257. /**
  258. * @brief Receives data from the WSPI bus.
  259. * @details This asynchronous function starts a receive operation.
  260. * @post At the end of the operation the configured callback is invoked.
  261. *
  262. * @param[in] wspip pointer to the @p WSPIDriver object
  263. * @param[in] cmdp pointer to the command descriptor
  264. * @param[in] n number of bytes to receive or zero if no data phase
  265. * @param[out] rxbuf the pointer to the receive buffer
  266. *
  267. * @iclass
  268. */
  269. #define wspiStartReceiveI(wspip, cmdp, n, rxbuf) { \
  270. osalDbgAssert(((cmdp)->cfg & WSPI_CFG_DATA_MODE_MASK) != \
  271. WSPI_CFG_DATA_MODE_NONE, \
  272. "data mode required"); \
  273. (wspip)->state = WSPI_ACTIVE; \
  274. wspi_lld_receive(wspip, cmdp, n, rxbuf); \
  275. }
  276. #if (WSPI_SUPPORTS_MEMMAP == TRUE) || defined(__DOXYGEN__)
  277. /**
  278. * @brief Maps in memory space a WSPI flash device.
  279. * @pre The memory flash device must be initialized appropriately
  280. * before mapping it in memory space.
  281. *
  282. * @param[in] wspip pointer to the @p WSPIDriver object
  283. * @param[in] cmdp pointer to the command descriptor
  284. * @param[out] addrp pointer to the memory start address of the mapped
  285. * flash or @p NULL
  286. *
  287. * @iclass
  288. */
  289. #define wspiMapFlashI(wspip, cmdp, addrp) \
  290. wspi_lld_map_flash(wspip, cmdp, addrp)
  291. /**
  292. * @brief Maps in memory space a WSPI flash device.
  293. * @post The memory flash device must be re-initialized for normal
  294. * commands exchange.
  295. *
  296. * @param[in] wspip pointer to the @p WSPIDriver object
  297. *
  298. * @iclass
  299. */
  300. #define wspiUnmapFlashI(wspip) \
  301. wspi_lld_unmap_flash(wspip)
  302. #endif /* WSPI_SUPPORTS_MEMMAP == TRUE */
  303. /** @} */
  304. /**
  305. * @name Low level driver helper macros
  306. * @{
  307. */
  308. #if (WSPI_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  309. /**
  310. * @brief Wakes up the waiting thread.
  311. *
  312. * @param[in] wspip pointer to the @p WSPIDriver object
  313. *
  314. * @notapi
  315. */
  316. #define _wspi_wakeup_isr(wspip) { \
  317. osalSysLockFromISR(); \
  318. osalThreadResumeI(&(wspip)->thread, MSG_OK); \
  319. osalSysUnlockFromISR(); \
  320. }
  321. #else /* !WSPI_USE_WAIT */
  322. #define _wspi_wakeup_isr(wspip)
  323. #endif /* !WSPI_USE_WAIT */
  324. /**
  325. * @brief Common ISR code.
  326. * @details This code handles the portable part of the ISR code:
  327. * - Callback invocation.
  328. * - Waiting thread wakeup, if any.
  329. * - Driver state transitions.
  330. * .
  331. * @note This macro is meant to be used in the low level drivers
  332. * implementation only.
  333. *
  334. * @param[in] wspip pointer to the @p WSPIDriver object
  335. *
  336. * @notapi
  337. */
  338. #define _wspi_isr_code(wspip) { \
  339. if ((wspip)->config->end_cb) { \
  340. (wspip)->state = WSPI_COMPLETE; \
  341. (wspip)->config->end_cb(wspip); \
  342. if ((wspip)->state == WSPI_COMPLETE) \
  343. (wspip)->state = WSPI_READY; \
  344. } \
  345. else \
  346. (wspip)->state = WSPI_READY; \
  347. _wspi_wakeup_isr(wspip); \
  348. }
  349. /** @} */
  350. /*===========================================================================*/
  351. /* External declarations. */
  352. /*===========================================================================*/
  353. #ifdef __cplusplus
  354. extern "C" {
  355. #endif
  356. void wspiInit(void);
  357. void wspiObjectInit(WSPIDriver *wspip);
  358. void wspiStart(WSPIDriver *wspip, const WSPIConfig *config);
  359. void wspiStop(WSPIDriver *wspip);
  360. void wspiStartCommand(WSPIDriver *wspip, const wspi_command_t *cmdp);
  361. void wspiStartSend(WSPIDriver *wspip, const wspi_command_t *cmdp,
  362. size_t n, const uint8_t *txbuf);
  363. void wspiStartReceive(WSPIDriver *wspip, const wspi_command_t *cmdp,
  364. size_t n, uint8_t *rxbuf);
  365. #if WSPI_USE_WAIT == TRUE
  366. void wspiCommand(WSPIDriver *wspip, const wspi_command_t *cmdp);
  367. void wspiSend(WSPIDriver *wspip, const wspi_command_t *cmdp,
  368. size_t n, const uint8_t *txbuf);
  369. void wspiReceive(WSPIDriver *wspip, const wspi_command_t *cmdp,
  370. size_t n, uint8_t *rxbuf);
  371. #endif
  372. #if WSPI_SUPPORTS_MEMMAP == TRUE
  373. void wspiMapFlash(WSPIDriver *wspip,
  374. const wspi_command_t *cmdp,
  375. uint8_t **addrp);
  376. void wspiUnmapFlash(WSPIDriver *wspip);
  377. #endif
  378. #if WSPI_USE_MUTUAL_EXCLUSION == TRUE
  379. void wspiAcquireBus(WSPIDriver *wspip);
  380. void wspiReleaseBus(WSPIDriver *wspip);
  381. #endif
  382. #ifdef __cplusplus
  383. }
  384. #endif
  385. #endif /* HAL_USE_WSPI == TRUE */
  386. #endif /* HAL_WSPI_H */
  387. /** @} */