/* USER CODE BEGIN UX_Device_Memory_Buffer */
/* USER CODE END UX_Device_Memory_Buffer */
#if defined ( __ICCARM__ )
#pragma data_alignment=4
#endif
__ALIGN_BEGIN static UCHAR ux_device_byte_pool_buffer[UX_DEVICE_APP_MEM_POOL_SIZE] __ALIGN_END;
/* USER CODE BEGIN PV */
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
__ALIGN_BEGIN USB_MODE_STATE USB_Device_State_Msg __ALIGN_END;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
static UINT USBD_ChangeFunction(ULONG Device_State);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* USER CODE END MX_USBX_Device_Init0 */
pointer = ux_device_byte_pool_buffer;
/* Initialize USBX Memory */
if (ux_system_initialize(pointer, USBX_DEVICE_MEMORY_STACK_SIZE, UX_NULL, 0) != UX_SUCCESS)
{
/* USER CODE BEGIN USBX_SYSTEM_INITIALIZE_ERROR */
return UX_ERROR;
/* USER CODE END USBX_SYSTEM_INITIALIZE_ERROR */
}
/* Get Device Framework High Speed and get the length */
device_framework_high_speed = USBD_Get_Device_Framework_Speed(USBD_HIGH_SPEED,
&device_framework_hs_length);
/* Get Device Framework Full Speed and get the length */
device_framework_full_speed = USBD_Get_Device_Framework_Speed(USBD_FULL_SPEED,
&device_framework_fs_length);
/* Get String Framework and get the length */
string_framework = USBD_Get_String_Framework(&string_framework_length);
/* Get Language Id Framework and get the length */
language_id_framework = USBD_Get_Language_Id_Framework(&language_id_framework_length);
/* Install the device portion of USBX */
if (ux_device_stack_initialize(device_framework_high_speed,
device_framework_hs_length,
device_framework_full_speed,
device_framework_fs_length,
string_framework,
string_framework_length,
language_id_framework,
language_id_framework_length,
USBD_ChangeFunction) != UX_SUCCESS)
{
/* USER CODE BEGIN USBX_DEVICE_INITIALIZE_ERROR */
return UX_ERROR;
/* USER CODE END USBX_DEVICE_INITIALIZE_ERROR */
}
/* Initialize the storage class parameters for the device */
storage_parameter.ux_slave_class_storage_instance_activate = USBD_STORAGE_Activate;
storage_parameter.ux_slave_class_storage_instance_deactivate = USBD_STORAGE_Deactivate;
/* Store the number of LUN in this device storage instance */
storage_parameter.ux_slave_class_storage_parameter_number_lun = STORAGE_NUMBER_LUN;
/* Initialize the storage class parameters for reading/writing to the Flash Disk */
storage_parameter.ux_slave_class_storage_parameter_lun[0].
ux_slave_class_storage_media_last_lba = USBD_STORAGE_GetMediaLastLba();
/* Get storage configuration number */
storage_configuration_number = USBD_Get_Configuration_Number(CLASS_TYPE_MSC, 0);
/* Find storage interface number */
storage_interface_number = USBD_Get_Interface_Number(CLASS_TYPE_MSC, 0);
/* Initialize the device storage class */
if (ux_device_stack_class_register(_ux_system_slave_class_storage_name,
ux_device_class_storage_entry,
storage_configuration_number,
storage_interface_number,
&storage_parameter) != UX_SUCCESS)
{
/* USER CODE BEGIN USBX_DEVICE_STORAGE_REGISTER_ERROR */
return UX_ERROR;
/* USER CODE END USBX_DEVICE_STORAGE_REGISTER_ERROR */
}
/* USER CODE BEGIN MX_USBX_Device_Init1 */
USBX_APP_Device_Init();
/* USER CODE END MX_USBX_Device_Init1 */
return ret;
}
/**
* @brief _ux_utility_interrupt_disable
* USB utility interrupt disable.
* @param none
* @retval none
*/
ALIGN_TYPE _ux_utility_interrupt_disable(VOID)
{
UINT interrupt_save;
/* USER CODE BEGIN _ux_utility_interrupt_disable */
interrupt_save = __get_PRIMASK();
__disable_irq();
/* USER CODE END _ux_utility_interrupt_disable */
/* USER CODE BEGIN _ux_utility_interrupt_restore */
__set_PRIMASK(flags);
/* USER CODE END _ux_utility_interrupt_restore */
}
/**
* @brief _ux_utility_time_get
* Get Time Tick for host timing.
* @param none
* @retval time tick
*/
ULONG _ux_utility_time_get(VOID)
{
ULONG time_tick = 0U;
/* USER CODE BEGIN _ux_utility_time_get */
return HAL_GetTick();
/* USER CODE END _ux_utility_time_get */
return time_tick;
}
/**
* @brief USBD_ChangeFunction
* This function is called when the device state changes.
* @param Device_State: USB Device State
* @retval status
*/
static UINT USBD_ChangeFunction(ULONG Device_State)
{
UINT status = UX_SUCCESS;
/* USER CODE BEGIN USBD_ChangeFunction0 */
/* USER CODE END USBD_ChangeFunction0 */
switch (Device_State)
{
case UX_DEVICE_ATTACHED:
/* USER CODE BEGIN UX_DEVICE_ATTACHED */
printf("device_attached\r\n");
/* USER CODE END UX_DEVICE_ATTACHED */
break;
case UX_DEVICE_REMOVED:
/* USER CODE BEGIN UX_DEVICE_REMOVED */
printf("device_removed\r\n");
/* USER CODE END UX_DEVICE_REMOVED */
break;
case UX_DCD_STM32_DEVICE_CONNECTED:
/* USER CODE BEGIN UX_DCD_STM32_DEVICE_CONNECTED */
printf("device_connected\r\n");
/* USER CODE END UX_DCD_STM32_DEVICE_CONNECTED */
break;
case UX_DCD_STM32_DEVICE_DISCONNECTED:
/* USER CODE BEGIN UX_DCD_STM32_DEVICE_DISCONNECTED */
printf("device_disconnected\r\n");
/* USER CODE END UX_DCD_STM32_DEVICE_DISCONNECTED */
break;
case UX_DCD_STM32_DEVICE_SUSPENDED:
/* USER CODE BEGIN UX_DCD_STM32_DEVICE_SUSPENDED */
printf("device_suspended\r\n");
/* USER CODE END UX_DCD_STM32_DEVICE_SUSPENDED */
break;
case UX_DCD_STM32_DEVICE_RESUMED:
/* USER CODE BEGIN UX_DCD_STM32_DEVICE_RESUMED */
printf("device_resumed\r\n");
/* USER CODE END UX_DCD_STM32_DEVICE_RESUMED */
break;
case UX_DCD_STM32_SOF_RECEIVED:
/* USER CODE BEGIN UX_DCD_STM32_SOF_RECEIVED */
printf("device_received\r\n");
/* USER CODE END UX_DCD_STM32_SOF_RECEIVED */
break;
default:
/* USER CODE BEGIN DEFAULT */
/* USER CODE END DEFAULT */
break;
}
/* USER CODE BEGIN USBD_ChangeFunction1 */
/* USER CODE END USBD_ChangeFunction1 */
return status;
}
/* USER CODE BEGIN 1 */
VOID USBX_APP_Device_Init(VOID)
{
/* USER CODE BEGIN USB_Device_Init_PreTreatment_0 */
/* USER CODE END USB_Device_Init_PreTreatment_0 */
MX_USB_OTG_FS_PCD_Init();
/* USER CODE BEGIN USB_Device_Init_PreTreatment_1 */
HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_FS, 0x80);
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 0, USBD_MAX_EP0_SIZE / 4);
HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 1, USBD_MSC_EPIN_FS_MPS / 2);
/* USER CODE BEGIN USB_Device_Init_PostTreatment */
/* USER CODE END USB_Device_Init_PostTreatment */
}
/* USER CODE END 1 */
/* USER CODE BEGIN Header */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "ux_device_msc.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
extern HAL_SD_CardInfoTypeDef card_info;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//static UINT handle_scsi_commands(UCHAR *data_pointer, ULONG lba, ULONG number_blocks, ULONG lun);
/* USER CODE END 0 */
/**
* @brief USBD_STORAGE_Activate
* This function is called when insertion of a storage device.
* @param storage_instance: Pointer to the storage class instance.
* @retval none
*/
VOID USBD_STORAGE_Activate(VOID *storage_instance)
{
/* USER CODE BEGIN USBD_STORAGE_Activate */
UX_PARAMETER_NOT_USED(storage_instance);
/* USER CODE END USBD_STORAGE_Activate */
return;
}
/**
* @brief USBD_STORAGE_Deactivate
* This function is called when extraction of a storage device.
* @param storage_instance: Pointer to the storage class instance.
* @retval none
*/
VOID USBD_STORAGE_Deactivate(VOID *storage_instance)
{
/* USER CODE BEGIN USBD_STORAGE_Deactivate */
UX_PARAMETER_NOT_USED(storage_instance);
/* USER CODE END USBD_STORAGE_Deactivate */
return;
}
/**
* @brief USBD_STORAGE_Read
* This function is invoked to read from media.
* @param storage_instance : Pointer to the storage class instance.
* @param lun: Logical unit number is the command is directed to.
* @param data_pointer: Address of the buffer to be used for reading or writing.
* @param number_blocks: number of sectors to read/write.
* @param lba: Logical block address is the sector address to read.
* @param media_status: should be filled out exactly like the media status
* callback return value.
* @retval status
*/
UINT USBD_STORAGE_Read(VOID *storage_instance, ULONG lun, UCHAR *data_pointer,
ULONG number_blocks, ULONG lba, ULONG *media_status)
{
UINT status = UX_SUCCESS;
/* USER CODE BEGIN USBD_STORAGE_Read */
int8_t ret;
UX_PARAMETER_NOT_USED(storage_instance);
UX_PARAMETER_NOT_USED(media_status);
/**
* @brief USBD_STORAGE_Write
* This function is invoked to write in media.
* @param storage_instance : Pointer to the storage class instance.
* @param lun: Logical unit number is the command is directed to.
* @param data_pointer: Address of the buffer to be used for reading or writing.
* @param number_blocks: number of sectors to read/write.
* @param lba: Logical block address is the sector address to read.
* @param media_status: should be filled out exactly like the media status
* callback return value.
* @retval status
*/
UINT USBD_STORAGE_Write(VOID *storage_instance, ULONG lun, UCHAR *data_pointer,
ULONG number_blocks, ULONG lba, ULONG *media_status)
{
UINT status = UX_SUCCESS;
/* USER CODE BEGIN USBD_STORAGE_Write */
uint8_t ret;
UX_PARAMETER_NOT_USED(storage_instance);
UX_PARAMETER_NOT_USED(media_status);
/**
* @brief USBD_STORAGE_Flush
* This function is invoked to flush media.
* @param storage_instance : Pointer to the storage class instance.
* @param lun: Logical unit number is the command is directed to.
* @param number_blocks: number of sectors to read/write.
* @param lba: Logical block address is the sector address to read.
* @param media_status: should be filled out exactly like the media status
* callback return value.
* @retval status
*/
UINT USBD_STORAGE_Flush(VOID *storage_instance, ULONG lun, ULONG number_blocks,
ULONG lba, ULONG *media_status)
{
UINT status = UX_SUCCESS;
/* USER CODE BEGIN USBD_STORAGE_Flush */
UX_PARAMETER_NOT_USED(storage_instance);
UX_PARAMETER_NOT_USED(lun);
UX_PARAMETER_NOT_USED(lba);
UX_PARAMETER_NOT_USED(media_status);
/* USER CODE END USBD_STORAGE_Flush */
return status;
}
/**
* @brief USBD_STORAGE_Status
* This function is invoked to obtain the status of the device.
* @param storage_instance : Pointer to the storage class instance.
* @param lun: Logical unit number is the command is directed to.
* @param media_id: is not currently used.
* @param media_status: should be filled out exactly like the media status
* callback return value.
* @retval status
*/
UINT USBD_STORAGE_Status(VOID *storage_instance, ULONG lun, ULONG media_id,
ULONG *media_status)
{
UINT status = UX_SUCCESS;
/* USER CODE BEGIN USBD_STORAGE_Status */
UX_PARAMETER_NOT_USED(media_id);
UX_PARAMETER_NOT_USED(media_status);
// 返回设备就绪状态
*media_status = 0;
/* USER CODE END USBD_STORAGE_Status */
return status;
}
/**
* @brief USBD_STORAGE_Notification
* This function is invoked to obtain the notification of the device.
* @param storage_instance : Pointer to the storage class instance.
* @param lun: Logical unit number is the command is directed to.
* @param media_id: is not currently used.
* @param notification_class: specifies the class of notification.
* @param media_notification: response for the notification.
* @param media_notification_length: length of the response buffer.
* @retval status
*/
UINT USBD_STORAGE_Notification(VOID *storage_instance, ULONG lun, ULONG media_id,
ULONG notification_class, UCHAR **media_notification,
ULONG *media_notification_length)
{
UINT status = UX_SUCCESS;
/* USER CODE BEGIN USBD_STORAGE_Notification */
UX_PARAMETER_NOT_USED(storage_instance);
UX_PARAMETER_NOT_USED(lun);
UX_PARAMETER_NOT_USED(media_id);
UX_PARAMETER_NOT_USED(notification_class);
UX_PARAMETER_NOT_USED(media_notification);
UX_PARAMETER_NOT_USED(media_notification_length);
/* USER CODE END USBD_STORAGE_Notification */
return status;
}
/**
* @brief USBD_STORAGE_GetMediaLastLba
* Get Media last LBA.
* @param none
* @retval last lba
*/
ULONG USBD_STORAGE_GetMediaLastLba(VOID)
{
ULONG LastLba = 0U;
/* USER CODE BEGIN USBD_STORAGE_GetMediaLastLba */
LastLba = (ULONG)(card_info.BlockNbr - 1);
/* USER CODE END USBD_STORAGE_GetMediaLastLba */
/* USER CODE BEGIN USBD_STORAGE_GetMediaBlocklength */
MediaBlockLen = (ULONG)card_info.BlockSize;
/* USER CODE END USBD_STORAGE_GetMediaBlocklength */