在beaglebone黑色上获得分段错误

时间:2019-03-20 18:49:08

标签: c

对于beaglebone和linux来说是新的,因此对于一个项目,我必须通过使用地址来控制BBB上引导的用户,并通过确保可以设置所有gpio端口来具有将来使用的功能。但是当我运行它只是打开和关闭用户LED时,我遇到了分段错误,而且我不知道为什么或需要做些什么来解决此问题。我需要包含什么,或者我只是在做什么,如果可以,我该怎么办?

#include <string.h>
#include <stdint.h>
#include <stdio.h>

typedef struct {
    volatile uint32_t REVISION;         //Address offset: 0x00
    volatile uint32_t rangeA[3];        //Address offset: 0x04-0x0C
    volatile uint32_t SYSCONFIG;        //Address offset: 0x10
    volatile uint32_t rangeB[3];        //Address offset: 0x14-0x1C
    volatile uint32_t EOI;              //Address offset: 0x20
    volatile uint32_t IRQSTATUS_RAW_0;  //Address offset: 0x24
    volatile uint32_t IRQSTATUS_RAW_1;  //Address offset: 0x28
    volatile uint32_t IRQSTATUS_0;      //Address offset: 0x2C
    volatile uint32_t IRQSTATUS_1;      //Address offset: 0x30
    volatile uint32_t IRQSTATUS_SET_0;  //Address offset: 0x34
    volatile uint32_t IRQSTATUS_SET_1;  //Address offset: 0x38
    volatile uint32_t IRQSTATUS_CLR_0;  //Address offset: 0x3C
    volatile uint32_t IRQSTATUS_CLR_1;  //Address offset: 0x40
    volatile uint32_t IRQWAKEN_0 ;      //Address offset: 0x44
    volatile uint32_t IRQWAKEN_1;       //Address offset: 0x48
    volatile uint32_t rangeC[26];       //Address offset: 0x4C-0x110
    volatile uint32_t SYSSTATUS;        //Address offset: 0x114
    volatile uint32_t rangeD[6];        //Address offset: 0x118-0x12C
    volatile uint32_t CTRL;             //Address offset: 0x130
    volatile uint32_t OE;               //Address offset: 0x134
    volatile uint32_t DATAIN;           //Address offset: 0x138
    volatile uint32_t DATAOUT;          //Address offset: 0x13C
    volatile uint32_t LEVELDETECT0;     //Address offset: 0x140
    volatile uint32_t LEVELDETECT1;     //Address offset: 0x144
    volatile uint32_t RISINGDETEDT;     //Address offset: 0x148
    volatile uint32_t FALLINGDETECT;    //Address offset: 0x14C
    volatile uint32_t DEBOUNCENABLE;    //Address offset: 0x150
    volatile uint32_t DEBOUNCINGTIME;   //Address offset: 0x154
    volatile uint32_t rangeE[14];       //Address offset: 0x158-0x18C
    volatile uint32_t CLEARDATAOUT;     //Address offset: 0x190
    volatile uint32_t SETDATAOUT;       //Address offset: 0x194
} GPIO_TypeDef;

#define GPIO0 ((GPIO_TypeDef *) 0x44E07000)
#define GPIO1 ((GPIO_TypeDef *) 0x4804C000)
#define GPIO2 ((GPIO_TypeDef *) 0x481AC000)
#define GPIO3 ((GPIO_TypeDef *) 0x481AE000)

int main()      //testing if this would work
{
    GPIO1->OE &= ~(1UL<<21);       // set  bits to output eneble
    while(1)
    {
        GPIO1->DATAOUT |= 1UL<<21;
        int delay = 0;
        while(delay < 15000)    // just really quick crappy delay
            delay++;
        GPIO1->DATAOUT &= ~(1UL<<21);
        printf("Blink!!\n");    //test to see if it works
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您分配的地址似乎是物理地址或总线地址;您的程序将使用虚拟地址或程序地址运行。您计算机上的内核构造了翻译页面,因此您的程序可以保留内核可以动态分配的内存地址的规范化视图。

您需要将寄存器的设备空间映射(使用mmap())到程序中,并分配结构以指向它们。

您应该最终得到类似于以下内容的代码:

GPIO_TypeDef  *g0, *g1, ... ;
...
g0 = mmap(0, sizeof *g0,..., fd, 0);
...
g0->DataOut = 3;
...

ps:从样式上讲,_TypeDef开头无法达到typedef机制的目的,即创建有意义的名称和自记录程序。 _t的惯用用法好一点,因为它不会浪费那么多按键。请记住,某些程序员在视觉上有缺陷,每次遇到辅助设备时,都必须听一个辅助设备说“下划线大写T y p大写D e f”!