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

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

[复制链接]
STMCU小助手 发布时间:2022-4-21 21:00
EC20 4G模块可以通过串口和USB去驱动,但是串口速度比较慢,所以通过USB驱动才能提高通信速率。
& H7 U! B9 N/ V) J
6 a8 X7 K  M# ^: v4 c: E硬件连接:
  h. D4 ^6 x( P% DSTM32F407:
8 T  J2 {5 b% F+ k" Q
9 d% K2 w# M+ b6 X- a6 v XJ[GE6G}V~N4UM`N{85`60K.png ) n+ T! A$ W; y* f& }4 t+ Z

# j8 w6 q8 w  B; D# g/ b) z' l' b2 UEC20模块:
; k9 h6 ^* ^7 t1 J) ^0 L  a2 P& @0 u; Q  B
{(DV1){WKBN1LJ@UK`RHQVH.png " i4 i& L# t: T' F

. ?  G9 d0 w% ^' `. Q# s部分参考代码:! R& E2 o4 m% E
" [+ }- g7 g" d) B

  1. % M" Q- k5 I2 R
  2. /* USER CODE END Header */
    2 t0 x5 l" b0 h
  3. /* Includes ------------------------------------------------------------------*/
    ! N7 e$ T+ r  j' ~+ |0 c
  4. #include "main.h"4 i# K9 O3 [" r+ T# s2 \
  5. #include "usb_host.h") y: o2 Y# D& t/ v2 e+ e5 q7 T
  6. #include "usbh_def.h"8 [* l' v2 g9 r4 ?3 P
  7. #include "usbh_cdc.h"
    4 D7 _/ r9 q, m5 b  p2 M: C
  8. #include "string.h"
    / f/ G- x/ l, [" g$ o; n) t

  9. : z5 ?4 K; G8 p% Q% ?- g3 L  N/ g
  10. UART_HandleTypeDef huart1;
    9 A1 a8 _) @' G. t

  11. . f8 X- Y& T- p- G
  12. /* USER CODE BEGIN PV */6 o9 U: o, }9 P" J# B) \3 }
  13. 3 ?/ ~8 @, |3 s- p7 P$ ~
  14. /* USER CODE END PV */
    # ~7 ~1 }( F0 ]: {

  15. ' Q, S1 {% Y" s+ i4 C1 S) K7 B* v" L
  16. /* Private function prototypes -----------------------------------------------*/6 S$ ]9 s( m; H! h. P2 P
  17. void SystemClock_Config(void);0 {- t3 p5 o' j3 Q6 p" u0 ?
  18. static void MX_GPIO_Init(void);( o1 X9 h+ }8 b9 Y$ m4 O
  19. static void MX_USART1_UART_Init(void);1 g4 |7 |1 g8 ?
  20. void MX_USB_HOST_Process(void);6 H7 Z  B; c1 v+ `: o

  21. ) _: P/ U0 E- k6 Y
  22. : w, j  G- E( p
  23. unsigned char recData[100];! F8 l* v* H; {- J7 q' [6 r' A
  24. unsigned int recLen;  
    4 C6 N4 _* Z2 H* A8 Y

  25. 1 _* H+ x4 x; o4 `/ e
  26. void delay_ums(unsigned int i)
    6 x* r0 D/ g) f$ f7 a
  27. {
    8 R" s5 }8 x+ s2 {" X2 }; e
  28.         unsigned int cnt;
    # T# L% s- I# G9 E9 T7 n" R1 c; {
  29.         while(i--)
    + q2 I# z% b+ U5 x0 L0 Q
  30.         {
    ! E8 Y; J7 o, Q2 m# j
  31.                 cnt=33500;
    " C& A  c4 V5 K
  32.                 while(cnt--);, S2 i/ z- Z7 y4 N
  33.         }
    0 S) A$ ^/ U0 y5 G) Y3 m1 ?
  34. }
    4 q1 O3 C) y" @
  35. $ P" D9 n6 R) d1 I& O  g
  36. extern USBH_HandleTypeDef hUsbHostFS;
    * o* b; o2 X5 A, E/ c
  37. int main(void)
    0 c* h+ f9 q+ s9 P
  38. {
    % c/ P/ _* t& F: c! b3 m. |
  39.         unsigned int a;8 b9 {3 w2 q" u
  40.         HAL_Init();1 l" q# K1 k# w3 g
  41.         SystemClock_Config();, `8 K5 T' g* o! p$ N
  42.         MX_GPIO_Init();3 o, S. W) `- n# |! t
  43.         MX_USART1_UART_Init();5 y) {+ X1 l; ]; g, p
  44.         MX_USB_HOST_Init();0 m2 \1 P1 I/ D% {$ x9 N8 q5 D
  45.         MX_GPIO_Init();
    ' B1 S1 z* ^8 J0 O

  46. ( H* q7 b8 j; u+ @% ~: d
  47.         HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);  
    . [0 m7 J) a* P2 ?; [: o4 y
  48.   O! W6 f% C# Q5 b: ?! z
  49.         delay_ums(1000);' w/ @% _! L' [. b4 o8 b# y3 z
  50.         HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);  
    + [# Q% x" }# _

  51. 9 ]8 Z% V! i0 ~; [- _
  52.   while (1)
    % c9 ~2 c8 Y8 h$ q# {% w
  53.   {. n+ i0 r. i$ a, C: z
  54.     /* USER CODE END WHILE */, \  ~. F+ B! m2 A3 h
  55.     MX_USB_HOST_Process();
    8 j& n0 Q& D, O" y1 D' ~5 I
  56.         delay_ums(1);$ `' [! @# P5 `/ v& a
  57.           a++;
    ! C0 @8 o- R" Q) l
  58.           if(a>1000)5 N) v0 V6 K+ `  D3 I4 G+ `: ^
  59.           {
    ( E( J1 }( Y% U
  60.                   a=0;
    - b. B1 E  S- K
  61.                   USBH_CDC_Transmit(&hUsbHostFS, "AT\r\n", strlen("AT\r\n"));+ k! X6 N' Q9 f: ]9 s
  62.           }
    : L& s3 J# f3 N& u9 {) ~
  63.     /* USER CODE BEGIN 3 */
    ( b( L5 r* M$ N8 |4 D' T$ N
  64.   }
    $ E: M$ w6 U! P" k% \; z
  65.   /* USER CODE END 3 */2 Y5 S: [( @# s. F9 M
  66. }4 o3 Z$ f" U/ b
  67.   I% q, T- T6 {
  68. /**
    / F" t) f+ o% _8 e9 b9 y' k
  69.   * @brief System Clock Configuration
    " F- J: P4 l& f" Z, J$ p
  70.   * @retval None) e  ~" |# m% k# j) M* a
  71.   */
    1 ]) ~# |# w+ U- y* d" u0 W
  72. void SystemClock_Config(void)
    ! @) D8 [) y5 O+ i5 s- c3 C
  73. {' \5 i& |5 q/ d6 u
  74.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};' t7 H- Z, A0 j6 G
  75.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};6 U8 T4 H7 v( w3 c" ~
  76. ; V% R8 O! P4 F1 b/ J; B0 {. S
  77.   /** Configure the main internal regulator output voltage; t6 x3 N% S/ R; i( o
  78.   */
    4 Q, d1 K+ v& T) E  R$ e
  79.   __HAL_RCC_PWR_CLK_ENABLE();% t2 H' j  N5 d; h  P. n
  80.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    2 J8 J) x4 Y. z+ o
  81.   /** Initializes the RCC Oscillators according to the specified parameters
    ! q4 z' u$ B# ~* u% G4 K' y' w- |
  82.   * in the RCC_OscInitTypeDef structure.
    1 s( X( _3 K: R5 k: {' q
  83.   */
    $ i" R% U5 ]4 `7 {
  84.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;; Z# U4 }$ T. s
  85.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;  I* h( Y/ p- U& v4 b7 ^
  86.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;& D' y* N- R. z" N" b5 i4 q
  87.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;. y* _4 F! i" o  @8 n
  88.   RCC_OscInitStruct.PLL.PLLM = 4;  q+ p' S3 [: y$ F3 W$ N4 y3 e
  89.   RCC_OscInitStruct.PLL.PLLN = 168;
    " z" f1 h! a7 R
  90.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;$ j' k* }; V% j  l' T# i, k
  91.   RCC_OscInitStruct.PLL.PLLQ = 7;4 x( _+ X! a  G/ M7 x* D& N
  92.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)% y! i: j9 K$ H( r* h2 W& D
  93.   {
    ' J) q4 X+ Q( E
  94.     Error_Handler();7 `" c+ z4 ]9 g: j. b
  95.   }- Y/ k. Z- C# b+ \4 a
  96.   /** Initializes the CPU, AHB and APB buses clocks
    : o0 L! E  s, C
  97.   */4 y  Q! U1 z; w! Y' ?; m! R1 z  V
  98.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
      D; U1 _4 M; S9 Y+ R; t
  99.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    2 ]( I: ~, }; _8 d& M
  100.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    / x1 C" b0 L0 A) K
  101.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    5 z) X' s& i. U; N
  102.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    & S& Q( N$ u* P2 t, k1 J
  103.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
    7 I5 P: U6 F1 \( Z" m( ^+ D
  104.   `/ A5 T/ }6 ~, Z
  105.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
    6 V! v  s( [' m- T2 E
  106.   {- \: ~1 f9 J# S! h9 ~8 O6 y% y9 t
  107.     Error_Handler();
    + e5 _3 @' w/ g4 `) y( b
  108.   }' j  B' F, f/ e; x6 A. i
  109. }8 I7 j+ O  g5 s  u0 X5 z

  110. 7 k/ X& @" ]5 p
  111. /**9 f3 ]; }/ D6 n6 ~8 p/ V
  112.   * @brief USART1 Initialization Function1 c- D3 y' |# c0 C, e7 n
  113.   * @param None
    9 d# u$ U5 @* V
  114.   * @retval None' n. b3 k; v2 u: V: {) K( k. @4 B( e
  115.   */+ |$ l5 t, [8 I+ E3 ]9 I
  116. static void MX_USART1_UART_Init(void)3 @% F# z4 S# R- ?: v* o* C
  117. {
    . ^, f$ w( ^9 S( l; h9 ?% }+ w
  118. : N. K; Z. d7 M& t
  119.   /* USER CODE BEGIN USART1_Init 0 */' q6 a! L* l2 F7 c, I9 I2 b0 {

  120. 9 F4 b4 ?$ x; w$ g# a
  121.   /* USER CODE END USART1_Init 0 */
    8 T, o8 ]6 D  S! {" y. i6 x
  122. % |( {# W' B' o2 h* m1 h! q
  123.   /* USER CODE BEGIN USART1_Init 1 */
    - w; k$ K8 ~% D- u* e( G, o3 r
  124. 1 Z% i3 q& d3 Z
  125.   /* USER CODE END USART1_Init 1 */! N: l  `% U- g; X0 `
  126.   huart1.Instance = USART1;, a, n2 N. W# W/ R+ b
  127.   huart1.Init.BaudRate = 115200;' @/ F/ C7 P# x2 k
  128.   huart1.Init.WordLength = UART_WORDLENGTH_8B;3 I4 }% U) w" S+ w
  129.   huart1.Init.StopBits = UART_STOPBITS_1;; t' K6 M6 T4 W2 p2 Z6 o
  130.   huart1.Init.Parity = UART_PARITY_NONE;- @: `; R! D) x8 S8 p  B! d
  131.   huart1.Init.Mode = UART_MODE_TX_RX;
      T$ A1 b0 g' O- v
  132.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    6 L# q9 |7 u8 G. T
  133.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
    8 P0 [! t. T9 r+ l+ `6 z- z  B
  134.   if (HAL_UART_Init(&huart1) != HAL_OK)
    & [7 m+ G5 X' r+ g
  135.   {5 j- {8 q3 s; V& D- o% u; K4 o
  136.     Error_Handler();
    9 B4 `9 @; b5 ^" X
  137.   }" S( T% \9 ~# c/ {2 ?  r4 }
  138.   /* USER CODE BEGIN USART1_Init 2 */5 T% J2 o2 I/ x' c" e6 J2 c
  139.   U5 l" y, a- c. H
  140.   /* USER CODE END USART1_Init 2 */
    . p9 L8 v; c! L4 S6 c& E9 U6 Q$ U
  141. ' v' Z1 J* E1 K4 D3 f& z
  142. }: a: B+ ~# C1 \- j' A

  143. 6 m' \% E$ C( C9 P* J6 a6 e! y
  144. /**: U( l/ U2 N; U: x8 S+ A
  145.   * @brief GPIO Initialization Function4 \9 H- a! f1 g
  146.   * @param None- A; j  Q$ C8 l, ]& A( k0 n0 ^
  147.   * @retval None
    2 A5 P  ^0 a3 ?6 s5 w6 W. w3 a. o5 @3 t
  148.   */
    * w; ^: e7 e; E4 c
  149. static void MX_GPIO_Init(void)2 ]1 s, W" Y- [+ g5 b- n; [
  150. {
    ! R% {; \, Q; I% \2 e" u5 _5 |
  151.   GPIO_InitTypeDef GPIO_InitStruct = {0};6 E& C! ?- R# q8 P) D) [0 ^4 X4 S! @

  152. . l$ H7 ]8 R9 w7 C0 z/ H6 y
  153.         /* GPIO Ports Clock Enable */! o1 a% H2 [5 X' j% Q) j5 {
  154.         __HAL_RCC_GPIOH_CLK_ENABLE();) l7 y0 D$ R( a$ r4 |
  155.         __HAL_RCC_GPIOA_CLK_ENABLE();. q& V! L, s) h% Y8 w6 `) ^
  156.         __HAL_RCC_GPIOC_CLK_ENABLE();
    ) n9 n1 G$ p  U/ a; E
  157.   /*Configure GPIO pin Output Level */
    , n) {/ `1 a1 ~! f, j

  158. , J/ ^- w! D1 F3 E: ~0 }* B. F

  159. 8 X! f" x: \8 I; O! ~. E2 L
  160.   /*Configure GPIO pin : PA15 */- q% N7 z+ [/ ~/ g, m- i0 ^
  161.   GPIO_InitStruct.Pin = GPIO_PIN_15;
    ) v; n/ e4 B% Y/ S
  162.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;; B6 [8 t( @8 d5 J3 p' Q
  163.   GPIO_InitStruct.Pull = GPIO_NOPULL;( {$ Z  y7 z8 J, `4 Z% ^! Z
  164.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;& S3 C' \  K  u5 B& L4 x5 b' Q  h
  165.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    ( E* u  E% }6 l: N, ^+ e
  166.   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
    # n2 i, u5 B8 y, z1 {  x7 e- T
  167. . L$ b5 y! U) N5 ?/ P
  168.   E+ ?# y3 Z0 V' }2 D
  169. : S3 u0 D5 z  Z* u
  170.   /*Configure GPIO pin : PA15 */
    - ~1 T( G" R; c" y. L/ b. s' B
  171.   GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_2;
    6 x5 ^0 h3 ]/ l- S
  172.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    ' c, |9 }5 k4 X  \2 j0 h
  173.   GPIO_InitStruct.Pull = GPIO_NOPULL;
    - y" P0 Q- C4 y  D' M6 _* d
  174.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    " a/ q7 R; J5 P6 K# X, @& i
  175.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    . B6 J8 n* E7 y# @& ^
  176.   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_2, GPIO_PIN_RESET);  3 p3 K" R& x: [  q- h
  177. }
    , K# m9 X0 s& r0 |' X

  178. / t1 M: J& B8 R$ d; O0 m
  179. /* USER CODE BEGIN 4 */
    ' t) Z2 d! T: `

  180. ; h6 F, W  S  T$ N! p/ Z
  181. /* USER CODE END 4 */
    " U1 z& ]: j9 _5 x8 d8 V8 \

  182. 3 p2 g6 {& @' A" Z8 j
  183. /**
    + q% z4 K( G- J5 l' B$ t
  184.   * @brief  This function is executed in case of error occurrence.( h+ R/ m7 p8 [5 b  a' Q3 y* f
  185.   * @retval None
    ; C3 `8 s4 x, Z) g
  186.   */# f* [; P) Q9 x1 r
  187. void Error_Handler(void)% P# r% V1 ?! q) X1 S
  188. {
    , b( w. {! z2 ]2 j
  189.   /* USER CODE BEGIN Error_Handler_Debug */3 M- L5 t8 ~2 ~* A8 r/ [
  190.   /* User can add his own implementation to report the HAL error return state */4 G$ ^& F  K" M5 u; _- z0 b- W
  191. 4 a" F6 }! T8 j
  192.   /* USER CODE END Error_Handler_Debug */& F6 r8 X7 x% j* H& g, r1 t& k
  193. }8 x( v5 Y7 {3 L6 i) d4 c

  194. & w8 W/ _0 ]* C7 i( F
  195. #ifdef  USE_FULL_ASSERT( w  [- w" J. K' e" ~# x
  196. /**
    ! I% @5 Q' [& b* P. N! s9 z7 D
  197.   * @brief  Reports the name of the source file and the source line number
    ' l7 ^: O$ ?& |
  198.   *         where the assert_param error has occurred.$ F  {* A6 d1 y, S: _  g
  199.   * @param  file: pointer to the source file name
    + Q/ W7 C, }0 F7 k0 `" b
  200.   * @param  line: assert_param error line source number
    6 Y6 T0 E( n+ Y# a/ `+ k
  201.   * @retval None! p3 |$ Y+ ]1 z0 M4 V- N9 P
  202.   */- S' `% B+ V! k+ w
  203. void assert_failed(uint8_t *file, uint32_t line), E" t& g( h: i0 \* {
  204. {
    4 h5 t$ h  e6 U# e$ a5 W
  205.   /* USER CODE BEGIN 6 */
    & z" j' c) v. n3 c9 ~, ?6 }
  206.   /* User can add his own implementation to report the file name and line number,
    + ?3 I* b) _' ~/ D$ X
  207.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    * ^7 q5 X, e4 b& y5 s' s# S
  208.   /* USER CODE END 6 */. V! x, ~' _% u# L5 x7 G+ M% o+ {
  209. }* @" R8 l' g/ m6 N  ^3 M# X" _
  210. #endif /* USE_FULL_ASSERT */
    4 E& p* A" B' t: a. O) X
  211. 2 V  U, v+ h( @7 X
  212. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/8 h6 T9 g+ ~4 J, H/ s
  213. void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef *phost)" [# w. ]& Q3 d) `* T/ X% C2 R
  214. {- O( F  ?  N  [% A* T7 }6 B
  215.         recLen = USBH_CDC_GetLastReceivedDataSize(phost);
    * Q% {2 I! |" y9 Q
  216.         
    $ i1 W+ ^, W  X" e* b: j5 [' a- \
  217.         HAL_UART_Transmit_IT(&huart1 ,(uint8_t*)recData,recLen);  Z$ r2 K& h7 i: x; i
  218.         while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=SET);                //等待发送结束        4 m# Q# q7 o/ r7 z
  219.         
    + r0 u" G5 A; u, ?& u8 w( q
  220.         memset(recData,0x00,sizeof(recData));& c+ E/ b3 Y' a/ L. m8 O
  221. }
    % ?6 }1 h/ u! _( _

  222. ; m$ A  N8 T5 g4 O7 N/ l
复制代码
7 A* k3 x/ E: u8 |/ Q

: Q; {* e  _# E1 {0 n9 E4 g
赞 收藏 评论0 发布时间:2022-4-21 21:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版