新人求助:按照官方教程做的编译出错

发表在 内核学习营2019-4-17 02:22 [复制链接] 15 1649

新人求助:* p7 U. {! b3 T' }
       我按照如下两个教程
1 z6 a( E2 K% p, W% l  M1、https://www.rt-thread.org/docume ... nano/cube/nano-mx5/
" y8 C% l1 E+ t& [2、https://www.rt-thread.org/docume ... o_finsh/nano-finsh/
2 H* c3 K3 S; W2 C
: |% ~. F/ [9 i( P" \  j一步一步照着做,只是芯片换成103RB(创建nano工程都没问题),到第二个帖子修改头文件那个步骤后出现编译错误。(我本想上传图片,结果还是上传不了;想上传我的问题工程,结果附件也无法上传。。。。上传结果提示:SERVER(IO)ERROR)
1 Z% t0 F" }0 t
; C" m! b/ ]2 d编译提示如下错误:(要崩溃了,想用3.1.2版本的nano-msh怎么就那么难呢?纠结这个问题快一个月了,我一直是用标准库的,为了用nano-msh,今天照着教程现学hal库。。。)
% S9 k' {1 [! @( T& a1 F) Q2 w# \* L$ M" }/ z  L
../Inc/drv_usart.h(51): error:  #20: identifier "UART_HandleTypeDef" is undefined
3 A6 q* N- |- N. A. Z. }, s      UART_HandleTypeDef handle;
" E' i9 l0 `! z9 U..\Src\drv_usart.c(93): error:  #20: identifier "UART_HWCONTROL_NONE" is undefined' ~7 b2 S, d0 D' ~, {) j; e
      uart->handle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;: E; m1 J- a- r! t
..\Src\drv_usart.c(94): error:  #20: identifier "UART_MODE_TX_RX" is undefined& J3 t  ~& N# Q6 D7 C
      uart->handle.Init.Mode         = UART_MODE_TX_RX;
; C9 F( C$ L8 u..\Src\drv_usart.c(95): error:  #20: identifier "UART_OVERSAMPLING_16" is undefined6 y: N& i& {& H& g7 L8 X
      uart->handle.Init.OverSampling = UART_OVERSAMPLING_16;
- x9 j* U" E" ~& ~4 r  u..\Src\drv_usart.c(99): error:  #20: identifier "UART_WORDLENGTH_8B" is undefined/ a& X' H; m8 E( I; r
          uart->handle.Init.WordLength = UART_WORDLENGTH_8B;
/ E$ \* ^+ ]0 u% Q) A  l..\Src\drv_usart.c(102): error:  #20: identifier "UART_WORDLENGTH_9B" is undefined/ y3 s8 a* c- ^5 B# B' _$ U9 {
          uart->handle.Init.WordLength = UART_WORDLENGTH_9B;
) U1 j* N! T! {  b: P5 S2 [..\Src\drv_usart.c(111): error:  #20: identifier "UART_STOPBITS_1" is undefined% o8 [% M* v! u# _* N
          uart->handle.Init.StopBits   = UART_STOPBITS_1;
4 Z6 O* ~) \4 c: K# M, F) v3 n..\Src\drv_usart.c(114): error:  #20: identifier "UART_STOPBITS_2" is undefined( U! W7 Y& u9 m5 ?9 ^3 G
          uart->handle.Init.StopBits   = UART_STOPBITS_2;: ^: o7 b" q  R7 J
..\Src\drv_usart.c(123): error:  #20: identifier "UART_PARITY_NONE" is undefined
% [+ h9 T! N$ n8 ^# ]/ U          uart->handle.Init.Parity     = UART_PARITY_NONE;
  a2 ]! m$ K4 E& W: \..\Src\drv_usart.c(126): error:  #20: identifier "UART_PARITY_ODD" is undefined
# E7 b2 Y  T( u7 f- K9 }8 h% E9 `, C          uart->handle.Init.Parity     = UART_PARITY_ODD;
) {9 m3 z* X$ |" c" d1 ~' N% M..\Src\drv_usart.c(129): error:  #20: identifier "UART_PARITY_EVEN" is undefined( G8 E  S3 g; z$ c8 t1 b8 N0 C/ t
          uart->handle.Init.Parity     = UART_PARITY_EVEN;+ W) _9 {2 Y6 C$ U6 c
..\Src\drv_usart.c(136): warning:  #223-D: function "HAL_UART_Init" declared implicitly
% h2 W: W* h2 j1 _* \" K      if (HAL_UART_Init(&uart->handle) != HAL_OK)! }' H4 Q& |  Y) h5 \& @
..\Src\drv_usart.c(162): warning:  #223-D: function "__HAL_UART_DISABLE_IT" declared implicitly% I  \" H% ?2 \& V& ?( b" \
          __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_RXNE);
" M/ ~6 L: D( S! A8 {! e..\Src\drv_usart.c(162): error:  #20: identifier "UART_IT_RXNE" is undefined
% p! k2 B2 ~: Y. W8 M$ z/ G          __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_RXNE);
7 h+ R# g3 V% G' W..\Src\drv_usart.c(169): warning:  #223-D: function "__HAL_UART_ENABLE_IT" declared implicitly
6 ?, r2 s* V6 T8 K8 P# U+ ?3 Y          __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_RXNE);
# D- w/ |8 r- K( I..\Src\drv_usart.c(190): warning:  #223-D: function "__HAL_UART_CLEAR_FLAG" declared implicitly& q" N' G; R! H  |0 `
      UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);' Q# I, y  u4 l) t
..\Src\drv_usart.c(190): error:  #20: identifier "UART_FLAG_TC" is undefined/ N# s) T6 T- _1 I; c; `
      UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);0 P! A0 Y- @8 B. c% J
..\Src\drv_usart.c(197): warning:  #223-D: function "__HAL_UART_GET_FLAG" declared implicitly& `& S7 N8 D9 x7 p# m. a
      while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET);
5 T8 k# o1 w3 H% I& I..\Src\drv_usart.c(210): warning:  #223-D: function "__HAL_UART_GET_FLAG" declared implicitly
7 [% {: f5 H( W4 S& N( n      if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
3 g. ?- S) k4 R* d9 J..\Src\drv_usart.c(210): error:  #20: identifier "UART_FLAG_RXNE" is undefined- |+ w$ P- z0 d2 Z
      if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)/ I, m, W0 g2 ^, [
..\Src\drv_usart.c(249): warning:  #223-D: function "__HAL_UART_GET_FLAG" declared implicitly7 a4 R4 @( B" H, f
      if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) &&: \. M6 Y9 c) k- B
..\Src\drv_usart.c(249): error:  #20: identifier "UART_FLAG_RXNE" is undefined0 \* F1 I4 f0 l& [! `9 s' }
      if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) &&
) x7 G. J" U( U9 |..\Src\drv_usart.c(250): warning:  #223-D: function "__HAL_UART_GET_IT_SOURCE" declared implicitly2 a7 \* U' M. M4 E. e/ [
          (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET))
. P; W2 @$ m7 f+ a9 \..\Src\drv_usart.c(250): error:  #20: identifier "UART_IT_RXNE" is undefined5 [! x1 f( Z3 M7 W+ D$ X
          (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET))
7 g5 ]% e1 D+ R$ A* M) d( v  O..\Src\drv_usart.c(273): error:  #20: identifier "UART_FLAG_ORE" is undefined' \! G8 m* {8 X- s8 }
          if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_ORE) != RESET)
1 l9 Q# l3 o* R" R6 i) b..\Src\drv_usart.c(275): warning:  #223-D: function "__HAL_UART_CLEAR_OREFLAG" declared implicitly; n* N! z- t; r4 Y' T5 W9 N2 _
              __HAL_UART_CLEAR_OREFLAG(&uart->handle);
: b6 |& f0 L4 G( ^% W" j& V: D% W..\Src\drv_usart.c(277): error:  #20: identifier "UART_FLAG_NE" is undefined. d& c5 U' r  t/ `: a$ B
          if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_NE) != RESET)
! {6 H! B8 Y+ u0 e+ D" J6 m! g) k4 ~..\Src\drv_usart.c(279): warning:  #223-D: function "__HAL_UART_CLEAR_NEFLAG" declared implicitly
4 _) }. e' L% k. c              __HAL_UART_CLEAR_NEFLAG(&uart->handle);7 O5 X1 E1 ~. w6 \8 Q$ _
..\Src\drv_usart.c(281): error:  #20: identifier "UART_FLAG_FE" is undefined7 j0 y( ?; a! v
          if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_FE) != RESET), [' I- ]& m( X% r! Z) G/ {
..\Src\drv_usart.c(283): warning:  #223-D: function "__HAL_UART_CLEAR_FEFLAG" declared implicitly6 B* S+ s3 {  G) N' N6 _) F6 L/ g
              __HAL_UART_CLEAR_FEFLAG(&uart->handle);
+ j0 K) |9 @( ]' {5 _..\Src\drv_usart.c(285): error:  #20: identifier "UART_FLAG_PE" is undefined
. T, o6 D# B2 m& [3 W          if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_PE) != RESET)4 B/ r1 S1 j) |. Y. l
..\Src\drv_usart.c(287): warning:  #223-D: function "__HAL_UART_CLEAR_PEFLAG" declared implicitly) {4 D2 k0 D4 i) B. p; V6 @  _
              __HAL_UART_CLEAR_PEFLAG(&uart->handle);& _8 L9 {' t9 Q9 M/ R/ z1 O- `& h
..\Src\drv_usart.c(291): error:  #20: identifier "UART_FLAG_LBD" is undefined0 k1 K3 |! S; L0 ~6 C
          if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_LBD) != RESET)
& e4 _! j% G/ ~1 s1 y..\Src\drv_usart.c(293): warning:  #223-D: function "__HAL_UART_CLEAR_FLAG" declared implicitly
9 s  ]' I& |7 |/ s% I- o+ k              UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_LBD);' U0 I; n0 s1 ?! ^. t+ Z- l
..\Src\drv_usart.c(296): error:  #20: identifier "UART_FLAG_CTS" is undefined  ]9 K. u# L7 V7 U! M' k3 F  Q
          if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_CTS) != RESET)
* I7 D, X$ Z) ]. {..\Src\drv_usart.c(298): warning:  #223-D: function "__HAL_UART_CLEAR_FLAG" declared implicitly
- h  f4 E6 b# I! r              UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_CTS);
9 y$ d0 H' n' B$ K8 z5 x7 Z..\Src\drv_usart.c(300): error:  #20: identifier "UART_FLAG_TXE" is undefined
: Z* ^$ K7 v' u          if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET)
5 {4 b+ O/ L% j- e& |2 l, t..\Src\drv_usart.c(302): warning:  #223-D: function "__HAL_UART_CLEAR_FLAG" declared implicitly2 P* q* P( K, C' Y- d  n: p
              UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);# z, F! [- d* r; X
..\Src\drv_usart.c(304): error:  #20: identifier "UART_FLAG_TC" is undefined+ z0 n6 o: s2 l; y  m: n' r
          if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) != RESET)
! N. |7 Y  i3 M9 F2 F..\Src\drv_usart.c(306): warning:  #223-D: function "__HAL_UART_CLEAR_FLAG" declared implicitly% Q3 _: {2 Z4 C& M2 r
              UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);6 _4 l8 V/ K' z3 ^. g
..\Src\drv_usart.c(310): warning:  #223-D: function "__HAL_UART_CLEAR_FLAG" declared implicitly
3 B/ L9 O/ F8 Z1 E" E              UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_RXNE);' n: P( {) S+ q
..\Src\drv_usart.c: 17 warnings, 24 errors2 |) r2 d- t9 C& s. ?% Y! O; \

