Forum: Mikrocontroller und Digitale Elektronik stm32 cortex SPI und I2C funktionieren nicht gemeinsam


von Sylvia H. (sandy)


Lesenswert?

Hi,
ich habe folgendes Problem:
Auf meinem cortex STM32f103 habe ich zuerst SPI2 initialisiert:
1
void  SPI2_Init(void)
2
  {
3
  SPI_InitTypeDef  SPI_InitStructure;
4
  GPIO_InitTypeDef GPIO_InitStructure;
5
6
  /* Enable SPI2 and GPIOB clocks */
7
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
8
  RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
9
10
  // Input MISO PB14 as Intput
11
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_14;
12
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
13
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
14
  GPIO_Init(GPIOB, &GPIO_InitStructure);
15
16
  /* Configure SPI2 pins: NSS, SCK,  and MOSI */
17
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12| GPIO_Pin_13 | GPIO_Pin_15;
18
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
19
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
20
  GPIO_Init(GPIOA, &GPIO_InitStructure);
21
22
  /* SPI2 configuration */
23
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
24
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
25
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
26
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
27
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
28
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
29
  //SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
30
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
31
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
32
  SPI_InitStructure.SPI_CRCPolynomial = 7;
33
  SPI_Init(SPI2, &SPI_InitStructure);
34
35
  /* Enable SPI2  */
36
  SPI_Cmd(SPI2, ENABLE);
37
  }
und danach I2C2:
1
void I2C_init(void)
2
{
3
 I2C_DeInit(I2C2);
4
5
    // I2C configuration
6
    I2C_InitStructure2.I2C_Mode = I2C_Mode_I2C;
7
    I2C_InitStructure2.I2C_DutyCycle = I2C_DutyCycle_2;
8
    I2C_InitStructure2.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS7;
9
    I2C_InitStructure2.I2C_Ack = I2C_Ack_Enable;
10
    I2C_InitStructure2.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
11
    I2C_InitStructure2.I2C_ClockSpeed = I2C_Speed;
12
13
    //Apply I2C configuration 
14
     I2C_Init(I2C2, &I2C_InitStructure2);
15
     // I2C Peripheral Enable
16
     I2C_Cmd(I2C2, ENABLE);
17
18
     //GPIO Configs
19
      GPIO_InitTypeDef  GPIO_InitStructure2;
20
21
     // GPIOB clock enable 
22
       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
23
     // I2C2 clock enable
24
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
25
26
   // Configure I2C2 pins: SCL and SDA for the administrative bus
27
    GPIO_InitStructure2.GPIO_Pin =  GPIO_Pin_10 | GPIO_Pin_11;
28
    GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
29
    GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_OD;
30
    GPIO_Init(GPIOB, &GPIO_InitStructure2);
31
}

Wenn ich nur SPI2_Init() aufrufe, funktioniert der SPI Bus ohne 
Probleme.
Wenn aber SPI2_Init() und I2C_init() nacheinander aufgerufen wird, (egal 
in welcher Reihenfolge), dann Streikt der SPI Bus.
Ich habe auch schon zuerst SPI_init() aufgerufen, und in der I2C_init() 
Methode das GPIB clock enable auskommentiert,
das hat aber auch nichts genützt.
Hat jemand ne Idee?

Gruß
Sylvia

von (prx) A. K. (prx)


Lesenswert?

Liegt SPI wirklich auf einem Mix aus PORT A und Port B?

von Sylvia H. (sandy)


Lesenswert?

Entschuldige, ich verstehe dich nicht,  SPI2 und I2C2 sind doch beide an 
Port B ??????
Wann verwend ich Port A?

von (prx) A. K. (prx)


Lesenswert?

Die I2C-Ports einzuschalten nachdem I2C initialisiert ist, das ist 
sinnvoll. Aber I2C erst mit Takt versorgen, nachdem man versuchte es zu 
initialisieren, das nicht.

> Wann verwend ich Port A?

Dann schärf mal die Augen, putz die Brille und lies SPI_Init nochmal. 
Notfalls laut der Katze vorlesen.

von Sylvia H. (sandy)


Lesenswert?

Sylvia H. schrieb:
> /* Enable SPI2 and GPIOB clocks */
>     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
>   RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

Entschuldige, ich stehe auf dem Schlauch, wo ist im Text Port A ?

von (prx) A. K. (prx)


Lesenswert?

Sylvia H. schrieb:

