Fix CH582 iic and uart bug

Update pika_config.c for 1.8.0
This commit is contained in:
dreamcmi 2022-05-23 11:34:14 +08:00
parent 696b0600c2
commit bfc01e09fd
No known key found for this signature in database
GPG Key ID: 1AEAA4F80C165CF5
4 changed files with 63 additions and 33 deletions

View File

@ -73,10 +73,7 @@ class UART(PikaStdDevice.UART):
class IIC(PikaStdDevice.IIC):
SCL = GPIO()
SDA = GPIO()
# override
def platformEnable():
pass

View File

@ -1,6 +1,11 @@
#include "CH58x_common.h"
#include "CH582_IIC.h"
typedef struct pika_IIC_info_t
{
uint8_t deviceAddr;
uint8_t readBuff[32];
} pika_IIC_info;
void CH582_IIC_platformDisable(PikaObj *self)
{
@ -10,69 +15,98 @@ void CH582_IIC_platformDisable(PikaObj *self)
void CH582_IIC_platformEnable(PikaObj *self)
{
uint8_t deviceAddr = obj_getInt(self, "deviceAddr");
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);
GPIOB_ModeCfg(GPIO_Pin_13 | GPIO_Pin_12, GPIO_ModeIN_PU);
I2C_Init(I2C_Mode_I2C, 100000, I2C_DutyCycle_16_9, I2C_Ack_Enable, I2C_AckAddr_7bit, deviceAddr);
I2C_Init(I2C_Mode_I2C, 100000, I2C_DutyCycle_16_9, I2C_Ack_Enable, I2C_AckAddr_7bit, 0x01);
I2C_Cmd(ENABLE);
}
void CH582_IIC_platformRead(PikaObj *self)
{
uint8_t length = obj_getInt(self, "length");
uint8_t readAddr = obj_getInt(self, "readAddr");
uint8_t len = obj_getInt(self, "length");
uint8_t reg = obj_getInt(self, "readAddr");
pika_IIC_info *iic = obj_getPtr(self, "iic");
uint8_t i = 0;
uint8_t *data = {0};
I2C_AcknowledgeConfig(ENABLE);
I2C_GenerateSTART(ENABLE);
while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_Send7bitAddress(readAddr, I2C_Direction_Receiver);
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);
while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
;
while (i < length)
if (len == 1)
{
if (I2C_GetFlagStatus(I2C_FLAG_RXNE) != RESET)
I2C_AcknowledgeConfig(DISABLE);
iic->readBuff[i] = I2C_ReceiveData();
}
else
{
while (i < len)
{
if (i == (length - 2))
if (I2C_GetFlagStatus(I2C_FLAG_RXNE) != RESET)
{
I2C_AcknowledgeConfig(DISABLE);
data[i] = I2C_ReceiveData(); //读数据,发送nACK
if (i == (len - 2))
{
I2C_AcknowledgeConfig(DISABLE);
iic->readBuff[i] = I2C_ReceiveData();
}
else
{
iic->readBuff[i] = I2C_ReceiveData();
}
i++;
}
else
{
data[i] = I2C_ReceiveData(); //读数据,发送ACK
}
i++;
}
}
I2C_GenerateSTOP(ENABLE);
obj_setStr(self, "readData", (char *)data);
obj_setBytes(self, "readData", iic->readBuff, len);
}
void CH582_IIC_platformWrite(PikaObj *self)
{
char *writeData = obj_getStr(self, "writeData");
uint8_t writeAddr = obj_getInt(self, "writeAddr");
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");
uint8_t i = 0;
uint8_t length = strGetSize(writeData);
while (I2C_GetFlagStatus(I2C_FLAG_BUSY) != RESET)
;
I2C_AcknowledgeConfig(ENABLE);
I2C_GenerateSTART(ENABLE);
while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_Send7bitAddress(writeAddr, I2C_Direction_Transmitter);
I2C_Send7bitAddress(((iic->deviceAddr << 1) | 0), I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
;
while (i < length)
while (I2C_GetFlagStatus(I2C_FLAG_TXE) == RESET)
;
I2C_SendData(reg);
while (i < len)
{
if (I2C_GetFlagStatus(I2C_FLAG_TXE) != RESET)
{
I2C_SendData((uint8_t)writeData[i]);
I2C_SendData(writeData[i]);
i++;
}
}
while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED))
while (I2C_GetFlagStatus(I2C_FLAG_TXE) == RESET)
;
I2C_GenerateSTOP(ENABLE);
}

View File

@ -2,7 +2,6 @@
#include "CH582_UART.h"
#include "ch582_utils.h"
void UART0_DeInit(void)
{
R8_UART0_FCR = 0x0;
@ -108,14 +107,14 @@ void CH582_UART_platformRead(PikaObj *self)
default:
break;
}
obj_setStr(self, "readData", readData);
obj_setBytes(self, "readData", readData, len);
}
void CH582_UART_platformWrite(PikaObj *self)
{
int id = obj_getInt(self, "id");
char *data = obj_getStr(self, "data");
char *data = NULL;
size_t len = obj_loadBytes(self, "data", data);
switch (id)
{
case 0:

View File

@ -19,7 +19,7 @@ int __platform_sprintf(char *buff, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
int res = vsnprintf_(buff, PIKA_CONFIG_SPRINTF_BUFF_SIZE, fmt, args);
int res = vsnprintf_(buff, PIKA_SPRINTF_BUFF_SIZE, fmt, args);
va_end(args);
return res;
}