2022-03-26 20:46:22 +08:00
|
|
|
#include "CH58x_common.h"
|
|
|
|
#include "CH582_IIC.h"
|
|
|
|
|
2022-05-23 11:34:14 +08:00
|
|
|
typedef struct pika_IIC_info_t
|
|
|
|
{
|
|
|
|
uint8_t deviceAddr;
|
|
|
|
uint8_t readBuff[32];
|
|
|
|
} pika_IIC_info;
|
2022-03-26 20:46:22 +08:00
|
|
|
|
|
|
|
void CH582_IIC_platformDisable(PikaObj *self)
|
|
|
|
{
|
|
|
|
I2C_Cmd(DISABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CH582_IIC_platformEnable(PikaObj *self)
|
|
|
|
{
|
|
|
|
uint8_t deviceAddr = obj_getInt(self, "deviceAddr");
|
2022-05-23 11:34:14 +08:00
|
|
|
pika_IIC_info *iic = obj_getPtr(self, "iic");
|
|
|
|
if (NULL == iic)
|
|
|
|
{
|
|
|
|
iic = pikaMalloc(sizeof(pika_IIC_info));
|
|
|
|
obj_setPtr(self, "iic", iic);
|
|
|
|
}
|
|
|
|
iic->deviceAddr = deviceAddr;
|
|
|
|
|
|
|
|
I2C_SoftwareResetCmd(ENABLE);
|
|
|
|
I2C_SoftwareResetCmd(DISABLE);
|
2022-03-26 20:46:22 +08:00
|
|
|
GPIOB_ModeCfg(GPIO_Pin_13 | GPIO_Pin_12, GPIO_ModeIN_PU);
|
2022-05-23 11:34:14 +08:00
|
|
|
I2C_Init(I2C_Mode_I2C, 100000, I2C_DutyCycle_16_9, I2C_Ack_Enable, I2C_AckAddr_7bit, 0x01);
|
2022-03-26 20:46:22 +08:00
|
|
|
I2C_Cmd(ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CH582_IIC_platformRead(PikaObj *self)
|
|
|
|
{
|
2022-05-23 11:34:14 +08:00
|
|
|
uint8_t len = obj_getInt(self, "length");
|
|
|
|
uint8_t reg = obj_getInt(self, "readAddr");
|
|
|
|
pika_IIC_info *iic = obj_getPtr(self, "iic");
|
2022-03-26 20:46:22 +08:00
|
|
|
uint8_t i = 0;
|
|
|
|
|
|
|
|
I2C_AcknowledgeConfig(ENABLE);
|
|
|
|
I2C_GenerateSTART(ENABLE);
|
|
|
|
while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT))
|
|
|
|
;
|
2022-05-23 11:34:14 +08:00
|
|
|
I2C_Send7bitAddress((iic->deviceAddr << 1) | 0x00, I2C_Direction_Transmitter);
|
|
|
|
while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
|
|
|
|
;
|
|
|
|
I2C_SendData(reg);
|
|
|
|
I2C_GenerateSTART(ENABLE);
|
|
|
|
while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT))
|
|
|
|
;
|
|
|
|
I2C_Send7bitAddress(((iic->deviceAddr << 1) | 0x01), I2C_Direction_Receiver);
|
2022-03-26 20:46:22 +08:00
|
|
|
while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
|
|
|
|
;
|
2022-05-23 11:34:14 +08:00
|
|
|
if (len == 1)
|
2022-03-26 20:46:22 +08:00
|
|
|
{
|
2022-05-23 11:34:14 +08:00
|
|
|
I2C_AcknowledgeConfig(DISABLE);
|
|
|
|
iic->readBuff[i] = I2C_ReceiveData();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (i < len)
|
2022-03-26 20:46:22 +08:00
|
|
|
{
|
2022-05-23 11:34:14 +08:00
|
|
|
if (I2C_GetFlagStatus(I2C_FLAG_RXNE) != RESET)
|
2022-03-26 20:46:22 +08:00
|
|
|
{
|
2022-05-23 11:34:14 +08:00
|
|
|
if (i == (len - 2))
|
|
|
|
{
|
|
|
|
I2C_AcknowledgeConfig(DISABLE);
|
|
|
|
iic->readBuff[i] = I2C_ReceiveData();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iic->readBuff[i] = I2C_ReceiveData();
|
|
|
|
}
|
|
|
|
i++;
|
2022-03-26 20:46:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
I2C_GenerateSTOP(ENABLE);
|
2022-05-23 11:34:14 +08:00
|
|
|
|
|
|
|
obj_setBytes(self, "readData", iic->readBuff, len);
|
2022-03-26 20:46:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CH582_IIC_platformWrite(PikaObj *self)
|
|
|
|
{
|
2022-05-23 11:34:14 +08:00
|
|
|
char *writeData = NULL;
|
|
|
|
size_t len = obj_loadBytes(self, "writeData", writeData);
|
|
|
|
uint8_t reg = obj_getInt(self, "writeAddr");
|
|
|
|
pika_IIC_info *iic = obj_getPtr(self, "iic");
|
2022-03-26 20:46:22 +08:00
|
|
|
uint8_t i = 0;
|
|
|
|
|
2022-05-23 11:34:14 +08:00
|
|
|
I2C_AcknowledgeConfig(ENABLE);
|
2022-03-26 20:46:22 +08:00
|
|
|
I2C_GenerateSTART(ENABLE);
|
|
|
|
while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT))
|
|
|
|
;
|
2022-05-23 11:34:14 +08:00
|
|
|
I2C_Send7bitAddress(((iic->deviceAddr << 1) | 0), I2C_Direction_Transmitter);
|
2022-03-26 20:46:22 +08:00
|
|
|
while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
|
|
|
|
;
|
2022-05-23 11:34:14 +08:00
|
|
|
while (I2C_GetFlagStatus(I2C_FLAG_TXE) == RESET)
|
|
|
|
;
|
|
|
|
I2C_SendData(reg);
|
|
|
|
while (i < len)
|
2022-03-26 20:46:22 +08:00
|
|
|
{
|
|
|
|
if (I2C_GetFlagStatus(I2C_FLAG_TXE) != RESET)
|
|
|
|
{
|
2022-05-23 11:34:14 +08:00
|
|
|
I2C_SendData(writeData[i]);
|
2022-03-26 20:46:22 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 11:34:14 +08:00
|
|
|
while (I2C_GetFlagStatus(I2C_FLAG_TXE) == RESET)
|
2022-03-26 20:46:22 +08:00
|
|
|
;
|
|
|
|
I2C_GenerateSTOP(ENABLE);
|
|
|
|
}
|