EmbDev.net

Forum: ARM programming with GCC/GNU tools Hang - is there anything wrong with this code?


von Yee Y. (Company: Infowave) (armgcc)


Rate this post
useful
not useful
Hi, I am using ublox 5 GPS and Atmel AT91SAM7S512.

basically, the microcontroller needs to perform GPRS vehicle tracking. 
Not sure why. Everything runs fine but sometimes, the device just hangs 
occasionally after some time.

When i compile eventhandler fn, for GPSBufferWrite(pPsvl, 
sizeof(GPS_PSVL)), it returns me with a

"passing arg 1 of `GPSBufferWrite' from incompatible pointer type"

wonder if someone can help me look through these codes and see if there 
is any problem.

The coding is compiling using embest IDE.

thanks a lot
1
ECOME_STATUS GPSBufferWrite(char *data, int size)
2
{
3
  //LOG2("GPSBufferWrite: data = %s, size = %d", data, size);
4
  //if (ReadGPSLogBufferPointer(&gpsPtr) == COM_SUCCESS)
5
  {  
6
    //LOG2("GPSBufferWrite: before, pWrite = %d, pRead = %d", gpsPtr.pWrite, gpsPtr.pRead);
7
      if ((gpsPtr.pWrite + size) <= MAX_GPS_BUFFER_SIZE)
8
      {
9
      if (EFLASH_Write1(data, DATAFLASH_GPS_FIX_LOG_BUFFERING_DATA_ADDR + gpsPtr.pWrite, size) == COM_SUCCESS)
10
      {
11
#if 0 
12
        // read back verification
13
        char tmpbuf[200]="";
14
      EFLASH_Read(DATAFLASH_GPS_FIX_LOG_BUFFERING_DATA_ADDR + gpsPtr.pWrite, tmpbuf, size);
15
        if (strncmp(tmpbuf, data, size) != 0)
16
        LOG0("***** FLASHING ERROR *****");
17
18
 //tmpbuf[16] = 0x0;  
19
 //LOG2("GPSBufferWrite: pwrite = %d, data = %s", gpsPtr.pWrite, tmpbuf);  
20
#endif
21
        
22
 //LOG2("Before UpdatePointer, pWrite = %d, pRead = %d", gpsPtr.pWrite, gpsPtr.pRead);
23
        //data[16] = 0x0;  
24
        LOG1("pwrite=%d", gpsPtr.pWrite);  
25
        
26
#if 1
27
        gpsPtr.pWrite += size;
28
        UpdateGPSLogBufferPointer(&gpsPtr);
29
#endif
30
        
31
        return (COM_SUCCESS);
32
        
33
      }
34
    }
35
    else
36
    {
37
  LOG1("GPSBufferWrite: (gpsPtr.pWrite + size) > MAX_GPS_BUFFER_SIZE, gpsPtr.pWrite = %d", gpsPtr.pWrite);
38
      //gpsPtr.pRead = 0;
39
      gpsPtr.pWrite = 0;
40
      if (EFLASH_Write1(data, DATAFLASH_GPS_FIX_LOG_BUFFERING_DATA_ADDR + gpsPtr.pWrite, size) == COM_SUCCESS)
41
      {
42
        gpsPtr.pWrite += size;
43
        UpdateGPSLogBufferPointer(&gpsPtr);
44
      }
45
      //LOG0("Reset write pointer to 0");
46
    }
47
  }
48
  
49
  return (COM_SUCCESS);
50
}
51
52
53
static GPS_PSVL eventPsvl={0};
54
extern GPS_PSVL psvl;
55
56
57
void Events_Handler(unsigned int rtcStatus)
58
{
59
  extern char ringing;
60
  static char prevRinging = 0; 
61
  unsigned char emergency3, tamper3, ignition3; 
62
  
63
  static char flag_triggered = 0;
64
  static char xSampleCount = 0;
65
  static char ySampleCount = 0;
66
  static char zSampleCount = 0;
67
  
68
  static char adPowerValue[8];
69
  static char batteryValue[8];
70
  static char adSampCount = 0;
71
  
72
  float xdata,ydata,zdata;
73
  unsigned short currSpeed;
74
  GPS_PSVL *pPsvl;
75
  char msgLength;
76
  char xbuf[24];
77
  //char ybuf[24];
78
  //char zbuf[24];
79
  //char mbuf[24];
80
  ESYSS_CONFIG_EVENTS_SETUP1 *p = &sysEventsConfig1;
81
  
82
  //if (rtcStatus & RTC_SEC_CHANGE_BIT)
83
  {
84
    memcpy(&eventPsvl,&psvl,sizeof(GPS_PSVL));
85
    pPsvl = &eventPsvl;  
86
  }
87
  //accelermeter
88
  if(sysEventsConfig1.enable & EVENT_ACCELERMETER)
89
  {
90
    char xValue =0;
91
    char yValue = 0;
92
    char zValue = 0;
93
    
94
    if(flag_triggered)
95
    {
96
    Accelerometor2(&xValue, &yValue, &zValue);
97
    xdata =255 * 0.66 * sysEventsConfig1.XLimited / 3.3;
98
    ydata =255 * 0.66 * sysEventsConfig1.YLimited / 3.3;
99
    zdata =255 * 0.66 * sysEventsConfig1.ZLimited / 3.3;
100
    //sprintf(xbuf,"%3.1f",xdata);
101
    //sprintf(ybuf,"%3.1f",ydata);
102
    //sprintf(zbuf,"%3.1f",zdata);
103
    //LOG3("xdata=%s,ydata=%s,zdata=%s",xbuf, ybuf, zbuf);
104
      if(xdata < xValue)
105
      {
106
        flag_triggered = 1;
107
        xSampleCount++;
108
      }
109
      if(ydata < yValue)
110
      {
111
        flag_triggered = 1;
112
        ySampleCount++;
113
      }
114
      if(zdata < zValue)
115
      {
116
        flag_triggered = 1;
117
        zSampleCount++;
118
      }
119
    }  
120
    
121
  }//accelermeter
122
  
123
  //every second
124
  if (rtcStatus & RTC_SEC_CHANGE_BIT)
125
  {
126
    //GPI
127
    emergency3 = (unsigned char)EIO_GetBit(0);
128
    //ETIME_DelayMsec(20);
129
    emergency3 = (unsigned char)EIO_GetBit(0);
130
    
131
    ignition3 = (unsigned char)EIO_GetBit(1);
132
    //ETIME_DelayMsec(20);
133
    ignition3 = (unsigned char)EIO_GetBit(1);
134
    
135
    tamper3 = (unsigned char)EIO_TamperingCheck();
136
    //ETIME_DelayMsec(20);  
137
    tamper3 = (unsigned char)EIO_TamperingCheck();  
138
    //emergnecy cal
139
    
140
    if(preEmergency == 2)
141
      preEmergency = emergency3;
142
    else if(emergency3 != preEmergency )
143
    {
144
      if(emergency3 == 1)
145
      {
146
        if(sysEventsConfig1.enable & EVENT_EMERG_CALL)
147
        {
148
          pPsvl->MsgType = EVENT_EMERG_CALL;
149
          pPsvl->MsgValue = 0;
150
          utc2LocalTime(pPsvl);  
151
          GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
152
          FileLog(pPsvl);
153
        }
154
      }  
155
      preEmergency = emergency3;
156
    }    
157
    
158
    //tamper
159
    if(preTamper == 2)
160
      preTamper =  tamper3;
161
    else if(tamper3 != preTamper)
162
    {
163
      if(tamper3 == 1)
164
      {
165
        if(sysEventsConfig1.enable & EVENT_TAMPER)
166
        {
167
          pPsvl->MsgType = EVENT_TAMPER;
168
          pPsvl->MsgValue = 0;
169
          utc2LocalTime(pPsvl);  
170
          GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
171
          FileLog(pPsvl);
172
        }
173
      }  
174
      preTamper =  tamper3;
175
    }
176
177
    //ignition
178
    if(preIgnition == 2)
179
      preIgnition = ignition3;
180
    else if(ignition3 != preIgnition)
181
    {
182
      //LOG0("ignition pin level changed");
183
      if(sysEventsConfig1.enable & EVENT_IGNITION)
184
      {
185
        LOG0("trigger ignition alert");
186
        pPsvl->MsgType = EVENT_IGNITION;
187
        pPsvl->MsgValue = ignition3;
188
        utc2LocalTime(pPsvl);  
189
        GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
190
        FileLog(pPsvl);
191
        
192
      }
193
      preIgnition = ignition3;
194
    }
195
            
196
    //over speed and geofence
197
    if( pPsvl->flag_fix )
198
    {
199
      //LOG0("gps fixed");
200
      if(sysEventsConfig1.enable & EVENT_OVER_SPEED)
201
      {
202
        currSpeed = pPsvl->SOG;  
203
        if(currSpeed > sysEventsConfig1.overspeed )
204
        {
205
          pPsvl->MsgType = EVENT_OVER_SPEED;
206
          pPsvl->MsgValue = 0;
207
          utc2LocalTime(pPsvl);  
208
          GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
209
          FileLog(pPsvl);
210
        }    
211
      }
212
      //geo fence enter
213
      static char firstenter = 0;
214
      static char fistexit = 0;
215
      static char exitcounter = 0;
216
      if((sysEventsConfig1.enable & EVENT_ENTER_GEOZONE) || (sysEventsConfig1.enable & EVENT_EXIT_GEOZONE))
217
      {
218
    unsigned int flag = sysGeofenceConfig1.EnableFlag;
219
    char count = sysGeofenceConfig1.NumberOfPoints;
220
        int i, temp = 0;
221
        float currDist = 0;
222
        for(i = 0; i < count; i++)
223
        {
224
          temp = 1 << i;
225
          if(flag & temp)
226
          {
227
            currDist = calcTwoPointsDistance(sysGeofenceConfig1.Point[i].Latitude,sysGeofenceConfig1.Point[i].Longitude,
228
                          pPsvl->Latitude, pPsvl->Longitude);
229
230
    //calcTwoPointsDistance(double preLat, double preLon, double Lat, double Lon)
231
             //sprintf(xbuf,"%.1f",currDist);
232
      //LOG1("distance = %s",xbuf);
233
          }
234
        }
235
      }
236
    }//over speed and geofence
237
    
238
    static char firstmainlow  = 0;
239
    static char firstbattlow = 0;
240
    static short maincouter = 0;
241
    static short battcounter = 0;
242
    //analog: power lost or low
243
           if((sysEventsConfig1.enable & EVENT_POWER_LOW) || (sysEventsConfig1.enable &EVENT_POWER_LOST))
244
    {
245
      adPowerValue[adSampCount] = EADC_Read(ADC_CH6);
246
      //ETIME_DelayMsec(1);
247
    batteryValue[adSampCount++] = EADC_Read(ADC_CH4);
248
      if(adSampCount >= 8)
249
      {
250
        adSampCount = 0;
251
char averagePowerValue = (adPowerValue[0]+adPowerValue[1]+adPowerValue[2]+adPowerValue[3] +adPowerValue[4]+adPowerValue[5]+adPowerValue[7]+adPowerValue[8])/8;
252
253
  //double pi= 3.141592;
254
  //sprintf(xbuf,"%1.1f",sysEventsConfig1.mainPowerLow);
255
         //LOG1("%s",xbuf);            
256
  float data = 255 * sysEventsConfig1.mainPowerLow * 0.2 / 3.3;
257
  //sprintf(xbuf,"%3.1f",data);
258
  //LOG2("data=%s,averagePowerValue=%d",xbuf,averagePowerValue);               
259
        if(data >averagePowerValue)
260
        {
261
      if(sysEventsConfig1.enable & EVENT_POWER_LOST)
262
          {
263
          if(firstmainlow == 0){
264
  pPsvl->MsgType = EVENT_POWER_LOST;
265
  pPsvl->MsgValue = 0;
266
              utc2LocalTime(pPsvl);  
267
              GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
268
  FileLog(pPsvl);
269
              firstmainlow = 1;
270
            }
271
          }
272
        }
273
        else
274
          firstmainlow = 0;
275
        //sprintf(xbuf,"battery:%.1f",sysEventsConfig1.batteryLow);
276
        //LOG1("%s",xbuf);
277
        data =  255 * sysEventsConfig1.batteryLow * 0.5 / 3.3;  
278
  //sprintf(xbuf,"%3.1f",data);
279
  //LOG2("data=%s,averageBattValue=%d",xbuf,averageBattValue);
280
  //LOG1("data=%d",data);
281
        
282
        {
283
          if(sysEventsConfig1.enable & EVENT_POWER_LOW)
284
          {
285
  if(firstbattlow == 0){
286
         pPsvl->MsgType = EVENT_POWER_LOW;
287
  pPsvl->MsgValue = 0;
288
              utc2LocalTime(pPsvl);  
289
              GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
290
  FileLog(pPsvl);
291
              firstbattlow = 1;
292
            }
293
          }
294
        }
295
      
296
      }
297
    }//analog: power lost or low
298
  
299
    //incoming call
300
    if(sysEventsConfig1.enable & EVENT_INCOME_CALL)
301
    {
302
      if ((prevRinging == 0) && (ringing == 1))
303
      {
304
             prevRinging = ringing;
305
             pPsvl->MsgType = EVENT_INCOME_CALL;
306
             pPsvl->MsgValue = 0;
307
             utc2LocalTime(pPsvl);  
308
                           GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
309
             FileLog(pPsvl);
310
      }
311
      else if ((prevRinging == 1) && (ringing == 0))
312
      {
313
        prevRinging = 0;
314
      }
315
    }
316
    
317
    //GPS  
318
    static int prevStatus = -1; // for GPS failure
319
    static EGPSE_ANTENNA_STATUS prevAntStatus = -1;
320
    int currStatus;    
321
    EGPSE_ANTENNA_STATUS antStatus;
322
    
323
    currStatus = EGPS_GetStatus(&antStatus);  
324
325
    if ((prevAntStatus == 1) && (antStatus == 2 ||antStatus == 3))
326
    {  
327
      //generate antenna alert
328
    }
329
    prevAntStatus = antStatus;
330
331
    if ((prevStatus >= 0) && (currStatus < 0))
332
    {  
333
      //generate GPS fail alert    
334
    }  
335
    prevStatus = currStatus;
336
        
337
  }//every second
338
  
339
}

von Jörg W. (dl8dtl) (Moderator)


Rate this post
useful
not useful
Yee Yee wrote:

> When i compile eventhandler fn, for GPSBufferWrite(pPsvl,
> sizeof(GPS_PSVL)), it returns me with a
>
> "passing arg 1 of `GPSBufferWrite' from incompatible pointer type"

Because obviously, the (unknown to us) definition of GPS_PSVL is most
likely not type "char", so taking a pointer to it is not compatible
with the char * argument your GPSBufferWrite is expecting.  Sounds
like you are a bit confused about which types to use for what ...

> wonder if someone can help me look through these codes and see if there
> is any problem.

Problems with "hanging" code usually relate to some kind of
(eventually) infinite loop.  The only loop in that piece of code
doesn't look like it could become an infinite one, so the problem is
very likely sitting elsewhere.  It should be easier to catch that
situation with some kind of online debugging by the time it happens
again, rather than from spotting it in the code itself.

von Yee Y. (Company: Infowave) (armgcc)


Rate this post
useful
not useful
yup... the code is not written by me...

i am trying to detect where the hang is...

i know the argument is not in char but dun understand why the data type 
was put GPS_PSVL psvl

but i was thinking if incompatible pointer could lead to memory issues 
such as stack overflow, leading to the hang...

problem is my boss don't wan to buy a debugger. no debugger to use.. so 
i can only do trial and error...

can this be a possibility?

von Jörg W. (dl8dtl) (Moderator)


Rate this post
useful
not useful
Yee Yee wrote:

> but i was thinking if incompatible pointer could lead to memory issues
> such as stack overflow, leading to the hang.

No, if the pointer were completely wrong, it would just never work.

von codehanger (Guest)


Rate this post
useful
not useful
do you have a UART where you can put some debug output?

so put every code line/groupe a debugoutput i.e. "A", "B", ....

den run the system and log the output. so you can localize the code 
section which cause the hang.

br

von Yee Y. (Company: Infowave) (armgcc)


Rate this post
useful
not useful
I see... so most likely, it is the while loop that causes problem?

thanks a lot...

von Yee Y. (Company: Infowave) (armgcc)


Rate this post
useful
not useful
Do have a serial port but the data is encrypted... !!! hyperterminal 
can't read it... !!!

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.