找回密码
 注册
关于网站域名变更的通知
查看: 544|回复: 3
打印 上一主题 下一主题

linux学习之路_or1200下linux的i2c(二)

[复制链接]
  • TA的每日心情

    2019-11-20 15:22
  • 签到天数: 2 天

    [LV.1]初来乍到

    跳转到指定楼层
    1#
    发表于 2021-8-5 10:26 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式

    EDA365欢迎您登录!

    您需要 登录 才可以下载或查看,没有帐号?注册

    x

    + L- ^- p: W$ u# X与前一节添加GPIO Controller的步骤一样,进入虚拟机中的关于SOC的设备树文件目录openrisc-3.1\arch\openrisc\boot\dts添加关于板子上I2C器件的设备描述
    " B1 o/ n3 Q' a# J# b9 B6 c7 }/ `/ S% c
    ) v) ]1 P/ ?* m5 m% V/ u
    # \9 t5 J9 L6 U
    / q9 g7 O! y- T) }8 \        open之,在其中加入关于板子上的I2C器件的描述,例如我板子上有个AT24c08,所以我就添加eeprom设备,形式如下:! c$ T* d8 U; N! Z* U8 o* E2 w
    * i& N2 l9 N3 p$ S5 O4 K
    • 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>;
    •     };
    • };: j8 L6 h( c: l- }6 p
       
    ' \3 g# W) Z9 k' }* }- r4 L0 e        至于关于其中编写方法后述。
    & f; _% K5 r! G# H4 T
    2 a  O& \8 K6 Y, s8 w" P        然后,在命令行中进入linux源码目录,然后需要配置内核加入上面添加的I2C的总线驱动,这是因为关于这个ipcore的驱动社区上的大虾已经编写好,现在先熟悉和使用,在后面再去详细分析这个总线驱动如何编写。
    3 G4 z0 l$ \' R" ^
    * n7 ]# d: L2 }) x0 v, h* T 3 Y3 F: Z5 R0 L& l  r0 T

    ; ]- U+ m5 _9 A( {        内核配置,进入General setup项9 X4 h0 T  ~. T! Q5 i

    % R7 d- y. @$ K5 p" j3 v" \
    ; D, Z5 X9 d5 u0 E! I$ h0 q6 w2 |* M1 r4 k6 n
            将Prompt for development and/or incomplete code/drivers打开
    ; B& M$ F! d7 d/ l" W' d
    7 f- H  ]% a: n1 o- G# L
    0 Z" U3 W2 X1 J- [% }
    1 P  q4 J3 B; H( A- p        Exit后,到Device Drivers项中
    : y0 \' S( P: w, E2 T. L
    2 ^) }/ `, Y- c
    ! C' z5 |) s  c
    ) \5 G! ^6 D& C+ h, V        将I2C support选择编入内核中
    0 ]; K7 Z- W: |% K) Z2 y) p0 t3 H! Q) c6 T

    " d+ x8 K8 R, s. X6 y$ \8 l2 S" t- \* D( ]" Q3 q: A" {% s' {6 R
            进入,将I2C device inteRFace选中编入内核,我们第一个应用层的测试文件就基于内核编写好的设备接口文件来写% |8 M6 O$ g$ L" O5 [

    % x4 |/ Q: ?; D
    + ~7 ]3 U9 j1 w3 V2 _7 @  Z
    7 V' R8 V! Z5 W, a6 b9 w' w        然后选择I2C Hardware Bus support中! x) N) j4 H. Z" w+ V; Q) f- C$ G

    ) L$ _5 J7 H8 [% e( ` : v& I) g6 m1 G) R- N$ {( M

    # Z+ |$ }% M$ K4 L& d3 Z        将OpenCores I2C Controller选择编入内核! S) M+ S; n3 p
      W) ?, r! r; C9 h1 A

    - d* l% }- n* W  c0 w- ^
    - o# Q! Y2 P! y4 R        好,最后exit出来选择保存,重新make; t# [+ V3 C4 m

    . D# u* j3 n* C1 ^" F4 V, `2 C ) R3 g9 a  P  y
    - {/ W* |% C! d! I5 X
            之后的步骤按照之前移植linux的步骤一样,生成uImage镜像,然后下载到开发板上面即可。3 M4 F; Y7 a& E- P) `- f
    ' z. R, `* E/ Z' V: k; U2 F! d
            然后在boot linux的过程和进入/dev目录下可以看到i2c bus driver和生成设备文件i2c-x5 C1 K3 ?- f2 z. ^

    - x( N/ N3 B7 i! M) F8 C7 A4 G
    ! ~( K, N# K( d
    . K. x: H. K. C9 F        至此,i2c的总线驱动就添加到内核了。2 p/ W6 R7 W" e' ^5 T9 I

    $ p) \) W  _1 Y: c        根据我自己的理解:
    9 x- e4 K: W; T/ S1 A6 y( _/ U6 M: D5 q1 K
            1.对于i2c controller来说,编写的驱动程序称为总线驱动,例如上面在or1200_soc中添加的i2c控制器ipcore,为之编写的驱动程序就是总线驱动。7 M3 B' S& G; v5 q- h+ N
    . A# w' G) T, I7 u! e" b& v- ?
            2.对于挂在i2c总线上的slave device来说,为之编写的驱动程序称为设备驱动,例如我板子上挂在这个ipcore上的AT24c08的eeprom。8 X* ?% W2 H+ e* `7 E5 y# G5 J/ x7 |

    9 e  C4 N+ C' n' m- ]        3.linux下的i2c子系统中还有一个i2c核心模块来为总线驱动和设备驱动服务,完成注册,删除······功能。
    " Z# y" X4 w* P+ y4 D$ n+ E7 t: b& w3 Q1 Z5 z
            现在说明一下:在openrisc-3.1\drivers\i2c\busses目录下的i2c-ocores.c文件为soc上添加的i2c controller提供了总线驱动,即在刚才的内核配置中已经选择编入了内核,所以可以略过总线驱动的编写,在下节我们着重分析这个总线驱动。
    ( i+ a' _9 E$ U- m7 r
    ! e# x% R% B: \# E & ~) y! t, S& Y* C" j& g0 j3 i3 a
    . b  p5 @5 Q2 ]
            现在可以先打开这个文件,里面的comment中就有关于device-tree文件如何加入i2c device描述的说明
    7 ^* ?& \- {2 b- U: e
    # s9 l; f) o9 D- 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>;
    • *              };
    • *  };
    • *
    • */4 D4 s+ ~& R& v7 b5 b# `

    : b% [2 X4 V9 C( L" H- G; O( O/ I1 Y; R, x: R8 S& b5 L

    ' V8 i3 @# K2 P        在openrisc-3.1\drivers\i2c目录下的i2c-dev.c文件中,linux为我们提供了虚拟的统一的设备文件接口,如果我们懒得去编写设备驱动程序时,可以利用这个接口直接在应用层上完成对i2c总线的slave device操作。
    % n5 h! ^% h3 \5 c  z1 U2 P+ D3 d/ l9 z( ?: X+ Z

    + i; C0 ~( a" @: q
    1 y5 B( l4 J6 U4 w        所以在这一节当中,我们不编写总线驱动和设备驱动,先运用系统提供的文件接口进行操作,具体的代码主要参考hongtao_liu老师提供的代码。
    $ }% e  m8 I1 p6 J7 A* ^6 G
      j6 w" W+ M! D3 {4 J- f
    • #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;
    • }6 }" ~- Y, {2 O. n4 C9 Y

    2 x" c8 m5 W* A) K* }( u& Z
    / D' {; Q1 j/ E$ F- u
    1 i: n, d2 x1 n+ w$ i
    3 |' h: }8 o/ _        编写完后在虚拟机上编译,老规矩,在板子上lrz后修改文件属性+ }4 P6 Q, R7 a( `5 m& E2 X

    7 H5 S. N" Y) \: i7 a. @9 ~
    - g2 }/ o1 s$ V/ v; s. b
    ) d0 o# a/ I/ ?1 d# s1 _! ^0 p" y        然后可以多次修改其中写入地址和数据的代码,看看是否能正确读写
    - J6 Y0 [+ N. g" E3 x
    7 l" ~' k% d* c9 e* e; I9 B5 X & K1 t$ \1 G5 Y

    % t6 D3 L3 j6 P: x" s2 ]0 E- ]        至此,有时间修改一下这个测试文件成比较实用的文件,下节对这个ipcore的i2c bus driver的分析,即对openrisc-3.1\drivers\i2c\busses目录下的i2c-ocores.c的总线驱动稍作分析,当是学习如何编写总线驱动。  {/ G5 Q  d# \  W- k
  • TA的每日心情
    慵懒
    2020-6-13 15:46
  • 签到天数: 1 天

    [LV.1]初来乍到

    4#
    发表于 2021-8-5 18:54 | 只看该作者
    or1200下linux的i2c

    该用户从未签到

    3#
    发表于 2021-8-5 18:53 | 只看该作者
    or1200下linux的i2c

    该用户从未签到

    2#
    发表于 2021-8-5 14:17 | 只看该作者
    or1200下linux的i2c
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-11-24 09:40 , Processed in 0.187500 second(s), 27 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表