本文主要分享如何在龙芯1c上实现linux下的spi驱动。
这里假设已经对spi有一定了解,不了解的自己百度。
使用硬件SPI
基础
百度上已经有很多关于linux下spi驱动的文章,讲得很好很全。比如:linux下spi驱动分为三层——SPI核心层、SPI控制器层、SPI设备驱动层。
其中SPI核心层是硬件无关的;SPI控制器层是SPI总线中master的驱动,是平台移植相关的,也就是龙芯1c上spi控制器的驱动,通常龙芯开发板里面已经实现了这部分;SPI设备驱动层是具体spi设备的驱动,通常只需要实现这部分就可以了。
说了这么多,到底怎样实现一个spi驱动。简单来说,只需要在platform.c中找到“static struct spi_board_info ls1x_spi0_devices[]”,加入类似
<span style="font-size:18px;"><strong><span style="color:#6633FF;"><span style="background-color: rgb(255, 255, 255);">#ifdef CONFIG_SPI_MCP3201
{
.modalias = "mcp3201",
.bus_num = 0,
.chip_select = SPI0_CS3,
.max_speed_hz = 1000000,
},
#endif</span></span></strong></span>
就可以使用spi_read(),spi_write()和spi_write_then_read()来收发数据了。就这么简单
示例一:MCP3201驱动
上面的结构体中包含了spi设备的硬件接线情况,从其中可以知道设备mcp3201使用的是spi0的cs3,spi频率最大为1000000。下面来看看驱动源码
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/sysfs.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/mutex.h>
#include <linux/mod_devicetable.h>
#include <linux/spi/spi.h>
#define DRVNAME "mcp3201"
#define REFERENCE 5000
struct mcp3201 {
struct device *hwmon_dev;
struct mutex lock;
u32 channels;
u32 reference; /* in millivolts */
const char *name;
};
/* sysfs hook function */
static ssize_t mcp3201_read(struct device *dev,
struct device_attribute *devattr, char *buf, int differential)
{
struct spi_device *spi = to_spi_device(dev);
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct mcp3201 *adc = spi_get_dr