1 | #define SPI2_SCK_PIN GPIO_Pin_13
|
2 | #define SPI2_SCK_GPIO_PORT GPIOB
|
3 | #define SPI2_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB
|
4 | #define SPI2_SCK_SOURCE GPIO_PinSource13
|
5 | #define SPI2_SCK_AF GPIO_AF_SPI2
|
6 |
|
7 | #define SPI2_MOSI_PIN GPIO_Pin_15
|
8 | #define SPI2_MOSI_GPIO_PORT GPIOB
|
9 | #define SPI2_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB
|
10 | #define SPI2_MOSI_SOURCE GPIO_PinSource15
|
11 | #define SPI2_MOSI_AF GPIO_AF_SPI2
|
12 |
|
13 | #define SPI2_MISO_PIN GPIO_Pin_14
|
14 | #define SPI2_MISO_GPIO_PORT GPIOC
|
15 | #define SPI2_MISO_GPIO_CLK RCC_AHB1Periph_GPIOB
|
16 | #define SPI2_MISO_SOURCE GPIO_PinSource14
|
17 | #define SPI2_MISO_AF GPIO_AF_SPI2
|
18 |
|
19 | #define SPI2_CS_PIN GPIO_Pin_12
|
20 | #define SPI2_CS_GPIO_PORT GPIOB
|
21 | #define SPI2_CS_GPIO_CLK RCC_AHB1Periph_GPIOB
|
22 | #define SPI2_CS_SOURCE GPIO_PinSource12
|
23 | #define SPI2_CS_AF GPIO_AF_SPI2
|
24 |
|
25 | void SPI2_Init (void){
|
26 | /************************************************************************/
|
27 | SPI_InitTypeDef SPI_InitStructure;
|
28 | GPIO_InitTypeDef GPIO_InitStructure;
|
29 |
|
30 | GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN;
|
31 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN|GPIO_Mode_AF ;
|
32 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
33 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
34 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
35 |
|
36 | GPIO_InitStructure.GPIO_Pin = SPI2_MOSI_PIN;
|
37 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT|GPIO_Mode_AF;
|
38 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
39 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
40 |
|
41 | GPIO_InitStructure.GPIO_Pin = SPI2_MISO_PIN;
|
42 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN|GPIO_Mode_AF ;
|
43 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
44 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
45 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
46 |
|
47 | GPIO_InitStructure.GPIO_Pin = SPI2_CS_PIN;
|
48 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN|GPIO_Mode_AF ;
|
49 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
50 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
51 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
52 |
|
53 | GPIO_PinAFConfig(SPI2_SCK_GPIO_PORT, SPI2_SCK_SOURCE, SPI2_SCK_AF);
|
54 | GPIO_PinAFConfig(SPI2_MOSI_GPIO_PORT, SPI2_MOSI_SOURCE, SPI2_MOSI_AF);
|
55 | GPIO_PinAFConfig(SPI2_MISO_GPIO_PORT, SPI2_MISO_SOURCE, SPI2_MISO_AF);
|
56 | GPIO_PinAFConfig(SPI2_MISO_GPIO_PORT, SPI2_CS_SOURCE, SPI2_CS_AF);
|
57 |
|
58 | RCC_AHB1PeriphClockCmd(SPI2_SCK_GPIO_CLK, ENABLE);
|
59 | RCC_AHB1PeriphClockCmd(SPI2_MOSI_GPIO_CLK, ENABLE);
|
60 | RCC_AHB1PeriphClockCmd(SPI2_MISO_GPIO_CLK, ENABLE);
|
61 | RCC_AHB1PeriphClockCmd(SPI2_CS_GPIO_CLK, ENABLE);
|
62 |
|
63 | /************************************************************************/
|
64 | // SPI2 configurieren
|
65 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
|
66 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
67 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
68 | SPI_InitStructure.SPI_CRCPolynomial = 0;
|
69 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
70 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
71 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
72 | SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
|
73 | SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
|
74 | SPI_Init(SPI2, &SPI_InitStructure);
|
75 | SPI_Cmd(SPI2, ENABLE);
|
76 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
77 |
|
78 | NVIC_InitTypeDef NVIC_InitStructure;
|
79 | /* Enable the USARTy Interrupt */
|
80 | NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
|
81 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
82 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
83 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
84 | NVIC_Init(&NVIC_InitStructure);
|
85 | SPI_I2S_ITConfig(SPI2, SPI_I2S_FLAG_RXNE,ENABLE);
|
86 | /************************************************************************/
|
87 | }
|
88 |
|
89 |
|
90 | void spi_handleSPI2Interrupt(void){
|
91 | GPIOD->ODR ^= GPIO_Pin_14 ;
|
92 | }
|