Results 1 to 4 of 4

Thread: Segmentaiton Fault Error Difficulty

  1. #1

    Default Segmentaiton Fault Error Difficulty

    Hello,

    I'm running into a bit of trouble when trying to run a piece of code. Overall, what I'm trying to do is measure a signal from an ADC, process it and then display it on the monitor. The latter half is fairly straight forward, but before trying I'm trying to just configure the ADC process. The code is to run on a Raspberry Pi, with a Linux OS. I have code anyway which does sample the signal correctly using the ADC in C++, however when converting it to the format for Qt, I seem to get a Segmentation error. Now, from reading the forums here I've found that this occurs when the program tries to access a piece of memory that doesn't belong to it – which is fairly confusing as this problem does not occur when the same arguments are passed through it in c++. Most commonly this seems to be from pointers not being initialised or being initialised incorrectly. Through the debugging process, I've not been able to find any more information other than where the error occurs – which is why I'm now asking you guys here.

    The error I get is:
    Qt Code:
    1. Program received signal SIGSEGV, Segmentation fault.
    2. 0x00011f24 in gz_clock_ena(int, int) ()
    To copy to clipboard, switch view to plain text mode 

    Therefore it seems the error occurs in a function titled gz_clockena(). Using backtrace didn't really give any more information, other than to say where the error occured. This error occurs in an implementation file titled 'adcreader.cpp'.

    The relevant parts of this code, pertaining to the problem is:

    Qt Code:
    1. 1 #include "adcreader.h"
    2. 2 #include <QDebug>
    3. 3 #include <QtCore>
    4. 4 #include "gpio-sysfs.h"
    5. 5 #include <fcntl.h>
    6. 6 #include <sys/ioctl.h>
    7. 7 #include <linux/types.h>
    8. 8 #include <linux/spi/spidev.h>
    9. 9 #include <assert.h>
    10. 10 //#define GZ_CLK_5MHz 0
    11. 11 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    12. 12
    13. 13 #define MAX_SAMPLES 65536
    14. 14
    15. 15 ADCreader::ADCreader()
    16. 16 {
    17.  
    18. 72 fprintf(stderr, "spi mode: %d\n", mode);
    19. 73 fprintf(stderr, "bits per word: %d\n", bits);
    20. 74 fprintf(stderr, "max speed: %d Hz (%d KHz)\n", speed, speed/1000);
    21. 75
    22. 76 // enable master clock for the AD
    23. 77 // divisor results in roughly 4.9MHz
    24. 78 // this also inits the general purpose IO
    25. 79 gz_clock_ena(GZ_CLK_5MHz,5); .......
    26. 80 }
    To copy to clipboard, switch view to plain text mode 

    In this same implementation file there is a function ADCreader::run() - which is the thread I'm running.
    I initialised it in the main.cpp file as so:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include "adcreader.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QCoreApplication a(argc, argv);
    6.  
    7. ADCreader adcr;
    8. adcr.start();
    9.  
    10.  
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    The problem definitely occurs in the gz_clock_ena function as the three 'fprintf' lines are output.
    From what I read before about this error occuring when trying to access a memory location it doesn't have access to, I changed the values of the arguments. However this had no luck, and the same error occured.

    The implementation of this function is in a file called 'gz_clk.cpp'. The relevant parts of the code for this is:
    Qt Code:
    1. 29 int gz_clock_ena(int speed, int divisor) {
    2. 30 int speed_id = 6;
    3. 31 if (speed < GZ_CLK_5MHz || speed > GZ_CLK_125MHz) {
    4. 32 printf("gz_clock_ena: Unsupported clock speed selected.\n");
    5. 33 printf("Supported speeds: GZ_CLK_5MHz (0) and GZ_CLK_125MHz (1).");
    6. 34 exit(-1);
    7. 35 }
    8. 36 if (speed == 0) {
    9. 37 speed_id = 1;
    10. 38 }
    11. 39 if (divisor < 2) {
    12. 40 printf("gz_clock_ena: Minimum divisor value is 2.");
    13. 41 exit(-1);
    14. 42 }
    15. 43 if (divisor > 0xfff) {
    16. 44 printf("gz_clock_ena: Maximum divisor value is %d.", 0xfff);
    17. 45 exit(-1);
    18. 46 }
    19. 47 if (bcm2835_init() !=1) {
    20. 48 printf("gz_clock_ena: Failed to initialize I/O\n");
    21. 49 exit(-1);
    22. 50 }
    23. 51 usleep(5);
    24. 52 bcm2835_gpio_fsel(RPI_GPIO_P1_07, BCM2835_GPIO_FSEL_ALT0);
    25. 53 *(bcm2835_clk + 0x1C) = 0x5A000000 | speed_id; // GPCLK0 off
    26. 54 while (*(bcm2835_clk + 0x1C) & GZ_CLK_BUSY) {} // Wait for BUSY low
    27. 55 *(bcm2835_clk + 0x1D) = 0x5A002000 | (divisor << 12); // set DIVI
    28. 56 *(bcm2835_clk + 0x1C) = 0x5A000010 | speed_id; // GPCLK0 on
    29. 57 return 0;
    30. 58 }
    To copy to clipboard, switch view to plain text mode 

    Naturally I would have thought that the error would have occured because the argument Gz_CLK was not initialised correctly, but it was in the header file for this implementation. Also as mentioned previously, I changed the values of the arguments in the function 'gz_clock_ena(GZ_CLK_5MHz,5);' to make sure this wasn't the root of the problem.

    I've not been able to find any information that can get me close to solving the problem, so any help from anybody here as an indication would be greatly appreciated.

    Many thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Segmentaiton Fault Error Difficulty

    You seem to be very confused about the differences between compiling, linking, and running a program.

    If your constant GZ_CLK_5MHz was not defined, your code would not have compiled, and there would be nothing to link or run.

    If you didn't include the proper libraries when linking (as in your previous post), then your program won't link and there will be nothing to run.

    Your current problem appears to be a run-time error. You are accessing memory locations defined by some pointer named "bcm2835_clk". Where is this pointer initialized? If it isn't being initialized, then it is either NULL or garbage, but in either case trying to access its contents via *(bcm2835_clk +0xXX) type statements will blow up.

  3. #3

    Default Re: Segmentaiton Fault Error Difficulty

    Hi there, thanks for taking the time to reply.

    Apologies for the lack of terminology, I am new to programming as well as Qt.

    Thanks for pointing to the possible source of the error. The pointer "bcm2835_clk" is actually defined as: "extern volatile uint32_t *bcm2835_clk;", in a library I installed known as BCM2835 which allows access to GPIO functions. However, that is the only mention of it. I am not sure of the nuaunces of the code in 'gz_clk.cpp' specifically, as that was given as a working example.


    To avoid this error, would it bcm2835_clk need to be given a value, rather than just being defined as "extern volatile uint32_t *bcm2835_clk;"

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Segmentaiton Fault Error Difficulty

    You probably need to initialize the library.
    Most C libraries that maintain a state of some sort have some kind of init function.

    Cheers,
    _

Similar Threads

  1. internal compiler error: Segmentation fault
    By bibhukalyana in forum Installation and Deployment
    Replies: 3
    Last Post: 27th December 2013, 14:42
  2. Segmentation fault error
    By Niamita in forum Qt Programming
    Replies: 5
    Last Post: 23rd April 2012, 18:00
  3. building error: Segmentation fault
    By kaycee1 in forum Installation and Deployment
    Replies: 2
    Last Post: 21st October 2010, 11:08
  4. Segmentation Fault error on all Qt examples/demos
    By slscripters in forum Newbie
    Replies: 4
    Last Post: 26th April 2010, 12:00
  5. segmentation fault error
    By sagi in forum Installation and Deployment
    Replies: 2
    Last Post: 25th July 2008, 06:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.