i2c_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.c
  30. * @brief i2c general Function
  31. * @version 1.0
  32. * Copyright(C) 2016, PhyPlus Semiconductor
  33. * All rights reserved.
  34. *
  35. *******************************************************************************/
  36. /*******************************************************************************
  37. *@ Module : Pre-Define
  38. *@ Description : None
  39. *******************************************************************************/
  40. #define _I2C_Common_
  41. /*******************************************************************************
  42. *@ Module : Includes
  43. *@ Description : None
  44. *******************************************************************************/
  45. #include "i2c_common.h"
  46. #include "error.h"
  47. #include "log.h"
  48. /*******************************************************************************
  49. *@ Module : Macro Define
  50. *@ Description : None
  51. *******************************************************************************/
  52. #define CONCAT_2(p1,p2) p1##p2
  53. #define CONCAT_3(p1,p2,p3) p1##p2##p3
  54. #define DRV_IIC_INSTANCE(id) CONCAT_2(AP_I2C,id)
  55. #define DRV_GET_IIC_MOD_ID(id) CONCAT_2(MOD_I2C,id)
  56. #define DRV_GET_IIC_IRQ_ID(id) CONCAT_3(I2C,id,_IRQ)
  57. /*******************************************************************************
  58. *@ Module : Typedef struct
  59. *@ Description : I2C Module
  60. *******************************************************************************/
  61. typedef struct
  62. {
  63. uint8_t busy; // busy
  64. }I2C_Mode_t;
  65. /*******************************************************************************
  66. *@ Module : Internal Variable
  67. *@ Description : None
  68. *******************************************************************************/
  69. static I2C_Mode_t I2C_Mode[IIC_COUNT];
  70. /*******************************************************************************
  71. *@ Module : I2C IRQ Handler
  72. *@ Description : None
  73. *******************************************************************************/
  74. I2C_Hdl_t I2C0_IRQ_Handle = NULL;
  75. I2C_Hdl_t I2C1_IRQ_Handle = NULL;
  76. /*******************************************************************************
  77. *@ Module : IIC Slave Address pre-Declare
  78. *@ Description : address , which can not be used
  79. *******************************************************************************/
  80. const unsigned char I2C_INVALID_SLAVE_ADDR[I2C_NOT_USED_SLAVE_ADDRLEN] = {0x00,0x07,0x78,0x7f};
  81. /*******************************************************************************
  82. *@ Module : Get_IIC_Instance
  83. *@ Description : None
  84. *******************************************************************************/
  85. AP_I2C_TypeDef *Hal_Get_IIC_Instance(uint8_t id)
  86. {
  87. if(IIC_Module0 == id)
  88. return (((AP_I2C_TypeDef *)DRV_IIC_INSTANCE(IIC_Module0)));
  89. else
  90. return (((AP_I2C_TypeDef *)DRV_IIC_INSTANCE(IIC_Module1)));
  91. }
  92. /*******************************************************************************
  93. *@ Module : Hal_GetIIC_ModuleID
  94. *@ Description : Get IIC Module Index as MODULE_e type
  95. *******************************************************************************/
  96. MODULE_e Hal_GetIIC_ModuleID(uint8_t id)
  97. {
  98. if(IIC_Module0 == id)
  99. return DRV_GET_IIC_MOD_ID(IIC_Module0);
  100. else
  101. return DRV_GET_IIC_MOD_ID(IIC_Module1);
  102. }
  103. /*******************************************************************************
  104. *@ Module : Hal_GetIIC_IRQID
  105. *@ Description : Get IIC Interrupt Index
  106. *******************************************************************************/
  107. uint8_t Hal_GetIIC_IRQID(uint8_t id)
  108. {
  109. if(IIC_Module0 == id)
  110. return DRV_GET_IIC_IRQ_ID(IIC_Module0);
  111. else
  112. return DRV_GET_IIC_IRQ_ID(IIC_Module1);
  113. }
  114. /*******************************************************************************
  115. *@ Module : Hal_GetIIC_PIN_Fmux
  116. *@ Description : None
  117. *******************************************************************************/
  118. void Hal_GetIIC_PIN_Fmux(uint8_t id,Fmux_Type_e *SCL_Fmux,Fmux_Type_e *SDA_Fmux)
  119. {
  120. if(IIC_Module0 == id)
  121. {
  122. *SCL_Fmux = IIC0_SCL;
  123. *SDA_Fmux = IIC0_SDA;
  124. }
  125. else
  126. {
  127. *SCL_Fmux = IIC1_SCL;
  128. *SDA_Fmux = IIC1_SDA;
  129. }
  130. }
  131. /*******************************************************************************
  132. *@ Module : Register call back function
  133. *@ Description : None
  134. *******************************************************************************/
  135. uint8_t Hal_IIC_Register_CallBack(uint8_t id,I2C_Hdl_t cb)
  136. {
  137. if(id == IIC_Module0)
  138. {
  139. I2C_Mode[IIC_Module0].busy = TRUE;
  140. I2C0_IRQ_Handle = cb;
  141. return PPlus_IIC_SUCCESS;
  142. }
  143. else if( id == IIC_Module1 )
  144. {
  145. I2C_Mode[IIC_Module1].busy = TRUE;
  146. I2C1_IRQ_Handle = cb;
  147. return PPlus_IIC_SUCCESS;
  148. }
  149. return PPlus_ERR_IIC_ID;
  150. }
  151. /*******************************************************************************
  152. *@ Module : unregister call back function
  153. *@ Description : None
  154. *******************************************************************************/
  155. uint8_t Hal_IIC_unRegister_CallBack(uint8_t id)
  156. {
  157. if(id == IIC_Module0)
  158. {
  159. I2C_Mode[IIC_Module0].busy = FALSE;
  160. I2C0_IRQ_Handle = NULL;
  161. return PPlus_IIC_SUCCESS;
  162. }
  163. else if( id == IIC_Module1 )
  164. {
  165. I2C_Mode[IIC_Module1].busy = FALSE;
  166. I2C1_IRQ_Handle = NULL;
  167. return PPlus_IIC_SUCCESS;
  168. }
  169. return PPlus_ERR_IIC_ID;
  170. }
  171. /*******************************************************************************
  172. *@ Module : IIC Check IIC Valid
  173. *@ Description : [para in]:id,iic module 0,1
  174. *******************************************************************************/
  175. uint8_t Hal_IIC_Valid_Check(uint8_t id)
  176. {
  177. if( id > IIC_COUNT)
  178. return PPlus_ERR_IIC_ID;
  179. else
  180. return (I2C_Mode[id].busy);
  181. }
  182. /*******************************************************************************
  183. *@ Module : IIC Check IIC Address Valid
  184. *@ Description : [para in]:Addr,usr set master or slave address
  185. *******************************************************************************/
  186. uint8_t Hal_IIC_Addr_Valid(uint8_t Addr)
  187. {
  188. for(unsigned char i=0;i<I2C_NOT_USED_SLAVE_ADDRLEN;i++)
  189. {
  190. if(I2C_INVALID_SLAVE_ADDR[i] == Addr)
  191. return PPlus_ERR_IIC_ADDRESS;
  192. }
  193. return PPlus_IIC_SUCCESS;
  194. }
  195. /*******************************************************************************
  196. *@ Module : Check IIC Closed Success
  197. *@ Description : [para in]:Ins,the instance wanted to check
  198. *******************************************************************************/
  199. uint8_t Hal_Check_IIC_Closed(AP_I2C_TypeDef *Ins)
  200. {
  201. if(Ins->IC_ENABLE_STATUS & 0x0001)
  202. {
  203. // IIC Already enabled
  204. return PPLUS_ERR_IIC_ENABLE;
  205. }
  206. else
  207. {
  208. // IIC Closed Successed
  209. return PPlus_IIC_SUCCESS;
  210. }
  211. }
  212. /*******************************************************************************
  213. *@ Module : IIC Receive data from RX FIFO
  214. *@ Description : [para in]:Ins,the instance wanted to READ Data
  215. *******************************************************************************/
  216. uint8_t Hal_IIC_Read_RXFIFO(AP_I2C_TypeDef *Ins)
  217. {
  218. return Ins->IC_DATA_CMD ;
  219. }
  220. /*******************************************************************************
  221. *@ Module : Hal_INTR_SOURCE_Clear
  222. *@ Description : [para in]:Ins,the instance wanted to READ Data,irqs:interrupt source
  223. *******************************************************************************/
  224. void Hal_INTR_SOURCE_Clear(AP_I2C_TypeDef *Ins,uint32_t irqs)
  225. {
  226. switch( irqs )
  227. {
  228. case I2C_MASK_RX_UNDER:
  229. Ins->IC_CLR_UNDER;
  230. break;
  231. case I2C_MASK_RX_OVER:
  232. Ins->IC_CLR_RX_OVER;
  233. break;
  234. case I2C_MASK_TX_OVER:
  235. Ins->IC_CLR_TX_OVER;
  236. break;
  237. case I2C_MASK_RD_REQ:
  238. Ins->IC_CLR_RD_REG;
  239. break;
  240. case I2C_MASK_TX_ABRT:
  241. Ins->IC_CLR_TX_ABRT;
  242. break;
  243. case I2C_MASK_RX_DONE:
  244. Ins->IC_CLR_RX_DONE;
  245. break;
  246. case I2C_MASK_ACTIVITY:
  247. Ins->IC_CLR_ACTIVITY;
  248. break;
  249. case I2C_MASK_STOP_DET:
  250. Ins->IC_CLR_STOP_DET;
  251. break;
  252. case I2C_MASK_START_DET:
  253. Ins->IC_CLR_START_DET;
  254. break;
  255. case I2C_MASK_GEN_CALL:
  256. Ins->IC_CLR_GEN_CALL;
  257. break;
  258. }
  259. }
  260. /*******************************************************************************
  261. *@ Module : Hal_IIC_Write_TXFIFO
  262. *@ Description : [para in]:Ins,the instance wanted to write,
  263. *******************************************************************************/
  264. void Hal_IIC_Write_TXFIFO(AP_I2C_TypeDef *Ins,uint8_t data)
  265. {
  266. Ins->IC_DATA_CMD = ((I2C_Data_WRITE << 8) | data);
  267. }
  268. /*******************************************************************************
  269. *@ Module : hal_I2C0_IRQHandler
  270. *@ Description : This function process for i2c0 interrupt
  271. *******************************************************************************/
  272. void __attribute__((used)) Hal_I2C0_IRQHandler(void)
  273. {
  274. I2C_Evt_t irq_s;
  275. irq_s.type= (I2C_EVT)(AP_I2C0->IC_INTR_STAT);
  276. (*I2C0_IRQ_Handle)(&irq_s);
  277. }
  278. /*******************************************************************************
  279. *@ Module : hal_I2C1_IRQHandler
  280. *@ Description : This function process for i2c1 interrupt
  281. *******************************************************************************/
  282. void __attribute__((used)) Hal_I2C1_IRQHandler(void)
  283. {
  284. I2C_Evt_t irq_s;
  285. irq_s.type= (I2C_EVT)(AP_I2C1->IC_INTR_STAT);
  286. (*I2C1_IRQ_Handle)(&irq_s);
  287. }