/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file ux_device_msc.c * @author MCD Application Team * @brief USBX Device applicative file ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "ux_device_msc.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "main.h" #include "W25Qxx.h" #include "w25qxxx.h" #include "fatfs.h" #include "file_handling.h" #include "ff.h" // FatFS library #include "diskio.h" // Disk I/O functions (usually included in the FatFS library) /* 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 */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* 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); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 1); /* 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_Activate */ UX_PARAMETER_NOT_USED(storage_instance); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 1); /* USER CODE END USBD_STORAGE_Activate */ 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 */ HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); UX_PARAMETER_NOT_USED(storage_instance); UX_PARAMETER_NOT_USED(lun); /* UX_PARAMETER_NOT_USED(data_pointer); UX_PARAMETER_NOT_USED(number_blocks); UX_PARAMETER_NOT_USED(lba); */ UX_PARAMETER_NOT_USED(media_status); uint32_t blk_len = (uint32_t)(USBD_STORAGE_GetMediaBlocklength()); uint32_t addr = (uint32_t)(lba*blk_len); uint32_t num_bytes = (uint32_t)(number_blocks*blk_len); uint8_t stat = W25Qxxx_ReadBytes((uint8_t *)data_pointer, addr, num_bytes); if(stat == 0) HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); HAL_Delay(20); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0); HAL_Delay(20); /* USER CODE END USBD_STORAGE_Read */ return 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 */ UX_PARAMETER_NOT_USED(storage_instance); UX_PARAMETER_NOT_USED(lun); UX_PARAMETER_NOT_USED(data_pointer); UX_PARAMETER_NOT_USED(number_blocks); UX_PARAMETER_NOT_USED(lba); UX_PARAMETER_NOT_USED(media_status); /* USER CODE END USBD_STORAGE_Write */ return 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(number_blocks); 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(storage_instance); UX_PARAMETER_NOT_USED(lun); UX_PARAMETER_NOT_USED(media_id); UX_PARAMETER_NOT_USED(media_status); /* 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 */ // HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1); LastLba = (ULONG)(2*1024 - 1); /* USER CODE END USBD_STORAGE_GetMediaLastLba */ return LastLba; } /** * @brief USBD_STORAGE_GetMediaBlocklength * Get Media block length. * @param none. * @retval block length. */ ULONG USBD_STORAGE_GetMediaBlocklength(VOID) { ULONG MediaBlockLen = 0U; /* USER CODE BEGIN USBD_STORAGE_GetMediaBlocklength */ // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 1); MediaBlockLen = (ULONG)(2048); /* USER CODE END USBD_STORAGE_GetMediaBlocklength */ return MediaBlockLen; } /* USER CODE BEGIN 1 */ /* USER CODE END 1 */