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.
253 lines
5.8 KiB
253 lines
5.8 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include "uapi_spi.h"
|
|
#include "uapi_system.h"
|
|
|
|
void PrintUsage()
|
|
{
|
|
printf("Usage: spi_tools <ch> <baud> <type>\n");
|
|
printf("\t type r:<read_number>\n");
|
|
printf("\t type w:<write_number> <write_data0> <write_data n>\n");
|
|
printf("\t type t:<write_number> <read_number> <write_data0> <write_data n>\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<rb;i++) printf("%02x ",rbuf[i]);
|
|
|
|
free(rbuf);
|
|
|
|
}
|
|
else if(strcmp(type,CMD_TYPE_W)==0)
|
|
{
|
|
if(argc < 4)
|
|
{
|
|
printf("invalid parameter.\n");
|
|
PrintUsage();
|
|
spi_deinit(spi_ch);
|
|
uapi_sys_deinit();
|
|
return 0;
|
|
}
|
|
|
|
uint32_t wb = (uint32_t)strtol(argv[4], NULL, 0);
|
|
|
|
uint8_t *wbuf = (uint8_t *)malloc(wb);
|
|
if(wbuf == NULL)
|
|
{
|
|
printf("malloc fail.\n");
|
|
spi_deinit(spi_ch);
|
|
uapi_sys_deinit();
|
|
return 0;
|
|
}
|
|
|
|
memset(wbuf,0,wb);
|
|
|
|
for(uint32_t i=0;i<wb;i++) wbuf[i] = (uint8_t)strtol(argv[5+i],NULL,0);
|
|
|
|
if(spi_write(spi_ch,wbuf,wb)!=0)
|
|
{
|
|
free(wbuf);
|
|
spi_deinit(spi_ch);
|
|
uapi_sys_deinit();
|
|
return 0;
|
|
}
|
|
free(wbuf);
|
|
printf("write success.\n");
|
|
}
|
|
else if(strcmp(type,CMD_TYPE_T)==0)
|
|
{
|
|
if(argc < 5)
|
|
{
|
|
printf("invalid parameter.\n");
|
|
PrintUsage();
|
|
spi_deinit(spi_ch);
|
|
uapi_sys_deinit();
|
|
return 0;
|
|
}
|
|
|
|
uint32_t twb = (uint32_t)strtol(argv[4], NULL, 0);
|
|
uint32_t trb = (uint32_t)strtol(argv[5], NULL, 0);
|
|
|
|
uint8_t *twbuf = (uint8_t *)malloc(twb);
|
|
if(twbuf == NULL)
|
|
{
|
|
printf("twbuf malloc fail.\n");
|
|
spi_deinit(spi_ch);
|
|
uapi_sys_deinit();
|
|
return 0;
|
|
}
|
|
|
|
memset(twbuf,0,twb);
|
|
|
|
uint8_t *trbuf = (uint8_t *)malloc(trb);
|
|
if(trbuf == NULL)
|
|
{
|
|
free(twbuf);
|
|
printf("trbuf malloc fail.\n");
|
|
spi_deinit(spi_ch);
|
|
uapi_sys_deinit();
|
|
return 0;
|
|
}
|
|
memset(trbuf,0,trb);
|
|
|
|
for(uint32_t i=0;i<twb;i++) twbuf[i] = (uint8_t)strtol(argv[6+i],NULL,0);
|
|
|
|
if(spi_transfer(spi_ch,twbuf,twb,trbuf,trb)!=0)
|
|
{
|
|
free(twbuf);
|
|
free(trbuf);
|
|
spi_deinit(spi_ch);
|
|
uapi_sys_deinit();
|
|
return 0;
|
|
}
|
|
|
|
printf("recv:");
|
|
for(int i=0;i<trb;i++) printf("[0x%02X]",trbuf[i]);
|
|
printf("\n");
|
|
free(twbuf);
|
|
free(trbuf);
|
|
printf("tranfer success.\n");
|
|
}
|
|
else
|
|
{
|
|
printf("unkown type %s\n",type);
|
|
PrintUsage();
|
|
spi_deinit(spi_ch);
|
|
uapi_sys_deinit();
|
|
}
|
|
|
|
spi_deinit(spi_ch);
|
|
uapi_sys_deinit();
|
|
return 0;
|
|
} |