> Entschuldige, ich stehe auf dem Schlauch, wo ist im Text Port A ?
1
  /* Configure SPI2 pins: NSS, SCK,  and MOSI */
2
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12| GPIO_Pin_13 | GPIO_Pin_15;
3
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
4
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
5
  GPIO_Init(GPIOA, &GPIO_InitStructure);
6
              --^--

von Sylvia H. (sandy)


Lesenswert?

oh neeeeeeeee
(und die Katze nickt mit dem Kopf ;) )

von Sylvia H. (sandy)


Lesenswert?

ok, alles verbesert, SPI2 liegt jetzt ganz in Port B,
I2C bekommt seinen Takt am Anfang der Initialisierung,
aber trotzdem laufen SPI und I2C nicht gemeinsam.
Woran könnts liegen?

von (prx) A. K. (prx)


Lesenswert?

Nun hörts bei mir auf, weil ich mit der StdPeriphLib nichts am Hut habe.

Ausserdem: Was heisst "läuft nicht" genau?

von Sylvia H. (sandy)


Lesenswert?

A. K. schrieb:
> Ausserdem: Was heisst "läuft nicht" genau?

ich schicke in einer Endlosschleife eine 0x05 auf den SPI Bus (zu 
testzwecken).
1
//SendeMethode:
2
unsigned char  SPI2_ReadWrite(unsigned char writedat)
3
  {
4
  /* Loop while DR register in not emplty */
5
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
6
7
  /* Send byte through the SPI1 peripheral */
8
  SPI_I2S_SendData(SPI2, writedat);
9
10
  /* Wait to receive a byte */
11
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
12
13
  /* Return the byte read from the SPI bus */
14
  return SPI_I2S_ReceiveData(SPI2);
15
  }
(Natürlich ziehe ich vorher den NSS auf 0, und gebe ihn nach den Senden 
wieder frei)

Wenn ich die Methode SPI2_Init() aufrufe, und danach dann die 0x05 
losschicke, kann ich auf dem Oszi schön die 0x05 beobachten wie sie auf 
die Reise geht (am MOSI Pin).
Rufe ich nun zusätzlich die I2C_init() Methode auf, ist sofort tote Hose 
am MOSI Pin, der SPI schickt nichts mehr los.

Seltsamerweise, ist da noch ein Phänomen:
Wenn ich die SPI2_Init() Methode auskommentiere, und nur die  I2C_init() 
Methode aufrufe, kann ich trotzdem die 0x05 am MOSI Pin mit meinem Oszi 
ablesen.....
versteh ich auch nicht so richtig......

von Sylvia H. (sandy)


Lesenswert?

Sylvia H. schrieb:
> Seltsamerweise, ist da noch ein Phänomen:
> Wenn ich die SPI2_Init() Methode auskommentiere, und nur die  I2C_init()
> Methode aufrufe, kann ich trotzdem die 0x05 am MOSI Pin mit meinem Oszi
> ablesen.....
> versteh ich auch nicht so richtig......

Kommando zurück,
wenn die SPI Init Methode auskommentiert ist, wird auch nichts aus MOSI 
rausgeschickt.
War ein Irrtum von mir.

Aber nichts desdo trotz:
Warum sendet MOSI nichts, wenn die Methoden so aufgerufen werden:
1
int main(void) {
2
3
  // this sets up the oscilator
4
  SystemInit();
5
  
6
    // Initialization
7
        SPI2_Init();
8
        I2C_config();
9
10
  while(1)
11
  {
12
              
13
   // Reset PB12
14
    GPIOB->BRR  = GPIO_Pin_12;
15
    SPI2_ReadWrite(0x05);
16
    // Set PB12
17
    GPIOB->BSRR = GPIO_Pin_12;
18
          
19
    }
20
}

von Klaus S. (skibby)


Lesenswert?

Hallo Sylvia,

wo hast Du denn "I2C_InitStructure2" deklariert ?
In Deinen Code-Fragmenten habe ich es nicht entdeckt.
Ich hatte mal ein ähnliches Problem und ich habe es glaube ich gelöst 
bekommen indem ich "SPI_InitStructure","GPIO_InitStructure" etc.
global definiert habe.

Gruß, Klaus

von Sylvia H. (sandy)


Lesenswert?

Klaus Skibowski schrieb:
> wo hast Du denn "I2C_InitStructure2" deklariert ?

