TA的每日心情 | 怒 2019-11-20 15:22 |
|---|
签到天数: 2 天 [LV.1]初来乍到
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
6 H8 I" f3 G# W9 f与前一节添加GPIO Controller的步骤一样,进入虚拟机中的关于SOC的设备树文件目录openrisc-3.1\arch\openrisc\boot\dts添加关于板子上I2C器件的设备描述
4 r6 [8 D7 |1 B# [6 W3 ^+ i. E7 j% u! P8 U% O
- R; i$ d4 M/ W, C* p
! ? Z$ Q( e5 q9 b: W open之,在其中加入关于板子上的I2C器件的描述,例如我板子上有个AT24c08,所以我就添加eeprom设备,形式如下:
2 e& U+ q, \. p3 J0 b8 k2 q/ Q0 D, Y: N
- i2c0: ocores@93000000 {
- compatible = "opencores,i2c-ocores";
- reg = <0x93000000 0x8>;
- interrupts = <5>;
- regstep = <1>;
- clock-frequency = <40000000>;
- #address-cells = <1>;
- #size-cells = <0>;
- eeprom@54 {
- compatible = "at24";
- reg = <0x54>;
- };
- };1 i/ ~3 {& H* W) t+ i& S5 E
4 ^/ f# p9 G' x9 h; p+ v 至于关于其中编写方法后述。
; F6 j$ J8 o! N& |* Y& O9 r6 n, H5 @* T) q
然后,在命令行中进入linux源码目录,然后需要配置内核加入上面添加的I2C的总线驱动,这是因为关于这个ipcore的驱动社区上的大虾已经编写好,现在先熟悉和使用,在后面再去详细分析这个总线驱动如何编写。) H1 q; w& l* [/ q' [- {6 W% {
8 S5 l4 c+ q. P! @ L, S
2 q" q9 z% M% c' F! K: l: Y! ?8 q: w& Z9 e' p4 i4 L
内核配置,进入General setup项
% }3 Q1 M# q& x& e2 `) q) }( \; y5 S, e& h3 \1 V+ T4 ^
, R' ?/ k K& o% e8 Q1 O4 W! x2 n6 b& I) X
将Prompt for development and/or incomplete code/drivers打开
) |7 y! e# G% ~8 H, w8 Y: U' f
; f0 C. d- R4 \$ E& ]) `
% w4 q5 g: e i/ O/ M; y' j: p7 c. \; t# c0 p& D; q3 T# K* E. h
Exit后,到Device Drivers项中8 y% @' G* t9 t5 H0 e' g
; B R! C. j# l x, J5 z
6 u7 H, Q L5 z! `, J: g% f
$ F4 f i2 c0 ~ 将I2C support选择编入内核中
; d, |3 K/ G! p, \( |8 x4 B t* ?: {3 s P/ ~: z
1 m" V# R1 b# _2 H: K. }0 C5 f
7 |4 V$ h8 f n. e. h5 x3 F 进入,将I2C device inteRFace选中编入内核,我们第一个应用层的测试文件就基于内核编写好的设备接口文件来写
$ M7 b: _: t, n1 t2 h; K v0 y: J' P6 w6 W% h) c; e$ c, G
7 Z* Q. M/ T6 Y" G' ?9 \5 s! Y2 \% Z: X; f7 Q
然后选择I2C Hardware Bus support中1 S+ E" i7 X( q& C) @6 G, b
7 ~7 k0 \' S* Z
3 ?, K' F/ s3 e9 p/ K
! C! a3 x% _- t7 D6 @
将OpenCores I2C Controller选择编入内核
1 e6 N- ]5 S2 I9 W& b1 t# u. @8 H9 v- d+ e
7 Z* { m* Z0 b t5 E# e/ H7 M7 D
" e9 I, S. P& ]& ~. X 好,最后exit出来选择保存,重新make% Z2 f, E! Y! I. r
5 J' g" s( h+ D, L# s
8 ^- H/ w6 j4 R% D8 I( X+ I5 p0 H, v" ]( q% Y/ I- l5 |
之后的步骤按照之前移植linux的步骤一样,生成uImage镜像,然后下载到开发板上面即可。
, Y% _1 e' M9 n* t* }) ~
7 E' P% O0 P b* q+ U! {# Q& E 然后在boot linux的过程和进入/dev目录下可以看到i2c bus driver和生成设备文件i2c-x
5 b9 D% `$ v, ?* s1 ?: I5 D5 e. f5 y% z
5 @5 N8 t4 }8 w N9 K6 k
: y9 |1 Q5 U' s) c. v6 n+ m3 ]0 D. ? 至此,i2c的总线驱动就添加到内核了。
' _9 m8 D4 J6 I. H8 s
1 p" Z4 _6 x. k* R 根据我自己的理解:; }6 ~8 t6 n _' A! ~8 t
( _! b" K( ]2 M& [" k4 o 1.对于i2c controller来说,编写的驱动程序称为总线驱动,例如上面在or1200_soc中添加的i2c控制器ipcore,为之编写的驱动程序就是总线驱动。
) S4 X9 q: k, z$ ~, T: |5 l
0 h$ W7 @& \& S7 r 2.对于挂在i2c总线上的slave device来说,为之编写的驱动程序称为设备驱动,例如我板子上挂在这个ipcore上的AT24c08的eeprom。
) [9 Z( C9 c$ k& X4 N4 B) t% C- S& `* T l$ @
3.linux下的i2c子系统中还有一个i2c核心模块来为总线驱动和设备驱动服务,完成注册,删除······功能。& e) b* J4 F) e. f o/ u
) @6 R. n. k% {7 H& ^ 现在说明一下:在openrisc-3.1\drivers\i2c\busses目录下的i2c-ocores.c文件为soc上添加的i2c controller提供了总线驱动,即在刚才的内核配置中已经选择编入了内核,所以可以略过总线驱动的编写,在下节我们着重分析这个总线驱动。! S( f8 }7 p9 B h* X& n$ L
2 e' U g# D) J8 ^5 \0 P0 t. a: u7 I
, z6 J; I! a" s7 T& x
; b* c6 P+ _. r6 v/ G- H/ l* W
现在可以先打开这个文件,里面的comment中就有关于device-tree文件如何加入i2c device描述的说明4 s q) y5 z0 L |. S; ?. K. C
- B3 _' |# n' K
- /*
- * Device tree configuration:
- *
- * Required properties:
- * - compatible : "opencores,i2c-ocores"
- * - reg : bus address start and address range size of device
- * - interrupts : interrupt number
- * - regstep : size of device registers in bytes
- * - clock-frequency : frequency of bus clock in Hz
- *
- * Example:
- *
- * i2c0: ocores@a0000000 {
- * compatible = "opencores,i2c-ocores";
- * reg = <0xa0000000 0x8>;
- * interrupts = <10>;
- *
- * regstep = <1>;
- * clock-frequency = <20000000>;
- *
- * -- Devices connected on this I2C bus get
- * -- defined here; address- and size-cells
- * -- apply to these child devices
- *
- * #address-cells = <1>;
- * #size-cells = <0>;
- *
- * dummy@60 {
- * compatible = "dummy";
- * reg = <60>;
- * };
- * };
- *
- */
0 O, G( Q' |5 ~) h- ] ( `1 j" Q6 }, \
- B1 W4 O' H) s- p' M% v0 _+ n. M! O) x8 c4 a* v
在openrisc-3.1\drivers\i2c目录下的i2c-dev.c文件中,linux为我们提供了虚拟的统一的设备文件接口,如果我们懒得去编写设备驱动程序时,可以利用这个接口直接在应用层上完成对i2c总线的slave device操作。
3 h$ P' d0 v/ `" E, A* B2 k- I% j
% @7 l, u6 A4 [/ k* z
8 M9 _1 R( W, ]$ S- u9 [& `# U
所以在这一节当中,我们不编写总线驱动和设备驱动,先运用系统提供的文件接口进行操作,具体的代码主要参考hongtao_liu老师提供的代码。
+ e! G! s2 F8 D- {8 |8 h" N, i2 y" T) D7 L- B: P* [
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <fcntl.h> // open()
- #include <sys/ioctl.h>
- #include <unistd.h> // read() write() close() usleep()
- #include <linux/types.h>
- #include <linux/i2c.h> // i2c_msg
- #include <linux/i2c-dev.h> // i2c_rdwr_ioctl_data
- #define DEVICE_NAME "/dev/i2c-0"
- #define MAX_MSG 2
- #define EEPROM_ADDR 0x54
- //--------------------------------------------- main -----------------------------------------------------
- int main(){
- int fd;
- int ret;
- struct i2c_rdwr_ioctl_data eeprom_data;
- /* open device file */
- printf("\nstart simple_i2c application test ! \n");
- fd = open(DEVICE_NAME, O_RDWR);
- if (fd == -1){
- printf("open device %s error !\n",DEVICE_NAME);
- }
- else
- printf("open device file successful, fd = %d\n",fd);
- /* set parameter for msg */
- eeprom_data.nmsgs = MAX_MSG;
- eeprom_data.msgs=(struct i2c_msg *)malloc(eeprom_data.nmsgs * sizeof(struct i2c_msg));
- if( !eeprom_data.msgs ){
- printf("malloc error...\n");
- return -1;
- }
- /* timeout & retry */
- ioctl(fd, I2C_TIMEOUT, 1);
- ioctl(fd, I2C_RETRIES, 2);
- /* write data to eeprom */
- eeprom_data.nmsgs = 1;
- (eeprom_data.msgs[0]).len = 2;
- (eeprom_data.msgs[0]).addr = EEPROM_ADDR;
- (eeprom_data.msgs[0]).flags = 0;
- (eeprom_data.msgs[0]).buf = (unsigned char*)malloc(2);
- (eeprom_data.msgs[0]).buf[0] = 0x10;
- (eeprom_data.msgs[0]).buf[1] = 0x50;
- ret = ioctl(fd, I2C_RDWR, (unsigned long)&eeprom_data);
- if(ret < 0){
- printf("ioctl error...\n");
- }
- /* read data from eeprom */
- eeprom_data.nmsgs = 2;
- (eeprom_data.msgs[0]).len = 1;
- (eeprom_data.msgs[0]).addr = EEPROM_ADDR;
- (eeprom_data.msgs[0]).flags = 0;
- (eeprom_data.msgs[0]).buf[0] = 0x10;
- (eeprom_data.msgs[1]).len = 1;
- (eeprom_data.msgs[1]).addr = EEPROM_ADDR;
- (eeprom_data.msgs[1]).flags = I2C_M_RD;
- (eeprom_data.msgs[1]).buf = (unsigned char*)malloc(1);
- (eeprom_data.msgs[1]).buf[0] = 0;
- ret = ioctl(fd, I2C_RDWR, (unsigned long)&eeprom_data);
- if(ret < 0){
- printf("ioctl error...\n");
- }
- printf("buff[0]=%x\n",(eeprom_data.msgs[1]).buf[0]);
- /* close device file */
- ret = close(fd);
- printf ("ret=%d\n",ret);
- printf ("end simple_i2c test...\n");
- /* */
- return 0;
- }! J/ x2 r- j/ V3 x/ \
- |! f* r( b' C5 W |
j. v8 ]$ K, j( G+ |
2 l r- a; Y a! z* C% C1 C5 S/ y0 A/ O
编写完后在虚拟机上编译,老规矩,在板子上lrz后修改文件属性
. |& M- T; n7 @9 W5 g( T
2 W0 H/ W7 Z' w. J9 U* B
0 w% W( h- @; X! Y, N4 n
$ {( E; Q! p6 O( q0 J' z 然后可以多次修改其中写入地址和数据的代码,看看是否能正确读写) \2 v; `6 }9 g7 y3 }0 [) \# a
. n( K# ^7 M y2 o; _
* k# h7 p9 G3 v! Y' v
* t1 z4 F1 j5 h2 P+ W6 j2 @ 至此,有时间修改一下这个测试文件成比较实用的文件,下节对这个ipcore的i2c bus driver的分析,即对openrisc-3.1\drivers\i2c\busses目录下的i2c-ocores.c的总线驱动稍作分析,当是学习如何编写总线驱动。
+ a4 y& |0 L$ n* H7 f0 `* F |
|