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