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
) 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
" i4 i& L# t: T' F
. ? G9 d0 w% ^' `. Q# s部分参考代码:! R& E2 o4 m% E
" [+ }- g7 g" d) B
% M" Q- k5 I2 R- /* USER CODE END Header */
2 t0 x5 l" b0 h - /* Includes ------------------------------------------------------------------*/
! N7 e$ T+ r j' ~+ |0 c - #include "main.h"4 i# K9 O3 [" r+ T# s2 \
- #include "usb_host.h") y: o2 Y# D& t/ v2 e+ e5 q7 T
- #include "usbh_def.h"8 [* l' v2 g9 r4 ?3 P
- #include "usbh_cdc.h"
4 D7 _/ r9 q, m5 b p2 M: C - #include "string.h"
/ f/ G- x/ l, [" g$ o; n) t
: z5 ?4 K; G8 p% Q% ?- g3 L N/ g- UART_HandleTypeDef huart1;
9 A1 a8 _) @' G. t
. f8 X- Y& T- p- G- /* USER CODE BEGIN PV */6 o9 U: o, }9 P" J# B) \3 }
- 3 ?/ ~8 @, |3 s- p7 P$ ~
- /* USER CODE END PV */
# ~7 ~1 }( F0 ]: {
' Q, S1 {% Y" s+ i4 C1 S) K7 B* v" L- /* Private function prototypes -----------------------------------------------*/6 S$ ]9 s( m; H! h. P2 P
- void SystemClock_Config(void);0 {- t3 p5 o' j3 Q6 p" u0 ?
- static void MX_GPIO_Init(void);( o1 X9 h+ }8 b9 Y$ m4 O
- static void MX_USART1_UART_Init(void);1 g4 |7 |1 g8 ?
- void MX_USB_HOST_Process(void);6 H7 Z B; c1 v+ `: o
) _: P/ U0 E- k6 Y- : w, j G- E( p
- unsigned char recData[100];! F8 l* v* H; {- J7 q' [6 r' A
- unsigned int recLen;
4 C6 N4 _* Z2 H* A8 Y
1 _* H+ x4 x; o4 `/ e- void delay_ums(unsigned int i)
6 x* r0 D/ g) f$ f7 a - {
8 R" s5 }8 x+ s2 {" X2 }; e - unsigned int cnt;
# T# L% s- I# G9 E9 T7 n" R1 c; { - while(i--)
+ q2 I# z% b+ U5 x0 L0 Q - {
! E8 Y; J7 o, Q2 m# j - cnt=33500;
" C& A c4 V5 K - while(cnt--);, S2 i/ z- Z7 y4 N
- }
0 S) A$ ^/ U0 y5 G) Y3 m1 ? - }
4 q1 O3 C) y" @ - $ P" D9 n6 R) d1 I& O g
- extern USBH_HandleTypeDef hUsbHostFS;
* o* b; o2 X5 A, E/ c - int main(void)
0 c* h+ f9 q+ s9 P - {
% c/ P/ _* t& F: c! b3 m. | - unsigned int a;8 b9 {3 w2 q" u
- HAL_Init();1 l" q# K1 k# w3 g
- SystemClock_Config();, `8 K5 T' g* o! p$ N
- MX_GPIO_Init();3 o, S. W) `- n# |! t
- MX_USART1_UART_Init();5 y) {+ X1 l; ]; g, p
- MX_USB_HOST_Init();0 m2 \1 P1 I/ D% {$ x9 N8 q5 D
- MX_GPIO_Init();
' B1 S1 z* ^8 J0 O
( H* q7 b8 j; u+ @% ~: d- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
. [0 m7 J) a* P2 ?; [: o4 y - O! W6 f% C# Q5 b: ?! z
- delay_ums(1000);' w/ @% _! L' [. b4 o8 b# y3 z
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
+ [# Q% x" }# _
9 ]8 Z% V! i0 ~; [- _- while (1)
% c9 ~2 c8 Y8 h$ q# {% w - {. n+ i0 r. i$ a, C: z
- /* USER CODE END WHILE */, \ ~. F+ B! m2 A3 h
- MX_USB_HOST_Process();
8 j& n0 Q& D, O" y1 D' ~5 I - delay_ums(1);$ `' [! @# P5 `/ v& a
- a++;
! C0 @8 o- R" Q) l - if(a>1000)5 N) v0 V6 K+ ` D3 I4 G+ `: ^
- {
( E( J1 }( Y% U - a=0;
- b. B1 E S- K - USBH_CDC_Transmit(&hUsbHostFS, "AT\r\n", strlen("AT\r\n"));+ k! X6 N' Q9 f: ]9 s
- }
: L& s3 J# f3 N& u9 {) ~ - /* USER CODE BEGIN 3 */
( b( L5 r* M$ N8 |4 D' T$ N - }
$ E: M$ w6 U! P" k% \; z - /* USER CODE END 3 */2 Y5 S: [( @# s. F9 M
- }4 o3 Z$ f" U/ b
- I% q, T- T6 {
- /**
/ F" t) f+ o% _8 e9 b9 y' k - * @brief System Clock Configuration
" F- J: P4 l& f" Z, J$ p - * @retval None) e ~" |# m% k# j) M* a
- */
1 ]) ~# |# w+ U- y* d" u0 W - void SystemClock_Config(void)
! @) D8 [) y5 O+ i5 s- c3 C - {' \5 i& |5 q/ d6 u
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};' t7 H- Z, A0 j6 G
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};6 U8 T4 H7 v( w3 c" ~
- ; V% R8 O! P4 F1 b/ J; B0 {. S
- /** Configure the main internal regulator output voltage; t6 x3 N% S/ R; i( o
- */
4 Q, d1 K+ v& T) E R$ e - __HAL_RCC_PWR_CLK_ENABLE();% t2 H' j N5 d; h P. n
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
2 J8 J) x4 Y. z+ o - /** Initializes the RCC Oscillators according to the specified parameters
! q4 z' u$ B# ~* u% G4 K' y' w- | - * in the RCC_OscInitTypeDef structure.
1 s( X( _3 K: R5 k: {' q - */
$ i" R% U5 ]4 `7 { - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;; Z# U4 }$ T. s
- RCC_OscInitStruct.HSEState = RCC_HSE_ON; I* h( Y/ p- U& v4 b7 ^
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;& D' y* N- R. z" N" b5 i4 q
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;. y* _4 F! i" o @8 n
- RCC_OscInitStruct.PLL.PLLM = 4; q+ p' S3 [: y$ F3 W$ N4 y3 e
- RCC_OscInitStruct.PLL.PLLN = 168;
" z" f1 h! a7 R - RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;$ j' k* }; V% j l' T# i, k
- RCC_OscInitStruct.PLL.PLLQ = 7;4 x( _+ X! a G/ M7 x* D& N
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)% y! i: j9 K$ H( r* h2 W& D
- {
' J) q4 X+ Q( E - Error_Handler();7 `" c+ z4 ]9 g: j. b
- }- Y/ k. Z- C# b+ \4 a
- /** Initializes the CPU, AHB and APB buses clocks
: o0 L! E s, C - */4 y Q! U1 z; w! Y' ?; m! R1 z V
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
D; U1 _4 M; S9 Y+ R; t - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
2 ]( I: ~, }; _8 d& M - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
/ x1 C" b0 L0 A) K - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
5 z) X' s& i. U; N - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
& S& Q( N$ u* P2 t, k1 J - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
7 I5 P: U6 F1 \( Z" m( ^+ D - `/ A5 T/ }6 ~, Z
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
6 V! v s( [' m- T2 E - {- \: ~1 f9 J# S! h9 ~8 O6 y% y9 t
- Error_Handler();
+ e5 _3 @' w/ g4 `) y( b - }' j B' F, f/ e; x6 A. i
- }8 I7 j+ O g5 s u0 X5 z
7 k/ X& @" ]5 p- /**9 f3 ]; }/ D6 n6 ~8 p/ V
- * @brief USART1 Initialization Function1 c- D3 y' |# c0 C, e7 n
- * @param None
9 d# u$ U5 @* V - * @retval None' n. b3 k; v2 u: V: {) K( k. @4 B( e
- */+ |$ l5 t, [8 I+ E3 ]9 I
- static void MX_USART1_UART_Init(void)3 @% F# z4 S# R- ?: v* o* C
- {
. ^, f$ w( ^9 S( l; h9 ?% }+ w - : N. K; Z. d7 M& t
- /* USER CODE BEGIN USART1_Init 0 */' q6 a! L* l2 F7 c, I9 I2 b0 {
9 F4 b4 ?$ x; w$ g# a- /* USER CODE END USART1_Init 0 */
8 T, o8 ]6 D S! {" y. i6 x - % |( {# W' B' o2 h* m1 h! q
- /* USER CODE BEGIN USART1_Init 1 */
- w; k$ K8 ~% D- u* e( G, o3 r - 1 Z% i3 q& d3 Z
- /* USER CODE END USART1_Init 1 */! N: l `% U- g; X0 `
- huart1.Instance = USART1;, a, n2 N. W# W/ R+ b
- huart1.Init.BaudRate = 115200;' @/ F/ C7 P# x2 k
- huart1.Init.WordLength = UART_WORDLENGTH_8B;3 I4 }% U) w" S+ w
- huart1.Init.StopBits = UART_STOPBITS_1;; t' K6 M6 T4 W2 p2 Z6 o
- huart1.Init.Parity = UART_PARITY_NONE;- @: `; R! D) x8 S8 p B! d
- huart1.Init.Mode = UART_MODE_TX_RX;
T$ A1 b0 g' O- v - huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
6 L# q9 |7 u8 G. T - huart1.Init.OverSampling = UART_OVERSAMPLING_16;
8 P0 [! t. T9 r+ l+ `6 z- z B - if (HAL_UART_Init(&huart1) != HAL_OK)
& [7 m+ G5 X' r+ g - {5 j- {8 q3 s; V& D- o% u; K4 o
- Error_Handler();
9 B4 `9 @; b5 ^" X - }" S( T% \9 ~# c/ {2 ? r4 }
- /* USER CODE BEGIN USART1_Init 2 */5 T% J2 o2 I/ x' c" e6 J2 c
- U5 l" y, a- c. H
- /* USER CODE END USART1_Init 2 */
. p9 L8 v; c! L4 S6 c& E9 U6 Q$ U - ' v' Z1 J* E1 K4 D3 f& z
- }: a: B+ ~# C1 \- j' A
6 m' \% E$ C( C9 P* J6 a6 e! y- /**: U( l/ U2 N; U: x8 S+ A
- * @brief GPIO Initialization Function4 \9 H- a! f1 g
- * @param None- A; j Q$ C8 l, ]& A( k0 n0 ^
- * @retval None
2 A5 P ^0 a3 ?6 s5 w6 W. w3 a. o5 @3 t - */
* w; ^: e7 e; E4 c - static void MX_GPIO_Init(void)2 ]1 s, W" Y- [+ g5 b- n; [
- {
! R% {; \, Q; I% \2 e" u5 _5 | - GPIO_InitTypeDef GPIO_InitStruct = {0};6 E& C! ?- R# q8 P) D) [0 ^4 X4 S! @
. l$ H7 ]8 R9 w7 C0 z/ H6 y- /* GPIO Ports Clock Enable */! o1 a% H2 [5 X' j% Q) j5 {
- __HAL_RCC_GPIOH_CLK_ENABLE();) l7 y0 D$ R( a$ r4 |
- __HAL_RCC_GPIOA_CLK_ENABLE();. q& V! L, s) h% Y8 w6 `) ^
- __HAL_RCC_GPIOC_CLK_ENABLE();
) n9 n1 G$ p U/ a; E - /*Configure GPIO pin Output Level */
, n) {/ `1 a1 ~! f, j
, J/ ^- w! D1 F3 E: ~0 }* B. F
8 X! f" x: \8 I; O! ~. E2 L- /*Configure GPIO pin : PA15 */- q% N7 z+ [/ ~/ g, m- i0 ^
- GPIO_InitStruct.Pin = GPIO_PIN_15;
) v; n/ e4 B% Y/ S - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;; B6 [8 t( @8 d5 J3 p' Q
- GPIO_InitStruct.Pull = GPIO_NOPULL;( {$ Z y7 z8 J, `4 Z% ^! Z
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;& S3 C' \ K u5 B& L4 x5 b' Q h
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
( E* u E% }6 l: N, ^+ e - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
# n2 i, u5 B8 y, z1 { x7 e- T - . L$ b5 y! U) N5 ?/ P
- E+ ?# y3 Z0 V' }2 D
- : S3 u0 D5 z Z* u
- /*Configure GPIO pin : PA15 */
- ~1 T( G" R; c" y. L/ b. s' B - GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_2;
6 x5 ^0 h3 ]/ l- S - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
' c, |9 }5 k4 X \2 j0 h - GPIO_InitStruct.Pull = GPIO_NOPULL;
- y" P0 Q- C4 y D' M6 _* d - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
" a/ q7 R; J5 P6 K# X, @& i - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
. B6 J8 n* E7 y# @& ^ - HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_2, GPIO_PIN_RESET); 3 p3 K" R& x: [ q- h
- }
, K# m9 X0 s& r0 |' X
/ t1 M: J& B8 R$ d; O0 m- /* USER CODE BEGIN 4 */
' t) Z2 d! T: `
; h6 F, W S T$ N! p/ Z- /* USER CODE END 4 */
" U1 z& ]: j9 _5 x8 d8 V8 \
3 p2 g6 {& @' A" Z8 j- /**
+ q% z4 K( G- J5 l' B$ t - * @brief This function is executed in case of error occurrence.( h+ R/ m7 p8 [5 b a' Q3 y* f
- * @retval None
; C3 `8 s4 x, Z) g - */# f* [; P) Q9 x1 r
- void Error_Handler(void)% P# r% V1 ?! q) X1 S
- {
, b( w. {! z2 ]2 j - /* USER CODE BEGIN Error_Handler_Debug */3 M- L5 t8 ~2 ~* A8 r/ [
- /* User can add his own implementation to report the HAL error return state */4 G$ ^& F K" M5 u; _- z0 b- W
- 4 a" F6 }! T8 j
- /* USER CODE END Error_Handler_Debug */& F6 r8 X7 x% j* H& g, r1 t& k
- }8 x( v5 Y7 {3 L6 i) d4 c
& w8 W/ _0 ]* C7 i( F- #ifdef USE_FULL_ASSERT( w [- w" J. K' e" ~# x
- /**
! I% @5 Q' [& b* P. N! s9 z7 D - * @brief Reports the name of the source file and the source line number
' l7 ^: O$ ?& | - * where the assert_param error has occurred.$ F {* A6 d1 y, S: _ g
- * @param file: pointer to the source file name
+ Q/ W7 C, }0 F7 k0 `" b - * @param line: assert_param error line source number
6 Y6 T0 E( n+ Y# a/ `+ k - * @retval None! p3 |$ Y+ ]1 z0 M4 V- N9 P
- */- S' `% B+ V! k+ w
- void assert_failed(uint8_t *file, uint32_t line), E" t& g( h: i0 \* {
- {
4 h5 t$ h e6 U# e$ a5 W - /* USER CODE BEGIN 6 */
& z" j' c) v. n3 c9 ~, ?6 } - /* User can add his own implementation to report the file name and line number,
+ ?3 I* b) _' ~/ D$ X - tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
* ^7 q5 X, e4 b& y5 s' s# S - /* USER CODE END 6 */. V! x, ~' _% u# L5 x7 G+ M% o+ {
- }* @" R8 l' g/ m6 N ^3 M# X" _
- #endif /* USE_FULL_ASSERT */
4 E& p* A" B' t: a. O) X - 2 V U, v+ h( @7 X
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/8 h6 T9 g+ ~4 J, H/ s
- void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef *phost)" [# w. ]& Q3 d) `* T/ X% C2 R
- {- O( F ? N [% A* T7 }6 B
- recLen = USBH_CDC_GetLastReceivedDataSize(phost);
* Q% {2 I! |" y9 Q -
$ i1 W+ ^, W X" e* b: j5 [' a- \ - HAL_UART_Transmit_IT(&huart1 ,(uint8_t*)recData,recLen); Z$ r2 K& h7 i: x; i
- while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=SET); //等待发送结束 4 m# Q# q7 o/ r7 z
-
+ r0 u" G5 A; u, ?& u8 w( q - memset(recData,0x00,sizeof(recData));& c+ E/ b3 Y' a/ L. m8 O
- }
% ?6 }1 h/ u! _( _
; m$ A N8 T5 g4 O7 N/ l
复制代码 7 A* k3 x/ E: u8 |/ Q
: Q; {* e _# E1 {0 n9 E4 g |