你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32F407通过自带USB驱动EC20 4G模块-完整工程

[复制链接]
STMCU小助手 发布时间:2022-4-21 21:00
EC20 4G模块可以通过串口和USB去驱动,但是串口速度比较慢,所以通过USB驱动才能提高通信速率。

硬件连接:
STM32F407:

XJ[GE6G}V~N4UM`N{85`60K.png

EC20模块:

{(DV1){WKBN1LJ@UK`RHQVH.png

部分参考代码:


  1. /* USER CODE END Header */
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "main.h"
  4. #include "usb_host.h"
  5. #include "usbh_def.h"
  6. #include "usbh_cdc.h"
  7. #include "string.h"

  8. UART_HandleTypeDef huart1;

  9. /* USER CODE BEGIN PV */

  10. /* USER CODE END PV */

  11. /* Private function prototypes -----------------------------------------------*/
  12. void SystemClock_Config(void);
  13. static void MX_GPIO_Init(void);
  14. static void MX_USART1_UART_Init(void);
  15. void MX_USB_HOST_Process(void);


  16. unsigned char recData[100];
  17. unsigned int recLen;  

  18. void delay_ums(unsigned int i)
  19. {
  20.         unsigned int cnt;
  21.         while(i--)
  22.         {
  23.                 cnt=33500;
  24.                 while(cnt--);
  25.         }
  26. }

  27. extern USBH_HandleTypeDef hUsbHostFS;
  28. int main(void)
  29. {
  30.         unsigned int a;
  31.         HAL_Init();
  32.         SystemClock_Config();
  33.         MX_GPIO_Init();
  34.         MX_USART1_UART_Init();
  35.         MX_USB_HOST_Init();
  36.         MX_GPIO_Init();

  37.         HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);  

  38.         delay_ums(1000);
  39.         HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);  

  40.   while (1)
  41.   {
  42.     /* USER CODE END WHILE */
  43.     MX_USB_HOST_Process();
  44.         delay_ums(1);
  45.           a++;
  46.           if(a>1000)
  47.           {
  48.                   a=0;
  49.                   USBH_CDC_Transmit(&hUsbHostFS, "AT\r\n", strlen("AT\r\n"));
  50.           }
  51.     /* USER CODE BEGIN 3 */
  52.   }
  53.   /* USER CODE END 3 */
  54. }

  55. /**
  56.   * @brief System Clock Configuration
  57.   * @retval None
  58.   */
  59. void SystemClock_Config(void)
  60. {
  61.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  62.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  63.   /** Configure the main internal regulator output voltage
  64.   */
  65.   __HAL_RCC_PWR_CLK_ENABLE();
  66.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  67.   /** Initializes the RCC Oscillators according to the specified parameters
  68.   * in the RCC_OscInitTypeDef structure.
  69.   */
  70.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  71.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  72.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  73.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  74.   RCC_OscInitStruct.PLL.PLLM = 4;
  75.   RCC_OscInitStruct.PLL.PLLN = 168;
  76.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  77.   RCC_OscInitStruct.PLL.PLLQ = 7;
  78.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  79.   {
  80.     Error_Handler();
  81.   }
  82.   /** Initializes the CPU, AHB and APB buses clocks
  83.   */
  84.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  85.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  86.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  87.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  88.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  89.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  90.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  91.   {
  92.     Error_Handler();
  93.   }
  94. }

  95. /**
  96.   * @brief USART1 Initialization Function
  97.   * @param None
  98.   * @retval None
  99.   */
  100. static void MX_USART1_UART_Init(void)
  101. {

  102.   /* USER CODE BEGIN USART1_Init 0 */

  103.   /* USER CODE END USART1_Init 0 */

  104.   /* USER CODE BEGIN USART1_Init 1 */

  105.   /* USER CODE END USART1_Init 1 */
  106.   huart1.Instance = USART1;
  107.   huart1.Init.BaudRate = 115200;
  108.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  109.   huart1.Init.StopBits = UART_STOPBITS_1;
  110.   huart1.Init.Parity = UART_PARITY_NONE;
  111.   huart1.Init.Mode = UART_MODE_TX_RX;
  112.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  113.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  114.   if (HAL_UART_Init(&huart1) != HAL_OK)
  115.   {
  116.     Error_Handler();
  117.   }
  118.   /* USER CODE BEGIN USART1_Init 2 */

  119.   /* USER CODE END USART1_Init 2 */

  120. }

  121. /**
  122.   * @brief GPIO Initialization Function
  123.   * @param None
  124.   * @retval None
  125.   */
  126. static void MX_GPIO_Init(void)
  127. {
  128.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  129.         /* GPIO Ports Clock Enable */
  130.         __HAL_RCC_GPIOH_CLK_ENABLE();
  131.         __HAL_RCC_GPIOA_CLK_ENABLE();
  132.         __HAL_RCC_GPIOC_CLK_ENABLE();
  133.   /*Configure GPIO pin Output Level */


  134.   /*Configure GPIO pin : PA15 */
  135.   GPIO_InitStruct.Pin = GPIO_PIN_15;
  136.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  137.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  138.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  139.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  140.   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);



  141.   /*Configure GPIO pin : PA15 */
  142.   GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_2;
  143.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  144.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  145.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  146.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  147.   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_2, GPIO_PIN_RESET);  
  148. }

  149. /* USER CODE BEGIN 4 */

  150. /* USER CODE END 4 */

  151. /**
  152.   * @brief  This function is executed in case of error occurrence.
  153.   * @retval None
  154.   */
  155. void Error_Handler(void)
  156. {
  157.   /* USER CODE BEGIN Error_Handler_Debug */
  158.   /* User can add his own implementation to report the HAL error return state */

  159.   /* USER CODE END Error_Handler_Debug */
  160. }

  161. #ifdef  USE_FULL_ASSERT
  162. /**
  163.   * @brief  Reports the name of the source file and the source line number
  164.   *         where the assert_param error has occurred.
  165.   * @param  file: pointer to the source file name
  166.   * @param  line: assert_param error line source number
  167.   * @retval None
  168.   */
  169. void assert_failed(uint8_t *file, uint32_t line)
  170. {
  171.   /* USER CODE BEGIN 6 */
  172.   /* User can add his own implementation to report the file name and line number,
  173.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  174.   /* USER CODE END 6 */
  175. }
  176. #endif /* USE_FULL_ASSERT */

  177. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  178. void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef *phost)
  179. {
  180.         recLen = USBH_CDC_GetLastReceivedDataSize(phost);
  181.         
  182.         HAL_UART_Transmit_IT(&huart1 ,(uint8_t*)recData,recLen);
  183.         while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=SET);                //等待发送结束        
  184.         
  185.         memset(recData,0x00,sizeof(recData));
  186. }

复制代码


收藏 评论0 发布时间:2022-4-21 21:00

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版