⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 l3-algo-bit_c.txt

📁 2410uda1341的测试代码
💻 TXT
字号:
/*
 * L3 bus algorithm module.
 *
 *  Copyright (C) 2001 Russell King, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *  Note that L3 buses can share the same pins as I2C buses, so we must
 *  _not_ generate an I2C start condition.  An I2C start condition is
 *  defined as a high-to-low transition of the data line while the clock
 *  is high.  Therefore, we must only change the data line while the
 *  clock is low.
 */
//#define DEBUG 1
#include "debug.h"
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/sched.h>

#include "l3.h"
#include "algo-bit.h"

#ifdef DEBUG
#undef pr_debug
#define pr_debug(fmt,arg...) printk(fmt,##arg)
#endif

#define setdat(adap,val)	adap->setdat(adap->data, val)
#define setclk(adap,val)	adap->setclk(adap->data, val)
#define setmode(adap,val)	adap->setmode(adap->data, val)
#define setdatin(adap)		adap->setdir(adap->data, 1)
#define setdatout(adap)		adap->setdir(adap->data, 0)
#define getdat(adap)		adap->getdat(adap->data)

/*
 * Send one byte of data to the chip.  Data is latched into the chip on
 * the rising edge of the clock.
 */
static void sendbyte(struct l3_algo_bit_data *adap, unsigned int byte)
{
	int i;

//	pr_debug("--- %s\n", __FUNCTION__);
	for (i = 0; i < 8; i++) {
		setclk(adap, 0);
		udelay(adap->data_hold);
		setdat(adap, byte & 1);
		udelay(adap->data_setup);
		setclk(adap, 1);
		udelay(adap->clock_high);
		byte >>= 1;
	}
}

/*
 * Send a set of bytes to the chip.  We need to pulse the MODE line
 * between each byte, but never at the start nor at the end of the
 * transfer.
 */
static void sendbytes(struct l3_algo_bit_data *adap, const char *buf, int len)
{
	int i;

//	pr_debug("--- %s\n", __FUNCTION__);
	for (i = 0; i < len; i++) {
		if (i) {
			udelay(adap->mode_hold);
			setmode(adap, 0);
			udelay(adap->mode);
		}
		setmode(adap, 1);
		udelay(adap->mode_setup);
		sendbyte(adap, buf[i]);
	}
}

/*
 * Read one byte of data from the chip.  Data is latched into the chip on
 * the rising edge of the clock.
 */
static unsigned int readbyte(struct l3_algo_bit_data *adap)
{
	unsigned int byte = 0;
	int i;

//	pr_debug("--- %s\n", __FUNCTION__);
	for (i = 0; i < 8; i++) {
		setclk(adap, 0);
		udelay(adap->data_hold + adap->data_setup);
		setclk(adap, 1);
		if (getdat(adap))
			byte |= 1 << i;
		udelay(adap->clock_high);
	}

	return byte;
}

/*
 * Read a set of bytes from the chip.  We need to pulse the MODE line
 * between each byte, but never at the start nor at the end of the
 * transfer.
 */
static void readbytes(struct l3_algo_bit_data *adap, char *buf, int len)
{
	int i;

//	pr_debug("--- %s\n", __FUNCTION__);
	for (i = 0; i < len; i++) {
		if (i) {
			udelay(adap->mode_hold);
			setmode(adap, 0);
		}
		setmode(adap, 1);
		udelay(adap->mode_setup);
		buf[i] = readbyte(adap);
	}
}

static int l3_xfer(struct l3_adapter *l3_adap, struct l3_msg msgs[], int num)
{
	struct l3_algo_bit_data *adap = l3_adap->algo_data;
	int i;

	/*
	 * If we share an I2C bus, ensure that it is in STOP mode
	 */
//	pr_debug("--- %s\n", __FUNCTION__);
	setclk(adap, 1);
	setdat(adap, 1);
	setmode(adap, 1);
	setdatout(adap);
	udelay(adap->mode);

	for (i = 0; i < num; i++) {
		struct l3_msg *pmsg = &msgs[i];

		if (!(pmsg->flags & L3_M_NOADDR)) {
			setmode(adap, 0);
			udelay(adap->mode_setup);
			sendbyte(adap, pmsg->addr);
			udelay(adap->mode_hold);
		}

		if (pmsg->flags & L3_M_RD) {
			setdatin(adap);
			readbytes(adap, pmsg->buf, pmsg->len);
		} else {
			setdatout(adap);
			sendbytes(adap, pmsg->buf, pmsg->len);
		}
	}

	/*
	 * Ensure that we leave the bus in I2C stop mode.
	 */
	setclk(adap, 1);
	setdat(adap, 1);
	setmode(adap, 0);
	setdatin(adap);

	return num;
}

static struct l3_algorithm l3_bit_algo = {
	name:	"L3 bit-shift algorithm",
	xfer:	l3_xfer,
};

int l3_bit_add_bus(struct l3_adapter *adap)
{
	adap->algo = &l3_bit_algo;
	pr_debug("--- %s\n", __FUNCTION__);
	return l3_add_adapter(adap);
}

int l3_bit_del_bus(struct l3_adapter *adap)
{
	pr_debug("--- %s\n", __FUNCTION__);
	return l3_del_adapter(adap);
}

EXPORT_SYMBOL(l3_bit_add_bus);
EXPORT_SYMBOL(l3_bit_del_bus);


MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("l3 bit banging");
MODULE_AUTHOR("RMK");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -