i2c.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @file i2c.h
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. /*******************************************************************************
  11. * @file i2c.h
  12. * @brief Contains all functions support for i2c driver
  13. * @version 0.0
  14. * @date 25. Oct. 2017
  15. * @author qing.han
  16. *
  17. *
  18. *******************************************************************************/
  19. #ifndef __I2C__H__
  20. #define __I2C__H__
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /*******************************************************************************
  25. *@ Module : pre-compile
  26. *@ Description : NULL
  27. *******************************************************************************/
  28. #ifdef _I2C_CMD_
  29. #define I2C_Ext
  30. #else
  31. #define I2C_Ext extern
  32. #endif
  33. /*******************************************************************************
  34. *@ Module : Includes
  35. *@ Description : None
  36. *******************************************************************************/
  37. #include "types.h"
  38. #include "gpio.h"
  39. #include "clock.h"
  40. #define I2C_USE_TIMEOUT 0
  41. #if(I2C_USE_TIMEOUT == 1)
  42. #define I2C_INIT_TOUT(to) int to = hal_systick()
  43. #define I2C_CHECK_TOUT(to, timeout, loginfo) {if(hal_ms_intv(to) > timeout){LOG(loginfo);return ERR_TIMEOUT;}}
  44. #else
  45. #define I2C_INIT_TOUT(to)
  46. #define I2C_CHECK_TOUT(to, timeout, loginfo)
  47. #endif
  48. /*******************************************************************************
  49. *@ Module : Macro Define
  50. *@ Description : None
  51. *******************************************************************************/
  52. typedef enum{
  53. I2C_0 =0, //define master mode,1:master mode,0:salve mode
  54. I2C_1 =1 //define master mode,1:master mode,0:salve mode
  55. }i2c_dev_t;
  56. /*******************************************************************************
  57. *@ Module : Macro Define
  58. *@ Description : I2C_SLAVE_ADDR_DEF ---- as slave mode
  59. * I2C_MASTER_ADDR_DEF --- as master mode addressing device
  60. *******************************************************************************/
  61. #define I2C_SLAVE_ADDR_DEF 0x10
  62. #define I2C_MASTER_ADDR_DEF I2C_SLAVE_ADDR_DEF
  63. /*******************************************************************************
  64. *@ Module : I2C Buffer Length
  65. *@ Description : relate to RX FIFO & TX FIFO
  66. *******************************************************************************/
  67. #define I2C_FIFO_DEPTH 8
  68. #define I2C_RX_TL_CNT I2C_FIFO_DEPTH
  69. #define I2C_TX_TL_CNT I2C_FIFO_DEPTH
  70. /*******************************************************************************
  71. *@ Module : Macro Define
  72. *@ Description : Which IIC To be used
  73. *******************************************************************************/
  74. #define USE_AP_I2CX AP_I2C0 //define use i2c0 or i2c1,0:i2c0,1:i2c1
  75. /*******************************************************************************
  76. *@ Module : Macro Define
  77. *@ Description : None
  78. *******************************************************************************/
  79. #define I2C0_IRQ_ENABLE() *(volatile unsigned int *) 0xe000e100 |= BIT(12)
  80. #define I2C0_IRQ_DISABLE() *(volatile unsigned int *) 0xe000e100 &= ~BIT(12)
  81. #define I2C1_IRQ_ENABLE() *(volatile unsigned int *) 0xe000e100 |= BIT(13)
  82. #define I2C1_IRQ_DISABLE() *(volatile unsigned int *) 0xe000e100 &= ~BIT(13)
  83. #define I2C_READ_CMD(pi2cdev) (pi2cdev->IC_DATA_CMD = 0x100) //Read
  84. #define I2C_RX_FIFO_FULL(pi2cdev) ((pi2cdev->IC_STATUS & 0x10)==0x10)
  85. #define I2C_RX_FIFO_NOT_EMPTY(pi2cdev) ((pi2cdev->IC_STATUS & 0x08)==0x08)
  86. #define I2C_TX_FIFO_EMPTY(pi2cdev) ((pi2cdev->IC_STATUS & 0x04)==0x04)
  87. #define I2C_TX_FIFO_NOT_FULL(pi2cdev) ((pi2cdev->IC_STATUS & 0x02)==0x02)
  88. #define I2C_WAIT_RD_REQ(pi2cdev) !((pi2cdev->IC_RAW_INTR_STAT & 0x20)==0x20)
  89. #define I2C_RD_REQ(pi2cdev) ((pi2cdev->IC_RAW_INTR_STAT & 0x20)==0x20)
  90. #define I2C_NUMBER_DATA_RX_FIFO(pi2cdev) (pi2cdev->IC_RXFLR)
  91. #define I2C_NUMBER_DATA_TX_FIFO(pi2cdev) (pi2cdev->IC_TXFLR)
  92. #define I2C_CLR_RD_REQ(pi2cdev) (pi2cdev->IC_CLR_RD_REG)
  93. #define I2C_CLR_TX_ABRT(pi2cdev) (pi2cdev->IC_CLR_TX_ABRT)
  94. #define I2C_ENABLE(pi2cdev) (pi2cdev->IC_ENABLE=1)
  95. #define I2C_DISABLE(pi2cdev) (pi2cdev->IC_ENABLE=0)
  96. /*******************************************************************************
  97. *@ Module : I2C Interrupt Mask Register
  98. *@ Description : Interrupt MASK bit
  99. *******************************************************************************/
  100. #define I2C_MASK_RX_UNDER 0x0001
  101. #define I2C_MASK_RX_OVER 0x0002
  102. #define I2C_MASK_RX_FULL 0x0004
  103. #define I2C_MASK_TX_OVER 0x0008
  104. #define I2C_MASK_TX_EMPTY 0x0010
  105. #define I2C_MASK_RD_REQ 0x0020
  106. #define I2C_MASK_TX_ABRT 0x0040
  107. #define I2C_MASK_RX_DONE 0x0080
  108. #define I2C_MASK_ACTIVITY 0x0100
  109. #define I2C_MASK_STOP_DET 0x0200
  110. #define I2C_MASK_START_DET 0x0400
  111. #define I2C_MASK_GEN_CALL 0x0800
  112. /*******************************************************************************
  113. *@ Module : I2C Status Register
  114. *@ Description : Status Register BIT(Indicate transfer and FIFO Status)
  115. *******************************************************************************/
  116. #define I2C_STATUS_ACTIVITY 0x0001
  117. #define I2C_STATUS_TFNF 0x0002
  118. #define I2C_STATUS_TFE 0x0004
  119. #define I2C_STATUS_RFNE 0x0008
  120. #define I2C_STATUS_RFF 0x0010
  121. #define I2C_STATUS_MST_ACTIVITY 0x0020
  122. #define I2C_STATUS_SLV_ACTIVITY 0x0040
  123. /*******************************************************************************
  124. *@ Module : IIC Speed Mode
  125. *@ Description : None
  126. *******************************************************************************/
  127. typedef enum{
  128. SPEED_STANDARD = 1, //standard mode
  129. SPEED_FAST, //fast mode
  130. // SPEED_HIGH //high mode
  131. }I2C_SPEED_e;
  132. /*******************************************************************************
  133. *@ Module : IIC Clock
  134. *@ Description : None
  135. *******************************************************************************/
  136. typedef enum
  137. {
  138. I2C_CLOCK_100K = 0x00,
  139. I2C_CLOCK_400K ,
  140. } I2C_CLOCK_e;
  141. typedef struct _I2C_Evt_t{
  142. uint8_t type;
  143. uint8_t* data;
  144. uint8_t len;
  145. }I2C_Evt_t;
  146. typedef enum{
  147. I2C_TX_STATE_UNINIT = 0,
  148. I2C_TX_STATE_IDLE,
  149. I2C_TX_STATE_TX,
  150. I2C_TX_STATE_ERR
  151. }I2C_STATE;
  152. /*******************************************************************************
  153. *@ Module : Function declaration
  154. *@ Description : None
  155. *******************************************************************************/
  156. int hal_i2c_send(void* pi2c, uint8_t* str,uint8_t len);
  157. void* hal_i2c_init(i2c_dev_t dev, I2C_CLOCK_e i2c_clock_rate);
  158. int hal_i2c_deinit(void* pi2c);
  159. int hal_i2c_pin_init(i2c_dev_t dev, GpioPin_t pin_sda, GpioPin_t pin_clk);
  160. int hal_i2c_addr_update(void* pi2c, uint8_t addr);
  161. int hal_i2c_wait_tx_completed(void* pi2c);
  162. int hal_i2c_tx_start(void* pi2c);
  163. int hal_i2c_read(void* pi2c,uint8_t slave_addr,uint8_t reg,uint8_t* data,uint8_t size);
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. #endif