前文:【易开嵌入式】rt-thread+stm32f407+nandflash,实现RL-FLASHFS文件系统移植
【易开嵌入式】rt-thread+stm32f407,实现RL-TCPnet网络协议栈
版权声明:本版面文章皆为原创、或参考其他技术网站、博客后自己动手做实验所得,转载请注明出处。
鸣谢:感谢eric硬汉
商务合作:lz_kwok@foxmail.com
易开嵌入式工作室
前面博文,针对rt-thread操作系统下,关于nandflash的文件系统和rl- tcpnet网络协议栈的适配做了说明。本文主要针对同样操作系统环境,使用spi接口的tf卡,说明如何适配rl-flashfs。
首先看下RL-ARM官网的对memory card的驱动说明:
可以看到,对于 spi接口的tf卡,只要实现上述函数接口即可。ok,针对开发板来进行驱动配置,首先配置spi驱动。
在rtt工程配置里,使能SPI2接口,并支持DMA操作。驱动如下:
void tf_Card_Init(void)
{
// if(SD_Ctrbus_Init == 0){
SD_Ctrbus_Init = 1;
__HAL_RCC_GPIOA_CLK_ENABLE();
rt_pin_mode(TF_CS_PIN,PIN_MODE_OUTPUT);
rt_pin_mode(TF_DET_PIN,PIN_MODE_INPUT);
rt_hw_spi_device_attach("spi2", "tfCard", GPIOA, GPIO_PIN_0);
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
cfg.max_hz = 20*400*1000;
spi_dev_microsd = (struct rt_spi_device *)rt_device_find(TF_SPI_DEVICE_NAME);
rt_spi_configure(spi_dev_microsd, &cfg);
// }
}
rt_uint8_t tf_Card_Send(rt_uint8_t cmd)
{
rt_uint8_t recb;
if(spi_dev_microsd != NULL){
rt_spi_transfer(spi_dev_microsd,&cmd,&recb,1);
}
return recb;
}
void tf_Card_SendBuf(rt_uint8_t *data,rt_uint32_t len)
{
if(spi_dev_microsd != NULL){
rt_spi_transfer(spi_dev_microsd,data,RT_NULL,len);
}
}
void tf_Card_RecBuf(rt_uint8_t *data,rt_uint32_t len)
{
if(spi_dev_microsd != NULL){
rt_spi_transfer(spi_dev_microsd,RT_NULL,data,len);
}
}
void tf_Card_BusSpeed(rt_uint32_t khz)
{
if(spi_dev_microsd != NULL){
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
cfg.max_hz = 20*khz*1000;
rt_spi_configure(spi_dev_microsd, &cfg);
}
}
对接口进行适配:
static BOOL Init (void);
static BOOL UnInit (void);
static U8 Send (U8 outb);
static BOOL SendBuf (U8 *buf, U32 sz);
static BOOL RecBuf (U8 *buf, U32 sz);
static BOOL BusSpeed (U32 kbaud);
static BOOL SetSS (U32 ss);
static U32 CheckMedia (void);
/*-----------------------------------------------------------------------------
NAND Device Driver Control Block
*----------------------------------------------------------------------------*/
const SPI_DRV spi0_drv = {
Init,
UnInit,
Send,
SendBuf,
RecBuf,
BusSpeed,
SetSS,
CheckMedia
};
static BOOL Init (void) {
tf_Card_Init();
return __TRUE;
}
static BOOL UnInit (void) {
return __TRUE;
}
static U8 Send(U8 outb){
U8 recb;
recb = tf_Card_Send(outb);
return recb;
}
static BOOL SendBuf(U8 *buf, U32 sz)
{
tf_Card_SendBuf(buf,sz);
return __TRUE;
}
static BOOL RecBuf(U8 *buf, U32 sz)
{
tf_Card_RecBuf(buf,sz);
return __TRUE;
}
static BOOL BusSpeed(U32 kbaud)
{
tf_Card_BusSpeed(kbaud);
return __TRUE;
}
static BOOL SetSS(U32 ss)
{
if(ss){
rt_pin_write(TF_CS_PIN, PIN_HIGH);
}
else
{
rt_pin_write(TF_CS_PIN, PIN_LOW);
}
return __TRUE;
}
static U32 CheckMedia(void)
{
U32 stat = 0;
if(rt_pin_read(TF_DET_PIN) == PIN_LOW){
stat |= M_INSERTED;
}else{
stat |= M_PROTECTED;
}
return stat;
}
至此,移植完成。可以尽情适配spi方式来操作tf卡了