/* Includes ------------------------------------------------------------------*/
#include "usbd_storage.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 64 //64 blocks * 512 = 32k
#define STORAGE_BLK_SIZ 512 //doesn't seem to work well with values other than 512
#define STORAGE_CAPACITY (STORAGE_BLK_NBR * STORAGE_BLK_SIZ)
#define WAIT_TIMEOUT 5000
uint8_t usbMassStorage[STORAGE_BLK_NBR][STORAGE_BLK_SIZ];
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* USB Mass storage Standard Inquiry Data */
int8_t STORAGE_Inquirydata[] = { /* 36 */
/* LUN 0 */
0x00,
0x80,
0x02,
0x02,
(STANDARD_INQUIRY_DATA_LEN - 5),
0x00,
0x00,
0x00,
'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer: 8 bytes */
'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
'0', '.', '0','1', /* Version : 4 Bytes */
};
extern USBD_HandleTypeDef USBD_Device;
/* Private function prototypes -----------------------------------------------*/
int8_t STORAGE_Init(uint8_t lun);
int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
int8_t STORAGE_IsReady(uint8_t lun);
int8_t STORAGE_IsWriteProtected(uint8_t lun);
int8_t STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
int8_t STORAGE_GetMaxLun(void);
USBD_StorageTypeDef USBD_DISK_fops = {
STORAGE_Init,
STORAGE_GetCapacity,
STORAGE_IsReady,
STORAGE_IsWriteProtected,
STORAGE_Read,
STORAGE_Write,
STORAGE_GetMaxLun,
STORAGE_Inquirydata,
};
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initializes the storage unit (medium)
* @param lun: Logical unit number
* @retval Status (0 : OK / -1 : Error)
*/
int8_t STORAGE_Init(uint8_t lun)
{
return (USBD_OK);
}
/**
* @brief Returns the medium capacity.
* @param lun: Logical unit number
* @param block_num: Number of total block number
* @param block_size: Block size
* @retval Status (0: OK / -1: Error)
*/
int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
*block_num = STORAGE_BLK_NBR;
*block_size = STORAGE_BLK_SIZ;
return (USBD_OK);
}
/**
* @brief Checks whether the medium is ready.
* @param lun: Logical unit number
* @retval Status (0: OK / -1: Error)
*/
int8_t STORAGE_IsReady(uint8_t lun)
{
return (USBD_OK);
}
/**
* @brief Checks whether the medium is write protected.
* @param lun: Logical unit number
* @retval Status (0: write enabled / -1: otherwise)
*/
int8_t STORAGE_IsWriteProtected(uint8_t lun)
{
return (USBD_OK);
}
/**
* @brief Reads data from the medium.
* @param lun: Logical unit number
* @param blk_addr: Logical block address
* @param blk_len: Blocks number
* @retval Status (0: OK / -1: Error)
*/
int8_t STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
uint32_t i, j;
switch(lun){
case 0:
for (i = 0; i < blk_len; i++){
for(j = 0; j < STORAGE_BLK_SIZ; j++){//512
buf[j] = usbMassStorage[(i + blk_addr)][j];
}
}
break;
case 1:
break;
default:
return (USBD_FAIL);
}
return (USBD_OK);
}
/**
* @brief Writes data into the medium.
* @param lun: Logical unit number
* @param blk_addr: Logical block address
* @param blk_len: Blocks number
* @retval Status (0 : OK / -1 : Error)
*/
int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
uint8_t i = 0;
uint16_t j = 0;
switch(lun){
case 0:
for (i = 0; i < blk_len; i++){
for(j = 0; j < STORAGE_BLK_SIZ; j++){//512
usbMassStorage[(i + blk_addr)][j] = buf[j + (i*STORAGE_BLK_SIZ)];
}
}
case 1:
break;
default:
return (USBD_FAIL);
}
return (USBD_OK);
}
/**
* @brief Returns the Max Supported LUNs.
* @param None
* @retval Lun(s) number
*/
int8_t STORAGE_GetMaxLun(void)
{
return(STORAGE_LUN_NBR - 1);
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
'초보의 아웅다웅 설계하기 > STM32' 카테고리의 다른 글
SPI Slave에서 입력 데이터 처리 (0) | 2019.09.30 |
---|---|
Multi Lun 사용시 설정 - Cube 사용시 (0) | 2019.07.28 |
CKS32F103 => STM의 카피캣을 만나다. (0) | 2019.07.08 |
STM Custom HID 보드로 입력되는 값 4Byte로 수정하기 (0) | 2019.07.02 |
J-Link를 사용하여 MCU내 라이팅되어 있는 메모리 확인하기 (0) | 2019.06.28 |