简单的i2c oled 驱动尝试

:expressionless: 第一次接触linux驱动开发,带部分内容都是在参考别人的, 如有误导或者错误之处,还请各位大佬嘴下留情* :joy:

1.配置开发环境

我是在docker中创建了一个ubuntu20.04系统,根据官方这个git库配置的环境
https://github.com/milkv-duo/milkv-duo-buildroot-sdk

2.修改I2C0的引脚

我尝试了开发板引脚图上标出出来的所以i2c引脚,但是都扫描不到设备,最后看了社区一篇文章才知道要改引脚。
就是这篇 : https://community.milkv.io/t/milk-v-pwm10/145

在下面这个文件里可以看到,I2C-0的引脚默认连接的是XGPIO29和XGPIO28,并不是I2C的引脚

duo-buildroot-sdk/build/boards/cv180x/cv1800b_milkv_duo_sd/u-boot/cvi_board_init.c

本以为默认的就是I2C引脚,试了半天又看了看引脚说明才知道默认的也不是I2C引脚。 :sweat_smile:


要改成这样就行了:

然还按照第一步的链接里官方的库中说的方法编译文件烧录到开发板上,就可以用I2C0了。

3. 连接设备到开发板上

i2c设备 开发板
SDA ----- GPIO29
SCL ----- GPIO28

4.通过命令行测试

列出I2C-0上的所有设备

i2cdetect -y -r 0
输出是这样的
image

终于成功了!

5.编写驱动

我把自己写的代码上传到git上了,不太会用git 和 makefile 见谅。

driver中的是驱动,只有ssd1306.c,ssd1306.h,Makefile三个文件有用,ssd1306.ko 是生成的驱动,,其他的都是自动生成的,不懂是什么。app是简单的显示字符的测试程序。

6.加载驱动

上传到开发板上后执行加载驱动

insmod ssd1306.ko

查看驱动

lsmod
image
成功加载了

7.驱动测试

直接通过命令行让oled显示实时

echo “hello world!” > /dev/oled_128_64_1

成功!激动! :laughing:

4 Likes

感谢~
介绍引脚信息很有用,学习了。
今晚回去试试看

Can you please share the exact command with whom you could build the driver? I am getting the following error after calling make on this Makefile:

root@d7743918aff8:/home/work/milk-v_duo_oled_driver/driver# make
make -C /home/work//linux_5.10/build/cv1800b_milkv_duo_sd M=/home/work/milk-v_duo_oled_driver/driver modules
make[1]: Entering directory '/home/work/linux_5.10/build/cv1800b_milkv_duo_sd'
  LDS     arch/riscv/kernel/vdso/vdso.lds
  AS      arch/riscv/kernel/vdso/rt_sigreturn.o
  CC      arch/riscv/kernel/vdso/vgettimeofday.o
In file included from /home/work/linux_5.10/include/vdso/datapage.h:137,
                 from /home/work/linux_5.10/lib/vdso/gettimeofday.c:5,
                 from <command-line>:
/home/work/linux_5.10/arch/riscv/include/asm/vdso/gettimeofday.h: In function 'gettimeofday_fallback':
/home/work/linux_5.10/arch/riscv/include/asm/vdso/gettimeofday.h:21:31: error: '__NR_gettimeofday' undeclared (first use in this function)
   21 |  register long nr asm("a7") = __NR_gettimeofday;
      |                               ^~~~~~~~~~~~~~~~~
/home/work/linux_5.10/arch/riscv/include/asm/vdso/gettimeofday.h:21:31: note: each undeclared identifier is reported only once for each function it appears in
/home/work/linux_5.10/arch/riscv/include/asm/vdso/gettimeofday.h: In function 'clock_gettime_fallback':
/home/work/linux_5.10/arch/riscv/include/asm/vdso/gettimeofday.h:37:31: error: '__NR_clock_gettime' undeclared (first use in this function)
   37 |  register long nr asm("a7") = __NR_clock_gettime;
      |                               ^~~~~~~~~~~~~~~~~~
/home/work/linux_5.10/arch/riscv/include/asm/vdso/gettimeofday.h: In function 'clock_getres_fallback':
/home/work/linux_5.10/arch/riscv/include/asm/vdso/gettimeofday.h:53:31: error: '__NR_clock_getres' undeclared (first use in this function)
   53 |  register long nr asm("a7") = __NR_clock_getres;
      |                               ^~~~~~~~~~~~~~~~~
make[3]: *** [/home/work/linux_5.10/scripts/Makefile.build:279: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make[2]: *** [arch/riscv/Makefile:108: vdso_prepare] Error 2
make[1]: *** [/home/work/linux_5.10/Makefile:185: __sub-make] Error 2
make[1]: Leaving directory '/home/work/linux_5.10/build/cv1800b_milkv_duo_sd'
make: *** [Makefile:8: all] Error 2
root@d7743918aff8:/home/work/milk-v_duo_oled_driver/driver# 

Also added the following lines to the beggining of the Makefile:

 export ARCH:=riscv
 export CROSS_COMPILE:=/home/work/host-tools/gcc/riscv64-linux-musl-x86_64/bin/riscv64-unknown-linux-musl-

Thanks!

My compilation commands are based on the following article without any changes. The Makefile file is located on my GitHub,
Milk-v duo编写一个最简单的内核模块(驱动) - Duo - MilkV Community

我今天又尝试了一下,发现新版的sdk与我之前写这篇文章时使用的不一样了.
我使用./build.sh lunch选择6,发现构建出来的内核目录为“duo-buildroot-sdk/linux_5.10/build/cv1813h_milkv_duos_sd/”,而不是"duo-buildroot-sdk/linux_5.10/build/cv1800b_milkv_duo_sd"了,
只要修改自己驱动项目里的Makefile的这一句"KERN_DIR = $(SDK_DIR)/linux_5.10/build/cv1813h_milkv_duos_sd"就可以了,然后执行

cd duo-buildroot-sdk
source build/cvisetup.sh
defconfig cv1800b_milkv_duo_sd
./build.sh milkv-duos-sd
cd <your project>
make

我的duo开发板是使用sd卡的