1.5k views
in System Integration by

The only changes done by me are to the device driver .c file while using the generated code from the Device Integration example of Raspberry Pi.

1 Answer

+1 vote
by
 
Best answer
Hello,

difficult to say....

What happens if you take the original DeviceDriver.c file - does it work without segmentation fault?

Best regards,

Manfred.
by
yes, the original example works fine.
by
So it seems that the segmentation fault is caused by some of your changes...

Maybe you can exclude parts of your modifications to nail down the location where the error happens.
by
int DeviceDriver_ProcessData( void )
{
  int needUpdate = 0;

  /*
     Get the data you want to provide to the GUI application.
     In case your are working with an operating system and your device is
     controlled from a separate task/thread/process, take all information
     from your device driver out of the message queue.
     Please note, that this function is called within the context of the main
     GUI thread.
     If you control your system by direct register access or some BSP functions,
     get all necessary data you want to provide to the GUI application.
  */

#ifdef _ApplicationDeviceClass_

  /* here we just evaluate the current hardware button state */
//  if ( digitalRead( BUTTON_GPIO ) == LOW )
//    ButtonCounter++;
//  else
//    ButtonCounter = 0;

  /* check for a valid access to the autoobject of the device class */
  if ( DeviceObject == 0 )
    return 0;

  /*
     For each device paramter, that is represented by a property within the
     Embedded Wizard device class and that you want to update, you have to call
     the appropriate UpdateProperty() method.

     The following examples assumes, that you have a device class with the
     name 'DeviceClass' within the unit 'Application'.
  */

  /* Update the property HardButtonCounter within the class Application::DeviceClass
     by calling the method 'UpdateHardButtonCounter' - the generated define is
     evaluated to ensures that the method is available within the generated code. */
//  #ifdef _ApplicationDeviceClass__UpdateHardButtonCounter_

//    ApplicationDeviceClass__UpdateHardButtonCounter( DeviceObject, (XInt32)ButtonCounter );
//    needUpdate = 1;

//  #endif

  /*
     Trigger system events if necessary, e.g. if a certain situation happens,
     if an error occurs or just if a certain value has changed...
  */

  /* When the hardware button is pressed, call the method 'TriggerHardButtonEvent()' of the
     device class 'DeviceClass' within the unit 'Application' - the generated define is
     evaluated to ensure that the method is available within the generated code. */
//  if ( ButtonCounter == 1 )
//  {
//    #ifdef _ApplicationDeviceClass__TriggerHardButtonEvent_

//      ApplicationDeviceClass__TriggerHardButtonEvent( DeviceObject );
//      needUpdate = 1;

//    #endif

//  }
 while((ADS1256_Scan() == 0));
		for (i = 0; i < ch_num; i++)
		{
			adc[i] = ADS1256_GetAdc(i);
              	 volt[i] = (adc[i] * 100) / 167;	
		}
		
		for (i = 0; i < ch_num; i++)
		{
	                buf[0] = ((uint32_t)adc[i] >> 16) & 0xFF;
	                buf[1] = ((uint32_t)adc[i] >> 8) & 0xFF;
	                buf[2] = ((uint32_t)adc[i] >> 0) & 0xFF;
	                printf("%d=%02X%02X%02X, %8ld", (int)i, (int)buf[0], 
	                       (int)buf[1], (int)buf[2], (long)adc[i]);                

	                iTemp = volt[i];	/* uV  */
					if (iTemp < 0)
					{
						iTemp = -iTemp;
	                  		  	printf(" (-%ld.%03ld %03ld V) \r\n", iTemp /1000000, (iTemp%1000000)/1000, iTemp%1000);
					}
					else
					{
	                    			printf(" ( %ld.%03ld %03ld V) \r\n", iTemp /1000000, (iTemp%1000000)/1000, iTemp%1000);                    
					}
					
		}
			printf("\33[%dA", (int)ch_num);  
		bsp_DelayUS(100000);
          needUpdate = 1;	

#endif

  /*
     Return a value != 0 if there is at least on property changed or if a
     system event was triggered. The return value is used by the main loop, to
     decide whether the GUI application has changed or not.
  */

  return needUpdate;
}

This is the part of the code that is meant to run infinitely.Corresponding changes have been done in the rest of the file as well. 

This code is intended to print ADC values continuously. I am guessing, this is the part which might be the culprit . My intention currently is to have the UI running while ADC values get printed on the terminal at the backend.

by

The intention of the function DeviceDriver_ProcessData() is that you just read the data that are necessary for the user interface and then you should return. If this function is blocking due to an endless loop, the entire UI application will hang. For more details, please have a look to the description of the main loop

If you want to process data continuously and independent from the UI, you can create a separate worker thread that is processing your ADC data.

If you want to display a certain ADC value within your UI application, then you can just read the ADC and return the value. Similar to the original example, that increments the ButtonCounter and then returns.

by
Certainly, I understand that. If you look closely, there is no endless loop implemented here. ADC values will simply be read from all 8 channels of the device, printed on the terminal and then the function returns control to the main loop.

Here, the UI is not starting up only. It exits by giving a segmentation fault.
by
Understood. That's fine.

Then you should find the function call that is causing the segmentation fault. For that purpose you can place simple printf() statements within your code, just to see which is the instruction that produces the error.
by

Hello Manfred

I followed your suggestion. And pin-pointed the location where the error is occuring. 

The fault occurs before the DeviceDriver_Initialize() function is even called. Since devicedriver.c was the file which was modified , I am not able to draw any conclusions from this.

 

by
Can you send your modified DeviceDriver.c file? I will have a look on that.
by

https://ask.embedded-wizard.de/?qa=blob&qa_blobid=614896531816490184 //devicedriver,c

Here, I have attached the device driver.c file. 

Just FYI, I am using an ADC chip ADS1256 . An example file(namely ADS1256test.c- which is also attached below) was available online which uses bcm2835.h as the base functionality file. I had tested the file independently , also I tested the Deviceintegeration project independently. This is my attempt to basically include the ADC functions into the Devicedriver.

https://ask.embedded-wizard.de/?qa=blob&qa_blobid=6536290395998010090    //ads1256test.c

https://ask.embedded-wizard.de/?qa=blob&qa_blobid=5383896756556811157    //bcm2835.c

https://ask.embedded-wizard.de/?qa=blob&qa_blobid=12596710160883662912 //bcm2835.h

by

I still believe the segmentation fault happens somewhere within the DeviceDriver_Intialize() function - your EwPrint() messages are maybe not transferred because of the missing "\n".

Try to find the erroneous location e.g. by putting a lot of simple but unique messages within your code, e.g.:

void DeviceDriver_Initialize( void )
{
  EwPrint("A\n");
    if (!bcm2835_init())
        return 1;
  EwPrint("B\n");
    bcm2835_spi_begin();
  EwPrint("C\n");
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);   //default
  EwPrint("D\n");
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE1);                //default
  EwPrint("E\n");
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256);//default
  EwPrint("F\n");
    bcm2835_gpio_fsel(SPICS, BCM2835_GPIO_FSEL_OUTP);//
  EwPrint("G\n");
    bcm2835_gpio_write(SPICS, HIGH);
  EwPrint("H\n");
    bcm2835_gpio_fsel(DRDY, BCM2835_GPIO_FSEL_INPT);
  EwPrint("I\n");
    bcm2835_gpio_set_pud(DRDY, BCM2835_GPIO_PUD_UP);    	
...

 

by

Thanks a lot Manfred

You were right. The error was due the bcm2835.h library. It required root access to run

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...