i2c_hal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  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. * NOTICE
  16. * The hal is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. // The HAL layer for I2C
  20. #pragma once
  21. #include "hal/i2c_ll.h"
  22. #include "hal/i2c_types.h"
  23. /**
  24. * @brief I2C hal Context definition
  25. */
  26. typedef struct {
  27. i2c_dev_t *dev;
  28. uint32_t version;
  29. } i2c_hal_context_t;
  30. /**
  31. * @brief Write the I2C rxfifo with the given length
  32. *
  33. * @param hal Context of the HAL layer
  34. * @param wr_data Pointer to data buffer
  35. * @param wr_size Amount of data needs write
  36. *
  37. * @return None
  38. */
  39. #define i2c_hal_write_txfifo(hal,wr_data,wr_size) i2c_ll_write_txfifo((hal)->dev,wr_data,wr_size)
  40. /**
  41. * @brief Read the I2C rxfifo with the given length
  42. *
  43. * @param hal Context of the HAL layer
  44. * @param buf Pointer to data buffer
  45. * @param rd_size Amount of data needs read
  46. *
  47. * @return None
  48. */
  49. #define i2c_hal_read_rxfifo(hal,buf,rd_size) i2c_ll_read_rxfifo((hal)->dev,buf,rd_size)
  50. /**
  51. * @brief Write I2C cmd register
  52. *
  53. * @param hal Context of the HAL layer
  54. * @param cmd I2C hardware command
  55. * @param cmd_idx The index of the command register, should be less than 16
  56. *
  57. * @return None
  58. */
  59. #define i2c_hal_write_cmd_reg(hal,cmd, cmd_idx) i2c_ll_write_cmd_reg((hal)->dev,cmd,cmd_idx)
  60. /**
  61. * @brief Configure the I2C to triger a trasaction
  62. *
  63. * @param hal Context of the HAL layer
  64. *
  65. * @return None
  66. */
  67. #define i2c_hal_trans_start(hal) i2c_ll_trans_start((hal)->dev)
  68. /**
  69. * @brief Enable I2C master RX interrupt
  70. *
  71. * @param hal Context of the HAL layer
  72. *
  73. * @return None
  74. */
  75. #define i2c_hal_enable_master_rx_it(hal) i2c_ll_master_enable_rx_it((hal)->dev)
  76. /**
  77. * @brief Enable I2C master TX interrupt
  78. *
  79. * @param hal Context of the HAL layer
  80. *
  81. * @return None
  82. */
  83. #define i2c_hal_enable_master_tx_it(hal) i2c_ll_master_enable_tx_it((hal)->dev)
  84. /**
  85. * @brief Clear I2C slave TX interrupt
  86. *
  87. * @param hal Context of the HAL layer
  88. *
  89. * @return None
  90. */
  91. #define i2c_hal_slave_clr_tx_it(hal) i2c_ll_slave_clr_tx_it((hal)->dev)
  92. /**
  93. * @brief Clear I2C slave RX interrupt
  94. *
  95. * @param hal Context of the HAL layer
  96. *
  97. * @return None
  98. */
  99. #define i2c_hal_slave_clr_rx_it(hal) i2c_ll_slave_clr_rx_it((hal)->dev)
  100. /**
  101. * @brief Init the I2C master.
  102. *
  103. * @param hal Context of the HAL layer
  104. * @param i2c_num I2C port number
  105. *
  106. * @return None
  107. */
  108. void i2c_hal_master_init(i2c_hal_context_t *hal, i2c_port_t i2c_num);
  109. /**
  110. * @brief Init the I2C slave.
  111. *
  112. * @param hal Context of the HAL layer
  113. * @param i2c_num I2C port number
  114. *
  115. * @return None
  116. */
  117. void i2c_hal_slave_init(i2c_hal_context_t *hal, i2c_port_t i2c_num);
  118. /**
  119. * @brief Reset the I2C hw txfifo
  120. *
  121. * @param hal Context of the HAL layer
  122. *
  123. * @return None
  124. */
  125. void i2c_hal_txfifo_rst(i2c_hal_context_t *hal);
  126. /**
  127. * @brief Reset the I2C hw rxfifo
  128. *
  129. * @param hal Context of the HAL layer
  130. *
  131. * @return None
  132. */
  133. void i2c_hal_rxfifo_rst(i2c_hal_context_t *hal);
  134. /**
  135. * @brief Configure the I2C data MSB bit shifted first or LSB bit shifted first.
  136. *
  137. * @param hal Context of the HAL layer
  138. * @param tx_mode Data format of TX
  139. * @param rx_mode Data format of RX
  140. *
  141. * @return None
  142. */
  143. void i2c_hal_set_data_mode(i2c_hal_context_t *hal, i2c_trans_mode_t tx_mode, i2c_trans_mode_t rx_mode);
  144. /**
  145. * @brief Configure the I2C hardware filter function.
  146. *
  147. * @param hal Context of the HAL layer
  148. * @param filter_num If the glitch period on the line is less than this value(in APB cycle), it will be filtered out
  149. * If `filter_num == 0`, the filter will be disabled
  150. *
  151. * @return None
  152. */
  153. void i2c_hal_set_filter(i2c_hal_context_t *hal, uint8_t filter_num);
  154. /**
  155. * @brief Get the I2C hardware filter configuration
  156. *
  157. * @param hal Context of the HAL layer
  158. * @param filter_num Pointer to accept the hardware filter configuration
  159. *
  160. * @return None
  161. */
  162. void i2c_hal_get_filter(i2c_hal_context_t *hal, uint8_t *filter_num);
  163. /**
  164. * @brief Configure the I2C SCL timing
  165. *
  166. * @param hal Context of the HAL layer
  167. * @param hight_period SCL high period
  168. * @param low_period SCL low period
  169. *
  170. * @return None
  171. */
  172. void i2c_hal_set_scl_timing(i2c_hal_context_t *hal, int hight_period, int low_period);
  173. /**
  174. * @brief Configure the I2C master SCL frequency
  175. *
  176. * @param hal Context of the HAL layer
  177. * @param src_clk The I2C Source clock frequency
  178. * @param scl_freq The SCL frequency to be set
  179. *
  180. * @return None
  181. */
  182. void i2c_hal_set_scl_freq(i2c_hal_context_t *hal, uint32_t src_clk, uint32_t scl_freq);
  183. /**
  184. * @brief Clear the I2C interrupt status with the given mask
  185. *
  186. * @param hal Context of the HAL layer
  187. * @param mask The interrupt bitmap needs to be clearned
  188. *
  189. * @return None
  190. */
  191. void i2c_hal_clr_intsts_mask(i2c_hal_context_t *hal, uint32_t mask);
  192. /**
  193. * @brief Enable the I2C interrupt with the given mask
  194. *
  195. * @param hal Context of the HAL layer
  196. * @param mask The interrupt bitmap needs to be enabled
  197. *
  198. * @return None
  199. */
  200. void i2c_hal_enable_intr_mask(i2c_hal_context_t *hal, uint32_t mask);
  201. /**
  202. * @brief Disable the I2C interrupt with the given mask
  203. *
  204. * @param hal Context of the HAL layer
  205. * @param mask The interrupt bitmap needs to be disabled
  206. *
  207. * @return None
  208. */
  209. void i2c_hal_disable_intr_mask(i2c_hal_context_t *hal, uint32_t mask);
  210. /**
  211. * @brief Configure the I2C memory access mode, FIFO mode or none FIFO mode
  212. *
  213. * @param hal Context of the HAL layer
  214. * @param fifo_mode_en Set true to enable FIFO access mode, else set it false
  215. *
  216. * @return None
  217. */
  218. void i2c_hal_set_fifo_mode(i2c_hal_context_t *hal, bool fifo_mode_en);
  219. /**
  220. * @brief Configure the I2C timeout value
  221. *
  222. * @param hal Context of the HAL layer
  223. * @param tout_val the timeout value to be set
  224. *
  225. * @return None
  226. */
  227. void i2c_hal_set_tout(i2c_hal_context_t *hal, int tout_val);
  228. /**
  229. * @brief Get the I2C time out configuration
  230. *
  231. * @param tout_val Pointer to accept the timeout configuration
  232. *
  233. * @return None
  234. */
  235. void i2c_hal_get_tout(i2c_hal_context_t *hal, int *tout_val);
  236. /**
  237. * @brief Configure the I2C slave address
  238. *
  239. * @param hal Context of the HAL layer
  240. * @param slave_addr Slave address
  241. * @param addr_10bit_en Set true to enable 10-bit slave address mode, Set false to enable 7-bit address mode
  242. *
  243. * @return None
  244. */
  245. void i2c_hal_set_slave_addr(i2c_hal_context_t *hal, uint16_t slave_addr, bool addr_10bit_en);
  246. /**
  247. * @brief Configure the I2C stop timing
  248. *
  249. * @param hal Context of the HAL layer
  250. * @param stop_setup The stop condition setup period (in APB cycle)
  251. * @param stop_hold The stop condition hold period (in APB cycle)
  252. *
  253. * @return None
  254. */
  255. void i2c_hal_set_stop_timing(i2c_hal_context_t *hal, int stop_setup, int stop_hold);
  256. /**
  257. * @brief Configure the I2C start timing
  258. *
  259. * @param hal Context of the HAL layer
  260. * @param start_setup The start condition setup period (in APB cycle)
  261. * @param start_hold The start condition hold period (in APB cycle)
  262. *
  263. * @return None
  264. */
  265. void i2c_hal_set_start_timing(i2c_hal_context_t *hal, int start_setup, int start_hold);
  266. /**
  267. * @brief Configure the I2C sda sample timing
  268. *
  269. * @param hal Context of the HAL layer
  270. * @param sda_sample The SDA sample time (in APB cycle)
  271. * @param sda_hold The SDA hold time (in APB cycle)
  272. *
  273. * @return None
  274. */
  275. void i2c_hal_set_sda_timing(i2c_hal_context_t *hal, int sda_sample, int sda_hold);
  276. /**
  277. * @brief Configure the I2C txfifo empty threshold value
  278. *
  279. * @param hal Context of the HAL layer.
  280. * @param empty_thr TxFIFO empty threshold value
  281. *
  282. * @return None
  283. */
  284. void i2c_hal_set_txfifo_empty_thr(i2c_hal_context_t *hal, uint8_t empty_thr);
  285. /**
  286. * @brief Configure the I2C rxfifo full threshold value
  287. *
  288. * @param hal Context of the HAL layer
  289. * @param full_thr RxFIFO full threshold value
  290. *
  291. * @return None
  292. */
  293. void i2c_hal_set_rxfifo_full_thr(i2c_hal_context_t *hal, uint8_t full_thr);
  294. /**
  295. * @brief Get the I2C interrupt status
  296. *
  297. * @param hal Context of the HAL layer
  298. * @param mask Pointer to accept the interrupt status
  299. *
  300. * @return None
  301. */
  302. void i2c_hal_get_intsts_mask(i2c_hal_context_t *hal, uint32_t *mask);
  303. /**
  304. * @brief Check if the I2C bus is busy
  305. *
  306. * @param hal Context of the HAL layer
  307. *
  308. * @return True if the bus is busy, otherwise, fale will be returned
  309. */
  310. bool i2c_hal_is_bus_busy(i2c_hal_context_t *hal);
  311. /**
  312. * @brief Get the I2C sda sample timing configuration
  313. *
  314. * @param hal Context of the HAL layer
  315. * @param sample_time Pointer to accept the SDA sample time
  316. * @param hold_time Pointer to accept the SDA hold time
  317. *
  318. * @return None
  319. */
  320. void i2c_hal_get_sda_timing(i2c_hal_context_t *hal, int *sample_time, int *hold_time);
  321. /**
  322. * @brief Get the I2C stop timing configuration
  323. *
  324. * @param hal Context of the HAL layer
  325. * @param setup_time Pointer to accept the stop condition setup period
  326. * @param hold_time Pointer to accept the stop condition hold period
  327. *
  328. * @return None
  329. */
  330. void i2c_hal_get_stop_timing(i2c_hal_context_t *hal, int *setup_time, int *hold_time);
  331. /**
  332. * @brief Get the I2C scl timing configuration
  333. *
  334. * @param hal Context of the HAL layer
  335. * @param high_period Pointer to accept the scl high period
  336. * @param low_period Pointer to accept the scl low period
  337. *
  338. * @return None
  339. */
  340. void i2c_hal_get_scl_timing(i2c_hal_context_t *hal, int *high_period, int *low_period);
  341. /**
  342. * @brief Get the I2C start timing configuration
  343. *
  344. * @param hal Context of the HAL layer
  345. * @param setup_time Pointer to accept the start condition setup period
  346. * @param hold_time Pointer to accept the start condition hold period
  347. *
  348. * @return None
  349. */
  350. void i2c_hal_get_start_timing(i2c_hal_context_t *hal, int *setup_time, int *hold_time);
  351. /**
  352. * @brief Check if the I2C is master mode
  353. *
  354. * @param hal Context of the HAL layer
  355. *
  356. * @return True if in master mode, otherwise, false will be returned
  357. */
  358. bool i2c_hal_is_master_mode(i2c_hal_context_t *hal);
  359. /**
  360. * @brief Get the rxFIFO readable length
  361. *
  362. * @param hal Context of the HAL layer
  363. * @param len Pointer to accept the rxFIFO readable length
  364. *
  365. * @return None
  366. */
  367. void i2c_hal_get_rxfifo_cnt(i2c_hal_context_t *hal, uint32_t *len);
  368. /**
  369. * @brief Set I2C bus timing with the given frequency
  370. *
  371. * @param hal Context of the HAL layer
  372. * @param scl_freq The scl frequency to be set
  373. * @param src_clk Source clock of I2C
  374. *
  375. * @return None
  376. */
  377. void i2c_hal_set_bus_timing(i2c_hal_context_t *hal, uint32_t scl_freq, i2c_sclk_t src_clk);
  378. /**
  379. * @brief Get I2C txFIFO writeable length
  380. *
  381. * @param hal Context of the HAL layer
  382. * @param len Pointer to accept the txFIFO writeable length
  383. *
  384. * @return None
  385. */
  386. void i2c_hal_get_txfifo_cnt(i2c_hal_context_t *hal, uint32_t *len);
  387. /**
  388. * @brief Check if the I2C is master mode
  389. *
  390. * @param hal Context of the HAL layer
  391. * @param tx_mode Pointer to accept the TX data mode
  392. * @param rx_mode Pointer to accept the RX data mode
  393. *
  394. * @return None
  395. */
  396. void i2c_hal_get_data_mode(i2c_hal_context_t *hal, i2c_trans_mode_t *tx_mode, i2c_trans_mode_t *rx_mode);
  397. /**
  398. * @brief I2C hardware FSM reset
  399. *
  400. * @param hal Context of the HAL layer
  401. *
  402. * @return None
  403. */
  404. void i2c_hal_master_fsm_rst(i2c_hal_context_t *hal);
  405. /**
  406. * @brief @brief Clear I2C bus
  407. *
  408. * @param hal Context of the HAL layer
  409. *
  410. * @return None
  411. */
  412. void i2c_hal_master_clr_bus(i2c_hal_context_t *hal);
  413. /**
  414. * @brief Enable I2C slave TX interrupt
  415. *
  416. * @param hal Context of the HAL layer
  417. *
  418. * @return None
  419. */
  420. void i2c_hal_enable_slave_tx_it(i2c_hal_context_t *hal);
  421. /**
  422. * @brief Disable I2C slave TX interrupt
  423. *
  424. * @param hal Context of the HAL layer
  425. *
  426. * @return None
  427. */
  428. void i2c_hal_disable_slave_tx_it(i2c_hal_context_t *hal);
  429. /**
  430. * @brief Enable I2C slave RX interrupt
  431. *
  432. * @param hal Context of the HAL layer
  433. *
  434. * @return None
  435. */
  436. void i2c_hal_enable_slave_rx_it(i2c_hal_context_t *hal);
  437. /**
  438. * @brief Disable I2C slave RX interrupt
  439. *
  440. * @param hal Context of the HAL layer
  441. *
  442. * @return None
  443. */
  444. void i2c_hal_disable_slave_rx_it(i2c_hal_context_t *hal);
  445. /**
  446. * @brief I2C master handle tx interrupt event
  447. *
  448. * @param hal Context of the HAL layer
  449. * @param event Pointer to accept the interrupt event
  450. *
  451. * @return None
  452. */
  453. void i2c_hal_master_handle_tx_event(i2c_hal_context_t *hal, i2c_intr_event_t *event);
  454. /**
  455. * @brief I2C master handle rx interrupt event
  456. *
  457. * @param hal Context of the HAL layer
  458. * @param event Pointer to accept the interrupt event
  459. *
  460. * @return None
  461. */
  462. void i2c_hal_master_handle_rx_event(i2c_hal_context_t *hal, i2c_intr_event_t *event);
  463. /**
  464. * @brief I2C slave handle interrupt event
  465. *
  466. * @param hal Context of the HAL layer
  467. * @param event Pointer to accept the interrupt event
  468. *
  469. * @return None
  470. */
  471. void i2c_hal_slave_handle_event(i2c_hal_context_t *hal, i2c_intr_event_t *event);