浏览代码

Merge pull request #230 from WiringPi/pwm

Pwm
example
Manfred Wallner 6 个月前
committed by GitHub
父节点
当前提交
6ca817f645
找不到此签名对应的密钥 GPG 密钥 ID: B5690EEEBB952194
共有 3 个文件被更改,包括 91 次插入12 次删除
  1. +26
    -10
      gpio/gpio.c
  2. +64
    -2
      wiringPi/wiringPi.c
  3. +1
    -0
      wiringPi/wiringPi.h

+ 26
- 10
gpio/gpio.c 查看文件

@@ -1397,15 +1397,15 @@ static void doVersion (char *argv [])
printf ("\n") ;
piBoardId (&model, &rev, &mem, &maker, &warranty) ;

printf ("Raspberry Pi Details:\n") ;
printf ("Hardware details:\n") ;
printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
piModelNames [model], piRevisionNames [rev], piMemorySize [mem], piMakerNames [maker], warranty ? "[Out of Warranty]" : "") ;

// Check for device tree
if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ...
printf (" * Device tree is enabled.\n") ;
printf ("System details:\n") ;
if (stat ("/proc/device-tree", &statBuf) == 0) { // We're on a devtree system ...
printf (" * Device tree present.\n") ;
}
if (stat ("/proc/device-tree/model", &statBuf) == 0) // Output Kernel idea of board type
{
if ((fd = fopen ("/proc/device-tree/model", "r")) != NULL)
@@ -1415,14 +1415,30 @@ static void doVersion (char *argv [])
perror("Error reading /proc/device-tree/model");
}
fclose (fd) ;
printf (" *--> %s\n", name) ;
printf (" Model: %s\n", name) ;
}
}

if (wiringPiUserLevelAccess()) // User level GPIO is GO
printf (" * This Raspberry Pi supports user-level GPIO access.\n") ;
else
printf (" * Root or sudo required for GPIO access.\n") ;
int bGlobalAccess = wiringPiGlobalMemoryAccess(); // User level GPIO is GO
switch(bGlobalAccess) {
case 0:
printf (" * Does not support basic user-level GPIO access via memory.\n") ;
break;
case 1:
printf (" * Supports basic user-level GPIO access via /dev/mem.\n") ;
break;
case 2:
printf (" * Supports full user-level GPIO access via memory.\n") ;
break;
}
if (wiringPiUserLevelAccess()) {
printf (" * Supports basic user-level GPIO access via /dev/gpiomem.\n") ;
} else {
printf (" * Does not support basic user-level GPIO access via /dev/gpiomem.\n") ;
if(0==bGlobalAccess) {
printf (" * root or sudo may be required for GPIO access.\n") ;
}
}
}




+ 64
- 2
wiringPi/wiringPi.c 查看文件

@@ -2497,6 +2497,10 @@ int wiringPiUserLevelAccess(void)
struct stat statBuf ;
const char* gpiomemModule = gpiomem_BCM;

if (RaspberryPiModel<0) { //need to detect pi model
int model, rev, mem, maker, overVolted ;
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
}
if (PI_MODEL_5 == RaspberryPiModel) {
gpiomemModule = gpiomem_RP1;
}
@@ -2505,6 +2509,61 @@ int wiringPiUserLevelAccess(void)
}


int wiringPiGlobalMemoryAccess(void)
{
const char* gpiomemGlobal;
int fd=-1;
unsigned int MMAP_size;
unsigned int BaseAddr, PWMAddr;

if (RaspberryPiModel<0) { //need to detect pi model
int model, rev, mem, maker, overVolted ;
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
}
if (PI_MODEL_5 == RaspberryPiModel) {
gpiomemGlobal = pciemem_RP1;
MMAP_size = pciemem_RP1_Size;
BaseAddr = 0x00000000;
PWMAddr = 0x00000000; //not supported so far
} else {
gpiomemGlobal = gpiomem_global;
MMAP_size = BLOCK_SIZE;
BaseAddr = piGpioBase + 0x00200000 ;
PWMAddr = piGpioBase + 0x0020C000 ;
}

if ((fd = open (gpiomemGlobal, O_RDWR | O_SYNC | O_CLOEXEC)) >0) {
int returnvalue = 1; // OK

uint32_t * lgpio = (uint32_t *)mmap(0, MMAP_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, BaseAddr) ;
if (lgpio == MAP_FAILED) {
returnvalue = 0;
if (wiringPiDebug)
fprintf(stderr,"wiringPiGlobalMemoryAccess: mmap (GPIO) failed: %s\n", strerror (errno)) ;
} else {
munmap(lgpio, MMAP_size);
if (PI_MODEL_5 == RaspberryPiModel) {
returnvalue = 2; // GPIO & PWM accessible (same area, nothing to mmap)
} else {
//check PWM area
uint32_t* lpwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, PWMAddr) ;
if (lpwm == MAP_FAILED) {
returnvalue = 1; // only GPIO accessible
if (wiringPiDebug)
fprintf(stderr,"wiringPiGlobalMemoryAccess: mmap (PWM) failed: %s\n", strerror (errno)) ;
} else {
returnvalue = 2; // GPIO & PWM accessible
munmap(lpwm, BLOCK_SIZE);
}
}
}

close(fd);
return returnvalue;
}
return 0; // Failed!
}

/*
* wiringPiSetup:
* Must be called once at the start of your program execution.
@@ -2608,9 +2667,12 @@ int wiringPiSetup (void)
gpiomemModule = gpiomem_RP1;
}

usingGpioMem = FALSE;
if (gpiomemGlobal==NULL || (fd = open (gpiomemGlobal, O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
{

if (wiringPiDebug) {
printf ("wiringPi: no access to %s try %s\n", gpiomemGlobal, gpiomemModule) ;
}
if (gpiomemModule && (fd = open (gpiomemModule, O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) // We're using gpiomem
{
piGpioBase = 0 ;
@@ -2623,7 +2685,7 @@ int wiringPiSetup (void)
" Try running with sudo?\n", gpiomemGlobal, gpiomemModule, strerror (errno)) ;
}
if (wiringPiDebug) {
printf ("wiringPi: access to %s succeded\n", usingGpioMem ? gpiomemModule : gpiomemGlobal) ;
printf ("wiringPi: access to %s succeded %d\n", usingGpioMem ? gpiomemModule : gpiomemGlobal, fd) ;
}
// GPIO:
if (PI_MODEL_5 != model) {


+ 1
- 0
wiringPi/wiringPi.h 查看文件

@@ -208,6 +208,7 @@ extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
extern int GPIOToSysFS(const int pin) ;

extern void wiringPiVersion (int *major, int *minor) ;
extern int wiringPiGlobalMemoryAccess(void); //Interface 3.3
extern int wiringPiUserLevelAccess (void) ;
extern int wiringPiSetup (void) ;
extern int wiringPiSetupSys (void) ;


正在加载...
取消
保存