|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2015-10-16 14:35:01
|
显示全部楼层
附上嵌入式linux spi转CH432驱动,不知道这个和单片机的有什么区别呢?
#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\n",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\n", mode);
printf("bits per word: %d\n", bits);
printf("max speed: %d Hz (%d KHz)\n", 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\n");
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\n");
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);
}
|
|