马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
最近申请了富士通半导体的SPI 铁电片子MB85RS64,很快邮寄到了,给厂商赞一个! 看资料,铁电存储器吸收了Flash和RAM二者的优点于一身,耐擦写次数可达到10的12次方,写数据耗时也比Flash和EEPROM要少,片子在3.3V下操作,运行和待机功耗也做得很棒。 这次的方案是用M058S的IO模拟SPI,完成读写,看了资料,主要就是注意6个 OP-CODE在操作时完成的功能。
在读状态寄存器之前,先赋值0xab,arraydata[]是读取buffer,writedata[]是写入 buffer,之前,测试时是这样,写入,由于使能了写使能1次,然后连续写,发现只在 0x00地址处写入了0xaa,细读手册,发现,每次写之后,片子都会复位写使能,所以每次写之前都要使能写使能,有点拗口,之后的测试向地址0x00~0x04,写入0xaa~0xee ,再读出,与写入的一样,读写成功。
片子操作不复杂,使用起来容易上手,性能不错,值得推荐!! 源码如下:
uint8_t SpiRead(void) //主机在SCK下降沿,从MISO上读取1字节 { uint8_t i; for(i=0;i<8;i++) { DATA_BUF = DATA_BUF<<1;
MB85RS64_SCK = 1;
if(MB85RS64_MISO) { DATA_BUF+=1; } else { DATA_BUF+=0; }
MB85RS64_SCK = 0; } return DATA_BUF; }
void SpiWrite(uint8_t byte) //主机向MOSI写1字节,设备在SCK上升沿读取 { uint8_t i; DATA_BUF = byte; MB85RS64_SCK = 0; for(i=0;i<8;i++) { if(DATA_BUF >> 7) { MB85RS64_MOSI = 1; } else { MB85RS64_MOSI = 0; } #if 1 MB85RS64_SCK = 1; DATA_BUF = DATA_BUF << 1; MB85RS64_SCK = 0; #else MB85RS64_SCK = 0; MB85RS64_SCK = 1; DATA_BUF = DATA_BUF << 1; #endif
}
}
void SetWriteEnableLatch(void) //OP CODE: 0000 0110 { MB85RS64_CS = 0; SpiWrite(0x06); MB85RS64_CS = 1; }
voidResetWriteEnableLatch(void) //OP CODE: 0000 0100 { MB85RS64_CS = 0; SpiWrite(0x04); MB85RS64_CS = 1; }
uint8_tReadStatusRegister(void) //OP CODE: 0000 0101 { uint8_t rsr_byte;
MB85RS64_CS = 0;
SpiWrite(0x05);
rsr_byte = SpiRead();
MB85RS64_CS = 1;
return rsr_byte;
}
voidWriteStatusRegister(uint8_t wsr_byte) //OP CODE: 0000 0001 { uint8_t rsr_byte;
MB85RS64_CS = 0;
SpiWrite(0x01);
SpiWrite(wsr_byte);
MB85RS64_CS = 1; }
uint8_t ReadMemoryCode(uint16_taddr) //OP CODE: 0000 0011 { uint8_t rmc_byte; uint8_t addr_high, addr_low;
addr_high =(uint8_t)(addr&0xff00)>>8; addr_low = (uint8_t)(addr&0x00ff);
MB85RS64_CS = 0;
SpiWrite(0x03);
SpiWrite(addr_high); SpiWrite(addr_low);
rmc_byte = SpiRead();
MB85RS64_CS = 1;
return rmc_byte;
}
void WriteMemoryCode(uint16_taddr, uint8_t wmc_byte) //OP CODE: 0000 0010 { uint8_t addr_high, addr_low;
addr_high =(uint8_t)(addr&0xff00)>>8; addr_low = (uint8_t)(addr&0x00ff);
MB85RS64_CS = 0;
SpiWrite(0x02);
SpiWrite(addr_high); SpiWrite(addr_low);
SpiWrite(wmc_byte);
MB85RS64_CS = 1;
}
void FRAM_Operate(void) { uint8_t status = 0xab; uint8_t arraydata[5]; uint8_t writedata[5]; uint16_t i;
memcpy(writedata,"\xaa\xbb\xcc\xdd\xee", 5);
memcpy(arraydata,"\x01\x02\x03\x04\x05", 5);
TRACE("------- before Read ---------\r\n"); TRACE("status = 0x%02x",status); printbuf("arraydata",arraydata, sizeof(arraydata));
SetWriteEnableLatch();
status = ReadStatusRegister();
for(i=0;i<sizeof(arraydata);i++) { arraydata = ReadMemoryCode(i); }
TRACE("------- after Read ---------\r\n"); TRACE("status = 0x%02x",status); printbuf("arraydata",arraydata, sizeof(arraydata));
for(i=0;i<sizeof(writedata);i++) { SetWriteEnableLatch(); WriteMemoryCode(i,writedata);
}
TRACE("\r\n------- after Write writedata ---------"); printbuf("writedata",writedata, sizeof(writedata)); TRACE("------- after Read again ---------");
memset(arraydata, 0x00,sizeof(arraydata)); for(i=0;i<sizeof(arraydata);i++) { arraydata =ReadMemoryCode(i); }
printbuf("arraydata",arraydata, sizeof(arraydata));
}
既然是模拟SPI,涉及到的CS, MOSI,MISO, SCK可以自由设定了,注意MISO,设置成输入模式。(本文作者:springvirus)
|