TA的每日心情 | 怒 2019-11-20 15:22 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
+ a1 X4 v9 D a% b8 n: r' @6 m与前一节添加GPIO Controller的步骤一样,进入虚拟机中的关于SOC的设备树文件目录openrisc-3.1\arch\openrisc\boot\dts添加关于板子上I2C器件的设备描述
2 L1 S `+ j9 P- i! u# f, m. a( U, U- @& [* o
& _, ?: b w1 N6 B# p8 T+ b# U; N: E1 v, A, q( C8 j% ]
open之,在其中加入关于板子上的I2C器件的描述,例如我板子上有个AT24c08,所以我就添加eeprom设备,形式如下:
) g# n- U- R) C
) V3 S8 W# v6 e# @8 I- 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>;
- };
- };
6 K/ R2 n" o- w' E, ^. l
5 E, A7 j, [9 \$ X3 i1 o. ^ 至于关于其中编写方法后述。
; g4 q: \3 n0 \8 k! n; \5 V% @1 `" Y' H: Z7 R9 r( W
然后,在命令行中进入linux源码目录,然后需要配置内核加入上面添加的I2C的总线驱动,这是因为关于这个ipcore的驱动社区上的大虾已经编写好,现在先熟悉和使用,在后面再去详细分析这个总线驱动如何编写。: N5 c' `2 {4 U6 u) |( y+ G6 m
* B$ l' Y8 |* a, ^1 f
% N! x$ j8 d# v. {* W) v; F5 V! k
内核配置,进入General setup项
' d* _/ d; h1 [( B$ q
G7 P+ u1 I1 ^$ D9 y1 d
8 b& P5 K. z- k9 a, U* ^3 d
1 @( ~# ?' V9 z
将Prompt for development and/or incomplete code/drivers打开
! w6 |# y" ~5 P4 |
) J; R8 P% L u+ {" O* X
: c6 q* w- a+ D. M v$ j
& _( p/ H V+ [1 Y6 F' Y Exit后,到Device Drivers项中
: l4 @8 \" U, ~( |$ A
6 B0 S2 d( G9 c- V" p) ?5 z
: M9 _2 @2 h9 |4 `- K) q
& d, O# \# P G w1 u2 f$ n 将I2C support选择编入内核中: O, @% R4 i5 P" V2 T; A7 X R3 G2 h
" U3 w: W1 B' a
0 x4 W* N6 K1 G4 w, @2 M' }4 r
, q/ h$ C8 \9 R8 E `
进入,将I2C device inteRFace选中编入内核,我们第一个应用层的测试文件就基于内核编写好的设备接口文件来写
6 j1 {' T& S9 m/ {& g- @+ a: O6 N/ \' i# D
1 `! }+ D7 ], k' I9 @1 `' J5 N4 A( b/ F' H* y. ^
然后选择I2C Hardware Bus support中
' i3 ^2 K' I1 e; q) Q# M0 F+ _4 K. O* K
$ }& L9 g4 A! @# C! j5 U" y- I- K/ X+ u( `! I
将OpenCores I2C Controller选择编入内核
. D$ |; f1 j& A
+ k( |' n& C3 j, Q P
/ ^5 S& r' W4 B7 d+ h! B5 L+ O
: i7 Z3 W( W) t 好,最后exit出来选择保存,重新make
1 }) T, B4 p! G7 i4 Y% w# E* o
4 s5 x4 h" |5 k* C2 m. M/ l3 U
8 j' \; W' b3 H( n
N: T4 Q' I' p9 J$ \! J( G( M 之后的步骤按照之前移植linux的步骤一样,生成uImage镜像,然后下载到开发板上面即可。
) v2 w, |. Y- @- S. g! p
/ N2 a6 h2 Q1 I7 v 然后在boot linux的过程和进入/dev目录下可以看到i2c bus driver和生成设备文件i2c-x( x+ x$ [7 ^8 O% D2 L) ~ I7 u) K4 x
, i" x/ M, g- F6 Y" V
+ U) }/ {4 C$ v; o# F5 f. H3 u3 o( ~( u1 l
至此,i2c的总线驱动就添加到内核了。% s7 k/ S5 m) g/ s T
8 k% }) e) _: L5 j3 e' K- b6 J
根据我自己的理解:
9 Y0 k( U) C; K; [% ^. Z" Y1 S! g
1.对于i2c controller来说,编写的驱动程序称为总线驱动,例如上面在or1200_soc中添加的i2c控制器ipcore,为之编写的驱动程序就是总线驱动。0 P6 B# p/ `( X$ F e
( B8 f' L; k; a5 V% |: x) v
2.对于挂在i2c总线上的slave device来说,为之编写的驱动程序称为设备驱动,例如我板子上挂在这个ipcore上的AT24c08的eeprom。
$ b! o- T: |0 o7 y+ G: g- O
8 o7 t, `7 R* u. @% J 3.linux下的i2c子系统中还有一个i2c核心模块来为总线驱动和设备驱动服务,完成注册,删除······功能。) u1 W, ]) m0 C1 p
- _3 x9 ]6 b% D* z' p+ c& h; O
现在说明一下:在openrisc-3.1\drivers\i2c\busses目录下的i2c-ocores.c文件为soc上添加的i2c controller提供了总线驱动,即在刚才的内核配置中已经选择编入了内核,所以可以略过总线驱动的编写,在下节我们着重分析这个总线驱动。
5 q, x1 x6 A7 F+ [4 Q; y, \
$ P7 D& L; {3 b8 H* X
7 Y) a2 T) K" ~ T
/ x5 o9 W" Y- q- {% S 现在可以先打开这个文件,里面的comment中就有关于device-tree文件如何加入i2c device描述的说明4 ~0 ?5 Q M1 q N+ V
% e6 H4 A7 Q* L# N, x- /*
- * 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>;
- * };
- * };
- *
- */
* [$ V) P+ F4 }8 P. V8 u! N 3 d# I) _6 K, j T8 V* V
. V* s' [; }" f' }
+ G0 C. P! i' v1 v 在openrisc-3.1\drivers\i2c目录下的i2c-dev.c文件中,linux为我们提供了虚拟的统一的设备文件接口,如果我们懒得去编写设备驱动程序时,可以利用这个接口直接在应用层上完成对i2c总线的slave device操作。+ `& a D+ o @$ y8 A/ M
" C$ \, i, e5 G- T0 ^: X
' F; a2 d. t* a+ V
3 ~2 U2 a4 F0 p 所以在这一节当中,我们不编写总线驱动和设备驱动,先运用系统提供的文件接口进行操作,具体的代码主要参考hongtao_liu老师提供的代码。
# I9 L/ w9 C3 [% d+ R( o+ G
& K6 G5 o% K4 Q( ~+ w- #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;
- }# B% K! B" f- ~( A# n
- x* F0 _& Q( w, Q) Z, B
1 Q# h! u7 G7 ]5 e5 [$ \1 X! e2 O
9 c* G# w( M" _. _! y8 ^ P) S+ V4 E
编写完后在虚拟机上编译,老规矩,在板子上lrz后修改文件属性
! M: ]$ J y. u% I, m( x' }
1 D7 c; t$ L3 l$ i/ Y) A3 U
5 v& x& R0 u& `! O8 T
$ W9 k) F$ A, e& s& y 然后可以多次修改其中写入地址和数据的代码,看看是否能正确读写
/ c2 c: U2 F8 l4 V
' X/ Y5 J# u/ j* s& @- V
2 y1 E/ m( N; h' U: P0 R( ` a5 [7 W; O, Q' u# l
至此,有时间修改一下这个测试文件成比较实用的文件,下节对这个ipcore的i2c bus driver的分析,即对openrisc-3.1\drivers\i2c\busses目录下的i2c-ocores.c的总线驱动稍作分析,当是学习如何编写总线驱动。 Z% c" C- u H7 Z3 h
|
|