mein Cod ist nicht ganz komplett, ich inititalisiere in der I2C_init 
Methode auch noch I2C1, das habe ich aber der Übersicht halber 
weggelassen. Dabei habe ich auch vergessen, die I2C_InitStructure2 mit 
in mein Codfragement zu kopieren.....
Die Structure wird aber am Anfang der Methode deklariert.

Ich habe noch was rausgefunden, vielleicht kann jemand was damit 
anfangen (vielleicht auch jemand, der nichts mit der StdPeriphLib am Hut 
hat ;):

Während der Initialisierung des I2C Busses springt das Programm jedesmal 
in  diese Infiniteloop routine :
1
/**
2
 * @brief  This is the code that gets called when the processor receives an
3
 *         unexpected interrupt.  This simply enters an infinite loop, preserving
4
 *         the system state for examination by a debugger.
5
 *
6
 * @param  None
7
 * @retval : None
8
*/
9
    .section  .text.Default_Handler,"ax",%progbits
10
Default_Handler:
11
Infinite_Loop:
12
  b  Infinite_Loop
13
  .size  Default_Handler, .-Default_Handler

Nämlich bei der Abfrage
1
 /* Get PCLK2 prescaler */
2
  tmp = RCC->CFGR & CFGR_PPRE2_Set_Mask;

in der Methode RCC_GetClocksFreq() aus der Lib stm3sf10x_rcc.c

Kann da jemand was mit anfangen?

von (prx) A. K. (prx)


Lesenswert?

Sylvia H. schrieb:

> Während der Initialisierung des I2C Busses springt das Programm jedesmal
> in  diese Infiniteloop routine :

Diese Schleife steht für alle nicht explizit definierten Interrupt- und 
Exception-Handler. Bei einfachem Code ohne Pfriemelei an Priority-Levels 
und ohne Interrupts ist das meistens der HardError, ausgelöst durch 
Zugriff auf nicht existierende Adressen, Call/Return in den Wald und 
sowas in der Art.

> Nämlich bei der Abfrage
>   tmp = RCC->CFGR & CFGR_PPRE2_Set_Mask;

Wenn es wirklich exakt diese Zeile ist, und nicht eine davor oder so 
(kann bei Exceptions leicht passieren), dann hiesse es, dass der Pointer 
RCC in den Wald zeigt.

von Klaus S. (skibby)


Lesenswert?

hast Du vielleicht das & beim Aufruf vergessen ?
"RCC_GetClocksFreq(&RCC_ClockFreq);"

von Sylvia H. (sandy)


Lesenswert?

Klaus Skibowski schrieb:
> hast Du vielleicht das & beim Aufruf vergessen ?
> "RCC_GetClocksFreq(&RCC_ClockFreq);"

nee, das & steht da....

von Jan (Gast)


Lesenswert?

Interrupt Handler richtig geschrieben ?

Ich denke er löst eher in dieser Programmzeile aus weil dann dein I2C so 
weit ist und ein Ready als Interrupt Routine sendet.

Bei solchen Problemen bietet es sich immer an das komplette 
Programm(natürliche nicht die StdPeriphlib) zu posten. Wo ist denn dein 
NVIC ?

mfg Jan

von Sylvia H. (sandy)


Lesenswert?

Jan schrieb:
> Interrupt Handler richtig geschrieben ?

ich habe gar keinen NIVC geschrieben, da ich keinen Interrupt benutze 
sondern alles gepollt von statten gehen muß.

von (prx) A. K. (prx)


Lesenswert?

Ohne Code gehts hier vermutlich nicht wirklich weiter.

von Sylvia H. (sandy)


Lesenswert?

da gibts nicht viel mehr Code,

SPI.c:
1
#include "../Std_lib/stm32f10x_SPI.h"
2
#include "../Std_lib/stm32f10x_GPIO.h"
3
#include "../Std_lib/stm32f10x_RCC.h"
4
5
/**
6
 *
7
 */
8
void  SPI1_Init(void)
9
  {
10
  SPI_InitTypeDef  SPI_InitStructure;
11
  GPIO_InitTypeDef GPIO_InitStructure;
12
  
13
  /* Enable SPI1 and GPIOA clocks */
14
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
15
  
16
  /* Configure SPI1 pins: NSS, SCK, MISO and MOSI */
17
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
18
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
19
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
20
  GPIO_Init(GPIOA, &GPIO_InitStructure);
21
  
22
  /* SPI1 configuration */ 
23
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
24
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
25
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
26
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
27
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
28
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
29
  //SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;  für den Advaark zu schnell!
30
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
31
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
32
  SPI_InitStructure.SPI_CRCPolynomial = 7;
33
  SPI_Init(SPI1, &SPI_InitStructure);
34
  
35
  /* Enable SPI1  */
36
  SPI_Cmd(SPI1, ENABLE); 
37
  }
