你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【我的STM32U5 项目秀】+02-STM32U5利用LL库点灯

[复制链接]
shenxiaolin_mai 发布时间:2023-2-2 23:27
        说实话,我不喜欢hall库,也许很多人用起来很顺手,但是我用起来不习惯,而且LL库效率明显高得多,也许是没有接触过上层代码的原因吧,那么我就来说一下LL库的环境搭建以及点灯。4 h  G. e+ ^  p4 {+ K
        我们首先根据官方的LL库以及例程做更改,新建文件夹,拷贝文件,具体如下图。0 f3 n& C) ~* ~2 w5 C
01-文件夹.png

  t4 l$ q. k7 z
        并将文件拷贝到个个文件夹内,方便将文件归类以及后期文件管理,具体的可以看附件里,附件内的源码。具体的可以查看keil工程下的文件,如下图

6 k; w' d. S% A0 e
02-文件.png

* H7 _2 |# C1 q$ M0 P; n# o
文件拷贝完成之后,然后添加文件路径和初始化定义,然后增加如下代码到Main.c文件。
' _/ w) J  f. Q5 U2 q. l) |

  1. 6 W7 R0 P  u$ p4 T
  2. #include "stm32u5xx_ll_icache.h"
    ' q( T1 y( W* _) S
  3. #include "stm32u5xx_ll_pwr.h"" S- D7 F' V( T3 E5 d
  4. #include "stm32u5xx_ll_crs.h"! V$ S- w0 \5 @  r! {
  5. #include "stm32u5xx_ll_rcc.h"* y' F+ H) R2 B9 Z% ]5 G
  6. #include "stm32u5xx_ll_bus.h"/ K: h# r( O+ M; ]: h
  7. #include "stm32u5xx_ll_system.h"
    5 T. R! J7 i: L0 |6 i* S3 q8 D, G
  8. #include "stm32u5xx_ll_exti.h"
    / O6 o+ }9 A" z% m2 k
  9. #include "stm32u5xx_ll_cortex.h"0 V! y+ L5 O3 V9 r
  10. #include "stm32u5xx_ll_utils.h"; E: W% K" r0 y- g9 S% ?4 w
  11. #include "stm32u5xx_ll_dma.h"
    5 ?: S7 {5 Y. ?# w" ]) ?! h
  12. #include "stm32u5xx_ll_gpio.h"; I: v$ e' D+ x! ~5 t
  13. #include "stm32u5xx_ll_lpgpio.h"
    , {$ A( n9 R* ^* k/ V

  14. ; n* x$ f' R2 G( L1 w; B3 T+ ^
  15. ' F2 |! C) W  s, o, D' Y- s

  16. 9 {. s# g4 b0 |5 q

  17. ' F6 o; i2 x7 L( O$ m
  18. #define LED1_Pin LL_GPIO_PIN_7! j& t6 \, i, f9 L3 p, ]+ L
  19. #define LED1_GPIO_Port GPIOC
    3 v; N! Y4 ^, x# X3 L- @! R, h

  20. 6 s5 f% c7 d: F  w3 m

  21. $ P% e5 v1 x/ O' r2 Z

  22. ! u% E" A& ?3 I5 }3 ~/ V
  23. /**
    . U. V. {) x) z5 n
  24.   * @brief Power Configuration& Y( p1 {) P; Z/ v* w+ e  }9 V# |% }
  25.   * @retval None  |% t5 F( I5 t5 P2 l+ ~, G, b( L
  26.   */" i/ ~/ ^# F. q- H
  27. static void SystemPower_Config(void)/ [. y1 R1 o  L! F
  28. {& U  M4 _; a% C( J' S

  29. ' W- n, |0 \8 R7 ?! U6 a. I4 `8 q
  30.   /*8 z5 H; {% P; c& o* o
  31.    * Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
    $ c4 k! k( V& H3 C; z3 }1 t; S7 |: ~
  32.    */. t$ G1 d4 e$ U+ a
  33.   LL_PWR_DisableUCPDDeadBattery();9 ^. I% c5 K  ~# a

  34. " v$ |1 P* J& S( N8 k/ q% }, y
  35.   /*
    3 M$ ]& ~; L7 S, |! e  `6 d7 p
  36.    * Switch to SMPS regulator instead of LDO
    ) b& e8 V9 n0 I  `1 I4 N- W
  37.    */
    ( z) S, i: k$ l1 ]8 ?- d. L3 X1 r
  38.   LL_PWR_SetRegulatorSupply(LL_PWR_SMPS_SUPPLY);' @, X. S4 a* o5 A5 }/ R; x

  39.   C) W/ j+ z. E9 g' ?' Y8 H
  40.   while(LL_PWR_IsActiveFlag_REGULATOR()!=1)/ n, c+ R6 r: j" m
  41.   {- d# u2 @# N# Y# H5 T1 X
  42.   }$ v" _" n* _# `3 y0 }
  43. }2 b7 ]9 V8 w0 ]: M
  44. , z# T! L9 d) B3 L5 }! G5 _# C. F
  45. /**
    ( a' w, _' \. u2 {% D; \/ h
  46.   * @brief ICACHE Initialization Function
    8 R' F3 t+ f1 b  g' _
  47.   * @param None2 f( M" j2 @1 V/ S6 u
  48.   * @retval None
    9 D* A- B7 @% r; l1 o# C
  49.   */
    4 S0 C9 Z% w4 T
  50. static void MX_ICACHE_Init(void)$ r, S* ?5 x  U
  51. {
    6 a2 I# O0 x  x1 d0 g  e

  52. : i+ l4 l* F3 t* S/ i- X9 y
  53.   /** Enable instruction cache in 1-way (direct mapped cache)
    5 G! j( z! \& I4 I  W
  54.   */
    + [# {4 Y$ [  Y1 O4 ]- \. H) L. i! Y
  55.   LL_ICACHE_SetMode(LL_ICACHE_1WAY);
    ! |3 [% {" j) q9 [
  56.   LL_ICACHE_Enable();) t4 _$ {' B" }
  57.   /* USER CODE BEGIN ICACHE_Init 2 */
    % Q; P" h. K  t

  58. 9 U' d6 u. o  O4 x* N# v" N
  59.   /* USER CODE END ICACHE_Init 2 */  x8 q) g' [/ v% S& N. Q
  60. 4 \$ P4 x$ u) L( J- P" m9 n/ |
  61. }# \7 a8 ~3 [/ x' A
  62. / Z) s) p% |5 d, R0 |0 Y; P
  63. ! S2 ^6 Z6 B) D( f7 m& O
  64. void SystemClock_Config(void)
    ; M& l! V2 l7 C' j* Y
  65. {
    # w& f) z$ U2 s" W/ y5 }
  66.   LL_FLASH_SetLatency(LL_FLASH_LATENCY_4);
    5 {3 {8 N: M* E3 M+ i
  67.   while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_4)
    ; L7 Q- D3 a  q6 K$ N+ {5 N4 T
  68.   {
    9 {. H0 O/ ~! e9 q& i' A- l: U
  69.   }" z2 m# S- s; k' `5 I$ F
  70. . X9 r# c7 H% d: {0 E6 i) Y% B
  71.   LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
    : K2 j$ o2 n" G" A
  72.   LL_RCC_MSIS_Enable();) `2 h7 d3 R+ d% w
  73. , _( o' f: g0 _7 d4 K) O
  74.    /* Wait till MSIS is ready */
    # C4 f# Z& Q+ }) ^. G1 {' e4 ?# `
  75.   while(LL_RCC_MSIS_IsReady() != 1)3 J  S6 A3 ?" Q' u* h+ W
  76.   {: f. L2 M: B3 _; x, c9 |
  77.   }8 c3 g- j5 m4 t8 p
  78. " r4 W( K! `2 F. ^
  79.   LL_RCC_MSI_EnableRangeSelection();1 s0 U2 o7 b. @5 v
  80.   LL_RCC_MSIS_SetRange(LL_RCC_MSISRANGE_4);% z4 B4 C. X# w* Q/ s
  81.   LL_RCC_MSI_SetCalibTrimming(16, LL_RCC_MSI_OSCILLATOR_1);- ]+ d! u+ I% h: C1 K1 Z% X6 U
  82.   LL_RCC_PLL1_ConfigDomain_SYS(LL_RCC_PLL1SOURCE_MSIS, 1, 80, 2);
    8 F; y; j, N/ }: d" O
  83.   LL_RCC_PLL1_EnableDomain_SYS();
    7 P( v, D) ?- V% r! J  y$ i
  84.   LL_RCC_SetPll1EPodPrescaler(LL_RCC_PLL1MBOOST_DIV_1);6 }. T( V) f3 E2 G7 b' P
  85.   LL_RCC_PLL1_SetVCOInputRange(LL_RCC_PLLINPUTRANGE_4_8);' ^& E5 @$ I8 D2 q/ U+ i6 S& ~& l
  86.   LL_RCC_PLL1_Enable();5 W' u! V6 z8 F# f; ?' `( L$ s
  87. 2 e1 e1 @; d: Z1 _
  88.    /* Wait till PLL is ready */
    0 o) Q8 W; L1 ~. k& u; e
  89.   while(LL_RCC_PLL1_IsReady() != 1)
    2 B$ W/ n) J2 Y( B% v" X( U( v) ?
  90.   {
    ) T; {. r( @6 @% j, }$ r8 y! ]
  91.   }
    1 V' H) j! E( O9 m0 t4 o: \
  92. 6 P7 b0 ^2 U! m! h' n; P. o
  93.    /* Intermediate AHB prescaler 2 when target frequency clock is higher than 80 MHz */! E) b& \0 N( j
  94.   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2);4 R1 d# E& Z6 Z
  95. % u& D! _. O3 J8 ?6 o5 a3 O
  96.   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL1);
    0 R& F3 Y1 o3 a) D7 y
  97. & O& h5 Q7 |: }+ E' v
  98.    /* Wait till System clock is ready */
    2 e3 V+ i/ d8 N* ]) M+ f1 s
  99.   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL1)  t% F5 ]: C& {0 d
  100.   {9 U+ }9 Q& H' d" k+ W
  101.   }) T) l. F. S( T( K( z" {5 [
  102. 9 y6 f  ]7 s. \/ C4 h
  103.   /* Insure 1µs transition state at intermediate medium speed clock based on DWT*/
    $ o7 i& ~3 R7 @9 O5 x, o
  104.   CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;0 X+ j! ?2 i4 C: [4 G* h9 G

  105. , G: p4 a9 [, U) u& t* @4 `1 z: R: {
  106.   DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;: X0 C6 a& r* h* C! y
  107.   DWT->CYCCNT = 0;8 p3 g% K8 J) G3 k  y5 ~
  108.   while(DWT->CYCCNT < 100);
    . u+ a  H- m( R2 M/ H

  109. / V& S4 d) P& U3 W. T7 H
  110.   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
    - z2 w( ?, m! P" G
  111.   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);7 ?9 x$ k& F' e, `4 l4 C
  112.   LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);6 H2 H4 W* Q4 f0 l" x6 R( q1 b
  113.   LL_RCC_SetAPB3Prescaler(LL_RCC_APB3_DIV_1);! o2 N7 O0 ~0 \

  114. * f- c7 `+ S( X. c
  115.   LL_Init1msTick(160000000);& n" e& H& ~1 H

  116. + w' s1 U8 e& B# X: d9 ^
  117.   LL_SetSystemCoreClock(160000000);  Y' `7 M0 }4 m" |$ K* j
  118. }
    ' t; @) a& j4 H; }* q

  119. 5 T  _# ^6 r+ N6 w
  120. /**
    . R: }' z7 }7 j% Z
  121.   * @brief GPIO Initialization Function$ ~" I' @" Q: M
  122.   * @param None$ B2 ]: o$ y  T! Y) i1 I6 W$ Q* L
  123.   * @retval None
      Q$ J2 B. U: `
  124.   */4 V/ _  m' y* u1 Z8 v
  125. static void MX_GPIO_Init(void)7 H0 Q7 Q5 e* m' H8 M) R4 B
  126. {! x  u2 W+ g! I& D
  127.   LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
    / V* `! d0 \( k
  128. * f; H, I2 B- O8 R4 ~( G# H
  129.   /* GPIO Ports Clock Enable */- `$ V( {% r% Q# a9 \/ `$ k9 h
  130.   LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
    ; o# x  x# m5 m6 Z

  131. 0 q8 f4 W! V5 l. Z; v' q
  132.   /**/
    ' n( R# j7 O8 m) F* b
  133.   LL_GPIO_ResetOutputPin(LED1_GPIO_Port, LED1_Pin);, W0 J0 D% E- \5 C

  134. 7 y" I( @' P% E* k
  135.   /**/. X( }+ c# h8 W1 K* j& s8 v% a
  136.   GPIO_InitStruct.Pin = LED1_Pin;/ L: X! ]4 p0 D, V; k2 M/ F# I
  137.   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;3 A: D; O, E, e& E$ ^
  138.   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
    - f! n+ T6 c* t/ h2 p  U
  139.   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
    # `  k! P# T! q  a) I
  140.   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
    ! i- K5 E! g. I2 R2 O, J6 g- e
  141.   LL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);
    , L7 E6 Z& K/ E, x* |

  142. + J" e) S  Z2 H. ^# _6 P
  143. }
    ( \9 E0 X9 s) l$ u
  144. * i- B* }5 q5 f4 o! }) G+ X& M

  145. 4 o. f& v6 W' G# s+ C8 y' W$ [

  146. $ ]5 f! M7 ]' f1 S% X
  147. int main(void). I! I; W  Y3 `+ @# o4 f: i( e
  148. {+ R3 }$ C% k  @/ z4 Y' G7 r
  149.   LL_AHB3_GRP1_EnableClock(LL_AHB3_GRP1_PERIPH_PWR);
    # |1 J3 b, Y, Q! u
  150.   SystemClock_Config();
    6 n1 g# \3 b2 p, i; r
  151.   SystemPower_Config();
    : v6 M4 n* r% ^' W, m; Q
  152.   MX_GPIO_Init();+ b9 Y  U2 ~$ l( m$ E
  153.   MX_ICACHE_Init();
    ! f/ u9 {* F6 y1 K
  154.   while (1)
    " W+ s: ?6 i) G! N+ u6 P, k
  155.   {
    # S6 Y- l! M7 P" z3 `
  156.                 LL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);4 i0 z' s4 |7 x" ]; E3 k; A( E
  157.                 LL_mDelay(500);( L: }; c: v2 i# ~
  158.   }  ]0 e) M9 s7 a9 L# y
  159.   /* USER CODE END 3 */5 P3 J6 a% W. z
  160. }
    : x5 D. H4 H1 @- v5 X+ g9 C3 `

  161.   h! S2 i" y. x/ y
  162. $ }8 I( X& d2 _" B
  163. 4 A7 S; H1 _5 W5 D7 [! Q& C. j
  164. #ifdef  USE_FULL_ASSERT' S* B3 b* b% Y0 S
  165. /**2 v2 o- \7 a$ W0 Z# j, N2 p
  166.   * @brief  Reports the name of the source file and the source line number
    , d! X: z! I: }9 F, s
  167.   *         where the assert_param error has occurred.  f& X7 e9 |; \7 [6 h
  168.   * @param  file: pointer to the source file name$ L% g1 C  C7 T3 U( r* T
  169.   * @param  line: assert_param error line source number
    ; p1 r/ `/ _) }6 Q" c
  170.   * @retval None9 s0 e$ J, o. x/ w5 Z* f' ~4 C
  171.   */( N; H/ A1 m% x5 |
  172. void assert_failed(uint8_t *file, uint32_t line)! N% Y7 V* g1 o' k& v% {
  173. {
    ) g+ C! a( |+ }: I# z/ e3 ^
  174.   /* USER CODE BEGIN 6 */
    2 L6 f. G! k$ u* [/ B

  175. , |( i  h) p$ y+ ]7 w
  176.   /* User can add his own implementation to report the file name and line number,
    : p: T) A  R; P. W5 s: v
  177.      ex: printf("Wrong parameters value: file %s on line %d", file, line) */' O% p+ |0 P+ c& G

  178. 3 r2 ~0 Z2 Y: Q% H; m- k  P% c6 R
  179.   /* Infinite loop */
    4 I$ b- X, |  s4 s/ I2 ?3 N' h# B0 z
  180.   while (1)
    * K6 T. i8 e( T- B8 t+ z
  181.   {' \- m1 e9 O; g' Z0 [8 ]5 C. Z
  182.   }
    0 W5 ]! v4 w" c+ ^: w2 e" o% \1 t
  183.   /* USER CODE END 6 */; b4 d- p( n! Q. E- K& ?( {
  184. }* v, M$ H, y6 A: {/ l6 B1 p
  185. #endif /* USE_FULL_ASSERT */
    5 z$ i& S( A; }% P1 {% Y3 d3 ^# d+ ?
复制代码
在完成之后,编译,就可成功下载点灯,具体可查看附件。
2 a1 d# l5 I9 c7 U/ G3 _
03-点灯图.png
7 T( ^6 p' G# k+ O( L8 w3 J' [$ y
好了,先分享到这,过年后来了的第一篇。
0 D" Z/ X+ ]5 {6 Q. {6 d: a

2 _* _+ P: p) ~0 K/ v
* Y2 u7 [" r4 R: u* p
附件:工程源码 Demo.rar (716.81 KB, 下载次数: 1)
收藏 评论1 发布时间:2023-2-2 23:27

举报

1个回答
STMCU-管管 回答时间:2023-2-3 17:15:07

期待下一篇

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版