mirror of
https://gitee.com/Vancouver2017/luban-lite-t3e-pro.git
synced 2025-12-16 11:28:54 +00:00
V1.0.5
This commit is contained in:
@@ -1,165 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, ArtInChip Technology Co., Ltd
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <console.h>
|
||||
#include <aic_common.h>
|
||||
#include <aic_errno.h>
|
||||
#include <aic_core.h>
|
||||
#include <unistd.h>
|
||||
#include <hal_i2c.h>
|
||||
#include <aic_soc.h>
|
||||
|
||||
#define I2C_HELP \
|
||||
"i2c write <I2C BUS ID> <slave addr> <reg_addr> data\n" \
|
||||
"i2c read <I2C BUS ID> <slave addr> <reg_addr>\n" \
|
||||
"This program is used to test master read&write eeprom function of i2c\n " \
|
||||
"Example:\n" \
|
||||
"write one byte: i2c write 0 0x50 0x0000 0x11\n" \
|
||||
"read one byte: i2c read 0 0x50 0x0000 \n" \
|
||||
"tips: slave addr is 16bit such as : 0x1234\n" \
|
||||
" if use se_i2c please input bus_id write 5\n"
|
||||
|
||||
#define I2C_BASE_OFFSET 0x1000
|
||||
#define SE_BUS_ID (CLK_SE_I2C - CLK_I2C0)
|
||||
|
||||
#ifdef SE_I2C_BASE
|
||||
#define I2C_REG_BASE(x) (SE_I2C_BASE)
|
||||
#else
|
||||
#define I2C_REG_BASE(x) (I2C0_BASE + (x * I2C_BASE_OFFSET))
|
||||
#endif
|
||||
|
||||
void i2c_usage(void)
|
||||
{
|
||||
puts(I2C_HELP);
|
||||
}
|
||||
|
||||
struct aic_i2c_msg *i2c_msg;
|
||||
|
||||
void read_eeprom(unsigned long reg_base, struct aic_i2c_msg *msg)
|
||||
{
|
||||
uint8_t *receive_data = msg->buf;
|
||||
unsigned long reg_data;
|
||||
aic_i2c_module_disable(reg_base);
|
||||
aic_i2c_target_addr(reg_base, msg->addr);
|
||||
aic_i2c_module_enable(reg_base);
|
||||
|
||||
aic_i2c_transmit_data(reg_base, receive_data[0]); //write high_address
|
||||
aic_i2c_transmit_data(reg_base, receive_data[1]); //write low_address
|
||||
aic_i2c_transmit_data(reg_base, I2C_INTR_ACTIVITY | I2C_INTR_STOP_DET); //read signal
|
||||
|
||||
while ((aic_i2c_get_raw_interrupt_state(reg_base) & I2C_INTR_STOP_DET) == 0) {;};
|
||||
|
||||
reg_data = aic_i2c_get_receive_data(reg_base);
|
||||
printf("read_data = 0x%lx\n", reg_data);
|
||||
}
|
||||
|
||||
void write_eeprom(unsigned long reg_base, struct aic_i2c_msg *msg)
|
||||
{
|
||||
uint8_t *send_data = msg->buf;
|
||||
aic_i2c_module_disable(reg_base);
|
||||
aic_i2c_target_addr(reg_base, msg->addr);
|
||||
aic_i2c_module_enable(reg_base);
|
||||
|
||||
aic_i2c_transmit_data(reg_base, send_data[0]); //write high_address
|
||||
aic_i2c_transmit_data(reg_base, send_data[1]); //write low_address
|
||||
aic_i2c_transmit_data(reg_base, send_data[2] | I2C_INTR_STOP_DET); //write data
|
||||
|
||||
while ((aic_i2c_get_raw_interrupt_state(reg_base) & I2C_INTR_STOP_DET) == 0) {;};
|
||||
aic_i2c_module_disable(reg_base);
|
||||
}
|
||||
|
||||
static int test_i2c_example(int argc, char *argv[])
|
||||
{
|
||||
int bus_id = 0;
|
||||
struct aic_i2c_msg *msgs = NULL;
|
||||
|
||||
msgs = aicos_malloc(MEM_CMA, sizeof(struct aic_i2c_msg));
|
||||
if (!msgs) {
|
||||
hal_log_err("msgs malloc fail\n");
|
||||
goto __exit;
|
||||
}
|
||||
memset(msgs, 0, sizeof(struct aic_i2c_msg));
|
||||
|
||||
uint16_t buf_size = 10;
|
||||
msgs->buf = aicos_malloc(MEM_CMA, buf_size);
|
||||
if (!msgs->buf) {
|
||||
hal_log_err("buf malloc fail\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (argc < 5) {
|
||||
i2c_usage();
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
bus_id = atoi(argv[2]);
|
||||
if (bus_id < 0 || bus_id > 5) {
|
||||
hal_log_err("bus id param error,pleaseinput range 0-5\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
uint16_t slave_addr = strtol((argv[3]), NULL, 16);
|
||||
msgs->addr = slave_addr;
|
||||
|
||||
/* get reg high addr&low addr */
|
||||
uint16_t data = strtol((argv[4]), NULL, 16);
|
||||
uint8_t high_addr = data >> 8;
|
||||
uint8_t low_addr = data & 0xff;
|
||||
memcpy(&msgs->buf[0], &high_addr, 1);
|
||||
memcpy(&msgs->buf[1], &low_addr, 1);
|
||||
|
||||
if (argc == 6) {
|
||||
uint16_t write_data = strtol(argv[5], NULL, 16);
|
||||
memcpy(&msgs->buf[2], &write_data, 1);
|
||||
printf("write_data = 0x%x\n", write_data);
|
||||
}
|
||||
|
||||
int ret = -1;
|
||||
#ifdef SE_I2C_BASE
|
||||
ret = aic_i2c_init(SE_BUS_ID);
|
||||
#else
|
||||
ret = aic_i2c_init(bus_id);
|
||||
#endif
|
||||
if (ret) {
|
||||
hal_log_err("init error\n");
|
||||
goto __exit;
|
||||
}
|
||||
ret = aic_i2c_set_master_slave_mode(I2C_REG_BASE(bus_id), true);
|
||||
if (ret) {
|
||||
hal_log_err("mode set error\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
hal_i2c_set_hold(I2C_REG_BASE(bus_id), 10);
|
||||
aic_i2c_master_10bit_addr(I2C_REG_BASE(bus_id), false);
|
||||
aic_i2c_slave_10bit_addr(I2C_REG_BASE(bus_id), false);
|
||||
aic_i2c_master_enable_transmit_irq(I2C_REG_BASE(bus_id));
|
||||
aic_i2c_master_enable_receive_irq(I2C_REG_BASE(bus_id));
|
||||
aic_i2c_speed_mode_select(I2C_REG_BASE(bus_id), I2C_DEFALT_CLOCK, true);
|
||||
aic_i2c_module_enable(I2C_REG_BASE(bus_id));
|
||||
|
||||
if (!strcmp(argv[1], "write")) {
|
||||
write_eeprom(I2C_REG_BASE(bus_id), msgs);
|
||||
} else if (!strcmp(argv[1], "read")) {
|
||||
read_eeprom(I2C_REG_BASE(bus_id), msgs);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (msgs != NULL) {
|
||||
if (msgs->buf != NULL) {
|
||||
aicos_free(MEM_CMA, msgs->buf);
|
||||
}
|
||||
aicos_free(MEM_CMA, msgs);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
CONSOLE_CMD(i2c, test_i2c_example, "i2c-tools");
|
||||
|
||||
160
bsp/examples_bare/test-i2c/test_i2c.c
Normal file
160
bsp/examples_bare/test-i2c/test_i2c.c
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, ArtInChip Technology Co., Ltd
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <console.h>
|
||||
#include <aic_core.h>
|
||||
#include <unistd.h>
|
||||
#include <hal_i2c.h>
|
||||
|
||||
#define BUF_LEN 128
|
||||
#define I2C_BASE_OFFSET 0x1000
|
||||
#define SE_BUS_ID (CLK_SE_I2C - CLK_I2C0)
|
||||
|
||||
#ifdef SE_I2C_BASE
|
||||
#define I2C_REG_BASE(x) (SE_I2C_BASE)
|
||||
#else
|
||||
#define I2C_REG_BASE(x) (I2C0_BASE + (x * I2C_BASE_OFFSET))
|
||||
#endif
|
||||
|
||||
aic_i2c_ctrl g_i2c_ctrl;
|
||||
|
||||
static void i2c_usage(void)
|
||||
{
|
||||
printf("i2c write [I2C BUS ID] [slave addr] [-8 | -16] [reg] [send data]\n");
|
||||
printf("i2c read [I2C BUS ID] [slave addr] [-8 | -16] [reg]\n");
|
||||
printf("Example:\n");
|
||||
printf("16bit reg width:\n");
|
||||
printf(" write one byte: i2c write 0 0x50 -16 0x1234 0x11\n");
|
||||
printf(" read one byte: i2c read 0 0x50 -16 0x1234\n");
|
||||
printf("8bit reg width:\n");
|
||||
printf(" write one byte: i2c write 0 0x50 -8 0x00 0x11\n");
|
||||
printf(" read one byte: i2c read 0 0x50 -8 0x00\n");
|
||||
printf("tips:\n");
|
||||
printf(" if use se_i2c please input bus_id write 6\n");
|
||||
}
|
||||
|
||||
static int32_t read_func(aic_i2c_ctrl *i2c_dev)
|
||||
{
|
||||
uint32_t bytes_cnt = 0;
|
||||
|
||||
bytes_cnt = hal_i2c_master_send_msg(i2c_dev, i2c_dev->msg, 1);
|
||||
if (bytes_cnt != i2c_dev->msg->len) {
|
||||
return bytes_cnt;
|
||||
}
|
||||
i2c_dev->msg->len = 1;
|
||||
bytes_cnt = hal_i2c_master_receive_msg(i2c_dev, i2c_dev->msg, 1);
|
||||
return bytes_cnt;
|
||||
}
|
||||
|
||||
static int32_t write_func(aic_i2c_ctrl *i2c_dev)
|
||||
{
|
||||
return hal_i2c_master_send_msg(i2c_dev, i2c_dev->msg, 1);
|
||||
}
|
||||
|
||||
static int test_i2c_example(int argc, char *argv[])
|
||||
{
|
||||
int bus_id = 0;
|
||||
uint16_t slave_addr;
|
||||
uint16_t data;
|
||||
uint8_t high_reg, low_reg, reg, write_data = 0;
|
||||
struct aic_i2c_msg *msgs = NULL;
|
||||
|
||||
msgs = aicos_malloc(MEM_CMA, sizeof(struct aic_i2c_msg));
|
||||
if (!msgs) {
|
||||
hal_log_err("msgs malloc fail\n");
|
||||
goto __exit;
|
||||
}
|
||||
memset(msgs, 0, sizeof(struct aic_i2c_msg));
|
||||
|
||||
msgs->buf = aicos_malloc(MEM_CMA, BUF_LEN);
|
||||
if (!msgs->buf) {
|
||||
hal_log_err("buf malloc fail\n");
|
||||
goto __exit;
|
||||
}
|
||||
memset(msgs->buf, 0, BUF_LEN);
|
||||
|
||||
if (argc < 6) {
|
||||
i2c_usage();
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
bus_id = atoi(argv[2]);
|
||||
if (bus_id < 0 || bus_id > 6) {
|
||||
hal_log_err("bus id param error,please input range 0-6\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
slave_addr = strtol(argv[3], NULL, 16);
|
||||
msgs->addr = slave_addr;
|
||||
|
||||
if (strtol(argv[4], NULL, 10) == -8) {
|
||||
reg = strtol((argv[5]), NULL, 16);
|
||||
memcpy(&msgs->buf[0], ®, 1);
|
||||
msgs->len = 1;
|
||||
|
||||
if (argc == 7) {
|
||||
write_data = strtol(argv[6], NULL, 16);
|
||||
memcpy(&msgs->buf[1], &write_data, 1);
|
||||
msgs->len = 2;
|
||||
}
|
||||
} else {
|
||||
data = strtol((argv[5]), NULL, 16);
|
||||
high_reg = data >> 8;
|
||||
low_reg = data & 0xff;
|
||||
memcpy(&msgs->buf[0], &high_reg, 1);
|
||||
memcpy(&msgs->buf[1], &low_reg, 1);
|
||||
msgs->len = 2;
|
||||
|
||||
if (argc == 7) {
|
||||
write_data = strtol(argv[6], NULL, 16);
|
||||
memcpy(&msgs->buf[2], &write_data, 1);
|
||||
msgs->len = 3;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef SE_I2C_BASE
|
||||
g_i2c_ctrl.index = SE_BUS_ID;
|
||||
#else
|
||||
g_i2c_ctrl.index = bus_id;
|
||||
#endif
|
||||
g_i2c_ctrl.reg_base = I2C_REG_BASE(bus_id);
|
||||
g_i2c_ctrl.msg = msgs;
|
||||
g_i2c_ctrl.addr_bit = I2C_7BIT_ADDR;
|
||||
g_i2c_ctrl.speed_mode = I2C_400K_SPEED;
|
||||
g_i2c_ctrl.bus_mode = I2C_MASTER_MODE;
|
||||
|
||||
hal_i2c_init(&g_i2c_ctrl);
|
||||
|
||||
if (!strcmp(argv[1], "write")) {
|
||||
if (write_func(&g_i2c_ctrl) != msgs->len) {
|
||||
printf("i2c write error\n");
|
||||
goto __exit;
|
||||
}
|
||||
printf("write_data: %#x\n", write_data);
|
||||
} else if (!strcmp(argv[1], "read")) {
|
||||
if (read_func(&g_i2c_ctrl) != 1) {
|
||||
printf("i2c read error\n");
|
||||
goto __exit;
|
||||
}
|
||||
printf("read_data: %#x\n", msgs->buf[0]);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (msgs != NULL) {
|
||||
if (msgs->buf != NULL) {
|
||||
aicos_free(MEM_CMA, msgs->buf);
|
||||
}
|
||||
aicos_free(MEM_CMA, msgs);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
CONSOLE_CMD(i2c, test_i2c_example, "i2c-tools");
|
||||
|
||||
Reference in New Issue
Block a user