dev_iic.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* ------------------------------------------
  2. * Copyright (c) 2017, Synopsys, Inc. All rights reserved.
  3. * Redistribution and use in source and binary forms, with or without modification,
  4. * are permitted provided that the following conditions are met:
  5. * 1) Redistributions of source code must retain the above copyright notice, this
  6. * list of conditions and the following disclaimer.
  7. * 2) Redistributions in binary form must reproduce the above copyright notice,
  8. * this list of conditions and the following disclaimer in the documentation and/or
  9. * other materials provided with the distribution.
  10. * 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
  11. * be used to endorse or promote products derived from this software without
  12. * specific prior written permission.
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. *
  24. * \version 2017.03
  25. * \date 2014-06-16
  26. * \author Huaqi Fang(Huaqi.Fang@synopsys.com)
  27. --------------------------------------------- */
  28. /**
  29. * \defgroup DEVICE_HAL_IIC IIC Device HAL Interface
  30. * \ingroup DEVICE_HAL_DEF
  31. * \brief definitions for iic device hardware layer (\ref dev_iic.h)
  32. * \details provide interfaces for iic driver to implement
  33. * Here is a diagram for the iic interface.
  34. *
  35. * \htmlonly
  36. * <div class="imagebox">
  37. * <div style="width: 600px">
  38. * <img src="pic/dev_iic_hal.jpg" alt="IIC Device HAL Interface Diagram"/>
  39. * <p>IIC Device HAL Interface Diagram</p>
  40. * </div>
  41. * </div>
  42. * \endhtmlonly
  43. *
  44. * @{
  45. *
  46. * \file
  47. * \brief iic device hardware layer definitions
  48. * \details provide common definitions for iic device,
  49. * then software developer can develop iic driver
  50. * following this definitions, and the applications
  51. * can directly call this definition to realize functions
  52. */
  53. #ifndef _DEVICE_HAL_IIC_H_
  54. #define _DEVICE_HAL_IIC_H_
  55. #include "device/device_hal/inc/dev_common.h"
  56. /**
  57. * \defgroup DEVICE_HAL_IIC_CFG IIC Related Configurations
  58. * \ingroup DEVICE_HAL_IIC
  59. * \brief Macros for IIC device related configurations.
  60. * @{
  61. */
  62. /** IIC Bus possible speed modes */
  63. typedef enum iic_speed_mode {
  64. IIC_SPEED_STANDARD = 0, /*!< Bidirectional, Standard-mode (Sm), with a bit rate up to 100 kbit/s */
  65. IIC_SPEED_FAST = 1, /*!< Bidirectional, Fast-mode (Fm), with a bit rate up to 400 kbit/s */
  66. IIC_SPEED_FASTPLUS = 2, /*!< Bidirectional, Fast-mode Plus (Fm+), with a bit rate up to 1 Mbit/s */
  67. IIC_SPEED_HIGH = 3, /*!< Bidirectional, High-speed mode (Hs-mode), with a bit rate up to 3.4 Mbit/s */
  68. IIC_SPEED_ULTRA = 4 /*!< Unidirectional(Write only), Ultra Fast-mode (UFm), with a bit rate up to 5 Mbit/s */
  69. } IIC_SPEED_MODE;
  70. /** IIC next Condition */
  71. typedef enum iic_next_condtion {
  72. IIC_MODE_STOP = 0, /*!< Send a STOP condition after write/read operation */
  73. IIC_MODE_RESTART = 1 /*!< Send a RESTART condition after write/read operation */
  74. } IIC_NEXT_CONDTION;
  75. /** IIC Error State */
  76. typedef enum iic_error_state {
  77. IIC_ERR_NONE = 0, /*!< Currently in iic device free state */
  78. IIC_ERR_LOST_BUS = 1, /*!< Master or slave lost bus during operation */
  79. IIC_ERR_ADDR_NOACK = 2, /*!< Slave address is sent but not addressed by any slave devices */
  80. IIC_ERR_DATA_NOACK = 3, /*!< Data in transfer is not acked when it should be acked */
  81. IIC_ERR_TIMEOUT = 4, /*!< Transfer timeout, no more data is received or sent */
  82. IIC_ERR_MSTSTOP = 5, /*!< Slave received a STOP condition from master device */
  83. IIC_ERR_UNDEF = 6 /*!< Undefined error cases */
  84. } IIC_ERROR_STATE;
  85. /** IIC Working State */
  86. typedef enum iic_working_state {
  87. IIC_FREE = 0, /*!< Currently in iic device free state */
  88. IIC_IN_TX = 1, /*!< Currently in iic master transmit state */
  89. IIC_IN_RX = 2 /*!< Currently in iic master receive state */
  90. } IIC_WORKING_STATE;
  91. /** IIC Addressing Mode */
  92. typedef enum iic_address_mode {
  93. IIC_7BIT_ADDRESS = 0, /*!< Use 7bit address mode */
  94. IIC_10BIT_ADDRESS = 1 /*!< Use 10bit address mode */
  95. } IIC_ADDRESS_MODE;
  96. /** IIC Slave State */
  97. typedef enum iic_slave_state {
  98. IIC_SLAVE_STATE_FREE = 0, /*!< None state, in free */
  99. IIC_SLAVE_STATE_START = (1<<1), /*!< Start or Restart condition, clear it when read */
  100. IIC_SLAVE_STATE_STOP = (1<<2), /*!< Stop condition, clear it when read */
  101. IIC_SLAVE_STATE_RD_REQ = (1<<3), /*!< Read request from master, this will trigger the slave transmit callback */
  102. IIC_SLAVE_STATE_RD_DONE = (1<<4), /*!< Read request done from master, clear it when read */
  103. IIC_SLAVE_STATE_WR_REQ = (1<<5), /*!< Write request from master, this will trigger the slave receive callback */
  104. IIC_SLAVE_STATE_GC_REQ = (1<<6), /*!< General call request from master */
  105. IIC_SLAVE_STATE_ERROR = (1<<7) /*!< Error, clear it when read */
  106. } IIC_SLAVE_STATE;
  107. /** 7bit IIC address mask */
  108. #define IIC_7BIT_ADDRESS_MASK (0x7F)
  109. /** 10bit IIC address mask */
  110. #define IIC_10BIT_ADDRESS_MASK (0x3FF)
  111. /** @} */
  112. /**
  113. * \defgroup DEVICE_HAL_IIC_CTRLCMD IIC Device Control Commands
  114. * \ingroup DEVICE_HAL_IIC
  115. * \brief Definitions for iic control command, used in \ref dev_iic::iic_control "IIC IO Control"
  116. * \details These commands defined here can be used in user code directly.
  117. * - Parameters Usage
  118. * - For passing parameters like integer, just use uint32_t/int32_t to directly pass values
  119. * - For passing parameters for a structure, please use pointer to pass values
  120. * - For getting some data, please use pointer to store the return data
  121. * - Common Return Values
  122. * - \ref E_OK, Control device successfully
  123. * - \ref E_CLSED, Device is not opened
  124. * - \ref E_OBJ, Device object is not valid or not exists
  125. * - \ref E_PAR, Parameter is not valid for current control command
  126. * - \ref E_SYS, Control device failed, due to hardware issues such as device is disabled
  127. * - \ref E_CTX, Control device failed, due to different reasons like in transfer state
  128. * - \ref E_NOSPT, Control command is not supported or not valid
  129. * @{
  130. */
  131. /** Define IIC control commands for common usage */
  132. #define DEV_SET_IIC_SYSCMD(cmd) DEV_SET_SYSCMD((cmd))
  133. /** Define IIC control commands for master usage */
  134. #define DEV_SET_IIC_MST_SYSCMD(cmd) DEV_SET_SYSCMD(0x00004000|(cmd))
  135. /** Define IIC control commands for slave usage */
  136. #define DEV_SET_IIC_SLV_SYSCMD(cmd) DEV_SET_SYSCMD(0x00008000|(cmd))
  137. /* ++++ Common commands for IIC Device ++++ */
  138. /**
  139. * Get \ref dev_iic_info::status "current device status"
  140. * - Param type : uint32_t *
  141. * - Param usage : store result of current status
  142. * - Return value explanation :
  143. */
  144. #define IIC_CMD_GET_STATUS DEV_SET_IIC_SYSCMD(0)
  145. /**
  146. * Set \ref dev_iic_info::addr_mode "iic addressing mode".
  147. * - Param type : uint32_t
  148. * - Param usage : iic addressing mode, possible values can be found \ref IIC_ADDRESS_MODE "here"
  149. * - Return value explanation :
  150. */
  151. #define IIC_CMD_SET_ADDR_MODE DEV_SET_IIC_SYSCMD(1)
  152. /**
  153. * Set \ref dev_iic_cbs::tx_cb "iic transmit success callback" function
  154. * when all required bytes are transmitted for interrupt method
  155. * - Param type : \ref DEV_CALLBACK * or NULL
  156. * - Param usage : transmit success callback function for iic
  157. * - Return value explanation :
  158. */
  159. #define IIC_CMD_SET_TXCB DEV_SET_IIC_SYSCMD(2)
  160. /**
  161. * Set \ref dev_iic_cbs::rx_cb "iic receive success callback" function
  162. * when all required bytes are received for interrupt method
  163. * - Param type : \ref DEV_CALLBACK * or NULL
  164. * - Param usage : receive success callback function for iic
  165. * - Return value explanation :
  166. */
  167. #define IIC_CMD_SET_RXCB DEV_SET_IIC_SYSCMD(3)
  168. /**
  169. * Set \ref dev_iic_cbs::err_cb "iic transfer error callback" function
  170. * when something error happened for interrupt method
  171. * - Param type : \ref DEV_CALLBACK * or NULL
  172. * - Param usage : transfer error callback function for iic
  173. * - Return value explanation :
  174. */
  175. #define IIC_CMD_SET_ERRCB DEV_SET_IIC_SYSCMD(4)
  176. /**
  177. * Set buffer for interrupt transmit, and it will set \ref dev_iic_info::tx_buf "tx_buf".
  178. * - IIC master mode use case \n
  179. * For master mode, if you set tx buffer to NULL, when tx interrupt is enabled and entered into tx interrupt,
  180. * it will automatically disable the tx interrupt, so when you want to transfer something, you need to set the
  181. * tx buffer to Non-NULL and enable tx interrupt, when the tx buffer is sent, it will disable the tx interrupt
  182. * and call tx callback function if available.
  183. * - IIC slave mode use case \n
  184. * For slave mode, the tx buffer is not used, only tx callback function is used, and if tx callback is not set,
  185. * then it will automatically disable the tx interrupt, if tx callback is set, it will call the tx callback function
  186. * and you need to write or read in the tx callback function, to avoid meaningless waiting, you can use control command
  187. * \ref IIC_CMD_GET_TXAVAIL to get how many bytes space existing in transmit fifo, and use iic_write to send the available
  188. * bytes.
  189. * - Param type : DEV_BUFFER * or NULL
  190. * - Param usage : buffer structure pointer, if param is NULL, then it will set tx_buf to NULL
  191. * - Return value explanation :
  192. */
  193. #define IIC_CMD_SET_TXINT_BUF DEV_SET_IIC_SYSCMD(5)
  194. /**
  195. * Set buffer for interrupt receive, and it will set \ref dev_iic_info::rx_buf "rx_buf"
  196. * - IIC master mode use case \n
  197. * Similar to \ref IIC_CMD_SET_TXINT_BUF
  198. * - IIC slave mode use case \n
  199. * Similiar to \ref IIC_CMD_SET_TXINT_BUF
  200. * - Param type : DEV_BUFFER * or NULL
  201. * - Param usage : buffer structure pointer, if param is NULL, then it will set rx_buf to NULL
  202. * - Return value explanation :
  203. */
  204. #define IIC_CMD_SET_RXINT_BUF DEV_SET_IIC_SYSCMD(6)
  205. /**
  206. * Enable or disable transmit interrupt,
  207. * for master mode, only one of tx and rx interrupt can be enabled,
  208. * if tx interrupt is enabled, then rx interrupt can't be changed.
  209. * - Param type : uint32_t
  210. * - Param usage : enable(none-zero) or disable(zero) flag
  211. * - Return value explanation :
  212. */
  213. #define IIC_CMD_SET_TXINT DEV_SET_IIC_SYSCMD(7)
  214. /**
  215. * Enable or disable receive interrupt,
  216. * for master mode, only one of tx and rx interrupt can be enabled,
  217. * if rx interrupt is enabled, then tx interrupt can't be changed.
  218. * - Param type : uint32_t
  219. * - Param usage : enable(none-zero) or disable(zero) flag
  220. * - Return value explanation :
  221. */
  222. #define IIC_CMD_SET_RXINT DEV_SET_IIC_SYSCMD(8)
  223. /**
  224. * Abort current interrupt transmit operation if tx interrupt enabled,
  225. * it will disable transmit interrupt, and set \ref DEV_IN_TX_ABRT
  226. * in \ref dev_iic_info::status "status" variable,
  227. * and call the transmit callback function, when tx callback is finished,
  228. * it will clear \ref DEV_IN_TX_ABRT and return
  229. * - Param type : NULL
  230. * - Param usage :
  231. * - Return value explanation :
  232. */
  233. #define IIC_CMD_ABORT_TX DEV_SET_IIC_SYSCMD(9)
  234. /**
  235. * Abort current interrupt receive operation if rx interrupt enabled,
  236. * it will disable receive interrupt, and set \ref DEV_IN_TX_ABRT
  237. * in \ref dev_iic_info::status "status" variable,
  238. * and call the receive callback function, when rx callback is finished,
  239. * it will clear \ref DEV_IN_TX_ABRT and return
  240. * - Param type : NULL
  241. * - Param usage :
  242. * - Return value explanation :
  243. */
  244. #define IIC_CMD_ABORT_RX DEV_SET_IIC_SYSCMD(10)
  245. /**
  246. * Do a software reset for IIC device, it will stop current transfer,
  247. * and clear error state and bring device to normal state, set next condition to STOP
  248. * - Param type : NULL
  249. * - Param usage :
  250. * - Return value explanation :
  251. */
  252. #define IIC_CMD_RESET DEV_SET_IIC_SYSCMD(11)
  253. /**
  254. * Flush iic device transmit buffer or fifo
  255. * - Param type : NULL
  256. * - Param usage :
  257. * - Return value explanation :
  258. */
  259. #define IIC_CMD_FLUSH_TX DEV_SET_IIC_SYSCMD(12)
  260. /**
  261. * Flush iic device receive buffer or fifo
  262. * - Param type : NULL
  263. * - Param usage :
  264. * - Return value explanation :
  265. */
  266. #define IIC_CMD_FLUSH_RX DEV_SET_IIC_SYSCMD(13)
  267. /**
  268. * Enable iic device
  269. * - Param type : NULL
  270. * - Param usage : param is not required
  271. * - Return value explanation :
  272. */
  273. #define IIC_CMD_ENA_DEV DEV_SET_IIC_SYSCMD(14)
  274. /**
  275. * Disable iic device, when device is disabled,
  276. * only \ref IIC_CMD_ENA_DEV, \ref IIC_CMD_DIS_DEV,
  277. * \ref IIC_CMD_GET_STATUS and \ref IIC_CMD_RESET
  278. * commands can be executed, other commands will return \ref E_SYS
  279. * - Param type : NULL
  280. * - Param usage : param is not required
  281. * - Return value explanation :
  282. */
  283. #define IIC_CMD_DIS_DEV DEV_SET_IIC_SYSCMD(15)
  284. /**
  285. * Get how many bytes space in iic are available to transmit,
  286. * this can be used in interrupt callback functions,
  287. * cooperate with \ref dev_iic::iic_write "iic_write" API to realize non-blocked write
  288. * - Param type : int32_t *
  289. * - Param usage : store the write available bytes, > 0 for available bytes, 0 for not available
  290. * - Return value explaination :
  291. */
  292. #define IIC_CMD_GET_TXAVAIL DEV_SET_IIC_SYSCMD(16)
  293. /**
  294. * Get how many bytes in iic are available to receive,
  295. * this can be used in interrupt callback functions,
  296. * cooperate with \ref dev_iic::iic_read "iic_read" API to realize non-blocked read
  297. * - Param type : int32_t *
  298. * - Param usage : store the read available bytes, > 0 for available bytes, 0 for not available
  299. * - Return value explanation :
  300. */
  301. #define IIC_CMD_GET_RXAVAIL DEV_SET_IIC_SYSCMD(17)
  302. /* ++++ Master only commands for IIC Device ++++ */
  303. /**
  304. * Set \ref dev_iic_info::speed_mode "iic speed mode".
  305. * - Param type : uint32_t
  306. * - Param usage : iic speed mode, possible values can be found \ref IIC_SPEED_MODE "here",
  307. * and if passing mode is not supported, it will choose a lower supported speed mode
  308. * - Return value explanation :
  309. */
  310. #define IIC_CMD_MST_SET_SPEED_MODE DEV_SET_IIC_MST_SYSCMD(0)
  311. /**
  312. * Set next condition for following transmit or receive operation.
  313. * For example, you can change next condition before iic_read or iic_write,
  314. * then in iic_read/iic_write operation, it will send a STOP/RESTART condition
  315. * after the last byte of this operation. For interrupt, this is similar.
  316. * - Param type : uint32_t
  317. * - Param usage : next condition can be \ref IIC_NEXT_CONDTION
  318. * - Return value explanation :
  319. */
  320. #define IIC_CMD_MST_SET_NEXT_COND DEV_SET_IIC_MST_SYSCMD(1)
  321. /**
  322. * Set target slave device address for selecting slave device
  323. * - Param type : uint32_t
  324. * - Param usage : target slave address value
  325. * - Return value explanation :
  326. */
  327. #define IIC_CMD_MST_SET_TAR_ADDR DEV_SET_IIC_MST_SYSCMD(2)
  328. /* ++++ Slave only commands for IIC Device ++++ */
  329. /**
  330. * Set slave address when working as slave iic device
  331. * - Param type : uint32_t
  332. * - Param usage : slave address value
  333. * - Return value explanation :
  334. */
  335. #define IIC_CMD_SLV_SET_SLV_ADDR DEV_SET_IIC_SLV_SYSCMD(0)
  336. /**
  337. * Get \ref iic_slave_state "slave state" when working as slave iic device
  338. * - Param type : uint32_t *
  339. * - Param usage : slave state
  340. * - Return value explanation :
  341. */
  342. #define IIC_CMD_SLV_GET_SLV_STATE DEV_SET_IIC_SLV_SYSCMD(1)
  343. /** @} */
  344. /**
  345. * \defgroup DEVICE_HAL_IIC_CALLBACK IIC Interrupt callback functions
  346. * \ingroup DEVICE_HAL_IIC
  347. * \brief callback function structure for IIC device
  348. * @{
  349. */
  350. typedef struct dev_iic_cbs {
  351. DEV_CALLBACK tx_cb; /*!< iic data transmit success required bytes callback */
  352. DEV_CALLBACK rx_cb; /*!< iic data receive success required bytes callback */
  353. DEV_CALLBACK err_cb; /*!< iic error callback */
  354. } DEV_IIC_CBS, *DEV_IIC_CBS_PTR;
  355. /** @} */
  356. /**
  357. * \defgroup DEVICE_HAL_IIC_DEVSTRUCT IIC Device Structure
  358. * \ingroup DEVICE_HAL_IIC
  359. * \brief contains definitions of iic device structure.
  360. * \details this structure will be used in user implemented code, which was called
  361. * Device Driver Implement Layer for iic to realize in user code.
  362. * @{
  363. */
  364. /**
  365. * \brief iic information struct definition
  366. * \details informations about iic open state, working state,
  367. * baurate, iic registers, working method, interrupt number
  368. */
  369. typedef struct dev_iic_info {
  370. void *iic_ctrl; /*!< iic control related pointer, implemented by bsp developer, and this should be set during iic object implementation */
  371. uint32_t opn_cnt; /*!< iic open count, open it will increase 1, close it will decrease 1, 0 for close, >0 for open */
  372. uint32_t status; /*!< current working status, refer to \ref DEVICE_HAL_COMMON_DEVSTATUS, this should be \ref DEV_ENABLED for first open */
  373. uint32_t mode; /*!< current working mode, which can be \ref DEV_MASTER_MODE "master mode" or \ref DEV_SLAVE_MODE "slave mode" */
  374. uint32_t speed_mode; /*!< current working \ref IIC_SPEED_MODE "iic speed mode" */
  375. uint32_t cur_state; /*!< \ref IIC_WORKING_STATE "current working state for iic device", this should be \ref IIC_FREE for first open */
  376. uint32_t err_state; /*!< \ref IIC_ERROR_STATE "current error state for iic device", this should be \ref IIC_ERR_NONE for first open */
  377. uint32_t addr_mode; /*!< \ref IIC_ADDRESS_MODE "current addressing mode", this should be \ref IIC_7BIT_ADDRESS for first open */
  378. uint32_t slv_addr; /*!< slave address when working as slave iic device, this should be 0 for first open */
  379. uint32_t tar_addr; /*!< target slave device address when addressing that slave device, this should be 0 for first open */
  380. uint32_t next_cond; /*!< \ref IIC_NEXT_CONDTION "next condition for master transmit or receive", \
  381. possible values are STOP or RESTART, it should be STOP for first open */
  382. DEV_BUFFER tx_buf; /*!< transmit buffer via interrupt, this should be all zero for first open */
  383. DEV_BUFFER rx_buf; /*!< receive buffer via interrupt, this should be all zero for first open */
  384. DEV_IIC_CBS iic_cbs; /*!< iic callbacks, for both master and slave mode, this should be all NULL for first open */
  385. void *extra; /*!< a extra pointer to get hook to applications which should not used by bsp developer,
  386. this should be NULL for first open and you can \ref DEV_IIC_INFO_SET_EXTRA_OBJECT "set"
  387. or \ref DEV_IIC_INFO_GET_EXTRA_OBJECT "get" the extra information pointer */
  388. } DEV_IIC_INFO, * DEV_IIC_INFO_PTR;
  389. /** Set extra information pointer of iic info */
  390. #define DEV_IIC_INFO_SET_EXTRA_OBJECT(iic_info_ptr, extra_info) (iic_info_ptr)->extra = (void *)(extra_info)
  391. /** Get extra information pointer of iic info */
  392. #define DEV_IIC_INFO_GET_EXTRA_OBJECT(iic_info_ptr) ((iic_info_ptr)->extra)
  393. /**
  394. * \brief iic device interface definition
  395. * \details define iic device interface, like iic information structure,
  396. * fuctions to get iic info, open/close/control iic, send/receive data by iic
  397. * \note all this details are implemented by user in user porting code
  398. */
  399. typedef struct dev_iic {
  400. DEV_IIC_INFO iic_info; /*!< iic device information */
  401. int32_t (*iic_open) (uint32_t mode, uint32_t param); /*!< open iic device in master/slave mode, \
  402. when in master mode, param stands for speed mode, \
  403. when in slave mode, param stands for slave address */
  404. int32_t (*iic_close) (void); /*!< close iic device */
  405. int32_t (*iic_control) (uint32_t ctrl_cmd, void *param);/*!< control iic device */
  406. int32_t (*iic_write) (const void *data, uint32_t len); /*!< send data by iic device (blocking method) */
  407. int32_t (*iic_read) (void *data, uint32_t len); /*!< read data from iic device (blocking method) */
  408. } DEV_IIC, * DEV_IIC_PTR;
  409. /**
  410. * \fn int32_t (* dev_iic::iic_open) (uint32_t mode, uint32_t param)
  411. * \details open an iic device with selected mode (master or slave) with defined \ref param
  412. * \param[in] mode working mode (\ref DEV_MASTER_MODE "master" or \ref DEV_SLAVE_MODE "slave")
  413. * \param[in] param When mode is \ref DEV_MASTER_MODE, param stands for \ref dev_iic_info::speed_mode "speed mode",
  414. * when mode is \ref DEV_SLAVE_MODE, param stands for \ref dev_iic_info::slv_addr "slave device 7bit address"
  415. * \retval E_OK Open successfully without any issues
  416. * \retval E_OPNED If device was opened before with different parameters,
  417. * then just increase the \ref dev_iic_info::opn_cnt "opn_cnt" and return \ref E_OPNED
  418. * \retval E_OBJ Device object is not valid
  419. * \retval E_SYS Device is opened for different mode before, if you want to open it with different mode, you need to fully close it first.
  420. * \retval E_PAR Parameter is not valid
  421. * \retval E_NOSPT Open settings are not supported
  422. */
  423. /**
  424. * \fn int32_t (* dev_iic::iic_close) (void)
  425. * \details close an iic device, just decrease the \ref dev_iic_info::opn_cnt "opn_cnt",
  426. * if \ref dev_iic_info::opn_cnt "opn_cnt" equals 0, then close the device
  427. * \retval E_OK Close successfully without any issues(including scenario that device is already closed)
  428. * \retval E_OPNED Device is still opened, the device \ref dev_iic_info::opn_cnt "opn_cnt" decreased by 1
  429. * \retval E_OBJ Device object is not valid
  430. */
  431. /**
  432. * \fn int32_t (* dev_iic::iic_control) (uint32_t ctrl_cmd, void *param)
  433. * \details control an iic device by \ref ctrl_cmd, with passed \ref param.
  434. * you can control iic device using predefined iic control commands defined using \ref DEV_SET_SYSCMD
  435. * (which must be implemented by bsp developer), such as \ref IIC_CMD_SET_SPEED "set iic speed mode",
  436. * \ref IIC_CMD_FLUSH_TX "flush tx" and \ref DEVICE_HAL_IIC_CTRLCMD "more".
  437. * And you can also control iic device using your own specified commands defined using \ref DEV_SET_USRCMD,
  438. * but these specified commands should be defined in your own iic device driver implementation.
  439. * \param[in] ctrl_cmd \ref DEVICE_HAL_IIC_CTRLCMD "control command", to change or get some thing related to iic
  440. * \param[in,out] param parameters that maybe argument of the command,
  441. * or return values of the command, must not be NULL
  442. * \retval E_OK Control device successfully
  443. * \retval E_CLSED Device is not opened
  444. * \retval E_OBJ Device object is not valid or not exists
  445. * \retval E_PAR Parameter is not valid for current control command
  446. * \retval E_SYS Control device failed, due to hardware issues, such as device is disabled
  447. * \retval E_CTX Control device failed, due to different reasons like in transfer state
  448. * \retval E_NOSPT Control command is not supported or not valid
  449. */
  450. /**
  451. * \fn int32_t (* dev_iic::iic_write) (const void *data, uint32_t len)
  452. * \details send \ref data through iic with defined \ref len to slave device which slave address is \ref slv_addr.
  453. * \param[in] data pointer to data need to send by iic
  454. * \param[in] len length of data to be sent
  455. * \retval >0 Byte count that was successfully sent for poll method,
  456. * it might can't send that much due to \ref \ref dev_iic_info::err_state "different error state".
  457. * \retval E_OBJ Device object is not valid or not exists
  458. * \retval E_PAR Parameter is not valid
  459. * \retval E_CTX Device is still in transfer state
  460. * \retval E_SYS Can't write data to hardware due to hardware issues, such as device is disabled
  461. */
  462. /**
  463. * \fn int32_t (* dev_iic::iic_read) (void *data, uint32_t len)
  464. * \details receive \ref data of defined \ref len through iic from slave device which slave address is \ref slv_addr.
  465. * \param[out] data pointer to data need to received by iic
  466. * \param[in] len length of data to be received
  467. * \retval >0 Byte count that was successfully received for poll method,
  468. * it might can't send that much due to \ref \ref dev_iic_info::err_state "different error state".
  469. * \retval E_OBJ Device object is not valid or not exists
  470. * \retval E_CTX Device is still in transfer state
  471. * \retval E_PAR Parameter is not valid
  472. * \retval E_SYS Can't receive data from hardware due to hardware issues, such as device is disabled
  473. */
  474. /** @} */
  475. #ifdef __cplusplus
  476. extern "C" {
  477. #endif
  478. /**
  479. * \brief get an \ref dev_iic "iic device" by iic device id.
  480. * For how to use iic device hal refer to \ref DEVICE_HAL_IIC_DEVSTRUCT "Functions in iic device structure"
  481. * \param[in] iic_id id of iic, defined by user
  482. * \retval !NULL pointer to an \ref dev_iic "iic device structure"
  483. * \retval NULL failed to find the iic device by \ref iic_id
  484. * \note need to implemented by user in user code
  485. */
  486. extern DEV_IIC_PTR iic_get_dev(int32_t iic_id);
  487. #ifdef __cplusplus
  488. }
  489. #endif
  490. /** @} */
  491. #endif /* _DEVICE_HAL_IIC_H_ */