forked from xuos/xiuos
Fix edu-arm32 i2c.
This commit is contained in:
parent
a51ef1c41f
commit
379cd567a2
|
@ -24,18 +24,16 @@
|
||||||
|
|
||||||
#define I2C_SLAVE_ADDRESS 0x0012U
|
#define I2C_SLAVE_ADDRESS 0x0012U
|
||||||
|
|
||||||
void TestI2C(void)
|
int open_iic(void)
|
||||||
{
|
{
|
||||||
// config IIC pin(SCL:34.SDA:35) in menuconfig
|
|
||||||
int iic_fd = PrivOpen(I2C_DEV_DRIVER, O_RDWR);
|
int iic_fd = PrivOpen(I2C_DEV_DRIVER, O_RDWR);
|
||||||
if (iic_fd < 0)
|
if (iic_fd < 0)
|
||||||
{
|
{
|
||||||
printf("open iic_fd fd error:%d\n", iic_fd);
|
printf("[TestI2C] Open iic_fd fd error: %d\n", iic_fd);
|
||||||
return;
|
return -ERROR;
|
||||||
}
|
}
|
||||||
printf("IIC open successful!\n");
|
printf("[TestI2C] IIC open successful!\n");
|
||||||
|
|
||||||
// init iic
|
|
||||||
uint16 iic_address = I2C_SLAVE_ADDRESS;
|
uint16 iic_address = I2C_SLAVE_ADDRESS;
|
||||||
|
|
||||||
struct PrivIoctlCfg ioctl_cfg;
|
struct PrivIoctlCfg ioctl_cfg;
|
||||||
|
@ -44,28 +42,55 @@ void TestI2C(void)
|
||||||
|
|
||||||
if (0 != PrivIoctl(iic_fd, OPE_INT, &ioctl_cfg))
|
if (0 != PrivIoctl(iic_fd, OPE_INT, &ioctl_cfg))
|
||||||
{
|
{
|
||||||
printf("ioctl iic fd error %d\n", iic_fd);
|
printf("[TestI2C] Ioctl iic fd error %d\n", iic_fd);
|
||||||
PrivClose(iic_fd);
|
PrivClose(iic_fd);
|
||||||
return;
|
return -ERROR;
|
||||||
}
|
}
|
||||||
printf("IIC configure successful!\n");
|
printf("IIC configure successful!\n");
|
||||||
|
|
||||||
// I2C read and write
|
return iic_fd;
|
||||||
char tmp_buff[100];
|
}
|
||||||
while (1)
|
|
||||||
{
|
static const int nr_transmit = 15;
|
||||||
PrivTaskDelay(1000);
|
|
||||||
PrivWrite(iic_fd, "Hello World!\n", sizeof("Hello World!\n"));
|
void TestMasterI2c(void)
|
||||||
printf("msg send:%s\n", "Hello World!\n");
|
{
|
||||||
PrivTaskDelay(1000);
|
char recv_buff[13] = { 0 };
|
||||||
memset(tmp_buff, 0, sizeof(tmp_buff));
|
|
||||||
PrivRead(iic_fd, tmp_buff, sizeof(tmp_buff));
|
int iic_fd = open_iic();
|
||||||
printf("msg recv:%s\n", tmp_buff);
|
if (iic_fd < 0) {
|
||||||
|
printf("[%s] Error open iic\n", __func__);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int transmit_cnt = 0; transmit_cnt < nr_transmit; transmit_cnt++) {
|
||||||
|
// wait if you like.
|
||||||
|
PrivTaskDelay(500);
|
||||||
|
memset(recv_buff, 0, sizeof(recv_buff));
|
||||||
|
PrivRead(iic_fd, recv_buff, sizeof(recv_buff));
|
||||||
|
printf("[%s] Msg recv: %s\n", __func__, recv_buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
PrivClose(iic_fd);
|
PrivClose(iic_fd);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PRIV_SHELL_CMD_FUNCTION(TestI2C, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
void TestSlaveI2c(void)
|
||||||
|
{
|
||||||
|
char send_buff[] = "Hello, World";
|
||||||
|
|
||||||
|
int iic_fd = open_iic();
|
||||||
|
|
||||||
|
for (int transmit_cnt = 0; transmit_cnt < nr_transmit; transmit_cnt++) {
|
||||||
|
// wait if you like.
|
||||||
|
PrivTaskDelay(500);
|
||||||
|
PrivWrite(iic_fd, send_buff, sizeof(send_buff));
|
||||||
|
printf("[%s] Msg send: %s\n", __func__, send_buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
PrivClose(iic_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
PRIV_SHELL_CMD_FUNCTION(TestMasterI2c, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||||
|
PRIV_SHELL_CMD_FUNCTION(TestSlaveI2c, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -117,6 +117,9 @@ static uint32 I2cDrvConfigure(void *drv, struct BusConfigureInfo *configure_info
|
||||||
|
|
||||||
static uint32 I2cMasterWriteData(struct I2cHardwareDevice *i2c_dev, struct I2cDataStandard *msg)
|
static uint32 I2cMasterWriteData(struct I2cHardwareDevice *i2c_dev, struct I2cDataStandard *msg)
|
||||||
{
|
{
|
||||||
|
if (msg->len == 0) {
|
||||||
|
return EOK;
|
||||||
|
}
|
||||||
uint32 i32Ret;
|
uint32 i32Ret;
|
||||||
|
|
||||||
I2C_Cmd(I2C_UNIT, ENABLE);
|
I2C_Cmd(I2C_UNIT, ENABLE);
|
||||||
|
@ -171,6 +174,9 @@ static uint32 I2cMasterReadData(struct I2cHardwareDevice *i2c_dev, struct I2cDat
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32 I2cSlaveWriteData(struct I2cHardwareDevice *i2c_dev, struct I2cDataStandard *msg) {
|
static uint32 I2cSlaveWriteData(struct I2cHardwareDevice *i2c_dev, struct I2cDataStandard *msg) {
|
||||||
|
if (msg->len == 0) {
|
||||||
|
return EOK;
|
||||||
|
}
|
||||||
uint32 i32Ret;
|
uint32 i32Ret;
|
||||||
|
|
||||||
I2C_Cmd(I2C_UNIT, ENABLE);
|
I2C_Cmd(I2C_UNIT, ENABLE);
|
||||||
|
@ -222,7 +228,7 @@ static uint32 I2cSlaveReadData(struct I2cHardwareDevice *i2c_dev, struct I2cData
|
||||||
if (RESET == I2C_GetStatus(I2C_UNIT, I2C_FLAG_TRA)) {
|
if (RESET == I2C_GetStatus(I2C_UNIT, I2C_FLAG_TRA)) {
|
||||||
/* Slave receive data*/
|
/* Slave receive data*/
|
||||||
i32Ret = I2C_ReceiveData(I2C_UNIT, msg->buf, msg->len, I2C_TIMEOUT);
|
i32Ret = I2C_ReceiveData(I2C_UNIT, msg->buf, msg->len, I2C_TIMEOUT);
|
||||||
KPrintf("Slave receive success!\r\n");
|
KPrintf("Slave receive success!\r\n");
|
||||||
|
|
||||||
if ((LL_OK == i32Ret) || (LL_ERR_TIMEOUT == i32Ret)) {
|
if ((LL_OK == i32Ret) || (LL_ERR_TIMEOUT == i32Ret)) {
|
||||||
/* Wait stop condition */
|
/* Wait stop condition */
|
||||||
|
@ -336,7 +342,7 @@ int HwI2cInit(void)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
//#define I2C_TEST
|
// #define I2C_TEST
|
||||||
#ifdef I2C_TEST
|
#ifdef I2C_TEST
|
||||||
|
|
||||||
#define USER_KEY_PORT (GPIO_PORT_I)
|
#define USER_KEY_PORT (GPIO_PORT_I)
|
||||||
|
|
Loading…
Reference in New Issue