- #include <stm32f10x_conf.h>/ v- l( E. G! q. Z3 v) V/ [; v* f* q
- #include "sys.h"! c7 r4 u1 m' j' R' _* z4 r( l
- #include "delay.h"
' i/ z- ?5 \2 ^. Z2 J1 A0 ~ - #include "usart.h"0 u6 e! }7 G( k
- #include "timer.h"
9 o( m( i [$ @( t$ m+ i - #include "pwm.h"
! d( x$ _ h1 G5 K) D% ?$ l - #include "gpio.h"# B6 ^- ?$ ~$ q6 d5 f4 { V
- #include "fsmc.h"
& p* X- J X f- r* ] - #include "iic.h"# h6 C3 {9 J3 l: {; d; H
- #include "FreeRTOS.h"% u* \( x+ x. T& l! v# c5 k
- #include "task.h"
5 J: F% G' E6 \3 {4 M - #include "semphr.h"
. [3 \0 t7 r+ E9 N3 x8 R# H - #include "task_com.h"
8 l/ F& D4 P% W* V g. W( d - #include "task_motor.h"
- P% ]1 h8 Z' W0 {7 S/ @1 E - #include "task_led.h"0 E+ K. V) t/ S0 }
- #include "at_cmd.h"3 t) d& L. T6 i2 p7 I, q
- #include "string.h"2 l0 z4 w. A( L" `( D
- #include "lib5500.h"
! K- G/ w$ ]6 D+ ? - #include "flash.h"6 s+ G* F7 \3 x! ^: ?! b3 }/ {
- #include "iwdg.h"- u- f& y; B) f R+ Z
- #include "ws2812_sim.h"
( F3 R% C" O# { - ( W2 f) \& ` u7 z# F& J0 S
- #define LOG_LEVEL_DISABLE -1, f9 D4 K. W" a5 Y* ^
- #define LOG_LEVEL_ERROR 06 V+ }! t$ i3 i6 f+ v
- #define LOG_LEVEL_WARNING 1
r/ a1 m" [( V - #define LOG_LEVEL_INFO 2
, c7 }; O# A3 H - #define LOG_LEVEL_DEBUG 3
4 }+ Z! N: U) u7 L$ S o: ? -
( ?9 [* C2 }9 U. c( J9 f4 _ - #define LOG_LEVEL LOG_LEVEL_DEBUG
& u9 u* C: h- y e( ?' h - ! u* q* Q: a+ Y" j4 ^9 G
- #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_DEBUG)
& }' k0 R; x- d' J: W4 u - #define LOGD(fmt, ...) printf("[D %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__) //%16s,%3d0 `; m7 T; i. p" Z3 d1 |
- #else- W9 f: x5 w% p9 f& y, i- }
- #define LOGD(fmt, ...) |- X% j- d N! `, O
- #endif$ v# ^4 c1 r: ^+ U1 s
-
4 f8 g( t0 q. \6 X) k$ D' f+ [# s - #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_INFO)
9 ?0 O/ B+ {1 F8 w8 v- `) I - #define LOGI(fmt, ...) printf("[I %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
' t; k( M" `6 o6 H - #else
9 s/ H5 V" h9 w1 P - #define LOGI(fmt, ...)
+ ]0 ^5 l8 w% X* {6 q$ u - #endif3 S- ` m" z* F# c8 E. Y
- ( F7 x; a% S8 o# ~# s- F' [
- #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_WARNING)0 y0 w! c# [ d% g, x- e
- #define LOGW(fmt, ...) printf( "[W %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)$ T4 t+ Q1 X( v$ `& T, W# x- Z
- #else
' j0 k$ X6 y8 R& f* A - #define LOGW(fmt, ...)
% m" U5 I, a% o: u) F% U - #endif
" Y, q5 c7 G; r; X -
) B. p" g9 u+ s2 R, `0 I0 e9 ` - #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_ERROR)
* M( Z3 K+ `: y, ?5 Z0 i - #define LOGE(fmt, ...) printf( "[E %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)+ }4 C' y7 n) J7 U3 p4 p
- #else
" `& W6 _$ r" `+ I+ P - #define LOGE(fmt, ...)! R, F: D d- W; [7 [5 N
- #endif- E) }3 O$ }+ }. b2 y% S
- # q2 l6 a7 `7 _/ q! x, K/ x
- #define MARK LOGD("\n");# m' j# a$ j) D" h3 A( ?
- 4 N2 [ f4 D4 i/ `- | v9 `
- SemaphoreHandle_t SensorReadLock;
1 Y, E8 l N1 o& ^7 | - SemaphoreHandle_t UdpSendLock;
! e/ Q' ?+ Q8 R) I ^ - SemaphoreHandle_t slave_ready_lock;" @0 _# s; |# \0 Y' q- I0 Q- Y2 [, |" z
-
( _/ ^1 t& _$ D- s -
& e! \: P$ B* |3 l8 [7 e) | - void TaskMaster( void *pvParameters );
+ m& W& H- }# ~7 l# U! }, e+ k - void TaskSlave( void *pvParameters );) P3 S, ?# j, ]. \
- static void SPI1_init(void);, P. j( }" q4 e' `1 X: I( g
- static void SPI2_init(void);; n5 E; u/ _3 F
-
6 P l& i3 \- V1 r - void Initialization()8 B( j# v$ ]& j
- {( |9 S# W! i% m6 P
- MCU_INIT(RCC_PLLMul_9); //?????: 12MHz * 6 = 72MHz
" Y# U6 B% Q5 P8 C+ s( } - Delay_Init(72);# n" h; l5 ?$ h% S
- Uart_Init(USART1,115200,72); //???????,PCLK1??????PCLK??RCC_HCLK_Div1??? j+ U) N+ l7 s" }" X
- Uart_Init(USART2,9600,36); //?????????,PCLK2??????PCLK??RCC_HCLK_Div1???2 m2 ^; h3 |5 e+ f( Y' B
- // Uart_Init(USART3,115200,36); //???????????
2 E' _) S7 ]9 E9 X - printf("\r\n\r\n****************Build:%s %s****************\r\n",__DATE__,__TIME__);8 ~' W* D( @9 e2 V2 D
- // UartSendStr(USART2,"System start\r\n");: b% \. N% C6 a6 ~8 J$ g
- }+ G1 m( Z0 h# h/ r5 p: p
-
4 Y6 [3 z! S& O' Y - int main(void). F3 q1 m; q+ b9 L
- {
% J0 s8 A: y% x/ U, m; m - Initialization();
& S+ h8 I" v% W+ m2 B2 ?: ?3 o -
. h. _! h3 H& J7 w! B - // QStepMotorCommand = xQueueCreate( 1,sizeof(int) );
" ^! v! p' Z6 b# o - // QHostCommand = xQueueCreate( 16,sizeof(int) );
2 I$ m) W! n5 m% w9 y& F7 _) f -
0 B# F2 u) z [, Y% Z, d - SensorReadLock = xSemaphoreCreateMutex();7 |* O3 M/ ?$ K) F
- xSemaphoreGive(SensorReadLock); q% x$ c* J: T4 I4 j
-
- H8 r) {& Q2 v. q6 o$ S, z# I# } - UdpSendLock = xSemaphoreCreateMutex();0 ?5 F+ {! P' C, X
- xSemaphoreGive(UdpSendLock);
( {! _6 B& U7 ?5 b) T -
: j9 q8 Q& u5 I7 Z( d- d" v - slave_ready_lock = xSemaphoreCreateMutex();7 W1 k3 Q4 i' H+ y6 u
- xSemaphoreTake(slave_ready_lock, 0 * portTICK_PERIOD_MS);$ L! r+ m! p! Q& `* Q
-
- n+ z7 Z; a: B p5 @' F: g - /* Start the tasks defined within this file/specific to this demo. */6 a! ~0 a+ u' ?/ U2 [2 T% V* Y
- // xTaskCreate( TaskComm, "task2", 2 * 256, NULL, 2, NULL );" O4 t) ]; @) x# |* ?0 m0 o/ p% h. C
- xTaskCreate( TaskMaster, "task1", 4 * 256, NULL, 3, NULL );
* g* u3 M9 J' M: B/ m - xTaskCreate( TaskSlave, "task2", 4 * 256, NULL, 1, NULL );8 `, ^* A6 D ]; Z
- " |# K' p9 \( h* W
- /* Start the scheduler. */: J# F( X( r5 s* s
- vTaskStartScheduler();
3 w6 Y/ t" P! x @- A \8 } - 2 z# d" `1 M! m: P. h
- while(1);! ]+ ?4 Q1 J2 d9 j1 H3 ~
-
7 N( X. D# N8 b9 t9 f - }' G; s" |/ \$ b. O2 ~. {4 h5 D
-
% s* `5 m5 w0 Z. a. `! K$ G% ]4 g - 1 \! S6 z2 b5 k3 g
- #define USART1_RX_DMA_BUFF_SIZE 128
1 T/ T2 X/ i5 k+ B- k - #define SPI2_RX_DMA_BUFF_SIZE 32
5 u9 N; M9 P% S4 }* {# G* L" @9 Z: [ - #define SPI2_TX_DMA_BUFF_SIZE 32# V& Q& Z# B/ `% V# z! Q) W" c% J) A
-
1 y0 j- X) _4 m s: I$ Z ?/ _ -
/ E1 a3 {3 F% R1 i8 e3 D/ u9 [ - static void read_uart_input_buff(void);6 ^ P, b& x2 `/ c
- static void USART1_RX_DMA_Init(void);0 N2 S. W9 W; H$ I8 ^
- static void SPI2_RX_DMA_Init(void); L+ W( P R5 U* r6 a. t# J
- static void SPI2_TX_DMA_Init(void);" G7 F O5 d) D9 V% q
- static void read_spi2_input_buff(void);0 x+ m' E% Q3 u* b0 v
- static void SPI2_frame_handler(uint8_t data);
7 v& L! F' g8 C' |3 s- K+ _ - $ P2 s1 }. i4 P4 D( E
- static u32 s_frame_buff[1024];8 D$ u/ S" R6 s6 w
- static u8 USART1_RX_DMA_Buff[USART1_RX_DMA_BUFF_SIZE]; //????????????��????????????????????��????????????* A& V) P" w0 _7 g7 w
- static u8 SPI2_RX_DMA_Buff[SPI2_RX_DMA_BUFF_SIZE];, b$ ]+ o" q: }. l& J, {) L/ ?. n
- 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
-
: V8 {$ t8 t7 x2 Y) G - void TaskMaster( void *pvParameters )7 S3 \" q+ D4 A6 J
- {7 ]: R$ X% T$ d S& m
- LOGI("TaskMaster start...\r\n");
8 _6 e7 y6 l7 o9 Y5 p. I, j* k5 Q/ j+ ? - 3 n7 t4 J& Z" C
- // USART1_RX_DMA_Init();; c, p0 R3 |( ^% ~4 c x& f& v
- // ws2812_sim_init();
% B! h1 ^4 N) M6 ]5 @4 p! ~ - SPI1_init();
u) i' n. {* U) S3 ?% g' g -
$ v1 B$ f7 J ^/ `) P1 n/ f2 s - GPIO_InitTypeDef GPIO_InitStructure; //定义一个GPIO结构体变量3 a! I0 b0 g, ?8 ^
- SPI_InitTypeDef SPI_InitStructure;0 h$ B/ F8 z3 k- I: j* f
- 0 ?7 X, c1 ^' g- d! A( Q
- //初始化片选引脚 o! ?! x4 l( K9 O
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); {. L( O" C& Q W+ z: U
-
4 S& H8 ~# |0 U4 Y+ o! v3 g4 v - //chip select/reset pin init& V4 V' ~& I0 {# J6 h3 J9 l
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
" o0 m, K: Y' d! f9 u0 I - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;4 S5 L2 N9 G/ z. N. d
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;5 p" {2 P# S: x/ w( @" M
- GPIO_Init(GPIOC, &GPIO_InitStructure);7 ~: X+ T3 Z. B3 u' R* e0 r. i
-
% F! m8 F, r0 |' m# i - @: f4 A8 _+ N* `4 t: l1 I
- xSemaphoreTake(slave_ready_lock, portMAX_DELAY);
# K2 X. z$ y0 M& k$ W -
7 i$ V; C8 p5 k - uint8_t count = 0;
* v2 M4 D9 H1 s* P - ) x$ }' U- J$ [' M% T0 A# k
- while(1)+ }+ o. N5 W% q6 [* z
- {/ _ z& p7 F/ h _( w9 K7 K
- // LOGI("test %d %d\r\n", count, DMA_GetCurrDataCounter(DMA1_Channel4));
7 N# R+ k8 D' H1 k; x! T# x( g - ! F3 x) r8 _- Y
- // read_uart_input_buff();4 ?: c, ~ [: Z2 U. q T3 O
- ! ^& W( |2 ~- B! f
- static int state = 0;. E+ Z0 y, c' G) D
- if (state == 0) {8 w/ z5 R, p3 j* U* v
- SPIx_ReadWriteByte(0xA5); // preamble
1 h3 }" b5 m: _0 U - SPIx_ReadWriteByte(0x5A);
; [8 b8 m. t9 p# u/ M. S6 u - SPIx_ReadWriteByte(0x55);& n# h2 i, d" P0 J- K
- SPIx_ReadWriteByte(0xAA);
5 r+ \) U* Z9 w9 P- d) k0 `; d% ]1 h - SPIx_ReadWriteByte(count); // addr
( Q" j/ X- ^/ }% c - SPIx_ReadWriteByte(~count); // cmd
+ r2 u, Z+ K+ V; I, Q* v -
$ F& O! k& \7 \8 @: w- U0 X - // count++;
1 ^! N8 E7 f$ @( F7 v - 7 C- V3 h# K1 \# R
- state = 1;
; M# C9 d+ g. r2 e* P - } else if (state == 1) {
' |+ K5 n; A0 R0 f - static int timeout = 0;) f: n+ \% G3 D1 P$ ~+ }- K" z
- uint8_t data = SPIx_ReadWriteByte(0xFF); // dummy byte6 ? {% |* _" a+ M: G. Z
- // LOGI("%.2X\r\n", data);* e+ n: n; ^9 H9 l
- if (data == 0xA0) {
( |4 {. c) f$ q0 m, g+ u - for (int i = 0; i < 32; i++) {
! T* l$ F& N4 L0 D - uint8_t data = SPIx_ReadWriteByte(0xFF);
) q# G6 M: {9 J8 Q. h/ o/ Z - // printf("%.2X ", data);
, X7 {% {9 |, t2 S2 k3 I* W6 \ - }
5 i8 E6 \- `$ \/ m# F) c - // printf("\r\n");4 v/ }& w! [4 L# L7 Z
- timeout = 0;
# m( ^$ o2 |4 i" R2 a$ d - state = 0;& C- }: X' f* T$ E, m% V W
- PCout(13) = ~PCout(13);3 O% x- c, {" M4 o0 \
- // vTaskDelay(100);
. C, W& o0 w$ Q& ?4 {: k" h - } else {
9 I4 H( j- }7 _ - if (timeout++ == 10) {
& K1 D! Z# d- `9 D3 |4 }. e - timeout = 0;: ^; M) O( B$ Y6 V# Q& ` Q5 i- S
- printf("timeout\r\n");
; \- A* e$ y. e; n$ a/ V. v6 L - vTaskDelay(1000);
( q$ v3 b, `0 w s& ?; K - state = 0;- N7 j) d/ ] b. @
- }5 \. B: K( |& W3 _; F
- }6 s- j9 U1 Z; `) P" {+ [
- } else if (state == 2) {
. X' B& W* Q$ U1 D0 ~! D; u - }
; W+ Z- @$ N) }! ]( e1 ^ -
7 r# G) y, ^! h2 }0 J8 q; c' W- z; _ - vTaskDelay(1);( y& [3 t' Z2 F! }; {2 O3 e3 D; r
- }9 M. Q% m8 r8 K8 Q
- }
$ z8 G; H- C g -
/ g3 ?" S, E* N+ i( m- ] N - void TaskSlave( void *pvParameters )! s: @& S# g- f; n1 R- r: U
- {+ v- h$ k5 p/ `( E3 M$ h
- LOGI("TaskSlave start...\r\n");: w1 D$ c! v3 A" F( q! V1 A! Q8 f
- " t: G: n" Z& S6 B
- SPI2_init();1 G6 K! q7 c9 |- ]' m
- SPI2_RX_DMA_Init();7 Q5 ?/ K @7 Q& Z+ O
- SPI2_TX_DMA_Init();
8 j) O2 D( e# J [; c+ e - 5 U* K' ^) C# \$ [+ u6 {. o
- xSemaphoreGive(slave_ready_lock);2 `9 n2 ~: n! [' y8 M/ f
-
7 }6 y! ~! n9 \ - uint8_t count = 0;
% s5 M% n! S% ~* z; [2 L* z# A9 H# P -
# }; X) ]5 y. A0 k- R - while(1)
3 s5 }4 X7 o3 P' r7 h - {
( _1 `! X+ i" n' J - read_spi2_input_buff();% J% E, R8 Y3 n
- ' K ]: m5 z- j' t$ g3 @
- vTaskDelay(1);: g# b" f, F2 J" u; n
- }
) F! V) ` F% l4 _) c# G2 L( a - }! Y# P# }4 Q: s. h: t- u
-
; U, T5 ^ p. R2 ^2 I* | - static void USART1_RX_DMA_Init(void)" Y9 `+ E- q+ i
- {
$ A3 m6 T9 R" h8 h1 B - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
! I' D: n& F" p -
1 }0 I: M8 D2 u3 v/ n# Q' _ - DMA_DeInit(DMA1_Channel5);; i# g+ G2 o. l1 m; x
-
6 R g9 }( b, V$ q% t - DMA_InitTypeDef DMA_InitStruct;
; `' a- i* w" {' t7 z, F) ? - DMA_StructInit(&DMA_InitStruct);
6 Q4 A5 Y: l* N! o2 [2 R - DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
, W9 c% s4 h# `4 z - DMA_InitStruct.DMA_MemoryBaseAddr = (u32)USART1_RX_DMA_Buff;
! W U7 E1 t8 a; s" Z8 O8 y - DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;* F9 H* t- ` |% d2 W4 o. R
- DMA_InitStruct.DMA_BufferSize = sizeof(USART1_RX_DMA_Buff);
& X+ O8 h* l5 U) _ m - DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;, \. |7 `7 W/ k$ s; s7 U
- DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;2 X: |; w) J L# y Q, o, s
- DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
2 h" ?4 w4 M+ f% O* I7 t - DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;/ b2 U; x4 h- Y$ v: W" C! H7 B
- DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;3 o5 O" c+ y" L. ]9 C$ R3 L
- DMA_InitStruct.DMA_Priority = DMA_Priority_High; x# s/ F) q2 K+ s3 ~
- DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; J9 p/ \5 @+ Y4 _
- ; x8 T: [) j, S2 X
- DMA_Init(DMA1_Channel5,&DMA_InitStruct);
4 n: y9 i* g2 W& i2 S- \% O) d -
& R# J1 c# l7 L - DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,DISABLE);! ]* m% p* R$ v
- DMA_ITConfig(DMA1_Channel5,DMA_IT_HT,DISABLE);
7 D7 U3 q: n' q% t( t - DMA_ITConfig(DMA1_Channel5,DMA_IT_TE,DISABLE);# ?: ^( H$ j' O4 N' R5 b
- 6 C1 f% }) W1 `7 s
- NVIC_InitTypeDef NVIC_InitStruct;
0 k- Z! y2 y. N - NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel5_IRQn;; [5 \! D- l7 Q% i( Q9 L" ?
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
* T0 u( c& a! a6 ^$ a [9 @' k - NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;* `& [& t) E$ u1 p- |5 h
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
+ ?- H. t5 @! Y" \ - NVIC_Init(&NVIC_InitStruct);5 O7 _! e) g8 N# W
-
x7 d) ]6 |/ O& G) J - USART1->CR3 |= (1<<6); //???DMA????6 B4 }; @, I$ _% V# C" k. _1 c
- DMA_Cmd(DMA1_Channel5,ENABLE); //????DMA????
3 ^) P0 T* A0 o+ k1 o - / t, I) C5 A9 V. z
- }
_: c; d, M5 f8 t A/ M: B6 k; e- f -
+ b( q. _; @# `8 ]$ R6 l, b - // void DMA1_Channel5_IRQHandler()( P/ a. c0 Z8 ?' c
- // {
& m# u, r$ L9 |( n - // DMA_ClearFlag(DMA1_FLAG_TC5);, v8 X& ?+ H: h) D. J
- // int i;
7 S+ R; K5 I! B' q) R. ~ - // for(i=0;i<sizeof(USART1_RX_DMA_Buff);i++)0 e" u; K7 k/ r
- // printf("%c",USART1_RX_DMA_Buff[i]);+ E- P- z. c( p! w& S, u
- // printf("\r\n");
( L8 V2 K2 R& J - // }& y0 f+ r5 \5 \4 s- I- V) o
- : o* M5 Q M! e9 o# V2 M k. ]
- static void read_uart_input_buff(void), _. g- |4 }' i6 W' V, i
- {: [, E% }7 D( \5 s z( z; L2 u m
- static int LastDataCount = 0;) Q# u0 v4 |. H" Y8 {2 w' A% X
- 5 h4 a5 L" T' G7 K
- //????????DMA��???.????DMA_GetCurrDataCounter?????????????????????.: C) n# A7 c, h. \
- int CurrDataCount = USART1_RX_DMA_BUFF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5);6 p1 f" X+ T' g
-
|; `! {: |1 k$ f+ g' i, x1 u( c1 S - if(LastDataCount != CurrDataCount)
5 o( [9 n6 T# p0 n y - {; I% L+ k4 H' a, [* q g' u
- int i;( J: s" U% K$ Y8 W/ Q
- if(LastDataCount < CurrDataCount)0 A0 R# K% W- e# l, S
- {1 p7 |" S5 A3 t+ m7 _
- // printf("1:%d %d,",LastDataCount,CurrDataCount);: N0 J. {% V1 u
- for(i = LastDataCount;i<CurrDataCount;i++) { f F: v H8 s& W) ]0 h) P
- // MasterFrameInput(USART2_RX_DMA_Buff[i]);
# S* v3 I1 f0 B6 S' l! u - printf("%c",USART1_RX_DMA_Buff[i]);
+ Q! M) m% Z& C3 m; o0 w' s6 L - }
, ~. L% J" X4 c) X - // printf("\r\n");
* Z+ @ ~9 [* a$ w& D; a% q - }' d) t, K4 X- G; H# J7 E% z
- else
* g4 z" e) f* q3 Q( O - {
; B i: e6 v. B# s% l* Y; e - // printf("2:%d %d,",LastDataCount,CurrDataCount);
8 M/ J5 D) [- i7 h8 g; D$ }. d - for(i = LastDataCount;i<USART1_RX_DMA_BUFF_SIZE;i++) {- q6 d8 d6 M$ i I
- // MasterFrameInput(USART2_RX_DMA_Buff[i]);
( n* e9 k9 k7 S; D( q - printf("%c",USART1_RX_DMA_Buff[i]);
: h/ m8 \' ^) Z# \( |8 R4 ^, f - }9 v( ?" W) D+ \3 _, g
- for(i = 0;i<CurrDataCount;i++) {
: ^0 C! \5 B' u8 P, Z - // MasterFrameInput(USART2_RX_DMA_Buff[i]);8 O9 r* ]% k$ n; x2 \
- printf("%c",USART1_RX_DMA_Buff[i]);
# S5 _2 {* T& ]2 r j2 m7 f( k - }* t5 w) K# b& k6 F/ N2 F' j1 M, e5 O
- // printf("\r\n");& l9 N) b: l" h6 b
- }
3 E7 c. w3 M) m; b - }
( T' G9 I; x" G# [ -
& H2 d/ J4 C8 x. k$ Z: o; P' z - LastDataCount = CurrDataCount;
3 d' P8 D! ~( _# w" W+ ^! A - }
; m* E) j5 p: Y - : ~9 i7 z+ ~. x+ F% Z* I1 M! L
- static void SPI1_init(void), |' [( q* |( k( x" C' [- \/ L( U9 C
- {
2 `: M6 B( D. z0 U1 Y - GPIO_InitTypeDef GPIO_InitStructure; //定义一个GPIO结构体变量9 a! k5 B ^; d4 S) t6 f# i* h
- SPI_InitTypeDef SPI_InitStructure;8 N5 l5 D5 i' C2 |7 j) j0 m3 {8 L
-
' h. {% e2 n( R3 Z( ?5 m# \7 \+ t - //初始化片选引脚3 D6 a+ W. X/ M5 D Z0 ^: i
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- d+ T9 \/ T4 E) G' v; u( l9 x -
& Q% J8 Z' Z% v( u% v$ Z/ q - //chip select/reset pin init
! ]% F% N: F! Z - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11;
7 u9 L2 [+ m s3 R' u: n - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
" Y1 m. ~2 ~* d5 b& i - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/ `" O8 N+ q, m- {& Q0 P* ? - GPIO_Init(GPIOA, &GPIO_InitStructure);0 Z$ S3 k1 y2 d4 X
- // SPI_CS = 1;* ~8 F* @( C: N
- // SPI_RST = 1;
! F: D/ [6 s$ `( p! M -
1 s( x' b+ u- u# @; | - //初始化SPI引脚( V5 f$ y2 w3 a, s( n6 R
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;# K' M1 a4 }% }- H4 a- }7 \+ ^
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;0 y! m# E c7 D
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;( g! l6 R3 L& F8 M! i! N
- GPIO_Init(GPIOA, &GPIO_InitStructure);* u6 P# Q* \3 [- D
- 4 X! s: D9 H$ {
- //初始化SPI10 d% f D, K- c! v) V% w
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);) z: [) S$ _4 z9 w0 z
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//复位SPI1
8 l& `& @0 H: G# n {, e" d - RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);//停止复位SPI1+ g9 t: y: b3 y2 w6 ]: U9 X) m4 d
- 7 H3 U( x) Q3 i% V& j5 u1 _8 p
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;( |* g" H. F( v5 @2 w; B* r* a
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
, u) R# S7 l4 {# }; ~ - SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
4 w' }8 H& V' o+ W7 p+ r+ ]) a6 ] - SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;, _) H7 T# a- Z# ?( p
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
0 A q# ~ A' l - SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
) m0 A' ^8 }$ A( P3 r* l; Y - SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;5 T& _4 H& U% u4 U, ]: U
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
# ?/ h, B S0 T3 k - SPI_InitStructure.SPI_CRCPolynomial = 7;; {1 `- u2 w0 J+ F
- SPI_Init(SPI1,&SPI_InitStructure);9 d, [( l- A" u1 V; G
- / G9 d3 h2 l. o% b7 |
- SPI_SSOutputCmd(SPI1, ENABLE);
& S" E0 A$ s3 F - // SPI_Cmd(SPI1, DISABLE); //Enable SPI1
6 c4 A( @( i" v# F9 V5 g. m - G! P) d6 {5 R2 m- Z" Q
- SPI_Cmd(SPI1, ENABLE); //Enable SPI1
& O* u' r9 _+ e - // SPIx_ReadWriteByte(0xff); //启动传输: X) r1 D) G2 w" ?- c. E5 S; N4 S
- // SPI_Cmd(SPI1, DISABLE); //Enable SPI1 % {9 D8 K1 h( a, [) @
- }* m# }1 z4 u* f1 A3 |0 z
- 5 P; A; D* Q7 {) r' y, e
- static void SPI2_init(void)4 t1 c0 r: `6 h- k$ d Z
- {9 ~& r5 [, f4 ^% ]% y% N
- GPIO_InitTypeDef GPIO_InitStructure; //定义一个GPIO结构体变量9 d% s4 s% d2 ~$ |7 v3 I; A% p
- SPI_InitTypeDef SPI_InitStructure;
9 }, R# t4 y* j, C0 ] -
$ p: F4 _% R2 k, L, m6 t! P* B& ]9 P - //初始化片选引脚5 l Z5 g! h- b' M
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); * ^. z( c$ F5 v: j. D [
-
( _! i3 s0 c9 ]6 V1 \5 S/ r - //chip select/reset pin init% a& n. b' K1 V) ^
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
& c* s! J* E$ ?& q6 `0 b - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;3 o6 Q) P/ w6 O2 ? e: G L
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
5 I) _* x& s0 `" c - GPIO_Init(GPIOB, &GPIO_InitStructure);
4 @9 \, I1 S Z# ?) ~- k. D7 g - // SPI_CS = 1;
3 c! G. X6 j3 C. k. x - // SPI_RST = 1;. P1 k0 h+ S4 t k
- ) k' M1 w' X' D1 x8 w
- //初始化SPI引脚
) s! _, ~! r+ }$ S, ? - 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 {
- // device, if it is pulled up, SPI slave will not shift
: o4 C1 i+ Q: D% V; X - // any byte to its shift register
. g0 I; l9 X1 P, `1 ^% n - GPIO_Pin_13 |
4 V: W4 F$ c; x: ?8 u; S% H, w - GPIO_Pin_14 | - q0 Z( e6 N4 d! t3 ]
- GPIO_Pin_15;
8 ~% y" q2 g& L6 p0 p1 F - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
! W" O- b0 F- ] - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;2 k( I A) ?' o% K* u# |2 s( j0 \
- GPIO_Init(GPIOB, &GPIO_InitStructure);
4 T* |6 Z' X# e/ \$ Z -
- Q1 J' G/ z' P2 h9 V+ x - //初始化SPI20 s3 d" o1 l& E! j- ^7 Z
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);+ n" A# y$ d. \0 h6 s; t
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE);//复位SPI2
2 } p3 @2 f( t1 Z' q5 E1 v* c/ P - RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE);//停止复位SPI26 u& ~4 E$ j0 z, y& _+ N+ _
- / x3 q# K: E9 O; I; l6 o
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;5 s' |4 t1 r9 t; S# C; W
- SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
( W1 d f+ ^% D) J, c - SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
/ t6 W: o9 \0 L# e) D$ g4 E - SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
% U6 }" G9 G' \7 s - SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;8 U9 \; d8 x$ g# i, x+ A* D# u
- SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;: B8 H) X# g7 D8 y. Q8 H
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
& `2 B4 @' P; U, W. @+ O6 T - SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
; J/ L6 e8 [* J) { - SPI_InitStructure.SPI_CRCPolynomial = 7;# i4 v. R f2 @. q% [) P$ s, O4 I
- SPI_Init(SPI2,&SPI_InitStructure);
; u1 |. v- ^0 P* X, V3 V- w -
8 V/ b) V* A7 }0 a+ A4 E+ m - SPI_SSOutputCmd(SPI2, DISABLE);% o2 `) ?7 d; e6 p
- // SPI_Cmd(SPI2, DISABLE); //Enable SPI2 $ F6 }* n S& S6 b! K4 y
-
( |, i& q3 z6 e r* b1 r6 e - SPI_Cmd(SPI2, ENABLE); //Enable SPI2
% f% N3 g) e* z4 K) p1 D' P6 j - // SPIx_ReadWriteByte(0xff); //启动传输6 e( R6 S, E( l2 f+ \* j
- // SPI_Cmd(SPI2, DISABLE); //Enable SPI2
( H6 c3 m$ o8 k/ D. n" r - 7 x2 n* k; h6 Y" J
- // SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);+ g. o) O- T- X# w& m+ I
- SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_ERR, ENABLE);
) H" c8 ]5 i9 I -
) F% \1 O# ?7 M" i - NVIC_InitTypeDef NVIC_InitStruct;
' [* K! S3 _( S+ D+ n# c6 T3 e - NVIC_InitStruct.NVIC_IRQChannel = SPI2_IRQn;6 d/ s Z5 K: f8 `/ |
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;+ \/ Z+ O) @- H) k9 n' m5 f p
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;' K9 v! Z: o0 l
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;! i) k- g4 {" q' i/ U4 O9 z
- NVIC_Init(&NVIC_InitStruct);
' a7 B" Y+ U* A# J* A -
3 c- b' W& C: m - }
4 z7 X- a- A* \4 T$ Q - " e2 b5 y6 \/ R) {
- void SPI2_IRQHandler(); s1 A, z2 }; c
- {2 q6 C6 I% @8 q& t8 N* a5 [ D
- if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET) {
4 q2 k5 ?' L& K - // SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_RXNE);
8 }2 ?0 P" X& n6 q' @8 ` - LOGI("IRQ:%.2X\r\n", SPI2->DR);
7 y+ r0 N0 A5 w2 X - } else if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_ERR) == SET) {
6 d0 H" X6 A5 M1 S - LOGE("SPI2 Error\r\n");
3 w C, t9 q9 m/ I5 \ - while (1);5 a$ g; |& o* W" ?7 L
- } else {/ ~& N& x4 a/ u, w, S
- LOGE("Unknown SPI2 interrupt\r\n");& _8 q# n7 M7 {1 T- o8 B
- while (1);) p% O/ O3 o: j2 i5 f' l K
- }
( ?: `# b: | s g - }
; a& E9 ~0 Q' }, B - 9 a [. l7 F+ B* s7 M
- static void SPI2_RX_DMA_Init(void)
% V+ ^# u5 s0 e. q- x& h - {
7 N. p% `2 W3 J y+ [. t4 J - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);( }# [. c6 [. H; Y
- . O7 e0 W5 `9 O; Y7 y' h4 v9 K
- DMA_DeInit(DMA1_Channel4);
I$ h$ k0 v% A. B1 o - 2 m5 ?+ I/ w- t3 ?
- DMA_InitTypeDef DMA_InitStruct;! W# \" D* F3 M, y; Q X, I
- DMA_StructInit(&DMA_InitStruct);) Q6 f; i3 z6 m
- DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&SPI2->DR);6 x# E1 V1 x3 W$ I3 v
- DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SPI2_RX_DMA_Buff;8 S1 l4 L! T, D; V( b ]! u
- DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
3 A6 W: |( }( J2 I q* c# R Z! c - DMA_InitStruct.DMA_BufferSize = sizeof(SPI2_RX_DMA_Buff);/ O; A- I1 L+ N
- DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
* x% i7 n/ g4 a - DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;7 V2 ]' i/ t: N1 x7 Z
- DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;4 C9 Y# K( g7 `- K" \, q; N
- DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
: A. L" m* S" N - DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
& [6 o3 m5 W/ u2 | - DMA_InitStruct.DMA_Priority = DMA_Priority_High;
}0 X4 k0 j7 q9 v - DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;& h, f5 p7 R/ g$ \
-
* E1 g( Y, t9 ]8 r - DMA_Init(DMA1_Channel4,&DMA_InitStruct);
2 H6 ^. Z& D2 ^% e) V* {3 s -
: P" O4 C7 T! w0 {% v/ s: K% B6 ?# I - DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,DISABLE);! M, z, k& o* `6 }& A3 Q
- DMA_ITConfig(DMA1_Channel4,DMA_IT_HT,DISABLE);
- D9 O0 n3 s9 v' k- s - DMA_ITConfig(DMA1_Channel4,DMA_IT_TE,DISABLE);
: W8 o7 G8 r! u+ K/ F3 R - - X7 V( q1 A, \
- NVIC_InitTypeDef NVIC_InitStruct;$ U/ |3 b: [1 {' ~
- NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel4_IRQn;
/ d5 z9 k: M1 Y- l0 D% F - NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;# b, |+ P# W% B x) _
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;0 N0 Z! [- t% t+ r4 G% }3 L
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;) h5 k8 q; O. K8 M }
- NVIC_Init(&NVIC_InitStruct);
, e6 y9 C) e) E" B$ ~ -
' ^, J4 n" z* b7 a2 W4 a% ? - SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
! E" F* q( Z, w5 ]7 v1 R - , f( F9 h* N( h) x' n2 _5 y* u+ j
- DMA_Cmd(DMA1_Channel4,ENABLE); //????DMA????
4 [6 j) {; A% u -
8 m$ J" l' m5 F( D" w - 3 j: r) q# I7 T, g, X( x9 E' G0 N9 B
- }5 O- T# A/ S2 r5 W# n) X( i
-
1 E/ G' k) ~9 o7 D G3 a - void DMA1_Channel4_IRQHandler()
' M( q/ c5 _4 k& W3 t* P; D - {0 L: l+ d5 x1 s7 p+ d2 o3 {( V
- DMA_ClearFlag(DMA1_FLAG_TC4);
, U5 y9 R4 I7 R$ `# X( k6 R - int i;2 x/ Q3 ]0 U( {6 K. k. n
- for(i=0;i<sizeof(SPI2_RX_DMA_Buff);i++)
+ Y0 f" J# Y8 F6 p+ B0 w; z# _7 } - printf("%c",SPI2_RX_DMA_Buff[i]);
7 R! R; B- X% C: L: g' S7 m6 V - printf("\r\n");, X0 W8 y4 i/ Y3 f( \9 m3 w1 \
- }
+ t9 p! u( x" Q9 f$ O# u- L0 c -
* P' A' p' |% s0 P, y9 x) ]4 K2 K0 S - static void read_spi2_input_buff(void)
& p* N* Y* n J/ g - {
6 H- M- _' U9 f+ k - static int LastDataCount = 0;: ^$ }! O5 k5 ^0 X- C2 y' w
- / s2 B1 p, o0 ]& k; {7 x! n
- int CurrDataCount = SPI2_RX_DMA_BUFF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel4);. n, ]: P1 X$ J5 \, p: e
- $ c% ?: z$ r& [% h: w5 w! x) h
- if(LastDataCount != CurrDataCount)
0 T$ D z7 r I( L- b( D- Z - {
9 M. L* b" J4 s, U; i" N. }) K& M - int i;
: Q/ {1 S {' ?( Z& W8 O - if(LastDataCount < CurrDataCount)* b, D: @. T F$ ?1 S3 \
- {. E' a$ A6 j& v0 ?0 _0 o! y# M- R
- // printf("1:%d %d,",LastDataCount,CurrDataCount);) `; m' ]6 X, b7 R6 S6 c
- for(i = LastDataCount;i<CurrDataCount;i++) {; Y0 ?9 Y0 {" W% z3 e( a3 @, Z4 k
- // printf("%.2X ",SPI2_RX_DMA_Buff[i]);
& y% U! \; I8 X) ^0 ~ - SPI2_frame_handler(SPI2_RX_DMA_Buff[i]);
. i! c. w) R# D3 h8 S$ J7 D& f; v) f - }
% K) b$ B4 u* U* Z3 O - // printf("\r\n");8 C) h: s7 P4 E5 L0 N- B
- }
( r7 N) M# z7 y$ s7 S b3 M - else9 m7 \, t, Q1 h$ W& E. o: n5 L" {
- {
7 o# g! E _/ ?) `$ h( C - // printf("2:%d %d,",LastDataCount,CurrDataCount);
# H% `0 t' N8 W6 _/ t/ Q - for(i = LastDataCount;i<SPI2_RX_DMA_BUFF_SIZE;i++) {
7 }! `; d) A/ e7 ?2 U, u: B0 z - // printf("%.2X ",SPI2_RX_DMA_Buff[i]);3 y1 d4 v8 B3 I* o, H
- SPI2_frame_handler(SPI2_RX_DMA_Buff[i]);
! p- a% Q7 _# N2 O - }
$ v9 ?, n; p7 P+ d. q( M6 L6 j( Y, c - for(i = 0;i<CurrDataCount;i++) {6 Z! o5 n9 F Q) ]& i! |8 `
- // printf("%.2X ",SPI2_RX_DMA_Buff[i]);
: y# y3 S, b4 s% p5 C* W - SPI2_frame_handler(SPI2_RX_DMA_Buff[i]);
( l {* j, B: g, y - }
4 k H3 h2 t+ v7 L; f - // printf("\r\n");
( k2 B; ?# U7 V+ P% L9 ? - }- X2 W3 F0 |! \- B, b
- }; K8 c, c& b( _% N8 K& C9 G
- P7 w- e: Z$ F* h- L
- LastDataCount = CurrDataCount;
7 [' k! z: Q p+ J& ]" y - }; q: ?' b r$ K' X9 p
-
5 a: b+ z" w0 q( V* P6 H) ? - static void SPI2_frame_handler(uint8_t data)
) d$ Y8 g- r4 _% V - {2 M7 N/ n0 q/ B6 a: ]% z) Z' y. q: O
- // printf("%.2X\r\n", data);
5 ?7 P( L' S2 Z7 Z/ U1 f; | - 7 p% V6 N/ {5 ^
- static int state = 0;8 ]& r; b; b- \& k K
-
, q0 @7 B. l& T( }$ Q; `: B5 M - // 0xA5 5A 55 AA
/ \( l9 j: k. q4 p" n - if (state == 0) {' |+ w7 Q7 e+ I8 X5 V/ N9 q
- if (data == 0xA5) { N& Q9 B' H! k9 h$ _* l: A
- state = 1;4 @9 H5 D- ^3 c; a
- }
* | A5 V- A% b: @ - } else if (state == 1) {
) P0 S V8 D2 \5 a: s# A+ N; z1 B - if (data == 0x5A) {
# d+ H' j) {* m1 h$ q - state = 2;; M5 a H& I+ x* f0 u4 Z. Y; z
- } else if (data == 0xA5) {- N3 p( W* E$ J2 q2 D) G4 l
- 8 j" \, R- C1 `+ G
- } else {5 W- U( }. Q: [9 z h1 ?
- LOGE("Frame err\r\n");/ c% \2 U! X% Y; `$ u/ K
- // while (1);
* u- j% b) s9 S1 i" l% D/ [1 Q7 e - state = 0;
: G8 h5 B9 X# K; r - }& y3 w+ e T8 X! a3 ~/ W6 ]* M: c
- } else if (state == 2) {. n8 }8 K- N7 c$ j+ b+ y
- if (data == 0x55) {
$ V' f% a v6 D - state = 3;
; y" o# }' A% Z1 f3 g - } else if (data == 0xA5) {" U1 k* p6 \1 c7 I' O1 w
- state = 1;
/ @, o4 Z" [8 m - } else {% q& S/ U# T/ l. R; ]
- LOGE("Frame err\r\n");
# c: t, E& G) {/ Z - // while (1);
1 n0 k2 }& R0 j: F! r$ H+ n - state = 0;1 x, B5 e1 a: \8 d7 W
- }% a. n ~. A9 X3 E; ~
- } else if (state == 3) {
x0 w$ m1 `$ t5 S i - if (data == 0xAA) {& K0 n) K; I% f6 d
- state = 4;
6 _( }: v2 D# i- G0 h - } else if (data == 0xA5) {! |7 o, f$ U/ |
- state = 1;3 [) r* k+ L2 \ I* P# Q
- } else {1 S W; J: p4 u: r/ M# P: @2 M- i6 ]; p
- LOGE("Frame err\r\n");# n$ ^9 [0 k4 g& P! b( a9 D& o& W
- // while (1);5 `1 q! u0 _7 a6 x
- state = 0;) ?1 r4 K1 L% N1 L
- }
- N% | `+ v+ L$ F, J! { - } else if (state == 4) {
1 I! h( N, V2 \" t* z3 M6 [/ R - // printf("ADDR:%.2X\r\n", data);
: U6 R( Z4 ?# M' ]0 z! m - state = 5;
/ D% H# t, \5 ?# l - } else if (state == 5) {/ n; `4 j0 P$ ~$ G9 o
- // printf("CMD:%.2X\r\n", data);
) F( k. x% E; y/ C - // SPI2->DR = 0x33;& ~) Y0 s# e @7 p' J/ H& g2 A
- 1 Y( T( h6 E1 t
- DMA_Cmd(DMA1_Channel5, DISABLE); // disable DMA before starting a new DMA transaction
2 i6 _* [2 W/ s3 G -
, g) w" z* O, l+ D - DMA_InitTypeDef DMA_InitStruct;
$ P' ]1 a: S! N# `# M" v - DMA_StructInit(&DMA_InitStruct);
( Q; D2 m' C( @8 |+ u - DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&SPI2->DR);$ h& n) F U% ]3 a
- DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SPI2_TX_DMA_Buff;6 w5 p) U5 d+ F1 E/ g
- DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;: V& M6 ~: w( r) P
- DMA_InitStruct.DMA_BufferSize = sizeof(SPI2_TX_DMA_Buff);
+ I j2 L: w2 M g - DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
3 O1 o' d0 S* @9 y9 Y/ I - DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
9 E+ [: p% S! f+ c& z' H - DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
9 ^! W' o3 q( v3 Y0 l - DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
7 S# z" {, w5 ]* f9 S0 c/ f9 D - DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
, T# q1 j: W. Z7 n5 _0 f - DMA_InitStruct.DMA_Priority = DMA_Priority_High;+ f; s/ E) Q; A6 ^
- DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
0 s5 Y5 \; g) f1 u. z -
% l8 ~. X6 X# u4 Z( ^- n# f - DMA_Init(DMA1_Channel5,&DMA_InitStruct);
1 K% A/ P1 ~) g6 w" e -
" u9 I9 [2 A& N - DMA_Cmd(DMA1_Channel5, ENABLE);- P: z& A; @. M! I" g" u0 e/ x
-
3 x# V0 A1 U! V7 ?1 ` - state = 0;) v6 O# i+ K( j% l4 Q
- } else if (state == 6) {
4 C* v: [! u6 F# Y7 ~) H -
7 R9 J, m+ m [" P# Z- u3 j - } else if (state == 7) {
* k; ^! j6 m- _( Y9 l -
5 I6 U5 ~# ?1 G, M" A- R - } else if (state == 8) {7 S0 D/ W# N# E6 q% l1 H, e
-
2 P/ L4 d$ i# O% X - } else if (state == 9) {
1 g; g# B2 Z T8 G% S - ; g) l* O) v1 a
- } else if (state == 10) {
# y" u/ v6 v% B1 J -
- C9 V2 j0 S# w% Y. x& }2 {# y - } else {
- p; s8 `+ D; J6 D - ; I+ g) w1 p; a, i+ `
- }
4 U P1 J& z0 y" W9 D9 }8 n+ d- d - }/ Q8 ?" t) E% `* A3 k4 z
-
2 I! p# j p1 r; N - static void SPI2_TX_DMA_Init(void)
0 V: _! o) ]; b - {
$ a; X8 }" [% ?$ W" E5 q - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
) R7 D# w3 D( x/ f* l/ U. ~- b -
8 B4 L! Z2 u% |; K d4 B- A - DMA_DeInit(DMA1_Channel5);" A, k* y r' b( \$ R! r' \
-
, {+ F) Y# E4 w+ o0 q V - DMA_InitTypeDef DMA_InitStruct;
3 j" }- e! j- A$ E+ W3 H - DMA_StructInit(&DMA_InitStruct);6 @1 g. M. U5 m8 J
- DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&SPI2->DR);$ C! E8 Q8 t& F- i6 B
- DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SPI2_TX_DMA_Buff;
( @* S: l- L" N* ]4 N: S4 ^2 B - DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;/ Q7 t! J) W2 C
- DMA_InitStruct.DMA_BufferSize = sizeof(SPI2_TX_DMA_Buff);
0 K4 j" C' B: V( _7 H; ` - DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
# d6 P& s+ \, X5 e- a/ m# s - DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
6 n4 w7 s: G2 i% H8 x - DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;0 C3 J# N! v* r
- DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
% i1 w. R, N* P2 o - DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;3 c2 G: d% H- ^
- DMA_InitStruct.DMA_Priority = DMA_Priority_High; v) D$ p/ y0 C) U! R8 S/ S
- DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
6 K; z/ p% s# ?# ~ }- ^ - ' b; s, u+ _1 N! M
- DMA_Init(DMA1_Channel5,&DMA_InitStruct);
5 j$ `) B8 \& ~- Z+ n- T" k: K e# D - ( C( \, j/ k5 ~( H1 v! y0 C
- DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,DISABLE);" u2 T+ e1 q. s0 V
- DMA_ITConfig(DMA1_Channel5,DMA_IT_HT,DISABLE);& \ e6 h$ T# L3 ]& F
- DMA_ITConfig(DMA1_Channel5,DMA_IT_TE,DISABLE);
( [( E4 @9 F# M2 z; B1 v% @ - & s$ g7 V- l# x2 w
- NVIC_InitTypeDef NVIC_InitStruct;, U9 O5 D- u0 u2 h/ x
- NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel5_IRQn;3 A3 }( c) t- ]% `$ h4 B6 \( |
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
4 X! h5 k8 g% o6 @; T - NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;; @" [6 u# K( b3 f2 V
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
8 J% i, ^+ g4 X( F7 f& ` - NVIC_Init(&NVIC_InitStruct);
* [/ S6 z5 \7 T- a9 I -
# y' c/ e" O. G& u$ L - SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);( W% J" i. o+ c4 I/ A* d2 V( N
- 4 J1 Y3 J$ {4 Z) d+ J; e1 ]
- DMA_Cmd(DMA1_Channel5,DISABLE); //????DMA????+ z. k/ P3 y1 g- s
- : P' i8 a' y( {+ Z
- // note: the last byte moved from RAM to SPI2->DR is seen as the end of DMA transmission
/ j. u; d/ R* L8 @% M+ _ - // but the last byte still not been transferred out from the shift register which means
8 _( J$ N9 }2 c4 E! ` - // the SPI master might not received the last byte when the SPI slave finishing the DMA
+ X" a6 ]' F5 b& s& T, R - // transfer.
1 c/ |8 @5 s+ ?. r1 \, z5 |2 P - }
+ H' Z& t8 d7 _ q -
! ` u9 e1 }- x7 h) E6 u - void DMA1_Channel5_IRQHandler()
- |" t" P0 T$ p- }6 M2 g# ?* J - {
- Q. X* ~) T1 ^& H a( v+ D - DMA_ClearFlag(DMA1_FLAG_TC5);" g) q% i: O$ ~& y! M3 ?. `8 M
- // LOGI("Sent done\r\n");; |3 n! }. T8 e
- DMA_Cmd(DMA1_Channel5, DISABLE); S9 R0 S; J+ M! J
- // SPI2->DR = 0x00; // set the shift register to 0x00 other wise SPI master will get the previous byte/ F/ {9 R( ^: L
- // // which might be 0xA0 and causes error.9 l; \! D5 h- M/ T
- // 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 - // 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 - // byte of the DMA buffer if the last byte is 0xA0.
2 l# E* F; L8 D" ]" r - }
- 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 ~ |