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

如何做SPI主从收发实验基于STM32F103?

[复制链接]
攻城狮Melo 发布时间:2023-3-30 16:47
  1. #include <stm32f10x_conf.h>/ v- l( E. G! q. Z3 v) V/ [; v* f* q
  2. #include "sys.h"! c7 r4 u1 m' j' R' _* z4 r( l
  3. #include "delay.h"
    ' i/ z- ?5 \2 ^. Z2 J1 A0 ~
  4. #include "usart.h"0 u6 e! }7 G( k
  5. #include "timer.h"
    9 o( m( i  [$ @( t$ m+ i
  6. #include "pwm.h"
    ! d( x$ _  h1 G5 K) D% ?$ l
  7. #include "gpio.h"# B6 ^- ?$ ~$ q6 d5 f4 {  V
  8. #include "fsmc.h"
    & p* X- J  X  f- r* ]
  9. #include "iic.h"# h6 C3 {9 J3 l: {; d; H
  10. #include "FreeRTOS.h"% u* \( x+ x. T& l! v# c5 k
  11. #include "task.h"
    5 J: F% G' E6 \3 {4 M
  12. #include "semphr.h"
    . [3 \0 t7 r+ E9 N3 x8 R# H
  13. #include "task_com.h"
    8 l/ F& D4 P% W* V  g. W( d
  14. #include "task_motor.h"
    - P% ]1 h8 Z' W0 {7 S/ @1 E
  15. #include "task_led.h"0 E+ K. V) t/ S0 }
  16. #include "at_cmd.h"3 t) d& L. T6 i2 p7 I, q
  17. #include "string.h"2 l0 z4 w. A( L" `( D
  18. #include "lib5500.h"
    ! K- G/ w$ ]6 D+ ?
  19. #include "flash.h"6 s+ G* F7 \3 x! ^: ?! b3 }/ {
  20. #include "iwdg.h"- u- f& y; B) f  R+ Z
  21. #include "ws2812_sim.h"
    ( F3 R% C" O# {
  22. ( W2 f) \& `  u7 z# F& J0 S
  23. #define LOG_LEVEL_DISABLE      -1, f9 D4 K. W" a5 Y* ^
  24. #define LOG_LEVEL_ERROR        06 V+ }! t$ i3 i6 f+ v
  25. #define LOG_LEVEL_WARNING      1
      r/ a1 m" [( V
  26. #define LOG_LEVEL_INFO         2
    , c7 }; O# A3 H
  27. #define LOG_LEVEL_DEBUG        3
    4 }+ Z! N: U) u7 L$ S  o: ?

  28. ( ?9 [* C2 }9 U. c( J9 f4 _
  29. #define LOG_LEVEL   LOG_LEVEL_DEBUG
    & u9 u* C: h- y  e( ?' h
  30. ! u* q* Q: a+ Y" j4 ^9 G
  31. #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_DEBUG)
    & }' k0 R; x- d' J: W4 u
  32. #define LOGD(fmt, ...)  printf("[D %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)   //%16s,%3d0 `; m7 T; i. p" Z3 d1 |
  33. #else- W9 f: x5 w% p9 f& y, i- }
  34. #define LOGD(fmt, ...)  |- X% j- d  N! `, O
  35. #endif$ v# ^4 c1 r: ^+ U1 s

  36. 4 f8 g( t0 q. \6 X) k$ D' f+ [# s
  37. #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_INFO)
    9 ?0 O/ B+ {1 F8 w8 v- `) I
  38. #define LOGI(fmt, ...)  printf("[I %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
    ' t; k( M" `6 o6 H
  39. #else
    9 s/ H5 V" h9 w1 P
  40. #define LOGI(fmt, ...)
    + ]0 ^5 l8 w% X* {6 q$ u
  41. #endif3 S- `  m" z* F# c8 E. Y
  42. ( F7 x; a% S8 o# ~# s- F' [
  43. #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_WARNING)0 y0 w! c# [  d% g, x- e
  44. #define LOGW(fmt, ...)  printf( "[W %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)$ T4 t+ Q1 X( v$ `& T, W# x- Z
  45. #else
    ' j0 k$ X6 y8 R& f* A
  46. #define LOGW(fmt, ...)
    % m" U5 I, a% o: u) F% U
  47. #endif
    " Y, q5 c7 G; r; X

  48. ) B. p" g9 u+ s2 R, `0 I0 e9 `
  49. #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_ERROR)
    * M( Z3 K+ `: y, ?5 Z0 i
  50. #define LOGE(fmt, ...)  printf( "[E %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)+ }4 C' y7 n) J7 U3 p4 p
  51. #else
    " `& W6 _$ r" `+ I+ P
  52. #define LOGE(fmt, ...)! R, F: D  d- W; [7 [5 N
  53. #endif- E) }3 O$ }+ }. b2 y% S
  54. # q2 l6 a7 `7 _/ q! x, K/ x
  55. #define MARK    LOGD("\n");# m' j# a$ j) D" h3 A( ?
  56. 4 N2 [  f4 D4 i/ `- |  v9 `
  57. SemaphoreHandle_t SensorReadLock;
    1 Y, E8 l  N1 o& ^7 |
  58. SemaphoreHandle_t UdpSendLock;
    ! e/ Q' ?+ Q8 R) I  ^
  59. SemaphoreHandle_t slave_ready_lock;" @0 _# s; |# \0 Y' q- I0 Q- Y2 [, |" z

  60. ( _/ ^1 t& _$ D- s

  61. & e! \: P$ B* |3 l8 [7 e) |
  62. void TaskMaster( void *pvParameters );
    + m& W& H- }# ~7 l# U! }, e+ k
  63. void TaskSlave( void *pvParameters );) P3 S, ?# j, ]. \
  64. static void SPI1_init(void);, P. j( }" q4 e' `1 X: I( g
  65. static void SPI2_init(void);; n5 E; u/ _3 F

  66. 6 P  l& i3 \- V1 r
  67. void Initialization()8 B( j# v$ ]& j
  68. {( |9 S# W! i% m6 P
  69.     MCU_INIT(RCC_PLLMul_9);        //?????: 12MHz * 6 = 72MHz
    " Y# U6 B% Q5 P8 C+ s( }
  70.     Delay_Init(72);# n" h; l5 ?$ h% S
  71.     Uart_Init(USART1,115200,72);    //???????,PCLK1??????PCLK??RCC_HCLK_Div1???  j+ U) N+ l7 s" }" X
  72.     Uart_Init(USART2,9600,36);    //?????????,PCLK2??????PCLK??RCC_HCLK_Div1???2 m2 ^; h3 |5 e+ f( Y' B
  73. //     Uart_Init(USART3,115200,36);    //???????????
    2 E' _) S7 ]9 E9 X
  74.     printf("\r\n\r\n****************Build:%s %s****************\r\n",__DATE__,__TIME__);8 ~' W* D( @9 e2 V2 D
  75. //     UartSendStr(USART2,"System start\r\n");: b% \. N% C6 a6 ~8 J$ g
  76. }+ G1 m( Z0 h# h/ r5 p: p

  77. 4 Y6 [3 z! S& O' Y
  78. int main(void). F3 q1 m; q+ b9 L
  79. {
    % J0 s8 A: y% x/ U, m; m
  80.     Initialization();
    & S+ h8 I" v% W+ m2 B2 ?: ?3 o
  81.    
    . h. _! h3 H& J7 w! B
  82. //    QStepMotorCommand = xQueueCreate( 1,sizeof(int) );
    " ^! v! p' Z6 b# o
  83. //    QHostCommand = xQueueCreate( 16,sizeof(int) );
    2 I$ m) W! n5 m% w9 y& F7 _) f
  84.    
    0 B# F2 u) z  [, Y% Z, d
  85.     SensorReadLock = xSemaphoreCreateMutex();7 |* O3 M/ ?$ K) F
  86.     xSemaphoreGive(SensorReadLock);  q% x$ c* J: T4 I4 j
  87.    
    - H8 r) {& Q2 v. q6 o$ S, z# I# }
  88.     UdpSendLock = xSemaphoreCreateMutex();0 ?5 F+ {! P' C, X
  89.     xSemaphoreGive(UdpSendLock);
    ( {! _6 B& U7 ?5 b) T

  90. : j9 q8 Q& u5 I7 Z( d- d" v
  91.     slave_ready_lock = xSemaphoreCreateMutex();7 W1 k3 Q4 i' H+ y6 u
  92.     xSemaphoreTake(slave_ready_lock, 0 * portTICK_PERIOD_MS);$ L! r+ m! p! Q& `* Q

  93. - n+ z7 Z; a: B  p5 @' F: g
  94.     /* Start the tasks defined within this file/specific to this demo. */6 a! ~0 a+ u' ?/ U2 [2 T% V* Y
  95. //    xTaskCreate( TaskComm,    "task2", 2 * 256, NULL, 2, NULL );" O4 t) ]; @) x# |* ?0 m0 o/ p% h. C
  96.     xTaskCreate( TaskMaster,    "task1", 4 * 256, NULL, 3, NULL );
    * g* u3 M9 J' M: B/ m
  97.     xTaskCreate( TaskSlave,    "task2", 4 * 256, NULL, 1, NULL );8 `, ^* A6 D  ]; Z
  98.     " |# K' p9 \( h* W
  99.     /* Start the scheduler. */: J# F( X( r5 s* s
  100.     vTaskStartScheduler();
    3 w6 Y/ t" P! x  @- A  \8 }
  101.     2 z# d" `1 M! m: P. h
  102.     while(1);! ]+ ?4 Q1 J2 d9 j1 H3 ~
  103.    
    7 N( X. D# N8 b9 t9 f
  104. }' G; s" |/ \$ b. O2 ~. {4 h5 D

  105. % s* `5 m5 w0 Z. a. `! K$ G% ]4 g
  106. 1 \! S6 z2 b5 k3 g
  107. #define USART1_RX_DMA_BUFF_SIZE        128
    1 T/ T2 X/ i5 k+ B- k
  108. #define SPI2_RX_DMA_BUFF_SIZE        32
    5 u9 N; M9 P% S4 }* {# G* L" @9 Z: [
  109. #define SPI2_TX_DMA_BUFF_SIZE        32# V& Q& Z# B/ `% V# z! Q) W" c% J) A

  110. 1 y0 j- X) _4 m  s: I$ Z  ?/ _

  111. / E1 a3 {3 F% R1 i8 e3 D/ u9 [
  112. static void read_uart_input_buff(void);6 ^  P, b& x2 `/ c
  113. static void USART1_RX_DMA_Init(void);0 N2 S. W9 W; H$ I8 ^
  114. static void SPI2_RX_DMA_Init(void);  L+ W( P  R5 U* r6 a. t# J
  115. static void SPI2_TX_DMA_Init(void);" G7 F  O5 d) D9 V% q
  116. static void read_spi2_input_buff(void);0 x+ m' E% Q3 u* b0 v
  117. static void SPI2_frame_handler(uint8_t data);
    7 v& L! F' g8 C' |3 s- K+ _
  118. $ P2 s1 }. i4 P4 D( E
  119. static u32 s_frame_buff[1024];8 D$ u/ S" R6 s6 w
  120. static u8 USART1_RX_DMA_Buff[USART1_RX_DMA_BUFF_SIZE];    //????????????��????????????????????��????????????* A& V) P" w0 _7 g7 w
  121. static u8 SPI2_RX_DMA_Buff[SPI2_RX_DMA_BUFF_SIZE];, b$ ]+ o" q: }. l& J, {) L/ ?. n
  122. static u8 SPI2_TX_DMA_Buff[SPI2_TX_DMA_BUFF_SIZE] = {0xA0, 0x0 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 , 0x8 , 0x9 , 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30};: R3 g! b4 ]& B

  123. : V8 {$ t8 t7 x2 Y) G
  124. void TaskMaster( void *pvParameters )7 S3 \" q+ D4 A6 J
  125. {7 ]: R$ X% T$ d  S& m
  126.     LOGI("TaskMaster start...\r\n");
    8 _6 e7 y6 l7 o9 Y5 p. I, j* k5 Q/ j+ ?
  127. 3 n7 t4 J& Z" C
  128.     // USART1_RX_DMA_Init();; c, p0 R3 |( ^% ~4 c  x& f& v
  129.     // ws2812_sim_init();
    % B! h1 ^4 N) M6 ]5 @4 p! ~
  130.     SPI1_init();
      u) i' n. {* U) S3 ?% g' g
  131.    
    $ v1 B$ f7 J  ^/ `) P1 n/ f2 s
  132.     GPIO_InitTypeDef GPIO_InitStructure;                    //定义一个GPIO结构体变量3 a! I0 b0 g, ?8 ^
  133.     SPI_InitTypeDef SPI_InitStructure;0 h$ B/ F8 z3 k- I: j* f
  134. 0 ?7 X, c1 ^' g- d! A( Q
  135.     //初始化片选引脚  o! ?! x4 l( K9 O
  136.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);      {. L( O" C& Q  W+ z: U

  137. 4 S& H8 ~# |0 U4 Y+ o! v3 g4 v
  138.     //chip select/reset pin init& V4 V' ~& I0 {# J6 h3 J9 l
  139.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    " o0 m, K: Y' d! f9 u0 I
  140.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;4 S5 L2 N9 G/ z. N. d
  141.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;5 p" {2 P# S: x/ w( @" M
  142.     GPIO_Init(GPIOC, &GPIO_InitStructure);7 ~: X+ T3 Z. B3 u' R* e0 r. i

  143. % F! m8 F, r0 |' m# i
  144.   @: f4 A8 _+ N* `4 t: l1 I
  145.     xSemaphoreTake(slave_ready_lock, portMAX_DELAY);
    # K2 X. z$ y0 M& k$ W

  146. 7 i$ V; C8 p5 k
  147.     uint8_t count = 0;
    * v2 M4 D9 H1 s* P
  148. ) x$ }' U- J$ [' M% T0 A# k
  149.     while(1)+ }+ o. N5 W% q6 [* z
  150.     {/ _  z& p7 F/ h  _( w9 K7 K
  151.         // LOGI("test %d %d\r\n", count, DMA_GetCurrDataCounter(DMA1_Channel4));
    7 N# R+ k8 D' H1 k; x! T# x( g
  152. ! F3 x) r8 _- Y
  153.         // read_uart_input_buff();4 ?: c, ~  [: Z2 U. q  T3 O
  154. ! ^& W( |2 ~- B! f
  155.         static int state = 0;. E+ Z0 y, c' G) D
  156.         if (state == 0) {8 w/ z5 R, p3 j* U* v
  157.             SPIx_ReadWriteByte(0xA5);   // preamble
    1 h3 }" b5 m: _0 U
  158.             SPIx_ReadWriteByte(0x5A);
    ; [8 b8 m. t9 p# u/ M. S6 u
  159.             SPIx_ReadWriteByte(0x55);& n# h2 i, d" P0 J- K
  160.             SPIx_ReadWriteByte(0xAA);
    5 r+ \) U* Z9 w9 P- d) k0 `; d% ]1 h
  161.             SPIx_ReadWriteByte(count);   // addr
    ( Q" j/ X- ^/ }% c
  162.             SPIx_ReadWriteByte(~count);   // cmd
    + r2 u, Z+ K+ V; I, Q* v

  163. $ F& O! k& \7 \8 @: w- U0 X
  164.             // count++;
    1 ^! N8 E7 f$ @( F7 v
  165. 7 C- V3 h# K1 \# R
  166.             state = 1;
    ; M# C9 d+ g. r2 e* P
  167.         } else if (state == 1) {
    ' |+ K5 n; A0 R0 f
  168.             static int timeout = 0;) f: n+ \% G3 D1 P$ ~+ }- K" z
  169.             uint8_t data = SPIx_ReadWriteByte(0xFF);   // dummy byte6 ?  {% |* _" a+ M: G. Z
  170.             // LOGI("%.2X\r\n", data);* e+ n: n; ^9 H9 l
  171.             if (data == 0xA0) {
    ( |4 {. c) f$ q0 m, g+ u
  172.                 for (int i = 0; i < 32; i++) {
    ! T* l$ F& N4 L0 D
  173.                     uint8_t data = SPIx_ReadWriteByte(0xFF);
    ) q# G6 M: {9 J8 Q. h/ o/ Z
  174.                     // printf("%.2X ", data);
    , X7 {% {9 |, t2 S2 k3 I* W6 \
  175.                 }
    5 i8 E6 \- `$ \/ m# F) c
  176.                 // printf("\r\n");4 v/ }& w! [4 L# L7 Z
  177.                 timeout = 0;
    # m( ^$ o2 |4 i" R2 a$ d
  178.                 state = 0;& C- }: X' f* T$ E, m% V  W
  179.                 PCout(13) = ~PCout(13);3 O% x- c, {" M4 o0 \
  180.                 // vTaskDelay(100);
    . C, W& o0 w$ Q& ?4 {: k" h
  181.             } else {
    9 I4 H( j- }7 _
  182.                 if (timeout++ == 10) {
    & K1 D! Z# d- `9 D3 |4 }. e
  183.                     timeout = 0;: ^; M) O( B$ Y6 V# Q& `  Q5 i- S
  184.                     printf("timeout\r\n");
    ; \- A* e$ y. e; n$ a/ V. v6 L
  185.                     vTaskDelay(1000);
    ( q$ v3 b, `0 w  s& ?; K
  186.                     state = 0;- N7 j) d/ ]  b. @
  187.                 }5 \. B: K( |& W3 _; F
  188.             }6 s- j9 U1 Z; `) P" {+ [
  189.         } else if (state == 2) {
    . X' B& W* Q$ U1 D0 ~! D; u
  190.         }
    ; W+ Z- @$ N) }! ]( e1 ^

  191. 7 r# G) y, ^! h2 }0 J8 q; c' W- z; _
  192.         vTaskDelay(1);( y& [3 t' Z2 F! }; {2 O3 e3 D; r
  193.     }9 M. Q% m8 r8 K8 Q
  194. }
    $ z8 G; H- C  g

  195. / g3 ?" S, E* N+ i( m- ]  N
  196. void TaskSlave( void *pvParameters )! s: @& S# g- f; n1 R- r: U
  197. {+ v- h$ k5 p/ `( E3 M$ h
  198.     LOGI("TaskSlave start...\r\n");: w1 D$ c! v3 A" F( q! V1 A! Q8 f
  199. " t: G: n" Z& S6 B
  200.     SPI2_init();1 G6 K! q7 c9 |- ]' m
  201.     SPI2_RX_DMA_Init();7 Q5 ?/ K  @7 Q& Z+ O
  202.     SPI2_TX_DMA_Init();
    8 j) O2 D( e# J  [; c+ e
  203. 5 U* K' ^) C# \$ [+ u6 {. o
  204.     xSemaphoreGive(slave_ready_lock);2 `9 n2 ~: n! [' y8 M/ f
  205.    
    7 }6 y! ~! n9 \
  206.     uint8_t count = 0;
    % s5 M% n! S% ~* z; [2 L* z# A9 H# P

  207. # }; X) ]5 y. A0 k- R
  208.     while(1)
    3 s5 }4 X7 o3 P' r7 h
  209.     {
    ( _1 `! X+ i" n' J
  210.         read_spi2_input_buff();% J% E, R8 Y3 n
  211.         ' K  ]: m5 z- j' t$ g3 @
  212.         vTaskDelay(1);: g# b" f, F2 J" u; n
  213.     }
    ) F! V) `  F% l4 _) c# G2 L( a
  214. }! Y# P# }4 Q: s. h: t- u

  215. ; U, T5 ^  p. R2 ^2 I* |
  216. static void USART1_RX_DMA_Init(void)" Y9 `+ E- q+ i
  217. {
    $ A3 m6 T9 R" h8 h1 B
  218.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
    ! I' D: n& F" p
  219.    
    1 }0 I: M8 D2 u3 v/ n# Q' _
  220.     DMA_DeInit(DMA1_Channel5);; i# g+ G2 o. l1 m; x
  221.    
    6 R  g9 }( b, V$ q% t
  222.     DMA_InitTypeDef DMA_InitStruct;
    ; `' a- i* w" {' t7 z, F) ?
  223.     DMA_StructInit(&DMA_InitStruct);
    6 Q4 A5 Y: l* N! o2 [2 R
  224.     DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
    , W9 c% s4 h# `4 z
  225.     DMA_InitStruct.DMA_MemoryBaseAddr = (u32)USART1_RX_DMA_Buff;
    ! W  U7 E1 t8 a; s" Z8 O8 y
  226.     DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;* F9 H* t- `  |% d2 W4 o. R
  227.     DMA_InitStruct.DMA_BufferSize = sizeof(USART1_RX_DMA_Buff);
    & X+ O8 h* l5 U) _  m
  228.     DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;, \. |7 `7 W/ k$ s; s7 U
  229.     DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;2 X: |; w) J  L# y  Q, o, s
  230.     DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    2 h" ?4 w4 M+ f% O* I7 t
  231.     DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;/ b2 U; x4 h- Y$ v: W" C! H7 B
  232.     DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;3 o5 O" c+ y" L. ]9 C$ R3 L
  233.     DMA_InitStruct.DMA_Priority = DMA_Priority_High;  x# s/ F) q2 K+ s3 ~
  234.     DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;  J9 p/ \5 @+ Y4 _
  235.     ; x8 T: [) j, S2 X
  236.     DMA_Init(DMA1_Channel5,&DMA_InitStruct);
    4 n: y9 i* g2 W& i2 S- \% O) d
  237.    
    & R# J1 c# l7 L
  238.     DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,DISABLE);! ]* m% p* R$ v
  239.     DMA_ITConfig(DMA1_Channel5,DMA_IT_HT,DISABLE);
    7 D7 U3 q: n' q% t( t
  240.     DMA_ITConfig(DMA1_Channel5,DMA_IT_TE,DISABLE);# ?: ^( H$ j' O4 N' R5 b
  241.     6 C1 f% }) W1 `7 s
  242.     NVIC_InitTypeDef NVIC_InitStruct;
    0 k- Z! y2 y. N
  243.     NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel5_IRQn;; [5 \! D- l7 Q% i( Q9 L" ?
  244.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    * T0 u( c& a! a6 ^$ a  [9 @' k
  245.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;* `& [& t) E$ u1 p- |5 h
  246.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    + ?- H. t5 @! Y" \
  247.     NVIC_Init(&NVIC_InitStruct);5 O7 _! e) g8 N# W

  248.   x7 d) ]6 |/ O& G) J
  249.     USART1->CR3 |= (1<<6);    //???DMA????6 B4 }; @, I$ _% V# C" k. _1 c
  250.     DMA_Cmd(DMA1_Channel5,ENABLE);    //????DMA????
    3 ^) P0 T* A0 o+ k1 o
  251. / t, I) C5 A9 V. z
  252. }
      _: c; d, M5 f8 t  A/ M: B6 k; e- f

  253. + b( q. _; @# `8 ]$ R6 l, b
  254. // void DMA1_Channel5_IRQHandler()( P/ a. c0 Z8 ?' c
  255. // {
    & m# u, r$ L9 |( n
  256. //     DMA_ClearFlag(DMA1_FLAG_TC5);, v8 X& ?+ H: h) D. J
  257. //     int i;
    7 S+ R; K5 I! B' q) R. ~
  258. //     for(i=0;i<sizeof(USART1_RX_DMA_Buff);i++)0 e" u; K7 k/ r
  259. //         printf("%c",USART1_RX_DMA_Buff[i]);+ E- P- z. c( p! w& S, u
  260. //     printf("\r\n");
    ( L8 V2 K2 R& J
  261. // }& y0 f+ r5 \5 \4 s- I- V) o
  262. : o* M5 Q  M! e9 o# V2 M  k. ]
  263. static void read_uart_input_buff(void), _. g- |4 }' i6 W' V, i
  264. {: [, E% }7 D( \5 s  z( z; L2 u  m
  265.         static int LastDataCount = 0;) Q# u0 v4 |. H" Y8 {2 w' A% X
  266.         5 h4 a5 L" T' G7 K
  267.         //????????DMA��???.????DMA_GetCurrDataCounter?????????????????????.: C) n# A7 c, h. \
  268.         int CurrDataCount = USART1_RX_DMA_BUFF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5);6 p1 f" X+ T' g
  269.         
      |; `! {: |1 k$ f+ g' i, x1 u( c1 S
  270.         if(LastDataCount != CurrDataCount)
    5 o( [9 n6 T# p0 n  y
  271.         {; I% L+ k4 H' a, [* q  g' u
  272.             int i;( J: s" U% K$ Y8 W/ Q
  273.             if(LastDataCount < CurrDataCount)0 A0 R# K% W- e# l, S
  274.             {1 p7 |" S5 A3 t+ m7 _
  275. //                 printf("1:%d %d,",LastDataCount,CurrDataCount);: N0 J. {% V1 u
  276.                 for(i = LastDataCount;i<CurrDataCount;i++) {  f  F: v  H8 s& W) ]0 h) P
  277. //                    MasterFrameInput(USART2_RX_DMA_Buff[i]);
    # S* v3 I1 f0 B6 S' l! u
  278.                      printf("%c",USART1_RX_DMA_Buff[i]);
    + Q! M) m% Z& C3 m; o0 w' s6 L
  279.                 }
    , ~. L% J" X4 c) X
  280. //                 printf("\r\n");
    * Z+ @  ~9 [* a$ w& D; a% q
  281.             }' d) t, K4 X- G; H# J7 E% z
  282.             else
    * g4 z" e) f* q3 Q( O
  283.             {
    ; B  i: e6 v. B# s% l* Y; e
  284. //                 printf("2:%d %d,",LastDataCount,CurrDataCount);
    8 M/ J5 D) [- i7 h8 g; D$ }. d
  285.                 for(i = LastDataCount;i<USART1_RX_DMA_BUFF_SIZE;i++) {- q6 d8 d6 M$ i  I
  286. //                    MasterFrameInput(USART2_RX_DMA_Buff[i]);
    ( n* e9 k9 k7 S; D( q
  287.                      printf("%c",USART1_RX_DMA_Buff[i]);
    : h/ m8 \' ^) Z# \( |8 R4 ^, f
  288.                 }9 v( ?" W) D+ \3 _, g
  289.                 for(i = 0;i<CurrDataCount;i++) {
    : ^0 C! \5 B' u8 P, Z
  290. //                    MasterFrameInput(USART2_RX_DMA_Buff[i]);8 O9 r* ]% k$ n; x2 \
  291.                      printf("%c",USART1_RX_DMA_Buff[i]);
    # S5 _2 {* T& ]2 r  j2 m7 f( k
  292.                 }* t5 w) K# b& k6 F/ N2 F' j1 M, e5 O
  293. //                 printf("\r\n");& l9 N) b: l" h6 b
  294.             }
    3 E7 c. w3 M) m; b
  295.         }
    ( T' G9 I; x" G# [

  296. & H2 d/ J4 C8 x. k$ Z: o; P' z
  297.         LastDataCount = CurrDataCount;
    3 d' P8 D! ~( _# w" W+ ^! A
  298. }
    ; m* E) j5 p: Y
  299. : ~9 i7 z+ ~. x+ F% Z* I1 M! L
  300. static void SPI1_init(void), |' [( q* |( k( x" C' [- \/ L( U9 C
  301. {
    2 `: M6 B( D. z0 U1 Y
  302.     GPIO_InitTypeDef GPIO_InitStructure;                    //定义一个GPIO结构体变量9 a! k5 B  ^; d4 S) t6 f# i* h
  303.     SPI_InitTypeDef SPI_InitStructure;8 N5 l5 D5 i' C2 |7 j) j0 m3 {8 L

  304. ' h. {% e2 n( R3 Z( ?5 m# \7 \+ t
  305.     //初始化片选引脚3 D6 a+ W. X/ M5 D  Z0 ^: i
  306.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);   
    - d+ T9 \/ T4 E) G' v; u( l9 x

  307. & Q% J8 Z' Z% v( u% v$ Z/ q
  308.     //chip select/reset pin init
    ! ]% F% N: F! Z
  309.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11;
    7 u9 L2 [+ m  s3 R' u: n
  310.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    " Y1 m. ~2 ~* d5 b& i
  311.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    / `" O8 N+ q, m- {& Q0 P* ?
  312.       GPIO_Init(GPIOA, &GPIO_InitStructure);0 Z$ S3 k1 y2 d4 X
  313.     // SPI_CS = 1;* ~8 F* @( C: N
  314.     // SPI_RST = 1;
    ! F: D/ [6 s$ `( p! M

  315. 1 s( x' b+ u- u# @; |
  316.     //初始化SPI引脚( V5 f$ y2 w3 a, s( n6 R
  317.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;# K' M1 a4 }% }- H4 a- }7 \+ ^
  318.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;0 y! m# E  c7 D
  319.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;( g! l6 R3 L& F8 M! i! N
  320.       GPIO_Init(GPIOA, &GPIO_InitStructure);* u6 P# Q* \3 [- D
  321. 4 X! s: D9 H$ {
  322.     //初始化SPI10 d% f  D, K- c! v) V% w
  323.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);) z: [) S$ _4 z9 w0 z
  324.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//复位SPI1
    8 l& `& @0 H: G# n  {, e" d
  325.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);//停止复位SPI1+ g9 t: y: b3 y2 w6 ]: U9 X) m4 d
  326.     7 H3 U( x) Q3 i% V& j5 u1 _8 p
  327.       SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;( |* g" H. F( v5 @2 w; B* r* a
  328.       SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    , u) R# S7 l4 {# }; ~
  329.       SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    4 w' }8 H& V' o+ W7 p+ r+ ]) a6 ]
  330.       SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;, _) H7 T# a- Z# ?( p
  331.       SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    0 A  q# ~  A' l
  332.       SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
    ) m0 A' ^8 }$ A( P3 r* l; Y
  333.       SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;5 T& _4 H& U% u4 U, ]: U
  334.       SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    # ?/ h, B  S0 T3 k
  335.       SPI_InitStructure.SPI_CRCPolynomial = 7;; {1 `- u2 w0 J+ F
  336.     SPI_Init(SPI1,&SPI_InitStructure);9 d, [( l- A" u1 V; G
  337. / G9 d3 h2 l. o% b7 |
  338.     SPI_SSOutputCmd(SPI1, ENABLE);
    & S" E0 A$ s3 F
  339.     // SPI_Cmd(SPI1, DISABLE);    //Enable SPI1  
    6 c4 A( @( i" v# F9 V5 g. m
  340.   G! P) d6 {5 R2 m- Z" Q
  341.     SPI_Cmd(SPI1, ENABLE);    //Enable SPI1  
    & O* u' r9 _+ e
  342.     // SPIx_ReadWriteByte(0xff);    //启动传输: X) r1 D) G2 w" ?- c. E5 S; N4 S
  343.     // SPI_Cmd(SPI1, DISABLE);    //Enable SPI1  % {9 D8 K1 h( a, [) @
  344. }* m# }1 z4 u* f1 A3 |0 z
  345. 5 P; A; D* Q7 {) r' y, e
  346. static void SPI2_init(void)4 t1 c0 r: `6 h- k$ d  Z
  347. {9 ~& r5 [, f4 ^% ]% y% N
  348.     GPIO_InitTypeDef GPIO_InitStructure;                    //定义一个GPIO结构体变量9 d% s4 s% d2 ~$ |7 v3 I; A% p
  349.     SPI_InitTypeDef SPI_InitStructure;
    9 }, R# t4 y* j, C0 ]

  350. $ p: F4 _% R2 k, L, m6 t! P* B& ]9 P
  351.     //初始化片选引脚5 l  Z5 g! h- b' M
  352.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);    * ^. z( c$ F5 v: j. D  [

  353. ( _! i3 s0 c9 ]6 V1 \5 S/ r
  354.     //chip select/reset pin init% a& n. b' K1 V) ^
  355.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    & c* s! J* E$ ?& q6 `0 b
  356.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;3 o6 Q) P/ w6 O2 ?  e: G  L
  357.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    5 I) _* x& s0 `" c
  358.     GPIO_Init(GPIOB, &GPIO_InitStructure);
    4 @9 \, I1 S  Z# ?) ~- k. D7 g
  359.     // SPI_CS = 1;
    3 c! G. X6 j3 C. k. x
  360.     // SPI_RST = 1;. P1 k0 h+ S4 t  k
  361. ) k' M1 w' X' D1 x8 w
  362.     //初始化SPI引脚
    ) s! _, ~! r+ }$ S, ?
  363.     GPIO_InitStructure.GPIO_Pin =   GPIO_Pin_12 |   // note that NSS pin of the SPI slave is use to select the: l3 |3 {3 x0 ]! c- G1 {
  364.                                                     // device, if it is pulled up, SPI slave will not shift
    : o4 C1 i+ Q: D% V; X
  365.                                                     // any byte to its shift register
    . g0 I; l9 X1 P, `1 ^% n
  366.                                     GPIO_Pin_13 |
    4 V: W4 F$ c; x: ?8 u; S% H, w
  367.                                     GPIO_Pin_14 | - q0 Z( e6 N4 d! t3 ]
  368.                                     GPIO_Pin_15;
    8 ~% y" q2 g& L6 p0 p1 F
  369.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    ! W" O- b0 F- ]
  370.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;2 k( I  A) ?' o% K* u# |2 s( j0 \
  371.     GPIO_Init(GPIOB, &GPIO_InitStructure);
    4 T* |6 Z' X# e/ \$ Z

  372. - Q1 J' G/ z' P2 h9 V+ x
  373.     //初始化SPI20 s3 d" o1 l& E! j- ^7 Z
  374.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);+ n" A# y$ d. \0 h6 s; t
  375.     RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE);//复位SPI2
    2 }  p3 @2 f( t1 Z' q5 E1 v* c/ P
  376.     RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE);//停止复位SPI26 u& ~4 E$ j0 z, y& _+ N+ _
  377. / x3 q# K: E9 O; I; l6 o
  378.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;5 s' |4 t1 r9 t; S# C; W
  379.     SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
    ( W1 d  f+ ^% D) J, c
  380.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    / t6 W: o9 \0 L# e) D$ g4 E
  381.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    % U6 }" G9 G' \7 s
  382.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;8 U9 \; d8 x$ g# i, x+ A* D# u
  383.     SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;: B8 H) X# g7 D8 y. Q8 H
  384.     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
    & `2 B4 @' P; U, W. @+ O6 T
  385.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    ; J/ L6 e8 [* J) {
  386.     SPI_InitStructure.SPI_CRCPolynomial = 7;# i4 v. R  f2 @. q% [) P$ s, O4 I
  387.     SPI_Init(SPI2,&SPI_InitStructure);
    ; u1 |. v- ^0 P* X, V3 V- w

  388. 8 V/ b) V* A7 }0 a+ A4 E+ m
  389.     SPI_SSOutputCmd(SPI2, DISABLE);% o2 `) ?7 d; e6 p
  390.     // SPI_Cmd(SPI2, DISABLE);    //Enable SPI2  $ F6 }* n  S& S6 b! K4 y

  391. ( |, i& q3 z6 e  r* b1 r6 e
  392.     SPI_Cmd(SPI2, ENABLE);    //Enable SPI2  
    % f% N3 g) e* z4 K) p1 D' P6 j
  393.     // SPIx_ReadWriteByte(0xff);    //启动传输6 e( R6 S, E( l2 f+ \* j
  394.     // SPI_Cmd(SPI2, DISABLE);    //Enable SPI2
    ( H6 c3 m$ o8 k/ D. n" r
  395.     7 x2 n* k; h6 Y" J
  396.     // SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);+ g. o) O- T- X# w& m+ I
  397.     SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_ERR, ENABLE);
    ) H" c8 ]5 i9 I

  398. ) F% \1 O# ?7 M" i
  399.     NVIC_InitTypeDef NVIC_InitStruct;
    ' [* K! S3 _( S+ D+ n# c6 T3 e
  400.     NVIC_InitStruct.NVIC_IRQChannel = SPI2_IRQn;6 d/ s  Z5 K: f8 `/ |
  401.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;+ \/ Z+ O) @- H) k9 n' m5 f  p
  402.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;' K9 v! Z: o0 l
  403.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;! i) k- g4 {" q' i/ U4 O9 z
  404.     NVIC_Init(&NVIC_InitStruct);
    ' a7 B" Y+ U* A# J* A
  405.    
    3 c- b' W& C: m
  406. }
    4 z7 X- a- A* \4 T$ Q
  407. " e2 b5 y6 \/ R) {
  408. void SPI2_IRQHandler(); s1 A, z2 }; c
  409. {2 q6 C6 I% @8 q& t8 N* a5 [  D
  410.     if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET) {
    4 q2 k5 ?' L& K
  411.         // SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_RXNE);
    8 }2 ?0 P" X& n6 q' @8 `
  412.         LOGI("IRQ:%.2X\r\n", SPI2->DR);
    7 y+ r0 N0 A5 w2 X
  413.     } else if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_ERR) == SET) {
    6 d0 H" X6 A5 M1 S
  414.         LOGE("SPI2 Error\r\n");
    3 w  C, t9 q9 m/ I5 \
  415.         while (1);5 a$ g; |& o* W" ?7 L
  416.     } else {/ ~& N& x4 a/ u, w, S
  417.         LOGE("Unknown SPI2 interrupt\r\n");& _8 q# n7 M7 {1 T- o8 B
  418.         while (1);) p% O/ O3 o: j2 i5 f' l  K
  419.     }
    ( ?: `# b: |  s  g
  420. }
    ; a& E9 ~0 Q' }, B
  421. 9 a  [. l7 F+ B* s7 M
  422. static void SPI2_RX_DMA_Init(void)
    % V+ ^# u5 s0 e. q- x& h
  423. {
    7 N. p% `2 W3 J  y+ [. t4 J
  424.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);( }# [. c6 [. H; Y
  425.     . O7 e0 W5 `9 O; Y7 y' h4 v9 K
  426.     DMA_DeInit(DMA1_Channel4);
      I$ h$ k0 v% A. B1 o
  427.     2 m5 ?+ I/ w- t3 ?
  428.     DMA_InitTypeDef DMA_InitStruct;! W# \" D* F3 M, y; Q  X, I
  429.     DMA_StructInit(&DMA_InitStruct);) Q6 f; i3 z6 m
  430.     DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&SPI2->DR);6 x# E1 V1 x3 W$ I3 v
  431.     DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SPI2_RX_DMA_Buff;8 S1 l4 L! T, D; V( b  ]! u
  432.     DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
    3 A6 W: |( }( J2 I  q* c# R  Z! c
  433.     DMA_InitStruct.DMA_BufferSize = sizeof(SPI2_RX_DMA_Buff);/ O; A- I1 L+ N
  434.     DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    * x% i7 n/ g4 a
  435.     DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;7 V2 ]' i/ t: N1 x7 Z
  436.     DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;4 C9 Y# K( g7 `- K" \, q; N
  437.     DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    : A. L" m* S" N
  438.     DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
    & [6 o3 m5 W/ u2 |
  439.     DMA_InitStruct.DMA_Priority = DMA_Priority_High;
      }0 X4 k0 j7 q9 v
  440.     DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;& h, f5 p7 R/ g$ \
  441.    
    * E1 g( Y, t9 ]8 r
  442.     DMA_Init(DMA1_Channel4,&DMA_InitStruct);
    2 H6 ^. Z& D2 ^% e) V* {3 s
  443.    
    : P" O4 C7 T! w0 {% v/ s: K% B6 ?# I
  444.     DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,DISABLE);! M, z, k& o* `6 }& A3 Q
  445.     DMA_ITConfig(DMA1_Channel4,DMA_IT_HT,DISABLE);
    - D9 O0 n3 s9 v' k- s
  446.     DMA_ITConfig(DMA1_Channel4,DMA_IT_TE,DISABLE);
    : W8 o7 G8 r! u+ K/ F3 R
  447.     - X7 V( q1 A, \
  448.     NVIC_InitTypeDef NVIC_InitStruct;$ U/ |3 b: [1 {' ~
  449.     NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel4_IRQn;
    / d5 z9 k: M1 Y- l0 D% F
  450.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;# b, |+ P# W% B  x) _
  451.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;0 N0 Z! [- t% t+ r4 G% }3 L
  452.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;) h5 k8 q; O. K8 M  }
  453.     NVIC_Init(&NVIC_InitStruct);
    , e6 y9 C) e) E" B$ ~

  454. ' ^, J4 n" z* b7 a2 W4 a% ?
  455.     SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
    ! E" F* q( Z, w5 ]7 v1 R
  456. , f( F9 h* N( h) x' n2 _5 y* u+ j
  457.     DMA_Cmd(DMA1_Channel4,ENABLE);    //????DMA????
    4 [6 j) {; A% u

  458. 8 m$ J" l' m5 F( D" w
  459. 3 j: r) q# I7 T, g, X( x9 E' G0 N9 B
  460. }5 O- T# A/ S2 r5 W# n) X( i

  461. 1 E/ G' k) ~9 o7 D  G3 a
  462. void DMA1_Channel4_IRQHandler()
    ' M( q/ c5 _4 k& W3 t* P; D
  463. {0 L: l+ d5 x1 s7 p+ d2 o3 {( V
  464.     DMA_ClearFlag(DMA1_FLAG_TC4);
    , U5 y9 R4 I7 R$ `# X( k6 R
  465.     int i;2 x/ Q3 ]0 U( {6 K. k. n
  466.     for(i=0;i<sizeof(SPI2_RX_DMA_Buff);i++)
    + Y0 f" J# Y8 F6 p+ B0 w; z# _7 }
  467.         printf("%c",SPI2_RX_DMA_Buff[i]);
    7 R! R; B- X% C: L: g' S7 m6 V
  468.     printf("\r\n");, X0 W8 y4 i/ Y3 f( \9 m3 w1 \
  469. }
    + t9 p! u( x" Q9 f$ O# u- L0 c

  470. * P' A' p' |% s0 P, y9 x) ]4 K2 K0 S
  471. static void read_spi2_input_buff(void)
    & p* N* Y* n  J/ g
  472. {
    6 H- M- _' U9 f+ k
  473.         static int LastDataCount = 0;: ^$ }! O5 k5 ^0 X- C2 y' w
  474.         / s2 B1 p, o0 ]& k; {7 x! n
  475.         int CurrDataCount = SPI2_RX_DMA_BUFF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel4);. n, ]: P1 X$ J5 \, p: e
  476.         $ c% ?: z$ r& [% h: w5 w! x) h
  477.         if(LastDataCount != CurrDataCount)
    0 T$ D  z7 r  I( L- b( D- Z
  478.         {
    9 M. L* b" J4 s, U; i" N. }) K& M
  479.             int i;
    : Q/ {1 S  {' ?( Z& W8 O
  480.             if(LastDataCount < CurrDataCount)* b, D: @. T  F$ ?1 S3 \
  481.             {. E' a$ A6 j& v0 ?0 _0 o! y# M- R
  482. //              printf("1:%d %d,",LastDataCount,CurrDataCount);) `; m' ]6 X, b7 R6 S6 c
  483.                 for(i = LastDataCount;i<CurrDataCount;i++) {; Y0 ?9 Y0 {" W% z3 e( a3 @, Z4 k
  484.                      // printf("%.2X ",SPI2_RX_DMA_Buff[i]);
    & y% U! \; I8 X) ^0 ~
  485.                     SPI2_frame_handler(SPI2_RX_DMA_Buff[i]);
    . i! c. w) R# D3 h8 S$ J7 D& f; v) f
  486.                 }
    % K) b$ B4 u* U* Z3 O
  487. //              printf("\r\n");8 C) h: s7 P4 E5 L0 N- B
  488.             }
    ( r7 N) M# z7 y$ s7 S  b3 M
  489.             else9 m7 \, t, Q1 h$ W& E. o: n5 L" {
  490.             {
    7 o# g! E  _/ ?) `$ h( C
  491. //              printf("2:%d %d,",LastDataCount,CurrDataCount);
    # H% `0 t' N8 W6 _/ t/ Q
  492.                 for(i = LastDataCount;i<SPI2_RX_DMA_BUFF_SIZE;i++) {
    7 }! `; d) A/ e7 ?2 U, u: B0 z
  493.                     // printf("%.2X ",SPI2_RX_DMA_Buff[i]);3 y1 d4 v8 B3 I* o, H
  494.                     SPI2_frame_handler(SPI2_RX_DMA_Buff[i]);
    ! p- a% Q7 _# N2 O
  495.                 }
    $ v9 ?, n; p7 P+ d. q( M6 L6 j( Y, c
  496.                 for(i = 0;i<CurrDataCount;i++) {6 Z! o5 n9 F  Q) ]& i! |8 `
  497.                     // printf("%.2X ",SPI2_RX_DMA_Buff[i]);
    : y# y3 S, b4 s% p5 C* W
  498.                     SPI2_frame_handler(SPI2_RX_DMA_Buff[i]);
    ( l  {* j, B: g, y
  499.                 }
    4 k  H3 h2 t+ v7 L; f
  500. //              printf("\r\n");
    ( k2 B; ?# U7 V+ P% L9 ?
  501.             }- X2 W3 F0 |! \- B, b
  502.         }; K8 c, c& b( _% N8 K& C9 G
  503.   P7 w- e: Z$ F* h- L
  504.         LastDataCount = CurrDataCount;
    7 [' k! z: Q  p+ J& ]" y
  505. }; q: ?' b  r$ K' X9 p

  506. 5 a: b+ z" w0 q( V* P6 H) ?
  507. static void SPI2_frame_handler(uint8_t data)
    ) d$ Y8 g- r4 _% V
  508. {2 M7 N/ n0 q/ B6 a: ]% z) Z' y. q: O
  509.     // printf("%.2X\r\n", data);
    5 ?7 P( L' S2 Z7 Z/ U1 f; |
  510. 7 p% V6 N/ {5 ^
  511.     static int state = 0;8 ]& r; b; b- \& k  K

  512. , q0 @7 B. l& T( }$ Q; `: B5 M
  513.     // 0xA5 5A 55 AA
    / \( l9 j: k. q4 p" n
  514.     if (state == 0) {' |+ w7 Q7 e+ I8 X5 V/ N9 q
  515.         if (data == 0xA5) {  N& Q9 B' H! k9 h$ _* l: A
  516.             state = 1;4 @9 H5 D- ^3 c; a
  517.         }
    * |  A5 V- A% b: @
  518.     } else if (state == 1) {
    ) P0 S  V8 D2 \5 a: s# A+ N; z1 B
  519.         if (data == 0x5A) {
    # d+ H' j) {* m1 h$ q
  520.             state = 2;; M5 a  H& I+ x* f0 u4 Z. Y; z
  521.         } else if (data == 0xA5) {- N3 p( W* E$ J2 q2 D) G4 l
  522. 8 j" \, R- C1 `+ G
  523.         } else {5 W- U( }. Q: [9 z  h1 ?
  524.             LOGE("Frame err\r\n");/ c% \2 U! X% Y; `$ u/ K
  525.             // while (1);
    * u- j% b) s9 S1 i" l% D/ [1 Q7 e
  526.             state = 0;
    : G8 h5 B9 X# K; r
  527.         }& y3 w+ e  T8 X! a3 ~/ W6 ]* M: c
  528.     } else if (state == 2) {. n8 }8 K- N7 c$ j+ b+ y
  529.         if (data == 0x55) {
    $ V' f% a  v6 D
  530.             state = 3;
    ; y" o# }' A% Z1 f3 g
  531.         } else if (data == 0xA5) {" U1 k* p6 \1 c7 I' O1 w
  532.             state = 1;
    / @, o4 Z" [8 m
  533.         } else {% q& S/ U# T/ l. R; ]
  534.             LOGE("Frame err\r\n");
    # c: t, E& G) {/ Z
  535.             // while (1);
    1 n0 k2 }& R0 j: F! r$ H+ n
  536.             state = 0;1 x, B5 e1 a: \8 d7 W
  537.         }% a. n  ~. A9 X3 E; ~
  538.     } else if (state == 3) {
      x0 w$ m1 `$ t5 S  i
  539.         if (data == 0xAA) {& K0 n) K; I% f6 d
  540.             state = 4;
    6 _( }: v2 D# i- G0 h
  541.         } else if (data == 0xA5) {! |7 o, f$ U/ |
  542.             state = 1;3 [) r* k+ L2 \  I* P# Q
  543.         } else {1 S  W; J: p4 u: r/ M# P: @2 M- i6 ]; p
  544.             LOGE("Frame err\r\n");# n$ ^9 [0 k4 g& P! b( a9 D& o& W
  545.             // while (1);5 `1 q! u0 _7 a6 x
  546.             state = 0;) ?1 r4 K1 L% N1 L
  547.         }
    - N% |  `+ v+ L$ F, J! {
  548.     } else if (state == 4) {
    1 I! h( N, V2 \" t* z3 M6 [/ R
  549.         // printf("ADDR:%.2X\r\n", data);
    : U6 R( Z4 ?# M' ]0 z! m
  550.         state = 5;
    / D% H# t, \5 ?# l
  551.     } else if (state == 5) {/ n; `4 j0 P$ ~$ G9 o
  552.         // printf("CMD:%.2X\r\n", data);
    ) F( k. x% E; y/ C
  553.         // SPI2->DR = 0x33;& ~) Y0 s# e  @7 p' J/ H& g2 A
  554. 1 Y( T( h6 E1 t
  555.         DMA_Cmd(DMA1_Channel5, DISABLE);     // disable DMA before starting a new DMA transaction
    2 i6 _* [2 W/ s3 G

  556. , g) w" z* O, l+ D
  557.         DMA_InitTypeDef DMA_InitStruct;
    $ P' ]1 a: S! N# `# M" v
  558.         DMA_StructInit(&DMA_InitStruct);
    ( Q; D2 m' C( @8 |+ u
  559.         DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&SPI2->DR);$ h& n) F  U% ]3 a
  560.         DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SPI2_TX_DMA_Buff;6 w5 p) U5 d+ F1 E/ g
  561.         DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;: V& M6 ~: w( r) P
  562.         DMA_InitStruct.DMA_BufferSize = sizeof(SPI2_TX_DMA_Buff);
    + I  j2 L: w2 M  g
  563.         DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    3 O1 o' d0 S* @9 y9 Y/ I
  564.         DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
    9 E+ [: p% S! f+ c& z' H
  565.         DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    9 ^! W' o3 q( v3 Y0 l
  566.         DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    7 S# z" {, w5 ]* f9 S0 c/ f9 D
  567.         DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
    , T# q1 j: W. Z7 n5 _0 f
  568.         DMA_InitStruct.DMA_Priority = DMA_Priority_High;+ f; s/ E) Q; A6 ^
  569.         DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
    0 s5 Y5 \; g) f1 u. z
  570.         
    % l8 ~. X6 X# u4 Z( ^- n# f
  571.         DMA_Init(DMA1_Channel5,&DMA_InitStruct);
    1 K% A/ P1 ~) g6 w" e

  572. " u9 I9 [2 A& N
  573.         DMA_Cmd(DMA1_Channel5, ENABLE);- P: z& A; @. M! I" g" u0 e/ x
  574.         
    3 x# V0 A1 U! V7 ?1 `
  575.         state = 0;) v6 O# i+ K( j% l4 Q
  576.     } else if (state == 6) {
    4 C* v: [! u6 F# Y7 ~) H

  577. 7 R9 J, m+ m  [" P# Z- u3 j
  578.     } else if (state == 7) {
    * k; ^! j6 m- _( Y9 l

  579. 5 I6 U5 ~# ?1 G, M" A- R
  580.     } else if (state == 8) {7 S0 D/ W# N# E6 q% l1 H, e

  581. 2 P/ L4 d$ i# O% X
  582.     } else if (state == 9) {
    1 g; g# B2 Z  T8 G% S
  583. ; g) l* O) v1 a
  584.     } else if (state == 10) {
    # y" u/ v6 v% B1 J

  585. - C9 V2 j0 S# w% Y. x& }2 {# y
  586.     } else {
    - p; s8 `+ D; J6 D
  587. ; I+ g) w1 p; a, i+ `
  588.     }
    4 U  P1 J& z0 y" W9 D9 }8 n+ d- d
  589. }/ Q8 ?" t) E% `* A3 k4 z

  590. 2 I! p# j  p1 r; N
  591. static void SPI2_TX_DMA_Init(void)
    0 V: _! o) ]; b
  592. {
    $ a; X8 }" [% ?$ W" E5 q
  593.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
    ) R7 D# w3 D( x/ f* l/ U. ~- b
  594.    
    8 B4 L! Z2 u% |; K  d4 B- A
  595.     DMA_DeInit(DMA1_Channel5);" A, k* y  r' b( \$ R! r' \
  596.    
    , {+ F) Y# E4 w+ o0 q  V
  597.     DMA_InitTypeDef DMA_InitStruct;
    3 j" }- e! j- A$ E+ W3 H
  598.     DMA_StructInit(&DMA_InitStruct);6 @1 g. M. U5 m8 J
  599.     DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&SPI2->DR);$ C! E8 Q8 t& F- i6 B
  600.     DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SPI2_TX_DMA_Buff;
    ( @* S: l- L" N* ]4 N: S4 ^2 B
  601.     DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;/ Q7 t! J) W2 C
  602.     DMA_InitStruct.DMA_BufferSize = sizeof(SPI2_TX_DMA_Buff);
    0 K4 j" C' B: V( _7 H; `
  603.     DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    # d6 P& s+ \, X5 e- a/ m# s
  604.     DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
    6 n4 w7 s: G2 i% H8 x
  605.     DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;0 C3 J# N! v* r
  606.     DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    % i1 w. R, N* P2 o
  607.     DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;3 c2 G: d% H- ^
  608.     DMA_InitStruct.DMA_Priority = DMA_Priority_High;  v) D$ p/ y0 C) U! R8 S/ S
  609.     DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
    6 K; z/ p% s# ?# ~  }- ^
  610.     ' b; s, u+ _1 N! M
  611.     DMA_Init(DMA1_Channel5,&DMA_InitStruct);
    5 j$ `) B8 \& ~- Z+ n- T" k: K  e# D
  612.     ( C( \, j/ k5 ~( H1 v! y0 C
  613.     DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,DISABLE);" u2 T+ e1 q. s0 V
  614.     DMA_ITConfig(DMA1_Channel5,DMA_IT_HT,DISABLE);& \  e6 h$ T# L3 ]& F
  615.     DMA_ITConfig(DMA1_Channel5,DMA_IT_TE,DISABLE);
    ( [( E4 @9 F# M2 z; B1 v% @
  616.     & s$ g7 V- l# x2 w
  617.     NVIC_InitTypeDef NVIC_InitStruct;, U9 O5 D- u0 u2 h/ x
  618.     NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel5_IRQn;3 A3 }( c) t- ]% `$ h4 B6 \( |
  619.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    4 X! h5 k8 g% o6 @; T
  620.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;; @" [6 u# K( b3 f2 V
  621.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    8 J% i, ^+ g4 X( F7 f& `
  622.     NVIC_Init(&NVIC_InitStruct);
    * [/ S6 z5 \7 T- a9 I

  623. # y' c/ e" O. G& u$ L
  624.     SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);( W% J" i. o+ c4 I/ A* d2 V( N
  625. 4 J1 Y3 J$ {4 Z) d+ J; e1 ]
  626.     DMA_Cmd(DMA1_Channel5,DISABLE);    //????DMA????+ z. k/ P3 y1 g- s
  627. : P' i8 a' y( {+ Z
  628.     // note: the last byte moved from RAM to SPI2->DR is seen as the end of DMA transmission
    / j. u; d/ R* L8 @% M+ _
  629.     // but the last byte still not been transferred out from the shift register which means
    8 _( J$ N9 }2 c4 E! `
  630.     // the SPI master might not received the last byte when the SPI slave finishing the DMA
    + X" a6 ]' F5 b& s& T, R
  631.     // transfer.
    1 c/ |8 @5 s+ ?. r1 \, z5 |2 P
  632. }
    + H' Z& t8 d7 _  q

  633. ! `  u9 e1 }- x7 h) E6 u
  634. void DMA1_Channel5_IRQHandler()
    - |" t" P0 T$ p- }6 M2 g# ?* J
  635. {
    - Q. X* ~) T1 ^& H  a( v+ D
  636.     DMA_ClearFlag(DMA1_FLAG_TC5);" g) q% i: O$ ~& y! M3 ?. `8 M
  637.     // LOGI("Sent done\r\n");; |3 n! }. T8 e
  638.     DMA_Cmd(DMA1_Channel5, DISABLE);  S9 R0 S; J+ M! J
  639.     // SPI2->DR = 0x00;     // set the shift register to 0x00 other wise SPI master will get the previous byte/ F/ {9 R( ^: L
  640.     //                      // which might be 0xA0 and causes error.9 l; \! D5 h- M/ T
  641.                             // update: don't do it like this, due to the last byte still in the shift register
    3 A2 ~: ?# Q' s& k* P  ^/ N  ~9 W
  642.                             // and waiting to be shifted out from MISO. We can add a byte of 0x00 int the last
    5 S$ U' e$ U% z8 t( d
  643.                             // byte of the DMA buffer if the last byte is 0xA0.
    2 l# E* F; L8 D" ]" r
  644. }
    - K& F/ Z6 R. v( t1 n
复制代码
转载自:哐哐哐 Quan, L# [9 T9 i! y$ K. o' R; K
如有侵权请联系删除
* [% \; F. {% [
1 \0 E, \  x$ \7 U' c2 ~
赞 收藏 评论0 发布时间:2023-3-30 16:47

举报

0个回答

所属标签

相似技术帖

官网相关资源

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