Ich habe hier Code in c_gpio.c der beim Ausführen auf dem Raspi die
Meldung 'Segmentation fault' erzeugt:
1 | #include <stdint.h>
|
2 | #include <stdlib.h>
|
3 | #include <fcntl.h>
|
4 | #include <sys/mman.h>
|
5 |
|
6 | #include "c_gpio.h"
|
7 |
|
8 | //compilieren für Python für gcc mit den Schaltern:
|
9 | // -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC
|
10 |
|
11 |
|
12 | #define BCM2708_PERI_BASE 0x20000000
|
13 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
|
14 | #define OFFSET_FSEL 0 // 0x0000
|
15 | #define OFFSET_SET 7 // 0x001c / 4
|
16 | #define OFFSET_CLR 10 // 0x0028 / 4
|
17 | #define OFFSET_PINLEVEL 13 // 0x0034 / 4
|
18 | #define OFFSET_PULLUPDN 37 // 0x0094 / 4
|
19 | #define OFFSET_PULLUPDNCLK 38 // 0x0098 / 4
|
20 |
|
21 | #define PAGE_SIZE (4*1024)
|
22 | #define BLOCK_SIZE (4*1024)
|
23 |
|
24 | static volatile uint32_t *gpio_map;
|
25 |
|
26 |
|
27 |
|
28 | // Sets a pullup or -down resistor on a GPIO
|
29 | void set_pullupdn(int gpio, int pud)
|
30 | {
|
31 | int clk_offset = OFFSET_PULLUPDNCLK + (gpio/32);
|
32 | int shift = (gpio%32);
|
33 |
|
34 | if (pud == PUD_DOWN)
|
35 | *(gpio_map+OFFSET_PULLUPDN) = (*(gpio_map+OFFSET_PULLUPDN) & ~3) | PUD_DOWN;
|
36 | else if (pud == PUD_UP)
|
37 | *(gpio_map+OFFSET_PULLUPDN) = (*(gpio_map+OFFSET_PULLUPDN) & ~3) | PUD_UP;
|
38 | else // pud == PUD_OFF
|
39 |
|
40 | *(gpio_map+OFFSET_PULLUPDN) &= ~3;
|
41 |
|
42 | short_wait();
|
43 | *(gpio_map+clk_offset) = 1 << shift;
|
44 | short_wait();
|
45 | *(gpio_map+OFFSET_PULLUPDN) &= ~3;
|
46 | *(gpio_map+clk_offset) = 0;
|
47 |
|
48 |
|
49 | }
|
aufgerufen wird er:
1 | // in c_gpio.h
|
2 | #define SETUP_OK 0
|
3 | #define SETUP_DEVMEM_FAIL 1
|
4 | #define SETUP_MALLOC_FAIL 2
|
5 | #define SETUP_MMAP_FAIL 3
|
6 |
|
7 | #define INPUT 1 // is really 0 for control register!
|
8 | #define OUTPUT 0 // is really 1 for control register!
|
9 | #define ALT0 4
|
10 |
|
11 | #define HIGH 1
|
12 | #define LOW 0
|
13 |
|
14 | #define PUD_OFF 0
|
15 | #define PUD_DOWN 1
|
16 | #define PUD_UP 2
|
17 |
|
18 | // angeschlossene Relais
|
19 | #define GPIO_RELAIS_1 23
|
20 | #define GPIO_RELAIS_2 24
|
21 |
|
22 | set_pullupdn(GPIO_RELAIS_2, PUD_OFF);
|
Der Compiler auf dem Raspi (mit dem sich Code für PWM des gleichen
Authors erzeugen lässt)
/usr/bin/arm-linux-gnueabihf-cpp-4.6
Der Cross-Compiler, der den nicht-funktionierenden Code erzeugt:
@echo 'Invoking: Cross GCC Linker'
arm-linux-gnueabihf-gcc -lrt -o "test_1" $(OBJS) $(USER_OBJS) $(LIBS)
mit: -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall
-Wstrict-prototypes -fPIC
Egal ob ich die Schalter setze oder nicht, der Fehler bleibt gleich.
Könnte das an den unterschiedlichen Compilern liegen?