[STM32F2] stm32f207用stm32cube生成usb mass storage class无法识别

[复制链接]
4598|10
 楼主| 240011814 发表于 2015-12-24 15:15 | 显示全部楼层 |阅读模式
如题,我电路DP接了个上拉电阻,pc识别到未知设备,去掉后pc完全没反应
我用stm32cube生成的usb下载到另一块stm32f072板子可以正常识别,那块板子未接上拉电阻,晶振是内部晶振
我的stm32f207是外部25m晶振,晶振并了个100k电阻和10pf电容
s_f_tan 发表于 2015-12-24 16:23 | 显示全部楼层
时钟配置对了没有?USB的要配成48M吧,你用外部时钟25M,如何配出来?
 楼主| 240011814 发表于 2015-12-24 17:15 | 显示全部楼层
我用这函数 HAL_RCC_GetSysClockFreq();读出来 sysclock 120m
捕获.PNG
mintspring 发表于 2015-12-24 21:06 | 显示全部楼层
Pinout界面中修改:
开启USB_OTG_FS模式下的Device_Only,其他不选
middleWare里面的USB_DEVICE中的Class For FS IP选择Communication Device Class (Virtual Port Com)
时钟配置界面:8M外部晶振,设置系统168M,Apb1分频4,APB2分频2
Configuration界面中:
GPIO设置PD12,13,14,15输出上拉,中速


usb的GPIO的DM和DP均为AF,DM无上啦,DP上拉,高速模式(High)

开启中断优先组GRoup4
NVIC中断开启usb on the Go FS Global interrupt 中断1,0
USB_FS configuration 设置 speed为Full , ENDpoint 0 max packet size为64bytes
低功耗不使能, vbus不使能.....
usb_device configuration中的Parameter setting设置



ok,生成代码



mintspring 发表于 2015-12-24 21:07 | 显示全部楼层
PIO方面的配置图



mintspring 发表于 2015-12-24 21:08 | 显示全部楼层
生成之后,我用的事keil 编译的, 要去修改启动文件里面一个代码
打开工程的启动文件 startup_stm32f407xx.s
将里面的 Heap_Size EQU 0x00000200
改成 Heap_Size EQU 0x00001000
或者改成Heap_Size EQU 0x00001500
其中1500是官方那个演示程序的设置.

