/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "fatfs.h"
#include "u***_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, "%sn", "[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 */
}
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "fatfs.h"
#include "u***_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, "%sn", "[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 */
}
举报