![mmap을 사용하지 않고 /dev/mem에 쓰기](https://linux55.com/image/196582/mmap%EC%9D%84%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%A7%80%20%EC%95%8A%EA%B3%A0%20%2Fdev%2Fmem%EC%97%90%20%EC%93%B0%EA%B8%B0.png)
mmap을 사용하지 않고 /dev/mem에 쓸 수 있습니까?
LKM 내부의 Raspberry Pi에서 풀업 저항을 활성화했지만 해당 기능이 void *mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
없습니다.
open
(나중에 로 변환함 )을 사용해 보았지만 filp_open
아무 것도 수행되지 않았습니다.
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <time.h>
#include <errno.h>
// From https://github.com/RPi-Distro/raspi-gpio/blob/master/raspi-gpio.c
#define PULL_UNSET -1
#define PULL_NONE 0
#define PULL_DOWN 1
#define PULL_UP 2
#define GPIO_BASE_OFFSET 0x00200000
#define GPPUD 37
#define GPPUDCLK0 38
#define BASE_READ 0x1000
#define BASE_SIZE (BASE_READ/sizeof(uint32_t))
uint32_t getGpioRegBase(void) {
const char *revision_file = "/proc/device-tree/system/linux,revision";
uint8_t revision[4] = { 0 };
uint32_t cpu = 0;
FILE *fd;
if ((fd = fopen(revision_file, "rb")) == NULL) {
printf("Can't open '%s'\n", revision_file);
exit(EXIT_FAILURE);
}
else {
if (fread(revision, 1, sizeof(revision), fd) == 4) cpu = (revision[2] >> 4) & 0xf;
else {
printf("Revision data too short\n");
exit(EXIT_FAILURE);
}
fclose(fd);
}
printf("CPU: %d\n", cpu);
switch (cpu) {
case 0: // BCM2835 [Pi 1 A; Pi 1 B; Pi 1 B+; Pi Zero; Pi Zero W]
//chip = &gpio_chip_2835;
return 0x20000000 + GPIO_BASE_OFFSET;
case 1: // BCM2836 [Pi 2 B]
case 2: // BCM2837 [Pi 3 B; Pi 3 B+; Pi 3 A+]
//chip = &gpio_chip_2835;
return 0x3f000000 + GPIO_BASE_OFFSET;
case 3: // BCM2711 [Pi 4 B]
//chip = &gpio_chip_2711;
return 0xfe000000 + GPIO_BASE_OFFSET;
default:
printf("Unrecognised revision code\n");
exit(1);
}
}
int writeBase(uint32_t reg_base, uint32_t offset, uint32_t data) {
int fd;
if ((fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) return -1;
if (lseek(fd, reg_base+offset, SEEK_SET) == -1) return -2;
if (write(fd, (void*)&data, sizeof(uint32_t)) != sizeof(uint32_t)) return -3;
if (close(fd) == -1) return -4;
return 0;
}
int setPull(unsigned int gpio, int pull) {
int r;
int clkreg = GPPUDCLK0 + (gpio / 32);
int clkbit = 1 << (gpio % 32);
uint32_t reg_base = getGpioRegBase();
r = writeBase(reg_base, GPPUD, pull); // base[GPPUD] = pull
if (r < 0) return r;
usleep(10);
r = writeBase(reg_base, clkreg, clkbit); // base[clkreg] = clkbit
if (r < 0) return r;
usleep(10);
r = writeBase(reg_base, GPPUD, 0); // base[GPPUD] = 0
if (r < 0) return r;
usleep(10);
r = writeBase(reg_base, clkreg, 0); // base[clkreg] = 0
usleep(10);
return r;
}
int main(int argc, char *argv[]) {
int gpio, r;
if (argc!=2) {
printf("GPIO pin needed!\n");
return 1;
}
gpio = atoi(argv[1]);
printf("Enabling pull-up on GPIO%d...\n", gpio);
r = setPull(gpio, PULL_UP);
printf("Return value: %d\n", r);
if (r != 0) printf("%s\n", strerror(errno));
return r;
}
raspi-gpio
이것은 내가 원하는 스니펫입니다.
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <time.h>
// From https://github.com/RPi-Distro/raspi-gpio/blob/master/raspi-gpio.c
#define PULL_UNSET -1
#define PULL_NONE 0
#define PULL_DOWN 1
#define PULL_UP 2
#define GPIO_BASE_OFFSET 0x00200000
#define GPPUD 37
#define GPPUDCLK0 38
uint32_t getGpioRegBase(void) {
const char *revision_file = "/proc/device-tree/system/linux,revision";
uint8_t revision[4] = { 0 };
uint32_t cpu = 0;
FILE *fd;
if ((fd = fopen(revision_file, "rb")) == NULL)
{
printf("Can't open '%s'\n", revision_file);
}
else
{
if (fread(revision, 1, sizeof(revision), fd) == 4)
cpu = (revision[2] >> 4) & 0xf;
else
printf("Revision data too short\n");
fclose(fd);
}
printf("CPU: %d\n", cpu);
switch (cpu) {
case 0: // BCM2835 [Pi 1 A; Pi 1 B; Pi 1 B+; Pi Zero; Pi Zero W]
return 0x20000000 + GPIO_BASE_OFFSET;
case 1: // BCM2836 [Pi 2 B]
case 2: // BCM2837 [Pi 3 B; Pi 3 B+; Pi 3 A+]
return 0x3f000000 + GPIO_BASE_OFFSET;
case 3: // BCM2711 [Pi 4 B]
return 0xfe000000 + GPIO_BASE_OFFSET;
default:
printf("Unrecognised revision code\n");
exit(1);
}
}
volatile uint32_t *getBase(uint32_t reg_base) {
int fd;
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) return NULL;
return (uint32_t *)mmap(0, /*chip->reg_size*/ 0x1000,
PROT_READ|PROT_WRITE, MAP_SHARED,
fd, reg_base);
}
void setPull(volatile uint32_t *base, unsigned int gpio, int pull) {
int clkreg = GPPUDCLK0 + (gpio / 32);
int clkbit = 1 << (gpio % 32);
base[GPPUD] = pull;
usleep(10);
base[clkreg] = clkbit;
usleep(10);
base[GPPUD] = 0;
usleep(10);
base[clkreg] = 0;
usleep(10);
}
int main(int argc, char *argv[]) {
if (argc!=2) {
printf("GPIO pin needed!\n");
return 1;
}
uint32_t reg_base = getGpioRegBase();
volatile uint32_t *base = getBase(reg_base);
if (base == NULL || base == (uint32_t *)-1) {
printf("Base error");
return 1;
}
printf("Base: %p\n", base);
setPull(base, atoi(argv[1]), PULL_UP);
return 0;
}
다음은 풀업이 활성화된 KML 스니펫입니다(해당 부분을 제거해야 함 mmap
).
#include <linux/types.h> // uint_32
#include <linux/fs.h> // filp_open/filp_close
#include <linux/delay.h> // udelay
#define PULL_DOWN 1
#define PULL_UP 2
#define GPIO_BASE_OFFSET 0x00200000
#define GPPUD 37
#define GPPUDCLK0 38
static uint32_t getGpioRegBase(bool *error) {
uint8_t revision[4] = { 0 };
uint32_t cpu = 0;
struct file *fd;
ssize_t rc = 0;
if (IS_ERR(( fd = filp_open("/proc/device-tree/system/linux,revision", O_RDONLY | O_SYNC | O_CLOEXEC, 0) ))) {
*error = true;
return 0;
}
if ((rc = kernel_read(fd, revision, sizeof(revision), 0)) == 4) cpu = (revision[2] >> 4) & 0xf;
else {
*error = true;
return 0;
}
filp_close(fd, NULL);
*error = false;
switch (cpu) {
case 0: // BCM2835 [Pi 1 A; Pi 1 B; Pi 1 B+; Pi Zero; Pi Zero W]
return 0x20000000 + GPIO_BASE_OFFSET;
case 1: // BCM2836 [Pi 2 B]
case 2: // BCM2837 [Pi 3 B; Pi 3 B+; Pi 3 A+]
return 0x3f000000 + GPIO_BASE_OFFSET;
case 3: // BCM2711 [Pi 4 B]
return 0xfe000000 + GPIO_BASE_OFFSET;
default:
*error = true;
return 0;
}
}
static volatile uint32_t *getBase(uint32_t reg_base) {
struct file *fd;
volatile uint32_t *r;
if (IS_ERR(( fd = filp_open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC, 0) ))) return NULL;
r = (uint32_t*)mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, fd, reg_base);
filp_close(fd, NULL); // TODO the original didn't have this
return r;
}
static void setPull(volatile uint32_t *base, uint32_t gpio, int pull) {
int clkreg = GPPUDCLK0 + (gpio / 32);
int clkbit = 1 << (gpio % 32);
base[GPPUD] = pull;
udelay(10);
base[clkreg] = clkbit;
udelay(10);
base[GPPUD] = 0;
udelay(10);
base[clkreg] = 0;
udelay(10);
}
/**
* Equivalent to 'raspi-gpio set <gpio> <pu/pd>'
* @param gpio Valid GPIO pin
* @param pull PULL_DOWN/PULL_UP
*/
static int setGpioPull(uint32_t gpio, int pull) {
bool error;
uint32_t reg_base;
volatile uint32_t *base;
reg_base = getGpioRegBase(&error);
if (error) return -1;
base = getBase(reg_base);
if (base == NULL || base == (uint32_t*)-1) return -1;
setPull(base, gpio, pull);
return 0;
}```
답변1
/proc
장치 노드는 /dev
사용자 공간을 위한 것이며 커널에 필요하지 않으며 커널 모듈에서 이를 사용해서는 안 됩니다.
대신 GPIO에 액세스하려면 다음을 사용해야 합니다.ioremap
그리고 다양 ioread...
하고iowrite...
기능: ioremap
찾고 있는 물리적 주소에 해당하는 주소와 IO를 수행하는 기타 기능을 가져옵니다.
에서 얻은 장치 트리 정보를 즉시 검색하는 방법을 모르겠지만 /proc
이를 수행하려면 커널 함수가 있어야 합니다.
답변2
어쩌면 다른 방법이 있을 수도 있습니다:
내 RPi 3B+/bulleye에서:
$ ls -l /dev | grep mem
crw-rw---- 1 root gpio 247, 0 Jan 26 00:06 gpiomem
crw-r----- 1 root kmem 1, 1 Jan 26 00:06 mem
그룹 구성원은 액세스 권한 을 gpio
가지며gpiomem
기본(대개오직RPi OS(née raspbian)의 사용자는 pi
이 그룹의 구성원입니다.