今天主要记录一下STM32H7系列串口的使用,正点原子、野火等各大家都有教程,当然用起来也没有问题。
" W! d4 P# I$ _4 W: ?
% A8 p% _+ s% l0 I' O. R, M解决方法后边有红色大字提醒,直接看后边就可以,如果你没有时间想去了解HAL库的接收思想。7 h* B* g, R+ R: z y5 ?8 w
8 ]2 ^# o% e+ t& t$ \ST推的HAL库,在整个接收过程中,是没有用到串口的接收空闲中断,它的处理有三种,分别是轮询,接收完成中断(每一个字节一次),DMA接收。
# }; `- F7 r+ E) F4 |( N0 O* e! g4 q* a
整个Hal库把接收和发送过程都封装好了,就用最简单的轮询方式看,先看库函数代码:
5 {% w" K! r: X! o4 \+ @) z5 I
- _- A S+ A: G, ~. g7 r- /**
+ o$ R2 F1 x. U. m+ [* y - * @brief Receive an amount of data in blocking mode.
: k. y4 }. a4 N9 c - * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
3 ? o4 b5 [1 M5 M- M! x$ X* r - * is not empty. Read operations from the RDR register are performed when1 P( J1 C, I4 o
- * RXFNE flag is set. From hardware perspective, RXFNE flag and
& Y- L4 a ^* [$ v - * RXNE are mapped on the same bit-field.! D2 s( c- T8 K+ H; @1 @
- * @param huart UART handle.& r$ @5 F, z/ t+ c) p. a: N- p
- * @param pData Pointer to data buffer.1 Q* a' K' o. G0 M9 S
- * @param Size Amount of data to be received., `) }) F! E2 r- W7 H
- * @param Timeout Timeout duration.
* H( S, l; |' w5 M0 |, Z+ e5 `/ y - * @retval HAL status
+ `; G6 J1 s6 D$ c- |- D - */& O# }6 s" g( `/ D
- HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)1 r$ T- X7 h9 v* {
- {* x+ _! E1 B/ g# l: K" A- t
- uint8_t *pdata8bits;
- l, ]. o2 ~' Z5 p/ s h! x) Q - uint16_t *pdata16bits;
9 \7 E5 n) v1 H4 }5 ] - uint16_t uhMask;
( v }- b5 [- b - uint32_t tickstart;
" _2 U6 {( U' p0 x+ M" P - ! B! M; Y7 m5 i% w* R6 E
- /* Check that a Rx process is not already ongoing */
, J ? P$ N$ P/ P Y - if (huart->RxState == HAL_UART_STATE_READY)& O- y0 W/ r- G, L8 t6 F/ M
- {- f/ [/ L: v6 a! Q: } J
- if ((pData == NULL) || (Size == 0U))
7 v5 }' S4 d5 k4 g) D; _6 Q$ e - {# _1 r9 I9 H, ]
- return HAL_ERROR;5 m2 m$ G7 N g$ q0 f
- }: G& q7 Z$ H6 {4 Q
' t) u2 K$ y5 ]3 g$ E; }3 M- /* Process Locked */
6 B+ J$ x+ D- _$ p1 @ - __HAL_LOCK(huart);
' c ^# m6 X2 {( v
1 n7 t: {" d# B1 K, H; k$ q% ~$ t- huart->ErrorCode = HAL_UART_ERROR_NONE;2 L3 Q- e7 c0 ] S$ f& j8 Z3 B5 y0 `
- huart->RxState = HAL_UART_STATE_BUSY_RX;! M4 _/ y0 y. p3 _& b* y
8 Y9 H3 C& [1 G/ v! D' x& m( B- /* Init tickstart for timeout managment*/- w6 p" Q; [$ a, ]6 H/ k! b( ^
- tickstart = HAL_GetTick();
8 V" z' y6 \& l/ g7 g$ y - $ `4 p0 v. {; v3 \; ]
- huart->RxXferSize = Size;
4 M# y3 o5 X* U- X3 Q: ^6 P - huart->RxXferCount = Size;
/ u: @/ O# w; H
9 Z' u9 o/ r _3 g J" @- /* Computation of UART mask to apply to RDR register */
) l' s$ T; ~, h& p - UART_MASK_COMPUTATION(huart);
1 [4 U( v: B* f: \3 J - uhMask = huart->Mask;( A6 ]; l' M, X/ R$ H+ l/ `
- : _# C$ ~% Z& X1 P. k9 ^7 [8 J
- /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */' H3 y4 {4 [ ~; `
- if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
. f1 k1 c4 o; x. f* T4 L - {
, @# U9 q, \% r" n4 h - pdata8bits = NULL;
9 y$ _3 r. C; U+ [+ o$ L" H& G - pdata16bits = (uint16_t *) pData;0 d: ?6 v7 V/ p+ S( e
- }! h1 ]1 x( F7 l8 U
- else
, @- _% X: v* K1 Y- Y - {# w0 O [- n+ X, i; w4 d. h7 H2 p
- pdata8bits = pData;( |$ G, }9 e4 [- u/ B; f
- pdata16bits = NULL;2 f$ @ L& U; q1 x; a2 y% m
- }0 y0 V+ u# ]- F3 v. c) T
X! N: F: X e% o- ~5 F- /* as long as data have to be received */7 q" a* B/ `4 U$ v4 S u' I
- while (huart->RxXferCount > 0U)
0 X) l/ P! T, _ - {
: h" \8 _5 [! r. D4 r - if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)( g; V' r7 O3 ?- R% ?
- {
2 f0 C/ y5 \6 x$ U0 z - return HAL_TIMEOUT;( k# {; m& v1 o; S L
- }9 n) Y' k _; c
- if (pdata8bits == NULL)
$ B" _8 O& Z# B# B2 O - {3 J- S3 S2 t6 e5 ^
- *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);0 p2 P. U$ H W5 ? C3 z
- pdata16bits++;5 M# H9 c$ y. l! D) r6 R' b+ j
- }( X C o4 o- B4 @: \0 R- O
- else
" j) p% _1 p: w, `9 B - {, z' U5 y3 d2 X3 \3 R+ n( s
- *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
7 h `$ y& A- z9 }# w' q - pdata8bits++;. C( R% |" f8 x
- }
+ ?3 Y" R& r+ n! N: Q - huart->RxXferCount--;6 t1 d6 t. i! j; u2 n6 W
- }
" F% s, A% d5 ?
|# G* E# d. f) o; I7 J' M; ]- /* At end of Rx process, restore huart->RxState to Ready */) ?. Q R8 z ]) k# e- |( ?9 k
- huart->RxState = HAL_UART_STATE_READY; k' H: s# ]* j$ B/ q3 O. s: m
- ) I4 Z* \, n! F) t: O
- /* Process Unlocked */
% |) E$ [! R: t4 p - __HAL_UNLOCK(huart);
; l: u6 f* ^: D) ]
; B; l0 b# `( V- return HAL_OK;0 U0 z9 P6 v. s1 z% X
- }
! @& n% N% i! c: }# B r - else
9 }& m. i+ O5 i - {/ f x! H3 g4 i, b X
- return HAL_BUSY;
- b$ `# A3 A( v0 E; z, j7 u1 g - }0 Q& T* e% q. L' B! K; j% g$ X) q
- }
复制代码 ( ]- L. L3 m( \5 z- b$ s0 h2 F! _
简单的看,里边有个while循环,在规定的时间内接收固定字节数的数据,在裸机中,接收过程真就是接收过程,主循环就什么都干不了了,想想,480M的主频,你让我在这个地方等待接收直至完成,本来串口传输就慢(常用115200传输),简直浪费,如果是这样,我为何不用一个51 去玩,所以实际项目中这个方法,只能说拜拜。6 U/ t( T) ?* f. q- N- z( t9 L# ?
& W( R4 a' `/ }8 l& o
但是这个方法作为一个串口接收原理说明是一个很好的例子。
% J8 ?+ Z: m! p0 M: ?9 D8 q
' e" g, D0 i6 B接下来看中断接收方式,继续看库函数代码:
; I! H3 g# M/ Y; t5 W
2 V* u ]1 j+ \8 [1 r- /**
4 x; {: ?/ G% n2 k - * @brief Receive an amount of data in interrupt mode.
+ H" A7 Q2 H- l- u3 e - * @param huart UART handle.% w" |9 E" s5 ]9 ^7 p. ?8 W; |
- * @param pData Pointer to data buffer.1 [) x, ^( V7 T" B- {1 K$ D( t+ ^
- * @param Size Amount of data to be received.% z. O1 F2 ]/ L
- * @retval HAL status
' ?8 Q5 Q* S* x1 s - */
& U: s0 w! P5 D; V3 \. u' {% l - HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)+ e& Q" G' f* c9 B
- {
: I; i/ \3 X* Y6 f - /* Check that a Rx process is not already ongoing */# r- y5 w: @4 }4 L8 m
- if (huart->RxState == HAL_UART_STATE_READY)
8 L8 S+ u% L3 T9 T - {
1 R8 s3 G+ j7 g5 m7 D Z8 @ - if ((pData == NULL) || (Size == 0U))8 |/ S5 V4 c! p0 v5 V
- {/ c* h/ Q9 n0 l
- return HAL_ERROR;4 M# g, v! d/ }% B; q. [
- }6 `1 U- B3 c- M
- : u3 g w. v# D+ m4 T, r5 ~
- /* Process Locked */
4 x* m k3 m8 L) v. V - __HAL_LOCK(huart);
. i4 x- `9 C9 t- Y3 v
6 C& @9 Q& G) ~" m) [- huart->pRxBuffPtr = pData;! e/ \( U5 h5 i5 h {7 F4 e2 H
- huart->RxXferSize = Size;5 s5 ?" U, D) ^" \9 q& ?
- huart->RxXferCount = Size;, a% `4 R. A3 L% j7 l
- huart->RxISR = NULL; }3 l5 i& X9 H6 G$ T
$ G$ a d* o( c! _# `9 Y- /* Computation of UART mask to apply to RDR register */% d' o% O) b) B/ l" \
- UART_MASK_COMPUTATION(huart);' T! i* m b& K- e( T
- G* R/ }* a: e. v C- z7 b* X
- huart->ErrorCode = HAL_UART_ERROR_NONE;
( r+ d0 E, q% a o1 X g - huart->RxState = HAL_UART_STATE_BUSY_RX;5 G2 m; p5 C0 T, ^7 |
- 0 L) W0 C' i/ z. D- X1 B
- /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */) \# e0 e A8 H1 s% @
- SET_BIT(huart->Instance->CR3, USART_CR3_EIE);9 A- h# N9 L6 ?! p6 o
2 l5 B5 E H0 J* N3 h1 ^- p2 U- /* Configure Rx interrupt processing*/1 d. k: ^$ T) c; S0 [4 r
- if ((huart->FifoMode == UART_FIFOMODE_ENABLE) && (Size >= huart->NbRxDataToProcess)) ?" U5 g0 F3 P5 p- m5 z% o4 Z) c
- {# K# i! R, L: X+ M
- /* Set the Rx ISR function pointer according to the data word length *// @3 {' p" W# n
- if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
# j* T' E+ {0 u+ X+ G' ~ - {3 Y [6 f; e- E5 _) V
- huart->RxISR = UART_RxISR_16BIT_FIFOEN;0 J0 N: Y2 U' d3 i* q! v3 ^
- }
# R! B: t: n! V$ a - else" R! h! O! ?0 |5 J: i+ O
- {
# e7 D3 N7 A' r3 ^2 A. ^3 `& r - huart->RxISR = UART_RxISR_8BIT_FIFOEN;3 J9 R V/ b8 M$ O& O1 u+ u
- }: q. n5 L% {! d
3 o }- u2 I- }- /* Process Unlocked */, }$ {1 F( M! k6 B0 c
- __HAL_UNLOCK(huart);
' a* h% h3 h+ B- G& }' L8 w2 T - 1 H6 w% c9 b, B! z" m
- /* Enable the UART Parity Error interrupt and RX FIFO Threshold interrupt */: W. q$ x0 G& T# V6 F0 ^: s
- SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
( M [: Z- ^+ Z - SET_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);# o7 ]; w- N: F! h3 k, F; n
- }: p2 b B' I% b/ s
- else
1 F5 u) W0 a8 T - {0 z( k2 A D* F6 B K
- /* Set the Rx ISR function pointer according to the data word length */6 ]+ \/ F: Z1 q% |4 k9 J% ^3 V
- if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
- J! x* [$ `+ t7 J* g% m n& T% C - {* t4 R) ~# U4 k) D
- huart->RxISR = UART_RxISR_16BIT;: C7 \0 a( v0 q6 ]7 g
- }9 _9 h4 @" H% i- p+ C) {$ j8 J% Y
- else$ l/ C: D K, F' t6 Y+ }2 u
- {
2 ^( `8 q P ^ - huart->RxISR = UART_RxISR_8BIT;
1 r7 t! K! |( O+ m - }1 r! b* g+ |, a
+ l1 C8 \9 U! ?& c; J0 m* r- /* Process Unlocked */9 P- p5 p! z" @6 M
- __HAL_UNLOCK(huart);' {/ y/ \: { R$ e& @1 K" `
- - t2 K9 V# ?% |1 r
- /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */) R3 |4 _2 d7 C0 e
- SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
( ?7 C$ b' Y; a8 Y) \3 P$ h - }
" l ?& E G; x& L+ `
, h2 H* s' ]' r& ]9 e- return HAL_OK;
3 T; `( u5 f- ~2 c - }* G) o% x2 i" F: a7 \8 @+ z
- else
2 i) M* J, v* k, P F L5 r - {
6 ~! V* m T& r - return HAL_BUSY;1 O; r2 N4 ?1 K& X; \1 j1 ?
- }4 R5 z, e# q2 L* @) R
- }
复制代码 # P" t, X# h2 H) t8 k3 L! l; W
乍一眼没有看出什么问题吧,它仅仅做了一个,启动接收状态,并打开接收完成中断,看函数名就知道接收过程是在中断中进行的,中断函数比较长,咱就不贴完整的代码了,只贴接收部分相关的:! ]; ^* @) }$ ?; X) C" g4 K3 g* }
' F6 H! K6 I+ a: O- /**
3 |" b- k1 p0 O$ |1 [ - * @brief Handle UART interrupt request.; d) Y) `: m2 v1 I0 L
- * @param huart UART handle.9 i5 ?, c: q& ~2 l8 R) X
- * @retval None8 b) L: p# T3 j! ^2 l
- */! B8 p9 Z2 F0 ~4 l0 o3 P7 g5 N
- void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
e5 a8 l1 V. d o3 c7 }* m - {
9 Q; o: |5 \. _, c8 R1 H - uint32_t isrflags = READ_REG(huart->Instance->ISR);
; `4 s+ r5 u7 p: V - uint32_t cr1its = READ_REG(huart->Instance->CR1);9 m, T, f' m; W
- uint32_t cr3its = READ_REG(huart->Instance->CR3);1 U" \* J& ]! M! p1 ^
- & n; ^. d; C( \ `
- uint32_t errorflags;) G. t# ~9 x) q+ Q# s" f
- uint32_t errorcode;$ d. O, E4 X6 a& {) j
5 @& q: p% x$ s% S2 O* B2 M- /* If no error occurs */+ M; \' y" e. T5 E1 t A8 u
- errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
" d7 K5 Y' t3 A5 h/ M - if (errorflags == 0U)
2 [. n: K; o, ?* D - {# ]9 }" ], ~* [, ]+ M s/ F' B
- /* UART in mode Receiver ---------------------------------------------------*/0 j n( _ H( \
- if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
* |7 y r/ o# W, O - && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
5 j# w- ?9 F/ f @: q4 F% | - || ((cr3its & USART_CR3_RXFTIE) != 0U))). k2 T# `" M8 @, I2 Q
- {! K3 `! _ m! k, o+ `6 d$ K
- if (huart->RxISR != NULL)9 u3 e* P0 ^6 g# ^1 U$ R
- {
' f+ d. d1 S5 Y: c! ]3 B - huart->RxISR(huart);5 x. e) _9 @$ e. K) Z) K
- }: z' \9 x6 S; ?
- return;
; K, u' L0 P; `1 i6 Z( }% d - } N! m ~) L5 z' @- _5 ^. V
- }
+ g6 `/ c; K; h - /* 这里省略了其他关系不大的代码 */2 I+ O, i/ E. ~5 G% s
- }
! N. p2 x; C6 X% Q) Q5 i3 E - 从这能看出的是接收过程在huart->RxISR()中,因此我们还要看这个代码,上一段代码的倒数16行处对它有赋值,我们也需要去看,函数名是UART_RxISR_8BIT(),又要贴代码了 ^v^ ^v^:
" R; R6 {' }6 N* I3 i0 p - # T9 P l- ]0 t- u5 { C o- h
- /**
5 x. L+ M5 B8 _3 q* h- _* Y - * @brief RX interrrupt handler for 7 or 8 bits data word length ." u& c$ W& T3 Y
- * @param huart UART handle.: y; H9 \( J4 L9 s8 q" M# C+ A
- * @retval None
" @! I4 ~$ [3 K, [ - */
/ u3 q! y. _% F6 T/ r c: I - static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
$ n4 m6 K- |* w2 {# A( ~/ |+ y; G - {
1 l4 x, p5 y* Q* \ - uint16_t uhMask = huart->Mask;/ {1 F8 I6 Q7 \9 X2 I" V1 q
- uint16_t uhdata;' L: Y9 t9 U6 ]4 u6 w A# f4 y
- $ Q/ W+ g2 I' |- L$ z2 y. z+ ^# X1 L
- /* Check that a Rx process is ongoing */8 y3 _9 x: i/ d9 b7 e
- if (huart->RxState == HAL_UART_STATE_BUSY_RX)
" W& f( B$ a( _4 H9 p - {
/ M7 d" l: J: O" _ - uhdata = (uint16_t) READ_REG(huart->Instance->RDR);% X, H' r0 B; G/ Y' m
- *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
: h" ~1 [( Z2 A% b% \2 J# ^7 [ - huart->pRxBuffPtr++;1 K W8 O4 j5 S/ i5 `
- huart->RxXferCount--;
# T% ~5 |, D$ g9 s: N3 C6 v
& {4 U$ W, B4 J. F( V) Z- if (huart->RxXferCount == 0U)
2 ]5 j8 o0 w$ g4 l - {& F) U- E3 N9 f0 H6 _$ i9 {/ y$ g
- /* Disable the UART Parity Error Interrupt and RXNE interrupts */0 O+ l/ H3 ^. ^3 P, B
- CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
; {4 Y( P) V7 q7 a% _5 [ - 0 T& y8 l' A# A4 }9 u& r
- /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */# p4 D; Z/ l8 J1 \# K9 T: |4 t) z/ x
- CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);" E1 o1 k9 U6 i. i, c1 G5 S& e" E2 b
- ) g2 m2 {# x" t4 I8 ~
- /* Rx process is completed, restore huart->RxState to Ready */5 a9 R5 ?- k# @6 M6 o
- huart->RxState = HAL_UART_STATE_READY;
+ M9 a" c, S( U - 7 G1 f @0 x2 H1 ^
- /* Clear RxISR function pointer */5 H* f5 i. P% S8 x8 e
- huart->RxISR = NULL;
1 ]/ x' }3 O; e& F1 T( B
6 G' w: H) n3 \" M$ z+ p/ T- #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)3 g4 P5 Z# w8 N* B; D
- /*Call registered Rx complete callback*/
% P: O+ r I$ a% i; _+ B, l6 Q: S - huart->RxCpltCallback(huart);
f+ [9 ?: c4 F! ~- ?3 y0 Y q - #else
8 t% q0 ?3 q8 Q4 V J3 w( ~9 m- p - /*Call legacy weak Rx complete callback*/
m* D4 m* a4 Y' Q" K- K - HAL_UART_RxCpltCallback(huart);7 }5 ^2 r; Y9 a$ B% D
- #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
: ^3 n' i H9 X; X - }
& K% [; Z: R3 l9 S: f' ? - }& Y$ @& x1 Z- o* y E8 m
- else( K+ R* ~- J5 X! g/ q% C& f
- {
+ p2 W- e1 S0 x# P7 F5 t+ s - /* Clear RXNE interrupt flag */4 W {& X# C+ S$ x5 R
- __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
0 a- V; l: |5 g# e" J' y( E$ r - }9 Y( W) ~ U, G Q8 I4 r+ V
- }
复制代码 j/ t6 t6 m R
这样差不多一个接收过程就完整了,也仅仅是个差不多。这个函数里边做了一下工作:1.从外设里边读接收到的字节,并存储到缓存数组中。2.更新接收控制部分,就是那三行自增自减的代码。3.如果接收数据达到指定的数量,就调用HAL_UART_RxCpltCallback函数,并释放接收串口接收状态,关闭接收完成中断。用户需要实现HAL_UART_RxCpltCallback函数,需要注意的是,这个函数是在中断中执行的,单片机这类东西,我们都希望中断里边尽可能精简,显然HAL库那是不可能了。
$ h9 a# a* v7 \' D' I- b
( _9 z' u% B& R5 @DMA接收方式,H7暂时还没有仔细看,我在F1、F4中一律用DMA接收发送的,这里暂时不发表意见。
, c% k6 |7 O% U5 W
7 y+ J2 Q3 a( I. P1 N/ }轮询和终端接收方式,都需要用户提供一个数据缓存区,并制定接收数据的量,否则返回值就是错误或者超时,实际上,比如你用串口和串口转WiFi模块通信的时候,每次传输的数据量都是不固定也是未知的,这就比较麻烦了。
9 d) p* ]* q- j0 k a7 ^& z. M$ R+ Q: B$ u$ Y
当然解决方法有很多,如果你有心,完全可以直接寄存器操作,抛弃这个HAL库,并且效率你说了算。
5 V1 H# n7 e) { ]; B+ H! c' F, L3 E2 x3 r
正点原子的解决方式是,函数入口HAL_UART_Receive_IT填参数的时候,数量设为1个字节,相当于HAL库没有什么作用,反而占用了大量的执行空间和时间,效率更低。
1 t. l0 `/ z; \/ d8 d* K
& q0 ^- y8 `- h" A' h1 I4 D5 u精华来了,重点来了,然后再加个粗,把字号也放大一些。. q* o! N0 Z, E3 g. B9 Y/ F2 a
先贴上我的代码,后边简要说明实现方法,代码如下:
z4 m8 g8 F6 Q" P+ f. a: F: |, s
- #include "Uart.h": Z+ Q# X0 U2 P7 I4 Q# n: n$ }# i
- #include "stm32h7xx_hal.h"% a2 I3 O; }$ ^2 D. c
- # b6 L/ K1 H- ~8 V, I) Q! [) e( D8 d( P
- #define RxBufSize 1024: [7 e5 t. _/ v2 x+ G( l0 j9 _; b
8 n. f( d k9 B9 ~: g# P0 d- UART_HandleTypeDef huart1 = {0};# q# P9 Q6 v6 r6 P, K. `
- uint8_t RxBuf[2][RxBufSize] __attribute__((section (".RAM_D1")));' J: i1 t" m* I5 k6 W" v
- void (*Uart1RxCompleteCallback)(uint8_t *pData,uint16_t *Count);- J0 j, c* {; W4 @
" s* ^. `! [: l( A% W( I& n% h. L- void Uart1Init(uint32_t BaudRate,void (*RxCompleteCallback)(uint8_t *pData,uint16_t *Count))2 ~8 O+ C$ @$ j; D& z5 ^( @
- {
6 _1 e3 m" f1 |; h+ ]: H - huart1.Instance = USART1;
/ A, u3 L% {0 F( |: f+ [: A* R: D - huart1.Init.BaudRate = BaudRate;! b* m! v2 v6 R5 `" b
- huart1.Init.ClockPrescaler = UART_PRESCALER_DIV2;
! `. R) x6 t( g2 S( ]! i7 p - huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;7 f$ I6 T4 c; i& N) @; {
- huart1.Init.Mode = UART_MODE_TX_RX;
, T. H0 q' m6 B# b9 B - huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;& E; M, C- Z; ~$ z
- huart1.Init.OverSampling = UART_OVERSAMPLING_8;
- Y* W9 e+ }. X. s( o - huart1.Init.Parity = UART_PARITY_NONE;; H* e2 R8 a9 N7 g" D
- huart1.Init.StopBits = UART_STOPBITS_1;
2 J( ]$ J+ h7 H! \# A b. I - huart1.Init.WordLength = UART_WORDLENGTH_8B;
' X/ g9 t2 a+ `, X$ y, B3 A - huart1.FifoMode = UART_FIFOMODE_DISABLE;8 q; t! k! s2 ?% ^ U+ E9 T! I) X
-
- K: }, e9 K" V$ V$ t - HAL_UART_Init(&huart1);
( L; B% s: a: Q1 z2 V: K% f% `5 ~% ] - HAL_UART_Receive_IT(&huart1,RxBuf[0],RxBufSize);
6 u2 r+ y' C$ e/ q5 i7 h( S( N ~ - __HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);
8 |$ ]4 U" u/ I, _. f - HAL_NVIC_EnableIRQ(USART1_IRQn); [4 L% F! n3 g$ m( s8 `# [/ a& D: d
- HAL_NVIC_SetPriority(USART1_IRQn,11,0);
: i0 e* t( @3 w* k# s) \$ Z - ) f" R% E4 S9 _% w: {/ G' R4 [
- Uart1RxCompleteCallback = RxCompleteCallback;
# A x! p/ b8 j1 N - }
) b5 }: |0 e( t
! W3 U7 V# R4 B, R8 E- void HAL_UART_MspInit(UART_HandleTypeDef *huart)
4 k+ w1 l' X1 u4 \" t: I - {7 w* {5 @- Q% Q' u3 G( ?
- if(huart == &huart1)//串口1
: P5 Q( {; X; |! U" Q- m* E* Q - {, r9 Y6 F3 v7 w. r
- GPIO_InitTypeDef GPIO_InitStruct;
' Z$ ^. c8 p" K" V3 L' w -
1 u: c" p) ^: t" [' P6 Z4 s - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;# n# ~6 ^- h# ~% c) D
- GPIO_InitStruct.Pull = GPIO_NOPULL;1 ?2 I6 r) b0 n. z
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;9 h4 N5 I# l% b$ G! I! g% G
- GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
4 m- }( d$ T9 e( ` s - GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_9; O: z4 M7 h. T* Q) _; P* I4 s
-
5 I/ W; |- x. A: e - __HAL_RCC_GPIOA_CLK_ENABLE();- d* q) P/ X: A7 z/ D `
- __HAL_RCC_USART1_CLK_ENABLE();
: Y: s& w. q1 i5 A2 D4 O* j6 r -
9 B, Y$ T* N9 v5 D# z - HAL_GPIO_Init(GPIOA,&GPIO_InitStruct);
& S. B# ]) J# E" E - }" P, q; F# U+ F! O
- }$ i5 q' P. \! g1 L- i
- 9 Y0 d: n0 c* i9 S7 e
- void Uart1TxData(uint8_t *pData,uint16_t Count)
* L+ N: l1 B2 G" B$ k) H q, ? - {6 x" Z1 x/ {) f- ?! N/ N9 |
- HAL_UART_Transmit_IT(&huart1,pData,Count);4 \ U9 k' g) Z8 Q' I* [0 ~- `! ~: @
- }
, S. z& w. o$ H5 {5 V- p3 D - % v" F( ^1 }/ P$ x
- void USART1_IRQHandler(void), Y/ S9 \( G Z# X3 g
- {( n1 Z/ G* d' Y- W
- HAL_UART_IRQHandler(&huart1);
2 Z; Z( m! e4 g! {3 E - if(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE))
: D. O9 G! Y, l: u3 T, U, l) u - {
! }5 T( h9 l+ T, s5 N' N - static uint16_t count;
- x' c9 m3 P! O - __HAL_UART_CLEAR_IDLEFLAG(&huart1);
/ n" ~: w4 d; N - if(Uart1RxCompleteCallback)
3 c, G: _! _5 A' _ - {6 q# d$ M6 I9 W! ?3 f( B
- count = RxBufSize - huart1.RxXferCount;. S5 e2 Z! l1 |# p# l' U/ S
- huart1.RxState = HAL_UART_STATE_READY;
6 s; Y# r. Q3 `' W$ g. } - if(huart1.pRxBuffPtr <= RxBuf[1])
. M7 d, J7 Y( _( q5 a" N5 W - Uart1RxCompleteCallback(RxBuf[0],&count);
3 R2 Q" b0 t6 ]3 K+ H - else
6 L# E2 z. F4 u& r. ?0 V4 w! h - Uart1RxCompleteCallback(RxBuf[1],&count);
, |7 ]- D2 f" }1 o - HAL_UART_RxCpltCallback(&huart1);) @; M: ]; o* |9 C
- }5 w: o+ u6 N5 d" l7 K3 n
- }
- U5 x- ?6 O4 H6 r' G - }, E9 E' h' h- U/ q! g$ H' s' ]% b
! }" l- V3 i; g) J4 U5 y- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart): H: Y* U5 V3 l5 o3 o& n8 w6 I2 m
- {
& q& K$ r5 ^, o - if(huart == &huart1)# o- T7 m# y' V& X. \
- {
! t, ?! Y/ d' L" u - if(huart->pRxBuffPtr <= RxBuf[1])
+ {1 F3 i7 E5 z: ?) X% T L6 O - HAL_UART_Receive_IT(&huart1,RxBuf[1],RxBufSize);& q6 s* V9 F' Y. R% d
- else/ a4 z# n& w- {$ P
- HAL_UART_Receive_IT(&huart1,RxBuf[0],RxBufSize);5 Y* q" U: Y y7 g* Q
- }
; b" l; C' B& O4 x9 Z: L, E - }
复制代码
9 s1 C% d+ j5 _这是C文件,共计84行,也不多,连带着初始化也有了,下边是H文件,代码如下:
* }8 v& j$ P2 g; A# c1 f5 w% _, z. p: p" h7 G" Q% k
- #ifndef __Uart_H_0 |. K8 F1 H2 ~7 d
- #define __Uart_H_9 r q7 i% ~ E% ?+ C, \' o3 v4 @
7 p7 e; g6 x2 ~+ N- E& x4 T- #include "stdint.h"
1 o1 \) @. H$ n2 `7 r5 E* U - 5 U( {9 c! s2 X L+ z
- void Uart1Init(uint32_t BaudRate,void (*RxCompleteCallback)(uint8_t *pData,uint16_t *Count));) x3 x! g2 Q; ^& |, k
- void Uart1TxData(uint8_t *pData,uint16_t Count);
" x' q& i- h# R/ L" @- W+ {" p
* L8 b! k+ u& V/ X% e5 p- #endif /* End __Uart_H_ */+ c1 I% |$ h g @2 Q0 }8 B
- 就是这么简单明了,咦,是不是发现接口函数只有发送没有接收,没关系,咱继续,贴一个简单应用,代码如下:
$ @* \0 A: {! S+ k3 h/ _* U- S
% U: l; l$ P, Y! P$ ?# _- #include "Uart1Task.h"( R6 L) k7 m; w5 o; T8 o
- #include "limits.h"//ULONG_MAX- w: W4 y# K+ a4 W r; h
- #include "Uart.h"+ ]3 L- p. @2 ?2 s# }7 Z' D
; S7 Z' Y& [7 \# r- #define Uart1RxCompleteFlag 0x01+ ~0 B' l9 F% a7 M( {! w
3 B$ I5 n1 @+ P7 S+ b- static uint8_t *Uart1RxData;
+ ]( ]+ D) U8 b" K: | - static uint16_t Uart1RxCount;) w8 I' i# _. {! I0 _
- TaskHandle_t Uart1TaskHandle;4 H$ c% g# e* g$ @, F5 ~, g8 F& e
- % {8 X6 ^% E1 o, Q& l: a& G
- void Uart1RxIRQ(uint8_t *pData,uint16_t *Count);, n4 Q6 q) r& P4 U! @+ ^
) b: h- q* a" k! p% l- TaskHandle_t *GetTaskHandle_t(void)
+ a* j0 n" A( C9 Q1 | - {
; }9 N! ]/ s1 w$ A& O - return &Uart1TaskHandle;+ r1 U# o2 n& z& c5 f) K
- }2 A3 d; ~6 r+ L) ]: x! J
# A3 ]1 ]- a+ G" [- void Uart1Task(void *pvParameter)
" B% k) }0 w' W. w9 v- B$ B, G - {+ T! O% A/ r0 L9 o3 q
- Uart1Init(115200,Uart1RxIRQ);; f, L3 ]$ [' a! K- E, A
- while(1)
& o: |7 v/ g1 t" r - {
9 {- S. \; R# s- H9 q! w* ~ - uint32_t NotifyValue = 0;
: y* P" U3 y! A0 P4 b. t - xTaskNotifyWait(pdFALSE,ULONG_MAX,&NotifyValue,portMAX_DELAY);
% c% @2 H1 Q/ l. S% o' t4 H - Uart1TxData(Uart1RxData,Uart1RxCount); x0 G# f$ A3 s
- }
9 V4 ~8 C ^+ T! p; c: [ - }. S& a4 a- ~2 d7 t, G7 K; B
- 7 z2 D! a3 l1 _4 w
- void Uart1RxIRQ(uint8_t *pData,uint16_t *Count)
! A9 O; [- k, R6 t+ h - {" G8 t3 x. E7 }' y5 ]3 @* D8 X
- Uart1RxData = pData;( i! V# L# i. S) P m& r. `
- Uart1RxCount = *Count;
U7 q4 j. ]1 w - BaseType_t pxHigherPriorityTaskWoken;
6 }% P" q' O" g6 H - xTaskNotifyFromISR(*GetTaskHandle_t(),Uart1RxCompleteFlag,eSetBits,&pxHigherPriorityTaskWoken);
* [& P: Z3 {3 [. b; t - portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
, d- R1 B1 w) q# p% Y - }
复制代码 ! {# f) p$ p1 A4 b
以上应用,仅仅是做一个,把接收到数据再发回去,这个Uart1Task函数是在FreeRTOS里边的一个任务,裸机中你就可以当main函数理解,用到了任务通知,这个不是重点,重点是初始化有两个参数,一个是波特率没啥好说的,另一个是注册了一个回调函数,在空闲中断发送的时候调用,这个函数也有连个参数,这个不需要你关心,你只需要知道,你接收到的数据在哪里,接收了多少,pData就是接收到数据起始位置的一个指针,Count就是接收到的数据量,目前单帧最大可接收1024字节,缓存为2K,暂时不支持连续超过1K数据的接收行为,当你仔细看一遍我的方法,你就知道该怎么解决这个问题了,以后有空再适配到DMA接收方式,那才是真正的完美。/ g0 K$ \. f- u1 V, i$ t+ @+ Y
: a' I9 i+ Q: `" |* p0 n1 ^简单说一下,实现思路,在HAL库的基础上,把固定死的大小设为缓存,接收过程由HAL库完成。每当空闲中断来,意味着,一帧不定长数据接收完毕了,此时只需要把接收到的数据起始地址和接收到的数据量发给用户代码即可,然后把缓存切换到另一个缓存区,防止覆盖,且留给用户足够多的时间处理串口数据。
: |; S) B0 ^/ X. o+ q$ O
* h0 E: f5 D, A$ A' F" C6 p# n# _6 j( F
* G& Y0 q. W6 z" E1 W
* h4 D2 ~" S! n |