SPI
Pin control Programming Debugging C Interface Reference Platform Notes Release Notes DownloadSPI Sessions
NSDSP supports SPI communications using ICSPCLK and ICSPDAT pins. Only master mode is supported - NSDSP acts like an SPI master, the target device is SPI slave. For many PICs with PPS it is possible to route a standard SPI module to these pins and use it for SPI communications.
ICSPCLK line is used as an SPI clock and is driven by NSDSP programmer.
ICSPDAT line is bidirectional and is used as both MISO and MOSI. This means that any given SPI transmission is either read or write, but not both at the same time.
To start SPI communications you need to do the following:
When the SPI communications are finished, you need to end the session and free NSDSP. When you end the session, the library will automatically wait for the completion of all the pending SPI transactions.
The following pseudo-code shows the required procedure:
NSDSP.create(serial)
NSDSP.rate = 3000000
Session = NSDSP.startSPISession
Session.CKE = Off
Session.SMP = On
// Communications here
NSDSP.endSession
NSDSP.free
The rate cannot be specified exactly. NSDSP supports a number of pre-defined SPI frequencies - 50 kHz, 100 kHz, 250 kHz, 500 kHz, 1 MHz, 1.5 MHz, 2 MHz, 2.4 MHz, 3 MHz, 4 MHz, 6 MHz. When you specify a rate which does not match any of these frequencies, the software will round your rate down to match one of pre-defined frequencies. If you specify a rate below 50 kHz, the software will use 50 kHz.
CKE specifies clock edges. If CKE is set then the data is changing on the falling edge of ICSPCLK and are sampled on the rising edge of ICSPCLK. If CKE is not set, the data is changing on the rising edge of ICSPCLK and are sampled on the falling edge of ICSPCLK, Idle ICSPCLK polarity cannot be changed and it is always low.
SMP affects reception. If SMP is set, NSDSP will delay data sampling by half of the clock cycle. We recommend setting SMP in most cases.
SPI mode also allows direct pin manipulation, so you can use the LVP pin as a chip select pin if necessary
To organize fast SPI communications, read about principles of NSDSP data transfers.
© 2007-2025 Northern Software Inc. All Rights Reserved.