存储技术
FM25L256是由RAMTRON生产,以铁电存储介质的256Kb(32K字节)串行3V非易失性存储器,采用SPI总线控制,构成的系统具有简单,占用硬件资源少,存取快速的特点。下面将具体的来说说FM25L256的驱动测试。
#include 《stdio.h》
#include 《stdint.h》
#include 《string.h》
#include 《fcntl.h》
#include 《sys/ioctl.h》
#include 《linux/types.h》
#include 《linux/spi/spidev.h》
#define SPI_DEVICE “/dev/spidev1.0”
//fm25L256 cmd
#define FM_OPT_WREN 0x06
#define FM_OPT_WRDI 0x04
#define FM_OPT_RDSR 0x05
#define FM_OPT_WRSR 0x01
#define FM_OPT_ERAD 0x03
#define FM_OPT_WRIT 0x02
//fm25l256 para
#define FM_ADDR_MAX 0x7fff
#define FM_MODE SPI_MODE_0
#define FM_BITS 8
#define FM_FREQ_MAX 50000000
#define FM_OPT_ADDR 0x0
static uint8_t gMode = FM_MODE;
static uint8_t gBits = FM_BITS;
static uint32_t gSpeed = FM_FREQ_MAX;
static uint16_t gOptAddr = FM_OPT_ADDR;
uint8_t gLsb;
static int spi_write_read(int fd, const uint8_t *wbuf, int wlen, uint8_t *rbuf, int rlen){
int ret = 0;
struct spi_ioc_transfer tr[2] = {
{
.tx_buf = (unsigned long)wbuf,
.rx_buf = 0,
.len = wlen,
.speed_hz = 5000000,
},
{
.tx_buf = 0,
.rx_buf = (unsigned long)rbuf,
.len = rlen,
.speed_hz = 5000000,
},
};
ret = ioctl(fd, SPI_IOC_MESSAGE(2), tr);
return ret;
}
static int spi_write_only(int fd, const uint8_t *wbuf, int wlen, uint8_t *rbuf, int rlen){
int ret = 0;
struct spi_ioc_transfer tr[1] = {
{
.tx_buf = (unsigned long)wbuf,
.rx_buf = 0,
.len = wlen,
.speed_hz = 5000000,
},
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), tr);
return ret;
}
int main(int argc, char *argv[]){
int fd;
int index;
int optSize;
int ret;
int i;
int tmpSize;
uint8_t rdBuf[128];
uint8_t wrBuf[128];
uint8_t cmd;
if(argc != 2){
printf(“argc error.\n”);
return -1;
}
tmpSize = atoi(argv[1]);
fd = open(SPI_DEVICE, O_RDWR);
if(fd 《 0){
printf(“open spi file error.\n”);
return -1;
}
//mode
ret = ioctl(fd, SPI_IOC_WR_MODE, &gMode);
if(ret 《 0){
printf(“set wr mode error.\n”);
close(fd);
return -1;
}
//bits
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &gBits);
if(ret 《 0){
printf(“set bits error.\n”);
close(fd);
return -1;
}
//speed
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &gSpeed);
if(ret 《 0){
printf(“set speed error.\n”);
close(fd);
return -1;
}
//get mode
ret = ioctl(fd, SPI_IOC_RD_MODE, &gMode);
printf(“FM Mode: %d.\n”,gMode);
//get bits
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &gBits);
printf(“FM bits:%d.\n”, gBits);
//get speed
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &gSpeed);
printf(“FM speed:%d.\n”, gSpeed);
//get lsb
ret = ioctl(fd, SPI_IOC_RD_LSB_FIRST, &gLsb);
printf(“FM lsb:%d.\n”, gLsb);
#if 0 //---------------------- method 1 ----------------------------
cmd = FM_OPT_WREN;
ret = write(fd, &cmd, sizeof(cmd));
if(ret 《 0 )
printf(“write wren error.err = %d\n”, ret);
else
printf(“write wren ok.\n”);
index = 0;
wrBuf[index++] = FM_OPT_WRIT;
wrBuf[index++] = (gOptAddr》》8)&0xff;
wrBuf[index++] = (gOptAddr》》0)&0xff;
srand((unsigned)time(NULL));
printf(“write data = \n”);
if(tmpSize){
for(i = 0; i 《 tmpSize; i++){
rdBuf[i] = 1 + rand()%100+1;
printf(“%02x ”, rdBuf[i]);
}
}
else{
tmpSize = 128;
for(i = 0; i 《 tmpSize; i++){
rdBuf[i] = 0;
printf(“%02x ”, rdBuf[i]);
}
}
printf(“\n”);
memcpy(&wrBuf[index], rdBuf,tmpSize);
index += tmpSize;
optSize = index;
//ret = spi_write_read(fd, wrBuf, optSize, 0, 0);
ret = spi_write_only(fd, wrBuf,optSize, 0, 0);
if(ret 《 0)
printf(“write error,err = %d\n”, ret);
else
printf(“write ok.ret = %d\n”, ret);
memset(rdBuf, 0, sizeof(rdBuf));
cmd = FM_OPT_ERAD;
index = 0;
wrBuf[index++] = cmd;
wrBuf[index++] = (gOptAddr》》8)&0xff;
wrBuf[index++] = (gOptAddr》》0)&0xff;
ret = spi_write_read(fd, wrBuf, index, rdBuf, sizeof(rdBuf));
if(ret 《 0){
printf(“read error, err = %d\n”, ret);
}
else{
printf(“read ok.ret = %d, data = \n”, ret);
for(index = 0; index 《 sizeof(rdBuf); index++){
printf(“%02x ”, rdBuf[index]);
}
printf(“\n”);
}
#endif //endof method 1 ------------------------------
#if 1
//**************** method 2 *******************
cmd = FM_OPT_WREN;
ret = write(fd, &cmd, sizeof(cmd));
if(ret 《 0 )
printf(“write wren error.err = %d\n”, ret);
else
printf(“write wren ok.\n”);
index = 0;
//wrBuf[index++] = FM_OPT_WREN; //fail
wrBuf[index++] = FM_OPT_WRIT;
wrBuf[index++] = (gOptAddr》》8)&0xff;
wrBuf[index++] = (gOptAddr》》0)&0xff;
srand((unsigned)time(NULL));
printf(“write data = \n”);
if(tmpSize){
for(i = 0; i 《 tmpSize; i++){
rdBuf[i] = 1 + rand()%100+1;
printf(“%02x ”, rdBuf[i]);
}
}
else{
tmpSize = 128;
for(i = 0; i 《 tmpSize; i++){
rdBuf[i] = 0;
printf(“%02x ”, rdBuf[i]);
}
}
printf(“\n”);
memcpy(&wrBuf[index], rdBuf,tmpSize);
index += tmpSize;
ret = write(fd, wrBuf, index);
if(ret 《 0 )
printf(“write error.err = %d\n”, ret);
else
printf(“write ok.ret = %d\n”, ret);
memset(rdBuf, 0, sizeof(rdBuf));
index = 0;
wrBuf[index++] = FM_OPT_ERAD;
wrBuf[index++] = (gOptAddr》》8)&0xff;
wrBuf[index++] = (gOptAddr》》0)&0xff;
ret = spi_write_read(fd, wrBuf, index, rdBuf, sizeof(rdBuf));
if(ret 《 0 )
printf(“read error.err = %d\n”, ret);
else
printf(“read ok.ret = %d\n”, ret);
#if 0 //-- fail
ret = write(fd, wrBuf, index);
ret = read(fd, rdBuf, 100);
if(ret 《 0 )
printf(“read error.err = %d\n”, ret);
else
printf(“read wren ok.ret = %d\n”, ret);
#endif
printf(“data = \n”);
for(index = 0; index 《 sizeof(rdBuf); index++){
printf(“%02x ”, rdBuf[index]);
}
printf(“\n”);
#endif
//********************************************************************************/
close(fd);
return 0;
}
全部0条评论
快来发表一下你的评论吧 !