Forum: Mikrocontroller und Digitale Elektronik STM32F05 SPI 3 Wire Verbindung mit Accelerometer


von Simon S. (simonsz)


Angehängte Dateien:

Lesenswert?

Hi,
ich versuche momentan einen Sparkfun LSM303C 3D accelerometer und 
Magnetometer mit einem STM32F051R8T6 board via 3 wire SPI auszulesen. 
Ich bin  neu dabei beim Mikrocontroller Programmieren.

Als Programmieroberfläche benutze ich CooCox CoIDE

Zur Kommunikation wird die SPI2 Peripherie verwendet.


Die Pins habe ich wie folgt angesteckt.
Board           Sensor
PB11            CS_MAG
PB12            CS_XL
PB13(CLK)       SCL/SCLK
PB14 (MISO)     Nicht Verbunden wegen 3 wire
PB15(MOSI)      SDA/SDI/SDO

PB11 und PB12 dienen als  Slave Select.


Zunächst einmal möchte ich nur das "WHO_AM_I" Register des Sensors 
auslesen, um zu testen, ob die Kommunikation funktioniert.

Die Funktion für die Kommunikation habe ich von folgender Seite:

http://www.ba0sh1.com/2014/05/31/howto-use-stm32-spi-half-duplex-mode/


Problem ist, dass beim Aufruf der Funktion  send_and_read_byte   die 
erste while - Schleife

 while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

zu einer Endloschschleife wird. Ich habe die Vermutung, dass die Adresse 
0x0F(entspricht laut Datenblatt dem "WHO_AM_I" Register) garnicht erst 
gesendet wird, weshalb auch der SPI_I2S_FLAG_TXE nie gesetzt wird. Ich 
kann nur leider nicht sagen, warum dass Register anscheinend nicht 
gesendet wird.

Ich wäre schon sehr dankbar, wenn mir wer sagen könnte, ob die Pins 
überhaupt richtig verbunden sind und die Definitionen richtig 
eingestellt sind.
1
#include "semihosting.h"
2
#include <stdio.h>
3
#include <stm32f0xx_conf.h>
4
5
GPIO_InitTypeDef GPIO_InitStructure;                  
6
SPI_InitTypeDef SPI_InitStructure;                     
7
8
9
void SPI_Configure(void)
10
{
11
    SPI_InitTypeDef SPI_InitStructure;
12
13
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);        
14
                        
15
    SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;       
16
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;  //
17
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;          
18
    SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;               
19
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;             
20
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;              
21
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;  // 
22
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;          
23
    SPI_InitStructure.SPI_CRCPolynomial = 7;              
24
    SPI_Init(SPI2, &SPI_InitStructure);                  
25
    SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);
26
    SPI_Cmd(SPI2, ENABLE);
27
28
29
30
}
31
32
33
/*void LED_init(void){
34
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);        
35
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
36
    GPIO_InitStructure.GPIO_Pin=(GPIO_Pin_9 | GPIO_Pin_8);
37
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;          
38
    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
39
    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
40
    GPIO_Init(GPIOC,&GPIO_InitStructure);
41
42
}*/
43
44
void CS_config1(void)                          
45
{
46
47
    // Definition von CLK und MOSI
48
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;            
49
50
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
51
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
52
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
53
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
54
    GPIO_Init(GPIOB, &GPIO_InitStructure);
55
56
57
58
        // Defintion von Slave Select Pins
59
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;          
60
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
61
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
62
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
63
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
64
65
       GPIO_Init(GPIOB, &GPIO_InitStructure);
66
67
68
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_0);
69
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_0);
70
71
72
}
73
74
uint8_t send_and_read_byte(uint8_t cmd)                  // Funktionsdefinition
75
{
76
  // cmd ist die zu sendende Variable
77
  uint8_t result;  
78
  GPIO_SetBits(GPIOB, GPIO_Pin_12);    
79
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);     //wait buffer empty
80
  SPI_SendData8(SPI2, cmd);
81
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);       //wait finish sending                                  // Read receiving FIFO until it is empty
82
  while (SPI_GetReceptionFIFOStatus(SPI2) != SPI_ReceptionFIFOStatus_Empty) {
83
    SPI_ReceiveData8(SPI2);
84
  }
85
  SPI_BiDirectionalLineConfig(SPI2, SPI_Direction_Rx);
86
  while (!(SPI2->SR & SPI_I2S_FLAG_RXNE));                 // wait data received
87
  GPIO_ResetBits(GPIOB, GPIO_Pin_12);                   // CS high
88
  SPI2->CR1 |= SPI_Direction_Tx;                      // Set Tx mode to stop Rx clock
89
  result = SPI_ReceiveData8(SPI2);
90
  return result;
91
}
92
93
int main(void)
94
{
95
  uint8_t counter=0;
96
  //LED_init();
97
  //input_config();
98
  //CS_config2();
99
  CS_config1();
100
  SPI_Configure();
101
102
        GPIO_ResetBits(GPIOB, GPIO_Pin_11);
103
104
  while(1) {
105
    
106
    counter=send_and_read_byte(0x0F);
107
108
           }
109
110
}

Vielen Dank schon einmal
Viele Grüße,

Simon

von Long Don (Gast)


Lesenswert?

Der Chipselect ist Low-Aktiv.
Bist Du sicher, daß

  GPIO_SetBits(GPIOB, GPIO_Pin_12);

den Pin auf 0V schaltet?
(Ich kenne die gruselige ST-Malware nicht)

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.