i2c_common.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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_comman.h
  30. * @brief i2c General Configuration
  31. * @version 1.0
  32. * Copyright(C) 2016, PhyPlus Semiconductor
  33. * All rights reserved.
  34. *******************************************************************************/
  35. #ifndef __I2C_COMMON_H__
  36. #define __I2C_COMMON_H__
  37. /*******************************************************************************
  38. *@ Module : Includes
  39. *@ Description : NULL
  40. *******************************************************************************/
  41. #include "ap_cp.h"
  42. #include "gpio.h"
  43. #include "hal_mcu.h"
  44. /*******************************************************************************
  45. *@ Module : pre-compile
  46. *@ Description : NULL
  47. *******************************************************************************/
  48. /*******************************************************************************
  49. *@ Module : Macro Define
  50. *@ Description : IIC Error Code
  51. *******************************************************************************/
  52. #define PPlus_IIC_SUCCESS 0
  53. #define PPlus_ERR_IIC_ADDRESS 0xFE
  54. #define PPlus_ERR_IIC_ID 0xFD
  55. #define PPLUS_ERR_IIC_BUSY 0xFC
  56. #define PPLUS_ERR_IIC_ENABLE 0xFB
  57. #define PPlus_ERR_IIC_FAILURE 0xFF
  58. /*******************************************************************************
  59. *@ Module : Macro Define
  60. *@ Description : IIC InValid Handle
  61. *******************************************************************************/
  62. #define PPlus_INVALID_HANDLE 0xFF
  63. /*******************************************************************************
  64. *@ Module : Macro Define
  65. *@ Description : The number of IIC Module(Two IIC:IIC0,IIC1)
  66. *******************************************************************************/
  67. #define IIC_COUNT 2
  68. /*******************************************************************************
  69. *@ Module : Macro Define
  70. *@ Description : Indicate IIC Module
  71. *******************************************************************************/
  72. #define IIC_Module0 0
  73. #define IIC_Module1 1
  74. /*******************************************************************************
  75. *@ Module : Macro Define
  76. *@ Description : IIC Bus Read or Wirte Device
  77. *******************************************************************************/
  78. #define I2C_COMMAND_READ 0x01
  79. #define I2C_COMMAND_WRITE 0x00
  80. /*******************************************************************************
  81. *@ Module : Macro Define
  82. *@ Description : IIC Bus Read or Write data from FIFO
  83. *******************************************************************************/
  84. #define I2C_Data_READ 0x01
  85. #define I2C_Data_WRITE 0x00
  86. /*******************************************************************************
  87. *@ Module : Macro Define
  88. *@ Description : Can not be used IIC Slave Address Length
  89. *******************************************************************************/
  90. #define I2C_NOT_USED_SLAVE_ADDRLEN 4
  91. /*******************************************************************************
  92. *@ Module : Macro Define
  93. *@ Description : None
  94. *******************************************************************************/
  95. #define I2C_IC_DEFAULT_TAR_UPDATE 0
  96. #define I2C_IC_CLK_TYPE 1
  97. /*******************************************************************************
  98. *@ Module : Macro Define
  99. *@ Description : IIC Default Master/Slave Address
  100. *******************************************************************************/
  101. #define I2C_IC_DEFAULT_ADDR 0x0010
  102. /*******************************************************************************
  103. *@ Module : I2C Buffer Length
  104. *@ Description : relate to RX FIFO & TX FIFO
  105. *******************************************************************************/
  106. #define I2C_FIFO_DEPTH 8
  107. #define I2C_RX_TL_CNT I2C_FIFO_DEPTH
  108. #define I2C_TX_TL_CNT I2C_FIFO_DEPTH
  109. /*******************************************************************************
  110. *@ Module : Macro Define
  111. *@ Description : IC_CON Bit Field Description
  112. *******************************************************************************/
  113. // Determine IIC Slave enable or disable after reset(salve is enable in reset state)
  114. // if slave enabled , MASTER_MODE should be disabled
  115. #define IC_CON_IC_SLAVE_DISABLE 0x0040
  116. // Determine whether restart conditions may be send when act as a master
  117. // when act an a master , must enable this function
  118. #define IC_CON_IC_RESTART_EN 0x0020
  119. #define IC_CON_IC_10BITADDR_MASTER 0x0010
  120. // Determine the IIC Master Address Mode(10bit)
  121. // Determine the IIC Slave Address Mode(10bit)
  122. #define IC_CON_IC_10BITADDR_SLAVE 0x0008
  123. // Determine IIC Speed Mode
  124. #define IC_CON_IC_SPEED_100K 0x0002
  125. #define IC_CON_IC_SPEED_400K 0x0004
  126. #define IC_CON_IC_SPEED_3_4M 0x0006
  127. // Determine IIC Master Enable
  128. #define IC_CON_MASTER_MODE 0x0001
  129. /*******************************************************************************
  130. *@ Module : Macro Define
  131. *@ Description : IC_TAR Bit Field Description
  132. *******************************************************************************/
  133. // this bit related to I2C_IC_DEFAULT_TAR_UPDATE Value
  134. #define IC_TAR_IC_10BITADDR_MASTER 0x1000
  135. // Indicate whether performance a Gengral call or start byte Command
  136. // relate to IC_TAR_GC_OR_START
  137. #define IC_TAR_SPECIAL_COMMAND_ENABLE 0x0800
  138. // Dependency IC_TAR_SPECIAL_COMMAND_ENABLE
  139. // if set, Start Byte,otherwise General Call
  140. // General Call:only writes may be performed.when read resault TX_ABRT
  141. // Start Byte:send Start Byte(0x01) to Synchronizate IIC Clock
  142. #define IC_TAR_GC_OR_START 0x0400
  143. // IC Default Slave Address
  144. #define IC_TAR_DEFAULT_SLAVE_ADDRESS I2C_IC_DEFAULT_TAR_SLAVE_ADDR
  145. /*******************************************************************************
  146. *@ Module : Macro Define
  147. *@ Description : IC_SAR
  148. *******************************************************************************/
  149. #define IC_SAR_DEFAUT_SLAVE_ADDRESS I2C_IC_DEFAULT_TAR_SLAVE_ADDR
  150. /*******************************************************************************
  151. *@ Module : I2C Interrupt Status Register
  152. *@ Description : Interrupt Status bit(cleared by reading the matching interrupt clear register.)
  153. *******************************************************************************/
  154. #define I2C_INTR_STAT_RX_UNDER 0x0001
  155. #define I2C_INTR_STAT_RX_OVER 0x0002
  156. #define I2C_INTR_STAT_RX_FULL 0x0004
  157. #define I2C_INTR_STAT_TX_OVER 0x0008
  158. #define I2C_INTR_STAT_TX_EMPTY 0x0010
  159. #define I2C_INTR_STAT_RD_REQ 0x0020
  160. #define I2C_INTR_STAT_TX_ABRT 0x0040
  161. #define I2C_INTR_STAT_RX_DONE 0x0080
  162. #define I2C_INTR_STAT_ACTIVITY 0x0100
  163. #define I2C_INTR_STAT_STOP_DET 0x0200
  164. #define I2C_INTR_STAT_START_DET 0x0400
  165. #define I2C_INTR_STAT_GEN_CALL 0x0800
  166. /*******************************************************************************
  167. *@ Module : I2C Interrupt Mask Register
  168. *@ Description : Interrupt MASK bit
  169. *******************************************************************************/
  170. #define I2C_MASK_RX_UNDER 0x0001
  171. #define I2C_MASK_RX_OVER 0x0002
  172. #define I2C_MASK_RX_FULL 0x0004
  173. #define I2C_MASK_TX_OVER 0x0008
  174. #define I2C_MASK_TX_EMPTY 0x0010
  175. #define I2C_MASK_RD_REQ 0x0020
  176. #define I2C_MASK_TX_ABRT 0x0040
  177. #define I2C_MASK_RX_DONE 0x0080
  178. #define I2C_MASK_ACTIVITY 0x0100
  179. #define I2C_MASK_STOP_DET 0x0200
  180. #define I2C_MASK_START_DET 0x0400
  181. #define I2C_MASK_GEN_CALL 0x0800
  182. #define I2C_DISABLE_ALL_INTR 0
  183. /*******************************************************************************
  184. *@ Module : I2C Raw Interrupt Register
  185. *@ Description : Interrupt MASK bit
  186. *******************************************************************************/
  187. #define I2C_RAW_INTR_RX_UNDER 0x0001
  188. #define I2C_RAW_INTR_RX_OVER 0x0002
  189. #define I2C_RAW_INTR_RX_FULL 0x0004
  190. #define I2C_RAW_INTR_TX_OVER 0x0008
  191. #define I2C_RAW_INTR_TX_EMPTY 0x0010
  192. #define I2C_RAW_INTR_RD_REQ 0x0020
  193. #define I2C_RAW_INTR_TX_ABRT 0x0040
  194. #define I2C_RAW_INTR_RX_DONE 0x0080
  195. #define I2C_RAW_INTR_ACTIVITY 0x0100
  196. #define I2C_RAW_INTR_STOP_DET 0x0200
  197. #define I2C_RAW_INTR_START_DET 0x0400
  198. #define I2C_RAW_INTR_GEN_CALL 0x0800
  199. /*******************************************************************************
  200. *@ Module : I2C ENABLE Status Register
  201. *@ Description : I2C ENABLE STATUS Bit
  202. *******************************************************************************/
  203. #define I2C_ENSTA_IC_ENABLE_STATUS 0x0001
  204. #define I2C_ENSTA_SLV_RX_ABORT 0x0002
  205. #define I2C_ENSTA_SLV_RX_DATA_LOST 0x0004
  206. /*******************************************************************************
  207. *@ Module : I2C Status Register
  208. *@ Description : Only read register ,indicate current transfer stauts and FIFO status
  209. *******************************************************************************/
  210. #define I2C_STAT_SLV_ACTIVITY 0x0040
  211. #define I2C_STAT_MST_ACTIVITY 0x0020
  212. //Receive FIFO Completely Full
  213. #define I2C_STAT_RFF 0x0010
  214. //Receive FIFO Not Empty
  215. #define I2C_STAT_RFNF 0x0008
  216. // Transmit FIFO Completely Empty
  217. #define I2C_STAT_TFE 0x0004
  218. // Transmit FIFO Not Full
  219. #define I2C_STAT_TFNF 0x0002
  220. #define I2C_STAT_ACTIVITY 0x0001
  221. /*******************************************************************************
  222. *@ Module : I2C Transmit Abort Source Register
  223. *@ Description : indicate the source of the TX_ABRT
  224. *******************************************************************************/
  225. #define I2C_ABRT_7B_ADDR_NOACK 0x0001
  226. #define I2C_ABRT_10B_ADDR1_NOACK 0x0002
  227. #define I2C_ABRT_10B_ADDR2_NOACK 0x0004
  228. #define I2C_ABRT_TXDATA_NOACK 0x0008
  229. // master send a General Call , but no slave ACK
  230. #define I2C_ABRT_GCALL_NOACK 0x0010
  231. // master send data to the bus , but user programmed the data direction is read
  232. // from the bus(relate to IC_DATA_CMD[8])
  233. #define I2C_ABRT_GCALL_READ 0x0020
  234. // master in high speed mode ,and its code was ACK(Should not ACK)
  235. #define I2C_ABRT_HS_ACKDET 0x0040
  236. // master send START Byte and detect ACK(Should not ACK)
  237. #define I2C_ABRT_SBYTE_ACKDET 0x0080
  238. // the restart is diabled,and the user is trying to use the master to transmit
  239. // data in high speed mode
  240. #define I2C_ABRT_HS_NORTART 0x0100
  241. //
  242. #define I2C_ABRT_SBYTE_NORESTART 0x0200
  243. #define I2C_ABRT_10B_RD_NORSTART 0x0400
  244. // user tries to initiate a master operation with the master mode disabled
  245. #define I2C_ABRT_MASTER_DIS 0x0800
  246. #define I2C_ABR_LOST 0x1000
  247. // slave receive a read command ,but there's some data exists in TX FIFO,
  248. // and issues interrupt to flush old TX FIFO
  249. #define I2C_ABRT_SLVFLUSH_TXFIFO 0x2000
  250. #define I2C_ABRT_SLV_ABRLOST 0x4000
  251. // slave transmit data to the master , and use the read data command from the
  252. // bus(relate to IC_DATA_CMD[8])
  253. #define I2C_ABRT_SLVRD_INTX 0x8000
  254. /*******************************************************************************
  255. *@ Module : IIC Work Mode
  256. *@ Description : None
  257. *******************************************************************************/
  258. typedef enum{
  259. Slave = 0,
  260. Master = IC_CON_IC_SLAVE_DISABLE | IC_CON_IC_RESTART_EN | IC_CON_MASTER_MODE,
  261. }I2C_WorkMode;
  262. /*******************************************************************************
  263. *@ Module : IIC Speed Mode
  264. *@ Description : None
  265. *******************************************************************************/
  266. typedef enum{
  267. SPEED_STANDARD = 0x0002, //standard mode
  268. SPEED_FAST = 0x0004, //fast mode
  269. SPEED_HIGH = 0x0006, //high mode
  270. }I2C_SPEED_e;
  271. /*******************************************************************************
  272. *@ Module : IIC Clock
  273. *@ Description : None
  274. *******************************************************************************/
  275. typedef enum
  276. {
  277. I2C_CLOCK_100K = 0x0002,
  278. I2C_CLOCK_400K = 0x0004,
  279. I2C_CLOCK_3_4M = 0x0006,
  280. } I2C_CLOCK_e;
  281. /*******************************************************************************
  282. *@ Module : IIC Address Mode
  283. *@ Description : Only support 7 bit Address,be careful while using
  284. *******************************************************************************/
  285. typedef enum
  286. {
  287. I2C_ADDR_7bit = 0,
  288. I2C_ADDR_10bit
  289. }I2C_ADDRESS_e;
  290. /*******************************************************************************
  291. *@ Module : transfer mode
  292. *@ Description :
  293. * I2C_MODE_BLOCKING blocks task execution while an I2C transfer is in progress
  294. * I2C_MODE_CALLBACK does not block task execution; but calls a callback
  295. * function when the I2C transfer has completed
  296. *******************************************************************************/
  297. typedef enum
  298. {
  299. I2C_MODE_BLOCKING, /*!< I2C_transfer blocks execution*/
  300. I2C_MODE_CALLBACK /*!< I2C_transfer queues transactions and does not block */
  301. } I2C_TransferMode;
  302. /*******************************************************************************
  303. *@ Module : I2C Event enum
  304. *@ Description : None
  305. *******************************************************************************/
  306. typedef enum{
  307. // INTR SOURCE EVENT
  308. I2C_RX_UNDER_Evt = 0x0001,
  309. I2C_RX_OVER_Evt = 0x0002,
  310. I2C_RX_FULL_Evt = 0x0004,
  311. I2C_TX_OVER_Evt = 0x0008,
  312. I2C_TX_EMPTY_Evt = 0x0010,
  313. I2C_RD_REQ_Evt = 0x0020,
  314. I2C_TX_ABRT_Evt = 0x0040,
  315. I2C_RX_DONE_Evt = 0x0080,
  316. I2C_ACTIVITY_Evt = 0x0100,
  317. I2C_STOP_DET_Evt = 0x0200,
  318. I2C_START_DET_Evt = 0x0400,
  319. I2C_GEN_CALL_Evt = 0x0800,
  320. // User Event
  321. I2C_DINIT_SUCCESS = 0x1000
  322. }I2C_EVT;
  323. /*******************************************************************************
  324. *@ Module : I2C Event
  325. *@ Description : None
  326. *******************************************************************************/
  327. typedef struct {
  328. I2C_EVT type;
  329. uint8_t len;
  330. }I2C_Evt_t;
  331. /*******************************************************************************
  332. *@ Module : Function *p
  333. *@ Description : None
  334. *******************************************************************************/
  335. typedef void (*I2C_Hdl_t)(I2C_Evt_t *pev);
  336. /*******************************************************************************
  337. *@ Module : I2C Master Paramter structure
  338. *@ Description : None
  339. *******************************************************************************/
  340. typedef struct
  341. {
  342. // General
  343. uint8_t id; // set
  344. I2C_WorkMode workmode; // init some Para ,according to workmode
  345. I2C_CLOCK_e ClockMode;
  346. I2C_ADDRESS_e AddressMode;
  347. bool use_fifo; // check if use fifo,default used
  348. uint8_t RX_FIFO_Len; // RX , TX FIFO SET
  349. uint8_t Tx_FIFO_Len;
  350. GPIO_Pin_e SDA_PIN; // I2C Pin
  351. GPIO_Pin_e SCL_PIN;
  352. I2C_TransferMode TransferMode; // according to TransferMode, that evt_handler can be
  353. // enable or disable
  354. I2C_Hdl_t evt_handler;
  355. uint8_t Master_Addressing; // TAR,when as master
  356. uint32_t IC_xS_SCL_HCNT;
  357. uint32_t IC_xS_SCL_LCNT;
  358. }I2C_Master_Parameter;
  359. /*******************************************************************************
  360. *@ Module : Master Default Init Parameter
  361. *@ Description : None
  362. *******************************************************************************/
  363. //I2CCOM_Ext I2C_Parameter I2C_DefaultPara;
  364. /*******************************************************************************
  365. *@ Module : Function statement
  366. *@ Description :
  367. *******************************************************************************/
  368. uint8_t Hal_Check_IIC_IsAlready_Closed(void);
  369. /*******************************************************************************
  370. *@ Module : Get_IIC_Instance
  371. *@ Description :
  372. [para in]:id,iic module id 0 or 1
  373. [return]:instance address
  374. *******************************************************************************/
  375. AP_I2C_TypeDef *Hal_Get_IIC_Instance(uint8_t id);
  376. /*******************************************************************************
  377. *@ Module : Hal_GetIIC_ModuleID
  378. *@ Description : None
  379. *******************************************************************************/
  380. MODULE_e Hal_GetIIC_ModuleID(uint8_t id);
  381. /*******************************************************************************
  382. *@ Module : Hal_GetIIC_Interrupt ID
  383. *@ Description : None
  384. *******************************************************************************/
  385. uint8_t Hal_GetIIC_IRQID(uint8_t id);
  386. /*******************************************************************************
  387. *@ Module : Hal_GetIIC_PIN_Fmux ID
  388. *@ Description : None
  389. *******************************************************************************/
  390. void Hal_GetIIC_PIN_Fmux(uint8_t id,Fmux_Type_e *SCL_Fmux,Fmux_Type_e *SDA_Fmux);
  391. /*******************************************************************************
  392. *@ Module : IIC Register CallBack Function
  393. *@ Description :
  394. [para in]:id,iic module 0,1;cb , call back function
  395. *******************************************************************************/
  396. uint8_t Hal_IIC_Register_CallBack(uint8_t id,I2C_Hdl_t cb);
  397. /*******************************************************************************
  398. *@ Module : IIC unRegister CallBack Function
  399. *@ Description : [para in]:id,iic module 0,1
  400. *******************************************************************************/
  401. uint8_t Hal_IIC_unRegister_CallBack(uint8_t id);
  402. /*******************************************************************************
  403. *@ Module : IIC Check IIC Valid
  404. *@ Description : [para in]:id,iic module 0,1
  405. *******************************************************************************/
  406. uint8_t Hal_IIC_Valid_Check(uint8_t id);
  407. /*******************************************************************************
  408. *@ Module : IIC Check IIC Address Valid
  409. *@ Description : [para in]:Addr,usr set master or slave address
  410. *******************************************************************************/
  411. uint8_t Hal_IIC_Addr_Valid(uint8_t Addr);
  412. /*******************************************************************************
  413. *@ Module : Check IIC Closed Success
  414. *@ Description : [para in]:Ins,the instance wanted to check
  415. *******************************************************************************/
  416. uint8_t Hal_Check_IIC_Closed(AP_I2C_TypeDef *Ins);
  417. /*******************************************************************************
  418. *@ Module : IIC Receive data from RX FIFO
  419. *@ Description : [para in]:Ins,the instance wanted to READ Data
  420. *******************************************************************************/
  421. uint8_t Hal_IIC_Read_RXFIFO(AP_I2C_TypeDef *Ins);
  422. /*******************************************************************************
  423. *@ Module : Hal_INTR_SOURCE_Clear
  424. *@ Description : [para in]:Ins,the instance wanted to READ Data,irqs:interrupt source
  425. *******************************************************************************/
  426. void Hal_INTR_SOURCE_Clear(AP_I2C_TypeDef *Ins,uint32_t irqs);
  427. /*******************************************************************************
  428. *@ Module : Hal_IIC_Write_TXFIFO
  429. *@ Description : [para in]:Ins,the instance wanted to write,
  430. *******************************************************************************/
  431. void Hal_IIC_Write_TXFIFO(AP_I2C_TypeDef *Ins,uint8_t data);
  432. void Hal_TRANS_ABRT_SourceCheck(void);
  433. void __attribute__((weak)) Hal_I2C0_IRQHandler(void);
  434. void __attribute__((weak)) Hal_I2C1_IRQHandler(void);
  435. #endif