You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.5 KiB
129 lines
3.5 KiB
/*
|
|
* Copyright (c) Hisilicon Technologies Co., Ltd.. 2020-2021. All rights reserved.
|
|
* Description: sample test for spi function.
|
|
* Author: Hisilicon
|
|
* Create: 2020-03-10
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "uapi_spi.h"
|
|
#include "uapi_system.h"
|
|
|
|
#define ext_err_spi(format, arg...) printf(format, ## arg)
|
|
#define BAUD 8
|
|
#define DSS 8
|
|
#define ARGC_NUM 3
|
|
#define PARA_BIT 2
|
|
|
|
|
|
static td_s32 sample_spi_init(td_u32 spi_num)
|
|
{
|
|
td_s32 ret;
|
|
uapi_spi_attr attr = {0};
|
|
|
|
ret = uapi_sys_init();
|
|
if (ret != TD_SUCCESS) {
|
|
printf("call uapi_sys_init failed.\n");
|
|
return ret;
|
|
}
|
|
ret = uapi_spi_init();
|
|
if (ret != TD_SUCCESS) {
|
|
ext_err_spi("%s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, ret);
|
|
return ret;
|
|
}
|
|
ret = uapi_spi_open(spi_num);
|
|
if (ret != TD_SUCCESS) {
|
|
ext_err_spi("%s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, ret);
|
|
return ret;
|
|
}
|
|
attr.cs_cfg = UAPI_SPI_LOGIC_CS;
|
|
attr.is_bigend = TD_TRUE; /* big endian */
|
|
attr.device = spi_num;
|
|
attr.frame_format = UAPI_SPI_FORMAT_MOTO;
|
|
attr.baud = BAUD;
|
|
attr.dss = DSS;
|
|
|
|
ret = uapi_spi_set_attr(spi_num, &attr);
|
|
if (ret != TD_SUCCESS) {
|
|
ext_err_spi("%s: %d UAPI_SPI_SetAttr ErrorCode=0x%x\n", __FILE__, __LINE__, ret);
|
|
return ret;
|
|
}
|
|
return TD_SUCCESS;
|
|
}
|
|
|
|
static void sample_spi_read_write(td_u32 spi_num, td_u8 *data, td_u8 *read_data, td_u32 write_number)
|
|
{
|
|
td_s32 ret;
|
|
|
|
/* Read data from Device */
|
|
ret = uapi_spi_write(spi_num, data, write_number);
|
|
if (ret != TD_SUCCESS) {
|
|
ext_err_spi("spi write failed!\n");
|
|
} else {
|
|
printf("spi write SUCCESS!\n");
|
|
}
|
|
|
|
/* Read data from Device */
|
|
ret = uapi_spi_read(spi_num, read_data, write_number);
|
|
if (ret != TD_SUCCESS) {
|
|
ext_err_spi("spi read failed!\n");
|
|
}
|
|
}
|
|
|
|
td_s32 main(td_s32 argc, td_char **argv)
|
|
{
|
|
td_s32 ret;
|
|
td_u8 *data = TD_NULL;
|
|
td_u8 *read_data = TD_NULL;
|
|
td_u32 spi_num, write_number, loop;
|
|
|
|
if (argc < ARGC_NUM || argv == TD_NULL) {
|
|
ext_err_spi("Usage: sample_spi_write spi_channel data_num byte0 [... byten] \n");
|
|
return -1;
|
|
}
|
|
|
|
spi_num = (td_u32)strtol(argv[1], NULL, 0);
|
|
write_number = (td_u32)strtol(argv[PARA_BIT], NULL, 0);
|
|
if ((write_number + ARGC_NUM) > (td_u32)argc) {
|
|
ext_err_spi("input error! Usage: sample_spi_write spi_channel data_num byte0 [... byten] \n");
|
|
return -1;
|
|
}
|
|
|
|
if (spi_num >= UAPI_SPI_DEV_MAX) {
|
|
return -1;
|
|
}
|
|
|
|
ret = sample_spi_init(spi_num);
|
|
if (ret != TD_SUCCESS) {
|
|
return -1;
|
|
}
|
|
if (write_number == 0) {
|
|
return -1;
|
|
}
|
|
|
|
data = (td_u8 *)malloc(write_number);
|
|
if (data == TD_NULL) {
|
|
ext_err_spi("\n malloc() error!\n");
|
|
uapi_spi_deinit();
|
|
return TD_FAILURE;
|
|
}
|
|
read_data = (td_u8 *)malloc(write_number);
|
|
if (read_data == TD_NULL) {
|
|
free(data);
|
|
ext_err_spi("\n malloc() error!\n");
|
|
uapi_spi_deinit();
|
|
return TD_FAILURE;
|
|
}
|
|
for (loop = 0; loop < write_number; loop++) {
|
|
data[loop] = (td_u8)strtol(argv[loop + ARGC_NUM], NULL, 0);
|
|
}
|
|
sample_spi_read_write(spi_num, data, read_data, write_number);
|
|
free(data);
|
|
free(read_data);
|
|
uapi_spi_deinit();
|
|
uapi_spi_close(spi_num);
|
|
(td_void)uapi_sys_deinit();
|
|
return ret;
|
|
}
|