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