1 | void init_spi(void){
|
2 | // SPI Connections
|
3 | // D6 : PA8 CS
|
4 | // D10 : PA4 SPI1 NSS
|
5 | // D11 : PA7 MOSI1
|
6 | // D12 : PA6 MISO1
|
7 | // D13 : PA5 SCK
|
8 | uint16_t SPI_Mode;
|
9 | //TM_SPI_Mode_t SPI_mode;
|
10 | SPI_InitTypeDef SPI_InitStructure;
|
11 |
|
12 | RCC_PCLK2Config(RCC_HCLK_Div8); // set clock division of PCLK2
|
13 |
|
14 | // enable GPIOA, GPIOC and SPI1 clock
|
15 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC| RCC_APB2Periph_SPI1, ENABLE);
|
16 | RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
|
17 | //SPI MasterOutSerialIn Port
|
18 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
|
19 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
20 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
21 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
22 |
|
23 |
|
24 | // When using more devices on SPI bus use Chip Select line
|
25 | // GPIOA_Pin_4 = RS GPIOA_Pin_8 = CS
|
26 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_8 | GPIO_Pin_9;
|
27 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
28 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
29 |
|
30 | //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
31 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
32 |
|
33 | // Configure SPI1 pins: SCK, MISO and MOSI
|
34 | // SCK and MOSI pins as Alternate Function Push Pull
|
35 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
|
36 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
37 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
38 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
39 |
|
40 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
41 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
42 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
43 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
44 |
|
45 | //SPI1 Bus setup
|
46 | //SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
|
47 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
48 | //SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
49 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
50 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
51 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
|
52 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
53 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
|
54 |
|
55 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
56 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
57 |
|
58 | SPI_Mode=3;
|
59 | if (SPI_Mode == 0) {
|
60 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
61 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
62 | } else if (SPI_Mode == 1) {
|
63 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
64 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
65 | } else if (SPI_Mode == 2) {
|
66 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
|
67 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
68 | } else if (SPI_Mode == 3) {
|
69 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
|
70 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
78 | //SPI_InitStructure.SPI_CRCPolynomial = 7;
|
79 | SPI_Init(SPI1, &SPI_InitStructure);
|
80 | SPI_Cmd(SPI1, ENABLE);
|
81 |
|
82 | SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);
|
83 | }
|