<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Procyon AVRlib: spi.c Source File</title>
<link href="dox.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.2 -->
<div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div>
<h1>spi.c</h1><a href="spi_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file spi.c \brief SPI interface driver. */</span>
00002 <span class="comment">//*****************************************************************************</span>
00003 <span class="comment">//</span>
00004 <span class="comment">// File Name : 'spi.c'</span>
00005 <span class="comment">// Title : SPI interface driver</span>
00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2000-2002</span>
00007 <span class="comment">// Created : 11/22/2000</span>
00008 <span class="comment">// Revised : 06/06/2002</span>
00009 <span class="comment">// Version : 0.6</span>
00010 <span class="comment">// Target MCU : Atmel AVR series</span>
00011 <span class="comment">// Editor Tabs : 4</span>
00012 <span class="comment">//</span>
00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>
00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>
00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>
00016 <span class="comment">//</span>
00017 <span class="comment">// This code is distributed under the GNU Public License</span>
00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>
00019 <span class="comment">//</span>
00020 <span class="comment">//*****************************************************************************</span>
00021
00022 <span class="preprocessor">#include <avr/io.h></span>
00023 <span class="preprocessor">#include <avr/interrupt.h></span>
00024
00025 <span class="preprocessor">#include "<a class="code" href="spi_8h.html">spi.h</a>"</span>
00026
00027 <span class="comment">// Define the SPI_USEINT key if you want SPI bus operation to be</span>
00028 <span class="comment">// interrupt-driven. The primary reason for not using SPI in</span>
00029 <span class="comment">// interrupt-driven mode is if the SPI send/transfer commands</span>
00030 <span class="comment">// will be used from within some other interrupt service routine</span>
00031 <span class="comment">// or if interrupts might be globally turned off due to of other</span>
00032 <span class="comment">// aspects of your program</span>
00033 <span class="comment">//</span>
00034 <span class="comment">// Comment-out or uncomment this line as necessary</span>
00035 <span class="comment">//#define SPI_USEINT</span>
00036
00037 <span class="comment">// global variables</span>
00038 <span class="keyword">volatile</span> u08 spiTransferComplete;
00039
00040 <span class="comment">// SPI interrupt service handler</span>
00041 <span class="preprocessor">#ifdef SPI_USEINT</span>
00042 <span class="preprocessor"></span><a class="code" href="a2d_8c.html#a10">SIGNAL</a>(SIG_SPI)
00043 {
00044 spiTransferComplete = TRUE;
00045 }
00046 <span class="preprocessor">#endif</span>
00047 <span class="preprocessor"></span>
00048 <span class="comment">// access routines</span>
00049 <span class="keywordtype">void</span> spiInit()
00050 {
00051 <span class="preprocessor">#ifdef __AVR_ATmega128__</span>
00052 <span class="preprocessor"></span> <span class="comment">// setup SPI I/O pins</span>
00053 sbi(PORTB, 1); <span class="comment">// set SCK hi</span>
00054 sbi(DDRB, 1); <span class="comment">// set SCK as output</span>
00055 cbi(DDRB, 3); <span class="comment">// set MISO as input</span>
00056 sbi(DDRB, 2); <span class="comment">// set MOSI as output</span>
00057 sbi(DDRB, 0); <span class="comment">// SS must be output for Master mode to work</span>
00058 <span class="preprocessor">#elif __AVR_ATmega8__</span>
00059 <span class="preprocessor"></span> <span class="comment">// setup SPI I/O pins</span>
00060 sbi(PORTB, 5); <span class="comment">// set SCK hi</span>
00061 sbi(DDRB, 5); <span class="comment">// set SCK as output</span>
00062 cbi(DDRB, 4); <span class="comment">// set MISO as input</span>
00063 sbi(DDRB, 3); <span class="comment">// set MOSI as output</span>
00064 sbi(DDRB, 2); <span class="comment">// SS must be output for Master mode to work</span>
00065 <span class="preprocessor">#else</span>
00066 <span class="preprocessor"></span> <span class="comment">// setup SPI I/O pins</span>
00067 sbi(PORTB, 7); <span class="comment">// set SCK hi</span>
00068 sbi(DDRB, 7); <span class="comment">// set SCK as output</span>
00069 cbi(DDRB, 6); <span class="comment">// set MISO as input</span>
00070 sbi(DDRB, 5); <span class="comment">// set MOSI as output</span>
00071 sbi(DDRB, 4); <span class="comment">// SS must be output for Master mode to work</span>
00072 <span class="preprocessor">#endif</span>
00073 <span class="preprocessor"></span>
00074 <span class="comment">// setup SPI interface :</span>
00075 <span class="comment">// master mode</span>
00076 sbi(SPCR, MSTR);
00077 <span class="comment">// clock = f/4</span>
00078 <span class="comment">// cbi(SPCR, SPR0);</span>
00079 <span class="comment">// cbi(SPCR, SPR1);</span>
00080 <span class="comment">// clock = f/16</span>
00081 cbi(SPCR, SPR0);
00082 sbi(SPCR, SPR1);
00083 <span class="comment">// select clock phase positive-going in middle of data</span>
00084 cbi(SPCR, CPOL);
00085 <span class="comment">// Data order MSB first</span>
00086 cbi(SPCR,DORD);
00087 <span class="comment">// enable SPI</span>
00088 sbi(SPCR, SPE);
00089
00090
00091 <span class="comment">// some other possible configs</span>
00092 <span class="comment">//outp((1<<MSTR)|(1<<SPE)|(1<<SPR0), SPCR );</span>
00093 <span class="comment">//outp((1<<CPHA)|(1<<CPOL)|(1<<MSTR)|(1<<SPE)|(1<<SPR0)|(1<<SPR1), SPCR );</span>
00094 <span class="comment">//outp((1<<CPHA)|(1<<MSTR)|(1<<SPE)|(1<<SPR0), SPCR );</span>
00095
00096 <span class="comment">// clear status</span>
00097 inb(SPSR);
00098 spiTransferComplete = TRUE;
00099
00100 <span class="comment">// enable SPI interrupt</span>
00101 <span class="preprocessor"> #ifdef SPI_USEINT</span>
00102 <span class="preprocessor"></span> sbi(SPCR, SPIE);
00103 <span class="preprocessor"> #endif</span>
00104 <span class="preprocessor"></span>}
00105 <span class="comment">/*</span>
00106 <span class="comment">void spiSetBitrate(u08 spr)</span>
00107 <span class="comment">{</span>
00108 <span class="comment"> outb(SPCR, (inb(SPCR) & ((1<<SPR0)|(1<<SPR1))) | (spr&((1<<SPR0)|(1<<SPR1)))));</span>
00109 <span class="comment">}</span>
00110 <span class="comment">*/</span>
00111 <span class="keywordtype">void</span> spiSendByte(u08 data)
00112 {
00113 <span class="comment">// send a byte over SPI and ignore reply</span>
00114 <span class="preprocessor"> #ifdef SPI_USEINT</span>
00115 <span class="preprocessor"></span> <span class="keywordflow">while</span>(!spiTransferComplete);
00116 spiTransferComplete = FALSE;
00117 <span class="preprocessor"> #else</span>
00118 <span class="preprocessor"></span> <span class="keywordflow">while</span>(!(inb(SPSR) & (1<<SPIF)));
00119 <span class="preprocessor"> #endif</span>
00120 <span class="preprocessor"></span>
00121 outb(SPDR, data);
00122 }
00123
00124 u08 spiTransferByte(u08 data)
00125 {
00126 <span class="preprocessor"> #ifdef SPI_USEINT</span>
00127 <span class="preprocessor"></span> <span class="comment">// send the given data</span>
00128 spiTransferComplete = FALSE;
00129 outb(SPDR, data);
00130 <span class="comment">// wait for transfer to complete</span>
00131 <span class="keywordflow">while</span>(!spiTransferComplete);
00132 <span class="preprocessor"> #else</span>
00133 <span class="preprocessor"></span> <span class="comment">// send the given data</span>
00134 outb(SPDR, data);
00135 <span class="comment">// wait for transfer to complete</span>
00136 <span class="keywordflow">while</span>(!(inb(SPSR) & (1<<SPIF)));
00137 <span class="preprocessor"> #endif</span>
00138 <span class="preprocessor"></span> <span class="comment">// return the received data</span>
00139 <span class="keywordflow">return</span> inb(SPDR);
00140 }
00141
00142 u16 spiTransferWord(u16 data)
00143 {
00144 u16 rxData = 0;
00145
00146 <span class="comment">// send MS byte of given data</span>
00147 rxData = (spiTransferByte((data>>8) & 0x00FF))<<8;
00148 <span class="comment">// send LS byte of given data</span>
00149 rxData |= (spiTransferByte(data & 0x00FF));
00150
00151 <span class="comment">// return the received data</span>
00152 <span class="keywordflow">return</span> rxData;
00153 }
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
</body>
</html>
|