Forum: Mikrocontroller und Digitale Elektronik I2C Problem mit Cortex M3


von Mike R. (thesealion)


Lesenswert?

Hi,

ich versuche gerade eine EEPROM (24C64) Leseroutine auf einem STM32F102 
zu schreiben, aber leider will sie noch nicht unter allen Umständen 
funktionieren.

Wenn ich die Routine einzeln oder im Debugger aufruf liefert sie die 
erwarteten Werte zurück und ich kann die Kommunikation auch auf dem Oszi 
verfolgen.

Wenn ich jetzt aber versuche in einer Schleife mehrere Werte 
hintereinander zu lesen, dann bekomme ich irgendwann keine Antworten 
mehr, da die Routine an der gekennzeichneten Stelle abbricht.

Auf dem Oszi sehe ich, das die letzte Kommunikation mit einem Stop 
beendet wird und das auch wieder ein neues Start gesendet wird, danach 
kommt allerdings nichts weiter.

Hat jemand eine Idee, warum das so ist?

Gruß Mike
1
TestStatus read_byte_24CXX(I2C_Master_t *i2c, uint8_t DeviceType, uint8_t DeviceAddress, uint8_t * stuff, uint16_t memAddr)
2
   {  
3
   uint8_t slaveAddr;
4
   uint32_t TimeOut = 10;
5
  
6
   
7
   if (DeviceType == EEPROM_24C04)
8
      {
9
      if (memAddr >= 512)
10
        return FAILED;
11
      
12
      // Hardwarepins sind nur E1 und E2 vorhanden, E0 wird zur Adressierung verwendet
13
      slaveAddr  = 0xA0 + ((DeviceAddress&0x02)<<2);      
14
      
15
      if (memAddr >= 256)
16
        slaveAddr += 0x02;      
17
      }
18
   else
19
      {
20
      if (memAddr >= 8192)
21
        return FAILED;
22
      
23
      slaveAddr  = 0xA0 + ((DeviceAddress&0x07)<<1);      
24
      }
25
  
26
   TimeOut += SystemTicks;
27
   /*----- Transmission Phase -----*/
28
 
29
   /* Send I2C1 START condition */
30
   I2C_GenerateSTART(i2c->I2Cx, ENABLE);  
31
   
32
   /* Test on I2C1 EV5 and clear it */
33
   while(!I2C_CheckEvent(i2c->I2Cx, I2C_EVENT_MASTER_MODE_SELECT))  /* EV5 */
34
      {
35
      if(TimeOut == SystemTicks)
36
         {
37
         SET_I2C_NEW_ERROR(*i2c);
38
         return FAILED; 
39
         }
40
      }
41
  
42
   /* Send slave address for write */
43
   I2C_Send7bitAddress(i2c->I2Cx, slaveAddr, I2C_Direction_Transmitter);
44
  
45
   /* Test on I2C1 EV6 and clear it */
46
   while(!I2C_CheckEvent(i2c->I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
47
      {
48
      if(TimeOut == SystemTicks)
49
         {
50
// HIER TRITT EIN FEHLER AUF UND DIE ABARBEITUNG WIRD ABGEBROCHEN 
51
         SET_I2C_NEW_ERROR(*i2c);           
52
         return FAILED; 
53
         }
54
      }
55
   
56
   if (DeviceType == EEPROM_24C64)
57
      {
58
      /* Send the specified register data pointer */
59
      I2C_SendData(i2c->I2Cx, (uint8_t) (memAddr >> 8));
60
  
61
      /* Test on I2C1 EV8 and clear it */
62
      while(!I2C_CheckEvent(i2c->I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
63
         {
64
         if(TimeOut == SystemTicks) 
65
            {
66
            SET_I2C_NEW_ERROR(*i2c);              
67
            return FAILED; 
68
            }
69
         }
70
      }
71
72
   /* Send the specified register data pointer */
73
   I2C_SendData(i2c->I2Cx, (uint8_t)(memAddr));
74
  
75
   /* Test on I2C1 EV8 and clear it */
76
   while(!I2C_CheckEvent(i2c->I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
77
      {
78
      if(TimeOut == SystemTicks) 
79
         {
80
         SET_I2C_NEW_ERROR(*i2c);           
81
         return FAILED; 
82
         }
83
      }
84
85
  /*----- Recieve Phase -----*/
86
  /* Send I2C1 START condition */
87
  I2C_GenerateSTART(i2c->I2Cx, ENABLE);
88
  
89
   /* Test on I2C1 EV5 and clear it */
90
   while(!I2C_CheckEvent(i2c->I2Cx, I2C_EVENT_MASTER_MODE_SELECT))  /* EV5 */
91
      {
92
      if(TimeOut == SystemTicks)
93
         {
94
         SET_I2C_NEW_ERROR(*i2c);           
95
         return FAILED; 
96
         }
97
      }
98
  
99
   /* Send STAD5627R slave address for write */
100
   I2C_Send7bitAddress(i2c->I2Cx, slaveAddr, I2C_Direction_Receiver);
101
  
102
   /* Test on I2C1 EV6 and clear it */
103
   while(!I2C_CheckEvent(i2c->I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
104
      {
105
      if(TimeOut == SystemTicks) 
106
         {
107
         SET_I2C_NEW_ERROR(*i2c);           
108
         return FAILED; 
109
         }
110
      }
111
112
   /* Disable I2C1 acknowledgement */
113
   I2C_AcknowledgeConfig(i2c->I2Cx, DISABLE);
114
  
115
   /* Send I2C1 STOP Condition */
116
   I2C_GenerateSTOP(i2c->I2Cx, ENABLE);
117
  
118
   /* Test on EV7 and clear it */
119
   while(!I2C_CheckEvent(i2c->I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))  /* EV7 */
120
      {
121
      if(TimeOut == SystemTicks) 
122
         {
123
         SET_I2C_NEW_ERROR(*i2c);              
124
         return FAILED; 
125
         }
126
      }
127
      
128
   /* Store I2C1 received data */
129
   *stuff = I2C_ReceiveData(i2c->I2Cx) ;
130
      
131
   return PASSED;
132
   }

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.