Revision: 1621
Author: [email protected]
Date: Sun Jan 24 12:51:04 2010
Log: added SPI introduction tutorial, added link to spi intro tutorial in
sd card tutorial & 23k256 tutorial. added sources to sd card tutorial
http://code.google.com/p/jallib/source/detail?r=1621
Added:
/trunk/doc/dita/tutorials/images/spi_intro_data_transfer.jpg
/trunk/doc/dita/tutorials/tutorial_spi_introduction.xml
Modified:
/trunk/doc/dita/tutorials/tutorial_sd_card.xml
/trunk/doc/dita/tutorials/tutorial_spi_sram_23k256.xml
/trunk/doc/dita/tutorials/tutorials.ditamap
=======================================
--- /dev/null
+++ /trunk/doc/dita/tutorials/images/spi_intro_data_transfer.jpg Sun Jan 24
12:51:04 2010
Binary file, no diff available.
=======================================
--- /dev/null
+++ /trunk/doc/dita/tutorials/tutorial_spi_introduction.xml Sun Jan 24
12:51:04 2010
@@ -0,0 +1,183 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!-- This document was created with Syntext Serna Free. --><!DOCTYPE topic
PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd" []>
+<topic id="sram_23k256">
+ <title>SPI Introduction</title>
+ <prolog>
+ <author>Matthew Schinkel</author>
+ <publisher>Jallib Group</publisher>
+ </prolog>
+ <body>
+ <p id="firstcontent">Introduction to SPI - Serial Peripheral
interface</p>
+ <section>
+ <title>What is SPI?</title>
+ <p>SPI is a protocol is simply a way to send data from device to
device in a serial fashion (bit by bit). This protocol is used for things
like SD memory cards, MP3 decoders, memory devices and other high speed
applications.</p>
+ <p>We can compare SPI to other data transfer protocols:</p>
+ <table>
+ <title>Protocol Comparison Chart</title>
+ <tgroup cols="4">
+ <thead>
+ <row>
+ <entry/>
+ <entry>SPI</entry>
+ <entry>RS-323</entry>
+ <entry>I2C</entry>
+ </row>
+ </thead>
+ <tbody>
+ <row>
+ <entry>PINS</entry>
+ <entry>3 + 1 per device</entry>
+ <entry>2</entry>
+ <entry>2</entry>
+ </row>
+ <row>
+ <entry>Number Of Devices</entry>
+ <entry>unlimited</entry>
+ <entry>2</entry>
+ <entry>127</entry>
+ </row>
+ <row>
+ <entry>Bits in one data byte transfer</entry>
+ <entry>8</entry>
+ <entry>10 (8 bytes + 1 start bit + 1 stop bit)</entry>
+ <entry>9 (8 bytes + 1 ack bit)</entry>
+ </row>
+ <row>
+ <entry>Must send one device address byte before
transmission</entry>
+ <entry>No</entry>
+ <entry>No</entry>
+ <entry>Yes</entry>
+ </row>
+ <row>
+ <entry>Clock Type</entry>
+ <entry>Master clock only</entry>
+ <entry>Both device clocks must match</entry>
+ <entry>Master Clock that slave can influence</entry>
+ </row>
+ <row>
+ <entry>Data can transfer in two directions at the same time
(full-duplex)</entry>
+ <entry>Yes</entry>
+ <entry>Yes</entry>
+ <entry>No</entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </table>
+ <p>As you can see SPI sends the least bit's per data byte
transfer byte and does not need to send a device address before
transmission. This makes SPI the fastest out of the three we compared.</p>
+ </section>
+ <section>
+ <title>How does SPI work?</title>
+ <p>Firstly, SPI works in a master/slave setup. The master is the one
that sends the clock pulses. At each pulse, data will be sent and
received.</p>
+ <p>SPI has a chip select pin. Every device will share the
"SDI", "SDO" and "Clock" pins, but each
device will have it's own chip select pin (also known as slave
select). This means we can have a virtually unlimited number of devices on
the same SPI bus. You should also note that the chip select pin can be
active high or active low depending on the device.</p>
+ <p>For some devices, the chip select pin must stay enabled
throughout the transmission, and others require a change in the chip select
line before the next transmission.</p>
+ <p>SPI is Dual-Duplex. This means data can be sent and received at
the same time. If you wish to send data and not receive any, the PIC will
receive data anyways. You may ignore the return byte.</p>
+ <p>Here's a diagram showing the way in which SPI sends &
receives data:</p>
+ <image href="images/spi_intro_data_transfer.jpg"/>
+ </section>
+ <section>
+ <title>SPI Modes</title>
+ <p>If you are using a device that does not yet have a Jallib
library, you will need to get the devices SPI mode. SPI devices can be set
to run in 4 different modes depending on the clock polarity & data
sample rising or falling edge.</p>
+ <p><b>Clock Polarity (CKP)</b> - Determines if the clock is normally
high or normally low during it's idle state.</p>
+ <p>If CKP is set to 1, the clock line will be high during idle.
With CKP=0 the clock will be low during idle.</p>
+ <p><b>Data Clock Edge (CKE)</b> - The edge that the data is sampled
on (rising edge or falling edge)</p>
+ <p>If CKP = 0, CKE = 0 - Data is read on the clocks rising edge
(idle to active clock state)</p>
+ <p>If CKP = 0, CKE = 1 - Data is read on the clocks falling edge
(active to idle clock state)</p>
+ <p>If CKP =1, CKE = 0 - Data is read on the clocks falling edge
(idle to active clock state)</p>
+ <p>If CKP = 1, CKE = 1 - Data is read on the clocks rising edge
(active to idle clock state)</p>
+ <p>We can put this in a chart to name the modes:</p>
+ <p><table>
+ <title>SPI MODE NAMES</title>
+ <tgroup cols="3">
+ <colspec/>
+ <colspec/>
+ <colspec/>
+ <thead>
+ <row>
+ <entry>MODE NAME</entry>
+ <entry>CKP</entry>
+ <entry>CKE</entry>
+ </row>
+ </thead>
+ <tbody>
+ <row>
+ <entry>0,0 </entry>
+ <entry>0</entry>
+ <entry>1</entry>
+ </row>
+ <row>
+ <entry>0,1</entry>
+ <entry>0</entry>
+ <entry>0</entry>
+ </row>
+ <row>
+ <entry>1,0</entry>
+ <entry>1</entry>
+ <entry>1</entry>
+ </row>
+ <row>
+ <entry>1,1</entry>
+ <entry>1</entry>
+ <entry>0</entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </table></p>
+ </section>
+ <p><b>Note:</b> I noticed the mode numbers & mode table on
Wikipedia is different then the table in the Microchip PDF. I am going by
the Microchip PDF, as well as the tested and working PIC Jallib library +
samples. Wikipedia also names these registers CPOL/CPHA instead of
CKP/CKE.</p>
+ <section>
+ <title>Using The Jallib Library</title>
+ <p>At the moment, there is only a SPI master hardware library,
therefore any device you wish to control must be connected to the
PIC's SDI, SDO, SCK pins. The chip select pin can be any digital
output pin.</p>
+ <p>The library requires you to set the pin directions of the SDI,
SDO, SCK lines as follows:</p>
+ <codeblock>-- setup SPI
+include spi_master_hw -- first include the library
+
+-- define SPI inputs/outputs
+pin_sdi_direction = input -- spi data input
+pin_sdo_direction = output -- spi data output
+pin_sck_direction = output -- spi data clock</codeblock>
+ <p>You only need to set the pin direction of the chip select pin,
the PIC will set the direction of the SDI, SDO & SCK for you. You will
Alias this chip select pin as required by the device's jallib
library.</p>
+ <p>If you are using more then one device in your circuit, you will
need to declare your chip select pin near the beginning of your program. If
you do not do this at the beginning of your program, some of your devices
may receive data because their chip select pin could be enabled during
init procedures of other devices on the SPI bus.</p>
+ <codeblock>-- choose your SPI chip select pin
+-- pin_SS is the PIC's slave select (or chip select) pin.
+ALIAS device_chip_select_direction is pin_SS_direction
+ALIAS device_chip_select is pin_SS
+device_chip_select_direction = output -- chip select/slave select pin
+device_chip_select = low -- disable the device</codeblock>
+ <p>Now the last step in setting up the SPI library is to use the
init procedure. </p>
+ <p>Use the SPI mode name chart to get your SPI mode. The modes can
be any of the following:</p>
+ <p>SPI_MODE_00</p>
+ <p>SPI_MODE_01</p>
+ <p>SPI_MODE_10</p>
+ <p>SPI_MODE_11</p>
+ <p>You will also need to set the spi bus speed. Here is a list of
the speeds you may choose from:</p>
+ <p>SPI_RATE_FOSC_4 -- oscillator / 4</p>
+ <p>SPI_RATE_FOSC_16 -- oscillator / 16</p>
+ <p>SPI_RATE_FOSC_64 -- oscillator / 64</p>
+ <p>SPI_RATE_TMR -- PIC's internal timer</p>
+ <p>You will use the following init procedure with your custom values
entered:</p>
+ <codeblock>spi_init(SPI_MODE_11,SPI_RATE_FOSC_16) -- choose spi mode
and speed</codeblock>
+ <p>Now your ready to use the procedures to send and receive data.
First you must enable the device with the chip select line:</p>
+ <codeblock>device_chip_select = high -- enable the device</codeblock>
+ <p>You can use the pseudo variable spi_master_hw to send and receive
data as follows:</p>
+ <codeblock>-- send decimal 50 to spi bus
+spi_master_hw = 50</codeblock>
+ <p>Or receive data like this:</p>
+ <codeblock>-- receive data from the spi port into byte x
+var byte x
+x = spi_master_hw</codeblock>
+ <p>You can also send and receive data at the same time with the
spi_master_hw_exchange procedure. here's an example:</p>
+ <codeblock>-- send decimal byte 50 and receive data into byte x
+var byte x
+x = spi_master_hw_exchange (50)</codeblock>
+ <p>When your done transmitting & receiving data, don't
forget to disable your device</p>
+ <codeblock>device_chip_select = low -- enable the device</codeblock>
+ <p>Alright, now you should be able to implement SPI into any of your
own devices. If you need assistance, contact us at the <xref
href="http://tech.groups.yahoo.com/group/jallist/" format="html">Jallist
Support Group</xref> or at <xref
href="http://groups.google.com/group/jallib/topics?gvc=2"
format="html">Jallib Group</xref>.</p>
+ </section>
+ <section>
+ <title>References</title>
+ <p><b>The Jallib spi_master_hw library</b> - Written by William
Welch</p>
+ <p><b>Microchip Technology SPI Overview</b> - <xref
href="http://ww1.microchip.com/downloads/en/devicedoc/spi.pdf"
format="html">http://ww1.microchip.com/downloads/en/devicedoc/spi.pdf</xref></p>
+ <p><b>Wikipedia</b> - <xref
href="http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus"
format="html">http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus</xref></p>
+ </section>
+ </body>
+</topic>
=======================================
--- /trunk/doc/dita/tutorials/tutorial_sd_card.xml Fri Dec 4 11:04:44 2009
+++ /trunk/doc/dita/tutorials/tutorial_sd_card.xml Sun Jan 24 12:51:04 2010
@@ -20,7 +20,7 @@
<section>
<title>SD Card Introduction</title>
<p>SD Cards (Secure Digital Cards) are quite popular these days for
things like digital camera's, video camera's, mp3 players and
mobile phones. Now you will have one in your project! The main advantages
are: small size, large data storage capability, speed, cost. It has flash
storage that does not require power to hold data. The current version of
the sd card library that we will be using in this tutorial works with
"standard capacity" sd cards up 4gb in size. I hope to find time
to add "high capacity" and "extended capacity"
capability to the library.</p>
- <p>SD Card have 2 data transfer types "SD Bus" and
"SPI Bus". Most PIC's have an SPI port. The "SD
Bus" is faster, however uses more pins. We will be using SPI in our
circuit. For more info on SPI visit <xref
href="http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus"
format="html">http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus</xref>.
The SPI mode for SD Cards is 1,1. </p>
+ <p>SD Card have 2 data transfer types "SD Bus" and
"SPI Bus". Most PIC's have an SPI port. The "SD
Bus" is faster, however uses more pins. We will be using SPI in our
circuit. For more info on SPI read the tutorial in this book: <xref
href="tutorial_spi_introduction.xml">SPI Introduction</xref>. The SPI mode
for SD Cards is 1,1. </p>
<p>We are not responsible for your data or SD card. Make sure you
have nothing important on your SD card before you continue.</p>
<image href="images/sd_card_sd_card.jpg" width="200"/>
<image href="images/sd_card_pinout.jpg" width="200"/>
@@ -170,5 +170,11 @@
<p>If you want to read files stored on the card by your PC, there
wil soon be a FAT32 library and tutorial so you can easily browse, read and
write to files and folders stored on your card. </p>
<p>What are you waiting for, go build something cool!</p>
</section>
+ <section>
+ <title>Sources</title>
+ <p><b>The Jallib SD Card Library</b> - Written by Matthew
Schinkel</p>
+ <p><b>SanDisk Secure Digital Card</b> - <xref format="html"
href="http://www.cs.ucr.edu/~amitra/sdcard/ProdManualSDCardv1.9.pdf">http://www.cs.ucr.edu/~amitra/sdcard/ProdManualSDCardv1.9.pdf</xref></p>
+ <p><b>How to use MMC/SDC</b> - <xref format="html"
href="http://forums.parallax.com/forums/attach.aspx?a=32012">http://forums.parallax.com/forums/attach.aspx?a=32012</xref></p>
+ </section>
</body>
</topic>
=======================================
--- /trunk/doc/dita/tutorials/tutorial_spi_sram_23k256.xml Sat Jan 23
00:25:24 2010
+++ /trunk/doc/dita/tutorials/tutorial_spi_sram_23k256.xml Sun Jan 24
12:51:04 2010
@@ -11,12 +11,11 @@
<section>
<title>What is the 23k256 sram and why use it?</title>
<p>So, you need some data storage? Put your data on a 23k256!</p>
- <image href="images/sram_23k256.jpg" width="150"
otherprops="clickable"/>
+ <image href="images/sram_23k256.jpg" width="100"
otherprops="clickable"/>
<p>If speed is your thing, this one is for you! This is FAST.
According to Microchip's datasheet, data can be clocked in at 20mhz.
The disadvantage to this memory however is that it will not hold it's
memory when power is off since it is a type of RAM (Random Access
memory).</p>
<p>If you wish to hold memory while power is off, you will have to
go with EEPROM but it is much slower. EEPROM requires a 1ms delay between
writes. In the time that I could write 1 byte to an EEPROM (1ms), I could
write 2500 bytes to the 23k256 (if I can get my PIC to run fast enough).</p>
<p>Yet another advantage, is that it is only 8 pins (as you can see
from the image). Other RAM memories have 10 or so address lines + 8 data
lines. If you haven't guessed yet, we are sending serial data for
reads & writes. We will be using SPI (Serial Peripheral Interface
Bus).</p>
- <p>You can read more about SPI here:</p>
- <p><xref
href="http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus"
format="html">http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus</xref></p>
+ <p>I suggest you start by reading the <xref
href="tutorial_spi_introduction.xml">SPI Introduction</xref> within this
book first.</p>
<p>You can read more about the 23k256 here:</p>
<p><xref
href="http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en539039"
format="html">http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en539039</xref></p>
</section>
=======================================
--- /trunk/doc/dita/tutorials/tutorials.ditamap Sat Jan 23 00:25:24 2010
+++ /trunk/doc/dita/tutorials/tutorials.ditamap Sun Jan 24 12:51:04 2010
@@ -41,6 +41,7 @@
<topicref href="tutorial_i2c2.xml"/>
<topicref href="tutorial_i2c3.xml"/>
</topichead>
+ <topicref href="tutorial_spi_introduction.xml"/>
</chapter>
<chapter href="tutorial_externals.xml">
<topicref href="tutorial_sd_card.xml"/>
--
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en.