Skip to content

Commit

Permalink
Merge pull request #1 from sc-bin/main
Browse files Browse the repository at this point in the history
update
  • Loading branch information
sc-bin authored Sep 30, 2024
2 parents a64bf67 + e5c74f1 commit f8ad2af
Show file tree
Hide file tree
Showing 35 changed files with 4,209 additions and 340 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,4 @@ cython_debug/
.vscode/settings.json
exe
command/_gpiocommand
server/gpioc-server
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
1. 编写代码的时候调用头文件`#include "gpio.h"`
2. 编译代码的时候使用`-lgpio`,如`gcc main.c -lgpio -o exe`

操作寄存器需要以管理员权限运行,当代码以普通用户运行时底层会通过与一个后台服务通信来间接操作寄存器,速度会慢一点点
```
while (1)
{
pin_write(LED, 1);
pin_write(LED, 0);
}
```
以管理员权限运行时,高电平与低电平的时长都接近1us,示波器测算频率为530k
以普通用户运行时,高电平与低电平的时长都接近250us,示波器测算频率为2.3k


## shell命令
所有命令都支持自动补全,按两下TAB键就能查看有哪些可选参数

Expand Down
2 changes: 1 addition & 1 deletion README_PY.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ from gpioc.pwm import Pwm
from gpioc.chips.H616 import *
import time
led = Pwm(PC13.id)
led = Pwm(PC13)
led.frequency = 1000
while 1:
for i in range(1,100,5):
Expand Down
32 changes: 32 additions & 0 deletions command/uninstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

set -e

echo_red() {
echo -e "\r\033[31m$1\033[0m"
}
echo_green() {
echo -e "\r\033[32m$1\033[0m"
}
echo_blue() {
echo -e "\r\033[36m$1\033[0m"
}


PATH_PWD="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
VERSION=$(uname -r)
VERSION_digit=${VERSION:0:1}


bin_path="/usr/bin/gpio"
bin_file_name="gpio.sh"
comple_path="/etc/bash_completion.d/gpio"
comple_file_name="gpio-completion.sh"


if [ -f $bin_path ]; then
rm $bin_path
fi

rm $comple_path

11 changes: 11 additions & 0 deletions examples-c/gpio_mode/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cc = gcc

src = $(wildcard *.c)
target = exe


all:$(src)
gcc $(src) -lgpio -o $(target)

clean:
-rm $(target)
19 changes: 19 additions & 0 deletions examples-c/gpio_mode/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <unistd.h>

#include "gpio.h"
#define IO 41

int main()
{
printf("引脚 %d 的mode 0 : %s\n", IO, pin_get_mode_name_by_num(IO, 0));
printf("引脚 %d 的mode 1 : %s\n", IO, pin_get_mode_name_by_num(IO, 1));

printf("引脚 %d 工作于 %d %s\n", IO, pin_get_mode(IO), pin_get_mode_name_now(IO));
pin_set_mode(IO, OUTPUT); // 设置为输出模式
printf("引脚 %d 工作于 %d %s\n", IO, pin_get_mode(IO), pin_get_mode_name_now(IO));
pin_set_mode_by_name(IO, "IN"); // 设置为输入模式
printf("引脚 %d 工作于 %d %s\n", IO, pin_get_mode(IO), pin_get_mode_name_now(IO));

return 0;
}
9 changes: 6 additions & 3 deletions install
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/bin/bash
PATH_PWD="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"

cd libgpio
cd ${PATH_PWD}/libgpio
make
make install
cd ../command

cd ${PATH_PWD}/command
make
./install


cd ${PATH_PWD}/server
./install

25 changes: 18 additions & 7 deletions libgpio/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
cc = gcc
target = libgpio
head_file = gpio.h

src = $(wildcard *.c)
src := $(filter-out %_py.c, $(src))
target = libgpio.so
head_file = gpio.h
all:
gcc -shared -fPIC $(src) -o $(target)
install:
cp ${target} /usr/lib/
objects = $(patsubst %.c,%.o,$(src))

all: $(target).a $(target).so

$(target).a: $(objects)
ar -rc $@ $^

$(target).so: $(src)
gcc -fPIC -shared $^ -o $@


install:all
cp $(target).a $(target).so /usr/lib/
cp ${head_file} /usr/include

clean:
-rm $(target)
-rm /usr/include/${head_file}
-rm $(objects)
-rm $(target).a $(target).so
53 changes: 53 additions & 0 deletions libgpio/board-pin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef BOARD_PIN_H
#define BOARD_PIN_H

#define _BOARD_DEF_WANUTPI_1B 1
#define _BOARD_DEF_WANUTPI_1B_EMMC 2

