mirror of
https://gitee.com/Vancouver2017/luban-lite.git
synced 2025-12-17 09:38:55 +00:00
v1.2.1
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2024, ArtInChip Technology Co., Ltd
|
||||
* Copyright (c) 2024-2025, ArtInChip Technology Co., Ltd
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
@@ -188,7 +188,7 @@ static void test_qspi_sendhex(int argc, char **argv)
|
||||
data_len = argc - 5;
|
||||
}
|
||||
data = RT_NULL;
|
||||
printf("data len %ld\n", data_len);
|
||||
printf("data len %lu\n", data_len);
|
||||
if (data_len) {
|
||||
align_len = roundup(data_len, CACHE_LINE_SIZE);
|
||||
data = aicos_malloc_align(0, align_len, CACHE_LINE_SIZE);
|
||||
@@ -260,7 +260,7 @@ static void test_qspi_sendlen(int argc, char **argv)
|
||||
printf("Low memory!\n");
|
||||
return;
|
||||
} else {
|
||||
printf("data len %ld\n", data_len);
|
||||
printf("data len %lu\n", data_len);
|
||||
rt_memset(&msg, 0, sizeof(msg));
|
||||
}
|
||||
msg.instruction.content = cmd;
|
||||
@@ -271,6 +271,13 @@ static void test_qspi_sendlen(int argc, char **argv)
|
||||
msg.qspi_data_lines = pl[2] - '0';
|
||||
msg.parent.send_buf = (void *)data;
|
||||
msg.parent.length = data_len;
|
||||
|
||||
u8 *temp = data;
|
||||
int k;
|
||||
for (k = 0; k < data_len; k++) {
|
||||
*temp = k & 0xff;
|
||||
temp++;
|
||||
}
|
||||
}
|
||||
msg.parent.cs_take = 1;
|
||||
msg.parent.cs_release = 1;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2024, ArtInChip Technology Co., Ltd
|
||||
* Copyright (c) 2024-2025, ArtInChip Technology Co., Ltd
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
@@ -89,6 +89,8 @@ static int test_spi_attach(int argc, char **argv)
|
||||
return result;
|
||||
}
|
||||
|
||||
#define AIC_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB | RT_SPI_SLAVE | RT_SPI_3WIRE)
|
||||
|
||||
static int test_spi_init(int argc, char **argv)
|
||||
{
|
||||
struct rt_spi_configuration spi_cfg;
|
||||
@@ -116,6 +118,7 @@ static int test_spi_init(int argc, char **argv)
|
||||
}
|
||||
rt_memset(&spi_cfg, 0, sizeof(spi_cfg));
|
||||
spi_cfg.mode = atol(argv[2]);
|
||||
spi_cfg.mode &= AIC_SPI_MODE_MASK;
|
||||
spi_cfg.max_hz = atol(argv[3]);
|
||||
ret = rt_spi_configure(g_spi, &spi_cfg);
|
||||
if (ret < 0) {
|
||||
@@ -164,7 +167,7 @@ static void test_spi_send_recv(int argc, char **argv)
|
||||
printf("Low memory!\n");
|
||||
return;
|
||||
} else {
|
||||
printf("send len %ld, recv len %ld\n", send_len, recv_len);
|
||||
printf("send len %lu, recv len %lu\n", send_len, recv_len);
|
||||
}
|
||||
|
||||
rt_spi_take_bus((struct rt_spi_device *)g_spi);
|
||||
@@ -217,7 +220,7 @@ static void test_spi_send(int argc, char **argv)
|
||||
printf("Low memory!\n");
|
||||
return;
|
||||
} else {
|
||||
printf("data len %ld\n", data_len);
|
||||
printf("data len %lu\n", data_len);
|
||||
}
|
||||
|
||||
rt_spi_take_bus((struct rt_spi_device *)g_spi);
|
||||
@@ -266,7 +269,7 @@ static void test_spi_treansfer(int argc, char **argv)
|
||||
printf("Low memory!\n");
|
||||
return;
|
||||
} else {
|
||||
printf("data len %ld\n", data_len);
|
||||
printf("data len %lu\n", data_len);
|
||||
}
|
||||
|
||||
rt_spi_take_bus((struct rt_spi_device *)g_spi);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2023, ArtInChip Technology Co., Ltd
|
||||
* Copyright (c) 2023-2025, ArtInChip Technology Co., Ltd
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
@@ -12,9 +12,12 @@
|
||||
#include <aic_core.h>
|
||||
#include <drv_qspi.h>
|
||||
|
||||
static struct rt_qspi_device *g_qspi;
|
||||
struct rt_spi_device *g_spi;
|
||||
int g_async = 0;
|
||||
static struct rt_qspi_device *g_qspi = NULL;
|
||||
static struct rt_spi_device *g_spi = NULL;
|
||||
static int g_async = 0;
|
||||
static int g_flag_use_qspi = 0;
|
||||
#define PREFIX_QSPI 1
|
||||
#define PREFIX_SPI 0
|
||||
|
||||
#define USAGE \
|
||||
"spi_async help : Get this information.\n" \
|
||||
@@ -25,7 +28,7 @@ int g_async = 0;
|
||||
"spi_async init qtestdev 1\n" \
|
||||
"spi_async send 0x100\n"
|
||||
|
||||
static void spibit_usage(void)
|
||||
static void spi_usage(void)
|
||||
{
|
||||
printf("%s", USAGE);
|
||||
}
|
||||
@@ -43,48 +46,143 @@ static void show_speed(char *msg, u32 len, u32 us)
|
||||
printf("%s: %d byte, %d us -> %d KB/s\n", msg, len, us, speed);
|
||||
}
|
||||
|
||||
static int test_spibit_attach(int argc, char **argv)
|
||||
static int check_use_qspi(const char *str) {
|
||||
if (strncmp(str, "qspi", 4) == 0) {
|
||||
return PREFIX_QSPI;
|
||||
}
|
||||
|
||||
if (strncmp(str, "spi", 3) == 0) {
|
||||
return PREFIX_SPI;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int test_spi_attach_spi_device(char *bus_name, char *dev_name)
|
||||
{
|
||||
char *bus_name, *dev_name;
|
||||
struct rt_spi_device *spi_device = RT_NULL;
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
|
||||
spi_device =
|
||||
(struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
|
||||
if (spi_device == RT_NULL) {
|
||||
printf("malloc failed.\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
result = rt_spi_bus_attach_device(spi_device, dev_name,
|
||||
bus_name, RT_NULL);
|
||||
if (result < 0)
|
||||
printf("spi_async attach device failed! bus name: %s, device name: %s\n", bus_name, dev_name);
|
||||
|
||||
if (result != RT_EOK)
|
||||
rt_free(spi_device);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_spi_attach_qspi_device(char *bus_name, char *dev_name)
|
||||
{
|
||||
struct rt_qspi_device *qspi_device = RT_NULL;
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
|
||||
qspi_device = (struct rt_qspi_device *)rt_malloc(sizeof(struct rt_qspi_device));
|
||||
if (qspi_device == RT_NULL) {
|
||||
printf("malloc failed.\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
qspi_device->enter_qspi_mode = NULL;
|
||||
qspi_device->exit_qspi_mode = NULL;
|
||||
qspi_device->config.qspi_dl_width = 1; // data_width
|
||||
|
||||
result = rt_spi_bus_attach_device(&qspi_device->parent, dev_name,
|
||||
bus_name, RT_NULL);
|
||||
if (result < 0)
|
||||
printf("spi_async attach device failed! bus name: %s, device name: %s\n", bus_name, dev_name);
|
||||
|
||||
if (result != RT_EOK)
|
||||
rt_free(qspi_device);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_spi_attach(int argc, char **argv)
|
||||
{
|
||||
char *bus_name, *dev_name;
|
||||
|
||||
if (argc != 3) {
|
||||
spibit_usage();
|
||||
spi_usage();
|
||||
return -1;
|
||||
}
|
||||
bus_name = argv[1];
|
||||
dev_name = argv[2];
|
||||
|
||||
/* Attach/Create dev to spi bus */
|
||||
result = aic_qspi_bus_attach_device(bus_name, dev_name, 0, 1, RT_NULL, RT_NULL);
|
||||
if (result < 0)
|
||||
printf("spi_async attach device failed! bus name: %s, device name: %s\n", bus_name, dev_name);
|
||||
g_flag_use_qspi = check_use_qspi(bus_name);
|
||||
if (g_flag_use_qspi == PREFIX_QSPI) {
|
||||
test_spi_attach_qspi_device(bus_name, dev_name);
|
||||
return 0;
|
||||
} else if (g_flag_use_qspi == PREFIX_SPI) {
|
||||
test_spi_attach_spi_device(bus_name, dev_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("[ERROR] bus name: (%s) unsupport! %d\n", bus_name, g_flag_use_qspi);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int test_spi_init_spi_device(char *devname, int set_noblock)
|
||||
{
|
||||
struct rt_spi_configuration spi_cfg;
|
||||
struct rt_device *dev;
|
||||
int ret = 0;
|
||||
|
||||
g_spi = (struct rt_spi_device *)rt_device_find(devname);
|
||||
if (!g_spi) {
|
||||
printf("Failed to get device in name %s\n", devname);
|
||||
return -1;
|
||||
}
|
||||
|
||||
dev = (struct rt_device *)g_spi;
|
||||
if (dev->type != RT_Device_Class_SPIDevice) {
|
||||
g_spi = NULL;
|
||||
printf("%s is not SPI device.\n", devname);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rt_memset(&spi_cfg, 0, sizeof(spi_cfg));
|
||||
spi_cfg.mode = 0;
|
||||
spi_cfg.max_hz = 50000000;
|
||||
ret = rt_spi_configure(g_spi, &spi_cfg);
|
||||
|
||||
ret = rt_spi_nonblock_set(g_spi, set_noblock);
|
||||
|
||||
if (ret < 0) {
|
||||
printf("spi nonblock set failure. ret = %d\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_spibit_init(int argc, char **argv)
|
||||
static int test_spi_init_qspi_device(char *devname, int set_noblock)
|
||||
{
|
||||
struct rt_qspi_configuration qspi_cfg;
|
||||
struct rt_device *dev;
|
||||
int ret = 0;
|
||||
char *name;
|
||||
|
||||
if (argc != 3) {
|
||||
spibit_usage();
|
||||
return -1;
|
||||
}
|
||||
name = argv[1];
|
||||
|
||||
g_qspi = (struct rt_qspi_device *)rt_device_find(name);
|
||||
g_qspi = (struct rt_qspi_device *)rt_device_find(devname);
|
||||
if (!g_qspi) {
|
||||
printf("Failed to get device in name %s\n", name);
|
||||
printf("Failed to get device in name %s\n", devname);
|
||||
return -1;
|
||||
}
|
||||
|
||||
dev = (struct rt_device *)g_qspi;
|
||||
if (dev->type != RT_Device_Class_SPIDevice) {
|
||||
g_qspi = NULL;
|
||||
printf("%s is not SPI device.\n", name);
|
||||
printf("%s is not SPI device.\n", devname);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -96,13 +194,7 @@ static int test_spibit_init(int argc, char **argv)
|
||||
|
||||
g_spi = (struct rt_spi_device*) &g_qspi->parent;
|
||||
|
||||
if (atol(argv[2]) == 1) {
|
||||
ret = rt_spi_nonblock_set(g_spi, 1);
|
||||
g_async = 1;
|
||||
} else {
|
||||
ret = rt_spi_nonblock_set(g_spi, 0);
|
||||
g_async = 0;
|
||||
}
|
||||
ret = rt_spi_nonblock_set(g_spi, set_noblock);
|
||||
|
||||
if (ret < 0) {
|
||||
printf("spi nonblock set failure. ret = %d\n", ret);
|
||||
@@ -112,16 +204,119 @@ static int test_spibit_init(int argc, char **argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_spi_init(int argc, char **argv)
|
||||
{
|
||||
int set_noblock = 0;
|
||||
char *name;
|
||||
|
||||
if (argc != 3) {
|
||||
spi_usage();
|
||||
return -1;
|
||||
}
|
||||
name = argv[1];
|
||||
|
||||
if (atol(argv[2]) == 1) {
|
||||
set_noblock = 1;
|
||||
}
|
||||
g_async = set_noblock;
|
||||
|
||||
if (g_flag_use_qspi == PREFIX_QSPI)
|
||||
test_spi_init_qspi_device(name, set_noblock);
|
||||
else
|
||||
test_spi_init_spi_device(name, set_noblock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_spi_async_send_spi_device(uint8_t *data, uint32_t send_len)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
rt_size_t ret;
|
||||
|
||||
if (g_spi == NULL) {
|
||||
printf("[ERROR] g_spi did not init!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rt_spi_take_bus((struct rt_spi_device *)g_spi);
|
||||
|
||||
unsigned long start_us = aic_get_time_us();
|
||||
ret = rt_spi_transfer(g_spi, (void *)data, NULL, send_len);
|
||||
if (g_async == 1)
|
||||
show_speed("async speed", send_len, aic_get_time_us() - start_us);
|
||||
else
|
||||
show_speed("sync speed", send_len, aic_get_time_us() - start_us);
|
||||
|
||||
if (g_async == 1) {
|
||||
while (rt_spi_get_transfer_status(g_spi) != 0) {
|
||||
count++;
|
||||
}
|
||||
show_speed("sync speed", send_len, aic_get_time_us() - start_us);
|
||||
}
|
||||
|
||||
rt_spi_release_bus((struct rt_spi_device *)g_spi);
|
||||
|
||||
if (ret != send_len) {
|
||||
printf("Send data failed. ret 0x%x\n", (int)ret);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_spi_async_send_qspi_device(uint8_t *data, uint32_t send_len)
|
||||
{
|
||||
struct rt_qspi_message msg;
|
||||
uint32_t count = 0;
|
||||
rt_size_t ret;
|
||||
|
||||
if (g_qspi == NULL) {
|
||||
printf("[ERROR] g_spi did not init!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rt_memset(&msg, 0, sizeof(msg));
|
||||
msg.instruction.content = 0;
|
||||
msg.instruction.qspi_lines = 0;
|
||||
msg.dummy_cycles = 0;
|
||||
|
||||
msg.qspi_data_lines = 1;
|
||||
msg.parent.send_buf = (void *)data;
|
||||
msg.parent.length = send_len;
|
||||
msg.parent.cs_take = 1;
|
||||
msg.parent.cs_release = 1;
|
||||
|
||||
rt_spi_take_bus((struct rt_spi_device *)g_qspi);
|
||||
|
||||
unsigned long start_us = aic_get_time_us();
|
||||
ret = rt_qspi_transfer_message(g_qspi, &msg);
|
||||
if (g_async == 1)
|
||||
show_speed("async speed", send_len, aic_get_time_us() - start_us);
|
||||
else
|
||||
show_speed("sync speed", send_len, aic_get_time_us() - start_us);
|
||||
|
||||
if (g_async == 1) {
|
||||
while (rt_spi_get_transfer_status((struct rt_spi_device *)g_qspi) != 0) {
|
||||
count++;
|
||||
}
|
||||
show_speed("async mode spi true speed", send_len, aic_get_time_us() - start_us);
|
||||
}
|
||||
|
||||
rt_spi_release_bus((struct rt_spi_device *)g_qspi);
|
||||
|
||||
if (ret != send_len) {
|
||||
printf("Send data failed. ret 0x%x\n", (int)ret);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_spi_async_send(int argc, char **argv)
|
||||
{
|
||||
uint32_t align_len = 0, send_len = 0;
|
||||
uint8_t *data = 0;
|
||||
uint32_t count = 0;
|
||||
struct rt_qspi_message msg;
|
||||
rt_size_t ret;
|
||||
|
||||
if (argc != 2) {
|
||||
spibit_usage();
|
||||
spi_usage();
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -136,37 +331,12 @@ static int test_spi_async_send(int argc, char **argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_memset(&msg, 0, sizeof(msg));
|
||||
msg.instruction.content = 0;
|
||||
msg.instruction.qspi_lines = 0;
|
||||
msg.dummy_cycles = 0;
|
||||
|
||||
msg.qspi_data_lines = 1;
|
||||
msg.parent.send_buf = (void *)data;
|
||||
msg.parent.length = align_len;
|
||||
msg.parent.cs_take = 1;
|
||||
msg.parent.cs_release = 1;
|
||||
rt_spi_take_bus((struct rt_spi_device *)g_qspi);
|
||||
|
||||
unsigned long start_us = aic_get_time_us();
|
||||
ret = rt_qspi_transfer_message(g_qspi, &msg);
|
||||
if (g_async == 1)
|
||||
show_speed("async speed", align_len, aic_get_time_us() - start_us);
|
||||
if (g_flag_use_qspi == PREFIX_QSPI)
|
||||
test_spi_async_send_qspi_device(data, align_len);
|
||||
else
|
||||
show_speed("sync speed", align_len, aic_get_time_us() - start_us);
|
||||
|
||||
rt_spi_release_bus((struct rt_spi_device *)g_qspi);
|
||||
if (g_async == 1) {
|
||||
while (rt_spi_get_transfer_status(g_spi) != 0) {
|
||||
count++;
|
||||
}
|
||||
show_speed("sync speed", align_len, aic_get_time_us() - start_us);
|
||||
}
|
||||
test_spi_async_send_spi_device(data, align_len);
|
||||
|
||||
aicos_free_align(MEM_DEFAULT, data);
|
||||
if (ret != align_len) {
|
||||
printf("Send data failed. ret 0x%x\n", (int)ret);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -179,17 +349,75 @@ static void cmd_test_spi_async(int argc, char **argv)
|
||||
if (!rt_strcmp(argv[1], "help")) {
|
||||
goto help;
|
||||
} else if (!rt_strcmp(argv[1], "attach")) {
|
||||
test_spibit_attach(argc - 1, &argv[1]);
|
||||
test_spi_attach(argc - 1, &argv[1]);
|
||||
return;
|
||||
} else if (!rt_strcmp(argv[1], "init")) {
|
||||
test_spibit_init(argc - 1, &argv[1]);
|
||||
test_spi_init(argc - 1, &argv[1]);
|
||||
return;
|
||||
} else if (!rt_strcmp(argv[1], "send")) {
|
||||
test_spi_async_send(argc - 1, &argv[1]);
|
||||
return;
|
||||
}
|
||||
help:
|
||||
spibit_usage();
|
||||
spi_usage();
|
||||
}
|
||||
|
||||
MSH_CMD_EXPORT_ALIAS(cmd_test_spi_async, spi_async, Test spi async transfer);
|
||||
|
||||
#define WORK_IN_SPI_MODE 0
|
||||
|
||||
void spi_thread_entry(void *parameter)
|
||||
{
|
||||
u64 start_ms;
|
||||
#if WORK_IN_SPI_MODE
|
||||
char *dev_name = "spi3_dev";
|
||||
char *bus_name = "spi3";
|
||||
#else
|
||||
char *dev_name = "qspi1_dev";
|
||||
char *bus_name = "qspi1";
|
||||
#endif
|
||||
u8 *data;
|
||||
u32 non_block = 1;
|
||||
|
||||
g_spi = (struct rt_spi_device *)rt_device_find(dev_name);
|
||||
if (!g_spi) {
|
||||
printf("try to attach bus:%s dev: %s\n", bus_name, dev_name);
|
||||
test_spi_attach_spi_device(bus_name, dev_name);
|
||||
}
|
||||
#if WORK_IN_SPI_MODE
|
||||
test_spi_init_spi_device(dev_name, non_block);
|
||||
#else
|
||||
test_spi_init_qspi_device(dev_name, non_block);
|
||||
#endif
|
||||
|
||||
data = aicos_malloc_align(MEM_DEFAULT, 65536, CACHE_LINE_SIZE);
|
||||
|
||||
rt_spi_take_bus((struct rt_spi_device *)g_spi);
|
||||
start_ms = aic_get_time_ms();
|
||||
|
||||
while (1)
|
||||
{
|
||||
rt_spi_transfer(g_spi, (void *)data, NULL, 65536);
|
||||
if (non_block) {
|
||||
rt_spi_wait_completion(g_spi);
|
||||
}
|
||||
|
||||
if (aic_get_time_ms() - start_ms > 30000)
|
||||
break;
|
||||
}
|
||||
|
||||
rt_spi_release_bus((struct rt_spi_device *)g_spi);
|
||||
aicos_free_align(MEM_DEFAULT, data);
|
||||
}
|
||||
|
||||
static void cmd_test_spi_async_thread(int argc, char **argv)
|
||||
{
|
||||
rt_thread_t tid = RT_NULL;
|
||||
|
||||
tid = rt_thread_create("spi_async_thread", spi_thread_entry, RT_NULL, 8192, 22, 5);
|
||||
if (tid != RT_NULL)
|
||||
rt_thread_startup(tid);
|
||||
|
||||
}
|
||||
|
||||
MSH_CMD_EXPORT_ALIAS(cmd_test_spi_async_thread, spi_async_thread, Test spi async transfer);
|
||||
|
||||
Reference in New Issue
Block a user