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

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

[复制链接]
shenxiaolin_mai 发布时间:2023-2-2 23:27
        说实话,我不喜欢hall库,也许很多人用起来很顺手,但是我用起来不习惯,而且LL库效率明显高得多,也许是没有接触过上层代码的原因吧,那么我就来说一下LL库的环境搭建以及点灯。( }6 c, ]7 u) j$ q- A- e$ u; m
        我们首先根据官方的LL库以及例程做更改,新建文件夹,拷贝文件,具体如下图。! x5 O+ s7 f# F. l- X* l. M6 s; o
01-文件夹.png

" F: a6 W/ X! q* S  L# k
        并将文件拷贝到个个文件夹内,方便将文件归类以及后期文件管理,具体的可以看附件里,附件内的源码。具体的可以查看keil工程下的文件,如下图
8 T+ r  q% r, F9 E" H
02-文件.png
* {, D8 Q+ r: Z) U/ n
文件拷贝完成之后,然后添加文件路径和初始化定义,然后增加如下代码到Main.c文件。
4 W- c+ P# Q" W6 T/ T( i
  1. ! E- v6 `$ k0 g( _2 l
  2. #include "stm32u5xx_ll_icache.h"+ u# ]. o, f# l; Z# v
  3. #include "stm32u5xx_ll_pwr.h"( q5 Y# k( x: b
  4. #include "stm32u5xx_ll_crs.h"
    + M1 f8 X# y/ v
  5. #include "stm32u5xx_ll_rcc.h"  n+ m7 e; Z+ O! t+ ^/ |
  6. #include "stm32u5xx_ll_bus.h"
    / y/ r2 T. m/ G# h4 \. A# K& B
  7. #include "stm32u5xx_ll_system.h"
    5 b* t9 t$ a' I8 n8 L- @# ]
  8. #include "stm32u5xx_ll_exti.h"
    ) V, @7 y- S0 ^  F9 ~$ ^5 _/ ^
  9. #include "stm32u5xx_ll_cortex.h", p- g8 U- x+ \+ ~& E* R! R
  10. #include "stm32u5xx_ll_utils.h"( \' d0 W" t2 Y: W2 I
  11. #include "stm32u5xx_ll_dma.h"% e: X) B3 e0 R- S. _' S* ?
  12. #include "stm32u5xx_ll_gpio.h"$ K" G+ f, j1 z+ L6 `9 l6 j
  13. #include "stm32u5xx_ll_lpgpio.h"
    3 X  z% d; P9 h& |& ?9 h6 g4 O
  14. 7 D! x0 B& U4 e

  15. ( d- F( K7 E& h7 ^" |
  16. ( \1 y0 X7 f, w) G+ c5 q

  17. ' l( h; s' C8 Q3 X) C
  18. #define LED1_Pin LL_GPIO_PIN_7; k: O: V- {; C( J  y( d% m( H2 j" w
  19. #define LED1_GPIO_Port GPIOC, ^+ Q" p# L1 F- j" N
  20. ( P( l4 M8 j- \+ h+ v
  21. # u+ z, u5 u- ~0 Q6 ~8 }# {8 z
  22. % i4 w. y9 @& E% {, _9 p" S3 B5 `+ x
  23. /**$ i, E$ \" P2 K
  24.   * @brief Power Configuration+ w& c1 w% F0 d9 d: g( Y' j
  25.   * @retval None
    ! q0 P3 L# A! f4 L, P2 G
  26.   */
    ; R+ e  p" K0 e- _6 U3 b
  27. static void SystemPower_Config(void)2 G" u; H6 r' N, R
  28. {
    7 h  V; z  ^8 S. q" N# c' A

  29. - K3 a9 @% D9 E  M, C" ?
  30.   /*8 R' }7 x. `1 H# q
  31.    * Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral$ @( u! ^1 H' s' j9 z6 O5 c
  32.    */7 D0 f2 Z) S3 T) b. H& [
  33.   LL_PWR_DisableUCPDDeadBattery();
    8 S1 {3 X2 u  Q2 s& n3 z
  34. . l3 a7 T( M4 L$ y& p: I
  35.   /*2 m; {( j1 C$ |* I3 r
  36.    * Switch to SMPS regulator instead of LDO& ?9 e2 s# |( U. {
  37.    */  R& E% |" T6 v' }
  38.   LL_PWR_SetRegulatorSupply(LL_PWR_SMPS_SUPPLY);+ B$ R3 F5 A8 H9 T% g
  39. . t6 A" [7 r# V2 o3 K( k# F/ s
  40.   while(LL_PWR_IsActiveFlag_REGULATOR()!=1)
    0 Z+ s. \# _& Y: l
  41.   {# \$ i9 W7 i7 M; M( [8 i
  42.   }
    3 }' i. N! ?: J6 t$ T* R7 M
  43. }" W$ k" A3 F% a+ T* e

  44. 4 h7 w4 Q, b: x* k& o, Z
  45. /**, q1 K% n+ g+ l( s
  46.   * @brief ICACHE Initialization Function6 S' ]7 g3 g# Q" `
  47.   * @param None) {0 S" S" F# U& J0 Z
  48.   * @retval None! X& W  B$ K! f' k6 j1 k+ j+ @
  49.   */
    9 A$ [/ ], M' A
  50. static void MX_ICACHE_Init(void)
    ) G5 A' n7 m5 J9 O; H2 S( g4 X# V! J- C. O
  51. {
    4 A& b# r: X  M: b/ a! t' X! A

  52. 0 O, B1 W) q2 p5 h
  53.   /** Enable instruction cache in 1-way (direct mapped cache)3 @( [8 C8 W- i+ Y) v% g
  54.   */
    ; j2 Y: b: f7 f. ]! H
  55.   LL_ICACHE_SetMode(LL_ICACHE_1WAY);  @) x: Y" S: ?; b$ D
  56.   LL_ICACHE_Enable();
    5 _6 q/ A% p" H; J- p. w
  57.   /* USER CODE BEGIN ICACHE_Init 2 */4 k, Q9 z$ d5 M3 u5 i% ?
  58. ' h& D) \3 g/ D
  59.   /* USER CODE END ICACHE_Init 2 */
    ( E" x7 a9 O! Q. [+ D* O% t
  60. 2 d* ]% |  c& ^" P# b
  61. }! D; `0 `8 {3 n, _" k' l" T

  62. + ~8 q% E  L4 M9 w# e

  63. 7 b2 ~" V5 ?6 R& E, Q2 g' m9 L
  64. void SystemClock_Config(void)# T. g7 ^, p7 a$ D+ m
  65. {6 |. v3 M. C- b. ^0 \  Y8 E; K
  66.   LL_FLASH_SetLatency(LL_FLASH_LATENCY_4);  E4 W0 }& R( B9 ]" m4 [
  67.   while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_4)6 z! i, U, t5 g+ g3 M+ _/ @: X
  68.   {* m2 R  v/ ^  D. D* F7 P% [
  69.   }
    ( Z+ `( N+ [3 `( [) ], ?, B

  70. ; l# m' T4 u9 S: E
  71.   LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
    & I4 C" J+ P0 l. A
  72.   LL_RCC_MSIS_Enable();
    6 [3 ]: R- |% ?# v
  73. , z6 S. l+ |/ p/ g9 T1 z' V
  74.    /* Wait till MSIS is ready */
    ) W- q) c- x  e: u3 r
  75.   while(LL_RCC_MSIS_IsReady() != 1)
    # e: S2 y! i* X0 v) A2 f
  76.   {) R* y7 @2 t+ f4 p& p* y0 q
  77.   }
    0 |6 Z7 k7 z/ [: E% @

  78. / G" }7 k- L4 A- ~. g
  79.   LL_RCC_MSI_EnableRangeSelection();
    3 F+ a. f9 a/ {8 ~7 k( D
  80.   LL_RCC_MSIS_SetRange(LL_RCC_MSISRANGE_4);
    ( \* d: w' S/ o
  81.   LL_RCC_MSI_SetCalibTrimming(16, LL_RCC_MSI_OSCILLATOR_1);, `& O8 q( T2 K' l5 D
  82.   LL_RCC_PLL1_ConfigDomain_SYS(LL_RCC_PLL1SOURCE_MSIS, 1, 80, 2);
    3 g/ U$ H9 |- d6 e/ o7 W
  83.   LL_RCC_PLL1_EnableDomain_SYS();8 o1 [0 b% V2 J& l
  84.   LL_RCC_SetPll1EPodPrescaler(LL_RCC_PLL1MBOOST_DIV_1);
    # I0 }7 H2 ]( e5 q" R; p5 i; Z
  85.   LL_RCC_PLL1_SetVCOInputRange(LL_RCC_PLLINPUTRANGE_4_8);
    . ~6 q$ R+ ?( H& S& b# V/ ~- e
  86.   LL_RCC_PLL1_Enable();
    & P/ k# @- Y5 T4 I+ k

  87. - p  E( _) W+ T8 c
  88.    /* Wait till PLL is ready */9 d- I6 w6 d& \8 S8 L4 E+ a
  89.   while(LL_RCC_PLL1_IsReady() != 1). |1 u0 Q( ]; l+ n, h6 Z, _# u# R( }1 ?
  90.   {
    0 T0 P% @& k# q* k) W7 ?0 [1 j  G
  91.   }
      ^% A$ ^) e6 @8 [1 Y

  92. 4 V0 r# a8 b# E, E3 u
  93.    /* Intermediate AHB prescaler 2 when target frequency clock is higher than 80 MHz */
    2 M# r2 S6 n; w% d, ]! i
  94.   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2);
    8 }* U' K' v( F5 p

  95. 5 e9 r4 J0 R' w' I
  96.   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL1);* a6 k# m0 M7 n/ o! P  I' A- X9 _
  97. 3 y  Y2 _2 T% d$ \' I
  98.    /* Wait till System clock is ready */
    * O( b# e, m' U" |- z' D+ W3 j
  99.   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL1)2 ?, ?2 N0 @) D* A" b6 f4 a
  100.   {7 r/ \# c0 d/ ]( X% k0 l4 J$ A
  101.   }
    * Z9 P" c8 S$ u# O
  102. & c, L0 m0 a7 N& d  U; @. G1 \
  103.   /* Insure 1µs transition state at intermediate medium speed clock based on DWT*/5 V7 ~2 u4 j3 x7 z+ N: |' j$ s$ y
  104.   CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
    7 `3 h  p, k6 K4 J

  105. 2 ?% J! A7 E' E( i
  106.   DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;. V/ O+ l: C% [
  107.   DWT->CYCCNT = 0;
    3 p5 w. p* q9 B  {
  108.   while(DWT->CYCCNT < 100);- [1 }( A( u6 k" ?  B' W6 B( C
  109. % S; h8 t. V8 j3 \5 J# d
  110.   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
    ( j0 L0 X& \+ S) `2 h3 P
  111.   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
    1 G0 g8 O" R6 \3 k% x* b
  112.   LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
    4 G& S; H: r( R' ?7 k/ R
  113.   LL_RCC_SetAPB3Prescaler(LL_RCC_APB3_DIV_1);  h4 P( b5 m. x( Y7 p8 B
  114. 4 }1 ~6 C+ c8 W& n
  115.   LL_Init1msTick(160000000);
    , H- j$ X' o  A, h) T, P) |

  116. / h9 w+ i1 a7 V2 d
  117.   LL_SetSystemCoreClock(160000000);; p$ R4 A/ O4 u& O0 i, D5 U2 c
  118. }5 ^) L) P6 L2 E) B4 B

  119. ! Y# v, j6 @6 G& f
  120. /**% |, M  N& A/ c1 j' I4 X5 x) K
  121.   * @brief GPIO Initialization Function( g: r/ E" Y2 Z' H3 f* r0 Y9 P- t
  122.   * @param None2 a" q6 L  c' o" _
  123.   * @retval None7 R3 F3 @7 g; w' R8 d1 F
  124.   */( b; E  _0 X$ ^3 w- O! x- o
  125. static void MX_GPIO_Init(void)
    , h- f- |- n" W8 g
  126. {% i$ {  V+ ~# L
  127.   LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
    & v7 o3 {1 h. h1 M& J# U, L

  128. ' W0 S- M/ ], X/ L2 i
  129.   /* GPIO Ports Clock Enable */* t3 x* Y( ^) Q4 W  B
  130.   LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
    5 J" j/ [& l6 k3 S/ {  `8 E

  131. - e+ [1 i  W" G; F3 V5 H$ [, n# I
  132.   /**/
    7 Z' K  A9 T# l& K& C
  133.   LL_GPIO_ResetOutputPin(LED1_GPIO_Port, LED1_Pin);
    , {7 o$ w. W- s( M+ C. G# n; P% h5 F

  134. / ]9 z+ F/ w! j: e" h) R8 S
  135.   /**/- H" X  Q3 m( r* a
  136.   GPIO_InitStruct.Pin = LED1_Pin;" J' ^# `# g8 [4 T& W0 e8 h6 a: @5 U
  137.   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
    $ F& H: a& j* L# Q
  138.   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;0 b) J( \2 z: R) h, q& s; v0 H
  139.   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;6 \( _' e. C/ p
  140.   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;$ X+ [- R/ n/ |7 B1 ?4 N, ?
  141.   LL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);4 d( }7 T& `9 ]. [: E" R
  142.   T3 J) a* D1 {* K& s/ |) L& Z
  143. }; E! }3 }4 z7 J% m% w$ x
  144. 7 ~9 A5 h4 e) I
  145. + q; U7 M+ x; b. U7 q
  146. 9 e* x9 E8 J1 i+ W% t
  147. int main(void)
    9 @, l5 H4 y# a8 l
  148. {7 u( Q6 D& u7 q7 m9 R2 J
  149.   LL_AHB3_GRP1_EnableClock(LL_AHB3_GRP1_PERIPH_PWR);
    0 H. z% S- x7 `/ k
  150.   SystemClock_Config();( [- B' {7 f5 z# j' n
  151.   SystemPower_Config();' D/ R' A( P2 g" N9 i
  152.   MX_GPIO_Init();
    ; Y- Y5 |3 Q7 U
  153.   MX_ICACHE_Init();
    / @: A. E3 Z+ G: C" i9 {
  154.   while (1)
    4 Y# c' l& B7 [. ~" K" o  [
  155.   {
    6 G- N7 \7 U  M  r0 Z' g0 E9 k
  156.                 LL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);1 }5 d- i6 U1 G: U
  157.                 LL_mDelay(500);
    + L6 ^$ y% A7 w3 T; {$ y! m9 T: J) n4 x$ ~0 Z
  158.   }0 E5 b3 r5 q+ `) V
  159.   /* USER CODE END 3 */
    0 A" w" h9 d: P3 E
  160. }& n4 b+ I- L9 t  z- j! l
  161. ' o% J3 F+ {3 y0 ?

  162. 9 X) C2 d" ]1 U8 j3 D
  163. 9 X$ O, I8 N' Q/ U' \0 [. X
  164. #ifdef  USE_FULL_ASSERT8 x7 Q/ M5 x# u6 N# Y$ R
  165. /**
    , W4 k. f5 r% t' L' B
  166.   * @brief  Reports the name of the source file and the source line number: l1 w% F! e% H2 V
  167.   *         where the assert_param error has occurred.% N! T1 V" ~$ A- k2 y  D
  168.   * @param  file: pointer to the source file name
    ; B" c$ [+ R' \, C  J
  169.   * @param  line: assert_param error line source number
    + U; x6 }. H* T- s: D1 Z2 \1 Y* A
  170.   * @retval None
    / e4 ]5 Y6 a, Q% k- e. w  Y& w
  171.   */6 V8 B* [  M2 ^, n# T% p4 [
  172. void assert_failed(uint8_t *file, uint32_t line)% L! p  I: e- f& m1 D- p. Z
  173. {: B" j. D9 g5 O
  174.   /* USER CODE BEGIN 6 */" ^8 v/ c: q% D. u/ [/ l7 v
  175. ' N* T2 v$ z7 c6 Y2 o3 E
  176.   /* User can add his own implementation to report the file name and line number,
    : p# S7 k. {: |  |8 j9 W, l
  177.      ex: printf("Wrong parameters value: file %s on line %d", file, line) */
    ! c$ ?: m) H, A

  178. ! A0 N/ B* x, |
  179.   /* Infinite loop */
    7 n/ ~3 P% ^# n( `* z) r
  180.   while (1)
    9 o$ o( {+ u! ~! b
  181.   {
    . H4 F1 a* P0 K
  182.   }
    + c- H& ?" [0 K' A
  183.   /* USER CODE END 6 */1 S6 |4 m" X1 A) H3 z
  184. }5 S4 c9 G* |7 ]" x
  185. #endif /* USE_FULL_ASSERT */
    ) |4 M! A/ R# r9 a1 H
复制代码
在完成之后,编译,就可成功下载点灯,具体可查看附件。

4 R* M2 t7 ~9 w0 `& A5 e7 ~' |8 q
03-点灯图.png

& R* g2 c8 Q. Z' H+ e- j) \
好了,先分享到这,过年后来了的第一篇。
  C9 d0 _8 h- H, \& G3 u
, ^* U$ n7 X% N) T! G5 d# `0 C3 @

2 V+ R, I6 @# N. u  s! {+ H  M+ F. L0 v
附件:工程源码 Demo.rar (716.81 KB, 下载次数: 3)
赞 收藏 评论1 发布时间:2023-2-2 23:27

举报

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

期待下一篇

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版