STM kommuniziert mit einem externen SPI EEPROM. Logic Analyzer zeigt mir auf der MISO Leitung die Daten, die ich zuvor über MOSI geschrieben habe. Leider erzählt mir die Empfangsfunktion, dass 0xFF empfangen wurde. Ich habe keine Idee woran das liegen mag. Beim Einlesen sende ich wie "vorgeschrieben" Dummy-Daten.
1 | void
|
2 | Spi2Init (void) { |
3 | |
4 | GPIO_InitTypeDef GPIO_InitStructure; |
5 | SPI_InitTypeDef SPI_InitStructure; |
6 | NVIC_InitTypeDef NVIC_InitStructure; |
7 | |
8 | // Disable SPI
|
9 | SPI_Cmd(SPI2, DISABLE); |
10 | |
11 | /* GPIOB Peripheral clock enable */
|
12 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); |
13 | |
14 | /* SPI2 Peripheral clock enable */
|
15 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 | RCC_APB2Periph_AFIO, ENABLE); |
16 | |
17 | /* Configure SPI2 pins: NSS, SCK, MISO and MOSI -------------------------------*/
|
18 | // SPI2: CS
|
19 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; |
20 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; |
21 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; |
22 | GPIO_Init(GPIOB, &GPIO_InitStructure); |
23 | |
24 | // SPI2: SCK and MOSI push-pull
|
25 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15 | GPIO_Pin_14; |
26 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
27 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; |
28 | GPIO_Init(GPIOB, &GPIO_InitStructure); |
29 | |
30 | // SPI2: Configuration
|
31 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; |
32 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master; |
33 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; |
34 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; |
35 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; |
36 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; |
37 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; |
38 | SPI_InitStructure.SPI_CRCPolynomial = 7; |
39 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // ((72/2)/8)= 4.5MHz |
40 | SPI_Init(SPI2, &SPI_InitStructure); |
41 | SPI_Cmd(SPI2, ENABLE); |
42 | }
|
Die Einleseroutine:
1 | uint8_t
|
2 | SPI_ReadByte (void) { |
3 | |
4 | // send dummy byte (0xFF)
|
5 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); |
6 | SPI_I2S_SendData(SPI2, 0x00); |
7 | |
8 | // read data
|
9 | while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); |
10 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET); |
11 | |
12 | return SPI_I2S_ReceiveData(SPI2); |
13 | }
|