[ STM32F4-discovery开发板测评 ] U盘读写功能测试
[复制链接]
STM32F4-discovery开发板提供了对U盘读写的支持,利用该性能可有效地扩展存储空间。
这里是将U盘的读写功能与LCD屏显示结合在一起,以便实现后续的数码相框功能或是进行采集数据的实时记录等。
关于LCD屏显示驱动的内容参见:
https://bbs.eeworld.com.cn/thread-1258164-1-1.html
实现功能测试的主程序为:
int main(void)
{
HAL_Init();
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
lcd_gpio();
Lcd_Init();
BACK_COLOR=RED;
LCD_Clear(RED);
LCD_ShowString(20,5,"STM32F429",YELLOW);
LCD_ShowString(20,25,"0.96'LCD",YELLOW);
LCD_ShowString(20,50,"jinglixixi",YELLOW);
HAL_Delay(2000);
LCD_Clear(RED);
LCD_ShowString(20,0,"USB Disk Test",YELLOW);
SystemClock_Config();
if(FATFS_LinkDriver(&USBH_Driver, USBDISKPath) == 0)
{
USBH_Init(&hUSBHost, USBH_UserProcess, 0);
USBH_RegisterClass(&hUSBHost, USBH_MSC_CLASS);
USBH_Start(&hUSBHost);
while (1)
{
USBH_Process(&hUSBHost);
switch(Appli_state)
{
case APPLICATION_START:
MSC_Application();
Appli_state = APPLICATION_IDLE;
break;
case APPLICATION_IDLE:
default:
break;
}
}
}
while (1);
}
进行U盘读写测试及信息显示的函数为:
static void MSC_Application(void)
{
FRESULT res;
uint32_t byteswritten, bytesread;
uint8_t wtext[] = "This is STM32 working with FatFs";
uint8_t rtext[100];
if(f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 0) != FR_OK)
{
Error_Handler();
}
else
{
LCD_ShowString(20,20,"Open STM32.TXT ",YELLOW);
if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
Error_Handler();
}
else
{
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
//LCD_ShowString(20,40,"Write data ",YELLOW);
if((byteswritten == 0) || (res != FR_OK))
{
Error_Handler();
}
else
{
f_close(&MyFile);
if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
{
Error_Handler();
}
else
{
res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
//LCD_ShowString(20,60,"Read data ",YELLOW);
if((bytesread == 0) || (res != FR_OK))
{
Error_Handler();
}
else
{
LCD_ShowString(20,40,rtext,YELLOW);
f_close(&MyFile);
//LCD_ShowString(20,60,"Close file ",YELLOW);
if((bytesread != byteswritten))
{
Error_Handler();
}
else
{
BSP_LED_On(LED3);
}
}
}
}
}
}
FATFS_UnLinkDriver(USBDISKPath);
}
经程序编译与下载,其测试效果如图1至图4所示。
图1 未连接U盘状态
图2接入U盘状态
图3显示测试过程
图4显示读取内容
|