/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : usb_host.c
* @version : v1.0_Cube
* @brief : This file implements the USB Host
******************************************************************************
* @attention
*
*
© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usb_host.h"
#include "usbh_core.h"
#include "usbh_msc.h"
/* USER CODE BEGIN Includes */
#include "fatfs.h"
#include
#include "stm32h7xx_hal.h"
#include
/** @addtogroup USBH_LIB
* @{
*/
FATFS myUsbFatFS;//FATFS variable
uint32_t size;
extern char USBHPath[4]; /* USBH logical drive path */
//extern void UsbTest_Read(void);
/* USER CODE END PTD */
bool UsbTest_write(void);
void UsbTest_Read(void);
//FILE IO Variables
FIL myFile;
FRESULT res;
UINT byteswritten,bytesread;
char rwtext[100];
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USB Host core handle declaration */
USBH_HandleTypeDef hUsbHostHS;
ApplicationTypeDef Appli_state = APPLICATION_IDLE;
/*
* -- Insert your variables declaration here --
*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/*
* user callback declaration
*/
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
/*
* -- Insert your external function declaration here --
*/
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/**
* Init USB host library, add supported class and start the library
* @retval None
*/
void MX_USB_HOST_Init(void)
{
/* USER CODE BEGIN USB_HOST_Init_PreTreatment */
/* USER CODE END USB_HOST_Init_PreTreatment */
/* Init host Library, add supported class and start the library. */
if (USBH_Init(&hUsbHostHS, USBH_UserProcess, HOST_HS) != USBH_OK)
{
Error_Handler();
}
if (USBH_RegisterClass(&hUsbHostHS, USBH_MSC_CLASS) != USBH_OK)
{
Error_Handler();
}
if (USBH_Start(&hUsbHostHS) != USBH_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USB_HOST_Init_PostTreatment */
/* USER CODE END USB_HOST_Init_PostTreatment */
}
/*
* Background task
*/
void MX_USB_HOST_Process(void)
{
/* USB Host Background task */
USBH_Process(&hUsbHostHS);
}
/*
* user callback definition
*/
static void USBH_UserProcess (USBH_HandleTypeDef *phost, uint8_t id)
{
/* USER CODE BEGIN CALL_BACK_1 */
switch(id)
{
case HOST_USER_SELECT_CONFIGURATION:
break;
case HOST_USER_DISCONNECTION:
Appli_state = APPLICATION_DISCONNECT;
break;
case HOST_USER_CLASS_ACTIVE:
Appli_state = APPLICATION_READY;
UsbTest_Read();
break;
case HOST_USER_CONNECTION:
Appli_state = APPLICATION_START;
if(f_mount(&myUsbFatFS, (TCHAR const*)USBHPath,0)==FR_OK)
{
printf("Ff-mount function\n\t");
// flag=1;
}
break;
default:
break;
}
/* USER CODE END CALL_BACK_1 */
}
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
bool UsbTest_write(void)
{
//Open and Created file for writing
if(f_open(&myFile,"LD.BIN",FA_CREATE_ALWAYS | FA_WRITE)!=FR_OK)
{
return 0;
}
else
{
//copy to string into the temporary variable
sprintf(rwtext ,"Froom MANOJ");
//write to text file
res= f_write(&myFile,(const void *)rwtext,strlen(rwtext),&byteswritten);
if( (res != FR_OK)|| (byteswritten==0) )
{
return 0;
}
f_close(&myFile);
return 1;
}
}
void UsbTest_Read(void)
{
res=f_open(&myFile, "FW.ABS", FA_READ);
if (res == FR_OK)
{
printf("File PRODUCT_FP2043T_V2_FW.ABS sucessfully opened!\n");
}
size = f_size(&myFile);
char * data = NULL;
data = malloc(size); /* allocate memory to store image data */
printf("File size: %lu bytes\n", size);
res = f_read(&myFile, data, (uint32_t) size, &bytesread);
if (res == FR_OK){
printf("File successfully read!\n");
printf("%d bytes read\n", bytesread);
for (int i=0; i < bytesread; i++)
{
printf("data[%d]: 0x%x\n", i, data[i]);
}
}
free(data); // free allocated memory when you don't need it
f_close(&myFile);
}