2020-01-19 19:36发布
rain73 发表于 2015-10-16 15:02 STC有四串口的,还不够用?SPI通信很简单的了,自己写不行?
MC-26 发表于 2015-10-16 15:17 我刚出道,对单片机了解有限,而且我们这边板子已经做好了,用的不是四串口的单片机,而是用CH432T来扩展 ...
rain73 发表于 2015-10-16 19:10 这样别人很难帮到你,除非以前做过类似的例程。 要不你发块板过来,我帮你写出来?呵呵 ...
最多设置5个标签!
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <linux/types.h>
#include "appspi.h"
#include "spidev.h"
#include "log/log.h"
#include "ch432.h"
#include "include/basetype.h"
static int fd_spi[MAXNUM_SPI] = {-1, -1};
static uint8_t mode=SPI_MODE_0;
static uint8_t bits = 8;
static uint32_t speed = 24000000;
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define SIZE_APPSPI_RCVBUF 1024
struct appspi_rcvst
{
char buf[SIZE_APPSPI_RCVBUF];
int len;
int head;
};
static struct appspi_rcvst appspi_rcvbuf[MAXNUM_SPI];
int appspi_open(unsigned int port)
{
int fd;
char dev[32];
int ret;
if(-1 != fd_spi[port])
close(fd_spi[port]);
if(port==0) sprintf(dev, "/dev/spidev0.0");
else if(port==1) sprintf(dev, "/dev/spidev0.1");
fd = open(dev, O_RDWR);
if(fd<0)
{
printf("can't open spi device");
return 1;
}
print_log(LOGLVL_DEBUG,"open spi port=%d ",port);
ret = ioctl(fd, SPI_IOC_WR_MODE,&mode);
if (ret == -1)
print_log(LOGLVL_ERROR,"can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_MODE,&mode);
if (ret == -1)
print_log(LOGLVL_ERROR,"can't get spi mode");
/*
* bits per word
*/
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
print_log(LOGLVL_ERROR,"can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
print_log(LOGLVL_ERROR,"can't get bits per word");
/*
* max speed hz
*/
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
print_log(LOGLVL_ERROR,"can't set max speed hz");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
print_log(LOGLVL_ERROR,"can't get max speed hz");
print_log(LOGLVL_DEBUG,"spi mode: %d ", mode);
printf("bits per word: %d ", bits);
printf("max speed: %d Hz (%d KHz) ", speed, speed/1000);
fd_spi[port]=fd;
appspi_rcvbuf[port].len=0;
return 0;
}
void appspi_close(unsigned int port)
{
if((port >= MAXNUM_SPI) || (-1 == fd_spi[port]))
return;
close(fd_spi[port]);
fd_spi[port] = -1;
}
int appspi_send(unsigned int port, UCHAR *buf, unsigned int len)
{
// int i;
int ret;
int fd;
if(port >= MAXNUM_SPI)
return -1;
fd = fd_spi[port];
if(fd < 0)
{
appspi_open(port);
fd = fd_spi[port];
if(fd<0)
return -1;
}
struct spi_ioc_transfer xfer[1];
memset(xfer, 0, sizeof(xfer));
xfer[0].tx_buf = (__u64)(int) buf;
xfer[0].len = len;
ret = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);
if (ret < 0)
{
print_log(LOGLVL_ERROR,"Error: SPI_IOC_MESSAGE ");
return -1;
}
return ret;
}
int appspi_recv(unsigned int port,UCHAR addr, UCHAR *buf, unsigned int len)
{
// int i;
int ret;
int fd;
UCHAR tx[32];
// UCHAR *bp;
if(port>= MAXNUM_SPI)
return -1;
fd = fd_spi[port];
if(fd < 0)
{
appspi_open(port);
fd = fd_spi[port];
if(fd<0)
return -1;
}
struct spi_ioc_transfer xfer[2];
memset(xfer, 0, sizeof(xfer));
tx[0]=addr;
xfer[0].tx_buf =(__u64)(int)tx;
xfer[0].len = 1;
xfer[1].rx_buf =(__u64)(int) buf;
xfer[1].len =len;
ret = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);
if (ret < 0)
{
print_log(LOGLVL_ERROR,"Error: SPI_IOC_MESSAGE ");
return -1;
}
return ret;
}
int appspi_getfd(unsigned int port)
{
if(port >= MAXNUM_SPI)
return -1;
return(fd_spi[port]);
}
void writeCH432Data(unsigned int port,UCHAR mAddr,UCHAR dat)
{
UCHAR buf[2];
mAddr=(mAddr<<2)|0x02;
buf[0]=mAddr;
buf[1]=dat;
appspi_send(port,buf,2);
}
void WriteCH432Block(unsigned int port,UCHAR mAddr,int len,UCHAR *dat)
{
while(len--)
{
writeCH432Data(port,mAddr,*dat++);
}
}
unsigned char ReadCH432Data(unsigned int port,UCHAR mAddr)
{
unsigned char recvdata=0;
int ret;
mAddr=(mAddr<<2)&0xFD;
// appspi_send(port,&mAddr,1);
ret=appspi_recv(port,mAddr,&recvdata,1);
return(recvdata);
}
我刚出道,对单片机了解有限,而且我们这边板子已经做好了,用的不是四串口的单片机,而是用CH432T来扩展串口,我也是没办法啊
这样别人很难帮到你,除非以前做过类似的例程。
要不你发块板过来,我帮你写出来?呵呵
这个是我们的电路图,您就帮我看看,写个简单的收、发示例程序吧,谢谢了!
一周热门 更多>