#define PH_NUM_ERROR -1
#define PH_5V -2
#define PH_3V3 -3
#define PH_GND -4
#define PH_NC -1
#define PH_COLOR_BLACK 0
#define PH_COLOR_RED 1
#define PH_COLOR_GREEEN 2
#define PH_COLOR_BLUE 3
#define PH_COLOR_YELLOW 4

struct BOARD_PIN
{
int pin_num; // 在板上的引脚编号
int gpio_num; // 芯片上的gpio编号
char *name; // 要显示出来的引脚名称
int color; // 引脚颜色
};
#define DEF_A_BOARD_PIN(_pin_num, _gpio_num, _name, _color) \
{ \
.pin_num = _pin_num, \
.gpio_num = _gpio_num, \
.name = _name, \
.color = _color, \
}

struct BOARD_PIN_PER
{
int count; // 共有几个引脚
int *pins; // 哪些引脚
int *modes; // 对应引脚需要设置为什么模式
};

struct BOARD_DESC
{
char *model; // 设备树model字段
int pin_num; // 共有几个pin脚
struct BOARD_PIN *pins; // 描述板子所带的所有引脚
struct BOARD_PIN_PER *pwms;
struct BOARD_PIN_PER *uarts;
struct BOARD_PIN_PER *spis;
struct BOARD_PIN_PER *i2cs;
};
extern struct BOARD_DESC walnutpi_1b;
extern struct BOARD_DESC walnutpi_1b_emmc;

#endif
89 changes: 89 additions & 0 deletions libgpio/board-walnutpi1b-emmc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "board-pin.h"