38
39
/**
40
 *
41
 */
42
void  SPI2_Init(void)
43
{
44
45
  SPI_InitTypeDef  SPI_InitStructure;
46
  GPIO_InitTypeDef GPIO_InitStructure;
47
48
  // Enable SPI2 and GPIOB clocks
49
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
50
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
51
52
  // Input MISO PB14 as Intput
53
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_14;
54
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
55
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
56
  GPIO_Init(GPIOB, &GPIO_InitStructure);
57
58
  // Configure SPI2 pins: NSS, SCK,  and MOSI
59
  //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12| GPIO_Pin_13 | GPIO_Pin_15;
60
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
61
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
62
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
63
  GPIO_Init(GPIOB, &GPIO_InitStructure);
64
65
  // SPI1 configuration
66
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
67
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
68
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
69
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
70
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
71
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
72
  //SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
73
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
74
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
75
  SPI_InitStructure.SPI_CRCPolynomial = 7;
76
  SPI_Init(SPI2, &SPI_InitStructure);
77
78
  // Enable SPI1
79
  SPI_Cmd(SPI2, ENABLE);
80
  }
81
82
83
unsigned char  SPI1_ReadWrite(unsigned char writedat)
84
  {
85
  /* Loop while DR register in not emplty */
86
  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
87
  
88
  /* Send byte through the SPI1 peripheral */
89
  SPI_I2S_SendData(SPI1, writedat);
90
  
91
  /* Wait to receive a byte */
92
  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
93
  
94
  /* Return the byte read from the SPI bus */
95
  return SPI_I2S_ReceiveData(SPI1);
96
  }
97
98
unsigned char  SPI2_ReadWrite(unsigned char writedat)
99
  {
100
  /* Loop while DR register in not emplty */
101
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
102
103
  /* Send byte through the SPI1 peripheral */
104
  SPI_I2S_SendData(SPI2, writedat);
105
106
  /* Wait to receive a byte */
107
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
108
109
  /* Return the byte read from the SPI bus */
110
  return SPI_I2S_ReceiveData(SPI2);
111
  }

I2C.C:
1
#include "i2c.h"
2
#include "stm32f10x.h"
3
#include "../lib/stm32f10x_conf.h"
4
#include "../Std_lib/stm32f10x_i2c.h"
5
6
#define BUFFER uint8
7
8
9
/**
10
  * @brief  Initialize all stuff you need for I2C
11
  * @retval None.
12
  */
13
void I2C_config(void)
14
{
15
  I2C_InitTypeDef  I2C_InitStructure1;
16
  I2C_InitTypeDef  I2C_InitStructure2;
17
18
 //zuerste I2C1 initialisieren------------------------------------------------
19
  I2C_DeInit(I2C1);
20
21
  /* I2C configuration */
22
  I2C_InitStructure1.I2C_Mode = I2C_Mode_I2C;
23
  I2C_InitStructure1.I2C_DutyCycle = I2C_DutyCycle_2;
24
  I2C_InitStructure1.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;
25
  I2C_InitStructure1.I2C_Ack = I2C_Ack_Enable;
26
  I2C_InitStructure1.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
27
  I2C_InitStructure1.I2C_ClockSpeed = I2C_Speed;
28
29
30
  /* Apply I2C configuration (war in der vorlage erst nach I2C_Cmd()) */
31
  I2C_Init(I2C1, &I2C_InitStructure1);
32
  /* I2C Peripheral Enable */
33
  I2C_Cmd(I2C1, ENABLE);
34
35
  //GPIO Configs
36
  GPIO_InitTypeDef  GPIO_InitStructure1;
37
38
   /* GPIOB clock enable */
39
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //??????????????wurde schon bei SPI Init erledigt
40
    /* I2C1 clock enable */
41
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
42
43
   /* Configure I2C1 pins: SCL and SDA to communicate with outside */
44
   GPIO_InitStructure1.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
45
   GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
46
   GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_AF_OD;
47
   GPIO_Init(GPIOB, &GPIO_InitStructure1);
48
49
50
   //danach I2C2 initialisieren------------------------------------------------
51
   I2C_DeInit(I2C2);
52
53
   // GPIOB clock enable  wurde oben schon erledigt
54
       // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
55
       // I2C2 clock enable
56
       RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
57
58
    // I2C configuration
59
    I2C_InitStructure2.I2C_Mode = I2C_Mode_I2C;
60
    I2C_InitStructure2.I2C_DutyCycle = I2C_DutyCycle_2;
61
    I2C_InitStructure2.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS7;
62
    I2C_InitStructure2.I2C_Ack = I2C_Ack_Enable;
63
    I2C_InitStructure2.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
64
    I2C_InitStructure2.I2C_ClockSpeed = I2C_Speed;
65
66
    //Apply I2C configuration (war in der vorlage erst nach I2C_Cmd())
67
     I2C_Init(I2C2, &I2C_InitStructure2);
68
     // I2C Peripheral Enable
69
     I2C_Cmd(I2C2, ENABLE);
70
71
     //GPIO Configs
72
      GPIO_InitTypeDef  GPIO_InitStructure2;
73
74
75
76
   // Configure I2C2 pins: SCL and SDA for the administrative bus
77
    GPIO_InitStructure2.GPIO_Pin =  GPIO_Pin_10 | GPIO_Pin_11;
78
    GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
79
    GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_OD;
80
    GPIO_Init(GPIOB, &GPIO_InitStructure2);
81
82
83
}

