EmbDev.net

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


Author: Yee Yee (Company: Infowave) (armgcc)
Posted on:

Rate this post
0 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
ECOME_STATUS GPSBufferWrite(char *data, int size)
{
  //LOG2("GPSBufferWrite: data = %s, size = %d", data, size);
  //if (ReadGPSLogBufferPointer(&gpsPtr) == COM_SUCCESS)
  {  
    //LOG2("GPSBufferWrite: before, pWrite = %d, pRead = %d", gpsPtr.pWrite, gpsPtr.pRead);
      if ((gpsPtr.pWrite + size) <= MAX_GPS_BUFFER_SIZE)
      {
      if (EFLASH_Write1(data, DATAFLASH_GPS_FIX_LOG_BUFFERING_DATA_ADDR + gpsPtr.pWrite, size) == COM_SUCCESS)
      {
#if 0 
        // read back verification
        char tmpbuf[200]="";
      EFLASH_Read(DATAFLASH_GPS_FIX_LOG_BUFFERING_DATA_ADDR + gpsPtr.pWrite, tmpbuf, size);
        if (strncmp(tmpbuf, data, size) != 0)
        LOG0("***** FLASHING ERROR *****");

 //tmpbuf[16] = 0x0;  
 //LOG2("GPSBufferWrite: pwrite = %d, data = %s", gpsPtr.pWrite, tmpbuf);  
#endif
        
 //LOG2("Before UpdatePointer, pWrite = %d, pRead = %d", gpsPtr.pWrite, gpsPtr.pRead);
        //data[16] = 0x0;  
        LOG1("pwrite=%d", gpsPtr.pWrite);  
        
#if 1
        gpsPtr.pWrite += size;
        UpdateGPSLogBufferPointer(&gpsPtr);
#endif
        
        return (COM_SUCCESS);
        
      }
    }
    else
    {
  LOG1("GPSBufferWrite: (gpsPtr.pWrite + size) > MAX_GPS_BUFFER_SIZE, gpsPtr.pWrite = %d", gpsPtr.pWrite);
      //gpsPtr.pRead = 0;
      gpsPtr.pWrite = 0;
      if (EFLASH_Write1(data, DATAFLASH_GPS_FIX_LOG_BUFFERING_DATA_ADDR + gpsPtr.pWrite, size) == COM_SUCCESS)
      {
        gpsPtr.pWrite += size;
        UpdateGPSLogBufferPointer(&gpsPtr);
      }
      //LOG0("Reset write pointer to 0");
    }
  }
  
  return (COM_SUCCESS);
}


static GPS_PSVL eventPsvl={0};
extern GPS_PSVL psvl;


