GPIO模拟I2c

#include <platform/gpio.h>
#include <debug.h>
#include <string.h>
#include <smem.h>
#include <err.h>
#include <qtimer.h>


#define SDA 10
#define SCL 11
#define OUTP 1
#define INP 0

#define  gpio_set_direction(gpio,val)  gpio_tlmm_config(gpio, 0, 1, 0, 3, val)
#define  gpio_set_val(gpio,val)           gpio_set_dir(gpio, 2*val)

int lvds_i2c_start()
{

    gpio_set_direction(SDA, OUTP);
    gpio_set_direction (SCL, OUTP);
    gpio_set_val(SCL, 1);
    udelay(10);
    gpio_set_val(SDA, 1);
    udelay(10);

    gpio_set_val(SDA, 0);
    udelay(10);
    gpio_set_val(SCL, 0);
    udelay(10);
    return 0 ;
}

void lvds_i2c_stop()
{
    gpio_set_val(SCL, 0);
    udelay(10);
    gpio_set_direction(SDA, OUTP);
    gpio_set_val(SDA, 0);
    udelay(10);
    gpio_set_val(SCL, 1);
    udelay(10);
    gpio_set_val(SDA, 1);
    udelay(10);
}

unsigned char lvds_i2c_read_ack()
{
    unsigned char r;
    gpio_set_direction(SDA, INP);
    gpio_set_val(SCL,1);
    udelay(10);
    r = gpio_status(SDA);
    udelay(10);
    gpio_set_val(SCL,0);
    udelay(10);
    return r;
}

int lvds_i2c_send_ack()
{
    gpio_set_direction(SDA, OUTP);
    gpio_set_val(SDA, 0);
    udelay(10);
    gpio_set_val(SCL,1);
    udelay(10);
    gpio_set_val(SCL,0);
    udelay(10);
    return 0 ;
}
int lvds_i2c_send_noack()
{
    gpio_set_direction(SDA, OUTP);
    gpio_set_val(SDA, 1);
    udelay(10);
    gpio_set_val(SCL,1);
    udelay(10);
    gpio_set_val(SCL,0);
    udelay(10);
    return 0 ;
}

void lvds_i2c_write_byte(unsigned char b)
{
    int i;
    gpio_set_direction(SDA, OUTP);
    for (i=7; i>=0; i--) {
    udelay(10);
    gpio_set_val(SDA, !!(b & (1<<i)));
    udelay(10);
    gpio_set_val(SCL, 1);
    udelay(10);
    gpio_set_val(SCL, 0);
    }
    udelay(10);
}

unsigned char lvds_i2c_read_byte()
{
    int i;
    unsigned char r = 0;
    gpio_set_direction(SDA, INP);
    for (i=7; i>=0; i--) {
    gpio_set_val(SCL, 1);
    udelay(10);
    r = (r <<1) | gpio_status(SDA);
    gpio_set_val(SCL, 0);
    udelay(10);
    }
    return r;
}

void lvds_i2c_read(unsigned char addr, unsigned char* buf, int len)
{
    int i;
    unsigned char t;
    lvds_i2c_start();

    t = (addr << 1) | 1;
    lvds_i2c_write_byte(t);

    for (i=0; i<len; i++)
    buf[i] = lvds_i2c_read_byte();
    lvds_i2c_stop();
}

void lvds_i2c_write (unsigned char addr, unsigned char* buf, int len)
{
    int i;
    unsigned char t;
    lvds_i2c_start();

    t = (addr << 1) | 0;
    lvds_i2c_write_byte(t);

    for (i=0; i<len; i++)
    lvds_i2c_write_byte(buf[i]);
    lvds_i2c_stop();
}

//S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P

unsigned char lvds_i2c_read_one(unsigned char Slave_addr, unsigned char addr)
{
    unsigned char t,val;
    lvds_i2c_start();

    t = (Slave_addr << 1) | 0;

    lvds_i2c_write_byte(t);
    lvds_i2c_read_ack();

    lvds_i2c_write_byte(addr);
    lvds_i2c_read_ack();

    lvds_i2c_start();

    t = (Slave_addr << 1) | 1;

    lvds_i2c_write_byte(t);
    lvds_i2c_read_ack();

    val = lvds_i2c_read_byte();
    lvds_i2c_send_noack() ;
    lvds_i2c_stop();

    return val ;
}

//S Addr Wr [A] Comm [A] Data [A] P
void lvds_i2c_write_one(unsigned char addr, unsigned char val)
{
    unsigned char t;
    unsigned char Slave_addr = 0x2c;
    lvds_i2c_start();

    t = (Slave_addr << 1) | 0;
    lvds_i2c_write_byte(t);
    lvds_i2c_read_ack();

    lvds_i2c_write_byte(addr);
    lvds_i2c_read_ack();

    lvds_i2c_write_byte(val);
    lvds_i2c_read_ack();

    lvds_i2c_stop();
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值