From dc2e38ec7f2836115bcf710b40937eb4b1b46b36 Mon Sep 17 00:00:00 2001 From: gaoyang3513 Date: Sat, 27 Jan 2024 15:27:10 +0000 Subject: [PATCH] =?UTF-8?q?[Add]=20=E6=96=B0=E5=A2=9EI2C=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 11 +++++---- 02-I2C/Makefile | 54 +++++++++++++++++++++++++++++++++++++++++++++ 02-I2C/ReadMe.md | 28 +++++++++++++++++++++++ 02-I2C/i2c_static.c | 39 ++++++++++++++++++++++++++++++++ Makefile | 3 ++- 5 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 02-I2C/Makefile create mode 100644 02-I2C/ReadMe.md create mode 100755 02-I2C/i2c_static.c diff --git a/.gitignore b/.gitignore index 0968eac45..55f02db7d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,17 @@ - +# Files *.o -*.mod - +*.ko *.cmd +*.mod +*.mod.c +*.mod.o Module.symvers modules.order compile_commands.json +# Directories .cache/ - output/ _install/ +.tmp_versions/ diff --git a/02-I2C/Makefile b/02-I2C/Makefile new file mode 100644 index 000000000..1cbdd8ecc --- /dev/null +++ b/02-I2C/Makefile @@ -0,0 +1,54 @@ +# All Right Reserved. +# Author : gaoyang3513@outlook.com +# Version : V1.0.0 202x.01.01 +# Description : +# Note : + +# --------------------------------------------------------------------------- +# Path information +LOCAL_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) + +# Subdirectory + +# Output directories +OUTPUT_DIR ?= $(LOCAL_DIR)/output +INSTALL_DIR ?= $(LOCAL_DIR)/_install + +# --------------------------------------------------------------------------- +# Variables +MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo) + +# --------------------------------------------------------------------------- +# Compile configure + +# --------------------------------------------------------------------------- +# Source and oject files +ifneq ($(KERNELRELEASE),) + +obj-m := i2c_static.o + +# --------------------------------------------------------------------------- +# Targets +else # KERNELRELEASE + +ifneq ($(filter $(BOARD),mikv-Duo),) +KERNELDIR := ${HOME}/Source/10-CV1800/01-MilkDuo/02-Project/SDK_CV1800_BR2/linux_5.10/build/cv1800b_milkv_duo_sd +ROOTFS := ${HOME}/nfs/rootfs +else +KERNELDIR ?= /lib/modules/$(shell uname -r)/build +endif + +.PHONY: all clean init + +all: + @$(MAKE) -C $(KERNELDIR) M=$(LOCAL_DIR) modules -j$(MULTI_CORES); + +install: + @$(MAKE) -C $(KERNELDIR) M=$(LOCAL_DIR) modules_install INSTALL_MOD_PATH=$(INSTALL_DIR) + +clean: + @rm -rf *.o *.ko .*.cmd *.mod* modules.order Module.symvers .tmp_versions + @if [ -d $(LOCAL_DIR)/output ]; then rm -rf $(LOCAL_DIR)/output; fi; + @if [ -d $(LOCAL_DIR)/_install ]; then rm -rf $(LOCAL_DIR)/_install; fi; + +endif # KERNELRELEASE diff --git a/02-I2C/ReadMe.md b/02-I2C/ReadMe.md new file mode 100644 index 000000000..9a2b73385 --- /dev/null +++ b/02-I2C/ReadMe.md @@ -0,0 +1,28 @@ + + +## 调试命令 + + +```shell +# ~/.bash_aliases +# alias source_env_MilkV-Duo='export BOARD=mikv-Duo; export ARCH=riscv; export CROSS_COMPILE=riscv64-unknown-linux-gnu-' + +source_env_MilkV-Duo +``` + +```shell +scp gaoyang3513@192.168.3.120:Source/12-Demo/Linux_Drivers/02-I2C/i2c_static.ko ./ + +``` + +```shell +# cat /proc/devices +Character devices: +... +256 GBox +``` + +```shell +mknod -m 0755 /dev/GBox c 256 0 +``` + diff --git a/02-I2C/i2c_static.c b/02-I2C/i2c_static.c new file mode 100755 index 000000000..7c9f9b0f8 --- /dev/null +++ b/02-I2C/i2c_static.c @@ -0,0 +1,39 @@ +#include +#include +#include + +#include + +#define VSER_MAJOR 256 +#define VSER_MINOR 0 +#define VSER_DEV_CNT 1 +#define VSER_DEV_NAME "GBox" + +static int __init vser_init(void) +{ + int ret; + dev_t dev; + + dev = MKDEV(VSER_MAJOR, VSER_MINOR); + ret = register_chrdev_region(dev, VSER_DEV_CNT, VSER_DEV_NAME); + if (ret) + goto reg_err; + return 0; + +reg_err: + return ret; +} + +static void __exit vser_exit(void) +{ + dev_t dev; + + dev = MKDEV(VSER_MAJOR, VSER_MINOR); + unregister_chrdev_region(dev, VSER_DEV_CNT); +} + +module_init(vser_init); +module_exit(vser_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("gaoyang3513 "); diff --git a/Makefile b/Makefile index 76f921778..c2aa373c4 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,8 @@ INSTALL_DIR ?= $(LOCAL_DIR)/_install # Variables MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo) -SUB_DIRS := 01-Char/ +SUB_DIRS := 01-Char/ \ + 02-I2C/ # --------------------------------------------------------------------------- # Compile configure