1 | void SpiInit(void)
|
2 | {
|
3 | SPI_InitTypeDef SPI_InitStructure;
|
4 | GPIO_InitTypeDef GPIO_InitStructure;
|
5 | NVIC_InitTypeDef NVIC_InitStructure;
|
6 |
|
7 | /* Enable the clock */
|
8 | SPIx_CLK_INIT(SPIx_CLK, ENABLE);
|
9 |
|
10 | /* Enable GPIO clocks */
|
11 | RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
|
12 |
|
13 | /* Connect pins to AF5 */
|
14 | GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
|
15 | GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);
|
16 | GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF);
|
17 |
|
18 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
19 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
20 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
21 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
22 |
|
23 | /* SCK pin configuration */
|
24 | GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN;
|
25 | GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
|
26 |
|
27 | /* MOSI pin configuration */
|
28 | GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN;
|
29 | GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);
|
30 |
|
31 | /* MISO pin configuration */
|
32 | GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN;
|
33 | GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStructure);
|
34 |
|
35 | SPI_I2S_DeInit(SPIx);
|
36 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
37 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
38 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
|
39 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
40 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
41 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
|
42 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
43 | SPI_InitStructure.SPI_CRCPolynomial = 7;
|
44 |
|
45 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
46 | SPI_Init(SPIx, &SPI_InitStructure);
|
47 |
|
48 | SPI_Cmd(SPIx, ENABLE);
|
49 | }
|