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

📄 spi-summary

📁 linux 内核源代码
💻
📖 第 1 页 / 共 2 页
字号:
Overview of Linux kernel SPI support====================================21-May-2007What is SPI?------------The "Serial Peripheral Interface" (SPI) is a synchronous four wire seriallink used to connect microcontrollers to sensors, memory, and peripherals.It's a simple "de facto" standard, not complicated enough to acquire astandardization body.  SPI uses a master/slave configuration.The three signal wires hold a clock (SCK, often on the order of 10 MHz),and parallel data lines with "Master Out, Slave In" (MOSI) or "Master In,Slave Out" (MISO) signals.  (Other names are also used.)  There are fourclocking modes through which data is exchanged; mode-0 and mode-3 are mostcommonly used.  Each clock cycle shifts data out and data in; the clockdoesn't cycle except when there is a data bit to shift.  Not all data bitsare used though; not every protocol uses those full duplex capabilities.SPI masters use a fourth "chip select" line to activate a given SPI slavedevice, so those three signal wires may be connected to several chipsin parallel.  All SPI slaves support chipselects; they are usually activelow signals, labeled nCSx for slave 'x' (e.g. nCS0).  Some devices haveother signals, often including an interrupt to the master.Unlike serial busses like USB or SMBus, even low level protocols forSPI slave functions are usually not interoperable between vendors(except for commodities like SPI memory chips).  - SPI may be used for request/response style device protocols, as with    touchscreen sensors and memory chips.  - It may also be used to stream data in either direction (half duplex),    or both of them at the same time (full duplex).  - Some devices may use eight bit words.  Others may different word    lengths, such as streams of 12-bit or 20-bit digital samples.  - Words are usually sent with their most significant bit (MSB) first,    but sometimes the least significant bit (LSB) goes first instead.  - Sometimes SPI is used to daisy-chain devices, like shift registers.In the same way, SPI slaves will only rarely support any kind of automaticdiscovery/enumeration protocol.  The tree of slave devices accessible froma given SPI master will normally be set up manually, with configurationtables.SPI is only one of the names used by such four-wire protocols, andmost controllers have no problem handling "MicroWire" (think of it ashalf-duplex SPI, for request/response protocols), SSP ("SynchronousSerial Protocol"), PSP ("Programmable Serial Protocol"), and otherrelated protocols.Some chips eliminate a signal line by combining MOSI and MISO, andlimiting themselves to half-duplex at the hardware level.  In factsome SPI chips have this signal mode as a strapping option.  Thesecan be accessed using the same programming interface as SPI, but ofcourse they won't handle full duplex transfers.  You may find suchchips described as using "three wire" signaling: SCK, data, nCSx.(That data line is sometimes called MOMI or SISO.)Microcontrollers often support both master and slave sides of the SPIprotocol.  This document (and Linux) currently only supports the masterside of SPI interactions.Who uses it?  On what kinds of systems?---------------------------------------Linux developers using SPI are probably writing device drivers for embeddedsystems boards.  SPI is used to control external chips, and it is also aprotocol supported by every MMC or SD memory card.  (The older "DataFlash"cards, predating MMC cards but using the same connectors and card shape,support only SPI.)  Some PC hardware uses SPI flash for BIOS code.SPI slave chips range from digital/analog converters used for analogsensors and codecs, to memory, to peripherals like USB controllersor Ethernet adapters; and more.Most systems using SPI will integrate a few devices on a mainboard.Some provide SPI links on expansion connectors; in cases where nodedicated SPI controller exists, GPIO pins can be used to create alow speed "bitbanging" adapter.  Very few systems will "hotplug" an SPIcontroller; the reasons to use SPI focus on low cost and simple operation,and if dynamic reconfiguration is important, USB will often be a moreappropriate low-pincount peripheral bus.Many microcontrollers that can run Linux integrate one or more I/Ointerfaces with SPI modes.  Given SPI support, they could use MMC or SDcards without needing a special purpose MMC/SD/SDIO controller.I'm confused.  What are these four SPI "clock modes"?-----------------------------------------------------It's easy to be confused here, and the vendor documentation you'llfind isn't necessarily helpful.  The four modes combine two mode bits: - CPOL indicates the initial clock polarity.  CPOL=0 means the   clock starts low, so the first (leading) edge is rising, and   the second (trailing) edge is falling.  CPOL=1 means the clock   starts high, so the first (leading) edge is falling. - CPHA indicates the clock phase used to sample data; CPHA=0 says   sample on the leading edge, CPHA=1 means the trailing edge.   Since the signal needs to stablize before it's sampled, CPHA=0   implies that its data is written half a clock before the first   clock edge.  The chipselect may have made it become available.Chip specs won't always say "uses SPI mode X" in as many words,but their timing diagrams will make the CPOL and CPHA modes clear.In the SPI mode number, CPOL is the high order bit and CPHA is thelow order bit.  So when a chip's timing diagram shows the clockstarting low (CPOL=0) and data stabilized for sampling during thetrailing clock edge (CPHA=1), that's SPI mode 1.How do these driver programming interfaces work?------------------------------------------------The <linux/spi/spi.h> header file includes kerneldoc, as does themain source code, and you should certainly read that chapter of thekernel API document.  This is just an overview, so you get the bigpicture before those details.SPI requests always go into I/O queues.  Requests for a given SPI deviceare always executed in FIFO order, and complete asynchronously throughcompletion callbacks.  There are also some simple synchronous wrappersfor those calls, including ones for common transaction types like writinga command and then reading its response.There are two types of SPI driver, here called:  Controller drivers ... controllers may be built in to System-On-Chip	processors, and often support both Master and Slave roles.	These drivers touch hardware registers and may use DMA.	Or they can be PIO bitbangers, needing just GPIO pins.  Protocol drivers ... these pass messages through the controller	driver to communicate with a Slave or Master device on the	other side of an SPI link.So for example one protocol driver might talk to the MTD layer to exportdata to filesystems stored on SPI flash like DataFlash; and others mightcontrol audio interfaces, present touchscreen sensors as input interfaces,or monitor temperature and voltage levels during industrial processing.And those might all be sharing the same controller driver.A "struct spi_device" encapsulates the master-side interface betweenthose two types of driver.  At this writing, Linux has no slave sideprogramming interface.There is a minimal core of SPI programming interfaces, focussing onusing the driver model to connect controller and protocol drivers usingdevice tables provided by board specific initialization code.  SPIshows up in sysfs in several locations:   /sys/devices/.../CTLR ... physical node for a given SPI controller   /sys/devices/.../CTLR/spiB.C ... spi_device on bus "B",	chipselect C, accessed through CTLR.   /sys/bus/spi/devices/spiB.C ... symlink to that physical   	.../CTLR/spiB.C device   /sys/devices/.../CTLR/spiB.C/modalias ... identifies the driver	that should be used with this device (for hotplug/coldplug)   /sys/bus/spi/drivers/D ... driver for one or more spi*.* devices   /sys/class/spi_master/spiB ... symlink (or actual device node) to	a logical node which could hold class related state for the	controller managing bus "B".  All spiB.* devices share one	physical SPI bus segment, with SCLK, MOSI, and MISO.Note that the actual location of the controller's class state dependson whether you enabled CONFIG_SYSFS_DEPRECATED or not.  At this time,the only class-specific state is the bus number ("B" in "spiB"), sothose /sys/class entries are only useful to quickly identify busses.How does board-specific init code declare SPI devices?------------------------------------------------------Linux needs several kinds of information to properly configure SPI devices.That information is normally provided by board-specific code, even forchips that do support some of automated discovery/enumeration.DECLARE CONTROLLERSThe first kind of information is a list of what SPI controllers exist.For System-on-Chip (SOC) based boards, these will usually be platformdevices, and the controller may need some platform_data in order tooperate properly.  The "struct platform_device" will include resourceslike the physical address of the controller's first register and its IRQ.Platforms will often abstract the "register SPI controller" operation,maybe coupling it with code to initialize pin configurations, so thatthe arch/.../mach-*/board-*.c files for several boards can all share thesame basic controller setup code.  This is because most SOCs have severalSPI-capable controllers, and only the ones actually usable on a givenboard should normally be set up and registered.So for example arch/.../mach-*/board-*.c files might have code like:	#include <asm/arch/spi.h>	/* for mysoc_spi_data */	/* if your mach-* infrastructure doesn't support kernels that can	 * run on multiple boards, pdata wouldn't benefit from "__init".	 */	static struct mysoc_spi_data __init pdata = { ... };	static __init board_init(void)	{		...		/* this board only uses SPI controller #2 */		mysoc_register_spi(2, &pdata);		...	}And SOC-specific utility code might look something like:	#include <asm/arch/spi.h>	static struct platform_device spi2 = { ... };	void mysoc_register_spi(unsigned n, struct mysoc_spi_data *pdata)	{		struct mysoc_spi_data *pdata2;		pdata2 = kmalloc(sizeof *pdata2, GFP_KERNEL);		*pdata2 = pdata;		...		if (n == 2) {			spi2->dev.platform_data = pdata2;			register_platform_device(&spi2);			/* also: set up pin modes so the spi2 signals are			 * visible on the relevant pins ... bootloaders on			 * production boards may already have done this, but			 * developer boards will often need Linux to do it.			 */		}		...	}Notice how the platform_data for boards may be different, even if thesame SOC controller is used.  For example, on one board SPI might usean external clock, where another derives the SPI clock from currentsettings of some master clock.DECLARE SLAVE DEVICESThe second kind of information is a list of what SPI slave devices existon the target board, often with some board-specific data needed for thedriver to work correctly.Normally your arch/.../mach-*/board-*.c files would provide a small tablelisting the SPI devices on each board.  (This would typically be only asmall handful.)  That might look like:	static struct ads7846_platform_data ads_info = {		.vref_delay_usecs	= 100,		.x_plate_ohms		= 580,		.y_plate_ohms		= 410,	};	static struct spi_board_info spi_board_info[] __initdata = {	{		.modalias	= "ads7846",		.platform_data	= &ads_info,		.mode		= SPI_MODE_0,

⌨️ 快捷键说明

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