1 | void InitSpi()
|
2 | {
|
3 | GPIO_InitTypeDef gpioInit;
|
4 | SPI_InitTypeDef spiInit;
|
5 |
|
6 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
7 |
|
8 | /* configure pins used by SPI-1
|
9 | * PA5 = SCK
|
10 | * PA7 = MOSI
|
11 | * PA8 = RCK
|
12 | */
|
13 |
|
14 | //SPI
|
15 | gpioInit.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
|
16 | gpioInit.GPIO_Mode = GPIO_Mode_AF;
|
17 | gpioInit.GPIO_OType = GPIO_OType_PP;
|
18 | gpioInit.GPIO_Speed = GPIO_Speed_50MHz;
|
19 | gpioInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
20 | GPIO_Init(GPIOA, &gpioInit);
|
21 |
|
22 | //Pin for RCK, means that transmitted bits will be written out
|
23 | gpioInit.GPIO_Pin = GPIO_Pin_8;
|
24 | gpioInit.GPIO_Mode = GPIO_Mode_OUT;
|
25 | gpioInit.GPIO_OType = GPIO_OType_PP;
|
26 | gpioInit.GPIO_Speed = GPIO_Speed_100MHz;
|
27 | gpioInit.GPIO_PuPd = GPIO_PuPd_UP;
|
28 | GPIO_Init(GPIOA, &gpioInit);
|
29 |
|
30 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
|
31 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
|
32 |
|
33 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
34 |
|
35 | //SPI only to transmit
|
36 | spiInit.SPI_Direction = SPI_Direction_Tx;
|
37 | spiInit.SPI_Mode = SPI_Mode_Master;
|
38 | spiInit.SPI_DataSize = SPI_DataSize_8b;
|
39 | spiInit.SPI_CPOL = SPI_CPOL_Low;
|
40 | spiInit.SPI_CPHA = SPI_CPHA_1Edge;
|
41 | spiInit.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
|
42 | spiInit.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
|
43 | spiInit.SPI_FirstBit = SPI_FirstBit_MSB;
|
44 | SPI_Init(SPI1, &spiInit);
|
45 |
|
46 | SPI_Cmd(SPI1, ENABLE);
|
47 | }
|
48 |
|
49 | void Spi1SendByte(uint8_t data)
|
50 | {
|
51 | SPI1->DR = data;
|
52 | while( !(SPI1->SR & SPI_I2S_FLAG_TXE));
|
53 | while( !(SPI1->SR & SPI_I2S_FLAG_BSY));
|
54 | }
|
55 |
|
56 | int main(void)
|
57 | {
|
58 | SystemInit();
|
59 | InitSpi();
|
60 |
|
61 | //Set RCK to low
|
62 | GPIO_WriteBit(GPIOA, GPIO_Pin_8, RESET);
|
63 |
|
64 | //Every led should be turned on
|
65 | Spi1SendByte(0b11111111);
|
66 |
|
67 | //Set RCK to high and again to low = 0-1-0 -> write transmitted bits out
|
68 | GPIO_WriteBit(GPIOA, GPIO_Pin_8, SET);
|
69 | GPIO_WriteBit(GPIOA, GPIO_Pin_8, RESET);
|
70 |
|
71 |
|
72 | while(1)
|
73 | {
|
74 |
|
75 | }
|
76 | }
|