1 | uint16_t SPI_MASTER_Buffer_Rx[64];
|
2 | uint16_t SPI_MASTER_Buffer_Tx[2];
|
3 |
|
4 |
|
5 | static void SPI_Config(void)
|
6 | {
|
7 | GPIO_InitTypeDef GPIO_InitStructure;
|
8 | NVIC_InitTypeDef NVIC_InitStructure;
|
9 |
|
10 | /* Enable the SPI clock */
|
11 | SPIx_CLK_INIT(SPIx_CLK, ENABLE);
|
12 |
|
13 | /* Enable GPIO clocks */
|
14 | RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
|
15 |
|
16 | /* Connect SPI pins to AF5 */
|
17 | GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
|
18 | GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);
|
19 | GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF);
|
20 |
|
21 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
22 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
23 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
24 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
25 |
|
26 | /* SPI SCK pin configuration */
|
27 | GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN;
|
28 | GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
|
29 |
|
30 | /* SPI MOSI pin configuration */
|
31 | GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN;
|
32 | GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);
|
33 |
|
34 | /* SPI MISO pin configuration */
|
35 | GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN;
|
36 | GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStructure);
|
37 |
|
38 | /* SPI configuration -------------------------------------------------------*/
|
39 | SPI_I2S_DeInit(SPIx);
|
40 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
41 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
|
42 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
|
43 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
44 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
45 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
|
46 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
47 | SPI_InitStructure.SPI_CRCPolynomial = 7;
|
48 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
49 |
|
50 | SPI_Init(SPIx, &SPI_InitStructure);
|
51 |
|
52 | /* Configure the Priority Group to 1 bit */
|
53 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
54 | /* Configure the SPI interrupt priority */
|
55 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
56 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
57 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
58 | NVIC_InitStructure.NVIC_IRQChannel = SPI_DMA_RX_IRQn;
|
59 | NVIC_Init(&NVIC_InitStructure);
|
60 |
|
61 | SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
|
62 |
|
63 | /* The Data transfer is performed in the SPI interrupt routine */
|
64 | /* Enable the SPI peripheral */
|
65 | SPI_Cmd(SPIx, ENABLE);
|
66 | }
|
67 |
|
68 | void DMA_Config(void)
|
69 | {
|
70 | /* Dma config */
|
71 | RCC_AHB1PeriphClockCmd(SPI_DMAx_CLK, ENABLE);
|
72 |
|
73 | /* DMA RX */
|
74 | DMA_StructInit(&DMA_InitStructure);
|
75 | DMA_DeInit(SPI_RX_DMA_STREAM);
|
76 | DMA_InitStructure.DMA_PeripheralBaseAddr = SPI_DR_ADDRESS;
|
77 | DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
78 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
79 | DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
|
80 | DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
81 | DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
82 | DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
|
83 | DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
|
84 | DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
|
85 | DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
|
86 | DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
|
87 | DMA_InitStructure.DMA_Channel = SPI_RX_DMA_CHANNEL;
|
88 | DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
|
89 | DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SPI_MASTER_Buffer_Rx;
|
90 | DMA_InitStructure.DMA_BufferSize = 64;
|
91 | DMA_Init(SPI_RX_DMA_STREAM, &DMA_InitStructure);
|
92 |
|
93 | DMA_Cmd(SPI_RX_DMA_STREAM, ENABLE);
|
94 |
|
95 | /* DMA TX */
|
96 | DMA_StructInit(&DMA_InitStructure);
|
97 | DMA_DeInit(SPI_TX_DMA_STREAM);
|
98 | DMA_InitStructure.DMA_PeripheralBaseAddr = SPI_DR_ADDRESS;
|
99 | DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
100 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
101 | DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
|
102 | DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
103 | DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
104 | DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
|
105 | DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
|
106 | DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
|
107 | DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
|
108 | DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
|
109 | DMA_InitStructure.DMA_Channel = SPI_TX_DMA_CHANNEL;
|
110 | DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
|
111 | DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SPI_MASTER_EmptyBuffer_Tx;
|
112 | DMA_InitStructure.DMA_BufferSize = 2;
|
113 | DMA_Init(SPI_TX_DMA_STREAM, &DMA_InitStructure);
|
114 |
|
115 | DMA_Cmd(SPI_TX_DMA_STREAM, ENABLE);
|
116 |
|
117 | DMA_ITConfig(SPI_RX_DMA_STREAM, DMA_IT_TC | DMA_IT_TE | DMA_IT_FE , ENABLE);
|
118 | }
|