i2c_s.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include "ap_cp.h"
  29. #include "gpio.h"
  30. #include "hal_mcu.h"
  31. #include "clock.h"
  32. #include "i2c_s.h"
  33. #include "i2c.h"
  34. #include "error.h"
  35. #include "log.h"
  36. typedef struct{
  37. uint8_t id; //0: uninit, 1: i2c0, 2:i2c1
  38. uint8_t mode; //(1)I2CS_MODE_REG_8BIT, (2)I2CS_MODE_REG_16BIT,(3)I2CS_MODE_RAW
  39. uint8_t saddr;
  40. GPIO_Pin_e cs; //cs pin, used to wakeup or release
  41. GPIO_Pin_e sda;
  42. GPIO_Pin_e scl;
  43. AP_I2C_TypeDef* dev;
  44. i2cs_hdl_t evt_handler;
  45. uint8_t rxoffset;
  46. uint8_t rxbuf[I2CS_RX_MAX_SIZE];
  47. uint8_t txoffset;
  48. uint8_t txbuf[I2CS_TX_MAX_SIZE];
  49. }i2cs_ctx_t;
  50. static uint8_t s_i2cs_state = I2CSST_IDLE;
  51. static i2cs_ctx_t s_i2cs_ctx;
  52. static void i2cs_irq_rx_handler(AP_I2C_TypeDef* pdev)
  53. {
  54. uint32_t val;
  55. while(1){
  56. if((pdev->IC_STATUS & BV(3)) == 0)
  57. break;
  58. val = pdev->IC_DATA_CMD;
  59. LOG("Rx %x\n",val);
  60. }
  61. }
  62. static uint32_t tx_dummy = 0;
  63. static void i2cs_irq_tx_handler(AP_I2C_TypeDef* pdev)
  64. {
  65. pdev->IC_DATA_CMD = tx_dummy &0xff;
  66. tx_dummy ++;
  67. pdev->IC_DATA_CMD = tx_dummy &0xff;
  68. tx_dummy ++;
  69. //LOG("Tx\n");
  70. }
  71. static void i2cs_irq_handler(AP_I2C_TypeDef* pdev)
  72. {
  73. uint32_t int_status = pdev->IC_INTR_STAT;
  74. uint32_t clr = pdev->IC_CLR_INTR;
  75. //LOG("i2cs_irq_handler %x\n",int_status);
  76. if(int_status & I2C_MASK_START_DET)
  77. {
  78. }
  79. if(int_status & I2C_MASK_RX_FULL)
  80. {
  81. i2cs_irq_rx_handler(pdev);
  82. }
  83. if(int_status & I2C_MASK_RD_REQ)
  84. {
  85. i2cs_irq_tx_handler(pdev);
  86. }
  87. }
  88. void __attribute__((used)) hal_I2C0_IRQHandler(void)
  89. {
  90. i2cs_ctx_t* pctx = &s_i2cs_ctx;
  91. if(pctx->id == I2CS_0)
  92. i2cs_irq_handler(AP_I2C0);
  93. }
  94. void __attribute__((used)) hal_I2C1_IRQHandler(void)
  95. {
  96. i2cs_ctx_t* pctx = &s_i2cs_ctx;
  97. if(pctx->id == I2CS_1)
  98. i2cs_irq_handler(AP_I2C1);
  99. }
  100. int i2cs_init(
  101. i2cs_channel_t ch_id,
  102. i2cs_mode_t mode,
  103. uint8_t saddr, //slave address
  104. GPIO_Pin_e cs, //if need not cs, choose GPIO_DUMMY_PIN
  105. GPIO_Pin_e sda,
  106. GPIO_Pin_e scl,
  107. i2cs_hdl_t evt_handler)
  108. {
  109. i2cs_ctx_t* pctx = &s_i2cs_ctx;
  110. Fmux_Type_e fmux;
  111. MODULE_e module;
  112. AP_I2C_TypeDef* pdev = NULL;
  113. int irqid;
  114. if(pctx->id){
  115. return PPlus_ERR_IO_CONFILCT;
  116. }
  117. //parameter validate check
  118. //set device
  119. irqid = (ch_id == I2CS_0) ? I2C0_IRQ : I2C1_IRQ;
  120. module = (ch_id == I2CS_0) ? MOD_I2C0 : MOD_I2C1;
  121. clk_gate_enable(module);
  122. pdev = (ch_id == I2CS_0) ? AP_I2C0 : AP_I2C1;
  123. pctx->id = ch_id;
  124. pctx->dev = pdev;
  125. pctx->saddr = saddr;
  126. pctx->cs = cs;
  127. pctx->scl = scl;
  128. pctx->sda = sda;
  129. pctx->evt_handler = evt_handler;
  130. fmux = (ch_id == I2CS_0) ? IIC0_SCL : IIC1_SCL;
  131. hal_gpio_fmux_set(scl, fmux);
  132. fmux = (ch_id == I2CS_0) ? IIC0_SDA : IIC1_SDA;
  133. hal_gpio_fmux_set(sda, fmux);
  134. hal_gpio_pull_set(scl , STRONG_PULL_UP);
  135. hal_gpio_pull_set(sda , STRONG_PULL_UP);
  136. pdev->IC_ENABLE = 0; //disable
  137. pdev->IC_CON = (SPEED_FAST) << 1;
  138. pdev->IC_SAR = saddr;
  139. pdev->IC_RX_TL = 1;
  140. pdev->IC_TX_TL = 1;
  141. pdev->IC_INTR_MASK = 0xfef;//(I2C_MASK_TX_ABRT | I2C_MASK_RD_REQ | I2C_MASK_RX_FULL | I2C_MASK_RX_DONE);
  142. pdev->IC_ENABLE = 1; //disable
  143. NVIC_EnableIRQ((IRQn_Type)irqid);
  144. NVIC_SetPriority(irqid, IRQ_PRIO_HAL);
  145. return PPlus_SUCCESS;
  146. }
  147. int i2cs_deinit(void)
  148. {
  149. i2cs_ctx_t* pctx = &s_i2cs_ctx;
  150. if(pctx->id == 0)
  151. return PPlus_ERR_IO_FAIL;
  152. //release io
  153. return PPlus_SUCCESS;
  154. }