一、串口初始化过程 G0 T6 f4 P* L4 K0 p! m: h$ d
1、时钟使能;
2 Y9 d$ L" T, Z. f6 Q; Z% [$ B& z) k: J' C% y9 p
2、GPIO初始化;
; X$ j5 }8 Z- \
Y! o; U8 I' ` K# f0 i. ?: W3、串口波特率设置;+ \1 A: u* t6 l2 N3 A* ]3 N& q
, w4 F$ E/ U, o4、串口控制;4 [+ F$ C, {& K, ^& L- s5 P
' r6 f3 m' g% ^: {5、数据发送与接收# o, r% m' X( |2 t! f* W1 Q
y8 N z8 z3 N& f
+ B1 F% s' m, _9 o8 |
二、几个重要的串口函数" |6 v5 k4 {% t$ L; k1 X
- HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart); // 串口初始化" D# U3 w. H! J4 X1 @( `* m
" w) w+ ?& K( x% Z- HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout); // 串口发送: w0 W+ D& g+ f
- 6 q. W& i! z1 n4 u Q
- HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout); // 串口接收
" ?" p3 J! h. S4 ~ - . b- J5 A5 j- l* W
- __HAL_UART_ENABLE_IT(__HANDLE__, __INTERRUPT__) // 串口中断使能
0 Z5 c; s# u: V T ]
- ?0 I6 W2 C# j1 t- void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority); // 设置中断优先级% S V, ^' G( {( e, ?
- , b0 F5 M) G1 a4 j4 z; |3 `
- void HAL_NVIC_EnableIRQ(IRQn_Type IRQn); // 使能中断
复制代码
3 O7 W6 K7 y- D4 \, j3 L, V v5 }三、几个重要的结构
6 j0 A0 H4 B% }9 q. l9 S- // 串口初始化结构体 包含了串口句柄 波特率配置 发送接收缓存 dma等5 q1 ^$ x9 [ j0 a! r" U0 x
- // 我们只描述前两个基本功能,对效率要求极高可以使用DMA。. I; u7 F, {4 e
- typedef struct2 b9 `( ~$ q A0 h Z) K* J
- {
8 a% A. ]3 l$ l+ y0 G+ Z9 ?$ C - USART_TypeDef *Instance; /*!< UART registers base address */3 \+ r, W! j* t9 `, M2 K
+ U3 O1 h7 Z% H6 c' Y" ~- UART_InitTypeDef Init; /*!< UART communication parameters */+ ^# I% ~5 y' Z- y- t; @
P' P! h( w; \- B' x$ y. H+ N- UART_AdvFeatureInitTypeDef AdvancedInit; /*!< UART Advanced Features initialization parameters */1 }; q7 N2 J4 w7 s6 G; b; o, T
- . [8 }# l2 x8 A0 f
- uint8_t *pTxBuffPtr; /*!< Pointer to UART Tx transfer Buffer */
" A" n, n/ W& m+ h4 e$ x
" |( Y) L% M% u# u @1 V! `9 P5 y- uint16_t TxXferSize; /*!< UART Tx Transfer size */1 x' V3 u4 |* p5 K9 j
( u( R8 l& l; i t& F1 e8 v- uint16_t TxXferCount; /*!< UART Tx Transfer Counter */+ S3 c9 l! Q" j# d& F
( L6 `/ t7 q+ q- uint8_t *pRxBuffPtr; /*!< Pointer to UART Rx transfer Buffer */1 y; u b3 f/ a$ t; Q |6 `. E% c
8 y/ r" d- Z) B) P+ b- uint16_t RxXferSize; /*!< UART Rx Transfer size */" L! ?5 I3 `) g/ |& C+ |3 E, @7 y& `7 t
- / [" Y" z+ ^9 o0 w
- uint16_t RxXferCount; /*!< UART Rx Transfer Counter */
' N# S$ t- G& ]7 z# n% p! o- p
+ _1 \, Q3 a4 K- uint16_t Mask; /*!< UART Rx RDR register mask */ |+ e, K! H6 S6 H
1 z2 Y) N2 P2 \- DMA_HandleTypeDef *hdmatx; /*!< UART Tx DMA Handle parameters */- h" X7 Y2 C. w4 T
# T4 _/ o6 G# Y0 K, n* a- DMA_HandleTypeDef *hdmarx; /*!< UART Rx DMA Handle parameters */
- n* m9 V, \: b
2 w9 p% U: `" D5 h( a- HAL_LockTypeDef Lock; /*!< Locking object */
: |2 V. ^/ _/ i+ } - 1 C w( [+ z( E8 H
- __IO HAL_UART_StateTypeDef gState; /*!< UART state information related to global Handle management
9 N. q' }8 f8 c8 u - and also related to Tx operations.
0 m, B- T) O, N5 I% B, Y6 } - This parameter can be a value of @ref HAL_UART_StateTypeDef */
2 P, t/ |$ Y* x2 N
" M- v. n3 z) l# N3 l- __IO HAL_UART_StateTypeDef RxState; /*!< UART state information related to Rx operations.4 G! v" Z6 |" G! O/ I4 k2 V6 y' p6 K
- This parameter can be a value of @ref HAL_UART_StateTypeDef */
- f" s2 E6 i+ a& `% k
) p7 i9 U! B- `4 M( j! A" C$ s- __IO uint32_t ErrorCode; /*!< UART Error code */# y8 v; [5 p- |1 _' c( K# @
. B9 G* V; d; @" t6 b8 R6 d) N4 H- }UART_HandleTypeDef;
复制代码- // 串口的操作句柄 如 USART1 USART2 USART3等
6 S9 g% t, @; E. V. s4 C; A/ E4 d- j - typedef struct, s: U7 D2 a, y- o# M
- {: L3 F( a* ?0 v' C: h- y
- __IO uint32_t CR1; /*!< USART Control register 1, Address offset: 0x00 */ # i A4 j; f4 r
- __IO uint32_t CR2; /*!< USART Control register 2, Address offset: 0x04 */
1 [) ?" u* @4 h1 S - __IO uint32_t CR3; /*!< USART Control register 3, Address offset: 0x08 */
0 r' P) o8 x6 e - __IO uint32_t BRR; /*!< USART Baud rate register, Address offset: 0x0C */ ' v' D X$ Q. U# s5 m) u, c. J
- __IO uint32_t GTPR; /*!< USART Guard time and prescaler register, Address offset: 0x10 */2 [3 O9 _, v" Z! f8 S
- __IO uint32_t RTOR; /*!< USART Receiver Time Out register, Address offset: 0x14 */
% k& O1 f7 N" L* j4 W& e0 u, b - __IO uint32_t RQR; /*!< USART Request register, Address offset: 0x18 */5 H$ x- P# N6 N
- __IO uint32_t ISR; /*!< USART Interrupt and status register, Address offset: 0x1C */
) V( |( H) o6 V9 u3 J, \" z1 P - __IO uint32_t ICR; /*!< USART Interrupt flag Clear register, Address offset: 0x20 */
9 M0 O0 P- `7 R. L: O - __IO uint32_t RDR; /*!< USART Receive Data register, Address offset: 0x24 */7 b" c: K8 F i5 X; g: F* Z/ F
- __IO uint32_t TDR; /*!< USART Transmit Data register, Address offset: 0x28 */7 E( d5 \$ d, S; d
- } USART_TypeDef;
复制代码- // 设置串口的各个参数 波特率 字长 停止位 奇偶校验 收发模式 硬件流 过采样1 U3 E& B( Q3 ]! V# D# i
- // 字长:8位/9位9 T( Z e& H( `0 |3 q6 `
- // 停止位:1位/2位/ R8 a1 s4 I0 F1 n. ^
- typedef struct
+ z1 O' u0 n6 x1 _2 j - { Y# ], l* Y4 b% M
- uint32_t BaudRate; /*!< This member configures the UART communication baud rate.
# _3 A2 B, ?2 a* c% Y - The baud rate register is computed using the following formula: X1 l( o9 n+ Q) a% O
- - If oversampling is 16 or in LIN mode,
( q5 A# F @0 h0 a, u - Baud Rate Register = ((PCLKx) / ((huart->Init.BaudRate)))3 m' V4 F |6 F) I6 X
- - If oversampling is 8,
( M. H9 K( z2 r9 X6 l - Baud Rate Register[15:4] = ((2 * PCLKx) / ((huart->Init.BaudRate)))[15:4]
% a8 c R# {) @5 Y6 v9 e - Baud Rate Register[3] = 0) v+ a/ g9 s5 X* M
- Baud Rate Register[2:0] = (((2 * PCLKx) / ((huart->Init.BaudRate)))[3:0]) >> 1 *// N1 o: b. A: b- A5 k, n
& ~9 W$ B! |8 d0 o- uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.( J8 k K* ?5 t5 G H; N
- This parameter can be a value of @ref UARTEx_Word_Length */
2 I& o$ E: u7 W
! ]( X* y$ R6 I- uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
, |5 x) F0 k0 }7 |8 X' t$ i6 z - This parameter can be a value of @ref UART_Stop_Bits */
7 G. [( }, A$ x( [% O( K" O - 6 W5 `! s' l. o: z9 }* F a6 J( P
- uint32_t Parity; /*!< Specifies the parity mode.3 X& e" C( ]! B, d" D9 R, }" E
- This parameter can be a value of @ref UART_Parity6 Q- ^% t3 |" T
- @note When parity is enabled, the computed parity is inserted: E8 {! ~; v$ o' T. b5 `
- at the MSB position of the transmitted data (9th bit when
- s% O# b- y3 u) @7 z - the word length is set to 9 data bits; 8th bit when the
: E" P1 P4 @1 n6 v! h! f - word length is set to 8 data bits). */5 w# p5 y, K) W3 {+ c2 w
4 @& s4 C \4 T4 ]- uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
7 E5 c/ _6 Q6 @8 Z9 q: [ g - This parameter can be a value of @ref UART_Mode */) S5 D9 G) t5 D, A: X, [1 B) S
- 6 P" Q1 c) P$ \& j& b
- uint32_t HwFlowCtl; /*!< Specifies whether the hardware flow control mode is enabled
: {8 }3 |9 w$ T3 b - or disabled.
! p. t! l; w2 ~: J: R - This parameter can be a value of @ref UART_Hardware_Flow_Control */7 x3 @" e# t5 J0 ^2 R
- * ?+ `+ `$ Y/ c1 e! p* c- x
- uint32_t OverSampling; /*!< Specifies whether the Over sampling 8 is enabled or disabled, to achieve higher speed (up to fPCLK/8).2 G4 H/ f! H3 f' h, K) [
- This parameter can be a value of @ref UART_Over_Sampling */
; {) i. D3 F- `- U( p8 @
- F, J5 H5 Z0 H6 v5 l) W- uint32_t OneBitSampling; /*!< Specifies whether a single sample or three samples' majority vote is selected.; K! N* ^0 r" H4 b( O6 E: I& @
- Selecting the single sample method increases the receiver tolerance to clock1 U5 ^6 S5 ^6 P
- deviations. This parameter can be a value of @ref UART_OneBit_Sampling */" A5 P: Z2 L; e& @& {/ l1 O
- }UART_InitTypeDef;
复制代码 四、基本接口设计# ?8 O7 u. ^ T( Y0 }: T% m
我们使用中断接收,普通发送。中断接收到的数据放入队列中,在外部可配置每路串口的功能。3 Z7 S6 j5 W3 Q/ F5 D& i
# K5 f2 y2 k! o( G
- // 抽象出一个串口设备结构体7 D3 E; r4 V/ B* ?
- typedef struct
. W2 p A& K' o3 ~- `8 C; }" { - { z/ T6 y5 G, i/ c
- UART_HandleTypeDef handle; // 串口句柄
; m' k- H, }: r - uart_queue_t recv; // 接收队列
2 Q" G& Q' D, E% W9 D$ x/ g% D - uart_queue_t send; // 发送队列
" O/ y( i1 M# w+ K2 \9 n# L- P - uint8_t ret; // 接收的值
% e& ]& n! i6 s9 E" D: w - } uart_dev_t;
, }# M7 d2 J+ B: I7 g7 B6 ~) s$ k
! t5 G$ E/ g7 [& K8 S" c- static uart_dev_t uart1_dev;
: I+ Y7 s3 s) `! q- u. { - static uart_dev_t uart2_dev;
复制代码- // 对外只有通道 不再包含USART1...) n( U5 Y- h! w: M) \; ?/ I
- typedef enum
! g% F1 }$ b, L3 _8 N - {
5 ?' G9 x# x3 J' ~ - UART_CHANNEL_NONE,- d7 w0 x8 Y: d/ ^% B0 |0 v
- UART_CHANNEL_1,
# C2 C& {8 _6 y. ]/ V - UART_CHANNEL_2,4 L4 [$ s' ?! M
- UART_CHANNEL_NUM: {3 z Z' g7 t! d& l+ l" i! _
- } uart_channel_t;
2 Z; ^ ?8 ]. I) Q% u$ s
; ^$ z2 ?" q; [0 h) c8 m; {- // 宏定义串口的基本信息 之所以这样写,方便移植修改1 u4 y' B* }( ^: a
- #define UART1_CHANNEL USART1: r6 w, B/ b( w
- #define UART1_PREEMPT_PRIO UART1_PRIORITY
4 @: N7 `; ~% H) K; d - #define UART1_IRQ USART1_IRQn- V% k: E5 `* G
- #define UART1_IRQ_FUNC USART1_IRQHandler
/ Z$ u5 t/ p9 C8 a. U2 I5 \0 L1 F - #define UART1_CLK_ENABLE() __HAL_RCC_USART1_CLK_ENABLE()" ^, H5 a3 L/ u4 z0 _1 H5 i9 y
- #define UART1_TX_PORT GPIOA5 r c* B& y9 e( p: r
- #define UART1_TX_PIN GPIO_PIN_10
9 {4 t _2 M$ _" o" L$ g - #define UART1_TX_AF GPIO_AF7_USART1
4 i; v+ F! k& _ - #define UART1_TX_CONFIG() GPIOConfigExt(UART1_TX_PORT, UART1_TX_PIN, GPIO_MODE_AF_PP, GPIO_PULLUP, UART1_TX_AF)) \% q3 R6 \% Z$ q
- #define UART1_RX_PORT GPIOA, B# F) W* H8 \2 \
- #define UART1_RX_PIN GPIO_PIN_9" ^7 g' _: k c9 m
- #define UART1_RX_AF GPIO_AF7_USART1% E9 B; d" a+ ^0 @7 k4 [# b
- #define UART1_RX_CONFIG() GPIOConfigExt(UART1_RX_PORT, UART1_RX_PIN, GPIO_MODE_AF_PP, GPIO_PULLUP, UART1_RX_AF)
1 v& R. [) C( L: P - 5 p" J; F6 y) X1 F. g: c
- #define UART2_CHANNEL USART2
6 D5 @) u4 g/ P" o, a6 T - #define UART2_PREEMPT_PRIO UART2_PRIORITY- v/ q% a. a z- p9 `- B9 X
- #define UART2_IRQ USART2_IRQn1 T, Q1 n N4 ~/ A* T( K* U
- #define UART2_IRQ_FUNC USART2_IRQHandler4 x8 Z" g' g% A, F/ z6 h
- #define UART2_CLK_ENABLE() __HAL_RCC_USART2_CLK_ENABLE()
0 N9 |0 @- c7 p' `) R - #define UART2_TX_PORT GPIOA
; }) |5 m) _; N1 o" o: M - #define UART2_TX_PIN GPIO_PIN_21 e- [$ g) C% E' \
- #define UART2_TX_AF GPIO_AF7_USART2 U$ `, o: x+ E
- #define UART2_TX_CONFIG() GPIOConfigExt(UART2_TX_PORT, UART2_TX_PIN, GPIO_MODE_AF_PP, GPIO_PULLUP, UART2_TX_AF)7 h: O2 e' S1 ?
- #define UART2_RX_PORT GPIOA
/ X# Y6 v2 t" J, H+ [' J. i' G; h4 o' _ - #define UART2_RX_PIN GPIO_PIN_3
$ X/ [0 Q3 Z) P# L6 X1 ]2 p/ } - #define UART2_RX_AF GPIO_AF7_USART2
* Y( y: e+ X/ m0 G* |. r* y! ~$ c - #define UART2_RX_CONFIG() GPIOConfigExt(UART2_RX_PORT, UART2_RX_PIN, GPIO_MODE_AF_PP, GPIO_PULLUP, UART2_RX_AF)
复制代码- // 串口的基本操作 + b" f6 i2 M+ m) [4 x5 r2 D+ {
- // 串口19 {. W* j" V1 y
- static void uart1_var_init(void) v# E I; h# ]) ]
- {
/ ]+ S: r* [# p - uart1_dev.recv = uart1_queue_recv;& e( S- `6 E+ ^0 N- T9 l1 @
- uart1_dev.send = uart1_queue_send;( _, R# W: U0 b; _( W- `
- ; T6 P u+ N4 A1 N2 L% `3 L* e$ I
- UartQueueInit(&uart1_dev.recv);7 a! @, p% B7 }: J
- UartQueueInit(&uart1_dev.send);( U: ? k3 ?- ~% _! m0 |
- }5 b6 r" a2 }8 W3 P% ]" x
& k8 j- G/ F5 m5 G- static void uart1_gpio_init(void)( g9 q2 z4 j) N. @" T1 B
- {+ I: L. Y, x0 r1 \: h1 t
- UART1_RX_CONFIG();
' E' ?8 u: l: e0 s1 p - UART1_TX_CONFIG();, E& V9 {: L! `2 q) k1 u
- }
5 P; {9 r; y; Z0 ?+ |/ d - ! T7 v* @; Q# e; X9 Y$ S% }, u
- static void uart1_mode_init(uint32_t bound)4 f5 `1 O; r2 R* i
- {9 X, `& q. S0 w- M1 ~
- UART1_CLK_ENABLE();
+ _; u, J9 H& j. P9 V - 3 E6 N* s3 i) e
- uart1_dev.handle.Instance = UART1_CHANNEL;9 T3 Y* A2 u3 l7 C, f5 f
- uart1_dev.handle.Init.BaudRate = bound; // 波特率0 J" c9 N; j9 R( R- }9 n
- uart1_dev.handle.Init.WordLength = UART_WORDLENGTH_8B; // 字长为8位数据格式5 v& M0 l: Z2 D$ l L
- uart1_dev.handle.Init.StopBits = UART_STOPBITS_1; // 一个停止位
9 B2 m- v& Q2 P& Q0 M- m" I - uart1_dev.handle.Init.Parity = UART_PARITY_NONE; // 无奇偶校验位" u) _6 s1 b6 v! ?' R
- uart1_dev.handle.Init.HwFlowCtl = UART_HWCONTROL_NONE; // 无硬件流控
* i0 U3 s# n# O2 P8 q8 e - uart1_dev.handle.Init.Mode = UART_MODE_TX_RX; // 收发模式& P2 y- m2 S- l* z4 s
- HAL_UART_Init(&uart1_dev.handle); // HAL_UART_Init()会使能UART1
1 ^% t f, U2 F* s0 \0 s. y3 Z - }+ _. E5 n+ f+ f0 F
- ! E* k6 v' [8 Y5 ~$ h
- static void uart1_nvic_init(void)* x9 w, |. ?- l% a. ?+ R
- {
* i5 n0 D1 i: n$ N4 d" I8 N/ X - HAL_NVIC_SetPriority(UART1_IRQ, UART1_PREEMPT_PRIO, 3);
& I4 v1 d7 h+ K4 p* M3 ^' ]# E- G - HAL_NVIC_EnableIRQ(UART1_IRQ);
/ C# F7 S9 g. b; J" s3 A% D* r
9 L7 ^' \- K$ a$ J6 j. I9 c- __HAL_UART_ENABLE_IT(&uart1_dev.handle, UART_IT_RXNE);/ U6 N( a- _$ B, T" V7 i" u! D
- }
5 y# A( X" Y; |* j9 X3 L% v
3 H e3 u& N" f5 Y. x3 V- // 串口2: L) q, [9 o1 S. U6 X5 d
- static void uart2_var_init(void)
3 I$ L& C0 F. ^0 z0 B# o - {
. t0 k1 N* ~9 X4 c2 A1 v( z* A- q - uart2_dev.recv = uart2_queue_recv;
5 g0 g$ z: Y5 W& q9 O( E - uart2_dev.send = uart2_queue_send;
. p$ _8 }+ v; d" ^ - / P" D: @9 `" a& w
- UartQueueInit(&uart2_dev.recv);
' [ M. I2 a4 r) }# i7 A - UartQueueInit(&uart2_dev.send);
+ ~3 ?! G- }. |# C7 r, q - }
( X9 ^. h& Z) n2 B8 G6 t6 a
: M' \8 v% b% t: r& j( j- static void uart2_gpio_init(void)
4 {/ L# w9 t8 b2 s" M - {
1 j2 J5 m- b K4 z+ L; A! x- g' N - UART2_RX_CONFIG();
. E4 `" i V: g( r: E& b - UART2_TX_CONFIG();
9 `9 {# w7 h: `: J) p5 { - }1 i: B6 G S# O
' x0 F2 q' m: F; Q$ a h$ g. I* q9 R8 Z- static void uart2_mode_init(uint32_t bound)
- y+ U: E2 I3 l( ?5 Z - {/ t8 |( \. g' ^" {/ n1 Z" X
- UART2_CLK_ENABLE();
* [9 o% V' E) P: Y+ m8 x2 p - 4 F, y5 W! r, I9 z3 M$ A* Y
- uart2_dev.handle.Instance = UART2_CHANNEL;
6 l5 E3 O& `6 Q8 |; ^) p - uart2_dev.handle.Init.BaudRate = bound;
0 l( q+ Y& |' D5 x2 M - uart2_dev.handle.Init.WordLength = UART_WORDLENGTH_8B;
9 j f2 z* K9 o9 t1 P) T - uart2_dev.handle.Init.StopBits = UART_STOPBITS_1;$ R6 ?5 ?0 I% r1 A W5 D
- uart2_dev.handle.Init.Parity = UART_PARITY_NONE;8 G+ {& }( ?1 `) F' G% E
- uart2_dev.handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;7 ^% n4 s# p, g
- uart2_dev.handle.Init.Mode = UART_MODE_TX_RX;
9 z( g! l; t9 o! y( K - HAL_UART_Init(&uart2_dev.handle);# i) S9 N4 u! {8 R' _
- }
) H' J& Q" N. H/ v1 b' g) t. X - K" h( r) t/ g1 {1 c' H* p' L' O3 ~- S" G6 |
- static void uart2_nvic_init(void)% P& X$ X+ `: N' G
- {0 I: \$ H x; g/ Z
- HAL_NVIC_EnableIRQ(UART2_IRQ);- c2 l4 h9 u; B& S8 v1 `8 @
- HAL_NVIC_SetPriority(UART2_IRQ, UART2_PREEMPT_PRIO, 1);- G! p' {) m4 j, w* {
5 k b" ^+ t& [7 D- __HAL_UART_ENABLE_IT(&uart2_dev.handle, UART_IT_RXNE);; P: l# ]7 q& H( u- j" [7 t
- }
复制代码- // 抽象出一个初始化的结构体 每个串口都有的操作
1 y s4 T T, u* k! I9 c& O9 C - // 定义一个串口的列表. J0 A N( G g& B7 k, ^: H* h, L9 l1 y
- // 下面的函数就是扫描列表 到这里为止,这些接口都是给内部使用的,外部文件用不到。9 V1 S$ V S# T: r, S
- typedef struct% K! G& L x0 J) V7 P
- {5 i8 B* H) q+ ] u7 R
- uint8_t channel;
! M; n3 S g" J6 u0 g) Z5 l - uart_dev_t *dev;& C; A: y: C) m L' E
- void (* var_init_cb)(void);
* D* a6 @" M- x1 C2 ^. j - void (* gpio_init_cb)(void);
, O8 a5 _, k& k: ? - void (* mode_init_cb)(uint32_t bound);
8 t2 K0 j% E3 A1 `7 ?; e8 f - void (* nvic_init_cb)(void);
2 i1 J9 ^( A! |8 ~% h/ \( L7 D7 ^ - } uart_config_t;' w/ Y$ i7 H1 Y6 N
- ( R+ }/ L, _5 o6 e' @9 V3 }0 U
- static const uart_config_t uart_configs[] =( a- Q/ i9 }- B3 i3 l: `" }
- {0 h* H4 u' K6 M2 j& M3 f3 K
- {UART_CHANNEL_1, &uart1_dev, uart1_var_init, uart1_gpio_init, uart1_mode_init, uart1_nvic_init},% s; A4 K6 I) k5 K" D" m# J+ m
- {UART_CHANNEL_2, &uart2_dev, uart2_var_init, uart2_gpio_init, uart2_mode_init, uart2_nvic_init},
: |, L) C/ z" g* k1 ^ - };
# W$ H3 D& c5 U& e9 X+ j4 ^ - % }# E' x: u9 @0 Y* F" N; _
- static uart_dev_t *uart_dev_get(uint8_t channel)6 [, f T; C2 y0 S, {
- {
2 ^# e* y$ \" y7 I, k2 Q - uint8_t i;
, a2 N6 i1 U& h" P* Z0 c) n- e - ! \/ n, F0 F( q! Q" O' J0 X
- for(i = 0; i < ARRAY_SIZE(uart_configs); ++i)
, |4 p6 T) Y. b* D l2 a; S - {
' q$ I' L& B3 Y$ l4 G: T - if(uart_configs<i>.channel == channel)
9 x- o: q2 q# I9 M - </i> {7 |3 w1 I$ i& Y4 ?7 Z
- return uart_configs.dev;
) z% }3 a$ G2 i - }9 r4 X; V \' y% M
- }* }5 d" T. c/ `. D0 b5 S1 ?
- c, x; _; p; d$ ^" W1 }% Q! g" W- return 0;: {- ?- `* M6 n2 C3 @+ H
- }
( c- H! x! u* E9 _' a
* u4 o' y* ~& k* U4 C9 `- U4 g4 A3 t- static void uart_var_init(uint8_t channel)
- I) F X7 U8 f0 F5 m( u+ ~( F - {; @! P' m7 P+ Y$ z' \
- uint8_t i;
3 }* g2 c+ O7 G1 {) h1 f- I - + S% _& z) M! Z# _3 j2 L/ k
- for(i = 0; i < ARRAY_SIZE(uart_configs); ++i)
1 c3 }' |+ {1 o - {
$ t1 s& `0 B& t( c Z - if(uart_configs.channel == channel)8 V) u& u3 \7 L. g3 S6 |. W0 o
- {" a8 g, @. w6 y4 _! U+ y6 f+ q
- uart_configs.var_init_cb();% q- ~& ~) q+ G' J: ]
- break;
^! ?" H! O: p8 \/ e) {7 j - }
7 Z, H h1 J0 T5 a1 x+ d* D - }% R9 \+ N6 u; L( N' U$ q5 B& W( r2 ~3 y
- }7 W) `: X- ^+ {) i1 m
8 O3 M3 s" d& ]4 [1 U9 j6 Z; X- static void uart_gpio_init(uint8_t channel)+ Z2 \( j8 w; b; Q& }5 i5 Y! w
- {3 ^7 q5 V& y7 j: q7 j
- uint8_t i;! K! v- Q$ g$ C8 i( e3 ~. \& q
- 3 n; z; B6 f3 d: S$ ?3 Q$ w+ u: h5 i
- for(i = 0; i < ARRAY_SIZE(uart_configs); ++i)
, |' c; q4 O+ t: C) M; c( L- g1 m - {
8 A+ e' h: K9 _ - if(uart_configs.channel == channel)6 ]7 B6 j/ `/ Q/ v; P5 u
- {) i( f; r6 W; B, }
- uart_configs.gpio_init_cb();" |5 M, Z1 M4 @5 q% T- X
- break;
. R. R3 E% Q5 B' N - }
2 b) ~* Q3 c' v' u, G" h: G+ v - }% S* N; p- w1 i$ A1 S" ]9 d9 b
- }! @6 z; Z8 X; @: ]( u! u. K. b* ?
5 E7 S) D1 }# X. P |3 Q+ u- static void uart_mode_init(uint8_t channel, uint32_t bound)1 E. T+ ?9 [* X& P
- {1 f0 L6 I2 m! |# t0 Z5 h6 y c
- uint8_t i;
# L! t+ T- Q- g. }# W- }1 V5 K - . g0 C3 k% ]9 T4 i: |. J
- for(i = 0; i < ARRAY_SIZE(uart_configs); ++i)
2 s8 ~0 c) C4 V0 I8 F- \! G - {+ H9 q6 [! G0 p( I7 E# ^
- if(uart_configs.channel == channel)
3 t* ]3 U' r( ^# S - {8 m) X$ ` r5 @0 h0 c. w
- uart_configs.mode_init_cb(bound);
2 N9 _ k% Y5 T g - break;1 v9 a) ~: C9 ^, U) b4 f
- }
! E7 Z; b* x; |2 B' A( t' I" \8 } - }
( S1 d8 {- u: a1 r - }
2 v4 n/ b" c5 _# Q% C
; h4 U* x0 d7 H& K, j/ m- static void uart_nvic_init(uint8_t channel)/ |3 s, \. q. U, d) f
- {1 e$ Q: k; }9 k, w5 t! e
- uint8_t i;+ v" G/ F& R( L7 m
- * J, f) f5 Z! l K$ t% [2 }
- for(i = 0; i < ARRAY_SIZE(uart_configs); ++i)
& M* }/ v3 ]6 Y, L8 }- V3 U - {
8 I- z+ r# p$ {0 `% k# y8 Y - if(uart_configs.channel == channel)
1 N) p P B; Y( w, O8 U5 ? - {
% K+ U9 q: U! Q" ]1 r' M - uart_configs.nvic_init_cb();' b0 b* I0 O8 H% ?
- break;* f" q2 g7 q" @( F Z, u
- }- d1 N/ X* t9 }, \3 i
- }
3 S" D% K Z9 } - }
复制代码- // 这里的函数就是非常重要的了,都是给外部使用的。
( D ?; H3 q7 r' f0 P- D - // 初始化函数,同步发送 异步发送 接收处理
( f# g$ S4 n: [, Q. T# | - void UartInit(uint8_t channel, uint32_t bound)) x) u$ s1 I' q( n3 e
- {; \! V$ Y0 r e8 I5 }& J
- uart_var_init(channel);
" C" A! h, _1 V - uart_gpio_init(channel);
& a5 }4 [& Y& U7 O - uart_mode_init(channel, bound);+ s3 |" [+ Z" P1 @9 ~
- uart_nvic_init(channel);
1 {5 m. B" `: }; @- K - }
, Y- ~: C5 b; C9 i5 ^$ P - 3 J& j. s5 N) c) r% k
- void UartSendSync(uint8_t channel, uint8_t *buffer, uint16_t length)! G' a+ d/ w; I# K( z
- {
' H& m6 ~6 g! I& o5 V. h3 D - uart_dev_t *dev = uart_dev_get(channel);
6 s" l4 Z+ `+ \" x+ K
' q' ]8 A- v' J" j4 }+ h2 A7 x- HAL_UART_Transmit(&dev->handle, buffer, length, 10);7 v0 Y1 q0 m! \
- }" W! l9 R9 V2 k# L9 e ~
4 |3 M1 N% F8 ]! ]+ D- void UartSendWriteAsyn(uint8_t channel, uint8_t *buffer, uint16_t length)9 e- d% Q9 N1 U% A @) M0 b# ]3 r1 H5 ~
- {
g6 e" l& D& S8 m: y - uint16_t i;
" `7 W" V' `! r0 r - uart_dev_t *dev = uart_dev_get(channel);
9 E) |8 ?9 m' d3 O" H6 {
6 x- x8 P4 U. q2 z. q% V2 [1 q7 W4 B- if(0 == dev)+ q1 b2 R5 z+ U" i
- {; M1 h" ]9 D$ }9 l4 b
- return;
, u0 B3 c3 }1 V/ b9 F3 u0 C - }# J3 q, |$ e% Y9 Z2 H
2 v% k+ O( H, D0 U+ i9 B3 Z- for(i = 0; i < length; ++i)5 m# v. ~4 I2 d F/ ]( w% {* M
- {7 C. j. V' ^+ b+ s/ z- n
- UartQueuePush(&dev->send, buffer<span style="font-style: italic;"><span style="font-style: normal;">);+ U. v$ t1 Q& Q& k& [
- }
$ x1 y& Y- N1 F$ o: L. ^/ v - }% }% \6 c2 [# [( R3 ~
4 e5 ?2 R. s; N- uint8_t UartSendReadAsyn(uint8_t channel, uint8_t *c)
L1 j3 p, R; b - {0 R6 u& C7 [9 E# d) o
- uart_dev_t *dev = uart_dev_get(channel);
1 y' A7 J0 m( h6 i1 J6 `
K; Q" p' e( ?1 ~* W- if(0 == dev)6 ]3 O- t* U5 Q% [0 M" z! ]! y
- {
% G M0 r9 u" e) ~8 r - return 0;; d$ t2 M; t1 ~" {5 D- Q) m' [* r
- }( ?5 h2 {+ U) @
8 t$ P# `& [# w9 X! a- return UartQueuePop(&dev->send, c);* a' H6 L/ t9 |* |2 J i/ D F7 v0 \
- }# p# {0 E8 \$ p1 f& v2 M
- # t5 L* S! r% p, n. m1 ^
- uint8_t UartRecv(uint8_t channel, uint8_t *c)
" O9 _8 Z$ m# l0 J) f i9 J - {( P- X0 U7 O4 F; s1 u
- uart_dev_t *dev = uart_dev_get(channel);( j! T6 j, I) v" X0 J; o3 H# i% A
- & t O0 n4 a& V3 }0 x! f
- return UartQueuePop(&dev->recv, c);
9 z0 ]3 [' P/ {! j6 y* q - }' L8 {0 g! t5 a
- // 这里我没有使用串口的回调函数。6 @9 P. O- W( A3 b1 N
- // 中断服务函数 接收到数据就加入到队列中。在UartRecv读队列数据并处理。
, D, N* A8 Y$ C9 F: M+ P1 @+ J - void UART1_IRQ_FUNC(void)* z: t4 g+ D4 z, q4 L- ^
- {
( K% m/ r4 b) V - if(__HAL_UART_GET_IT(&uart1_dev.handle, UART_IT_RXNE) != RESET)- P! x2 P( _! G' D
- {1 T Q# v8 X- r, i$ z3 `
- HAL_UART_Receive(&uart1_dev.handle, (uint8_t *)&uart1_dev.ret, 1, 1000);, _# y \% O3 q/ I/ W8 U
- UartQueuePush(&uart1_dev.recv, uart1_dev.ret);( }6 d l" n% b+ i8 ~ i, |" }
- }/ E" w Z" l! l* Z/ f
- 6 y( [( [& X4 k7 f! N
- HAL_UART_IRQHandler(&uart1_dev.handle);3 r ]4 k( V" G+ z
- }
- l2 q5 G4 \2 q9 {6 @. h9 U) L - - c3 D0 E8 R! a, O& g1 a3 S4 L. l
- void UART2_IRQ_FUNC(void). g1 M8 Q! U* V7 n @1 n% k
- {6 U" p$ D" ?6 n: |. S+ t
- if(__HAL_UART_GET_IT(&uart2_dev.handle, UART_IT_RXNE) != RESET)* n3 ~! n/ _/ w8 A
- {
3 Y, p( b, F1 I$ I- f+ v - HAL_UART_Receive(&uart2_dev.handle, (uint8_t *)&uart2_dev.ret, 1, 1000);# I) z' j2 O6 \/ t! H
- UartQueuePush(&uart2_dev.recv, uart2_dev.ret);+ r5 ~3 o8 A& W' p9 \+ Y# p
- } {6 Q4 t$ }* T
- & n2 t6 M: }- O: A9 o
- HAL_UART_IRQHandler(&uart2_dev.handle);$ o9 l. f1 l0 p- _
- }</span></span>
复制代码
5 a. n8 f7 \/ n, ^到这里,串口的初始化,发送,接收的接口就封装好了。
3 J, M0 \ I0 a4 ], m! H( X% {6 ~
7 n! O( D. s$ e裸机:裸机就在while(1)中调用UartRecv扫描数据;
& j7 \& N7 ?' y) S
3 l- s* Z* R/ z/ [) y系统:带系统就在任务中扫描并解析数据。(带系统可以使用信号量去同步数据 -- 这里只提出一种思路)
) b! e' J/ r# j( b
+ ?0 s$ |' S2 h# q8 m9 @9 E/ f串口队列可参考:串口队列
9 F9 S$ f' a5 b- d+ _
e( A; A% K' b9 B! ]' V+ s( F
) @ s5 P4 X0 `
# H _+ z$ b0 M/ d" H* k1 L* w; g: b( t# h) j
& W8 K$ c1 k. ?3 E0 ]! I$ z
|