问题描述: 本人的项目中要将一次不过100字节的少量数据存入U盘。最初用的是CH376芯片,后来发现STM32中有USB host资源的芯片比如105/107等都可以直接读写U盘,因此买来105的开发模块琢磨。 该开发模块比较简练(后面会附上原理图),附送的代码可以正常使用。 我找来微雪电子的例程,依样画葫芦,但出不来。 下面附上原理图,我的STM32CubeMX配置过程及源代码,请朋友们支招。 105芯片的PA11、PA12分别接USB口的USB D-和USB D+. J1正是U盘插口,+5V供电是由连接计算机USB口的J2提供的。105的PC9控制J1的+5V供电与否。 STM32CubeMX配置如上面的图。 |
#include "stm32f1xx_hal.h"
#include "fatfs.h"
#include "usb_host.h"
/* USER CODE BEGIN Includes */
#include "ff.h"
//#include "oled.h"
extern ApplicationTypeDef Appli_state;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
void MX_USB_HOST_Process(void);
/* USER CODE BEGIN PFP */
int main(void)
{
/* USER CODE BEGIN 1 */
static ApplicationTypeDef pre_state = APPLICATION_IDLE;
volatile FRESULT fr;
FATFS fs;
FIL fil;
UINT off = 0;
uint8_t buff[20];
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_FATFS_Init();
MX_USB_HOST_Init();
/* USER CODE BEGIN 2 */
// Enable USB Power
HAL_GPIO_WritePin(USB_POWER_GPIO_Port, USB_POWER_Pin, GPIO_PIN_RESET);
// Init OLED
// OLED_Init();
//OLED_Clear();
//OLED_ShowString(0,0,"[USB]Pls Insert Udisk!");
//HAL_Delay(2000);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
MX_USB_HOST_Process();
/* USER CODE BEGIN 3 */
if (pre_state != Appli_state) {
switch(Appli_state)
{
case APPLICATION_DISCONNECT:
/* Register the file system object to the FatFs module */
if(f_mount(NULL, "", 0) != FR_OK)
{
printf("ERROR : Cannot exit FatFs! \n");
}
// OLED_Clear();
// OLED_ShowString(0,0,"[USB]Disconnect!");
break;
case APPLICATION_READY:
// OLED_Clear();
// OLED_ShowString(0,0,"[USB]Connected!");
/* Open or create a log file and ready to append */
if(f_mount(&fs, "", 0) != FR_OK) {
break;
}
HAL_Delay(2000);
// OLED_ShowString(0,2,"[FAT]Open file!");
/* Opens an existing file. If not exist, creates a new file. */
fr = f_open(&fil, "0OG.TXT", FA_READ | FA_WRITE | FA_CREATE_ALWAYS);
if (fr != FR_OK)
break;
/* Append a line */
f_printf(&fil, "%s\n", "[USB]uDisk OK!0123456789");
HAL_Delay(2000);
// OLED_ShowString(0,4,"[FAT]Write OK!");
fr = f_lseek(&fil, 0);
if (fr != FR_OK) {
f_close(&fil);
break;
}
f_read(&fil, buff, 16, &off);
buff[14] = 0;
HAL_Delay(2000);
// OLED_ShowString(0,6,buff);
/* Close the file */
f_close(&fil);
HAL_Delay(2000);
// OLED_Clear();
// OLED_ShowString(0,0,"[FAT]Close file!");
break;
default:
break;
}
}
pre_state = Appli_state;
}
/* USER CODE END 3 */
}
嗯,是的!
我买的105的评估板 不是微雪的.
1、1个LED指示灯,1个电源指示灯
2、2个用户按键
3、串口ISP下载口(板载BOOT0下载按键)
4、JTAG/SWD仿真下载接口
5、外置FLASH芯片华邦W25Q80(8M-BIT)
6、WIFI接口,适配ESP8266模块
7、OLED显示屏接口,0.96寸
8、RTC时钟电路,含CR1220备用电池(锂锰电池 38mAh)
9、USB-Slave功能,可将STM32模拟成U盘
10、USB-Host功能,STM32读写U盘功能