再次打开main.c文件
添加头文件
include "usbd_cdc.h"算了直接上main.c里面的全部代码,其他的文件都不用修改
  1. /* Includes ------------------------------------------------------------------*/
  2. include "stm32f4xx_hal.h"

  3. include "usb_device.h"

  4. include "gpio.h"

  5. /* USER CODE BEGIN Includes */
  6. include "usbd_cdc.h"

  7. /* USER CODE END Includes */

  8. /* Private variables ---------------------------------------------------------*/

  9. /* USER CODE BEGIN PV */

  10. /* USER CODE END PV */

  11. /* Private function prototypes -----------------------------------------------*/
  12. void SystemClock_Config(void);

  13. /* USER CODE BEGIN PFP */

  14. /* USER CODE END PFP */

  15. /* USER CODE BEGIN 0 */
  16. uint16_t mmss;

  17. /* USER CODE END 0 */

  18. int main(void)
  19. {

  20. /* USER CODE BEGIN 1 */
  21. uint8_t UserTxBuffer55 = 0x55;
  22. /* USER CODE END 1 */

  23. /* MCU Configuration----------------------------------------------------------*/

  24. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  25. HAL_Init();

  26. /* Configure the system clock */
  27. SystemClock_Config();

  28. /* Initialize all configured peripherals */
  29. MX_GPIO_Init();
  30. MX_USB_DEVICE_Init();

  31. /* USER CODE BEGIN 2 */

  32. /* USER CODE END 2 */

  33. /* USER CODE BEGIN 3 */
  34. /* Infinite loop */
  35. while (1)
  36. {
  37.     if(mmss>=500)
  38.     {
  39.     HAL_GPIO_TogglePin (GPIOD,GPIO_PIN_12);

  40.     USBD_CDC_SetTxBuffer(&hUsbDeviceFS, (uint8_t*)&UserTxBuffer55, 1);

  41. USBD_CDC_TransmitPacket(&hUsbDeviceFS);

  42.     mmss = 0;

  43.     }

  44. }
  45. /* USER CODE END 3 */

  46. }

  47. /** System Clock Configuration
  48. */
  49. void SystemClock_Config(void)
  50. {

  51. RCC_OscInitTypeDef RCC_OscInitStruct;
  52. RCC_ClkInitTypeDef RCC_ClkInitStruct;

  53. __PWR_CLK_ENABLE();

  54. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  55. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  56. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  57. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  58. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  59. RCC_OscInitStruct.PLL.PLLM = 8;
  60. RCC_OscInitStruct.PLL.PLLN = 336;
  61. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  62. RCC_OscInitStruct.PLL.PLLQ = 7;
  63. HAL_RCC_OscConfig(&RCC_OscInitStruct);

  64. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
  65.                           |RCC_CLOCKTYPE_PCLK2;
  66. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  67. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  68. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  69. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  70. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

  71. }

  72. /* USER CODE BEGIN 4 */
  73. void HAL_SYSTICK_Callback(void)
  74. {
  75. /* NOTE : This function Should not be modified, when the callback is needed,
  76.         the HAL_SYSTICK_Callback could be implemented in the user file
  77. */
  78. mmss++;
  79. }
  80. /* USER CODE END 4 */
  81. ifdef USE_FULL_ASSERT

  82. /**
  83. * [url=home.php?mod=space&uid=247401]@brief[/url] Reports the name of the source file and the source line number
  84. * where the assert_param error has occurred.
  85. * @param file: pointer to the source file name
  86. * @param line: assert_param error line source number
  87. * @retval None
  88. */
  89. void assert_failed(uint8_t* file, uint32_t line)
  90. {
  91. /* USER CODE BEGIN 6 */
  92. /* User can add his own implementation to report the file name and line number,
  93. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  94. /* USER CODE END 6 */

  95. }
  96. endif

好了,编译烧录,打开串口调试助手
额,先上图







注意啊,串口调试助手里面,只需要选择相应的串口就可以了......其他的什么波特率啊,位数啊,都没用的.......... 咱是usb虚拟的,没定义这个....不信你可以自己试试

mintspring 发表于 2015-12-24 21:09 | 显示全部楼层
下面测试接收数据.要添加个东西...
打开usbd_cdc_if.c文件里面的倒数第二个函数
改为
static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len){/* USER CODE BEGIN 7 */ USBD_CDC_SetRxBuffer(&hUsbDeviceFS, (uint8_t *)&UserRxBufferFS);USBD_CDC_ReceivePacket(&hUsbDeviceFS);    return (USBD_OK);/* USER CODE END 7 */ }
不改的话,接收一次数据可以,第二次串口调试助手就会死机..............
我main函数中这样写
while (1){    if(addtime >=500)    {        addtime = 0;        HAL_GPIO_TogglePin (GPIOD,GPIO_PIN_12);//          UsbSendData (&datausb ,1);        CDC_Transmit_FS(UserRxBufferFS ,7);    }}
每次500ms发送一次接收到的数据回去....
我每次发送接收到的头7个数据...



图中,我发送16进制数 55 44 两个字节......
返回的是16进制数 55 44 C1 2C 60 18 7C 七个字节.....
这是我随便发送后,再次发送55 44两个字节的结果...
数据没清零.......说明程序还是有问题.........


mintspring 发表于 2015-12-24 21:09 | 显示全部楼层
说明程序还是有问题.........那个楼主也是个半罐子。
 楼主| 240011814 发表于 2015-12-25 18:27 | 显示全部楼层
是堆栈分配少了,另一块板子可以。。。。。
Thor9 发表于 2015-12-26 19:32 | 显示全部楼层
晶振并了个100k电阻和10pf电容

晶振并电阻干啥,不是两个电容吗
豆腐块 发表于 2015-12-27 21:55 | 显示全部楼层
是怎么查出来堆栈分配少的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

17

主题

70

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部