abstract: this article explains how to interface a gpio port to an spi peripheral. it defines the basics of the spi protocol, the signals and the four transfer variations for the sck signal. a software routine is included to implement the spi interface using the max7651, a flash programmable 12-bit integrated data-acquisition system.
introductionthe spi™ bus is a 4-wire serial communications interface used by many microprocessor peripheral chips. the max7651 microprocessor does not include dedicated hardware to implement the interface. however, simple software routines are shown that can send and receive data to spi peripherals.
the 4 pins of the spi interface are as follows:
sck (serial data clock): data is shifted/latched on the rising or falling edge of sck (see next section).
mosi (master output/slave input): data is transmitted out of this pin if the chip is a master and into this pin if the chip is a slave.
miso (master input/slave output): data is receivedinto this pin if the chip is a master and transmitted out of this pin if the chip is a slave.
active-low cs (chip select, active low): tells the peripheral that a transfer is about to begin.the spi protocol defines four combinations of sck phase and polarity with respect to the data. if a chip has control bits to set these four states, they are generally referred to as cpol (clock polarity) and cpha (clock phase). spi always transfers data in 8-bit blocks. you can transfer as many 8-bit blocks as you want. for example, a 16-bit write asserts active-low cs from the beginning of the first 8 bits until the end of the sixteenth bit.
table 1. the four variations
cpol
cpha
transfer
0
0
sck rising-edge transfer.
sck transitions in middle of bit timing.
1
0
sck falling-edge transfer.
sck transitions in middle of bit timing.
0
1
sck falling-edge transfer.
sck transitions at beginning of bit timing.
1
1
sck rising-edge transfer.
sck transitions at beginning of bit timing.
when reading a data sheet for a spi peripheral, it is common to see a reference to the spi mode as cpol = 0, cpha = 0, etc., even though the chip itself does not physically contain these bit definitions. rather, the spi interface is hard-wired to send/receive data as if the cpol and cpha bits had been set to 0. for example, the max5154 12-bit dac uses a spi interface with rising-edge mid-bit data transfer. this corresponds to cpol = 0, cpha = 0 protocol. because this is by far the most common spi transfer, that is the example code we will discuss. see the following diagram, which is from the max5154 data sheet. the signal active-low cs is the spi signal active-low ss, sclk is sck, and din would be connected to modi, because the peripheral is a slave, with only an input (no readback). this part uses 16 bits in the transfer.
figure 1. serial interface timing diagram.
code example: 8-bit data transferthe following example is the most common type of spi transfer, with cpo = 0 and cpha = 0. the routine does not assume anything about the clock speed of the max7651, as the i/o port bits are simply bit-banged as fast as possible. data-transfer times are shown at the end of this section.
figure 2 shows typical connections between the max7651/max7652 and an spi peripheral.
figure 2. general spi connection.
the number {n} inside the comment field is the number of clock cycles to execute the instruction.
; spi data transfer for 8-bit peripherals. max7651 is master; peripheral is slave.
;
; the following spi-defined pins are used. (some peripherals are write only, so only 3 wires are needed.)
; sck: the data transfer clock
; mis master input data (from peripheral), not always used
; mosi: master output data (to peripheral)
; ss: slave select (active low)
;
sck
equ
p1.0
miso
equ
p1.1
mosi
equ
p1.2
cs
equ
p1.3
; use port 1, but this is 100% arbitrary. use any available pins.
;
; now we need to use some of the internal ram in the max7651 as data storage.
; for speed of execution, two of these variables must be located in the ram area
; that allows bit addressing within the byte. in the max7651, the ram space corresponds
; to addresses 20h to 2fh. addresses below 20h or above 2fh cannot be bit-addressed!
spi_in
equ
20h
; result of 8-bit read from slave.
spi_out
equ
21h
; data we wish to send to slave.
;
; lastly, we need a loop counter to keep track of sending the 8 bits.
; this can be either an 'r' register (r0-r7) or
; any ram register (doesn't have to be bit-addressable). let's use a ram
; register.
loop
equ
30h
; can be anywhere in the map; this is just an example.
;
; it is assumed that when called, the chip select bit ss is already set to 1.
spi_i
clr
sck
; sck starts off low. {1}
clr
cs
; clearing cs begins the data transfer. {1}
setb
miso
; to be used as input, must be set internally. {1}
mov
loop,#8
; eight bits to transfer. {3}
xfer:
mov
c,spi_out.7
; move bit 7 into carry (spi is msb first). {2}
mov
mosi,c
; i/o port reflects the carry bit, which is the data bit. {2}
setb
sck
; generate sck rising edge, after data is stable. {1}
mov
c,miso
; read data from slave into carry (optional). {2}
mov
spi_in.7,c
; copy into the received data byte, bit-7 position. {2}
clr
sck
; generate sck falling edge, after data read in. {1}
mov
a,spi_out
; accumulator is temp holder for shift operation. {2}
rl
a
; rotate left (but not through carry!). {1}
mov
spi_out, a
; prepare bit 7 for next transfer to slave. {2}
mov
a,spi_in
; get previous slave read data. {2}
rl
a
; rotate left to get next bit position into proper spot. {1}
mov
spi_in,a
; save result. {2}
djnz
loop,xfer
; decrement loop. jump if not zero to xfer. {3}
; transfer done.
setb
cs
; de-assert chip select. {1}
.end
; tell assembler code completed.
the total number of cpu cycles to transfer 8 bits (both read and write to slave) is 6 + 8 × 23 + 1 = 191. for reading or writing only, the total is 6 + 8 × 18 + 1 = 151 cpu cycles. the following table shows various transfer rates using common max7651 clock speeds.
table 2. transfer rates
max7651 clock speed
bit transfer time
total read/write
transfer time
12mhz
7.95us (~125khz)
63.6us
11.0592mhz
8.63us (~116khz)
69.08us
4mhz
23.88us (~41.9khz)
191us
from this table, we can see that the fastest spi byte transfer is approximately 15.7khz whereas the slowest rate is 5.2khz. this is much slower than a dedicated 1mhz spi hardware port! therefore, if the max7651 is to be used as a slave, then the spi master must be set for the slowest bit transfer speed (125khz) and the max7651 must operate at a 12mhz clock speed.
三大巨头达成战略合作,龙芯版UOS系统将全面预装搜狗输入法Linux版
如何理解加密货币和区块链的密码学
日韩贸易纠纷却已愈来愈烈,中国制造商能受益?
微雪电子S29GL128P NorFlash存储模块简介
智能测控终端-现场数据采集+无线传输+远程控制的一体化
外围设备的SPI接口的MAX7651处理器-Interfac
射频功率放大器RFX2401C功放芯片和AT2401C功放芯片的对比
频谱分析仪的工作原理和如何提高测试灵敏度
核动力机器人登陆遥远星球探索外星生命
光耦失效的几种常见原因及分析
电阻柜智能监控装置安装在接地电阻柜内如何进行接线?
iPhone11价格最高却还是4G手机将成为其最大的弱势和黑点
3D打印技术:骑着水熊的山顶洞女
荣耀运动蓝牙耳机xSport魅焰红版首销 售299元
智能家居系统构成是什么_智能家居系统怎么安装
如何让自动驾驶系统运行100公里只耗1度电?元戎启行推理引擎亮相CES 2021
获红杉中国、华为哈勃投资的华旋传感器获评江苏省“专精特新中小企业”
智能硬件真的能实现健康管理吗?
无线充电器原理设计与电路解析—电路精选(23)
爱立信支持开放Open RAN技术,为5G网络架构创新提供更多可能