struct BOARD_PIN walnutpi1b_emmc_pins[] = {
DEF_A_BOARD_PIN(0, 0, "", 0),
DEF_A_BOARD_PIN(1, PH_3V3, "3.3v", PH_COLOR_YELLOW),
DEF_A_BOARD_PIN(2, PH_5V, "5v", PH_COLOR_RED),
DEF_A_BOARD_PIN(3, 264, "PI8", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(4, PH_5V, "5v", PH_COLOR_RED),
DEF_A_BOARD_PIN(5, 263, "PI7", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(6, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(7, PH_NC, "----", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(8, 261, "PI5", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(9, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(10, 262, "PI6", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(11, PH_NC, "----", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(12, PH_NC, "----", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(13, PH_NC, "----", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(14, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(15, 267, "PI11", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(16, 268, "PI12", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(17, PH_3V3, "3.3v", PH_COLOR_YELLOW),
DEF_A_BOARD_PIN(18, PH_NC, "----", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(19, 231, "PH7", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(20, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(21, 232, "PH8", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(22, PH_NC, "----", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(23, 230, "PH6", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(24, 229, "PH5", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(25, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(26, 233, "PH9", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(27, 266, "PI10", PH_COLOR_BLUE),
DEF_A_BOARD_PIN(28, 265, "PI9", PH_COLOR_BLUE),
DEF_A_BOARD_PIN(29, 256, "PI0", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(30, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(31, 257, "PI1", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(32, 272, "PI16", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(33, 258, "PI2", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(34, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(35, 259, "PI3", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(36, 271, "PI15", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(37, 260, "PI4", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(38, 269, "PI13", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(39, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(40, 270, "PI14", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(41, 76, "KEY", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(42, 71, "LED", PH_COLOR_BLACK),
};
static int walnutpi1b_emmc_pwms_pins[] = {15, 16, 38, 40};
static int walnutpi1b_emmc_pwms_modes[] = {5, 5, 5, 5};
static struct BOARD_PIN_PER walnutpi1b_emmc_pwms =
{
.count = 4,
.pins = walnutpi1b_emmc_pwms_pins,
.modes = walnutpi1b_emmc_pwms_modes,
};

static int walnutpi1b_emmc_uarts_pins[] = {8, 10, 38, 40};
static int walnutpi1b_emmc_uarts_modes[] = {3, 3, 3, 3};
static struct BOARD_PIN_PER walnutpi1b_emmc_uarts =
{
.count = 4,
.pins = walnutpi1b_emmc_uarts_pins,
.modes = walnutpi1b_emmc_uarts_modes,
};
static int walnutpi1b_emmc_spis_pins[] = {19, 21, 23, 24, 26};
static int walnutpi1b_emmc_spis_modes[] = {4, 4, 4, 4, 4};
static struct BOARD_PIN_PER walnutpi1b_emmc_spis =
{
.count = 4,
.pins = walnutpi1b_emmc_spis_pins,
.modes = walnutpi1b_emmc_spis_modes,
};
static int walnutpi1b_emmc_i2cs_pins[] = {3, 5, 27, 28};
static int walnutpi1b_emmc_i2cs_modes[] = {5, 5, 5, 5};
static struct BOARD_PIN_PER walnutpi1b_emmc_i2cs =
{
.count = 4,
.pins = walnutpi1b_emmc_i2cs_pins,
.modes = walnutpi1b_emmc_i2cs_modes,
};
struct BOARD_DESC walnutpi_1b_emmc = {
.model = "walnutpi-1b-emmc",
.pin_num = 42,
.pins = walnutpi1b_emmc_pins,
.pwms = &walnutpi1b_emmc_pwms,
.uarts = &walnutpi1b_emmc_uarts,
.spis = &walnutpi1b_emmc_spis,
.i2cs = &walnutpi1b_emmc_i2cs,
};
89 changes: 89 additions & 0 deletions libgpio/board-walnutpi1b.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "board-pin.h"

struct BOARD_PIN walnutpi1b_pins[] = {
DEF_A_BOARD_PIN(0, 0, "", 0),
DEF_A_BOARD_PIN(1, PH_3V3, "3.3v", PH_COLOR_YELLOW),
DEF_A_BOARD_PIN(2, PH_5V, "5v", PH_COLOR_RED),
DEF_A_BOARD_PIN(3, 264, "PI8", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(4, PH_5V, "5v", PH_COLOR_RED),
DEF_A_BOARD_PIN(5, 263, "PI7", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(6, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(7, 72, "PC8", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(8, 261, "PI5", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(9, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(10, 262, "PI6", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(11, 73, "PC9", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(12, 74, "PC10", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(13, 75, "PC11", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(14, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(15, 267, "PI11", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(16, 268, "PI12", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(17, PH_3V3, "3.3v", PH_COLOR_YELLOW),
DEF_A_BOARD_PIN(18, 78, "PC14", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(19, 231, "PH7", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(20, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(21, 232, "PH8", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(22, 79, "PC15", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(23, 230, "PH6", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(24, 229, "PH5", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(25, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(26, 233, "PH9", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(27, 266, "PI10", PH_COLOR_BLUE),
DEF_A_BOARD_PIN(28, 265, "PI9", PH_COLOR_BLUE),
DEF_A_BOARD_PIN(29, 256, "PI0", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(30, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(31, 257, "PI1", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(32, 272, "PI16", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(33, 258, "PI2", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(34, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(35, 259, "PI3", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(36, 271, "PI15", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(37, 260, "PI4", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(38, 269, "PI13", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(39, PH_GND, "GND", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(40, 270, "PI14", PH_COLOR_GREEEN),
DEF_A_BOARD_PIN(41, 76, "KEY", PH_COLOR_BLACK),
DEF_A_BOARD_PIN(42, 77, "LED", PH_COLOR_BLACK),
};
static int walnutpi1b_pwms_pins[] = {15, 16, 38, 40};
static int walnutpi1b_pwms_modes[] = {5, 5, 5, 5};
static struct BOARD_PIN_PER walnutpi1b_pwms =
{
.count = 4,
.pins = walnutpi1b_pwms_pins,
.modes = walnutpi1b_pwms_modes,
};

static int walnutpi1b_uarts_pins[] = {8, 10, 38, 40};
static int walnutpi1b_uarts_modes[] = {3, 3, 3, 3};
static struct BOARD_PIN_PER walnutpi1b_uarts =
{
.count = 4,
.pins = walnutpi1b_uarts_pins,
.modes = walnutpi1b_uarts_modes,
};
static int walnutpi1b_spis_pins[] = {19, 21, 23, 24, 26};
static int walnutpi1b_spis_modes[] = {4, 4, 4, 4, 4};
static struct BOARD_PIN_PER walnutpi1b_spis =
{
.count = 4,
.pins = walnutpi1b_spis_pins,
.modes = walnutpi1b_spis_modes,
};
static int walnutpi1b_i2cs_pins[] = {3, 5, 27, 28};
static int walnutpi1b_i2cs_modes[] = {5, 5, 5, 5};
static struct BOARD_PIN_PER walnutpi1b_i2cs =
{
.count = 4,
.pins = walnutpi1b_i2cs_pins,
.modes = walnutpi1b_i2cs_modes,
};
struct BOARD_DESC walnutpi_1b = {
.model = "walnutpi-1b",
.pin_num = 42,
.pins = walnutpi1b_pins,
.pwms = &walnutpi1b_pwms,
.uarts = &walnutpi1b_uarts,
.spis = &walnutpi1b_spis,
.i2cs = &walnutpi1b_i2cs,
};
Loading

0 comments on commit f8ad2af

Please sign in to comment.