1 | //Pseudo-Functions
|
2 | #define CS_low() GPIOC->ODR &= ~(1<<14);
|
3 | #define CS_high() GPIOC->ODR |= (1<<14);
|
4 |
|
5 |
|
6 | void spi_init(void)//set spi speed and settings
|
7 | {
|
8 | SPI_InitTypeDef SPI_InitStruct;
|
9 | GPIO_InitTypeDef GPIO_InitStruct;
|
10 |
|
11 | RCC_APB1PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
|
12 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, DISABLE); //Errata -> Don't use Remapped SPI1 with activated I2C1-Clock
|
13 |
|
14 | // GPIO pins for MISO, MOSI and SCK
|
15 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_4 | GPIO_Pin_3;
|
16 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
17 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
18 | GPIO_Init(GPIOB, &GPIO_InitStruct);
|
19 |
|
20 | //GPIO PortC 14 ->CS
|
21 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_14;
|
22 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
|
23 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
24 | GPIO_Init(GPIOC, &GPIO_InitStruct);
|
25 |
|
26 | GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
|
27 | GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE);
|
28 |
|
29 | SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
|
30 | SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
|
31 | SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
|
32 | SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
|
33 | SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
|
34 | SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
|
35 | SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
|
36 | SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
|
37 | SPI_Init(SPI1, &SPI_InitStruct);
|
38 | SPI_Cmd(SPI1, ENABLE);
|
39 |
|
40 | CS_high();
|
41 | }
|
42 |
|
43 | void spi_send(uint8_t spi_data)//send spi data to display
|
44 | {
|
45 | SPI1->DR = spi_data; // Write data to be transmitted to the SPI data register
|
46 | while (!(SPI1->SR & (SPI_I2S_FLAG_TXE))); // Wait until transmit complete
|
47 | while (SPI1->SR & (SPI_I2S_FLAG_BSY)); // Wait until SPI is not busy anymore
|
48 | }
|
49 |
|
50 | uint8_t spi_read(void)
|
51 | {
|
52 | SPI1->DR = 0x00; // Write data to be transmitted to the SPI data register
|
53 | while (!(SPI1->SR & (SPI_I2S_FLAG_TXE))); // Wait until transmit complete
|
54 | while (SPI1->SR & (SPI_I2S_FLAG_BSY)); // Wait until SPI is not busy anymore
|
55 | while (!(SPI1->SR & (SPI_I2S_FLAG_RXNE))); // Wait until receive complete
|
56 | return SPI1->DR; // Return received data from SPI data register
|
57 | }
|