赞
踩
SPI,是英语Serial Peripheral Interface的缩写,顾名思义就是串行外围设备接口。SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为PCB的布局上节省空间,提供方便,正是出于这种简单易用的特性,现在越来越多的芯片集成了这种通信协议。
SPI总线是Motorola公司推出的三线同步接口,同步串行3线方式进行通信:一条时钟线SCK,一条数据输入线MOSI,一条数据输出线MISO;用于 CPU与各种外围器件进行全双工、同步串行通讯。SPI主要特点有:可以同时发出和接收串行数据;可以当作主机或从机工作;提供频率可编程时钟;发送结束中断标志;写冲突保护;总线竞争保护等。
unsigned int spi_wr_1bit_cmd_read_16bit(){
int i = 0;
unsigned int rd_val = 0; unsigned char sda_in = 0;
unsigned char buf = 0xaa;
unsigned char rd16[16] = {0};
spi_wr_cs(1);
spi_wr_sck(1);
spi_wr_sda_out(1);
// drop down cs
spi_wr_cs(0);
delay_us(1);
// drop down sck
spi_wr_sck(0);
delay_us(1);
// write first data bit as 1
spi_wr_sda_out(1);
delay_us(1);
spi_wr_sck(1);
delay_us(1);
spi_wr_sck(0);
// read data
for(i = 0; i < 16; i ++){
spi_wr_sda_out(0);
buf = buf << 1;
delay_us(1);
spi_wr_sck(1);
delay_us(1);
spi_wr_sck(0);
// read sda in ,at sclk neg edge
sda_in = spi_rd_sda_in();
// printf("sda_in = %x \r\n",sda_in);
rd_val = (rd_val << 1)| (sda_in&0x01);
rd16[i] = sda_in;
}
spi_wr_cs(1);
spi_wr_sck(1);
// printf("rd_val = %x \r\n",rd_val);
#if 0 // bit print debug
for(i = 0; i < 16; i ++){
printf("rd16[%d] = %x \r\n",i, rd16[i]);
}
#endif
return rd_val;
}
void spi_wr_32bit_MSB_first(unsigned char B3,unsigned char B2,unsigned char B1,unsigned char B0){
int i = 0, j = 0;
unsigned char byte4[4] = {0};
unsigned char buf = 0;
byte4[3] = B3; byte4[2] = B2; byte4[1] = B1; byte4[0] = B0;
spi_wr_cs(1);
spi_wr_sck(1);
spi_wr_sda_out(1);
// drop down cs
spi_wr_cs(0);
delay_us(1);
// drop down sck
spi_wr_sck(0);
delay_us(1);
// put data
for(j = 0; j < 4; j ++){
buf = byte4[3-j];
for(i = 0; i < 8; i ++){
spi_wr_sda_out((buf&0x80) >> 7);
buf = buf << 1;
delay_us(1);
spi_wr_sck(1);
delay_us(1);
spi_wr_sck(0);
}
}
spi_wr_cs(1);
spi_wr_sck(1);
}
FPGA
`
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。