#include #include #include #include #include "uapi_spi.h" #include "uapi_system.h" void PrintUsage() { printf("Usage: spi_tools \n"); printf("\t type r:\n"); printf("\t type w: \n"); printf("\t type t: \n"); } #define CMD_TYPE_R "r" #define CMD_TYPE_W "w" #define CMD_TYPE_T "t" int8_t spi_init(uint32_t ch,uint32_t baud,uint32_t dss) { uapi_spi_attr attr={0}; if(uapi_spi_init() != TD_SUCCESS) { printf("uapi_spi_init failed.\n"); return -1; } if(uapi_spi_open(ch)!= TD_SUCCESS) { printf("uapi_spi_open failed.\n"); return -1; } attr.device = ch; attr.baud = baud; attr.cs_cfg = UAPI_SPI_LOGIC_CS; attr.is_bigend = TD_TRUE; attr.frame_format = UAPI_SPI_FORMAT_MOTO; attr.dss = dss; attr.ext_attr.motorola.spo = UAPI_SPI_SPO_0; attr.ext_attr.motorola.sph = UAPI_SPI_SPH_1; if(uapi_spi_set_attr(ch,&attr) != TD_SUCCESS) { printf("uapi_spi_set_attr failed.\n"); return -1; } return 0; } void spi_deinit(uint32_t ch) { uapi_spi_deinit(); uapi_spi_close(ch); } uint8_t spi_write(uint32_t ch,uint8_t *buf,uint32_t len) { if(uapi_spi_write(ch,buf,len)!= TD_SUCCESS) { printf("uapi_spi_write failed.\n"); return -1; } return 0; } uint8_t spi_read(uint32_t ch,uint8_t *buf,uint32_t len) { if(uapi_spi_read(ch,buf,len)!= TD_SUCCESS) { printf("uapi_spi_write failed.\n"); return -1; } return 0; } uint8_t spi_transfer(uint32_t ch,uint8_t *wbuf,uint32_t wb,uint8_t *rbuf,uint32_t rb) { if(uapi_spi_transmit(ch,wbuf,wb,rbuf,rb)!=TD_SUCCESS) { printf("uapi_spi_write failed.\n"); return -1; } return 0; } int main(int argc,char **argv) { if(argc < 3) { printf("invalid parameter.\n"); PrintUsage(); return 0; } if(uapi_sys_init() != TD_SUCCESS) { printf("upai_sys_init failed.\n"); return 0; } uint32_t spi_ch = (uint32_t)strtol(argv[1], NULL, 0); //uint32_t baud = (uint32_t)strtol(argv[2], NULL, 0); uint32_t baud=16; uint32_t dss = (uint32_t)strtol(argv[2], NULL, 0); if(spi_init(spi_ch,baud,dss)!=0) { return 0; } char *type = argv[3]; if(strcmp(type,CMD_TYPE_R)==0) { if(argc < 4) { printf("invalid parameter.\n"); PrintUsage(); spi_deinit(spi_ch); uapi_sys_deinit(); return 0; } uint32_t rb = (uint32_t)strtol(argv[4], NULL, 0); uint8_t *rbuf = (uint8_t *)malloc(rb); if(rbuf == NULL) { printf("malloc fail.\n"); spi_deinit(spi_ch); uapi_sys_deinit(); return 0; } memset(rbuf,0,rb); if(spi_read(spi_ch,rbuf,rb)!=0) { free(rbuf); spi_deinit(spi_ch); uapi_sys_deinit(); return 0; } for(int i=0;i