: ]" d' A: k  ?" C
使用道具 举报 显示全部楼层 回复
最新评论 | 正序浏览
显示全部楼层 |楼层直达:
发表于 2019-4-17 09:51:56 | 显示全部楼层
微信截图_20190417094950.png ( k  {" @% u" b
例程展示stm32l475,不是例程的平台,根据此仿照流程。上图表示,猜测可能出现的问题,你看看驱动里面的宏定义是什么,据此类似修改
. ?! ~, P8 U% F) ]5 V0 ]
使用道具 举报 回复
发表于 2019-4-17 09:57:30 | 显示全部楼层
微信截图_20190417094950.png 使用驱动如上图所示,相关文件地址、名称3 A6 ~; T2 {$ z$ H
使用道具 举报 回复
发表于 2019-4-17 10:01:55 | 显示全部楼层
https://www.rt-thread.org/qa/thread-10348-1-1.html% J9 W& |9 g9 g; M& J4 ]
老的bsp驱动可以参考这个更改! c$ h, K. E3 p, v8 I' o
使用道具 举报 回复
发表于 2019-4-17 10:02:51 | 显示全部楼层
Ernest 发表于 2019-4-17 09:51/ i& q9 A3 v) Z6 c. n
例程展示stm32l475,不是例程的平台,根据此仿照流程。上图表示,猜测可能出现的问题,你看看驱动里面的宏 ...
+ p% v* h- T5 D" a2 |; ~7 n& M
这里已安装F1的修改过了
使用道具 举报 回复
发表于 2019-4-17 10:03:06 | 显示全部楼层
Ernest 发表于 2019-4-17 09:57
8 G" E4 D' r9 v使用驱动如上图所示,相关文件地址、名称
, e  \, H+ K; A( j+ c4 X2 A
这里也是用的F1的
使用道具 举报 回复
发表于 2019-4-17 10:04:45 | 显示全部楼层
马上我帖出文件内容
使用道具 举报 回复
发表于 2019-4-17 10:07:52 | 显示全部楼层
Tiv 发表于 2019-4-17 10:03
2 ]0 u* V: A( N4 T这里也是用的F1的

9 M: N: ~9 i' Z5 i2 s4 V7 f完全没有问题:F1的有,参照改改,一步步来
使用道具 举报 回复
发表于 2019-4-17 10:17:38 | 显示全部楼层
rtconfig.h
4 `0 z3 V: _: R6 a
  1. ( d, y. T4 R5 N% V: I! A1 [
  2. /* RT-Thread config file */3 ~" F3 Y, F, ~) ~; G
  3. ) j! U+ B* b4 J
  4. #ifndef __RTTHREAD_CFG_H__
    " ?9 M' u5 K1 c. s
  5. #define __RTTHREAD_CFG_H__% D+ p5 k& r2 Z+ O- ~6 [

  6. + H; y/ I1 {" s% y; f  u, l
  7. #include "RTE_Components.h"
    , {9 C' X: O" I" x4 P, x4 T0 \

  8. ! h/ f0 W. {% d. S' W
  9. // <<< Use Configuration Wizard in Context Menu >>>4 m! a& J0 W# p: M! o# c) s
  10. // <h>Basic Configuration
    1 V7 m6 H; c8 h% E7 u' i
  11. // <o>Maximal level of thread priority <8-256>
    ! r+ J# F; S( y9 a) C, M# D+ y3 G
  12. //        <i>Default: 32& ?$ j* P3 ^5 W* \
  13. #define RT_THREAD_PRIORITY_MAX 32
    4 |0 n/ R) g7 }$ ]- x

  14. 0 Q9 v. j! S1 k: e! L7 D
  15. // <o>OS tick per second" G& L$ ]- Y/ }
  16. //  <i>Default: 1000   (1ms)
    - ]: y: x9 O* e& w, K2 g. A6 f. H9 a
  17. #define RT_TICK_PER_SECOND 10008 ?4 S2 P, \5 c( i7 M9 t1 u

  18. ) @# o% o2 u& z$ H
  19. // <o>Alignment size for CPU architecture data access0 R3 }; ?3 r1 E. i) g" ~, L3 L9 ?
  20. //        <i>Default: 4
    0 \2 [, y- K0 w# K; m2 x' q
  21. #define RT_ALIGN_SIZE 4. M9 J" ]9 I" B! x& B5 {
  22. 8 N. s/ n9 }  g& ?0 [' e; X
  23. // <o>the max length of object name<2-16>& z8 q9 r& Z' t: c2 o  l* x
  24. //        <i>Default: 8
    7 u+ e* u$ I: T( J( e# ^! p0 u* y
  25. #define RT_NAME_MAX 88 F  Y* c% x* I. J

  26. % }9 k& h$ Y5 ~! y, w
  27. // <c1>Using RT-Thread components initialization5 r& J2 L) P8 Q2 k" Z
  28. //  <i>Using RT-Thread components initialization( _& O: Y( Q8 O1 C6 m" a
  29. #define RT_USING_COMPONENTS_INIT# f9 N6 o; q7 u, {' S
  30. // </c>
    + |' Y, t$ @6 r; ~3 ?6 W
  31. ' f3 B$ A9 R" c/ p
  32. // <c1>Using user main; x+ H& @# Y( u
  33. //  <i>Using user main' t7 b$ k$ N( k4 l/ L' F% c
  34. #define RT_USING_USER_MAIN: C( ]4 l( y' E+ j: R( M: E
  35. // </c>
    2 P" r. E$ v* y* `3 d" e

  36. 2 R+ [: y: k# Z# @2 a
  37. // <o>the size of main thread<1-4086>
    " e% l8 o. _+ c
  38. //        <i>Default: 5120 d0 y+ e0 j1 m- i  }4 L
  39. #define RT_MAIN_THREAD_STACK_SIZE 1024
    2 z# W5 n5 }7 I! b  P
  40. . C4 H9 r* J4 I
  41. // <c1>using tiny size of memory2 X0 ^" c; B4 Z6 y: {
  42. //  <i>using tiny size of memory% _7 Y( J, [( q
  43. //#define RT_USING_TINY_SIZE
    1 g8 Q" ^' b0 ]0 Q7 r
  44. // </c>
    + ~5 T! K2 n* N: h& q
  45. // </h>, l' i/ _# M2 d) ]/ w' S

  46. + f  j) x$ l' V8 L3 g
  47. // <h>Debug Configuration0 y  c2 T7 B* q2 ~6 S, B+ Y
  48. // <c1>enable kernel debug configuration( |( V: ~- ?& e2 R0 Y* a' p
  49. //  <i>Default: enable kernel debug configuration
    , r& o3 m: m9 L
  50. //#define RT_DEBUG
    9 B: Q' w. d% m4 O2 ^* i$ J8 ^
  51. // </c>
    : s0 l, c5 {) s& S+ z# ^" m6 ]

  52. % s2 X5 e' _# d( {
  53. // <o>enable components initialization debug configuration<0-1>
    8 i) ~- W1 Z8 e; H$ M
  54. //  <i>Default: 0
    3 A" n) Y5 V* ?* z% V
  55. //#define RT_DEBUG_INIT6 j$ ~, @- F& L5 o# S
  56. 0 m, T' ], z7 w) v
  57. // <c1>thread stack over flow detect
    * D! ~, o: v) k2 z7 U
  58. //  <i> Diable Thread stack over flow detect6 W& J/ z% {- @' W2 J! p
  59. //#define RT_USING_OVERFLOW_CHECK
    : W$ l+ q) r! ]& o
  60. // </c>
    # ?5 ^$ L5 R& U3 x2 O
  61. // </h>
    & N& t4 p) B- A* K: n5 [% W
  62. 6 E8 j' ^1 Q9 Y% k! p6 Y3 X5 B( ]
  63. // <h>Hook Configuration- I0 g5 E/ {* _
  64. // <c1>using hook! O) l6 \7 {6 V% T! y' I7 p/ l
  65. //  <i>using hook
    # p' F/ T9 F$ x5 y/ `# L
  66. //#define RT_USING_HOOK3 v1 K9 U7 O1 g0 y. B! m; v% R
  67. // </c>& [. d" |4 n  y2 k; j$ D( Y$ u

  68. 6 p. m/ P) J9 e% n! E6 X
  69. // <c1>using idle hook  V! r* z* R4 i8 R4 N7 f
  70. //  <i>using idle hook
    ! |9 g) r: A) X4 r' Z" ~5 S$ }3 r
  71. //#define RT_USING_IDLE_HOOK4 O: d& f' a8 \3 P6 Y) k
  72. // </c>3 v5 h, H5 z, w
  73. // </h>& {( Y* R2 Y# ^- N: N" T( u
  74. ! A4 d" K: Q! W9 U. L' c4 h
  75. // <e>Software timers Configuration
    ! N  M/ f0 w% r. i" t/ t
  76. // <i> Enables user timers. X0 X% V! h! q6 k
  77. //#define RT_USING_TIMER_SOFT9 \0 P1 a, \# l9 a) m
  78. " s% F1 H  m! W4 W
  79. // <o>The priority level of timer thread <0-31>; o, V$ Y8 X, H( ^$ B" a* Y
  80. //  <i>Default: 4
    + g2 z3 X+ v3 m" q; v# b' x* x
  81. #define RT_TIMER_THREAD_PRIO 4
    0 o3 T6 k' M; S" B3 ]
  82. 4 V5 L, q& z3 j
  83. // <o>The stack size of timer thread <0-8192>
    % Q# _2 `8 [8 k  q
  84. //  <i>Default: 512
    " p" ^& Q$ K' u2 u' i6 A/ ]' m+ @
  85. #define RT_TIMER_THREAD_STACK_SIZE 512# S6 r" b/ G6 M" p1 S2 v6 W. L' q/ ]

  86. # y5 h( e& U9 a& K) O% U+ X
  87. // <o>The soft-timer tick per second <0-1000>  |9 h# }0 s" |. X4 c# n9 I7 H
  88. //  <i>Default: 100( A+ q$ R# Q2 B9 j9 ^
  89. #define RT_TIMER_TICK_PER_SECOND 100( ]$ G1 J- h/ ^) G( G
  90. // </e>
    ) y% t6 T. a) K% A
  91. . Q# p/ `2 P  q, e9 f9 T
  92. // <h>IPC(Inter-process communication) Configuration3 y, R6 x/ [% m) F, F: w5 L6 c: N' v
  93. // <c1>Using Semaphore# V7 a" |: W1 D- W0 D) X; y' g5 Q" Y
  94. //  <i>Using Semaphore6 u1 C  Y# Z! v2 R; `1 c
  95. #define RT_USING_SEMAPHORE
    1 T* X: q, j$ h4 V3 x- E
  96. // </c>
    ) q) F8 j8 C4 k9 Y5 W
  97. 0 u+ B. ?1 h* J5 u: [% O$ a
  98. // <c1>Using Mutex. f6 j5 I/ I8 p! L4 I# J/ r
  99. //  <i>Using Mutex$ o; W% \+ d5 V5 }
  100. //#define RT_USING_MUTEX" M' n4 D3 w# h; {
  101. // </c>
    % l1 N: z# ^( V' v: N7 o3 [
  102. ) X' Y3 m( j, {. [& j
  103. // <c1>Using Event
    1 U+ [6 n( J1 J$ w8 O; F4 y
  104. //  <i>Using Event  V" D  g; W! _1 V& z
  105. //#define RT_USING_EVENT
      B$ R* T0 G8 t1 v) D/ @/ F
  106. // </c>; w6 Z7 d: d4 q. \4 s" Q2 Y

  107. 1 d: j$ r+ T0 C9 L. @
  108. // <c1>Using MailBox4 m; d4 w1 f! K
  109. //  <i>Using MailBox
    ) M6 c7 c3 H% Y! L  O, J- G
  110. //#define RT_USING_MAILBOX/ k- l. w) ~* [* n
  111. // </c>$ H+ E4 E$ T9 b2 m% l8 d6 M
  112. $ k9 @& x0 L( H3 b
  113. // <c1>Using Message Queue
    , n0 r; L; }! [  g
  114. //  <i>Using Message Queue
    0 U, I8 Y: D0 P
  115. //#define RT_USING_MESSAGEQUEUE
    , X& H: E4 `( d( G& P$ [
  116. // </c>
    ( }4 h; r4 e5 q; ~( b0 j) `
  117. // </h>
      B1 O; `8 _4 x" A+ h/ f
  118. 4 T( }' Z: g& j& A
  119. // <h>Memory Management Configuration
    & \4 K0 U) [( x& d1 o/ S. h
  120. // <c1>Using Memory Pool Management" J! Q$ U7 _5 T4 K6 w
  121. //  <i>Using Memory Pool Management! e" v; o6 O$ t
  122. //#define RT_USING_MEMPOOL) ~" }2 @8 J( a# A- c! m
  123. // </c>- G5 U) u4 g& v' L2 o
  124. // <c1>Dynamic Heap Management9 {# J  Z1 I& Y2 Q9 J8 K: J7 E
  125. //  <i>Dynamic Heap Management8 ?- c: I! U0 ^2 ^* C$ ]3 B& C
  126. #define RT_USING_HEAP5 d7 s6 c- R5 U/ V% Q. r! `. `; Z
  127. // </c>
    6 \' S! C) @* w/ g6 m! J
  128. // <c1>using small memory9 Q0 a$ h1 t) |4 i% k
  129. //  <i>using small memory
    ; _/ ~4 n9 `% R" ^+ ]
  130. #define RT_USING_SMALL_MEM
    0 O  ]5 ]; U! C
  131. // </c>0 R, m! ^2 J6 C
  132. // </h>' h6 u/ n2 J$ d& [
  133. 9 M9 _" f& {; O/ }+ ^
  134. // <h>Console Configuration+ ?4 c. K6 G8 q
  135. // <c1>Using console
    2 r3 n8 V+ v% ], G! y
  136. //  <i>Using console
    & h% Z: b) U0 F* `0 S  Y
  137. #define RT_USING_CONSOLE& \: g9 W" Q* V) A! Y
  138. // </c>8 _* {! j3 C; i
  139. . x% V( ?& F) j5 V6 O
  140. // <o>the buffer size of console <1-1024>
    4 e/ }9 |1 r& }! M% F
  141. //  <i>the buffer size of console
    : c( j1 {' D6 n, L9 c; N* c
  142. //  <i>Default: 128  (128Byte)
    0 V* ^8 ~6 n- V, C$ c
  143. #define RT_CONSOLEBUF_SIZE 128
    ' E, _) R5 e3 ^

  144. + x' E) O7 b" Q  ]0 _: a
  145. // <s>The device name for console
    - f4 Y4 \: A0 ?# y9 q6 c& [; ]
  146. //  <i>The device name for console; M! F# G) q* n  z# {  o8 `+ p
  147. //  <i>Default: uart0) i7 \" p$ ?" ^
  148. #define RT_CONSOLE_DEVICE_NAME "uart2"7 m0 v- ]+ a" E  ^$ g- V
  149. // </h>
    4 _" y+ h5 y' g( \# j# m! y3 x( d+ ?

  150. ; y% q6 T5 g, ^1 Z5 H' c
  151. #if defined(RTE_USING_DEVICE)
    3 t4 K$ I* D* ?7 O+ ?
  152. #define RT_USING_DEVICE
    , W" _8 a8 f: V
  153. #define RT_USING_SERIAL
    # Z* q3 S& J5 S" v# w" ~
  154. #define BSP_USING_UART2
    : _# E$ P4 j9 ?2 X
  155. #endif
    " c, d9 M. y  l7 U

  156. + T9 s6 }; \0 ~6 L
  157. #if defined(RTE_USING_FINSH)
    6 R+ Q9 u- }; I3 a# B9 o( L( Q' }7 V# G
  158. ! f+ b% o, q$ [* a- D4 _
  159. // <h>Finsh Configuration* s, B: e4 p! R# {
  160. // <c1>Using FinSh Shell$ z5 w* w" B, I( k' J
  161. //  <i>Using FinSh Shell
    - y5 j4 {- g3 e: W6 g8 r8 j; [
  162. #define RT_USING_FINSH1 \) ~/ `$ p% p0 O4 W/ [" |
  163. // </c># |* j" d8 U% x# \) \

  164. ; J% \  G! G0 \5 I: R0 u! ~+ K( X
  165. // <c1>Using Msh Shell0 r7 [& F/ K9 |* t5 o
  166. //  <i>Using Msh Shell( d! ?6 S8 q. _' j" b7 F( L
  167. #define FINSH_USING_MSH
    . f3 m. t" P+ O( ~; s
  168. // </c>
    3 {, k6 |1 y( [2 K2 O

  169. . o3 ^  z' |, @- Y" o' C. v$ j
  170. // <c1>Only using Msh Shell
    * F' n% c0 P1 Z6 J! z) X
  171. //  <i>Only using Msh Shell
    6 t5 c- l! e6 Y& j
  172. #define FINSH_USING_MSH_ONLY, Q" S" q8 r# C
  173. // </c>
    ' J- z* W- r, f: H1 b* A

  174. & @' P( e0 u  A8 |# H
  175. // <o>the priority of finsh thread <1-7>0 ?. U" t, W1 x  ^" d% s
  176. //  <i>the priority of finsh thread  ]. F1 n7 h0 W, U2 e) w
  177. //  <i>Default: 60 m  \- k  P4 }
  178. #define __FINSH_THREAD_PRIORITY     1
    7 e, j7 a& R2 I
  179. #define FINSH_THREAD_PRIORITY       (RT_THREAD_PRIORITY_MAX / 8 * __FINSH_THREAD_PRIORITY + 1)
    5 b+ h9 M! C8 \# K2 p: D0 C  u

  180. ) X. L' T! U: `, S+ R
  181. // <o>the stack of finsh thread <1-4096>  Y; ?+ ?, e# z9 ?
  182. //  <i>the stack of finsh thread
    ( I+ h: C/ p$ S7 u9 ]
  183. //  <i>Default: 4096  (4096Byte)7 u7 c; i" \+ [* c1 V. m4 \; A
  184. #define FINSH_THREAD_STACK_SIZE 2048
    0 T: P: q! Y; g% _4 f4 V2 M4 l! T
  185. $ Z" e9 A1 z. e, [) t
  186. // <o>the history lines of finsh thread <1-32>& k6 D2 M, ?! h  M4 l
  187. //  <i>the history lines of finsh thread. }0 f1 Q( I1 h* G
  188. //  <i>Default: 5) S! k% z. y: ]; B$ b
  189. #define FINSH_HISTORY_LINES                5
    - p/ R4 ~1 K: ~2 B
  190. 4 {4 Q5 E% X; T3 m3 D1 ?8 M+ s; K
  191. // <c1>Using symbol table in finsh shell
    ( ~* @" V! u3 ?0 a+ S
  192. //  <i>Using symbol table in finsh shell
    7 A* n7 l. v% U. Z+ U
  193. #define FINSH_USING_SYMTAB
    ! K- g! ^% r* _7 n' ]: @  Y
  194. // </c>( a& g" e8 o* D3 s5 d& \  N9 K
  195. // </h>+ p) R% U0 K, ]# _

  196. ) b+ U0 _1 f8 S- n
  197. #endif //RTE_USING_FINSH
    4 G4 o0 k9 {5 e, G- J. T

  198.   O: d% S$ N$ T( s5 J! u2 d
  199. #define SOC_SERIES_STM32F1
    * D" y/ |$ ~' }) W
  200. // <<< end of configuration section >>>
    & F/ s$ T& U/ J( @/ T
  201. & H7 y$ O: P  o0 A' a1 m- H% t
  202. #endif( F* ^! s2 Y* j6 j

  203. & }. f5 j% {4 F% W9 I
复制代码
drv_usart.h9 w& l+ V' }) [
  1. /*
    , @9 R- t- E6 I" E3 e% ]
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
    ! i& L( m7 _* x2 [  w, V
  3. *7 C# @, D2 V7 H2 T, w/ ~
  4. * SPDX-License-Identifier: Apache-2.0. Q' j) J4 t& r+ ]4 f
  5. *
    ! C, S" x  Y1 c
  6. * Change Logs:0 x$ \; T. B/ ~4 h9 n1 j
  7. * Date           Author       Notes
    % X5 Y0 u) r' Q) \9 X
  8. * 2018.10.30     SummerGift   first version$ s& T( N  @7 }4 g6 s# J2 m& i# G( @' ~
  9. * 2019.03.05     whj4674672   add stm32h7
    4 k2 P8 v8 e1 ?4 E
  10. */$ f* n8 C: @+ V* B
  11. 9 K& ~6 f7 n" }& \9 A, H0 ]
  12. #ifndef __DRV_USART_H__' b* O. t, Y3 U" B- `
  13. #define __DRV_USART_H__  a" p7 }2 [5 g7 @( D, E6 \

  14. * ?( \, Q: S7 e* Q. z; ]
  15. #include <rtthread.h>/ U8 `; s: C, d
  16. #include "rtdevice.h"
    $ I# `2 H2 l8 ^) H
  17. #include <rthw.h>
    9 s6 b* @( @  V) y: j& c2 ^" V
  18. //#include <drv_common.h>
    / j' f% D; N: O6 L5 V  U
  19. //#include "drv_dma.h"/ h4 Q! o. z# E/ S- z/ p' d2 q+ w
  20. #include "uart_config.h"+ L$ y% U7 v3 r+ u1 ~* C
  21. #include "stm32f1xx_hal.h"
    6 a$ j! W5 X. J6 y

  22. * n/ H) l: l  }( p0 X8 I" B
  23. int rt_hw_usart_init(void);
    4 L& m! _* z8 f/ {/ i5 H$ K

  24. ! I( h$ z% {1 \
  25. #if defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32L4) \
    ) y" L( u& Y/ O8 R
  26.     || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0)/ q" e2 b) S) q2 k- z. I( s
  27. #define DMA_INSTANCE_TYPE              DMA_Channel_TypeDef9 ^+ ]3 n- q, g. e7 P
  28. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32H7)* h$ s, z( R2 g. e8 x
  29. #define DMA_INSTANCE_TYPE              DMA_Stream_TypeDef
    # r: `) S( \2 ]7 N0 ~; i
  30. #endif /*  defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32L4) */
    " J+ c' x1 c" l; K. _& }- D+ I. b

  31. - y  k3 n) W- G
  32. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F4) \: |5 w7 r+ f; ?, {
  33.     || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0)
    " N3 ~0 i8 ]( k+ Z8 c0 E" u
  34. #define UART_INSTANCE_CLEAR_FUNCTION    __HAL_UART_CLEAR_FLAG
    & |3 u3 X" F; q' m* f4 v
  35. #elif defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32H7)
    & ]9 Y. i. Y! H! z5 [( V" P% S
  36. #define UART_INSTANCE_CLEAR_FUNCTION    __HAL_UART_CLEAR_IT5 B- H) G; c1 L" F2 |
  37. #endif' ^% c0 i* k4 }% p
  38. * F4 J0 L% z, e9 @6 K* ~: G
  39. /* stm32 config class */9 d) _4 H0 [7 g) A3 T# ]; J& a% J
  40. struct stm32_uart_config
    / p1 c2 S& a" }" n4 Z  N8 C
  41. {
    . C7 F/ N  O$ c4 x
  42.     const char *name;* u: d8 ~1 C  [
  43.     USART_TypeDef *Instance;& F1 i- _8 h+ b5 V
  44.     IRQn_Type irq_type;' U1 e# h3 W! k$ x! Y
  45.     struct dma_config *dma_rx;% z4 \/ c; w% Y  O
  46. };9 V4 V9 _# |$ B

  47. ! _6 _: G' E- ]& j* a/ u4 `( E
  48. /* stm32 uart dirver class */
    4 C- V' R: }, U$ H) C9 z7 R$ L
  49. struct stm32_uart: C0 J8 @, Y2 _- U8 X
  50. {
    + v8 y' s6 h/ u4 }
  51.     UART_HandleTypeDef handle;  O& x: p1 L' o
  52.     struct stm32_uart_config *config;% |2 W+ j6 D  f) z  Y3 ]
  53.    
    . e/ P/ E* S; X
  54. #ifdef RT_SERIAL_USING_DMA4 b  ~' Z/ y4 h4 ?
  55.     struct
    0 o8 ]$ d$ m; ]6 K6 s
  56.     {
    . j0 C( a: x2 V: L
  57.         DMA_HandleTypeDef handle;
    & W* ^: V% |' D" g0 r  v& Q
  58.         rt_size_t last_index;( o- g5 r! W  @8 k. N
  59.     } dma;& Z: h& z1 W8 B$ z
  60. #endif
    " F# E4 ^; @, _  o0 Y6 R
  61.     rt_uint8_t uart_dma_flag;
    # |% Y/ T+ X) g0 ?0 U# G
  62.     struct rt_serial_device serial;# b7 x  L* b/ g& w
  63. };9 Y2 z( _% B+ B1 F7 b
  64. ' [1 H$ J& W" ?! |7 X
  65. #endif  /* __DRV_USART_H__ */3 M0 M7 b3 Q: Z9 p1 y& y
  66. ! W+ @1 M" J! _# {
复制代码
drv_usart.c
1 H+ I' n/ l8 j+ S, i8 }
  1. /*
    ; X* x, W8 u* T# m) f% q
  2. * Copyright (c) 2006-2018, RT-Thread Development Team# K- e+ Y1 g( x
  3. *
      E1 R/ N- v; ~: G( A5 I
  4. * SPDX-License-Identifier: Apache-2.0
    4 G/ s0 F2 Y% L: R( Y
  5. *, ]5 q0 W, ^. P: v& }
  6. * Change Logs:1 H+ d/ P' Q; u$ Q% S: X0 X! u
  7. * Date           Author       Notes: T; F! q* Y* \! b$ F* [5 X
  8. * 2018-10-30     SummerGift   first version% M+ o# @4 I2 M& ?
  9. */) B- h8 E( q' h: s; j
  10. 6 C$ V0 }" b1 D4 r& ~% c4 r6 t
  11. //#include "board.h"! d, B' G6 j/ ?7 n
  12. #include "drv_usart.h"
    ( E+ q( {" O; E2 S( w8 @+ K, R, e2 X
  13. //#include "drv_config.h"# o' [$ v+ \7 }: z2 y+ C; B, J

  14. 9 b# c) c, G' c
  15. #ifdef RT_USING_SERIAL
    5 O, @9 }* P3 I
  16. . u/ y4 y" Y4 \
  17. //#define DRV_DEBUG
    3 E- r9 m6 M) K  j- D% a
  18. #define LOG_TAG             "drv.usart"
    - O" u: R* s9 D. q
  19. //#include <drv_log.h>
    3 @0 c- g, s8 s3 C

  20. & w' f6 P2 D. ]% i% F3 @
  21. #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) \
    $ i# X* @2 \& ?8 s% I
  22.     && !defined(BSP_USING_UART4) && !defined(BSP_USING_UART5) && !defined(BSP_USING_LPUART1)( Q0 O9 `$ h2 i* O- F
  23. #error "Please define at least one BSP_USING_UARTx"
    & X2 n7 w9 i" y* x( v. b1 y9 [2 h
  24. /* this driver can be disabled at menuconfig ? RT-Thread Components ? Device Drivers */6 y4 C" ^& f+ I+ z, A! c
  25. #endif
    " X( ~8 F9 F- D7 Y, \( n4 v

  26. 3 u! j" w, U& f" c. {% O
  27. #ifdef RT_SERIAL_USING_DMA  c) N  o# Z' e3 T2 ]
  28. static void stm32_dma_config(struct rt_serial_device *serial);, w& d) t! H, x" R" q4 R  b
  29. #endif
    . P( x  {! E$ I1 O, Z$ \; \

  30. 6 S# K' F* P+ b) i( b- b
  31. enum( A6 o, _5 O/ ^' w
  32. {/ B' ~, T; c* i9 T0 }
  33. #ifdef BSP_USING_UART12 }7 y2 g1 m. O; j
  34.     UART1_INDEX,0 A" @) K" u- d% q3 d7 _+ R. K
  35. #endif# H. {6 e7 F9 U: w8 a. B3 S
  36. #ifdef BSP_USING_UART20 l' s$ M0 t5 I6 ?& Y' q( M) |, ?# s
  37.     UART2_INDEX,! {  T5 K- n  T! I
  38. #endif
    ' Y, g$ R/ _% g& r' E6 }
  39. #ifdef BSP_USING_UART3) c; H7 j2 B4 D" R1 J; V) ?
  40.     UART3_INDEX,
    $ u- {( M$ o% t' b
  41. #endif
    ( N9 d1 q# _$ w+ [& ?) K) P
  42. #ifdef BSP_USING_UART4' }4 ~$ w" C+ z; x0 ]/ `
  43.     UART4_INDEX,
    8 {4 ]5 ~+ @: P
  44. #endif! f/ q8 w, A& ^7 z9 N
  45. #ifdef BSP_USING_UART53 ^% A# y# v4 X& q
  46.     UART5_INDEX,2 j4 q. [+ F2 y0 a& g
  47. #endif
    ) r5 F! g- v# Z- P4 z- z
  48. #ifdef BSP_USING_UART6
    $ w" z- l, a( S8 f; Z. L
  49.     UART6_INDEX,; n6 O- p3 {) M6 P- I
  50. #endif3 g; w" d+ w% z8 S3 y
  51. #ifdef BSP_USING_LPUART12 k# ~0 l6 ~# V3 Q$ S9 S
  52.     LPUART1_INDEX,$ e# _6 b7 I9 e2 J/ r# T
  53. #endif
    - ]8 B/ U0 g4 V
  54. };
    4 Q7 H# S# B# s  I
  55. 8 P0 y  l5 z/ m+ }: o( Z$ X2 q" y
  56. static struct stm32_uart_config uart_config[] =  c/ H2 {& ]1 o$ b% Q$ H
  57. {
    % S* v9 A+ X7 K& _
  58. #ifdef BSP_USING_UART1
    9 [5 S$ g) S& S0 z
  59.         UART1_CONFIG,; T$ a+ ?4 g: g" f' ?7 H+ h5 `
  60. #endif1 ?) A6 M, p! Z, x
  61. #ifdef BSP_USING_UART2: P& Y/ W' b. s2 A9 F# Y6 \
  62.         UART2_CONFIG,
    3 [$ A4 }3 {7 ~. U( S
  63. #endif- q! F  V" }  ^5 r7 E: V
  64. #ifdef BSP_USING_UART3
    ' F* {* p6 o! C1 C
  65.         UART3_CONFIG,
    ; k; [& [7 }0 H( P2 w
  66. #endif
    ' s) i7 h6 I7 \- d9 }$ r" H; |
  67. #ifdef BSP_USING_UART4
    5 w# y  _% }- [
  68.         UART4_CONFIG,
    / H# A0 p5 Y6 f4 W
  69. #endif( Z0 H) p% c" K; W5 J
  70. #ifdef BSP_USING_UART5
    $ x" H; E5 ?8 L, K% k  l/ Y
  71.         UART5_CONFIG,8 ~) B# E1 v8 C5 e0 U
  72. #endif
    # Z; g8 D3 h7 U1 q
  73. #ifdef BSP_USING_UART6
    & b# `1 g6 w6 A7 b! ~& j
  74.         UART6_CONFIG,
    7 ?' O. ]- B' e% N4 \" q1 x
  75. #endif
    , X' L  Q( r+ n" Z* [
  76. #ifdef BSP_USING_LPUART1
    ' o! A0 E  c6 I5 l9 g$ k/ r9 t
  77.         LPUART1_CONFIG,
    " E" Z1 K% ]8 A% l0 c- R
  78. #endif% `' e( b" I) Q6 Z
  79. };2 p& I7 e5 z; u
  80. + l8 z" D6 E( E; I
  81. static struct stm32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
    8 ?" j' \0 ?6 m: b3 M1 ^8 c/ T* N
  82. 8 I( L% ~3 D! J
  83. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
    5 L; a6 h& N; j' t8 g
  84. {
    9 v( D; |* s5 y; a1 N# h
  85.     struct stm32_uart *uart;% y6 C( H. Q: ]% }5 v$ Q
  86.     RT_ASSERT(serial != RT_NULL);; _! a& [/ q! E( Q' V
  87.     RT_ASSERT(cfg != RT_NULL);
    % h3 I% J  j! o
  88.     uart = (struct stm32_uart *)serial->parent.user_data;5 m2 B* ^' h% B
  89.     RT_ASSERT(uart != RT_NULL);! i* p* `4 V9 I; d8 k0 `7 M2 Y1 h

  90. , l. S; c# y' |) {0 _
  91.     uart->handle.Instance          = uart->config->Instance;
    ) E$ ]& ^, |0 V% J5 s$ V
  92.     uart->handle.Init.BaudRate     = cfg->baud_rate;
    1 d* m7 g  ?+ }9 T& s) o- X4 }3 w
  93.     uart->handle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
      Q& j+ f0 n* c# D4 T
  94.     uart->handle.Init.Mode         = UART_MODE_TX_RX;
    / H  ~* `3 h) M. ~, H9 k
  95.     uart->handle.Init.OverSampling = UART_OVERSAMPLING_16;# @( [5 I  n$ V0 V: k' D! |
  96.     switch (cfg->data_bits)
    - b" u( Q) I+ Z
  97.     {
    . e! L8 C- V& y; B% V
  98.     case DATA_BITS_8:
    ; l! E) Q7 C9 }' I# h3 @# Y- o4 R
  99.         uart->handle.Init.WordLength = UART_WORDLENGTH_8B;
    ! B; y! D2 u# @. m; ^6 P: m
  100.         break;; f" A& s5 M4 Q+ ^; `6 n( s  _, G
  101.     case DATA_BITS_9:7 |1 b3 k" Z- q6 p6 ?9 d4 y
  102.         uart->handle.Init.WordLength = UART_WORDLENGTH_9B;- y- J2 z% R" b+ w1 q6 M! @/ y! |7 I
  103.         break;
    ( e* Y* ]& g# o8 D( U$ u; W
  104.     default:0 z8 F( w- c4 b* W
  105.         uart->handle.Init.WordLength = UART_WORDLENGTH_8B;: q( ?1 [" S$ X3 D, k/ f, ~
  106.         break;
    / C" E4 i0 V( _5 R# `/ R% m
  107.     }: L- O3 `# h% c* _! {/ X
  108.     switch (cfg->stop_bits). Z. m1 d+ ?; ~  I0 t  Q( I, x! c3 Z
  109.     {: w$ `1 J  X3 h  n$ B$ _% J, P
  110.     case STOP_BITS_1:5 S6 D$ ?* k; R, `; W
  111.         uart->handle.Init.StopBits   = UART_STOPBITS_1;
    0 @( t& x% d( ~" p3 t4 ?8 Q
  112.         break;" ]# M6 o# v; y0 ^. r
  113.     case STOP_BITS_2:
    . U  F( u4 t( f! i
  114.         uart->handle.Init.StopBits   = UART_STOPBITS_2;% J; R* L8 i" S6 G7 c# J
  115.         break;5 \3 C& c  L# u/ C& T1 \9 ~
  116.     default:
    ; W3 `5 f& H# D/ c- Q! E
  117.         uart->handle.Init.StopBits   = UART_STOPBITS_1;
    ; k9 |/ R3 ?. x! B
  118.         break;" n: m2 t! g6 Z. G$ O. m
  119.     }* E! P9 g7 [" g6 y, }! ^
  120.     switch (cfg->parity)
    9 A( h) e# _6 c# R9 d2 D
  121.     {/ C) J7 Y3 @( ^' ?$ f6 h5 n# a
  122.     case PARITY_NONE:
    3 r) }6 `( F* e! f' u/ w
  123.         uart->handle.Init.Parity     = UART_PARITY_NONE;
    # A# s6 Q4 ^; Q7 q
  124.         break;" i2 g8 Q" M$ |! w
  125.     case PARITY_ODD:
    ) ?' ^9 `9 p; C& {9 c5 A
  126.         uart->handle.Init.Parity     = UART_PARITY_ODD;% g, q  |. U0 e% ]& K
  127.         break;
    1 d' Z+ @0 P7 b7 M) C2 j
  128.     case PARITY_EVEN:
    # a3 f- D5 z# \- |; U
  129.         uart->handle.Init.Parity     = UART_PARITY_EVEN;2 T& h  R0 B2 N
  130.         break;6 P# o, m4 R! ]4 X  f
  131.     default:
    $ Q7 O/ f: b: O8 X# L
  132.         uart->handle.Init.Parity     = UART_PARITY_NONE;4 c- {) t% |& P, ~* p) F+ K
  133.         break;
    , L. A& U( _" y* N' r1 l9 Z3 J
  134.     }
    0 H7 J" H: a) c0 Q

  135. 0 Y( a4 e: g5 c7 y+ q
  136.     if (HAL_UART_Init(&uart->handle) != HAL_OK)
    8 p/ a3 N. G/ w: `/ m, ?
  137.     {
    6 g* _4 c( L" h4 v
  138.         return -RT_ERROR;
    ! r3 F% u0 G  v9 `+ V6 i! N4 q' T
  139.     }- [8 D; [, {2 p) @8 z) U/ u; c
  140. 0 e* S  y9 c& s  ~! k% \3 C
  141.     return RT_EOK;
    " k6 z, ]4 \) m" H5 P
  142. }4 r5 X2 I. |% v+ h+ `3 }6 H

  143. 6 k* V! g/ Z9 G# v( W# h& {
  144. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
    # b3 M/ D- n* L) h2 w8 j% P+ E
  145. {
    ) t# z( y+ f5 {) {: g' L
  146.     struct stm32_uart *uart;4 j% p+ q6 H$ x# k; U6 F6 f0 a
  147. #ifdef RT_SERIAL_USING_DMA
    % \9 U7 g# t6 N: Z
  148.     rt_ubase_t ctrl_arg = (rt_ubase_t)arg;+ N/ j1 f& M5 l  j) }: H
  149. #endif% T0 l$ m  ^* v
  150.     + Q! Q) n# h; t5 P
  151.     RT_ASSERT(serial != RT_NULL);7 ^& f, n2 S' F# T6 j
  152.     uart = (struct stm32_uart *)serial->parent.user_data;7 l' H8 \% F" F$ d
  153.     RT_ASSERT(uart != RT_NULL);, O9 V  u0 \/ a$ b

  154.   j; {0 [# v4 A0 z* j" h
  155.     switch (cmd); E6 g8 m4 Y6 f: W: h, Z' K7 u6 g
  156.     {
    & ]" [& Q* w2 x# {
  157.     /* disable interrupt */
    ( {2 j1 ^, K" [3 s2 Q$ E# d
  158.     case RT_DEVICE_CTRL_CLR_INT:
      I* J( J2 d4 Q* {- Y. W/ }5 I% a
  159.         /* disable rx irq */
    % L7 E, S5 S* E
  160.         NVIC_DisableIRQ(uart->config->irq_type);
    . V7 @4 u5 o( {! C$ y0 R
  161.         /* disable interrupt */
    2 u# r1 B. O( N. W! L5 A
  162.         __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_RXNE);
      M2 b" B8 o+ [  X6 q1 l
  163.         break;
    8 y& G  R' B( r. w
  164.     /* enable interrupt */
    3 w" _; U7 T' U8 W0 X7 W7 G
  165.     case RT_DEVICE_CTRL_SET_INT:
    3 f" C& W6 d4 I: q% l
  166.         /* enable rx irq */' o8 P% P, H6 M7 T
  167.         NVIC_EnableIRQ(uart->config->irq_type);
    $ ?: y- R  n' Y2 h7 L
  168.         /* enable interrupt */7 g, C- R; {: B* r/ q$ Z$ u
  169.         __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_RXNE);
    ( P; s4 T' _. K% c: e
  170.         break;
    $ i; D) U* x# D# e3 z; z1 `
  171. 6 `+ Y/ Y5 R# P% ~! _0 [, Q
  172. #ifdef RT_SERIAL_USING_DMA
    1 M2 P* _6 R* k
  173.     case RT_DEVICE_CTRL_CONFIG:1 L+ X# Y1 K- I3 u% f1 J* L3 S
  174.         if (ctrl_arg == RT_DEVICE_FLAG_DMA_RX)
    4 c0 p. n, {0 A/ U& ]
  175.         {
    ; X, C) }  R! X' p+ b- v& g/ R$ e* a/ k; k
  176.             stm32_dma_config(serial);
    . X2 O! t0 @: g$ P* {* D
  177.         }# d* b9 I1 c8 h5 v0 d# a
  178.         break;
    + n1 l" D9 ]$ {# A  i: v
  179. #endif2 O. o- {( D* i  J
  180.     }
    * N/ L7 i7 a6 M0 O9 H
  181.     return RT_EOK;2 _2 n$ j& ^" C' t6 ~
  182. }& `$ x/ Q) L- ~" E
  183. - l3 \8 T( e  e
  184. static int stm32_putc(struct rt_serial_device *serial, char c)$ |- o7 L. F8 Y2 d" g$ _
  185. {
    / v# o8 B9 j0 d4 E4 Z# D. R& n
  186.     struct stm32_uart *uart;2 v7 c8 R) `6 o3 x: w; m: _" `
  187.     RT_ASSERT(serial != RT_NULL);
    4 c. [9 W& N; r1 c
  188. 7 e; o/ Y1 R$ \6 d/ @
  189.     uart = (struct stm32_uart *)serial->parent.user_data;
    3 @& B5 A' f) B. |2 ]2 h
  190.     UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);5 \+ V% i( N% w! q; A6 b9 C
  191. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \( Y# g3 B1 [" {# |4 U$ ^4 Q6 F+ g
  192.     || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7)
    . r9 H% Y5 H! C  E" F5 Y3 P8 m
  193.     uart->handle.Instance->TDR = c;: V, k5 Z) w, a$ Q! l) x2 k
  194. #else* p5 N. R0 O( l+ Z* U; I- O
  195.     uart->handle.Instance->DR = c;8 M0 h/ _# X4 p" o0 j( Z
  196. #endif4 T% m: q& M1 x# O. ?% I4 c
  197.     while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET);7 L/ e. f' @* t# Z" G, a1 i, _
  198.     return 1;
    + T9 z: ]& H2 E+ u+ ~2 h) n! `# F
  199. }
    ( j, ~" }  d  a* z4 V3 @# ?5 c
  200. $ u- c$ Q2 [0 u6 j1 D/ w
  201. static int stm32_getc(struct rt_serial_device *serial)
    $ E' a/ [3 j# {; `* b- d
  202. {& O2 |! f) w9 k0 [  i
  203.     int ch;! T% q3 ~+ M# [0 i. J/ s
  204.     struct stm32_uart *uart;
    7 z- o" @9 V( K" ~. g- V& F
  205.     RT_ASSERT(serial != RT_NULL);
    * D# ~) f3 I- N( _/ j
  206.     uart = (struct stm32_uart *)serial->parent.user_data;
    , G. u) F' |  j! _/ I! y7 |
  207.     RT_ASSERT(uart != RT_NULL);
    & |$ z( }: M, a' R- K5 [
  208. # s  w- w: o7 q  b$ t
  209.     ch = -1;
    " a; E& z2 T$ c
  210.     if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET), y+ _# {, G: o- ]' {, m
  211.     {6 J: ~. H6 _1 w% w
  212. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \
    % }  F7 T6 f' U0 @9 }) S+ N8 h/ C
  213.     || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7)" P% h% E. |: v. }
  214.         ch = uart->handle.Instance->RDR & 0xff;
    # H- K' @, E3 T& l% u. u# Y
  215. #else, U1 U8 A# x- D- J
  216.         ch = uart->handle.Instance->DR & 0xff;
    & n* k9 o& g& R" Z& z
  217. #endif0 x+ s1 |* T  r! V5 ~1 K% R: y
  218.     }: `# Y0 r: w/ K
  219.     return ch;% o: T) R. S0 P* H& b
  220. }
    5 K& T% \( O7 G- M3 {+ j

  221. 5 K, C  r. X2 y! S6 G/ p4 F
  222. static const struct rt_uart_ops stm32_uart_ops =
    ' t% N7 u- E1 k/ H' n
  223. {
    5 u# J) f  Y% N/ X$ d9 \
  224.     .configure = stm32_configure,
    6 R$ ]* i, v4 S& ~# |" H! j! A4 }8 l
  225.     .control = stm32_control,/ c+ Y. g( C; V
  226.     .putc = stm32_putc,/ }& X0 b) b  A
  227.     .getc = stm32_getc,! f. L" D8 u8 ]- b- K, M
  228. };
    $ L% w4 W+ }! I' I: Y+ ~
  229. ' i; ~( v7 {& ^
  230. /**0 O" Y5 D) G6 [$ e$ m# {  o: Q
  231. * Uart common interrupt process. This need add to uart ISR.8 L% Q4 O8 F& L' R& g6 {. e
  232. *
    " \" M- w- J. r+ N0 Y
  233. * @param serial serial device
    - R1 u/ ]9 E" m
  234. */( q' [: j/ K( A0 U; Z  O
  235. static void uart_isr(struct rt_serial_device *serial)
    ; w# ]5 C1 l" V
  236. {
    + q& P( [( t% w  ?
  237.     struct stm32_uart *uart;9 U; ~" {2 n! ?" z4 v" W4 O
  238. #ifdef RT_SERIAL_USING_DMA: W; p0 p# `' W9 O* u% g
  239.     rt_size_t recv_total_index, recv_len;% c  j5 `: {( }3 @1 X5 _* X/ u
  240.     rt_base_t level;; h& V6 Y( j+ n# S3 p3 P
  241. #endif( |5 K; E# A) x$ E) v' W3 O( ^
  242.    
    " X" u8 G/ a# U  v% p
  243.     RT_ASSERT(serial != RT_NULL);% j" E5 G& q# e& e* s+ R9 F
  244. " ?$ ~" o" o. v& B. `' _* S) w
  245.     uart = (struct stm32_uart *) serial->parent.user_data;2 V* X8 H; ^9 u+ d2 P' M
  246.     RT_ASSERT(uart != RT_NULL);
    : H1 G" ~9 T5 ?# K! ]
  247. 3 _% y) T: g7 N, Z
  248.     /* UART in mode Receiver -------------------------------------------------*/9 ~: k7 d/ t% Y6 \+ _1 ?1 U
  249.     if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) &&& y' F1 o' X! \6 P1 {7 N% Y
  250.         (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET))+ J. s2 p' S  g. [. i9 {
  251.     {- s8 z. h1 l! C9 B! f- T( i! w
  252.         rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
    ; a2 u" f' d# o  h. ~
  253.     }
    ( I- m6 y7 \) a2 p) j
  254. #ifdef RT_SERIAL_USING_DMA1 h3 Q' t) @2 ?6 s* [+ C' k$ b2 _
  255.     else if ((uart->uart_dma_flag) && (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_IDLE) != RESET) &&, l# H1 D* L4 I: f
  256.              (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_IDLE) != RESET))
    # l4 t! t9 n8 w7 e: J8 ]7 H- i: j: q
  257.     {
    8 g* n2 S3 m1 |7 I" F
  258.         level = rt_hw_interrupt_disable();
    + `& l/ u, t$ S- w$ A' z7 @* b
  259.         recv_total_index = serial->config.bufsz - __HAL_DMA_GET_COUNTER(&(uart->dma.handle));8 W. R( }! N, |
  260.         recv_len = recv_total_index - uart->dma.last_index;
    4 Z7 W: l( `' X* {0 p
  261.         uart->dma.last_index = recv_total_index;! m; k" D; D5 R
  262.         rt_hw_interrupt_enable(level);
    % w: J* v% `9 F  H6 Q! _# `+ s- L
  263. ' S2 l1 s/ I! p
  264.         if (recv_len)
    . ^8 M" u0 X" N: K4 j& s" q
  265.         {9 D' `3 g( P( H8 S5 q6 J1 B
  266.             rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
    7 {( F; Y- R- x6 Y
  267.         }
    ; ^# Q" Z2 k( }; F' J
  268.         __HAL_UART_CLEAR_IDLEFLAG(&uart->handle);
    / {9 }% N1 S; ]& W
  269.     }, c& J1 m; B& }' m# c
  270. #endif8 S4 z' _& S/ w9 l
  271.     else& f% F- \3 {7 O+ x7 m
  272.     {
    8 T! `3 }# \. z
  273.         if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_ORE) != RESET)
    % X3 L+ u5 u* K* B$ v- L. V( ~- Z2 t
  274.         {
    ) e" d! B) v+ {6 F. `. V
  275.             __HAL_UART_CLEAR_OREFLAG(&uart->handle);8 _& N. R) x4 I8 v3 y# n  x
  276.         }5 L7 \. |, h$ V% o
  277.         if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_NE) != RESET)
    1 |+ j- G6 I' h! _" d4 @: Z' u
  278.         {! b" K0 U$ m/ K$ A) D* H
  279.             __HAL_UART_CLEAR_NEFLAG(&uart->handle);
    * h2 X& u: l( `' A) v. x
  280.         }% n) z' h0 U; _, v0 a- s2 C; t8 x, u
  281.         if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_FE) != RESET)
    7 o& \4 x: K9 _1 J4 S- d* i
  282.         {
    ' G9 v! G) x% g. f
  283.             __HAL_UART_CLEAR_FEFLAG(&uart->handle);
    ! L/ a7 `9 I9 c7 Z
  284.         }
    - {* c' p8 W3 G% d* c, P: C
  285.         if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_PE) != RESET)4 z/ h# f$ E4 _% H/ K4 @$ d
  286.         {
    & h/ u+ |8 ~* z) X+ e+ ^: y
  287.             __HAL_UART_CLEAR_PEFLAG(&uart->handle);5 R' J8 n; `) e
  288.         }8 t; Q: I4 F6 Q" C9 v+ ?( Z& M# j
  289. #if !defined(SOC_SERIES_STM32L4) && !defined(SOC_SERIES_STM32F7) && !defined(SOC_SERIES_STM32F0) \7 q; m# A9 X3 F. j0 H
  290.     && !defined(SOC_SERIES_STM32L0) && !defined(SOC_SERIES_STM32G0) && !defined(SOC_SERIES_STM32H7)
    & S) ~* c: M" H/ \; K
  291.         if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_LBD) != RESET)/ ?; _) c/ [8 j  q) j! K$ g0 T1 J
  292.         {$ r$ N4 q& R6 N" F; |2 n$ `6 {
  293.             UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_LBD);
    8 |% d& ?" S( F8 o' s
  294.         }
    7 \7 N$ ]. s9 `% X/ K1 \1 |
  295. #endif+ l0 Z4 x! a) J# [6 B4 b, Y
  296.         if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_CTS) != RESET)
    8 W$ j: K& m& O6 \4 {- n
  297.         {
    * H1 s5 c* v' A( d& n* B0 o
  298.             UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_CTS);
    - `8 R! }; K* n# i/ o$ i) W
  299.         }) G2 z! Y* C( s
  300.         if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET)
    % t3 ?) k, k: J- F5 F# @
  301.         {# z8 D4 E  w7 D6 r
  302.             UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);8 T) ]" D" X# q, m0 C/ H( ~
  303.         }- k7 w2 y- d% f" c6 \* |
  304.         if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) != RESET)
    $ E+ \9 c- Y' w
  305.         {+ W7 a! @3 l, m) O: ]! B
  306.             UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
    ' F6 L% [7 S/ b9 D  W# b( n
  307.         }
    : j6 l: W' F) I" y, ~5 y0 {& ~
  308.         if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)( a% z: M( w- W3 Q
  309.         {+ }5 o  F% I7 C0 I" W# {
  310.             UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_RXNE);
    ; r# Z1 l3 i5 r# I: j
  311.         }
    $ \2 @! a$ G, Y# ?/ j
  312.     }  w! W  C! {5 g) T
  313. }* x4 K# T9 b4 D2 \6 @
  314. ' p4 B3 @- \9 [* T# a  B* T
  315. #if defined(BSP_USING_UART1)
    ' q3 E. Y* V, Z! [) L/ Z
  316. void USART1_IRQHandler(void)9 i5 C( p/ [, F( t" g0 D. ?7 j
  317. {9 j4 ~5 l' k& i1 k5 g" u1 Z$ n
  318.     /* enter interrupt */
    * _' k5 ]# G( O+ V+ o4 |* s+ C6 o
  319.     rt_interrupt_enter();
    ; [; ~2 |0 s* E. l: D
  320.   a( t; i+ k& {
  321.     uart_isr(&(uart_obj[UART1_INDEX].serial));
    + s/ k* J" J# k) {: U) E8 E
  322.     + d; y5 s; G& |0 R( r' [
  323.     /* leave interrupt */
    & M/ P! g( b* Q# s0 q$ {
  324.     rt_interrupt_leave();
    $ A1 {6 W* X7 M, B
  325. }! U+ R! H& `/ |8 m9 ]! B6 e6 R
  326. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA)2 E) k2 @1 p0 p0 [4 {, v6 v# m
  327. void UART1_DMA_RX_IRQHandler(void)- h' J8 D7 C) D9 }" N
  328. {
    1 ^5 }' }6 ~  |+ ]3 ^# O
  329.     /* enter interrupt */2 h( _0 f, N7 \% t5 y) c
  330.     rt_interrupt_enter();: m* x$ F* l  Y  `( p8 }( O
  331. * f) G/ z, p/ {
  332.     HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma.handle);
    ! Q; ]6 j5 s2 y
  333. : `+ z3 p# S) Y6 U4 k
  334.     /* leave interrupt */
    / w# g* W& b* y& w& |# F1 t( _+ J
  335.     rt_interrupt_leave();- [! S) F" \' W& \, r; ^& D
  336. }
    9 u0 y0 S* A8 ~, q% x; m) c
  337. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA) */
    % T, [/ n$ _. i5 d$ a, h* O' X
  338. #endif /* BSP_USING_UART1 */# h1 U$ {" k& @4 A' V  d

  339. ) ]# B; e6 d% R1 p  G
  340. #if defined(BSP_USING_UART2)/ s. |. @" q6 e; Q5 H) w6 J
  341. void USART2_IRQHandler(void)  ^! M' W4 b4 Z& x! h) s
  342. {  t0 [9 y$ o5 a: s. h4 y& V( m
  343.     /* enter interrupt */" B7 }0 @2 H& q& n/ G
  344.     rt_interrupt_enter();; g' K# T1 B4 M( W2 q/ `' C
  345. / M% s' U( y6 R: }: m/ f2 y/ u
  346.     uart_isr(&(uart_obj[UART2_INDEX].serial));% Z$ g, W, L+ T8 O! j( e& ~
  347. ) ]- o+ a. d% r* _# n  X; t0 S& \
  348.     /* leave interrupt */
    2 G) H$ X. p$ S! \# d# A
  349.     rt_interrupt_leave();
    2 s0 B& ~4 g  e% m) z
  350. }) ~% L9 ]# k; x  D! c
  351. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA)- W- A  B( P0 [* ^8 p
  352. void UART2_DMA_RX_IRQHandler(void)
    + d$ y* R' Y; A+ V5 N; T
  353. {
    ) H9 q7 b0 O# a7 \
  354.     /* enter interrupt */
    9 Z7 B5 M8 I1 t% E, R  W
  355.     rt_interrupt_enter();
    9 o4 c/ ?8 {/ e* ?" w. v8 N
  356. ! K% O  T" z4 T! u
  357.     HAL_DMA_IRQHandler(&uart_obj[UART2_INDEX].dma.handle);6 X- P( X( m. C" Q( P
  358. 9 t& }) E9 e& h7 X$ W# T
  359.     /* leave interrupt */
    9 ~! Q: X6 L6 y. i5 s4 I% z
  360.     rt_interrupt_leave();( |) A0 r' _) \) t( O* z
  361. }
    3 T9 y1 N" {" X7 K2 `  E% k
  362. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA) */2 P' z/ e2 N6 e4 ~" b
  363. #endif /* BSP_USING_UART2 */4 ]  O8 ]/ ^  K3 `- f8 c) f% }0 [0 n
  364.   I* {1 \5 t  K/ b& {3 X8 a
  365. #if defined(BSP_USING_UART3)
    - `( d- N: \$ N! i2 ?: u
  366. void USART3_IRQHandler(void)
    - y: z) T* h! S0 s5 E
  367. {# g2 A& S- C4 e5 K6 v; ]3 I
  368.     /* enter interrupt */
    $ o# H, l6 P% f
  369.     rt_interrupt_enter();9 {4 Z) V+ T7 M, a- }. J, a
  370. / w5 }2 b# c) F. A" a; ]3 }. f+ _
  371.     uart_isr(&(uart_obj[UART3_INDEX].serial));  d) n" j. ?7 l' ]7 l/ C# M
  372.     / c* G- ?5 P  D! w
  373.     /* leave interrupt */
    ! N5 W: l# w4 \3 M
  374.     rt_interrupt_leave();6 P% m* {( N3 W( ?' E! }" l) u* g
  375. }
    & r' _, |  u, G9 {* }
  376. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_RX_USING_DMA)1 Z* m) J4 o2 f% n4 u" m# G: m
  377. void UART3_DMA_RX_IRQHandler(void)) V) }5 X9 n( I. Y4 k
  378. {
    3 n# E9 k( f: s( J5 j! _! J7 o
  379.     /* enter interrupt */
    8 M& O0 }6 _8 ~
  380.     rt_interrupt_enter();; ~- t4 l- `4 G6 A( |

  381. ) S/ F2 S0 b+ {* Y  x) w6 m
  382.     HAL_DMA_IRQHandler(&uart_obj[UART3_INDEX].dma.handle);
    2 x3 h0 {; ^. K" B4 e! y/ u
  383. 4 G( l" f" V- p( U: S1 M: p
  384.     /* leave interrupt */
    & h0 A/ T( Y0 l5 k, K( d+ ?" S
  385.     rt_interrupt_leave();
    5 t2 X6 t  `9 P' u
  386. }
    ! \1 N0 A* Z. H! ~: z, C
  387. #endif /* defined(BSP_UART_USING_DMA_RX) && defined(BSP_UART3_RX_USING_DMA) */
    ( m  N) u+ O6 V% \# ?! j
  388. #endif /* BSP_USING_UART3*/
    ; S4 K/ a/ A; E8 Q3 I; D  U

  389. / V; k1 g( }0 @5 R
  390. #if defined(BSP_USING_UART4)
    0 D% T/ E$ k" {# j, l4 M2 b
  391. void UART4_IRQHandler(void)
    : o5 L7 f0 \5 |; L
  392. {: w3 |6 o& K% t
  393.     /* enter interrupt */
    $ x2 ?- [5 _0 H8 J- d' q# L
  394.     rt_interrupt_enter();. E7 u7 i# {) K" ~" T
  395. ; Q7 D# K) R" G
  396.     uart_isr(&(uart_obj[UART4_INDEX].serial));
    2 A. d2 M& X: D8 v" W3 k/ K
  397.    
    " I; y4 ^: M+ V2 N9 B( u) w6 i4 I
  398.     /* leave interrupt */
    , X/ T% o. Q6 l$ O/ a
  399.     rt_interrupt_leave();& ]* W' t/ Z# N9 y3 n" A
  400. }7 }! ?  v0 R" M* h; B% I( A
  401. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART4_RX_USING_DMA)
    ( F3 E' I: z0 n0 G  O
  402. void UART4_DMA_RX_IRQHandler(void)
    ( e) M9 K* K; ~
  403. {
    5 x$ W: B8 r* [1 G: ]' p/ a
  404.     /* enter interrupt */. T/ L4 X, @; i
  405.     rt_interrupt_enter();* }  V9 Y5 ]; C& D7 @% Z
  406. : U/ [3 {0 g  E/ A+ y6 {
  407.     HAL_DMA_IRQHandler(&uart_obj[UART4_INDEX].dma.handle);
    6 x: f8 o) D% m' V% A+ X/ w* s1 s

  408. $ s+ J$ v5 z. o! d6 P6 o
  409.     /* leave interrupt */
    * F8 Q  m$ d7 c- F7 }* E3 v5 l* i! \
  410.     rt_interrupt_leave();. m* V: L8 x3 E2 T$ c2 I  }
  411. }
    9 M/ C* G% Z+ P) l
  412. #endif /* defined(BSP_UART_USING_DMA_RX) && defined(BSP_UART4_RX_USING_DMA) */
    ' Z% y" H7 h, V# Z% Y& A. Y5 ^
  413. #endif /* BSP_USING_UART4*/, h; F3 a+ b4 N; o4 R# y% [

  414. ( z( @2 ~7 ]  \  i) ]6 w9 J
  415. #if defined(BSP_USING_UART5)/ g# z: {9 k. l8 v" h, {% m
  416. void UART5_IRQHandler(void)  {( V: n& e, M7 J" ^
  417. {+ l3 v4 i: \; i( k8 a. o: G
  418.     /* enter interrupt */
    * O, X6 k; x. o) K' a
  419.     rt_interrupt_enter();9 x, R- g2 G! P& X# q+ A: z5 t

  420. 2 W& e! X) j1 I& ~' o
  421.     uart_isr(&(uart_obj[UART5_INDEX].serial));
    . t$ P5 i+ x1 M
  422.     , s5 t5 d6 k# Z6 x# i$ I
  423.     /* leave interrupt */; q, H6 {. U* a
  424.     rt_interrupt_leave();- E+ z) O" M- c6 Q9 s2 G) B
  425. }9 Z# i4 {1 m6 _/ c
  426. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_RX_USING_DMA)
    9 @2 \) }; p/ N) A
  427. void UART5_DMA_RX_IRQHandler(void)4 Z* N9 w" g. x. H
  428. {3 O1 W1 e$ j3 e3 e8 E7 Y# B# o0 {
  429.     /* enter interrupt */
    % C1 a/ Z; y+ F8 l5 o: {- |" G7 o
  430.     rt_interrupt_enter();; E" z/ x) P2 y+ {2 w4 f$ f/ p
  431. & ~+ e7 s# f8 q
  432.     HAL_DMA_IRQHandler(&uart_obj[UART5_INDEX].dma.handle);
    ! ^7 X6 X+ @& U* b

  433. 0 M% h% e6 N! t6 L( G
  434.     /* leave interrupt */" f1 H# |: I2 i: L3 A$ W
  435.     rt_interrupt_leave();
    ( j* k0 B; a' I0 S4 k2 E" r
  436. }
    1 m3 w; _, ^! O% ~# x
  437. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_RX_USING_DMA) */
    - U# _0 g$ w0 \2 u& G5 b; r
  438. #endif /* BSP_USING_UART5*// w- @0 w9 u# I7 \8 k

  439. ( h  t( Y0 _( w
  440. #if defined(BSP_USING_UART6)7 d) C0 ~; z$ M9 M: V, [
  441. void USART6_IRQHandler(void)
    $ t7 [- c8 T% i; \
  442. {% B- h! @3 E/ d  T: _9 l; N
  443.     /* enter interrupt */8 d: l5 F* c2 a0 d: b" e6 i
  444.     rt_interrupt_enter();" |; T$ h& R; G1 N& @; d- p, k% v
  445. # E, B9 d: t" l/ v0 \; P
  446.     uart_isr(&(uart_obj[UART6_INDEX].serial));
    & |* }8 y+ \2 G+ E& N
  447.     ; ~% k- C/ L: h" }5 f
  448.     /* leave interrupt */. v5 N9 R: [2 [* g4 P8 [
  449.     rt_interrupt_leave();& X9 T7 z/ |& |2 H  j+ m! |
  450. }
    ( e9 k* e2 f& h
  451. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_RX_USING_DMA), F3 V: V/ y' H; F. O; R( Q4 d; g
  452. void UART6_DMA_RX_IRQHandler(void). M8 Q7 R4 K! c2 A% x' ?
  453. {
    + p: {5 L5 U! D/ R
  454.     /* enter interrupt */5 M+ n% m) u4 @) r7 {& a6 U/ f
  455.     rt_interrupt_enter();
    & e& |1 j4 u1 T1 ^! m* c
  456. " n# Q& y3 O1 M* Z0 N. R
  457.     HAL_DMA_IRQHandler(&uart_obj[UART6_INDEX].dma.handle);
    0 \; Q9 P. }# l5 K; ?
  458. 5 s: k/ `, z# U2 l, U7 x- Q& m- M
  459.     /* leave interrupt */* `* _7 O2 a8 T8 b  `; u
  460.     rt_interrupt_leave();& X  t9 x: S1 I! O0 ?- n2 D; X
  461. }
    $ g1 H8 Q( X7 h' G3 O/ G' H
  462. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_RX_USING_DMA) */3 i  }9 O! [9 y  j6 n& L1 o4 {, o# n
  463. #endif /* BSP_USING_UART6*/
    0 l' K# }: T5 [, Y! B5 J  B) [
  464. ' |, p1 {$ s; M7 O+ i
  465. #if defined(BSP_USING_LPUART1)
    ( ^, {1 @6 d$ l2 E$ t+ U9 F
  466. void LPUART1_IRQHandler(void)
    % f5 B! u  g" a( G% z
  467. {
    0 M3 b5 J6 I) F; e8 p; _
  468.     /* enter interrupt */) d/ c) l" k2 k. s1 X: ~
  469.     rt_interrupt_enter();. [( Y9 x1 v8 h5 q* u( m" Z

  470. 7 J) s5 ]  d6 ~. Q( y1 c0 ?, Y% z3 C' N# \
  471.     uart_isr(&(uart_obj[LPUART1_INDEX].serial));
    + M1 m7 N: ~0 Y7 s
  472.     * S' }5 X- e- O" O
  473.     /* leave interrupt */
    , e) ?- g* m* ]* U
  474.     rt_interrupt_leave();& d* R' c$ G# @, V2 N' w/ J* Q
  475. }
    ) a$ K" j' ^9 M+ J" ?! a- x/ e
  476. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_LPUART1_RX_USING_DMA)
    ! U- T# H' o9 m% p5 [: I
  477. void LPUART1_DMA_RX_IRQHandler(void)
    , a; U! |7 X1 o3 `# ~, I/ u) P1 m
  478. {
    3 G- S, w6 w: B* F" z. w+ l: Y
  479.     /* enter interrupt */
    ( D/ m+ u" r+ _- C( K
  480.     rt_interrupt_enter();
    2 ^# u0 _0 M- E. g  u
  481. + }! ]2 r; ~3 g! A9 [
  482.     HAL_DMA_IRQHandler(&uart_obj[LPUART1_INDEX].dma.handle);; Q+ F9 M. C& o# K
  483. / [; e2 K% J. n! E: ~* _( U4 u5 g
  484.     /* leave interrupt */
    7 P! P7 U: d. u) }  ~! {
  485.     rt_interrupt_leave();1 ?3 u0 l2 u9 Q3 y1 |6 N# X
  486. }) m  s, j& ?7 z6 h- h5 ~
  487. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_LPUART1_RX_USING_DMA) */2 W& t* j1 b! B
  488. #endif /* BSP_USING_LPUART1*/- p1 j. ^" J, V8 }4 U
  489. , p9 X( Q% p  d
  490. #ifdef RT_SERIAL_USING_DMA
    # A( o% F, M. ~7 E
  491. static void stm32_dma_config(struct rt_serial_device *serial)+ C- T9 ?* _$ Y! X! q9 l
  492. {& J, o2 @; O5 h) l. a8 m
  493.     RT_ASSERT(serial != RT_NULL);6 X, u: E3 r' Z1 T0 w
  494.     struct stm32_uart *uart = (struct stm32_uart *)serial->parent.user_data;* ], V2 O3 T" I! @
  495.     RT_ASSERT(uart != RT_NULL);
    7 I/ K7 N0 B  o% w
  496.     struct rt_serial_rx_fifo *rx_fifo;5 V& t5 y- w/ D$ ]6 Q3 p2 g
  497.     / Z# v: [/ @! m& \3 y/ z6 k) ?% G( D: o
  498.     LOG_D("%s dma config start", uart->config->name);
    # b$ g% y, h3 B! j. H, K
  499. # \; W6 ~7 y2 A7 B" J
  500.     {
    9 [0 t" p5 ]) R" D/ N0 v6 L. ]: i
  501.         rt_uint32_t tmpreg= 0x00U;* D- M: D' g4 E9 h7 [- Q
  502. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) \4 H% M' Q$ S! I& ^7 m, \
  503.         || defined(SOC_SERIES_STM32L0)" E' y' n3 Z: m9 l6 ~
  504.         /* enable DMA clock && Delay after an RCC peripheral clock enabling*/# Y' J8 h' {5 }# C
  505.         SET_BIT(RCC->AHBENR, uart->config->dma_rx->dma_rcc);( r5 L- J9 |1 ~$ F4 u' u
  506.         tmpreg = READ_BIT(RCC->AHBENR, uart->config->dma_rx->dma_rcc);
      x$ S0 \0 X2 O
  507. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4)
    & ?& l" l7 d7 a; u% ^3 _* l
  508.         /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
    ' [5 G% R5 A! m3 f6 }% {; F. H+ G
  509.         SET_BIT(RCC->AHB1ENR, uart->config->dma_rx->dma_rcc);
    . \7 G9 g- }# a, n
  510.         tmpreg = READ_BIT(RCC->AHB1ENR, uart->config->dma_rx->dma_rcc);* x6 n6 D4 R# x8 H; a: c3 L) s
  511. #endif  
    ) P) o: J. r" g' _, E5 g
  512.         UNUSED(tmpreg);   /* To avoid compiler warnings */
    ; y, v5 A9 n8 B
  513.     }
    ! V* H  u& {* `6 ~2 q) R! i0 l
  514. * ^& ^7 m0 C7 _* ~% w! ~7 ^
  515.     __HAL_LINKDMA(&(uart->handle), hdmarx, uart->dma.handle);6 `' H7 z6 _5 R3 m1 \

  516. ) D1 x- U2 m7 s3 _6 C- n
  517. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L0)
    " j6 g" L: W; r
  518.     uart->dma.handle.Instance                 = uart->config->dma_rx->Instance;
    1 J7 c, \- \' _6 X/ B
  519. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
    . H' |( S6 t; Z! F& e$ P, [
  520.     uart->dma.handle.Instance                 = uart->config->dma_rx->Instance;
    # N# M. |/ H: I1 Y/ u
  521.     uart->dma.handle.Init.Channel             = uart->config->dma_rx->channel;
    ! f$ f5 D% U/ O! u
  522. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0)
    : V) c: R5 D1 q+ U1 A" z3 R
  523.     uart->dma.handle.Instance                 = uart->config->dma_rx->Instance;3 S$ u$ b$ l/ u8 b6 T
  524.     uart->dma.handle.Init.Request             = uart->config->dma_rx->request;: Y, a- F8 l( D
  525. #endif# o* T, l* k/ C/ q* _0 M" G- ]
  526.     uart->dma.handle.Init.Direction           = DMA_PERIPH_TO_MEMORY;# e8 Z8 i! ]9 M* Y. T( C
  527.     uart->dma.handle.Init.PeriphInc           = DMA_PINC_DISABLE;
    2 P' {& V8 t; M' ]" p% ~% j, \
  528.     uart->dma.handle.Init.MemInc              = DMA_MINC_ENABLE;
    6 s2 u1 v7 Q& j9 t' k# L9 A
  529.     uart->dma.handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;7 Y( x0 K  P& B6 V1 i
  530.     uart->dma.handle.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
    2 U' L  y& ~, V
  531.     uart->dma.handle.Init.Mode                = DMA_CIRCULAR;
    ! F4 s. b! ?, q" S& m( C7 ~
  532.     uart->dma.handle.Init.Priority            = DMA_PRIORITY_MEDIUM;8 n9 ~8 z8 Y. a4 g0 ^: p: W% s" k
  533. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
    % z/ o, c) x! Y: E
  534.     uart->dma.handle.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;' M$ ^+ }; @" R" C. \6 q! w6 z/ f
  535. #endif
    9 i# j& N2 B3 b* w8 H$ c9 n# I2 Q; z
  536.     if (HAL_DMA_DeInit(&(uart->dma.handle)) != HAL_OK)
    # J2 k1 i; S2 A$ X" W
  537.     {
    ! Z( H% T8 ~' ?' ^" x* \" D
  538.         RT_ASSERT(0);
    / c, G7 B0 [$ o9 d( T7 Z
  539.     }& U; h3 W$ z4 p8 A+ P4 O
  540. 3 ~% t. \/ M, P, P# Y
  541.     if (HAL_DMA_Init(&(uart->dma.handle)) != HAL_OK)7 O+ Y% r- ]# A4 z' s
  542.     {: _% a, N) H8 B3 k4 Y5 F* A( o
  543.         RT_ASSERT(0);
    * f8 ~, w3 G" a( X, ^. e- T: q
  544.     }
    3 E7 R( w$ ~( |$ \

  545. 4 j- H! Z* b0 F+ I% S# G
  546.     rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;7 d( d+ r0 R5 g4 w1 _7 U( g" ^
  547.     " Q7 s* r, V. ~' }$ O1 |
  548.     /* Start DMA transfer */
    ( y3 }4 V1 T# k6 J
  549.     if (HAL_UART_Receive_DMA(&(uart->handle), rx_fifo->buffer, serial->config.bufsz) != HAL_OK)
    0 m# H7 ~  N1 y7 f5 }' m- a3 R. K2 k
  550.     {  o; @: w9 k  V" P
  551.         /* Transfer error in reception process */
    9 H+ t  E4 Z. K- H
  552.         RT_ASSERT(0);
    # L9 a: ]4 A* K5 C- F$ w1 g$ z
  553.     }% b& o2 H# x) C+ U; T7 `

  554. ( H5 J4 X6 z3 U; E9 P- `' I
  555.     /* enable interrupt */7 o0 |* s0 u- }, g; h) k) F
  556.     __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_IDLE);
    . P$ c4 @# J/ u+ L  K: G
  557.     - T7 e/ k0 ?; ^, [
  558.     /* enable rx irq */
    7 A% h7 f6 X1 v' g$ c2 c
  559.     HAL_NVIC_SetPriority(uart->config->dma_rx->dma_irq, 0, 0);
    0 ^* d9 z; U: t- _2 T' z3 D1 E
  560.     HAL_NVIC_EnableIRQ(uart->config->dma_rx->dma_irq);
      Y4 z. w/ k, Y/ i
  561.    
    0 X4 E. y9 _2 `% c% q3 o9 u
  562.     HAL_NVIC_SetPriority(uart->config->irq_type, 1, 0);" E& Z2 K4 Z: Y5 [2 v6 u3 ?
  563.     HAL_NVIC_EnableIRQ(uart->config->irq_type);
    5 Y- k! x' |& n2 `2 _: `
  564.     1 S. `3 Z5 Y7 n2 S/ y# r
  565.     LOG_D("%s dma RX instance: %x", uart->config->name, uart->dma.handle.Instance);
    4 W. q7 _. N) d& K9 N2 I5 Q9 s
  566.     LOG_D("%s dma config done", uart->config->name);
    9 `$ a3 s- F1 d- U* _5 I$ t
  567. }; o! X) ?& {0 r, f
  568. ( c7 U4 i- S$ h& T6 Q
  569. /**( E" s8 [0 L3 s, K7 z  ]
  570.   * @brief  UART error callbacks
    ) d, a' n1 f! C
  571.   * @param  huart: UART handle
    5 x! j1 I7 I5 U: y7 }. ], V
  572.   * @note   This example shows a simple way to report transfer error, and you can
    , |$ X; H5 V! ^* V' G( ?4 Y
  573.   *         add your own implementation.
    , o9 p/ g) R  G) |
  574.   * @retval None- N$ c% s( ~( t3 ?1 y8 a* h
  575.   */
      x8 ?8 N. D5 D( Y
  576. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)' p: w& ~, T6 p7 N
  577. {7 M$ J0 g0 h8 J7 }$ J
  578.     RT_ASSERT(huart != NULL);  c' ]. l9 O6 R+ f$ A
  579.     struct stm32_uart *uart = (struct stm32_uart *)huart;
    ) s: Z7 S1 I9 r
  580.     LOG_D("%s: %s %d\n", __FUNCTION__, uart->config->name, huart->ErrorCode);- @! ~9 N( |8 z
  581.     UNUSED(uart);' r: t, d: q8 M; d
  582. }4 N- U# I7 g" ^- [! j
  583.   x$ Z' p0 D) Y# V& f
  584. /**9 C* @# q& ~/ y! P
  585.   * @brief  Rx Transfer completed callback" m! ]4 C3 V! S" \
  586.   * @param  huart: UART handle1 a. ^& m1 b) J. {
  587.   * @note   This example shows a simple way to report end of DMA Rx transfer, and
    1 b+ t  q( q" S, i; u/ w- }. s
  588.   *         you can add your own implementation.  x& F8 W0 K* P1 Q" @. A6 l
  589.   * @retval None3 \' e9 v1 _7 K% x
  590.   */( P+ I7 Q/ x  `2 `
  591. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
    * T* ^8 E9 s3 O8 E7 @: H
  592. {
    & N! i  d, F* G  N$ u
  593.     struct rt_serial_device *serial;4 ?9 C! B6 F/ ]$ t
  594.     struct stm32_uart *uart;& R* u6 e. K, z& |* L
  595.     rt_size_t recv_len;
    - p) w! L/ }2 P0 e# n5 [
  596.     rt_base_t level;' M# J( ~7 P" g- @: k$ R
  597. + s! q# e: T7 e/ g- J4 Q
  598.     RT_ASSERT(huart != NULL);
    % ]4 X/ y: r; O2 i* ^: g
  599.     uart = (struct stm32_uart *)huart;
    , |+ I% [0 ?5 U- H2 k0 `& ^
  600.     serial = &uart->serial;
    - x' Y, K* `/ r9 V4 P4 y
  601. 4 ~0 S  R! h# ?  X9 c5 E
  602.     level = rt_hw_interrupt_disable();3 f8 ]* p8 w* {5 H# k8 f( {
  603. $ w. p  m- k6 K0 X  C" x
  604.     recv_len = serial->config.bufsz - uart->dma.last_index;8 v; X: ?* i  [/ l+ ]% Z
  605.     uart->dma.last_index = 0;
    0 }& o0 i( q0 w  \4 g# J2 [

  606. ; N& [: o' Z% V; C4 p+ V9 F
  607.     rt_hw_interrupt_enable(level);  }3 M# P% g1 c8 h+ N
  608.     if (recv_len)
    7 _) m6 l: J' e: v0 \
  609.     {; F: b+ C. l4 _, h
  610.         rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
    " Q3 w7 ^. T1 ]2 L8 c
  611.     }4 C* N& V+ u9 W/ l  Z
  612. }
    * x( b) E. X" X9 M) x7 ~6 A& X
  613. #endif  /* RT_SERIAL_USING_DMA */
    + E- D$ `; f5 y' u+ s* G" G
  614. 3 y* c2 w+ Y/ F" p! W- a, [8 m
  615. static void stm32_uart_get_dma_config(void)
    ! J0 K7 W; _1 b  a: T6 |
  616. {# \# x, Q2 Q( Y3 T
  617. #ifdef BSP_UART1_RX_USING_DMA
    $ c" P0 U# f% S2 T4 n+ f
  618.     uart_obj[UART1_INDEX].uart_dma_flag = 1;$ B7 E, A% L6 j) W. x0 Q7 S+ U) r
  619.     static struct dma_config uart1_dma_rx = UART1_DMA_CONFIG;" t" f" M0 a4 b& c% C# x* t
  620.     uart_config[UART1_INDEX].dma_rx = &uart1_dma_rx;
    1 ~! K, v6 V1 a
  621. #endif
    + r  X2 F+ R, X5 Q: T9 i  R! S
  622. #ifdef BSP_UART2_RX_USING_DMA
    3 D3 M" v! E& f$ r) [9 U  N
  623.     uart_obj[UART2_INDEX].uart_dma_flag = 1;
    # t0 s: Y* I. b8 U. B) k2 j% a
  624.     static struct dma_config uart2_dma_rx = UART2_DMA_CONFIG;
    3 R9 r  I- J( n( H0 h! R
  625.     uart_config[UART2_INDEX].dma_rx = &uart2_dma_rx;7 C0 }7 \! ~7 @) A) B5 F* O  g. V4 }4 n: m/ |
  626. #endif
    6 P- e5 ~/ L4 L* o0 ^
  627. #ifdef BSP_UART3_RX_USING_DMA
    4 \5 X4 t! B; D
  628.     uart_obj[UART3_INDEX].uart_dma_flag = 1;4 b) k; t& P8 u
  629.     static struct dma_config uart3_dma_rx = UART3_DMA_CONFIG;
    - D+ w# ^9 |5 e4 {
  630.     uart_config[UART3_INDEX].dma_rx = &uart3_dma_rx;7 e# w, {9 ^6 P; m8 [9 u
  631. #endif% i* K6 X; O& Q5 G& e
  632. #ifdef BSP_UART4_RX_USING_DMA
    / e) ]2 H0 d1 B, b4 J( e
  633.     uart_obj[UART4_INDEX].uart_dma_flag = 1;# A) X4 M+ Y2 J
  634.     static struct dma_config uart4_dma_rx = UART4_DMA_CONFIG;
    * L1 Z& H6 W- m( H  Y( R
  635.     uart_config[UART4_INDEX].dma_rx = &uart4_dma_rx;, i- I/ S% e% b
  636. #endif
    6 n. s/ i, i; [5 \
  637. #ifdef BSP_UART5_RX_USING_DMA+ p# h8 c: S# _( t0 t5 N
  638.     uart_obj[UART5_INDEX].uart_dma_flag = 1;8 s) T9 k# k. F4 u  V( t% |- C% j7 N. @
  639.     static struct dma_config uart5_dma_rx = UART5_DMA_CONFIG;
    % f5 h3 b% E3 |# f
  640.     uart_config[UART5_INDEX].dma_rx = &uart5_dma_rx;: p+ B! n: }5 H7 c$ f
  641. #endif
    " d& M* S; }+ j6 P& T
  642. #ifdef BSP_UART6_RX_USING_DMA* U) T* a3 \1 g" P' H  S. ~9 h
  643.     uart_obj[UART6_INDEX].uart_dma_flag = 1;
    - b* L5 n4 `1 C% h* r7 s) H" t
  644.     static struct dma_config uart6_dma_rx = UART6_DMA_CONFIG;
    3 g/ }" Q5 ~" Q
  645.     uart_config[UART6_INDEX].dma_rx = &uart6_dma_rx;. {; A4 q; _/ F& g- C
  646. #endif& H+ e& p( y8 t) U* B$ I9 J- V
  647. #ifdef BSP_LPUART1_RX_USING_DMA
    6 f/ \: w' y2 X) O  [# r/ J
  648.     uart_obj[LPUART1_INDEX].uart_dma_flag = 1;
    + m. n# V% V6 ^9 p5 j
  649.     static struct dma_config lpuart1_dma_rx = LPUART1_DMA_CONFIG;
    7 ]0 ~5 ?4 {! V
  650.     uart_config[LPUART1_INDEX].dma_rx = &lpuart1_dma_rx;* o0 E( F: t' L6 Q4 @% ^( I
  651. #endif
      K7 f& [. u- M; r+ H& ]8 n* Q
  652. }# p$ b1 }- w; \
  653. , Z& q' \' s6 E( ?( X
  654. int rt_hw_usart_init(void)2 c/ ], y0 ^- r6 N$ U9 f0 f
  655. {/ E5 y# ~7 y, R1 H
  656.     rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct stm32_uart);) x9 _3 V3 M3 D9 b) X2 K5 z
  657.     struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    ) p/ u+ Z4 G% r* L" S' k& H
  658.     rt_err_t result = 0;& E" W; U6 |2 j  d5 x, c% `& H

  659. 6 \; c7 E- s& T. }% N% |5 B( y( h, w
  660.     stm32_uart_get_dma_config();
    * K5 ]* }8 L5 t% B
  661.    
      H2 o0 v% p( ^
  662.     for (int i = 0; i < obj_num; i++)' L$ L6 [% y4 k
  663.     {( x3 q) A' X2 }# e' S. g
  664.         uart_obj[i].config = &uart_config[i];
    + V( a! R. {' F, S3 H2 L
  665.         uart_obj[i].serial.ops    = &stm32_uart_ops;
    , |8 G" P7 i& n
  666.         uart_obj[i].serial.config = config;
    , m% ^, D; b5 N* e2 R4 W

  667. - F8 U+ H% }0 m! ^! O1 O- Q
  668. #if defined(RT_SERIAL_USING_DMA)4 q/ M3 \3 J* P/ p2 [6 Q* V
  669.         if(uart_obj[i].uart_dma_flag)2 Y& q! p: Q/ a8 B2 T
  670.         {, ?; C5 ^7 k: B+ R) E5 O: C+ ^
  671.             /* register UART device */0 l, t& k& t6 s1 l" E/ L
  672.             result = rt_hw_serial_register(&uart_obj[i].serial,uart_obj[i].config->name,
    ' z3 m: ?# f( _% f  i) ]
  673.                                            RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX| RT_DEVICE_FLAG_DMA_RX
    2 M5 B" F  v/ r, _8 `1 c
  674.                                            ,&uart_obj[i]);/ J0 r+ Y  ]# ?$ z
  675.         }$ E5 m+ S# h4 s+ B# N' U  L
  676.         else! F2 {/ L# ]1 e, e% j
  677. #endif3 }  p, g9 P! y* n% F0 D
  678.         {
    . J8 Z& ^! ~" V
  679.             /* register UART device */
    / b* i! Q' [7 S( c
  680.             result = rt_hw_serial_register(&uart_obj[i].serial,uart_obj[i].config->name,+ i5 A% C* q: T0 I3 j
  681.                                            RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX. L) h6 x) q9 i: u9 r
  682.                                            ,&uart_obj[i]);
    ; c7 F  r+ J( M/ n: i$ E0 P
  683.         }2 G( A5 B( w6 R
  684.         RT_ASSERT(result == RT_EOK);7 J1 D/ u" g2 ]  W- V1 O, U3 A
  685.     }/ @" X9 N# c& v' z3 f3 W
  686. 9 }1 u( Z; \2 Y$ {0 ^
  687.     return result;' O) g  w0 f. ]: b  F2 J$ t# }& R
  688. }
    ' T5 {2 z3 ^7 |3 w

  689. 0 n1 T! @+ H' w: S& s. ~) w
  690. #endif /* RT_USING_SERIAL */
    7 w% n8 p' A, T* `
  691. 1 X/ s; H8 R/ w& N, e
复制代码
6 M# E4 e7 O. ]. A0 }0 i
+ ^( c! h! M& f# }( K5 g. q
使用道具 举报 回复
发表于 2019-4-17 10:22:57 | 显示全部楼层
差37积分才能发消息,不好意思,回不了你们消息
使用道具 举报 回复
发表于 2019-4-17 10:26:12 | 显示全部楼层
我QQ 390763144
; ^% M' A) Y7 V7 w+ q" F2 S微信 dream_daiblo1 w5 C# M( ^* G
" l6 X2 x6 ~7 p0 x" ]0 I* x
5 [7 T! [8 e- n0 \) `
使用道具 举报 回复
发表于 2019-4-17 12:48:08 | 显示全部楼层
没有添加hal库的串口驱动导致,参考此文错误日志,解决
使用道具 举报 回复
发表于 2019-4-17 16:02:28 | 显示全部楼层
Ernest 发表于 2019-4-17 12:48$ y+ c. q$ \4 F& d1 n. q+ K- S
没有添加hal库的串口驱动导致,参考此文错误日志,解决

% `% Q1 U: T* B, q感谢陈老师的帮助,上面问题解决了,能编译通过了,第一次用CubeMX,没注意这个问题。。。
使用道具 举报 回复
发表于 2019-4-17 16:04:42 | 显示全部楼层
现在编译能通过,但是程序会卡在这个函数
* M3 V% M3 R4 r2 ]4 e3 E' y8 Q4 m
  1. static int stm32_putc(struct rt_serial_device *serial, char c)
    # L* }- u, R& X$ W; q
  2. {
    ; H( F9 _# B6 z; w% `3 Q, L; h
  3.     struct stm32_uart *uart;
    3 d  k: J* E. F5 y% L  D
  4.     RT_ASSERT(serial != RT_NULL);% N* L/ d- ~. E/ }- M
  5. ) a: o" {+ {3 N5 ?" }3 `& ~
  6.     uart = (struct stm32_uart *)serial->parent.user_data;! Y, `5 z. u4 s0 Z" U
  7.     UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);; n8 w. t1 H% ~4 M0 R! u7 ~) q  W
  8. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \4 C7 |# n- Y/ e* M
  9.     || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7)
    . ]! K9 S; }6 R) D
  10.     uart->handle.Instance->TDR = c;
    ' A  q- {) k4 Y3 @# ~7 }
  11. #else- ~+ K" U/ z  v: p$ ?
  12.     uart->handle.Instance->DR = c;
    ! w8 R8 W7 _6 j% s- [/ u
  13. #endif
    . j8 m' B: K3 v9 m* C4 o$ m' p
  14.     while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET);  v/ L2 K3 f7 x+ n, T
  15.     return 1;
      [* m6 A3 o/ s+ v7 X
  16. }
