1 | #include <stdint.h>
|
2 | #include "stm32f2xx.h"
|
3 |
|
4 | #define TRANSMIT_COMPLETE
|
5 | //#define RECEIVE_COMPLETE
|
6 |
|
7 | #ifdef TRANSMIT_COMPLETE
|
8 | #define DMA_STREAM_IRQn DMA1_Stream7_IRQn
|
9 | #define DMA_IRQ_STREAM DMA1_Stream7
|
10 | #define DMA_IRQ_TCIF DMA_IT_TCIF7
|
11 | #define DMA_IRQ_HANDLER DMA1_Stream7_IRQHandler
|
12 | #endif
|
13 | #ifdef RECEIVE_COMPLETE
|
14 | #define DMA_STREAM_IRQn DMA1_Stream0_IRQn
|
15 | #define DMA_IRQ_STREAM DMA1_Stream0
|
16 | #define DMA_IRQ_TCIF DMA_IT_TCIF0
|
17 | #define DMA_IRQ_HANDLER DMA1_Stream0_IRQHandler
|
18 | #endif
|
19 |
|
20 |
|
21 | void init(void) {
|
22 | GPIO_InitTypeDef gpio;
|
23 | SPI_InitTypeDef spi;
|
24 | //clock peripherals
|
25 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
|
26 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //CS
|
27 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //CLK, MISO, MOSI
|
28 | //init CS pin
|
29 | GPIO_StructInit(&gpio);
|
30 | gpio.GPIO_Pin = GPIO_Pin_15;
|
31 | gpio.GPIO_Mode = GPIO_Mode_OUT;
|
32 | gpio.GPIO_Speed = GPIO_Speed_50MHz;
|
33 | gpio.GPIO_OType = GPIO_OType_PP;
|
34 | gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
35 | GPIO_Init(GPIOA, &gpio);
|
36 | GPIOA->BSRRL = GPIO_Pin_15; //set CS
|
37 | //init spi pins
|
38 | gpio.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_10 | GPIO_Pin_10;
|
39 | gpio.GPIO_Mode = GPIO_Mode_AF;
|
40 | gpio.GPIO_Speed = GPIO_Speed_50MHz;
|
41 | gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
42 | GPIO_Init(GPIOC, &gpio);
|
43 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
|
44 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);
|
45 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
|
46 | //init spi
|
47 | SPI_StructInit(&spi);
|
48 | spi.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
49 | spi.SPI_Mode = SPI_Mode_Master;
|
50 | spi.SPI_DataSize = SPI_DataSize_8b;
|
51 | spi.SPI_CPOL = SPI_CPOL_Low;
|
52 | spi.SPI_CPHA = SPI_CPHA_1Edge;
|
53 | spi.SPI_NSS = SPI_NSS_Soft;
|
54 | spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
|
55 | SPI_Init(SPI3,&spi);
|
56 | SPI_Cmd(SPI3, ENABLE);
|
57 | }
|
58 |
|
59 | void send_dma(uint8_t* src, uint8_t* dest, uint32_t cnt) {
|
60 | DMA_InitTypeDef dma;
|
61 | NVIC_InitTypeDef nvic;
|
62 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
|
63 | //init_nvic
|
64 | nvic.NVIC_IRQChannel = DMA_STREAM_IRQn;
|
65 | nvic.NVIC_IRQChannelPreemptionPriority = 0;
|
66 | nvic.NVIC_IRQChannelSubPriority = 2;
|
67 | nvic.NVIC_IRQChannelCmd = ENABLE;
|
68 | NVIC_Init(&nvic);
|
69 | //init tx dma
|
70 | dma.DMA_Channel = DMA_Channel_0;
|
71 | dma.DMA_PeripheralBaseAddr = (uint32_t)&SPI3->DR;
|
72 | dma.DMA_Memory0BaseAddr = (uint32_t) &src;
|
73 | dma.DMA_DIR = DMA_DIR_MemoryToPeripheral;
|
74 | dma.DMA_BufferSize = cnt;
|
75 | dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
76 | dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
77 | dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
|
78 | dma.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
79 | dma.DMA_Mode = DMA_Mode_Normal;
|
80 | dma.DMA_Priority = DMA_Priority_High;
|
81 | dma.DMA_FIFOMode = DMA_FIFOMode_Disable;
|
82 | dma.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
|
83 | dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;
|
84 | dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
|
85 | DMA_Init(DMA1_Stream7, &dma);
|
86 | //init rx dma
|
87 | dma.DMA_Channel = DMA_Channel_0;
|
88 | dma.DMA_DIR = DMA_DIR_PeripheralToMemory;
|
89 | dma.DMA_Memory0BaseAddr = (uint32_t) &dest;
|
90 | dma.DMA_BufferSize = cnt;
|
91 | dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
92 | DMA_Init(DMA1_Stream0, &dma);
|
93 | //enable streams
|
94 | DMA_Cmd(DMA1_Stream0, ENABLE);
|
95 | DMA_Cmd(DMA1_Stream7, ENABLE);
|
96 | //enable interrupt
|
97 | DMA_ITConfig(DMA_IRQ_STREAM, DMA_IT_TC, ENABLE);
|
98 | SPI_I2S_DMACmd(SPI3,SPI_I2S_DMAReq_Rx,ENABLE);
|
99 | SPI_I2S_DMACmd(SPI3,SPI_I2S_DMAReq_Tx,ENABLE);
|
100 | GPIOA->BSRRH = GPIO_Pin_15; //clear CS
|
101 | I2S_Cmd(SPI3, ENABLE);
|
102 | }
|
103 |
|
104 | void DMA_IRQ_HANDLER(void) {
|
105 | DMA_ClearITPendingBit(DMA_IRQ_STREAM, DMA_IRQ_TCIF);
|
106 | GPIOA->BSRRL = GPIO_Pin_15; //set CS
|
107 | }
|
108 |
|
109 | uint8_t outbuffer[10];
|
110 | uint8_t inbuffer[10];
|
111 | int main(void) {
|
112 | outbuffer[0] = 1;
|
113 | outbuffer[0] = 2;
|
114 | outbuffer[0] = 3;
|
115 | init();
|
116 | for(int32_t i=0 ; i<100000 ; i++);
|
117 | send_dma(outbuffer, inbuffer, 3); //breakpoint
|
118 | for(int32_t i=0 ; i<100000 ; i++);
|
119 | send_dma(outbuffer, inbuffer, 3); //breakpoint
|
120 | while(1);
|
121 | return 0;
|
122 | }
|