i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.c
  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. /*******************************************************************************
  40. *@ Module : pre-compiler
  41. *@ Description : NULL
  42. *******************************************************************************/
  43. #define _I2C_CMD_
  44. /*******************************************************************************
  45. *@ Module : Includes
  46. *@ Description : None
  47. *******************************************************************************/
  48. #include "types.h"
  49. #include "hal_mcu.h"
  50. #include "i2c.h"
  51. #include "clock.h"
  52. #include "log.h"
  53. #include "error.h"
  54. #include "OSAL.h"
  55. #include "pwrmgr.h"
  56. #define I2C_OP_TIMEOUT 100 //100ms for an Byte operation
  57. extern uint32_t pclk;
  58. /**************************************************************************************
  59. * @fn hal_master_send_read_cmd
  60. *
  61. * @brief This function process for master send read command;It's vaild when the chip act as master
  62. *
  63. * input parameters
  64. *
  65. * @param uint8_t len: read length
  66. *
  67. * output parameters
  68. *
  69. * @param None.
  70. *
  71. * @return None.
  72. **************************************************************************************/
  73. static void hal_master_send_read_cmd(void* pi2c, uint8_t len){
  74. uint8_t i;
  75. AP_I2C_TypeDef * pi2cdev = (AP_I2C_TypeDef *)pi2c;
  76. for(i=0;i<len;i++){
  77. I2C_READ_CMD(pi2cdev);
  78. }
  79. }
  80. static void _hal_i2c_send_byte(void* pi2c, uint8_t data)
  81. {
  82. AP_I2C_TypeDef * pi2cdev = (AP_I2C_TypeDef *)pi2c;
  83. pi2cdev->IC_DATA_CMD = data; //data
  84. }
  85. /**************************************************************************************
  86. * @fn hal_i2c_send
  87. *
  88. * @brief This function process for send a serial(programe length) data by i2c interface
  89. *
  90. * input parameters
  91. *
  92. * @param unsigned char* str: send data(string)
  93. * uint32_t len: send length
  94. *
  95. * output parameters
  96. *
  97. * @param None.
  98. *
  99. * @return None.
  100. **************************************************************************************/
  101. int hal_i2c_send(void* pi2c, uint8_t* str,uint8_t len)
  102. {
  103. uint8_t i;
  104. AP_I2C_TypeDef * pi2cdev = (AP_I2C_TypeDef *)pi2c;
  105. if(pi2cdev != AP_I2C0 && pi2cdev != AP_I2C1){
  106. return PPlus_ERR_INVALID_PARAM;
  107. }
  108. for(i=0;i<len;i++){
  109. _hal_i2c_send_byte(pi2c, str[i]);
  110. }
  111. return PPlus_SUCCESS;
  112. }
  113. int hal_i2c_wait_tx_completed(void* pi2c)
  114. {
  115. int cnt = 0;
  116. //uint32 st0, st;
  117. AP_I2C_TypeDef * pi2cdev = (AP_I2C_TypeDef *)pi2c;
  118. I2C_INIT_TOUT(to);
  119. if(pi2cdev != AP_I2C0 && pi2cdev != AP_I2C1){
  120. return PPlus_ERR_INVALID_PARAM;
  121. }
  122. //st0 = pi2cdev->IC_INTR_STAT;
  123. //st = pi2cdev->IC_RAW_INTR_STAT;
  124. while(1){
  125. cnt++;
  126. if(pi2cdev->IC_RAW_INTR_STAT&0x200)//check tx empty
  127. break;
  128. I2C_CHECK_TOUT(to, I2C_OP_TIMEOUT, "hal_i2c_wait_tx_completed TO\n");
  129. }
  130. //LOG("I2c Tx empty:%x, %x\n", st0,st);
  131. return PPlus_SUCCESS;
  132. }
  133. void* hal_i2c_init(i2c_dev_t dev, I2C_CLOCK_e i2c_clock_rate)
  134. {
  135. AP_I2C_TypeDef * pi2cdev = NULL;
  136. if(dev == I2C_0){
  137. pi2cdev = AP_I2C0;
  138. clk_gate_enable(MOD_I2C0);
  139. }
  140. else if(dev == I2C_1){
  141. pi2cdev = AP_I2C1;
  142. clk_gate_enable(MOD_I2C1);
  143. }
  144. else{
  145. return NULL;
  146. }
  147. pi2cdev->IC_ENABLE=0;
  148. pi2cdev->IC_CON=0x61;
  149. if(i2c_clock_rate==I2C_CLOCK_100K){
  150. pi2cdev->IC_CON= ((pi2cdev->IC_CON) & 0xfffffff9)|(0x01 << 1);
  151. if(pclk==16000000){
  152. pi2cdev->IC_SS_SCL_HCNT=70; //16
  153. pi2cdev->IC_SS_SCL_LCNT=76; //32)
  154. }else if(pclk==32000000){
  155. pi2cdev->IC_SS_SCL_HCNT=148; //16
  156. pi2cdev->IC_SS_SCL_LCNT=154; //32)
  157. }else if(pclk==48000000){
  158. pi2cdev->IC_SS_SCL_HCNT=230; //16
  159. pi2cdev->IC_SS_SCL_LCNT=236; //32)
  160. }else if(pclk==64000000){
  161. pi2cdev->IC_SS_SCL_HCNT=307; //16
  162. pi2cdev->IC_SS_SCL_LCNT=320; //32)
  163. }else if(pclk==96000000){
  164. pi2cdev->IC_SS_SCL_HCNT=460; //16
  165. pi2cdev->IC_SS_SCL_LCNT=470; //32)
  166. }
  167. }else if(i2c_clock_rate==I2C_CLOCK_400K){
  168. pi2cdev->IC_CON= ((pi2cdev->IC_CON) & 0xfffffff9)|(0x02 << 1);
  169. if(pclk==16000000){
  170. pi2cdev->IC_FS_SCL_HCNT=10; //16
  171. pi2cdev->IC_FS_SCL_LCNT=17; //32)
  172. }else if(pclk==32000000){
  173. pi2cdev->IC_FS_SCL_HCNT=30; //16
  174. pi2cdev->IC_FS_SCL_LCNT=35; //32)
  175. }else if(pclk==48000000){
  176. pi2cdev->IC_FS_SCL_HCNT=48; //16
  177. pi2cdev->IC_FS_SCL_LCNT=54; //32)
  178. }else if(pclk==64000000){
  179. pi2cdev->IC_FS_SCL_HCNT=67; //16
  180. pi2cdev->IC_FS_SCL_LCNT=75; //32)
  181. }else if(pclk==96000000){
  182. pi2cdev->IC_FS_SCL_HCNT=105; //16
  183. pi2cdev->IC_FS_SCL_LCNT=113; //32)
  184. }
  185. }
  186. pi2cdev->IC_TAR = I2C_MASTER_ADDR_DEF;
  187. pi2cdev->IC_INTR_MASK=0;
  188. pi2cdev->IC_RX_TL=0x0;
  189. pi2cdev->IC_TX_TL=0x1;
  190. pi2cdev->IC_ENABLE=1;
  191. return (void*)pi2cdev;
  192. }
  193. int hal_i2c_deinit(void* pi2c){
  194. AP_I2C_TypeDef * pi2cdev = (AP_I2C_TypeDef *)pi2c;
  195. if(pi2cdev == AP_I2C0){
  196. pi2cdev->IC_ENABLE=0;
  197. clk_gate_disable(MOD_I2C0);
  198. }
  199. else if(pi2cdev == AP_I2C1){
  200. pi2cdev->IC_ENABLE=0;
  201. clk_gate_enable(MOD_I2C1);
  202. }
  203. else{
  204. return PPlus_ERR_INVALID_PARAM;
  205. }
  206. return PPlus_SUCCESS;
  207. }
  208. /**************************************************************************************
  209. * @fn hal_i2c_pin_init
  210. *
  211. * @brief This function process for i2c pin initial(2 lines);You can use two i2c,i2c0 and i2c1,should programe by USE_AP_I2CX
  212. *
  213. * input parameters
  214. *
  215. * @param GPIO_Pin_e pin_sda: define sda_pin
  216. * GPIO_Pin_e pin_clk: define clk_pin
  217. *
  218. * output parameters
  219. *
  220. * @param None.
  221. *
  222. * @return None.
  223. **************************************************************************************/
  224. int hal_i2c_pin_init(i2c_dev_t dev, GPIO_Pin_e pin_sda, GPIO_Pin_e pin_clk){
  225. if(dev == I2C_0){
  226. hal_gpio_fmux_set(pin_clk, IIC0_SCL);
  227. hal_gpio_fmux_set(pin_sda, IIC0_SDA);
  228. }
  229. else if(dev == I2C_1){
  230. hal_gpio_fmux_set(pin_clk, IIC1_SCL);
  231. hal_gpio_fmux_set(pin_sda, IIC1_SDA);
  232. }
  233. else{
  234. return PPlus_ERR_INVALID_PARAM;
  235. }
  236. hal_gpio_pull_set(pin_sda,STRONG_PULL_UP);
  237. hal_gpio_pull_set(pin_clk,STRONG_PULL_UP);
  238. return PPlus_SUCCESS;
  239. }
  240. int hal_i2c_tx_start(void* pi2c)
  241. {
  242. AP_I2C_TypeDef * pi2cdev = (AP_I2C_TypeDef *)pi2c;
  243. if(pi2cdev != AP_I2C0 && pi2cdev != AP_I2C1){
  244. return PPlus_ERR_INVALID_PARAM;
  245. }
  246. pi2cdev->IC_ENABLE=1;
  247. return PPlus_SUCCESS;
  248. }
  249. /**************************************************************************************
  250. * @fn hal_i2c_addr_update
  251. *
  252. * @brief This function process for tar update
  253. *
  254. * input parameters
  255. *
  256. * @param uint8_t addr: address
  257. *
  258. * output parameters
  259. *
  260. * @param None.
  261. *
  262. * @return None.
  263. **************************************************************************************/
  264. int hal_i2c_addr_update(void* pi2c, uint8_t addr){
  265. AP_I2C_TypeDef * pi2cdev = (AP_I2C_TypeDef *)pi2c;
  266. if(pi2cdev != AP_I2C0 && pi2cdev != AP_I2C1){
  267. return PPlus_ERR_INVALID_PARAM;
  268. }
  269. pi2cdev->IC_ENABLE=0;
  270. pi2cdev->IC_TAR = addr;
  271. pi2cdev->IC_ENABLE=0;
  272. return PPlus_SUCCESS;
  273. }
  274. int _hal_i2c_read_s(void* pi2c, uint8_t slave_addr, uint8_t reg, uint8_t* data, uint8_t size)
  275. {
  276. I2C_INIT_TOUT(to);
  277. AP_I2C_TypeDef * pi2cdev = (AP_I2C_TypeDef *)pi2c;
  278. if(pi2cdev != AP_I2C0 && pi2cdev != AP_I2C1){
  279. return PPlus_ERR_INVALID_PARAM;
  280. }
  281. hal_i2c_addr_update(pi2c, slave_addr);
  282. HAL_ENTER_CRITICAL_SECTION();
  283. hal_i2c_tx_start(pi2c);
  284. _hal_i2c_send_byte(pi2c, reg);
  285. hal_master_send_read_cmd(pi2c, size);
  286. HAL_EXIT_CRITICAL_SECTION();
  287. while(1){
  288. if(I2C_RX_FIFO_NOT_EMPTY(pi2cdev)){
  289. *data = (pi2cdev->IC_DATA_CMD&0xff);
  290. data++;
  291. size --;
  292. if(size == 0)
  293. break;
  294. }
  295. I2C_CHECK_TOUT(to, I2C_OP_TIMEOUT*size, "I2C RD TO\n");
  296. }
  297. return PPlus_SUCCESS;
  298. }
  299. int hal_i2c_read(
  300. void* pi2c,
  301. uint8_t slave_addr,
  302. uint8_t reg,
  303. uint8_t* data,
  304. uint8_t size)
  305. {
  306. uint8_t cnt;
  307. int ret = PPlus_SUCCESS;
  308. AP_I2C_TypeDef * pi2cdev = (AP_I2C_TypeDef *)pi2c;
  309. if(pi2cdev != AP_I2C0 && pi2cdev != AP_I2C1){
  310. return PPlus_ERR_INVALID_PARAM;
  311. }
  312. while(size){
  313. cnt = (size >7) ? 7 : size;
  314. size -= cnt;
  315. ret = _hal_i2c_read_s(pi2c, slave_addr, reg, data , cnt);
  316. if(ret != PPlus_SUCCESS)
  317. break;
  318. data += cnt;
  319. }
  320. return ret;
  321. }