1 | void init_SPI1(void){
|
2 |
|
3 | GPIO_InitTypeDef GPIO_InitStruct;
|
4 | SPI_InitTypeDef SPI_InitStruct;
|
5 |
|
6 | // enable peripheral clock
|
7 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
8 |
|
9 | // enable clock for used IO pins
|
10 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
11 |
|
12 | /* GPIO Deinitialisation */
|
13 | GPIO_DeInit(GPIOA);
|
14 | GPIO_DeInit(GPIOA);
|
15 | GPIO_DeInit(GPIOA);
|
16 |
|
17 | /* configure pins used by SPI1
|
18 | * PA4 = SCK
|
19 | * PA5 = MISO
|
20 | * PA6 = MOSI
|
21 | */
|
22 |
|
23 | // connect SPI1 pins to SPI alternate function
|
24 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
|
25 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
|
26 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
|
27 |
|
28 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
|
29 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
30 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
31 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
|
32 |
|
33 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
|
34 | GPIO_Init(GPIOA, &GPIO_InitStruct);
|
35 |
|
36 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
|
37 | GPIO_Init(GPIOA, &GPIO_InitStruct);
|
38 |
|
39 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
|
40 | GPIO_Init(GPIOA, &GPIO_InitStruct);
|
41 |
|
42 | // enable clock for used IO pins
|
43 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
|
44 |
|
45 | /* Configure the chip select pin
|
46 | in this case we will use PE7 */
|
47 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
|
48 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
|
49 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
50 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
51 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
|
52 | GPIO_Init(GPIOE, &GPIO_InitStruct);
|
53 |
|
54 | GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high
|
55 |
|
56 | /* configure SPI1 in Mode 0
|
57 | * CPOL = 0 --> clock is low when idle
|
58 | * CPHA = 0 --> data is sampled at the first edge
|
59 | */
|
60 |
|
61 | SPI_I2S_DeInit(SPI1);
|
62 | SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO line
|
63 | SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
|
64 | SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
|
65 | SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
|
66 | SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // data sampled at first edge
|
67 | SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
|
68 | SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // SPI frequency is APB2 frequency / 4
|
69 | SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
|
70 | SPI_Init(SPI1, &SPI_InitStruct);
|
71 |
|
72 | SPI_Cmd(SPI1, ENABLE); // enable SPI1
|
73 | }
|
74 |
|
75 | /* This funtion is used to transmit and receive data
|
76 | * with SPI1
|
77 | * data --> data to be transmitted
|
78 | * returns received value
|
79 | */
|
80 | uint8_t SPI1_send(uint8_t data){
|
81 |
|
82 | SPI1->DR = data; // write data to be transmitted to the SPI data register
|
83 | while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
|
84 | while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
|
85 | while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
|
86 | return SPI1->DR; // return received data from SPI data register
|
87 | }
|
88 |
|
89 | int main(void){
|
90 |
|
91 | uint8_t received_val = 0;
|
92 |
|
93 | init_SPI1();
|
94 |
|
95 | while(1){
|
96 |
|
97 | GPIOE->BSRRH |= GPIO_Pin_7; // set PE7 (CS) low
|
98 | SPI1_send(0xFA); // transmit data
|
99 | received_val = SPI1_send(0x00); // transmit dummy byte and receive data
|
100 | GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 (CS) high
|
101 | }
|
102 | }
|