main:
1
#include "stm32f10x.h"
2
#include "lib/i2c.h"
3
#include "lib/SPI.H"
4
5
6
int main(void) {
7
8
  // this sets up the oscilator
9
  SystemInit();
10
  
11
    // Initialization
12
        SPI2_Init();
13
        I2C_config();
14
15
  while(1)
16
  {
17
              
18
   // Reset PB12
19
    GPIOB->BRR  = GPIO_Pin_12;
20
    SPI2_ReadWrite(0x05);
21
    // Set PB12
22
    GPIOB->BSRR = GPIO_Pin_12;
23
          
24
    }
25
}

von Matthias K. (matthiask)


Lesenswert?

Sylvia H. schrieb:
> // Configure I2C2 pins: SCL and SDA for the administrative bus
>     GPIO_InitStructure2.GPIO_Pin =  GPIO_Pin_10 | GPIO_Pin_11;
>     GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
>     GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_OD;
>     GPIO_Init(GPIOB, &GPIO_InitStructure2);

Das würde ich vor der IC2 Konfiguration und Freigabe machen.

von (prx) A. K. (prx)


Lesenswert?

In der I2C Initialisierung wird er wohl rauskriegen müssen, mit welchem 
Takt der Controller arbeitet, um den I2C-Takt entsprechend zu 
programmieren. Und dies scheint nicht zu funktionieren. Dazu müsste man 
wohl mal in die entsprechende Routine reinsehen, das ist wohl mit der 
Exception, um festzustallen, woran sie scheitert. So könnte auch eine 
Division durch 0 die Exception auslösen.

von (prx) A. K. (prx)


Lesenswert?

Matthias K. schrieb:

> Das würde ich vor der IC2 Konfiguration und Freigabe machen.

Und ich würde das eben nicht davor machen, weil ich mich exakt mit 
dieser Frage ein Zeitlang rumärgern durfte.

Es führt nämlich dazu, dass die beiden I2C-Pins erst einmal aktiv eine 0 
ausgeben, bevor die Initialisierung des I2C-Moduls sie dann deaktiviert. 
Nicht jede I2C-Peripherie ist darüber glücklich.

von Sylvia H. (sandy)


Lesenswert?

Hi Leute,
das klingt jetzt vielleicht ein bischen komisch,
aber auf einmal gehts.....
Ich habe nicht den ganzen Code gepostet, weil das zu unübersichtlich 
wird. Das ganze läuft unter FreeRTOS, und CAN spielt auch noch eine 
Rolle.
Da ich dachte, ich brauche nur SPI und I2C für mein Project, hatte ich 
die CAN initialisierung nicht berücksichtigt. Aber dort steht wohl noch 
etwas wichtiges, was mein Project auch beeinflusst. Wenn ich die CAN 
initialisierung einkommentiere, dann läuft alles, ich muss nur noch 
herausfinden, was genau das ist.....
Vielen Dank für eure Unterstützung, das hat mir sehr geholfen. Ich war 
kurz vor dem Verzweifeln, da ist es gut, wenn ein paar Kollegen einem 
unter die Arme greifen und mithelfen.
Bei euch kann man noch viel Lernen!
Gruß
Sylvia

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.