当前位置:网站首页>[STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
[STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
2022-07-01 15:24:00 【Please call me Chang Sicong】
brief introduction
Use STM32USB Function configuration Mass Storage Class Storage media use w25qxx do U Disk time , Find a more interesting thing .
When changing storage media with different capacities, you need to change the program , Show different sizes .
change usbd_storage_if.c Under the document
/* USER CODE BEGIN PRIVATE_DEFINES */
#define USER_STORAGE_LUN_NBR 1
#define USER_STORAGE_BLK_NBR 512
#define USER_STORAGE_BLK_SIZ 4096
/* USER CODE END PRIVATE_DEFINES */
This paper introduces a new type of w25qxx when ,USB The method of automatically judging the capacity of equipment
principle
stay USB On initialization , By reading the w25qxx Of ID function , Get information about the storage medium ,
Then the discrimination information , Rewrite to the corresponding capacity .
start :
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x10000
#define STORAGE_BLK_SIZ 0x200
/* USER CODE BEGIN PRIVATE_DEFINES */
#define USER_STORAGE_BLK_SIZ 4096
/* USER CODE END PRIVATE_DEFINES */
initialization
/* USER CODE BEGIN PRIVATE_VARIABLES */
uint32_t w25qxx_storage;
/* USER CODE END PRIVATE_VARIABLES */
int8_t STORAGE_Init_FS(uint8_t lun)
{
/* USER CODE BEGIN 2 */
W25QXX_Init();
switch(W25QXX_TYPE)
{
case W25Q32:
w25qxx_storage = 32;
break;
case W25Q64:
w25qxx_storage = 64;
break;
case W25Q128:
w25qxx_storage = 128;
break;
case W25Q256:
w25qxx_storage = 256;
break;
default :
w25qxx_storage = 8;
}
w25qxx_storage = w25qxx_storage/8*1024*1024;
// w25qxx_storage is xxMB, but USER_STORAGE_BLK_SIZ is xxByte. Be care of the unit!
w25qxx_storage = w25qxx_storage/USER_STORAGE_BLK_SIZ;
return (USBD_OK);
/* USER CODE END 2 */
}
USB Read capacity initialization
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
/* USER CODE BEGIN 3 */
*block_num = w25qxx_storage;
*block_size = USER_STORAGE_BLK_SIZ;
return (USBD_OK);
/* USER CODE END 3 */
}
Reading and writing :
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 6 */
uint16_t i = 0;
for(i = 0;i < blk_len;i++)
{
W25QXX_Read(buf + i * USER_STORAGE_BLK_SIZ,blk_addr * USER_STORAGE_BLK_SIZ + i * USER_STORAGE_BLK_SIZ,USER_STORAGE_BLK_SIZ );
}
return (USBD_OK);
/* USER CODE END 6 */
}
/** * @brief . * @param lun: . * @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)
{
/* USER CODE BEGIN 7 */
uint16_t i = 0;
for(i = 0;i < blk_len;i++)
{
W25QXX_Write((uint8_t *)(buf + i * USER_STORAGE_BLK_SIZ),blk_addr * USER_STORAGE_BLK_SIZ + i * USER_STORAGE_BLK_SIZ,USER_STORAGE_BLK_SIZ );
}
return (USBD_OK);
/* USER CODE END 7 */
}
* Be careful bug
w25qxx The drive of is usually driven by punctual atoms 
“ Barthes ”(but),
There is a problem with the driver , I also found this during the test :
w25qxx.c In file Read ID function :
uint16_t W25QXX_ReadID(void)
{
uint16_t Temp = 0;
uint8_t byte = 0;
W25QXX_CS(0);
SPIx_ReadWriteByte(0x90); // Send read ID command
SPIx_ReadWriteByte(0x00);
SPIx_ReadWriteByte(0x00);
SPIx_ReadWriteByte(0x00);
SPIx_ReadByte(&byte, 1);
Temp |= byte;
SPIx_ReadByte(&byte, 1);
Temp |= byte;
W25QXX_CS(1);
return Temp;
}
Do you see the problem ?
Give you three minutes .
1min.
2min.
3min.
To reveal the answer :
.....
SPIx_ReadByte(&byte, 1);
Temp |= byte;
Temp<<=8; //Be care of this!
SPIx_ReadByte(&byte, 1);
Temp |= byte;
.....
The correct should be :
uint16_t W25QXX_ReadID(void)
{
uint16_t Temp = 0;
uint8_t byte = 0;
W25QXX_CS(0);
SPIx_ReadWriteByte(0x90); // Send read ID command
SPIx_ReadWriteByte(0x00);
SPIx_ReadWriteByte(0x00);
SPIx_ReadWriteByte(0x00);
SPIx_ReadByte(&byte, 1);
Temp |= byte;
Temp<<=8; //Be care of this!
SPIx_ReadByte(&byte, 1);
Temp |= byte;
W25QXX_CS(1);
return Temp;
}
Now you can read the chip smoothly ID 了 , After reading ID Judge the capacity .
Effect display
Plug in w25q64
change w25q128
Hommization !
边栏推荐
- 【ROS进阶篇】第五讲 ROS中的TF坐标变换
- 微信网页订阅消息实现
- "Qt+pcl Chapter 6" point cloud registration ICP Series 6
- SAP s/4hana: one code line, many choices
- Description | Huawei cloud store "commodity recommendation list"
- It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!
- 点云重建方法汇总一(PCL-CGAL)
- Flink 系例 之 TableAPI & SQL 与 Kafka 消息插入
- 22-06-26周总结
- 【云动向】6月上云新风向!云商店热榜揭晓
猜你喜欢

Hardware design guide for s32k1xx microcontroller

MySQL审计插件介绍

Fix the failure of idea global search shortcut (ctrl+shift+f)

【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
Implementation of deploying redis sentry in k8s

【LeetCode】16、最接近的三数之和

Phpcms background upload picture button cannot be clicked

微信小程序02-轮播图实现与图片点击跳转

微信小程序01-底部导航栏设置

Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
随机推荐
idea中新建的XML文件变成普通文件的解决方法.
opencv学习笔记六--图像拼接
异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
Task. Run(), Task. Factory. Analysis of behavior inconsistency between startnew() and new task()
TypeScript:var
TS reports an error don't use 'object' as a type The `object` type is currently hard to use
微信公众号订阅消息 wx-open-subscribe 的实现及闭坑指南
STM32F411 SPI2输出错误,PB15无脉冲调试记录【最后发现PB15与PB14短路】
重回榜首的大众,ID依然乏力
【天线】【3】CST一些快捷键
The solution to turn the newly created XML file into a common file in idea
IDEA全局搜索快捷键(ctrl+shift+F)失效修复
微信小程序03-文字一左一右显示,行内块元素居中
张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
Tableapi & SQL and Kafka message insertion in Flink
22-06-26周总结
swiper 轮播图,最后一张图与第一张图无缝衔接
选择在长城证券上炒股开户可以吗?安全吗?
Solid basic structure and array, private / public function, return value and modifier of function, event
Solid basic basic grammar and definition function