1 | int16_t SPI_write(uint8_t *data, uint32_t length) {
|
2 | uint16_t i;
|
3 | volatile uint32_t x;
|
4 | x=0xff;
|
5 | for (; x!=0; x--);
|
6 | // while( !(SPI3->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
|
7 | // while( !(SPI3->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
|
8 | // while( SPI3->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
|
9 | for (i=0;i<length;i++) {
|
10 | SPI3->DR = data[i];
|
11 | while( !(SPI3->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
|
12 | while( !(SPI3->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
|
13 | while( SPI3->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
|
14 | // x=0x40;
|
15 | // for (; x!=0; x--);
|
16 | }
|
17 | x=0xAA;
|
18 | for (; x!=0; x--);
|
19 | return length;
|
20 | }
|
21 |
|
22 | int16_t SPI_read(uint8_t *data, uint32_t length) {
|
23 | uint16_t i;
|
24 | volatile uint32_t x;
|
25 | x=0xff;
|
26 | for (; x!=0; x--);
|
27 | // while( !(SPI3->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
|
28 | // while( !(SPI3->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
|
29 | // while( SPI3->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
|
30 | for (i=0;i<length;i++) {
|
31 | SPI3->DR=0x00; // dummy daten schreiben, dies ist nötig damit empfangen werden kann
|
32 | while( !(SPI3->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
|
33 | while( !(SPI3->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
|
34 | while( SPI3->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
|
35 | data[i]=SPI3->DR;
|
36 | // x=0x40;
|
37 | // for (; x!=0; x--);
|
38 | }
|
39 | x=0xAA;
|
40 | for (; x!=0; x--);
|
41 | return length;
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | void init_SPI3(void){
|
47 |
|
48 | GPIO_InitTypeDef GPIO_InitStruct;
|
49 | SPI_InitTypeDef SPI_InitStruct;
|
50 |
|
51 | // enable clock for used IO pins
|
52 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
|
53 |
|
54 | /* configure pins used by SPI3
|
55 | * PA5 = SCK
|
56 | * PA6 = MISO
|
57 | * PA7 = MOSI
|
58 | */
|
59 |
|
60 | /*
|
61 | * PC10 = SPI3_SCK
|
62 | * PC11 = SPI3_MISO
|
63 | * PC12 = SPI3_MOSI
|
64 | */
|
65 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12; //GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
|
66 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
|
67 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
68 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;
|
69 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
70 | GPIO_Init(GPIOC, &GPIO_InitStruct);
|
71 |
|
72 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
|
73 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);
|
74 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
|
75 |
|
76 | #if 1
|
77 | // enable clock for used IO pins
|
78 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
|
79 |
|
80 | /* Configure the chip select pin
|
81 | in this case we will use PE7 */
|
82 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
|
83 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
|
84 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
85 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;
|
86 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; //UP;
|
87 | GPIO_Init(GPIOE, &GPIO_InitStruct);
|
88 |
|
89 | GPIOE->BSRRL |= GPIO_Pin_2; // set PE7 high
|
90 | #endif
|
91 |
|
92 | // enable peripheral clock
|
93 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
|
94 |
|
95 | /* configure SPI3 in Mode 0
|
96 | * CPOL = 0 --> clock is low when idle
|
97 | * CPHA = 0 --> data is sampled at the first edge
|
98 | */
|
99 | SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
|
100 | SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
|
101 | SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
|
102 | SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
|
103 | SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // data sampled at first edge
|
104 | // SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
|
105 | // SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // data sampled at first edge
|
106 | SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set // set the NSS management to internal and pull internal NSS high
|
107 | SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //256; //128; //SPI_BaudRatePrescaler_256; //SPI_BaudRatePrescaler_32; //SPI_BaudRatePrescaler_16; // should be 5 mhz;;; SPI_BaudRatePrescaler_4; // SPI frequency is APB2 frequency / 4
|
108 | SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
|
109 | SPI_Init(SPI3, &SPI_InitStruct);
|
110 |
|
111 | SPI_Cmd(SPI3, ENABLE); // enable SPI3
|
112 | }
|