复制代码

  K" L4 I/ |7 u! M, ^; t6 b
+ X- v' _8 l: ]* w具体是这条语句 while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET);,没有任何信息打印,不知道hal库怎么调试。。。
使用道具 举报 回复
发表于 2019-4-17 16:23:53 | 显示全部楼层
Ernest 发表于 2019-4-17 10:01
# g$ F: |9 ]; m( J) N$ Nhttps://www.rt-thread.org/qa/thread-10348-1-1.html4 [3 A$ I$ \' e5 c
老的bsp驱动可以参考这个更改

7 F6 Q& h; d4 O' T现在按这个链接再试试,实在还不行就只有将就用2.1.2版本的了。。。
使用道具 举报 回复
发表于 2019-4-17 17:13:36 | 显示全部楼层
本帖最后由 Ernest 于 2019-4-17 17:15 编辑
, x; l# H2 w- A- z. q8 U
Tiv 发表于 2019-4-17 16:239 }, Y* H* q7 e; }$ K
现在按这个链接再试试,实在还不行就只有将就用2.1.2版本的了。。。
: L- A1 c! o0 @" F* d( f6 J
中午不是与你交流了吗?是没有添加hal 库的串口驱动文件导致的。" f. l: b0 f7 Z6 q# B3 ]

. d/ `0 f: g' @添加之后,依然存在问题,是没有按照相关文档要求下载指定位置的文件
9 B8 y* a' u9 k4 v' N6 A$ T. L
% Y+ I) z9 s9 H5 r/ J% k
使用道具 举报 回复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|RT-Thread开发者社区 ( 沪ICP备13014002号-1

有害信息举报电话:021-31165890 手机:18930558079

© 2006-2019 上海睿赛德电子科技有限公司

Powered by RT-Thread

快速回复 返回顶部 返回列表