Use STM32cube to configure the relevant pins and enable the Mass Storage Class function of USB
The MSC_MEDIA_PACKET in USB_DEVICE can be set larger. Increasing the buffer can increase the speed. For external spi-flash, it should be larger than 4096 bytes.
Increase the stack size, otherwise USB will not work properly
usbd_storage_if.c add code:
#define FLASH_START_ADDR 0x08010000 //
#define FLASH_PAGE_NBR 90 // 30K
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
/* USER CODE BEGIN 3 */
if(lun == 0)
{
*block_num = FLASH_PAGE_NBR;
*block_size = FLASH_PAGE_SIZE;
}
return (USBD_OK);
/* USER CODE END 3 */
}
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 6 */
if(lun == 0)
{
memcpy(buf,(u8 *)(FLASH_START_ADDR + blk_addr*FLASH_PAGE_SIZE),blk_len*FLASH_PAGE_SIZE);
return USBD_OK;
}
return USBD_FAIL;
/* USER CODE END 6 */
}
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 7 */
if(lun == 0)
{
uint16_t i;
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef f;
f.TypeErase = FLASH_TYPEERASE_PAGES;
f.PageAddress = FLASH_START_ADDR + blk_addr*FLASH_PAGE_SIZE ;
f.NbPages = blk_len;
uint32_t PageError = 0;
HAL_FLASHEx_Erase(&f, &PageError);
for(i=0;i HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,FLASH_START_ADDR + blk_addr*FLASH_PAGE_SIZE + i , *(u32 *)(&buf[i])); HAL_FLASH_Lock(); return USBD_OK; } return USBD_FAIL; /* USER CODE END 7 */ } For the solution with pull-up resistor control on USB D+, add control in USB initialization
Previous article:STM32 HAL library study notes ------- (system clock configuration)
Next article:Combined with the firmware library to discuss STM32 read and write FLASH steps and HardFault problems
- Popular Resources
- Popular amplifiers
- AN5701 STM32Cube MCU package examples for STM32U5 series in Chinese
- AN5447 Overview of Secure Boot and Secure Firmware Update Solutions for STM32 Microcontrollers Based on Arm TrustZone
- (Chinese) Getting started with STM32CubeWL+ for the STM32WL series
- Introduction to porting STM32 standard peripheral library to STM32Cube
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University----UCD3138 Analog Front End (AFE) Module
- "Show goods" to come to a wave of commonly used development boards
- Measurement of the phase difference between a sine wave and a square wave
- [Sipeed LicheeRV 86 Panel Review] - 6 waft-ui component tests (3)
- Code size after keil compilation
- Tips for removing chip components on PCB
- Hardware System Engineer's Handbook
- Integrated operational amplifier practical circuit diagram
- For electronic hardware, there are many circuit structures, which can be said to be the units that make up a component. All circuits...
- 【Ended】Microchip Live|Wireless Power Consortium (WPC) compliant wireless charging authentication