void Events_Handler(unsigned int rtcStatus)
{
  extern char ringing;
  static char prevRinging = 0; 
  unsigned char emergency3, tamper3, ignition3; 
  
  static char flag_triggered = 0;
  static char xSampleCount = 0;
  static char ySampleCount = 0;
  static char zSampleCount = 0;
  
  static char adPowerValue[8];
  static char batteryValue[8];
  static char adSampCount = 0;
  
  float xdata,ydata,zdata;
  unsigned short currSpeed;
  GPS_PSVL *pPsvl;
  char msgLength;
  char xbuf[24];
  //char ybuf[24];
  //char zbuf[24];
  //char mbuf[24];
  ESYSS_CONFIG_EVENTS_SETUP1 *p = &sysEventsConfig1;
  
  //if (rtcStatus & RTC_SEC_CHANGE_BIT)
  {
    memcpy(&eventPsvl,&psvl,sizeof(GPS_PSVL));
    pPsvl = &eventPsvl;  
  }
  //accelermeter
  if(sysEventsConfig1.enable & EVENT_ACCELERMETER)
  {
    char xValue =0;
    char yValue = 0;
    char zValue = 0;
    
    if(flag_triggered)
    {
    Accelerometor2(&xValue, &yValue, &zValue);
    xdata =255 * 0.66 * sysEventsConfig1.XLimited / 3.3;
    ydata =255 * 0.66 * sysEventsConfig1.YLimited / 3.3;
    zdata =255 * 0.66 * sysEventsConfig1.ZLimited / 3.3;
    //sprintf(xbuf,"%3.1f",xdata);
    //sprintf(ybuf,"%3.1f",ydata);
    //sprintf(zbuf,"%3.1f",zdata);
    //LOG3("xdata=%s,ydata=%s,zdata=%s",xbuf, ybuf, zbuf);
      if(xdata < xValue)
      {
        flag_triggered = 1;
        xSampleCount++;
      }
      if(ydata < yValue)
      {
        flag_triggered = 1;
        ySampleCount++;
      }
      if(zdata < zValue)
      {
        flag_triggered = 1;
        zSampleCount++;
      }
    }  
    
  }//accelermeter
  
  //every second
  if (rtcStatus & RTC_SEC_CHANGE_BIT)
  {
    //GPI
    emergency3 = (unsigned char)EIO_GetBit(0);
    //ETIME_DelayMsec(20);
    emergency3 = (unsigned char)EIO_GetBit(0);
    
    ignition3 = (unsigned char)EIO_GetBit(1);
    //ETIME_DelayMsec(20);
    ignition3 = (unsigned char)EIO_GetBit(1);
    
    tamper3 = (unsigned char)EIO_TamperingCheck();
    //ETIME_DelayMsec(20);  
    tamper3 = (unsigned char)EIO_TamperingCheck();  
    //emergnecy cal
    
    if(preEmergency == 2)
      preEmergency = emergency3;
    else if(emergency3 != preEmergency )
    {
      if(emergency3 == 1)
      {
        if(sysEventsConfig1.enable & EVENT_EMERG_CALL)
        {
          pPsvl->MsgType = EVENT_EMERG_CALL;
          pPsvl->MsgValue = 0;
          utc2LocalTime(pPsvl);  
          GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
          FileLog(pPsvl);
        }
      }  
      preEmergency = emergency3;
    }    
    
    //tamper
    if(preTamper == 2)
      preTamper =  tamper3;
    else if(tamper3 != preTamper)
    {
      if(tamper3 == 1)
      {
        if(sysEventsConfig1.enable & EVENT_TAMPER)
        {
          pPsvl->MsgType = EVENT_TAMPER;
          pPsvl->MsgValue = 0;
          utc2LocalTime(pPsvl);  
          GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
          FileLog(pPsvl);
        }
      }  
      preTamper =  tamper3;
    }

    //ignition
    if(preIgnition == 2)
      preIgnition = ignition3;
    else if(ignition3 != preIgnition)
    {
      //LOG0("ignition pin level changed");
      if(sysEventsConfig1.enable & EVENT_IGNITION)
      {
        LOG0("trigger ignition alert");
        pPsvl->MsgType = EVENT_IGNITION;
        pPsvl->MsgValue = ignition3;
        utc2LocalTime(pPsvl);  
        GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
        FileLog(pPsvl);
        
      }
      preIgnition = ignition3;
    }
            
    //over speed and geofence
    if( pPsvl->flag_fix )
    {
      //LOG0("gps fixed");
      if(sysEventsConfig1.enable & EVENT_OVER_SPEED)
      {
        currSpeed = pPsvl->SOG;  
        if(currSpeed > sysEventsConfig1.overspeed )
        {
          pPsvl->MsgType = EVENT_OVER_SPEED;
          pPsvl->MsgValue = 0;
          utc2LocalTime(pPsvl);  
          GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
          FileLog(pPsvl);
        }    
      }
      //geo fence enter
      static char firstenter = 0;
      static char fistexit = 0;
      static char exitcounter = 0;
      if((sysEventsConfig1.enable & EVENT_ENTER_GEOZONE) || (sysEventsConfig1.enable & EVENT_EXIT_GEOZONE))
      {
    unsigned int flag = sysGeofenceConfig1.EnableFlag;
    char count = sysGeofenceConfig1.NumberOfPoints;
        int i, temp = 0;
        float currDist = 0;
        for(i = 0; i < count; i++)
        {
          temp = 1 << i;
          if(flag & temp)
          {
            currDist = calcTwoPointsDistance(sysGeofenceConfig1.Point[i].Latitude,sysGeofenceConfig1.Point[i].Longitude,
                          pPsvl->Latitude, pPsvl->Longitude);

    //calcTwoPointsDistance(double preLat, double preLon, double Lat, double Lon)
             //sprintf(xbuf,"%.1f",currDist);
      //LOG1("distance = %s",xbuf);
          }
        }
      }
    }//over speed and geofence
    
    static char firstmainlow  = 0;
    static char firstbattlow = 0;
    static short maincouter = 0;
    static short battcounter = 0;
    //analog: power lost or low
           if((sysEventsConfig1.enable & EVENT_POWER_LOW) || (sysEventsConfig1.enable &EVENT_POWER_LOST))
    {
      adPowerValue[adSampCount] = EADC_Read(ADC_CH6);
      //ETIME_DelayMsec(1);
    batteryValue[adSampCount++] = EADC_Read(ADC_CH4);
      if(adSampCount >= 8)
      {
        adSampCount = 0;
char averagePowerValue = (adPowerValue[0]+adPowerValue[1]+adPowerValue[2]+adPowerValue[3] +adPowerValue[4]+adPowerValue[5]+adPowerValue[7]+adPowerValue[8])/8;

  //double pi= 3.141592;
  //sprintf(xbuf,"%1.1f",sysEventsConfig1.mainPowerLow);
         //LOG1("%s",xbuf);            
  float data = 255 * sysEventsConfig1.mainPowerLow * 0.2 / 3.3;
  //sprintf(xbuf,"%3.1f",data);
  //LOG2("data=%s,averagePowerValue=%d",xbuf,averagePowerValue);               
        if(data >averagePowerValue)
        {
      if(sysEventsConfig1.enable & EVENT_POWER_LOST)
          {
          if(firstmainlow == 0){
  pPsvl->MsgType = EVENT_POWER_LOST;
  pPsvl->MsgValue = 0;
              utc2LocalTime(pPsvl);  
              GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
  FileLog(pPsvl);
              firstmainlow = 1;
            }
          }
        }
        else
          firstmainlow = 0;
        //sprintf(xbuf,"battery:%.1f",sysEventsConfig1.batteryLow);
        //LOG1("%s",xbuf);
        data =  255 * sysEventsConfig1.batteryLow * 0.5 / 3.3;  
  //sprintf(xbuf,"%3.1f",data);
  //LOG2("data=%s,averageBattValue=%d",xbuf,averageBattValue);
  //LOG1("data=%d",data);
        
        {
          if(sysEventsConfig1.enable & EVENT_POWER_LOW)
          {
  if(firstbattlow == 0){
         pPsvl->MsgType = EVENT_POWER_LOW;
  pPsvl->MsgValue = 0;
              utc2LocalTime(pPsvl);  
              GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
  FileLog(pPsvl);
              firstbattlow = 1;
            }
          }
        }
      
      }
    }//analog: power lost or low
  
    //incoming call
    if(sysEventsConfig1.enable & EVENT_INCOME_CALL)
    {
      if ((prevRinging == 0) && (ringing == 1))
      {
             prevRinging = ringing;
             pPsvl->MsgType = EVENT_INCOME_CALL;
             pPsvl->MsgValue = 0;
             utc2LocalTime(pPsvl);  
                           GPSBufferWrite(pPsvl, sizeof(GPS_PSVL));
             FileLog(pPsvl);
      }
      else if ((prevRinging == 1) && (ringing == 0))
      {
        prevRinging = 0;
      }
    }
    
    //GPS  
    static int prevStatus = -1; // for GPS failure
    static EGPSE_ANTENNA_STATUS prevAntStatus = -1;
    int currStatus;    
    EGPSE_ANTENNA_STATUS antStatus;
    
    currStatus = EGPS_GetStatus(&antStatus);  

    if ((prevAntStatus == 1) && (antStatus == 2 ||antStatus == 3))
    {  
      //generate antenna alert
    }
    prevAntStatus = antStatus;

    if ((prevStatus >= 0) && (currStatus < 0))
    {  
      //generate GPS fail alert    
    }  
    prevStatus = currStatus;
        
  }//every second
  
}

Author: Jörg Wunsch (dl8dtl) (Moderator)
Posted on:

Rate this post
0 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.

Author: Yee Yee (Company: Infowave) (armgcc)
Posted on:

Rate this post
0 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?

Author: Jörg Wunsch (dl8dtl) (Moderator)
Posted on:

Rate this post
0 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.

Author: codehanger (Guest)
Posted on:

Rate this post
0 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

Author: Yee Yee (Company: Infowave) (armgcc)
Posted on:

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

thanks a lot...

Author: Yee Yee (Company: Infowave) (armgcc)
Posted on:

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

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.