1 | tSpiError spiInit()
|
2 | {
|
3 | GPIO_InitTypeDef GPIO_InitStructure;
|
4 | NVIC_InitTypeDef NVIC_InitStructure;
|
5 | EXTI_InitTypeDef EXTI_InitStructure;
|
6 | SPI_InitTypeDef SPI_InitStructure;
|
7 | tSpiError Error = SpiNoError;
|
8 |
|
9 |
|
10 | // SPI1_NSS - PA15, SPI1_CLK - PB3, SPI1_MISO - PB4, SPI1_MOSI - PB5
|
11 | // EXT_INT_1 - PA1 to detect CS both edges
|
12 | // Peripheral Clock Enable -------------------------------------------------
|
13 | // Enable the SPI1 clock
|
14 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
15 |
|
16 | // Enable GPIO clocks
|
17 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
18 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
19 |
|
20 | // DEBUG-PIN
|
21 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
22 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
23 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
24 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
25 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
26 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
27 |
|
28 |
|
29 | // SPI GPIO Configuration --------------------------------------------------
|
30 | // Connect SPI pins to AF5
|
31 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_SPI1); // CS
|
32 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1); // SCK
|
33 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1); // MISO
|
34 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1); // MOSI
|
35 |
|
36 | // MISO, MOSI
|
37 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
|
38 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
39 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
|
40 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
41 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
42 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
43 |
|
44 | // SCK
|
45 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
46 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
47 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
48 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
49 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
50 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
51 |
|
52 | // CS - Software controlled
|
53 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
|
54 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
55 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
|
56 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
57 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
58 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
59 | GPIO_SetBits(GPIOA, GPIO_Pin_15);
|
60 |
|
61 | // SPI configuration -------------------------------------------------------
|
62 | SPI_I2S_DeInit(SPI1);
|
63 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
64 | //SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
|
65 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
66 | //SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
|
67 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
68 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
69 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
70 | //SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
|
71 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
72 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
|
73 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
74 | SPI_InitStructure.SPI_CRCPolynomial = 0;
|
75 | SPI_Init(SPI1, &SPI_InitStructure);
|
76 |
|
77 |
|
78 | // Configure the SPI interrupt priority
|
79 | NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
|
80 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = SPI_INTERRUPT_PRIO;
|
81 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//(uint8_t) SpiSimInst_p;
|
82 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
83 | NVIC_Init(&NVIC_InitStructure);
|
84 |
|
85 | // Enable the Rx buffer not empty interrupt
|
86 | SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);
|
87 | SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
|
88 | // SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_ERR, ENABLE);
|
89 |
|
90 | // Enable the SPI peripheral
|
91 | // Enable in Slave-Mode should be done with CS-Signal -> additional EXTI-Input
|
92 | //SPI_Cmd(SPI1, ENABLE);
|
93 | // SPI_SSOutputCmd(SPI1, ENABLE);
|
94 | Exit:
|
95 | return Error;
|
96 | }
|