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

ARM CMSIS Driver 学习 之 SPI

[复制链接]
XinLiYF 发布时间:2018-4-14 18:00
ARM CMSIS Driver 学习 之 SPI' _# d3 x, t% v. j3 O0 p. r& I

. ]# ?- k. ]2 w6 {) g8 [' y3 [       CMSIS Driver 都有着相似的 API 函数和相似的调用方法,它是在 ST HAL 库的基础上又进一步的封装,使用和配置起来都要比 ST HAL 库要方便和简单许多,并且还是跨平台的,非常有学习和使用的价值。今天学习 SPI API 的使用,详细介绍见 CMSIS Driver SPI API
1 V+ z9 K) U4 @. v$ S+ u
9 b' J, h& O: j/ z. tSPI 发送与接收
- B6 w& U+ K/ w' s
  1. /**
    $ Y. j4 `0 X8 ?& w4 W& a+ U
  2.   ******************************************************************************
    % T' c$ n% T$ p% K) d- g. j% P1 V
  3.   * @file    main.c9 s9 G5 }: F: U
  4.   * @author  XinLi
    3 [8 V8 k, i! N
  5.   * @version v1.0! h2 g# W) l$ ]# a# F/ i
  6.   * @date    20-March-20180 K% K) W6 [& E# u* j( A
  7.   * @brief   Main program body.; D' f* X7 \$ M1 _: @- c0 x1 W% k: u
  8.   ******************************************************************************
    $ h2 B  ~, w2 y
  9.   * @attention
    ! r& B* ?% J# y9 i. r$ e% Z& e: d
  10.   *
    ) j1 w' ~8 Q; w" f+ Q
  11.   * <h2><center>Copyright © 2018 XinLi</center></h2>
    $ j" V! O/ f5 g  t5 W
  12.   *7 L$ g8 A: F% n* X% |, C
  13.   * This program is free software: you can redistribute it and/or modify
    ) r6 {0 L  A2 M; g
  14.   * it under the terms of the GNU General Public License as published by6 W; ?  l( N$ n; B. ]
  15.   * the Free Software Foundation, either version 3 of the License, or0 O+ U! D) {% I
  16.   * (at your option) any later version.9 h0 W9 y2 k6 r, ]5 z* b. y
  17.   *
    2 o0 ]' B* q& O; I% Q
  18.   * This program is distributed in the hope that it will be useful,
    6 p2 _5 |: v/ g3 O5 E
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    + v4 P4 E% ^' g) P  _; S
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the! }8 ?0 L5 y- c: |# P/ H) R: t, Y
  21.   * GNU General Public License for more details.
    $ X4 Q4 M7 j2 S. Z. W4 ~' j
  22.   */ F% Q9 B, v# }1 X
  23.   * You should have received a copy of the GNU General Public License7 ?- ?, r) j9 w3 \
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.  Z/ U1 {8 u0 ?" b  O
  25.   *
    / T7 u: q1 O. _
  26.   ******************************************************************************+ F9 B. N3 U# L+ w/ d
  27.   */8 p2 P1 S0 j; a% O, W* L, ~

  28. + K! s1 x3 i/ O% ~- e
  29. /* Header includes -----------------------------------------------------------*/
    ! z$ H/ @8 t* L+ h
  30. #include "stm32f4xx.h") W! j& z9 g- w& }. W
  31. #include "Driver_SPI.h"! [6 l2 b& ?) L$ W
  32. #include <string.h>
    6 m; R+ U& I* e# S
  33. 0 g' ~; Y# P2 \- R4 b2 ?
  34. /* Macro definitions ---------------------------------------------------------*/
    # y/ x3 v9 ~$ S5 W1 p3 D! J, g
  35. /* Type definitions ----------------------------------------------------------*/# K* Z/ b5 N, F+ w
  36. /* Variable declarations -----------------------------------------------------*/1 S. `% @* X4 H3 _& v; H) r
  37. extern ARM_DRIVER_SPI Driver_SPI1;  Y% b" C' E# F2 H4 m4 s/ ^

  38. $ _$ n# M. k1 R2 {# h* n7 w+ o
  39. /* Variable definitions ------------------------------------------------------*/2 \) K/ F( s* z1 K+ @5 P# G
  40. static uint8_t txBuffer[5]   = {0xAB};
    & S& x8 p+ T4 m+ k' n5 J8 h/ D& `
  41. static uint8_t rxBuffer[5]   = {0};
    9 Z5 O( c* n# |- o- I: Q8 z
  42. static uint8_t dataBuffer[5] = {0};
    + I# _7 m) w3 v; g% J

  43. ( Y7 x% m. L+ t: ^4 I/ y
  44. /* Function declarations -----------------------------------------------------*/) K: v- [2 p% o" A5 K; {3 C
  45. static void SPI1_Callback(uint32_t event);/ d' B* W) u0 A4 C9 y2 y5 ~: ]
  46. static void SPI1_CS_Init(void);
    2 X, I/ x+ _8 B8 q: E& u
  47. static void SPI1_CS_Low(void);
    5 }, }- J- H, d3 B3 s5 r
  48. static void SPI1_CS_High(void);" m- l' Q7 _# @) P' S1 ~. [/ q. m8 W0 Z
  49. static void SystemClock_Config(void);" }& i, j- t% V9 s3 z' T3 J% c2 H
  50. 6 {, N5 Z1 T& J- w0 r
  51. /* Function definitions ------------------------------------------------------*/
    0 E8 I, n" q9 i+ S0 x
  52. 1 s7 |0 G2 H2 q0 P5 F8 [) i" {, v
  53. /**
    5 y  t% S, e1 j( n% d
  54.   * @brief  Main program.
    5 D7 X8 E+ T1 f
  55.   * @param  None.
    + k6 G6 ]% @$ H% b0 e# ^8 |
  56.   * @return None.: o- C2 `, q/ s/ _
  57.   */
    & c, C! \2 M* O: G) w& M2 q  v# o
  58. int main(void)
    6 G; i2 E9 l9 i" a( n" ^2 d( B
  59. {3 p% d: ^- m- c- }3 z
  60.   /* STM32F4xx HAL library initialization:
    / T2 i9 `! M. Z: |! y
  61.        - Configure the Flash prefetch, instruction and Data caches
    ( v' q2 e( @$ x2 o7 E, t  H0 _
  62.        - Configure the Systick to generate an interrupt each 1 msec- V9 _( _# ~$ G" u' z% f
  63.        - Set NVIC Group Priority to 4  q8 n' j3 I6 U! j% b" K$ Z
  64.        - Global MSP (MCU Support Package) initialization5 l9 v7 p% v% D$ z3 r# M
  65.      */5 l  n+ S1 s8 g/ Z, U
  66.   HAL_Init();5 Z8 e5 F% \2 w" N9 `
  67.   6 a- \. O0 H2 M
  68.   /* Configure the system clock to 168 MHz */& }) T# M5 Y+ q5 O
  69.   SystemClock_Config();8 x3 {" k& o  a0 ?
  70.   
    2 j! \  V: K2 @7 a" ~+ `1 R. r
  71.   SPI1_CS_Init();
    ( m% G/ h2 O- j3 B& _  P
  72.   
    6 }5 X& d; {* T, j2 t9 v
  73.   Driver_SPI1.Initialize(SPI1_Callback);  U9 E( X4 ]6 A. f" k
  74.   Driver_SPI1.PowerControl(ARM_POWER_FULL);. h4 a( \# o9 ]# {% `; W
  75.   Driver_SPI1.Control(ARM_SPI_MODE_MASTER |8 V: R  _! C/ }
  76.                       ARM_SPI_CPOL0_CPHA0 |
    2 n; G/ [/ r/ [+ Y9 B/ i( E, {
  77.                       ARM_SPI_MSB_LSB |" u% h. _$ G0 [& t
  78.                       ARM_SPI_SS_MASTER_UNUSED |$ {% R% D! B2 l5 l) G: |
  79.                       ARM_SPI_DATA_BITS(8), 10000000);3 {7 j* Y7 m0 B$ d- M' @- A/ g
  80.   
    , Q: ]  W8 d1 _
  81.   SPI1_CS_Low();
    3 p( `8 e5 l8 Y' i
  82.   Driver_SPI1.Transfer(txBuffer, rxBuffer, sizeof(txBuffer));
    : Y1 q) |  D9 w. }. u
  83.   
    % b$ F: W8 J7 O3 T/ E7 w) P( G( ]
  84.   for(;;)/ n8 c) r; r9 J% M9 t
  85.   {: b# ?+ S( D/ J* x
  86.    
    6 _$ N* Z; B% j; s7 F5 v
  87.   }
    + M& @0 D) S6 A  T: B$ j* W
  88. }* x9 J. _! l$ @, x3 \) i5 B
  89. 7 M5 m, t# D* c- {
  90. /**! e4 d9 K8 G- q; |1 t) o
  91.   * @brief  SPI1 callback function.
    % ~/ X0 ]4 `# q; Z% ]+ i
  92.   * @param  event: SPI events notification mask.$ z( w, j% d% Q# h6 s
  93.   * @return None.
    : q  x  E$ ?3 T; Z* Z: n: D$ l
  94.   */
    7 @9 I! }) [7 t3 w1 `
  95. static void SPI1_Callback(uint32_t event)) G1 z3 H7 W: \" c+ @6 u  m
  96. {7 x1 F* {1 `, K; k4 p
  97.   if(event & ARM_SPI_EVENT_TRANSFER_COMPLETE)
    % \! t; X+ [8 _4 \* A4 L* n) B1 U
  98.   {2 d) J3 a5 H/ a4 u5 V
  99.     SPI1_CS_High();2 n- N( b4 o: |4 B4 D7 z
  100.     memcpy(dataBuffer, rxBuffer, sizeof(rxBuffer));
    * I6 C1 r0 h5 c1 d. [' B* j* |
  101.   }
    ) J1 S/ W/ x8 y& q" t
  102. }
    1 ^2 h+ c' C" ^) R( P1 x
  103. ! m/ C0 t; Y0 a7 a" W" B# p
  104. /**7 P% B% T  K; D
  105.   * @brief  SPI1 CS pin initialize.. m: R: \8 P, s6 v
  106.   * @param  None.! E" G3 O3 I5 z: l3 {" S
  107.   * @return None.
    1 @- u- N1 N% w* e$ q4 I! p
  108.   */
    8 ~& l7 s+ f# }! L9 |
  109. static void SPI1_CS_Init(void)
    1 e) V* c6 l; s, b
  110. {9 ?- l6 |9 p. n% T# R
  111.   GPIO_InitTypeDef GPIO_InitStruct = {0};3 v7 ?  W6 {+ ~* W. L1 p' R
  112.   
    * ~" C. a& Z* l4 L* f
  113.   /* GPIO Ports Clock Enable */8 E, U* Q  G% Z& v: B
  114.   __HAL_RCC_GPIOB_CLK_ENABLE();! L" n; t8 g9 I  X  ?* t% `
  115.   7 r0 z3 o1 h. ~. n  }/ X
  116.   /* Configure GPIO pin : PtPin */
    % u5 ~( C! }1 K! I, P
  117.   GPIO_InitStruct.Pin   = GPIO_PIN_14;
    8 h$ Z. W7 p! W
  118.   GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;  Z! S. H$ _! V% S) Z! [9 p
  119.   GPIO_InitStruct.Pull  = GPIO_PULLUP;! z, {6 E( N* U  y$ n% Z" W5 g! X
  120.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    1 `2 ^/ h8 I7 F1 n' \
  121.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);$ E5 n, X" \7 c1 G5 F
  122.   0 z' M8 s4 A, p4 o, @. G2 v
  123.   /* Configure GPIO pin Output Level */8 F$ C5 v1 q  S9 X. |+ M; r% b. d
  124.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);& q; d% [: r5 {% Q" {4 \
  125. }
    9 P2 I" \2 U) `5 s
  126. 7 V' `+ S7 N$ a7 F2 Q: L2 c* K
  127. /**+ ^4 y" ~2 `) Z3 b* Y: y
  128.   * @brief  SPI1 CS pin level pull low.
    * G& a+ B5 l2 S0 U/ I1 W  H
  129.   * @param  None.
    6 T. `  f5 ]6 V9 \
  130.   * @return None.
      {( E: _& D  E3 h( d
  131.   */
    3 l0 L# s+ H3 A; ~0 f4 K, q
  132. static void SPI1_CS_Low(void)
    + h; G& H' }7 s- U6 m
  133. {$ i8 w, J3 }5 ^0 N. J" C
  134.   /* Configure GPIO pin Output Level */
    7 t; V6 E! H  A  K* N; ^$ q" J
  135.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
    3 _8 m0 p: e3 h1 [7 r, E+ Y
  136. }0 R* X( X4 I  W% w

  137. 8 I& ?5 \8 ]4 d0 {
  138. /**9 h! B) N7 P& [- p7 Z: V- Y( f
  139.   * @brief  SPI1 CS pin level pull high.6 {$ G5 D6 g# C8 v" f2 v/ Y
  140.   * @param  None., g4 M+ D4 v, t/ E) N
  141.   * @return None.0 k- t0 g- i  S! `+ e2 f( V( q
  142.   */
      _1 \2 G) u) `/ K8 x2 g1 b- }
  143. static void SPI1_CS_High(void)
    5 G  q5 E+ n* D8 ^, u& B
  144. {
    : N- @% G: q: u! X" M2 k
  145.   /* Configure GPIO pin Output Level */7 ]( W  A# {& X+ h, f0 d
  146.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
    7 e( z5 t  N6 A$ ^# G; N/ O! G! t: Y
  147. }. k  h2 @$ `4 ~% [  I

  148. ( u5 ^' h2 q  ^9 Q6 R, p1 {1 E
  149. /**
    $ }- Y& [( o: N1 T9 E
  150.   * @brief  System Clock Configuration( F  P) ?7 D+ G% a
  151.   *         The system Clock is configured as follow :
    8 d0 P# T! j" V
  152.   *            System Clock source            = PLL (HSE)
    - G/ t$ [) O! y+ y. N/ I4 s9 C3 K2 N
  153.   *            SYSCLK(Hz)                     = 168000000
    6 f" w4 ]( _( M
  154.   *            HCLK(Hz)                       = 168000000, o3 q# C: s3 P  e* P3 |& z: M3 ?
  155.   *            AHB Prescaler                  = 1
    8 G" d, O9 d7 n- ^7 G( i9 Z4 ?; w
  156.   *            APB1 Prescaler                 = 4) x% A+ t! R8 c. K
  157.   *            APB2 Prescaler                 = 27 Q" Y) {$ S3 ~5 |' N
  158.   *            HSE Frequency(Hz)              = 8000000
    - l! ?+ w# ^% v) n
  159.   *            PLL_M                          = 8
    & x* u* c) c1 Y6 k  p
  160.   *            PLL_N                          = 336: B8 M* L2 N4 \9 x" ?
  161.   *            PLL_P                          = 2# r$ {$ c! W4 d( r6 E# b
  162.   *            PLL_Q                          = 7; ^2 G7 Q: _  B+ |( z( j6 [6 c
  163.   *            VDD(V)                         = 3.3
    ! w- b5 w, |4 E- M0 N" G
  164.   *            Main regulator output voltage  = Scale1 mode) [9 e0 ~) y" h
  165.   *            Flash Latency(WS)              = 5
    9 r0 O% R7 g1 J) G
  166.   * @param  None, N, q! `" z3 Y% s" U: r+ H( E5 X
  167.   * @retval None
    . y7 L* Y  i; Z8 b% N+ j* b) W
  168.   */. B0 b- E7 [3 p0 |) d# o" w4 S) z8 k
  169. static void SystemClock_Config(void)
    , x6 M- _# z0 T2 |. f( i
  170. {& ]3 l7 D0 @' v4 ^2 b- L
  171.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
    4 `: g$ O) B% Q- S  z# o+ {" R# G
  172.   RCC_OscInitTypeDef RCC_OscInitStruct;
    2 t5 d) I; i3 Q4 H( ^" r
  173. ) N8 m8 J+ o/ }! A
  174.   /* Enable Power Control clock */
    1 Q( W3 r# r+ @3 ?4 B0 L
  175.   __HAL_RCC_PWR_CLK_ENABLE();( X1 o# G9 S  l8 U  }* E

  176. + G6 [1 H! O6 Z2 g+ X8 O& `: w3 F7 A
  177.   /* The voltage scaling allows optimizing the power consumption when the device is
    0 ?, \5 s1 R( g( R3 |: m
  178.      clocked below the maximum system frequency, to update the voltage scaling value & f" u7 W9 {% H
  179.      regarding system frequency refer to product datasheet.  */3 Y3 G. M9 S# H
  180.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    ( j: n8 Q# Q( {/ F. {
  181. * u% a7 H  t- H9 H4 K! O
  182.   /* Enable HSE Oscillator and activate PLL with HSE as source */! k# e7 S7 ?+ A
  183.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    * P5 _' Z2 j7 _5 P
  184.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    0 w! w8 F: o; f8 f9 o! c2 [% }
  185.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    ! |9 Q; S: X' ^8 Q0 B6 I1 ^9 D9 B
  186.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    $ U) f' ?- U% u# N6 {; w  i0 ]& q
  187.   RCC_OscInitStruct.PLL.PLLM = 8;  T( A5 B( ?. W% R! y
  188.   RCC_OscInitStruct.PLL.PLLN = 336;. t9 y! P% K/ z" Z
  189.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;& j: N* i9 d. {0 m0 q2 B
  190.   RCC_OscInitStruct.PLL.PLLQ = 7;
    1 u5 i) J( L9 c
  191.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
    9 y" p( S5 i7 h/ \
  192.   
    7 l8 }; M+ c/ G* c9 r$ {4 n2 [
  193.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
    ) h& J1 U" b0 O; p
  194.      clocks dividers */2 |, }  \0 I2 f9 l: |
  195.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
    & \( Q% n6 t" B0 n* D8 j' n6 \
  196.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    4 J1 q& H3 Z8 t( S
  197.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;& h% e) ~) C2 ~2 n
  198.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  8 e( o) k2 n' N
  199.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  $ ?3 D5 {0 [9 G: l* {; @: {2 I
  200.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);8 E9 i" c4 W( `

  201. * i( m% D, N- d
  202.   /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported  */
    ' r# [: U4 @2 p1 N8 x3 j0 Y
  203.   if (HAL_GetREVID() == 0x1001)
    ; x+ W& W: u$ E5 l# q
  204.   {9 \" `1 b! O- V& }  n# t
  205.     /* Enable the Flash prefetch */& Z  V' ^8 R: R1 V% }" S
  206.     __HAL_FLASH_PREFETCH_BUFFER_ENABLE();  @9 S) g" x7 W2 _, Y
  207.   }) B8 w/ b& ^+ e: P$ T4 a: _, t1 z
  208. }2 M3 U# f3 l" H, o  N$ n
复制代码

# o! m+ T7 U/ k$ i8 U归档链接9 H5 W" J- k4 Q
ARM CMSIS Driver 学习 之 USART
, T# T% S3 C8 M6 v4 K  e
收藏 评论0 发布时间:2018-4-14 18:00

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版