i2c.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file i2c.h
  30. * @brief Contains all functions support for i2c driver
  31. * @version 0.0
  32. * @date 25. Oct. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #ifndef __I2C__H__
  40. #define __I2C__H__
  41. /*******************************************************************************
  42. *@ Module : pre-compile
  43. *@ Description : NULL
  44. *******************************************************************************/
  45. #ifdef _I2C_CMD_
  46. #define I2C_Ext
  47. #else
  48. #define I2C_Ext extern
  49. #endif
  50. /*******************************************************************************
  51. *@ Module : Includes
  52. *@ Description : None
  53. *******************************************************************************/
  54. #include "types.h"
  55. #include "ap_cp.h"
  56. #include "gpio.h"
  57. #include "common.h"
  58. #define I2C_USE_TIMEOUT 1
  59. #if(I2C_USE_TIMEOUT == 1)
  60. #define I2C_INIT_TOUT(to) int to = hal_systick()
  61. #define I2C_CHECK_TOUT(to, timeout, loginfo) {if(hal_ms_intv(to) > timeout){LOG(loginfo);return PPlus_ERR_TIMEOUT;}}
  62. #else
  63. #define I2C_INIT_TOUT(to)
  64. #define I2C_CHECK_TOUT(to, timeout, loginfo)
  65. #endif
  66. /*******************************************************************************
  67. *@ Module : Macro Define
  68. *@ Description : None
  69. *******************************************************************************/
  70. typedef enum{
  71. I2C_0 =0, //define master mode,1:master mode,0:salve mode
  72. I2C_1 =1 //define master mode,1:master mode,0:salve mode
  73. }i2c_dev_t;
  74. /*******************************************************************************
  75. *@ Module : Macro Define
  76. *@ Description : I2C_SLAVE_ADDR_DEF ---- as slave mode
  77. * I2C_MASTER_ADDR_DEF --- as master mode addressing device
  78. *******************************************************************************/
  79. #define I2C_SLAVE_ADDR_DEF 0x10
  80. #define I2C_MASTER_ADDR_DEF I2C_SLAVE_ADDR_DEF
  81. /*******************************************************************************
  82. *@ Module : I2C Buffer Length
  83. *@ Description : relate to RX FIFO & TX FIFO
  84. *******************************************************************************/
  85. #define I2C_FIFO_DEPTH 8
  86. #define I2C_RX_TL_CNT I2C_FIFO_DEPTH
  87. #define I2C_TX_TL_CNT I2C_FIFO_DEPTH
  88. /*******************************************************************************
  89. *@ Module : Macro Define
  90. *@ Description : Which IIC To be used
  91. *******************************************************************************/
  92. #define USE_AP_I2CX I2C0 //define use i2c0 or i2c1,0:i2c0,1:i2c1
  93. /*******************************************************************************
  94. *@ Module : Macro Define
  95. *@ Description : None
  96. *******************************************************************************/
  97. #define I2C0_IRQ_ENABLE() *(volatile unsigned int *) 0xe000e100 |= BIT(12)
  98. #define I2C0_IRQ_DISABLE() *(volatile unsigned int *) 0xe000e100 &= ~BIT(12)
  99. #define I2C1_IRQ_ENABLE() *(volatile unsigned int *) 0xe000e100 |= BIT(13)
  100. #define I2C1_IRQ_DISABLE() *(volatile unsigned int *) 0xe000e100 &= ~BIT(13)
  101. #define I2C_READ_CMD(pi2cdev) (pi2cdev->IC_DATA_CMD = 0x100) //Read
  102. #define I2C_RX_FIFO_FULL(pi2cdev) ((pi2cdev->IC_STATUS & 0x10)==0x10)
  103. #define I2C_RX_FIFO_NOT_EMPTY(pi2cdev) ((pi2cdev->IC_STATUS & 0x08)==0x08)
  104. #define I2C_TX_FIFO_EMPTY(pi2cdev) ((pi2cdev->IC_STATUS & 0x04)==0x04)
  105. #define I2C_TX_FIFO_NOT_FULL(pi2cdev) ((pi2cdev->IC_STATUS & 0x02)==0x02)
  106. #define I2C_WAIT_RD_REQ(pi2cdev) !((pi2cdev->IC_RAW_INTR_STAT & 0x20)==0x20)
  107. #define I2C_RD_REQ(pi2cdev) ((pi2cdev->IC_RAW_INTR_STAT & 0x20)==0x20)
  108. #define I2C_NUMBER_DATA_RX_FIFO(pi2cdev) (pi2cdev->IC_RXFLR)
  109. #define I2C_NUMBER_DATA_TX_FIFO(pi2cdev) (pi2cdev->IC_TXFLR)
  110. #define I2C_CLR_RD_REQ(pi2cdev) (pi2cdev->IC_CLR_RD_REG)
  111. #define I2C_CLR_TX_ABRT(pi2cdev) (pi2cdev->IC_CLR_TX_ABRT)
  112. #define I2C_ENABLE(pi2cdev) (pi2cdev->IC_ENABLE=1)
  113. #define I2C_DISABLE(pi2cdev) (pi2cdev->IC_ENABLE=0)
  114. /*******************************************************************************
  115. *@ Module : I2C Interrupt Mask Register
  116. *@ Description : Interrupt MASK bit
  117. *******************************************************************************/
  118. #define I2C_MASK_RX_UNDER 0x0001
  119. #define I2C_MASK_RX_OVER 0x0002
  120. #define I2C_MASK_RX_FULL 0x0004
  121. #define I2C_MASK_TX_OVER 0x0008
  122. #define I2C_MASK_TX_EMPTY 0x0010
  123. #define I2C_MASK_RD_REQ 0x0020
  124. #define I2C_MASK_TX_ABRT 0x0040
  125. #define I2C_MASK_RX_DONE 0x0080
  126. #define I2C_MASK_ACTIVITY 0x0100
  127. #define I2C_MASK_STOP_DET 0x0200
  128. #define I2C_MASK_START_DET 0x0400
  129. #define I2C_MASK_GEN_CALL 0x0800
  130. /*******************************************************************************
  131. *@ Module : I2C Status Register
  132. *@ Description : Status Register BIT(Indicate transfer and FIFO Status)
  133. *******************************************************************************/
  134. #define I2C_STATUS_ACTIVITY 0x0001
  135. #define I2C_STATUS_TFNF 0x0002
  136. #define I2C_STATUS_TFE 0x0004
  137. #define I2C_STATUS_RFNE 0x0008
  138. #define I2C_STATUS_RFF 0x0010
  139. #define I2C_STATUS_MST_ACTIVITY 0x0020
  140. #define I2C_STATUS_SLV_ACTIVITY 0x0040
  141. /*******************************************************************************
  142. *@ Module : IIC Speed Mode
  143. *@ Description : None
  144. *******************************************************************************/
  145. typedef enum{
  146. SPEED_STANDARD = 1, //standard mode
  147. SPEED_FAST, //fast mode
  148. // SPEED_HIGH //high mode
  149. }I2C_SPEED_e;
  150. /*******************************************************************************
  151. *@ Module : IIC Clock
  152. *@ Description : None
  153. *******************************************************************************/
  154. typedef enum
  155. {
  156. I2C_CLOCK_100K = 0x00,
  157. I2C_CLOCK_400K ,
  158. } I2C_CLOCK_e;
  159. typedef struct _I2C_Evt_t{
  160. uint8_t type;
  161. uint8_t* data;
  162. uint8_t len;
  163. }I2C_Evt_t;
  164. typedef enum{
  165. I2C_TX_STATE_UNINIT = 0,
  166. I2C_TX_STATE_IDLE,
  167. I2C_TX_STATE_TX,
  168. I2C_TX_STATE_ERR
  169. }I2C_STATE;
  170. /*******************************************************************************
  171. *@ Module : Function declaration
  172. *@ Description : None
  173. *******************************************************************************/
  174. int hal_i2c_send(void* pi2c, uint8_t* str,uint8_t len);
  175. void* hal_i2c_init(i2c_dev_t dev, I2C_CLOCK_e i2c_clock_rate);
  176. int hal_i2c_deinit(void* pi2c);
  177. int hal_i2c_pin_init(i2c_dev_t dev, GPIO_Pin_e pin_sda, GPIO_Pin_e pin_clk);
  178. int hal_i2c_addr_update(void* pi2c, uint8_t addr);
  179. int hal_i2c_wait_tx_completed(void* pi2c);
  180. int hal_i2c_tx_start(void* pi2c);
  181. int hal_i2c_read(void* pi2c,uint8_t slave_addr,uint8_t reg,uint8_t* data,uint8_t size);
  182. #endif