/* * Copyright (c) Hisilicon Technologies Co., Ltd.. 2020-2021. All rights reserved. * Description: sample test for spi function. * Author: Hisilicon * Create: 2020-03-10 */ #include #include #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_read_init(td_u32 spi_id) { td_s32 ret; uapi_spi_attr attr = {0}; ret = uapi_sys_init(); if (ret != TD_SUCCESS) { printf("spi read: call uapi_sys_init failed.\n"); return ret; } ret = uapi_spi_init(); if (ret != TD_SUCCESS) { ext_err_spi("spi read: %s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, ret); return ret; } ret = uapi_spi_open(spi_id); if (ret != TD_SUCCESS) { ext_err_spi("spi read: %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_id; attr.frame_format = UAPI_SPI_FORMAT_MOTO; attr.baud = BAUD; attr.dss = DSS; ret = uapi_spi_set_attr(spi_id, &attr); if (ret != TD_SUCCESS) { ext_err_spi("spi read: %s: %d UAPI_SPI_SetAttr ErrorCode=0x%x\n", __FILE__, __LINE__, ret); return ret; } return TD_SUCCESS; } static void sample_spi_read(td_u32 spi_id, td_u8 *read_data, td_u32 read_number) { td_s32 ret; td_u32 i; /* Read data from Device */ ret = uapi_spi_read(spi_id, read_data, read_number); if (ret != TD_SUCCESS) { ext_err_spi("spi read failed!\n"); return; } printf("read %d byte:", read_number); for (i = 0; i < read_number; i++) { if (i % 16U == 0) { printf("\n"); } printf("0x%x ", read_data[i]); } printf("\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_id, read_number, loop; if (argc < ARGC_NUM || argv == TD_NULL) { ext_err_spi("Usage: sample_spi_read spi_channel read_number \n"); return -1; } spi_id = (td_u32)strtol(argv[1], NULL, 0); read_number = (td_u32)strtol(argv[PARA_BIT], NULL, 0); if (spi_id >= UAPI_SPI_DEV_MAX) { return -1; } ret = sample_spi_read_init(spi_id); if (ret != TD_SUCCESS) { return -1; } if (read_number == 0) { return -1; } read_data = (td_u8 *)malloc(read_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 < read_number; loop++) { read_data[loop] = 0; } (void)sample_spi_read(spi_id, read_data, read_number); free(data); free(read_data); uapi_spi_deinit(); uapi_spi_close(spi_id); (td_void)uapi_sys_deinit(); return ret; }