Dspic33 Spi
Dspic33 Spi
Dspic33 Spi
DS70206D-page 18-1
S
e
r
i
a
l
P
e
r
i
p
h
e
r
a
l
I
n
t
e
r
f
a
c
e
(
S
P
I
)
18
Section 18. Serial Peripheral Interface (SPI)
HIGHLIGHTS
This section of the manual contains the following major topics:
18.1 Introduction.................................................................................................................. 18-2
18.2 SPI Registers............................................................................................................... 18-3
18.3 Modes of Operation.....................................................................................................18-7
18.4 Master Mode Clock Frequency..................................................................................18-20
18.5 SPI Operation with DMA............................................................................................18-21
18.6 Operation in Power-Saving Modes ............................................................................ 18-24
18.7 Special Function Registers Associated with SPI Modules.........................................18-25
18.8 Related Application Notes..........................................................................................18-26
18.9 Revision History.........................................................................................................18-27
dsPIC33F/PIC24H Family Reference Manual
DS70206D-page 18-2 2007-2011 Microchip Technology Inc.
18.1 INTRODUCTION
The Serial Peripheral Interface (SPI) module is a synchronous serial interface useful for
communicating with other peripheral or microcontroller devices. The peripheral devices can be
serial EEPROMs, shift registers, display drivers, A/D converters, etc. The SPI module is
compatible with Motorolas SPI and SIOP interfaces.
Depending on the variant, the dsPIC33F/PIC24H family offers one or two SPI modules on a
single device. The modules, designated SPI1 and SPI2, are functionally identical. The SPI1
module is available on all devices, while the SPI2 module is available in many of the higher pin
count packages.
The SPIx serial interface consists of four pins:
SDIx: Serial Data Input
SDOx: Serial Data Output
SCKx: Shift Clock Input or Output
SSx/FSYNCx: Active-Low Slave Select or Frame Synchronization I/O Pulse
Figure 18-1 is a block diagram of the SPI module.
Figure 18-1: SPI Module Block Diagram
Note: This family reference manual section is meant to serve as a complement to device
data sheets. Depending on the device variant, this manual section may not apply to
all dsPIC33F/PIC24H devices.
Please consult the note at the beginning of the Serial Peripheral Interface (SPI)
chapter in the current device data sheet to check whether this document supports
the device you are using.
Device data sheets and family reference manual sections are available for
download from the Microchip Worldwide Web site at: http://www.microchip.com.
Note: In this section, the SPI modules are referred together as SPIx, or separately as SPI1
and SPI2. Special Function Registers (SFRs) follow a similar notation. For example,
SPIxCON refers to the Control register for the SPI1 or SPI2 module.
Internal Data Bus
SDIx
SDOx
SSx
SCKx
SPIxSR
bit 0
Shift Control
Edge
Select
FCY Primary
1:1/4/16/64
Enable
Prescaler
Sync
SPIxBUF
Control
Transfer Transfer
Write SPIxBUF Read SPIxBUF
16
SPIxCON1<1:0>
SPIxCON1<4:2>
Master Clock
Clock
Control
Secondary
Prescaler
1:1 to 1:8
SPIxRXB SPIxTXB
2007-2011 Microchip Technology Inc. DS70206D-page 18-3
Section 18. Serial Peripheral Interface (SPI)
S
e
r
i
a
l
P
e
r
i
p
h
e
r
a
l
I
n
t
e
r
f
a
c
e
(
S
P
I
)
18
18.2 SPI REGISTERS
SPIxSTAT: SPIx Status and Control Register
The SPIx Status and Control register (SPIxSTAT) indicates various status conditions such as
receive overflow, transmit buffer full and receive buffer full. This register specifies the operation
of the module during Idle mode. It also contains a bit that enables and disables the module.
SPIxCON1: SPIx Control Register 1
The SPIx Control Register 1 (SPIxCON1) specifies the clock prescaler, Master/Slave mode,
Word/Byte communication, clock polarity and clock/data pin operation.
SPIxCON2: SPIx Control Register 2
The SPIx Control Register 2 (SPIxCON2) enables/disables the Framed SPI operation. This
register also specifies the frame synchronization pulse direction, polarity and edge selection.
SPIxBUF: SPIx Data Receive/Transmit Buffer Register
The SPIx Data Receive/Transmit Buffer register (SPIxBUF) is two separate internal registers: the
Transmit Buffer (SPIxTXB) and the Receive Buffer (SPIxRXB). These two unidirectional, 16-bit
registers share the SFR address of SPIxBUF. If a user application writes data to be transmitted
to the SPIxBUF address, internally the data is written to the SPIxTXB register.
Similarly, when the user application reads the received data from SPIxBUF, internally the data is
read from the SPIxRXB register.
The technique double-buffers transmit/receive operations and allows continuous data transfers
in the background. Transmission and reception occur simultaneously.
In addition, there is an internal 16-bit shift register (SPIxSR) that is not memory mapped. It shifts
data in and out of the SPI port.
dsPIC33F/PIC24H Family Reference Manual
DS70206D-page 18-4 2007-2011 Microchip Technology Inc.
Register 18-1: SPIxSTAT: SPIx Status and Control Register
R/W-0 U-0 R/W-0 U-0 U-0 U-0 U-0 U-0
SPIEN SPISIDL
bit 15 bit 8
U-0 R/C-0 U-0 U-0 U-0 U-0 R-0 R-0
SPIROV SPITBF SPIRBF
bit 7 bit 0
Legend: C =Clearable bit
R =Readable bit W =Writable bit U =Unimplemented bit, read as 0
-n =Value at POR 1 =Bit is set 0 =Bit is cleared x =Bit is unknown
bit 15 SPIEN: SPIx Enable bit
1 =Enables module and configures SCKx, SDOx, SDIx and SSx as serial port pins
0 =Disables module
bit 14 Unimplemented: Read as 0
bit 13 SPISIDL: Stop in Idle Mode bit
1 =Discontinue module operation when device enters Idle mode
0 =Continue module operation in Idle mode
bit 12-7 Unimplemented: Read as 0
bit 6 SPIROV: Receive Overflow Flag bit
1 =A new byte/word is completely received and discarded. The user software has not read the
previous data in the SPIxBUF register
0 =No overflow has occurred
bit 5-2 Unimplemented: Read as 0
bit 1 SPITBF: SPIx Transmit Buffer Full Status bit
1 =Transmit not yet started, SPIxTXB is full
0 =Transmit started, SPIxTXB is empty
Automatically set in hardware when CPU writes SPIxBUF location, loading SPIxTXB.
Automatically cleared in hardware when SPIx module transfers data from SPIxTXB to SPIxSR.
bit 0 SPIRBF: SPIx Receive Buffer Full Status bit
1 =Receive complete, SPIxRXB is full
0 =Receive is not complete, SPIxRXB is empty
Automatically set in hardware when SPIx transfers data from SPIxSR to SPIxRXB.
Automatically cleared in hardware when core reads SPIxBUF location, reading SPIxRXB.
2007-2011 Microchip Technology Inc. DS70206D-page 18-5
Section 18. Serial Peripheral Interface (SPI)
S
e
r
i
a
l
P
e
r
i
p
h
e
r
a
l
I
n
t
e
r
f
a
c
e
(
S
P
I
)
18
Register 18-2: SPIXCON1: SPIx Control Register 1
U-0 U-0 U-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
DISSCK DISSDO MODE16 SMP
(1)
CKE
(2)
bit 15 bit 8
R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
SSEN CKP MSTEN SPRE<2:0>
(3)
PPRE<1:0>
(3)
bit 7 bit 0
Legend:
R =Readable bit W =Writable bit U =Unimplemented bit, read as 0
-n =Value at POR 1 =Bit is set 0 =Bit is cleared x =Bit is unknown
bit 15-13 Unimplemented: Read as 0
bit 12 DISSCK: Disable SCKx pin bit (SPI Master mode only)
1 =Internal SPI clock is disabled, pin functions as I/O
0 =Internal SPI clock is enabled
bit 11 DISSDO: Disable SDOx pin bit
1 =SDOx pin is not used by the module; pin functions as I/O
0 =SDOx pin is controlled by the module
bit 10 MODE16: Word/Byte Communication Select bit
1 =Communication is word-wide (16 bits)
0 =Communication is byte-wide (8 bits)
bit 9 SMP: SPIx Data Input Sample Phase bit
(1)
Master mode:
1 =Input data sampled at end of data output time
0 =Input data sampled at middle of data output time
Slave mode:
SMP bit must be cleared when SPIx module is used in Slave mode.
bit 8 CKE: SPIx Clock Edge Select bit
(2)
1 =Serial output data changes on transition from active clock state to Idle clock state (refer to bit 6)
0 =Serial output data changes on transition from Idle clock state to active clock state (refer to bit 6)
bit 7 SSEN: Slave Select Enable bit (Slave mode)
1 =SSx pin is used for Slave mode
0 =SSx pin is not used by the module. Pin controlled by port function
bit 6 CKP: Clock Polarity Select bit
1 =Idle state for clock is a high level; active state is a low level
0 =Idle state for clock is a low level; active state is a high level
bit 5 MSTEN: Master Mode Enable bit
1 =Master mode
0 =Slave mode
bit 4-2 SPRE<2:0>: Secondary Prescale bits (Master mode)
(3)
111 =Secondary prescale 1:1
110 =Secondary prescale 2:1
2
0
0
7
-
2
0
1
1
M
i
c
r
o
c
h
i
p
T
e
c
h
n
o
l
o
g
y
I
n
c
.
D
S
7
0
2
0
6
D
-
p
a
g
e
1
8
-
2
5
S
e
c
t
i
o
n
1
8
.
S
e
r
i
a
l
P
e
r
i
p
h
e
r
a
l
I
n
t
e
r
f
a
c
e
(
S
P
I
)
S e r i a l P e r i p h e r a l
I n t e r f a c e ( S P I )
1
8
18.7 SPECIAL FUNCTION REGISTERS ASSOCIATED WITH SPI MODULES
Table 18-3: SPIx Register Map
SFR Name Bit 15 Bit 14 Bit 13 Bit 12 Bit 11 Bit 10 Bit 9 Bit 8 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
All
Resets
SPIxSTAT SPIEN SPISIDL SPIROV SPITBF SPIRBF 0000
SPIxCON1 DISSCK DISSDO MODE16 SMP CKE SSEN CKP MSTEN SPRE<2:0> PPRE<1:0> 0000
SPIxCON2 FRMEN SPIFSD FRMPOL FRMDLY 0000
SPIxBUF SPIx Transmit and Receive Buffer Register 0000
Legend: =unimplemented, read as 0. Reset values are shown in hexadecimal.
dsPIC33F/PIC24H Family Reference Manual
DS70206D-page 18-26 2007-2011 Microchip Technology Inc.
18.8 RELATED APPLICATION NOTES
This section lists application notes that are related to this section of the manual. These
application notes may not be written specifically for the dsPIC33F/PIC24H device families, but
the concepts are pertinent and could be used with modification and possible limitations. The
current application notes related to the Serial Peripheral Interface (SPI) module are:
Title Application Note #
Interfacing Microchips MCP41XXX and MCP42XXX Digital Potentiometers to
a PIC
Microcontroller AN746
Interfacing Microchips MCP3201 Analog-to-Digital Converter to the PIC
Microcontroller AN719
Note: For additional application notes and code examples for the dsPIC33F/PIC24H
family of devices, visit the Microchip web site (www.microchip.com).
2007-2011 Microchip Technology Inc. DS70206D-page 18-27
Section 18. Serial Peripheral Interface (SPI)
S
e
r
i
a
l
P
e
r
i
p
h
e
r
a
l
I
n
t
e
r
f
a
c
e
(
S
P
I
)
18
18.9 REVISION HISTORY
Revision A (April 2007)
This is the initial released version of this document.
Revision B (January 2008)
This revision incorporates the following content updates:
Registers:
- SPIxCON1: SPIx Control Register 1 register (see Register 18-2) Note 1 has been
added
Additional minor corrections such as language and formatting updates are incorporated
throughout the document
Revision C (January 2010)
This revision incorporates the following updates:
Added a note to the beginning of the section, which provides information on
complementary documentation
All references to dsPIC33F in the document are updated to dsPIC33F/PIC24H
Figures:
- In Figure 18-3, the CKE bit value has been changed from 0 to 1 for the fourth Clock
mode
- The text in Figure 18-7, (SPIx Slave, Framed Slave) has been replaced by (SPIx
Master, Framed Master)
- The text in Figure 18-13, (SPIx Slave/Framed Slave) has been replaced by (SPIx
Slave, Framed Master)
- The text in Figure 18-14, (SPIx Master/Framed Slave) has been replaced by (SPIx
Slave, Framed Slave)
Added Note 3 to Register 18-2
Additional minor corrections such as language and formatting updates were incorporated
throughout the document
Revision D (September 2011)
This revision includes the following updates:
Added Note 2 after step 6 in 18.3.2.1 Master Mode
Updated the second paragraph in 18.3.5 SPIx Error Handling
Updated the code in SPI Transmission and Reception with DMA (see Example 18-3)
Updated the SPIx Register Map (see Table 18-3)
Removed the SPI2 Register Map (Table 18-4)
The Preliminary document status was removed
Minor updates to text and formatting were incorporated throughout the document
dsPIC33F/PIC24H Family Reference Manual
DS70206D-page 18-28 2007-2011 Microchip Technology Inc.
NOTES:
2007-2011 Microchip Technology Inc. DS70206D-page 18 -29
Information contained in this publication regarding device
applications and the like is provided only for your convenience
and may be superseded by updates. It is your responsibility to
ensure that your application meets with your specifications.
MICROCHIP MAKES NO REPRESENTATIONS OR
WARRANTIES OF ANY KIND WHETHER EXPRESS OR
IMPLIED, WRITTEN OR ORAL, STATUTORY OR
OTHERWISE, RELATED TO THE INFORMATION,
INCLUDING BUT NOT LIMITED TO ITS CONDITION,
QUALITY, PERFORMANCE, MERCHANTABILITY OR
FITNESS FOR PURPOSE. Microchip disclaims all liability
arising from this information and its use. Use of Microchip
devices in life support and/or safety applications is entirely at
the buyers risk, and the buyer agrees to defend, indemnify and
hold harmless Microchip from any and all damages, claims,
suits, or expenses resulting from such use. No licenses are
conveyed, implicitly or otherwise, under any Microchip
intellectual property rights.
Trademarks
The Microchip name and logo, the Microchip logo, dsPIC,
KEELOQ, KEELOQ logo, MPLAB, PIC, PICmicro, PICSTART,
PIC
32
logo, rfPIC and UNI/O are registered trademarks of
Microchip Technology Incorporated in the U.S.A. and other
countries.
FilterLab, Hampshire, HI-TECH C, Linear Active Thermistor,
MXDEV, MXLAB, SEEVAL and The Embedded Control
Solutions Company are registered trademarks of Microchip
Technology Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, chipKIT,
chipKIT logo, CodeGuard, dsPICDEM, dsPICDEM.net,
dsPICworks, dsSPEAK, ECAN, ECONOMONITOR,
FanSense, HI-TIDE, In-Circuit Serial Programming, ICSP,
Mindi, MiWi, MPASM, MPLAB Certified logo, MPLIB,
MPLINK, mTouch, Omniscient Code Generation, PICC,
PICC-18, PICDEM, PICDEM.net, PICkit, PICtail, REAL ICE,
rfLAB, Select Mode, Total Endurance, TSHARC,
UniWinDriver, WiperLock and ZENA are trademarks of
Microchip Technology Incorporated in the U.S.A. and other
countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
All other trademarks mentioned herein are property of their
respective companies.
2007-2011, Microchip Technology Incorporated, Printed in
the U.S.A., All Rights Reserved.
Printed on recycled paper.
ISBN: 978-1-61341-625-9
Note the following details of the code protection feature on Microchip devices:
Microchip products meet the specification contained in their particular Microchip Data Sheet.
Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchips Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
Microchip is willing to work with the customer who is concerned about the integrity of their code.
Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as unbreakable.
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchips code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Microchip received ISO/TS-16949:2009 certification for its worldwide
headquarters, design and wafer fabrication facilities in Chandler and
Tempe, Arizona; Gresham, Oregon and design centers in California
and India. The Companys quality system processes and procedures
are for its PIC
MCUs and dsPIC
DSCs, KEELOQ
code hopping
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchips quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.
DS70206D-page 18 -30 2007-2011 Microchip Technology Inc.
AMERICAS
Corporate Office
2355 West Chandler Blvd.
Chandler, AZ 85224-6199
Tel: 480-792-7200
Fax: 480-792-7277
Technical Support:
http://www.microchip.com/
support
Web Address:
www.microchip.com
Atlanta
Duluth, GA
Tel: 678-957-9614
Fax: 678-957-1455
Boston
Westborough, MA
Tel: 774-760-0087
Fax: 774-760-0088
Chicago
Itasca, IL
Tel: 630-285-0071
Fax: 630-285-0075
Cleveland
Independence, OH
Tel: 216-447-0464
Fax: 216-447-0643
Dallas
Addison, TX
Tel: 972-818-7423
Fax: 972-818-2924
Detroit
Farmington Hills, MI
Tel: 248-538-2250
Fax: 248-538-2260
Indianapolis
Noblesville, IN
Tel: 317-773-8323
Fax: 317-773-5453
Los Angeles
Mission Viejo, CA
Tel: 949-462-9523
Fax: 949-462-9608
Santa Clara
Santa Clara, CA
Tel: 408-961-6444
Fax: 408-961-6445
Toronto
Mississauga, Ontario,
Canada
Tel: 905-673-0699
Fax: 905-673-6509
ASIA/PACIFIC
Asia Pacific Office
Suites 3707-14, 37th Floor
Tower 6, The Gateway
Harbour City, Kowloon
Hong Kong
Tel: 852-2401-1200
Fax: 852-2401-3431
Australia - Sydney
Tel: 61-2-9868-6733
Fax: 61-2-9868-6755
China - Beijing
Tel: 86-10-8569-7000
Fax: 86-10-8528-2104
China - Chengdu
Tel: 86-28-8665-5511
Fax: 86-28-8665-7889
China - Chongqing
Tel: 86-23-8980-9588
Fax: 86-23-8980-9500
China - Hangzhou
Tel: 86-571-2819-3187
Fax: 86-571-2819-3189
China - Hong Kong SAR
Tel: 852-2401-1200
Fax: 852-2401-3431
China - Nanjing
Tel: 86-25-8473-2460
Fax: 86-25-8473-2470
China - Qingdao
Tel: 86-532-8502-7355
Fax: 86-532-8502-7205
China - Shanghai
Tel: 86-21-5407-5533
Fax: 86-21-5407-5066
China - Shenyang
Tel: 86-24-2334-2829
Fax: 86-24-2334-2393
China - Shenzhen
Tel: 86-755-8203-2660
Fax: 86-755-8203-1760
China - Wuhan
Tel: 86-27-5980-5300
Fax: 86-27-5980-5118
China - Xian
Tel: 86-29-8833-7252
Fax: 86-29-8833-7256
China - Xiamen
Tel: 86-592-2388138
Fax: 86-592-2388130
China - Zhuhai
Tel: 86-756-3210040
Fax: 86-756-3210049
ASIA/PACIFIC
India - Bangalore
Tel: 91-80-3090-4444
Fax: 91-80-3090-4123
India - New Delhi
Tel: 91-11-4160-8631
Fax: 91-11-4160-8632
India - Pune
Tel: 91-20-2566-1512
Fax: 91-20-2566-1513
Japan - Yokohama
Tel: 81-45-471- 6166
Fax: 81-45-471-6122
Korea - Daegu
Tel: 82-53-744-4301
Fax: 82-53-744-4302
Korea - Seoul
Tel: 82-2-554-7200
Fax: 82-2-558-5932 or
82-2-558-5934
Malaysia - Kuala Lumpur
Tel: 60-3-6201-9857
Fax: 60-3-6201-9859
Malaysia - Penang
Tel: 60-4-227-8870
Fax: 60-4-227-4068
Philippines - Manila
Tel: 63-2-634-9065
Fax: 63-2-634-9069
Singapore
Tel: 65-6334-8870
Fax: 65-6334-8850
Taiwan - Hsin Chu
Tel: 886-3-5778-366
Fax: 886-3-5770-955
Taiwan - Kaohsiung
Tel: 886-7-536-4818
Fax: 886-7-330-9305
Taiwan - Taipei
Tel: 886-2-2500-6610
Fax: 886-2-2508-0102
Thailand - Bangkok
Tel: 66-2-694-1351
Fax: 66-2-694-1350
EUROPE
Austria - Wels
Tel: 43-7242-2244-39
Fax: 43-7242-2244-393
Denmark - Copenhagen
Tel: 45-4450-2828
Fax: 45-4485-2829
France - Paris
Tel: 33-1-69-53-63-20
Fax: 33-1-69-30-90-79
Germany - Munich
Tel: 49-89-627-144-0
Fax: 49-89-627-144-44
Italy - Milan
Tel: 39-0331-742611
Fax: 39-0331-466781
Netherlands - Drunen
Tel: 31-416-690399
Fax: 31-416-690340
Spain - Madrid
Tel: 34-91-708-08-90
Fax: 34-91-708-08-91
UK - Wokingham
Tel: 44-118-921-5869
Fax: 44-118-921-5820
Worldwide Sales and Service
08/02/11