hpw421初始版本

This commit is contained in:
xushaoxiang
2026-06-06 16:49:48 +08:00
commit 2339bbfd1f
3283 changed files with 860261 additions and 0 deletions
@@ -0,0 +1,274 @@
/*!
* \file msc_flash.c
*
* \brief Target fatfs flash implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "msc_flash.h"
/*------------------------------------------------------------------------------------
Local Variables
-------------------------------------------
-----------------------------------------*/
uint8_t spim_init = false;
#define FLASH_SECTOR_ALIGN(address) \
((uint32_t)(address) & ~(FAT_FLASH_SECTOR_SIZE - 1))
#define FLASH_SECTOR_OFFSET(address) \
((uint32_t)(address) & (FAT_FLASH_SECTOR_SIZE - 1))
static uint8_t dataBuffer[FAT_FLASH_SECTOR_SIZE] = {0};
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------
-----------------------------------------*/
/**
* @brief FatFs flash read id
* @param void
* @retval uint32_t - id
*/
uint32_t Fat_Flash_ReadID(void)
{
uint32_t id = 0;
DEBUG("id\r");
if (XR_OK == spi_flash_rdid(XC_SPI0, (uint8_t *)&id)) {
DEBUG("read id :0x%08x\r\n", id);
return id;
}
return id;
}
/**
* @brief FatFs flash chip erase
* @param void
* @retval bool - true or false
*/
bool Fat_Flash_ChipErase(void)
{
uint32_t offset_cnt =
(FAT_FLASH_END_ADDR - FAT_FLASH_START_ADDR + 1) / FAT_FLASH_SECTOR_SIZE;
for (uint32_t i = 0; i < offset_cnt; i++) {
if (XR_OK !=
xc_spi_flash_erase_sector(XC_SPI0, FAT_FLASH_START_ADDR +
i * FAT_FLASH_SECTOR_SIZE))
return false;
}
return true;
}
/**
* @brief FatFs flash sector erase
* @param void
* @retval bool - true or false
*/
bool Fat_Flash_SectorErase(uint32_t address)
{
if (XR_OK == xc_spi_flash_erase_sector(XC_SPI0, address))
return true;
return false;
}
/**
* @brief FatFs flash write data
* @param void
* @retval bool - true or false
*/
bool Fat_Flash_WriteData(uint32_t address, uint8_t *buffer, uint16_t length)
{
uint16_t writeLen, pageOff;
while (length > 0) {
pageOff = FLASH_PAGE_SIZE - (address % FLASH_PAGE_SIZE);
writeLen = length > pageOff ? pageOff : length;
if (XR_OK == spi_write_bytes(XC_SPI0, address, buffer, writeLen)) {
length -= writeLen;
address += writeLen;
buffer += writeLen;
} else
return false;
}
return true;
}
/**
* @brief FatFs flash read data
* @param void
* @retval bool - true or false
*/
bool Fat_Flash_ReadData(uint32_t address, uint8_t *buffer, uint16_t length)
{
if (XR_OK == spi_flash_read(XC_SPI0, address, buffer, length))
return true;
return false;
}
/**
* @brief FatFs flash data are write in units of 512 bytes
* @param void
* @retval bool - true or false
*/
bool Fat_Flash_Write_512B(uint32_t address, uint8_t count, uint8_t *data)
{
uint32_t sectorAddress;
uint32_t sectorOffset;
address += FAT_FLASH_START_ADDR;
if (address >= FAT_FLASH_END_ADDR)
return false;
// count *= 2;
while (count > 0) {
sectorAddress = FLASH_SECTOR_ALIGN(address);
sectorOffset = FLASH_SECTOR_OFFSET(address);
Fat_Flash_ReadData(sectorAddress, dataBuffer, FAT_FLASH_SECTOR_SIZE);
Fat_Flash_SectorErase(address);
while (count > 0) {
memcpy(dataBuffer + sectorOffset, data, DISK_UNIT_SIZE);
data += DISK_UNIT_SIZE;
address += DISK_UNIT_SIZE;
count -= 1;
sectorOffset += DISK_UNIT_SIZE;
if ((sectorOffset & (FAT_FLASH_SECTOR_SIZE - 1)) == 0) {
break;
}
}
if (false == Fat_Flash_WriteData(sectorAddress, dataBuffer,
FAT_FLASH_SECTOR_SIZE))
return false;
}
return true;
}
/**
* @brief FatFs flash data are read in units of 512 bytes
* @param void
* @retval bool - true or false
*/
bool Fat_Flash_Read_512B(uint32_t address, uint8_t count, uint8_t *data)
{
address += FAT_FLASH_START_ADDR;
if (address >= FAT_FLASH_END_ADDR)
return false;
// count *= 2;
while (count--) {
if (true == Fat_Flash_ReadData(address, data, (DISK_UNIT_SIZE))) {
data += (DISK_UNIT_SIZE);
address += (DISK_UNIT_SIZE);
} else {
return false;
}
}
return true;
}
/*----------------------------- Disk Map -----------------------------------*/
/**
* @brief Disk init
* @param void
* @retval bool - true or false
*/
bool Disk_Init(void)
{
if (spim_init == false) {
SPI_Flash_Init();
spim_init = true;
}
return true;
}
/**
* @brief Disk erase
* @param void
* @retval bool - true or false
*/
bool Disk_Erase(void)
{
if (true == Fat_Flash_ChipErase())
return true;
return false;
}
/**
* @brief Disk unit size
* @param void
* @retval uint16_t - size
*/
uint16_t Disk_UnitSize(void) { return DISK_UNIT_SIZE; }
/**
* @brief Disk unit count
* @param void
* @retval uint32_t - count
*/
uint32_t Disk_UnitCount(void)
{
uint32_t count = 0;
count = (FAT_FLASH_END_ADDR - FAT_FLASH_START_ADDR + 1) /
FAT_FLASH_PAGE_SIZE / 2;
return count;
}
/**
* @brief Disk unit data read
* @param void
* @retval bool - true or false
*/
bool Disk_UnitRead(uint32_t unitAddr, uint16_t unitCount, uint8_t *data)
{
if (true == Fat_Flash_Read_512B(unitAddr * 512, unitCount, data))
return true;
return false;
}
/**
* @brief Disk unit data write
* @param void
* @retval bool - true or false
*/
bool Disk_UnitWrite(uint32_t unitAddr, uint16_t unitCount, uint8_t *data)
{
if (true == Fat_Flash_Write_512B(unitAddr * 512, unitCount, data))
return true;
return false;
}
/*--------------------------------------------------------------------------*/
@@ -0,0 +1,72 @@
/*!
* \file msc_flash.h
*
* \brief Target msc flash implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MSC_FLASH_H__
#define __MSC_FLASH_H__
#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "xc6xxx_hal_spi.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
#define FAT_FLASH_ID 0x12408500 //0x00136085
#define FAT_FLASH_PAGE_SIZE 256U
#define FAT_FLASH_SECTOR_SIZE 4096u
#define FAT_FLASH_START_ADDR 0x20000 //0x40000
#define FAT_FLASH_END_ADDR 0x40000 //0x7FFFF
#define DISK_UNIT_SIZE 512U
/*------------------------------------------------------------------------------------
Exported Functions
------------------------------------------- -----------------------------------------*/
uint32_t Fat_Flash_ReadID( void );
bool Disk_Init( void );
bool Disk_Erase( void );
uint16_t Disk_UnitSize( void );
uint32_t Disk_UnitCount( void );
bool Disk_UnitRead( uint32_t unitAddr, uint16_t unitCount, uint8_t *data );
bool Disk_UnitWrite( uint32_t unitAddr, uint16_t unitCount, uint8_t *data );
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
@@ -0,0 +1,71 @@
/*!
* \file usb_device.c
*
* \brief Target usb device implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "usb_device.h"
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_msc.h"
#include "usbd_storage_if.h"
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/* USB Device Core handle declaration. */
USBD_HandleTypeDef hUsbDeviceFS;
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
/**
* @brief This function handles USB On The Go FS global interrupt.
* @param void
* @retval void
*/
void USB_Handler(void) { HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS); }
/**
* @brief Init USB device Library, add supported class and start the library
* @param void
* @retval void
*/
void USB_DEVICE_Init(void)
{
/* Init Device Library, add supported class and start the library. */
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK) {
Error_Handler();
}
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_MSC) != USBD_OK) {
Error_Handler();
}
if (USBD_MSC_RegisterStorage(&hUsbDeviceFS,
&USBD_Storage_Interface_fops_FS) != USBD_OK) {
Error_Handler();
}
if (USBD_Start(&hUsbDeviceFS) != USBD_OK) {
Error_Handler();
}
}
@@ -0,0 +1,47 @@
/*!
* \file usb_device.h
*
* \brief Target usb device implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USB_DEVICE_H__
#define __USB_DEVICE_H__
#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "usbd_def.h"
/*------------------------------------------------------------------------------------
Exported Functions
-------------------------------------------------------------------------------------*/
void USB_DEVICE_Init( void );
#ifdef __cplusplus
}
#endif
#endif /* __USB_DEVICE_H__ */
@@ -0,0 +1,358 @@
/*!
* \file usbd_desc.c
*
* \brief Target the USB device descriptors implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "usbd_desc.h"
#include "usbd_conf.h"
#include "usbd_core.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------
-----------------------------------------*/
/** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
* @brief Private defines.
* @{
*/
#define USBD_VID 0x0483 // 1155
#define USBD_LANGID_STRING 0x0409 // 1033
#define USBD_MANUFACTURER_STRING "XinChip" //"XinChip"
#define USBD_PID_FS 22314 // 17788
#define USBD_PRODUCT_STRING_FS "XinChip Mass Storage" //"XinChip Mass Storage"
#define USBD_CONFIGURATION_STRING_FS "MSC Config"
#define USBD_INTERFACE_STRING_FS "MSC Interface"
#define USB_SIZ_BOS_DESC 0x0C
/*------------------------------------------------------------------------------------
Func Prototypes
-------------------------------------------
-----------------------------------------*/
/** @defgroup USBD_DESC_Private_FunctionPrototypes
* USBD_DESC_Private_FunctionPrototypes
* @brief Private functions declaration.
* @{
*/
static void Get_SerialNum(void);
static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len);
/** @defgroup USBD_DESC_Private_FunctionPrototypes
* USBD_DESC_Private_FunctionPrototypes
* @brief Private functions declaration for FS.
* @{
*/
uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed,
uint16_t *length);
uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed,
uint16_t *length);
uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
uint16_t *length);
#if (USBD_LPM_ENABLED == 1)
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
#endif /* (USBD_LPM_ENABLED == 1) */
/*------------------------------------------------------------------------------------
Local Variables
-------------------------------------------------------------------------------------*/
/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
* @brief Private variables.
* @{
*/
USBD_DescriptorsTypeDef FS_Desc = {USBD_FS_DeviceDescriptor,
USBD_FS_LangIDStrDescriptor,
USBD_FS_ManufacturerStrDescriptor,
USBD_FS_ProductStrDescriptor,
USBD_FS_SerialStrDescriptor,
USBD_FS_ConfigStrDescriptor,
USBD_FS_InterfaceStrDescriptor
#if (USBD_LPM_ENABLED == 1)
,
USBD_FS_USR_BOSDescriptor
#endif /* (USBD_LPM_ENABLED == 1) */
};
#if defined(__ICCARM__) /* IAR Compiler */
#pragma data_alignment = 4
#endif /* defined ( __ICCARM__ ) */
/** USB standard device descriptor. */
__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
0x12, /*bLength */
USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
#if (USBD_LPM_ENABLED == 1)
0x01,
/*bcdUSB */ /* changed to USB version 2.01
in order to support LPM L1 suspend
resume test of USBCV3.0*/
#else
0x00, /*bcdUSB */
#endif /* (USBD_LPM_ENABLED == 1) */
0x02,
0x00, /*bDeviceClass*/
0x00, /*bDeviceSubClass*/
0x00, /*bDeviceProtocol*/
USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
LOBYTE(USBD_VID), /*idVendor*/
HIBYTE(USBD_VID), /*idVendor*/
LOBYTE(USBD_PID_FS), /*idProduct*/
HIBYTE(USBD_PID_FS), /*idProduct*/
0x00, /*bcdDevice rel. 2.00*/
0x02,
USBD_IDX_MFC_STR, /*Index of manufacturer string*/
USBD_IDX_PRODUCT_STR, /*Index of product string*/
USBD_IDX_SERIAL_STR, /*Index of serial number string*/
USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
};
/* USB_DeviceDescriptor */
/** BOS descriptor. */
#if (USBD_LPM_ENABLED == 1)
#if defined(__ICCARM__) /* IAR Compiler */
#pragma data_alignment = 4
#endif /* defined ( __ICCARM__ ) */
__ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END = {
0x5, USB_DESC_TYPE_BOS, 0xC, 0x0, 0x1, /* 1 device capability*/
/* device capability*/
0x7, USB_DEVICE_CAPABITY_TYPE, 0x2, 0x2, /* LPM capability bit set*/
0x0, 0x0, 0x0};
#endif /* (USBD_LPM_ENABLED == 1) */
/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
* @brief Private variables.
* @{
*/
#if defined(__ICCARM__) /* IAR Compiler */
#pragma data_alignment = 4
#endif /* defined ( __ICCARM__ ) */
/** USB lang identifier descriptor. */
__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
USB_LEN_LANGID_STR_DESC, USB_DESC_TYPE_STRING, LOBYTE(USBD_LANGID_STRING),
HIBYTE(USBD_LANGID_STRING)};
#if defined(__ICCARM__) /* IAR Compiler */
#pragma data_alignment = 4
#endif /* defined ( __ICCARM__ ) */
/* Internal string descriptor. */
__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
#if defined(__ICCARM__) /*!< IAR Compiler */
#pragma data_alignment = 4
#endif
__ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
USB_SIZ_STRING_SERIAL,
USB_DESC_TYPE_STRING,
};
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
/**
* @brief Return the device descriptor
* @param speed : Current device speed
* @param length : Pointer to data length variable
* @retval Pointer to descriptor buffer
*/
uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED(speed);
*length = sizeof(USBD_FS_DeviceDesc);
return USBD_FS_DeviceDesc;
}
/**
* @brief Return the LangID string descriptor
* @param speed : Current device speed
* @param length : Pointer to data length variable
* @retval Pointer to descriptor buffer
*/
uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED(speed);
*length = sizeof(USBD_LangIDDesc);
return USBD_LangIDDesc;
}
/**
* @brief Return the product string descriptor
* @param speed : Current device speed
* @param length : Pointer to data length variable
* @retval Pointer to descriptor buffer
*/
uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
if (speed == 0) {
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
} else {
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
}
return USBD_StrDesc;
}
/**
* @brief Return the manufacturer string descriptor
* @param speed : Current device speed
* @param length : Pointer to data length variable
* @retval Pointer to descriptor buffer
*/
uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed,
uint16_t *length)
{
UNUSED(speed);
USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
return USBD_StrDesc;
}
/**
* @brief Return the serial number string descriptor
* @param speed : Current device speed
* @param length : Pointer to data length variable
* @retval Pointer to descriptor buffer
*/
uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
UNUSED(speed);
*length = USB_SIZ_STRING_SERIAL;
/* Update the serial number string descriptor with the data from the unique
* ID */
Get_SerialNum();
/* USER CODE BEGIN USBD_FS_SerialStrDescriptor */
/* USER CODE END USBD_FS_SerialStrDescriptor */
return (uint8_t *)USBD_StringSerial;
}
/**
* @brief Return the configuration string descriptor
* @param speed : Current device speed
* @param length : Pointer to data length variable
* @retval Pointer to descriptor buffer
*/
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
if (speed == USBD_SPEED_HIGH) {
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc,
length);
} else {
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc,
length);
}
return USBD_StrDesc;
}
/**
* @brief Return the interface string descriptor
* @param speed : Current device speed
* @param length : Pointer to data length variable
* @retval Pointer to descriptor buffer
*/
uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
uint16_t *length)
{
if (speed == 0) {
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
length);
} else {
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
length);
}
return USBD_StrDesc;
}
#if (USBD_LPM_ENABLED == 1)
/**
* @brief Return the BOS descriptor
* @param speed : Current device speed
* @param length : Pointer to data length variable
* @retval Pointer to descriptor buffer
*/
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{
INFO("\n");
UNUSED(speed);
*length = sizeof(USBD_FS_BOSDesc);
return (uint8_t *)USBD_FS_BOSDesc;
}
#endif /* (USBD_LPM_ENABLED == 1) */
/**
* @brief Create the serial number string descriptor
* @param None
* @retval None
*/
static void Get_SerialNum(void)
{
uint32_t deviceserial0, deviceserial1; //, deviceserial2;
// deviceserial0 = *(uint32_t *) DEVICE_ID1;
// deviceserial1 = *(uint32_t *) DEVICE_ID2;
// deviceserial2 = *(uint32_t *) DEVICE_ID3;
deviceserial0 = 0x20220902; // 5177440; //0;//5177402;
deviceserial1 = 0x1A0000; // 1395675155; //0x001A0000;
// deviceserial2 = //540488500; //0;
// deviceserial0 += deviceserial2;
if (deviceserial0 != 0) {
IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
}
}
/**
* @brief Convert Hex 32Bits value into char
* @param value: value to convert
* @param pbuf: pointer to the buffer
* @param len: buffer length
* @retval None
*/
static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len)
{
uint8_t idx = 0;
for (idx = 0; idx < len; idx++) {
if (((value >> 28)) < 0xA) {
pbuf[2 * idx] = (value >> 28) + '0';
} else {
pbuf[2 * idx] = (value >> 28) + 'A' - 10;
}
value = value << 4;
pbuf[2 * idx + 1] = 0;
}
}
/**
* @}
*/
@@ -0,0 +1,70 @@
/*!
* \file usbd_desc.h
*
* \brief Target the USB device descriptors implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USBD_DESC__C__
#define __USBD_DESC__C__
#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "usbd_def.h"
/*------------------------------------------------------------------------------------
Macros
------------------------------------------- -----------------------------------------*/
/** @defgroup USBD_DESC_Exported_Constants USBD_DESC_Exported_Constants
* @brief Constants.
* @{
*/
#define DEVICE_ID1 (UID_BASE)
#define DEVICE_ID2 (UID_BASE + 0x4)
#define DEVICE_ID3 (UID_BASE + 0x8)
#define USB_SIZ_STRING_SERIAL 0x1A
/**
* @}
*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/** Descriptor for the Usb device. */
extern USBD_DescriptorsTypeDef FS_Desc;
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBD_DESC__C__ */
@@ -0,0 +1,242 @@
/*!
* \file usbd_storage_if.c
*
* \brief Memory management layer.
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "usbd_storage_if.h"
#include "msc_flash.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------
-----------------------------------------*/
/** @defgroup USBD_STORAGE_Private_Defines
* @brief Private defines.
* @{
*/
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x48
#define STORAGE_BLK_SIZ 0x200
/*------------------------------------------------------------------------------------
Consts
-------------------------------------------
-----------------------------------------*/
/* USER CODE BEGIN INQUIRY_DATA_FS */
/** USB Mass storage Standard Inquiry Data. */
const int8_t STORAGE_Inquirydata_FS[] = {
/* 36 */
/* LUN 0 */
0x00, 0x80, 0x02, 0x02, (STANDARD_INQUIRY_DATA_LEN - 5),
0x00, 0x00, 0x00, 'X', 'i',
'n', 'C', 'h', 'i', 'p',
' ', /* Manufacturer : 8 bytes */
'P', 'r', 'o', 'd', 'u',
'c', 't', ' ', /* Product : 16 Bytes */
' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', '0', '.',
'0', '1' /* Version : 4 Bytes */
};
/**
* @}
*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/** @defgroup USBD_STORAGE_Exported_Variables
* @brief Public variables.
* @{
*/
extern USBD_HandleTypeDef hUsbDeviceFS;
/**
* @}
*/
/*------------------------------------------------------------------------------------
Func Prototype
-------------------------------------------------------------------------------------*/
/** @defgroup USBD_STORAGE_Private_FunctionPrototypes
* @brief Private functions declaration.
* @{
*/
static int8_t STORAGE_Init_FS(uint8_t lun);
static int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num,
uint16_t *block_size);
static int8_t STORAGE_IsReady_FS(uint8_t lun);
static int8_t STORAGE_IsWriteProtected_FS(uint8_t lun);
static int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr,
uint16_t blk_len);
static int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr,
uint16_t blk_len);
static int8_t STORAGE_GetMaxLun_FS(void);
/**
* @}
*/
USBD_StorageTypeDef USBD_Storage_Interface_fops_FS = {
STORAGE_Init_FS, STORAGE_GetCapacity_FS,
STORAGE_IsReady_FS, STORAGE_IsWriteProtected_FS,
STORAGE_Read_FS, STORAGE_Write_FS,
STORAGE_GetMaxLun_FS, (int8_t *)STORAGE_Inquirydata_FS};
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
/**
* @brief Initializes the storage unit (medium) over USB FS IP
* @param lun: Logical unit number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Init_FS(uint8_t lun)
{
UNUSED(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 USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num,
uint16_t *block_size)
{
UNUSED(lun);
#if STORAGE_NO_FLASH
// STORAGE_BLK_NBR;
// STORAGE_BLK_SIZ;
*block_num = STORAGE_BLK_NBR;
*block_size = STORAGE_BLK_SIZ;
#else
*block_num = Disk_UnitCount();
*block_size = Disk_UnitSize();
#endif
return (USBD_OK);
}
/**
* @brief Checks whether the medium is ready.
* @param lun: Logical unit number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_IsReady_FS(uint8_t lun)
{
UNUSED(lun);
return (USBD_OK);
}
/**
* @brief Checks whether the medium is write protected.
* @param lun: Logical unit number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
{
UNUSED(lun);
return (USBD_OK);
}
/**
* @brief Reads data from the medium.
* @param lun: Logical unit number.
* @param buf: data buffer.
* @param blk_addr: Logical block address.
* @param blk_len: Blocks number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr,
uint16_t blk_len)
{
UNUSED(lun);
#if STORAGE_NO_FLASH
UNUSED(buf);
UNUSED(blk_addr);
UNUSED(blk_len);
// memcpy(buf, temp + (blk_addr * STORAGE_BLK_SIZ), blk_len *
// STORAGE_BLK_SIZ);
#else
Disk_UnitRead(blk_addr, blk_len, buf);
#endif
return (USBD_OK);
}
/**
* @brief Writes data into the medium.
* @param lun: Logical unit number.
* @param buf: data buffer.
* @param blk_addr: Logical block address.
* @param blk_len: Blocks number.
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr,
uint16_t blk_len)
{
UNUSED(lun);
#if STORAGE_NO_FLASH
UNUSED(buf);
UNUSED(blk_addr);
UNUSED(blk_len);
// memcpy(temp + (blk_addr * STORAGE_BLK_SIZ), buf, blk_len *
// STORAGE_BLK_SIZ);
#else
Disk_UnitWrite(blk_addr, blk_len, buf);
#endif
return (USBD_OK);
}
/**
* @brief Returns the Max Supported LUNs.
* @param None
* @retval Lun(s) number.
*/
int8_t STORAGE_GetMaxLun_FS(void) { return (STORAGE_LUN_NBR - 1); }
/**
* @}
*/
@@ -0,0 +1,73 @@
/*!
* \file usbd_storage_if.h
*
* \brief Header for usbd_storage_if.c file.
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USBD_STORAGE_IF_H__
#define __USBD_STORAGE_IF_H__
#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "usbd_msc.h"
/*------------------------------------------------------------------------------------
Macros
------------------------------------------- -----------------------------------------*/
/** @defgroup USBD_STORAGE_Exported_Defines USBD_STORAGE_Exported_Defines
* @brief Defines.
* @{
*/
#define STORAGE_NO_FLASH 0U
/**
* @}
*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/** @defgroup USBD_STORAGE_Exported_Variables USBD_STORAGE_Exported_Variables
* @brief Public variables.
* @{
*/
/** STORAGE Interface callback. */
extern USBD_StorageTypeDef USBD_Storage_Interface_fops_FS;
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBD_STORAGE_IF_H__ */