123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #include "ap_cp.h"
- #include "gpio.h"
- #include "hal_mcu.h"
- #include "clock.h"
- #include "i2c_s.h"
- #include "i2c.h"
- #include "error.h"
- #include "log.h"
- typedef struct{
- uint8_t id;
- uint8_t mode;
- uint8_t saddr;
- GPIO_Pin_e cs;
- GPIO_Pin_e sda;
- GPIO_Pin_e scl;
- AP_I2C_TypeDef* dev;
- i2cs_hdl_t evt_handler;
- uint8_t rxoffset;
- uint8_t rxbuf[I2CS_RX_MAX_SIZE];
- uint8_t txoffset;
- uint8_t txbuf[I2CS_TX_MAX_SIZE];
- }i2cs_ctx_t;
- static uint8_t s_i2cs_state = I2CSST_IDLE;
- static i2cs_ctx_t s_i2cs_ctx;
- static void i2cs_irq_rx_handler(AP_I2C_TypeDef* pdev)
- {
- uint32_t val;
- while(1){
- if((pdev->IC_STATUS & BV(3)) == 0)
- break;
- val = pdev->IC_DATA_CMD;
- LOG("Rx %x\n",val);
- }
- }
- static uint32_t tx_dummy = 0;
- static void i2cs_irq_tx_handler(AP_I2C_TypeDef* pdev)
- {
- pdev->IC_DATA_CMD = tx_dummy &0xff;
- tx_dummy ++;
- pdev->IC_DATA_CMD = tx_dummy &0xff;
- tx_dummy ++;
-
- }
- static void i2cs_irq_handler(AP_I2C_TypeDef* pdev)
- {
- uint32_t int_status = pdev->IC_INTR_STAT;
- uint32_t clr = pdev->IC_CLR_INTR;
-
- if(int_status & I2C_MASK_START_DET)
- {
-
- }
- if(int_status & I2C_MASK_RX_FULL)
- {
- i2cs_irq_rx_handler(pdev);
- }
- if(int_status & I2C_MASK_RD_REQ)
- {
- i2cs_irq_tx_handler(pdev);
- }
- }
- void __attribute__((used)) hal_I2C0_IRQHandler(void)
- {
- i2cs_ctx_t* pctx = &s_i2cs_ctx;
- if(pctx->id == I2CS_0)
- i2cs_irq_handler(AP_I2C0);
- }
- void __attribute__((used)) hal_I2C1_IRQHandler(void)
- {
- i2cs_ctx_t* pctx = &s_i2cs_ctx;
- if(pctx->id == I2CS_1)
- i2cs_irq_handler(AP_I2C1);
- }
- int i2cs_init(
- i2cs_channel_t ch_id,
- i2cs_mode_t mode,
- uint8_t saddr,
- GPIO_Pin_e cs,
- GPIO_Pin_e sda,
- GPIO_Pin_e scl,
- i2cs_hdl_t evt_handler)
- {
- i2cs_ctx_t* pctx = &s_i2cs_ctx;
- Fmux_Type_e fmux;
- MODULE_e module;
- AP_I2C_TypeDef* pdev = NULL;
- int irqid;
- if(pctx->id){
- return PPlus_ERR_IO_CONFILCT;
- }
-
-
- irqid = (ch_id == I2CS_0) ? I2C0_IRQ : I2C1_IRQ;
- module = (ch_id == I2CS_0) ? MOD_I2C0 : MOD_I2C1;
- clk_gate_enable(module);
-
- pdev = (ch_id == I2CS_0) ? AP_I2C0 : AP_I2C1;
- pctx->id = ch_id;
- pctx->dev = pdev;
- pctx->saddr = saddr;
- pctx->cs = cs;
- pctx->scl = scl;
- pctx->sda = sda;
- pctx->evt_handler = evt_handler;
-
- fmux = (ch_id == I2CS_0) ? IIC0_SCL : IIC1_SCL;
- hal_gpio_fmux_set(scl, fmux);
-
- fmux = (ch_id == I2CS_0) ? IIC0_SDA : IIC1_SDA;
- hal_gpio_fmux_set(sda, fmux);
-
- hal_gpio_pull_set(scl , STRONG_PULL_UP);
- hal_gpio_pull_set(sda , STRONG_PULL_UP);
- pdev->IC_ENABLE = 0;
- pdev->IC_CON = (SPEED_FAST) << 1;
- pdev->IC_SAR = saddr;
- pdev->IC_RX_TL = 1;
- pdev->IC_TX_TL = 1;
- pdev->IC_INTR_MASK = 0xfef;
- pdev->IC_ENABLE = 1;
- NVIC_EnableIRQ((IRQn_Type)irqid);
-
- NVIC_SetPriority(irqid, IRQ_PRIO_HAL);
-
- return PPlus_SUCCESS;
- }
- int i2cs_deinit(void)
- {
- i2cs_ctx_t* pctx = &s_i2cs_ctx;
- if(pctx->id == 0)
- return PPlus_ERR_IO_FAIL;
-
-
- return PPlus_SUCCESS;
- }
|