CUBEMX STM32F105RB U盘读写详细教程
CUBEMX STM32F105RB U盘读写详细教程
abin 42817001
- 打开cubemx软件,
2.选择单片机型号,本文选stm32f105rb
3.设置RCC,
4.设置时钟
1 根据开发板选外部晶振,一般是8Mhz。
2 选通外部晶振通道,由于ubs要使用48Mhz频率,内部频率无法提供
3工具单片机选择主频,
1 2 3步骤无先后顺序,
5.选择调试端口,这里使用UART2,可以根据需要选择其他的uart
6.配置USB顺序
检查usb时钟是否是48Mhz,一般软件会自动设置好。
Host 设置 见usbh_conf.h 65-90行
7.设置PC9脚输出
由于本开发板的usb通过PC9低电平导通三极管给usb供电,所以要另外设置PC9脚, 如与本板不同可忽略本步骤。
8.管脚布局查看
9.PROJECT配置
10.点击软件界面的右上角的 产生代码,可能会出现一个警告窗口,不必理会,点YES
软件会自动生成KEIL需要的相关工程文件,点打开工程,然后就自动打开KEIL,
11.Keil 操作
12.Cubemx生成的main.c还不能直接使用,要定义变量 回调函数 USB操作函数等
代码要保存在cubemx设定好的用户代码区域,如果硬件变动生成新代码,用户自定义的代码就会保留。
13.下面是main.c修改后的文件,红色部分是增加的内容,其他文件不需修改
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
#include "usart.h"
#include "usb_host.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* 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 */
//定义PC9脚,如不需要可取消
#define USB_POWER_Pin GPIO_PIN_9
#define USB_POWER_GPIO_Port GPIOC
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void MX_USB_HOST_Process(void);
/* USER CODE BEGIN PFP */
FATFS USBDISKFatFs; /* File system object for USB disk logical drive */
FIL MyFile; /* File object */
extern ApplicationTypeDef Appli_state;
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//下面2个函数 使能、禁止HCD_Port的回调函数,勿修改,必须
/**
* @brief Port Port Enabled callback.
* @param hhcd: HCD handle
* @retval None
*/
void HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef *hhcd)
{
USBH_LL_PortEnabled(hhcd->pData);
}
/**
* @brief Port Port Disabled callback.
* @param hhcd: HCD handle
* @retval None
*/
void HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef *hhcd)
{
USBH_LL_PortDisabled(hhcd->pData);
}
/**
* @brief Main routine for Mass Storage Class
* @param None
* @retval None
*/
//下面函数操作USB 文件读写,可根据需要修改,函数名可自定义
void MSC_Application(void)
{
/* FatFs function common result code description: */
/* FR_OK = 0, (0) Succeeded */
/* FR_DISK_ERR, (1) A hard error occurred in the low level disk I/O layer */
/* FR_INT_ERR, (2) Assertion failed */
/* FR_NOT_READY, (3) The physical drive cannot work */
/* FR_NO_FILE, (4) Could not find the file */
/* FR_NO_PATH, (5) Could not find the path */
/* FR_INVALID_NAME, (6) The path name format is invalid */
/* FR_DENIED, (7) Access denied due to prohibited access or directory full */
/* FR_EXIST, (8) Access denied due to prohibited access */
/* FR_INVALID_OBJECT, (9) The file/directory object is invalid */
/* FR_WRITE_PROTECTED, (10) The physical drive is write protected */
/* FR_INVALID_DRIVE, (11) The logical drive number is invalid */
/* FR_NOT_ENABLED, (12) The volume has no work area */
/* FR_NO_FILESYSTEM, (13) There is no valid FAT volume */
/* FR_MKFS_ABORTED, (14) The f_mkfs() aborted due to any parameter error */
/* FR_TIMEOUT, (15) Could not get a grant to access the volume within defined period */
/* FR_LOCKED, (16) The operation is rejected according to thex file sharing policy */
/* FR_NOT_ENOUGH_CORE, (17) LFN working buffer could not be allocated */
/* FR_TOO_MANY_OPEN_FILES, (18) Number of open files > _FS_SHARE */
/* FR_INVALID_PARAMETER (19) Given parameter is invalid */
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "Hello Test USB!"; /* File write buffer */
uint8_t rtext[100]; /* File read buffer */
/* Register the file system object to the FatFs module */
res = f_mount(&USBDISKFatFs, (TCHAR const*)USBHPath, 0);
if(res != FR_OK)
{
/* FatFs Initialization Error */
USBH_UsrLog("f_mount error: %d", res);
Error_Handler();
}
USBH_UsrLog("create the file in: %s", USBHPath);
/* Create and Open a new text file object with write access */
res = f_open(&MyFile, "USBHost.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(res != FR_OK)
{
/* 'hello.txt' file Open for write Error */
USBH_UsrLog("f_open with write access error: %d", res);
Error_Handler();
}
/* Write data to the text file */
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
/* 'hello.txt' file Write or EOF Error */
USBH_UsrLog("file write or EOF error: %d", res);
Error_Handler();
}
/* Close the open text file */
f_close(&MyFile);
/* Open the text file object with read access */
res = f_open(&MyFile, "USBHost.txt", FA_READ);
if(res != FR_OK)
{
/* 'hello.txt' file Open for read Error */
USBH_UsrLog("f_open with read access error: %d", res);
Error_Handler();
}
/* Read data from the text file */
res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
if((bytesread == 0) || (res != FR_OK))
{
/* 'hello.txt' file Read or EOF Error */
USBH_UsrLog("f_read error: %d", res);
Error_Handler();
}
/* Close the open text file */
f_close(&MyFile);
/* Compare read data with the expected data */
if((bytesread != byteswritten))
{
/* Read data is different from the expected data */
USBH_UsrLog("file doesn't match");
Error_Handler();
}
/* Success of the demo: no error occurrence */
USBH_UsrLog("USBHost.txt was created into the disk");
/* Unlink the USB disk I/O driver */
FATFS_UnLinkDriver(USBHPath);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_FATFS_Init();
MX_USB_HOST_Init();
/* USER CODE BEGIN 2 */
//拉低PC9,如无该设置可取消
HAL_GPIO_WritePin(USB_POWER_GPIO_Port, USB_POWER_Pin, GPIO_PIN_RESET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
printf("STM32 start ok!\n");
while (1)
{
/* USER CODE END WHILE */
MX_USB_HOST_Process();
/* USER CODE BEGIN 3 */
//判断USB是否进入准备状态APPLICATION_READY
switch(Appli_state)
{
/**
* The generated code from STM32CubeMX has two "confusing" application states
* APPLICATION_START on HOST_USER_CONNECTION and APPLICATION_READY on HOST_USER_CLASS_ACTIVE.
* Any FatFs commands should be executed after APPLICATION_STATE_READY is reached."
*/
case APPLICATION_READY:
printf("ready ok\n");
MSC_Application();
Appli_state = APPLICATION_IDLE;
break;
case APPLICATION_IDLE:
default:
break;
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.Prediv1Source = RCC_PREDIV1_SOURCE_HSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV3;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/** Configure the Systick interrupt time
*/
__HAL_RCC_PLLI2S_ENABLE();
}
/* USER CODE BEGIN 4 */
//##############
//USB调试信息输出要用到printf,必须设置
//如果printf不能使用,需#include <stdio.h>,如使用UART1,则下面的huart2要改成huart1
/* fputc */
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart2 , (uint8_t *)&ch, 1 , 0xffff);
while (huart2.gState != HAL_UART_STATE_READY);//Loop until the end of transmission 每个发送都值得优化
return ch;
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
14.程序运行结果如下(启动后插上U盘,然后拔出U盘)。
由于FATFS系统版本不同,可能有些U盘无法识别。比如3.0 U盘
- 是MX_USB_HOST_Process(); 函数产生的信息
- 是 void MSC_Application(void) 输出的内容
15.网友介绍F407可以直接使用CUBEMX生成的文件,无需修改。使用F407的朋友可自行验证。
16.本文内容在STM32F105RB + cubemx5.4 + keilMDK5.2 + J-LINK V8.0调试通过。
17.本文由abin 42817001整理,
18.感谢 @厦门-电梯-窝瓜(892219846) @大拇指[em]IT(2571393392) 指点、解惑。
19.完成时间 2011.09 补充:日期有误,实为2019.09
cubemx+keil 源码 下载地址 https://download.csdn.net/download/silno/11967893
CUBEMX STM32F105RB U盘读写详细教程相关推荐
- 微软免费服务器申请教程,2019年8月13日最新免费申请微软OneDrive5TB云盘超详细教程!...
2019年8月13日最新免费申请微软OneDrive5TB云盘超详细教程!(已亲测!) 本人已于今天2019年8月13日日亲测,成功获取微软OneDrive 5T云盘! 第一步:.打开申请链接 学生版 ...
- 杰微主板bios设置u盘启动详细教程
杰微是大陆土生土长的主板品牌,凭借高性价比占有一定市场,那么如果U盘装系统的话,杰微主板如何进bios设置U盘启动呢?今天小编就为大家介绍杰微主板bios设置u盘启动详细教程,有需要的朋友不妨看看吧. ...
- 18年10月份最新免费申请微软OneDrive5TB云盘超详细教程!(已亲测!)
18年10月份最新免费申请微软OneDrive5TB云盘超详细教程!(已亲测!) 本人已于今天10月23日亲测,成功获取微软OneDrive5T云盘! 第一步:.打开申请链接 学生版:https:// ...
- 最新免费申请微软OneDrive5TB云盘超详细教程!(已亲测!)
https://www.cnblogs.com/wangyunfei/p/9836580.html 18年10月份最新免费申请微软OneDrive5TB云盘超详细教程!(已亲测!) 本人已于今天10月 ...
- ramdisk和linux PE,PE下建立Ramdisk盘的详细教程
如何在PE下建立一个Ramdisk盘呢?之前我们有介绍过如何在PE下安装系统ghost,有看过教程的朋友应该都会安装了吧.但是如果要在PE下建立一个Ramdisk盘,要如何建立呢?今天U大侠小编就和大 ...
- vSphere/ESXI 6.0 服务器U盘安装详细教程
回看各个ESXI 安装教程,步骤缺斤少两,怒写详细教程,慰求新手少踩坑... 一.制作U盘启动 1)下载U盘启动制作工具,推荐Rufus 2.11版本,免安装,简单强大又好用,服务器类U盘系统安装必备 ...
- thinkpad选择启动项_联想thinkpad笔记本新老机型bios设置u盘启动详细教程
[文章导读]现在越来越多的人买联想笔记本电脑,以前一些老机型还好,可以直接按快捷方式在bios中设置U盘启动,但随着一些新机型的出现,直接在BIOS中可能找不到U盘或找不到启动项,下面主要介绍一下关于 ...
- MAC抹盘重装详细教程
一年不抹盘就手痒痒系列>_< 在这里记录自己抹盘重装macOS Mojave的历程,引导器(USB启动磁盘)制作是一个大坑,目前的官方教程丢了一个小细节. 获取更多内容,请访问博主的个人博 ...
- 惠普cq42笔记本怎么u盘启动详细教程
惠普cq42笔记本是一款性价比极高的电脑,不仅外观设计时尚,配置也很优越,非常适合学生消费者们的娱乐需求,那么惠普cq42笔记本怎么u盘启动呢?下面快启动小编就为大家分享惠普cq42笔记本bios设置 ...
最新文章
- BCH接下来如何走?且看这场大会传达了什么思想
- spss聚类分析_SPSS聚类分析 I K均值聚类法案例实操
- 腾讯地图api_数据库API接口的类型及应用场景
- .NET开发框架(八)-服务器集群之网络负载平衡(视频)
- asp.net ajax 怎么获取前端ul li_useEffect Hook 是如何工作的(前端需要懂的知识点)
- java biginteger使用_在Java中使用BigInteger值
- 年底一大波“优化”来了
- GPO组策略 权限处理之原则
- k2路由器刷华硕固件
- python将图片转换成二进制文本逻辑_将python图片转为二进制文本的实例
- java代码混淆 源代码保护 代码逻辑混淆 代码加密 支持JDK16
- Python数据分析与挖掘——回归模型的假设检验
- 购物中心最好的无线AP是什么?
- html怎么转换成xmind,怎么把html导入XMind
- u盘内存怎么测试软件,U盘下的内存检测软件
- 北航外国语学院计算机项目,北京航空航天大学外国语学院游学项目.pdf
- 【全基因组关联分析GWAS专题1】——群体结构
- Linova and Kingdom
- PPT述课怎样倒计时
- GPRSsim800c
热门文章
- Linux FTDI
- BUUCTF RE WP31-32 [WUSTCTF2020]level1、[GWCTF 2019]xxor
- 计算机组成,南北桥,倍频,通信,频率一致才可以通信
- 计算机网络安全隔离之网闸、光闸
- 华为p40 pro鸿蒙系统体验,华为P40Pro升级鸿蒙系统体验_华为P40Pro升级鸿蒙系统感受...
- mvc 从客户端 中检测到有潜在危险的 Request 值
- Citric I 模拟赛心得
- 数据库总结(五):创建与使用视图
- Linux 4.15亮点特性
- Java 添加和删除Word文档水印