之前没有写调试记录的习惯,导致前段时间刚弄过的一个东西,现在拿起来要么翻之前的代码,要么就再慢慢调,虽然知道自己肯定是可以战胜困难,但总是浪费很多时间,所以以后准备多写一些记录,方便自己用的时候查。嘻嘻 STM32CubeMX这个工具用习惯了还是很爽的,就是有些傻逼的地方是,他并不能给你弄到位,有的东西还是得自己调一下。 打开串口并使能中断后,如果要开启DMA收发的功能,只需要使能对应的通道就可以了。 生成代码的过程就不废话了。 在串口初始化里面调用这个函数来定义串口中断接受长度。 HAL_UART_Receive_IT(&huart1, (uint8_t *)aRx1Buffer, 1); 不要忘记在中断函数里面再次调用。 一个完整的发送周期如下,清除完成标志也可以放在下次发送的开始。 - void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)' S- |8 z) e# t% I% L1 a
- {, E- O) K9 {# e- y+ q- z+ r
- static uint8_t sta = 0;; y0 U2 c8 R7 |! m# d# U
- if(huart->Instance==USART1)//Èç¹ûÊÇ´®¿Ú1 z% T. u% |/ r* t4 e0 M- u
- {& h+ M4 v1 P4 S7 i/ q- o6 ?4 A. r
- // USART_RX1_BUF[Usart1_len]=aRx1Buffer[0];- C- F9 H1 W4 Z: M8 A
- // Usart1_len++;) c6 J, q6 t6 E( w' H+ ~
- // if(Usart1_len == 0x04)
5 L; ?. g$ _0 c0 H. B8 W - // {
& }7 R4 O, [- @7 G - HAL_UART_Transmit_DMA(&huart1,(uint8_t*)aRx1Buffer,USART_RX1_LEN);
" ~$ N) Y* u- C6 Z! F' k - while(1)
9 q$ M$ F% d9 w4 q6 S% j - {
9 ^2 e" {9 T. _2 O6 o - if(__HAL_DMA_GET_FLAG(&hdma_usart1_tx,DMA_FLAG_TCIF3_7))//µÈ´ýDMA2_Steam7´«ÊäÍê³É
( `( S( b( V4 I/ ]2 o9 T; Y - {
5 H6 \2 h. q, d6 h$ D) c! _ - __HAL_DMA_CLEAR_FLAG(&hdma_usart1_tx,DMA_FLAG_TCIF3_7);//Çå³ýDMA2_Steam7´«ÊäÍê³É±êÖ¾
) E# L% `* N c7 [3 T) M- W - HAL_UART_DMAStop(&huart1); //´«ÊäÍê³ÉÒÔºó¹Ø±Õ´®¿ÚDMA% r4 D6 L/ n/ ~$ p
- break;
/ c3 m5 H% Y! y0 v6 m1 y' F - }0 u1 B8 @8 i% F. x3 ]+ J
- // __HAL_DMA_GET_COUNTER(&hdma_usart1_tx);1 G" ^# f6 U* \$ c+ P( y
- }: T, C( i3 M6 W$ `* x% P# u, w
- switch(sta)2 N# y: f" _& R, j* t
- {9 R' w* o& G$ Y4 y
- case 0:if(aRx1Buffer[0] == 0x55) sta = 1;break;
5 t% ?8 J3 b3 ]4 O$ B - case 1:if(aRx1Buffer[0] == 0xaa) mode = 1,sta = 2;else sta = 0;break;
0 ]) |& }8 T2 ?3 C - case 2:mode = 0;sta = 0;break;: K) Y) n- ^6 ~) X4 c
- }
! B) B) i( K w+ F - HAL_UART_Receive_IT(&huart1, (uint8_t *)aRx1Buffer, RX1BUFFERSIZE); 9 b/ z+ J5 V+ p" ~0 F
- // }
6 N! @# Q# h$ F# }$ S - }
3 B3 e6 s5 M& h& S, K! x* }0 ~ - if(huart->Instance==USART2)//Èç¹ûÊÇ´®¿Ú2
P6 Z% {$ g. E7 W7 X1 ~% I8 H - {
0 o) D: h5 x6 P. x - HAL_UART_Transmit_DMA(&huart2,(uint8_t*)aRx2Buffer,USART_RX2_LEN);' s4 _' o( ~0 k2 d. b! |% E
- while(1)
: |5 @3 U" R i$ A) _- P - {
+ d* P; f- H6 p* {6 }+ C - if(__HAL_DMA_GET_FLAG(&hdma_usart2_tx,DMA_FLAG_TCIF2_6))//µÈ´ýDMA2_Steam7´«ÊäÍê³É
# X7 k0 L6 ` X8 |" ?7 T6 Q - {
' t1 w C7 i4 q' y - __HAL_DMA_CLEAR_FLAG(&hdma_usart2_tx,DMA_FLAG_TCIF2_6);//Çå³ýDMA2_Steam7´«ÊäÍê³É±êÖ¾7 F# H9 q e2 c8 Z
- HAL_UART_DMAStop(&huart2); //´«ÊäÍê³ÉÒÔºó¹Ø±Õ´®¿ÚDMA! l& ~8 ?/ U) V) ~
- break;
M/ u6 l' q2 h2 a - }; p' a& y* i6 D1 A2 A
- // __HAL_DMA_GET_COUNTER(&hdma_usart2_tx);7 r0 W; m* o# T* f8 {
- } //·¢ËͽÓÊÕµ½µÄÊý¾Ý7 [) u: f: p5 i, q8 w- N
- while(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC)!=SET); //µÈ´ý·¢ËͽáÊø
8 t3 ~' ?4 x/ @4 _; V - HAL_UART_Receive_IT(&huart2, (uint8_t *)aRx2Buffer, RX2BUFFERSIZE);
. h9 l$ d' n% g T& Q' c8 @ - }
* z6 n; Q# j/ I! M- m& |1 J - if(huart->Instance==USART3)//Èç¹ûÊÇ´®¿Ú3; [" q) R$ K0 d7 x, ^
- {
, N+ D* Z( r# E: G2 b0 G6 `: A - HAL_UART_Transmit_DMA(&huart3,(uint8_t*)aRx3Buffer,USART_RX3_LEN);
- L# c& h2 Z, q) G' j$ j. X9 z - while(1)8 |. n( j5 X& r' {& ~
- {! O& o" z, s9 B+ \: v7 L
- if(__HAL_DMA_GET_FLAG(&hdma_usart3_tx,DMA_FLAG_TCIF3_7))//µÈ´ýDMA2_Steam7´«ÊäÍê³É& X+ ` Q4 T* g
- {
( b* S5 J% p, p% z" ]' N' c - __HAL_DMA_CLEAR_FLAG(&hdma_usart3_tx,DMA_FLAG_TCIF3_7);//Çå³ýDMA2_Steam7´«ÊäÍê³É±êÖ¾
6 l3 i# j; J }7 Z8 r1 e3 o - HAL_UART_DMAStop(&huart3); //´«ÊäÍê³ÉÒÔºó¹Ø±Õ´®¿ÚDMA1 F# M+ |1 i1 r9 L/ d5 R
- break;
7 n3 M% e, J0 g. Q1 f - }7 a& u- }1 y6 m% s7 a/ Y
- // __HAL_DMA_GET_COUNTER(&hdma_usart1_tx);
2 B8 n0 a/ s7 Y3 C- `9 q! y+ t - } //·¢ËͽÓÊÕµ½µÄÊý¾Ý5 ~3 E# _5 a6 \; s! r/ o
- while(__HAL_UART_GET_FLAG(&huart3,UART_FLAG_TC)!=SET); //µÈ´ý·¢ËͽáÊø
5 b$ l, \, ^. `: u4 p - HAL_UART_Receive_IT(&huart3, (uint8_t *)aRx3Buffer, RX3BUFFERSIZE); 8 a0 h" d) Z2 W" P* O3 M" G/ ]
- }
1 i2 N' Y E/ R* e+ M - if(huart->Instance==UART4)//Èç¹ûÊÇ´®¿Ú4/ d' F* C2 E# a/ H9 \8 Z% w3 N
- {. P& }( O: L2 T C# n' S2 C
- HAL_UART_Transmit_DMA(&huart4,(uint8_t*)aRx4Buffer,USART_RX4_LEN);$ p3 l! v. ]4 S) d# ^
- while(1)
& y0 R8 Z* Q0 ^! {' X, G6 C5 E - {- ]4 \5 N1 z; y1 f! m
- if(__HAL_DMA_GET_FLAG(&hdma_uart4_tx,DMA_FLAG_TCIF0_4))//µÈ´ýDMA2_Steam7´«ÊäÍê³É
2 \. [' \* e- x! k6 P, n' Y; O - {/ P W4 S& `4 {4 z7 e9 j: y
- __HAL_DMA_CLEAR_FLAG(&hdma_uart4_tx,DMA_FLAG_TCIF0_4);//Çå³ýDMA2_Steam7´«ÊäÍê³É±êÖ¾
- E! m9 Q9 n7 R5 S+ f0 ~ - HAL_UART_DMAStop(&huart4); //´«ÊäÍê³ÉÒÔºó¹Ø±Õ´®¿ÚDMA' c' ^! Z' ]1 y0 D0 y% ?) |% E7 w) c
- break;
' i- V: Y+ \5 z+ b2 U: Q8 p T - }5 I, e, p, f5 T; c! a
- // __HAL_DMA_GET_COUNTER(&hdma_uart4_tx);
4 e. F( c$ t0 e9 {: { - } //·¢ËͽÓÊÕµ½µÄÊý¾Ý6 x8 Q6 o- u: |* A, @. ] I, V* c
- while(__HAL_UART_GET_FLAG(&huart4,UART_FLAG_TC)!=SET); //µÈ´ý·¢ËͽáÊø
& ^5 N, O) N8 Y3 ^, H8 i' }3 U - HAL_UART_Receive_IT(&huart4, (uint8_t *)aRx4Buffer, RX4BUFFERSIZE);
* V! \ d" Y. h. k) g" L - }
# B7 p2 G: c6 A) [ - }3 N6 K9 g4 M E' b
-
复制代码 这个我是放在中断里实现串口自收自发的。- void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart), O; [8 y3 Y! E. J7 M
- 2 {
. v4 x; J/ p+ R. l( w0 ` - 3 uint32_t isrflags = READ_REG(huart->Instance->ISR);//?????,???????SR5 k \5 ^! l7 e: b3 |
- 4 if((__HAL_UART_GET_FLAG(huart, UART_FLAG_PE))!=RESET)" l1 I+ k6 l Z: f( x9 @+ r
- 5 {! y7 ?8 _1 t; g1 D, R5 d
- 6 READ_REG(huart->Instance->RDR);//PE???,????DR. ~" ^( s! C7 \3 v4 A0 ]
- 7 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_PEF);//???' w( s& D0 n- ~; g( x* @0 A
- 8 }
5 S3 H! w( r3 @7 V9 ? } - 9 if((__HAL_UART_GET_FLAG(huart, UART_FLAG_FE))!=RESET)
8 P) ] x/ b' H C: Z - 10 {8 r# {/ ?% t+ n5 R3 O, U5 r& Y& r
- 11 READ_REG(huart->Instance->RDR);//FE???,????DR: M6 n+ z2 Y: @/ P q
- 12 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_FEF);% V' t+ G+ ^2 f, [
- 13 }
: I3 c; p* m1 J% e7 ?# [- T2 ` - 14 . D- L( D& f+ @5 p7 I* ~, b
- 15 if((__HAL_UART_GET_FLAG(huart, UART_FLAG_NE))!=RESET)3 N! W2 s8 [9 l1 _2 N& u$ t
- 16 {+ C) \; Q: u$ N8 V* l* r
- 17 READ_REG(huart->Instance->RDR);//NE???,????DR
* r, \! @' q1 }8 t - 18 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_NEF);' F0 |3 Q; ^1 f: Y+ C
- 19 } + O" D; Y7 v) {) j; Y
- 20
! y9 \ W/ z/ _ - 21 if((__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE))!=RESET)8 S$ y# Y4 t$ w4 f
- 22 {# W1 t* |, Z* X6 m: ^- S0 d
- 23 READ_REG(huart->Instance->RDR);//ORE???,????CR7 p6 s8 R: o' X2 @
- 24 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
1 F s8 `+ R {5 W6 Y( d F7 p - 25 } - M, M5 A! ^$ t8 y6 O. W$ _
- 26 }
复制代码 3 p) M6 l- U- K4 T3 `
0 V" A* y& G: b
# N2 ~) M4 R; \) K8 j& q- S( i4 ?
$ a& T/ ^" c/ Z% m. m" Z: D3 {
|