📄 lm70_x86.c.txt
字号:
/*
* LM70_x86 v1.0 11/05/01
* www.embeddedlinuxinterfacing.com
*
* The original location of this code is
* http://www.embeddedlinuxinterfacing.com/chapters/10/
*
* Copyright (C) 2001 by Craig Hollabaugh
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* LM70_x86.c is based on procfs_example.c by Erik Mouw.
* For more information, please see The Linux Kernel Procfs Guide, Erik Mouw
* http://kernelnewbies.org/documents/kdoc/procfs-guide/lkprocfsguide.html
*
*/
/* LM70_x86
* This device driver demonstrates communication with a LM70 temperature
* sensor using SPI signalling. This routine implements SPI communication
* in software (bit-banging). The driver creates a /proc directory entry
* called /proc/trailblazer/temperature. Scripts can read the current
* temperature from this file.
*
* This device driver communicates with a National Semiconductor
* LM70CIMM-3 MUA08A connected in the following manner.
*
* Parallel Temperature
* Port Direction Sensor
* D0 2 -> V+ 5
* D1 3 -> /CS 7
* D2 4 -> SC 2
* GND 25 - GND 4
* Select 13 <- SI/O 1
*/
/*
gcc -O2 -D__KERNEL__ -DMODULE -I/usr/src/linux/include -c LM70_x86.c -o LM70_x86.o
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <asm/io.h>
#define MODULE_VERSION "1.0"
#define MODULE_NAME "LM70_x86"
static struct proc_dir_entry *tb_dir,
*temperature_file;
#define SPPDATAPORT 0x378
#define SPPSTATUSPORT (SPPDATAPORT + 1)
#define SPPCONTROLPORT (SPPDATAPORT + 2)
/* status port bit definitions */
#define SIO 0x10
/* data port bit definitions */
#define VCC 0x01
#define nCS 0x02
#define SC 0x04
/* bit delay time, in usecs */
#define DELAY 140
unsigned char data;
#define clkLow() outb(data & ~SC ,SPPDATAPORT)
#define clkHigh() outb(data | SC ,SPPDATAPORT)
#define assertCS() outb(data & ~nCS,SPPDATAPORT)
#define deassertCS() outb(data | nCS,SPPDATAPORT)
#define readBit() (SIO == (inb(SPPSTATUSPORT) & SIO))
/*
* function initializeSensor
* This function initializes the 'data' variable with VCC then
* applies power to the LM70 by writing 'data' to the parallel
* port. The /CS and SC lines are then put in an initialization state.
*
*/
void initializeSensor(void)
{
data = VCC;
outb(data ,SPPDATAPORT); /* write the data to the parallel port */
clkHigh(); /* clock high and /CS are SPI init state */
deassertCS();
}
/*
* function powerdownSensor
* This function removes power from the LM70 by zeroing 'data'
* and writing it to the parallel port. This function is called
* when the device driver module is removed
*
*/
void powerdownSensor(void)
{
data = 0;
outb(data ,SPPDATAPORT); /* write the data to the parallel port */
}
/*
* function getTemperature
* This function perform SPI communications with the LM70. The
* LM70 clocks serial out on the falling edge of the SC signal.
* This routine reads the SI/O line for this serial stream after
* the rising edge of SC signal. The LM70 sends MSB first, this
* routine performs the bit shifts. The signals, SC and /CS, are
* intentionally slowed down to allow settling time on long
* communication lines with extra capacitance.
*
* returns
* getTemperature function returns the temperature in degrees C
* in 1 degree increments
*
* This routine doesn't perform communication error checking or
* reporting.
*
*/
int getTemperature(void)
{
unsigned char i;
int temperature;
assertCS(); /* enter the start condition */
udelay(DELAY);
temperature = 0;
for (i = 0; i < 16; i++) {
clkLow();
udelay(DELAY);
clkHigh();
udelay(DELAY);
temperature = ((temperature << 1) | readBit());
/* shift the temperature bits then OR the input bit into the LSB */
}
deassertCS();
/*
* Now we to scale the temperature value. Simply shifting the
* bits right doesn't perform the correct operation if the
* temperature is below zero. Shifting introduces 0's into the
* MSB thus losing the negative sign bit. An integer divide by 32
* preserves the sign bit and eliminates the last unused 5 bits
*
* At this point, after the divide by 32. temperature contains
* the count of LSB bits where 1 LSB equals 0.25C. An additional
* divide by 4 scales temperature in 1 degree increments.
*/
temperature /= 128; /* 32 * 4 = 128 */
return temperature;
}
/*
* function proc_read_temperature
* This function is called when the user performs a read operation
* on /proc/trailblazer/temperature. This function fills the page
* buffer with a temperature in degrees C.
*
*/
static int proc_read_temperature(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
int len, temp;
temp = getTemperature();
len = sprintf(page, "%+d", temp); /* temp * 9 / 5 + 32 here for F */
return len;
}
/*
* function init_LM70_x86
* This initialization function creates the /proc/trailblazer
* directory and a temperature entry in it then initializes the
* LM70 temperature sensor
*
*/
static int __init init_LM70_x86(void)
{
int rv = 0;
/* Create the trailblazer /proc entry */
tb_dir = proc_mkdir("trailblazer", NULL);
if(tb_dir == NULL) {
rv = -ENOMEM;
goto out;
}
tb_dir->owner = THIS_MODULE;
/* Create temperature and make it readable by all - 0444 */
temperature_file = create_proc_entry("temperature", 0444, tb_dir);
if(temperature_file == NULL) {
rv = -ENOMEM;
goto no_temperature;
}
temperature_file->data = NULL;
temperature_file->read_proc = &proc_read_temperature;
temperature_file->write_proc = NULL;
temperature_file->owner = THIS_MODULE;
initializeSensor();
/* everything initialized */
printk(KERN_INFO "%s %s initialized\n",
MODULE_NAME, MODULE_VERSION);
return 0;
no_temperature:
remove_proc_entry("trailblazer", NULL);
out:
return rv;
}
/*
* function cleanup_LM70_x86
* This clean function powers down the sensor and removes
* the /proc/trailblazer directory and the temperature entry.
*/
static void __exit cleanup_LM70_x86(void)
{
powerdownSensor();
remove_proc_entry("temperature", tb_dir);
remove_proc_entry("trailblazer", NULL);
printk(KERN_INFO "%s %s removed\n", MODULE_NAME, MODULE_VERSION);
}
module_init(init_LM70_x86);
module_exit(cleanup_LM70_x86);
MODULE_AUTHOR("Craig Hollabaugh");
MODULE_DESCRIPTION("Trailblazer LM70_x86");
EXPORT_NO_SYMBOLS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -