| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
||
| 3 | <title>Procyon AVRlib: spi.c Source File</title> |
||
| 4 | <link href="dox.css" rel="stylesheet" type="text/css"> |
||
| 5 | </head><body> |
||
| 6 | <!-- Generated by Doxygen 1.4.2 --> |
||
| 7 | <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> |
||
| 8 | <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> |
||
| 9 | 00002 <span class="comment">//*****************************************************************************</span> |
||
| 10 | 00003 <span class="comment">//</span> |
||
| 11 | 00004 <span class="comment">// File Name : 'spi.c'</span> |
||
| 12 | 00005 <span class="comment">// Title : SPI interface driver</span> |
||
| 13 | 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2000-2002</span> |
||
| 14 | 00007 <span class="comment">// Created : 11/22/2000</span> |
||
| 15 | 00008 <span class="comment">// Revised : 06/06/2002</span> |
||
| 16 | 00009 <span class="comment">// Version : 0.6</span> |
||
| 17 | 00010 <span class="comment">// Target MCU : Atmel AVR series</span> |
||
| 18 | 00011 <span class="comment">// Editor Tabs : 4</span> |
||
| 19 | 00012 <span class="comment">//</span> |
||
| 20 | 00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span> |
||
| 21 | 00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span> |
||
| 22 | 00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span> |
||
| 23 | 00016 <span class="comment">//</span> |
||
| 24 | 00017 <span class="comment">// This code is distributed under the GNU Public License</span> |
||
| 25 | 00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span> |
||
| 26 | 00019 <span class="comment">//</span> |
||
| 27 | 00020 <span class="comment">//*****************************************************************************</span> |
||
| 28 | 00021 |
||
| 29 | 00022 <span class="preprocessor">#include <avr/io.h></span> |
||
| 30 | 00023 <span class="preprocessor">#include <avr/interrupt.h></span> |
||
| 31 | 00024 |
||
| 32 | 00025 <span class="preprocessor">#include "<a class="code" href="spi_8h.html">spi.h</a>"</span> |
||
| 33 | 00026 |
||
| 34 | 00027 <span class="comment">// Define the SPI_USEINT key if you want SPI bus operation to be</span> |
||
| 35 | 00028 <span class="comment">// interrupt-driven. The primary reason for not using SPI in</span> |
||
| 36 | 00029 <span class="comment">// interrupt-driven mode is if the SPI send/transfer commands</span> |
||
| 37 | 00030 <span class="comment">// will be used from within some other interrupt service routine</span> |
||
| 38 | 00031 <span class="comment">// or if interrupts might be globally turned off due to of other</span> |
||
| 39 | 00032 <span class="comment">// aspects of your program</span> |
||
| 40 | 00033 <span class="comment">//</span> |
||
| 41 | 00034 <span class="comment">// Comment-out or uncomment this line as necessary</span> |
||
| 42 | 00035 <span class="comment">//#define SPI_USEINT</span> |
||
| 43 | 00036 |
||
| 44 | 00037 <span class="comment">// global variables</span> |
||
| 45 | 00038 <span class="keyword">volatile</span> u08 spiTransferComplete; |
||
| 46 | 00039 |
||
| 47 | 00040 <span class="comment">// SPI interrupt service handler</span> |
||
| 48 | 00041 <span class="preprocessor">#ifdef SPI_USEINT</span> |
||
| 49 | 00042 <span class="preprocessor"></span><a class="code" href="a2d_8c.html#a10">SIGNAL</a>(SIG_SPI) |
||
| 50 | 00043 { |
||
| 51 | 00044 spiTransferComplete = TRUE; |
||
| 52 | 00045 } |
||
| 53 | 00046 <span class="preprocessor">#endif</span> |
||
| 54 | 00047 <span class="preprocessor"></span> |
||
| 55 | 00048 <span class="comment">// access routines</span> |
||
| 56 | 00049 <span class="keywordtype">void</span> spiInit() |
||
| 57 | 00050 { |
||
| 58 | 00051 <span class="preprocessor">#ifdef __AVR_ATmega128__</span> |
||
| 59 | 00052 <span class="preprocessor"></span> <span class="comment">// setup SPI I/O pins</span> |
||
| 60 | 00053 sbi(PORTB, 1); <span class="comment">// set SCK hi</span> |
||
| 61 | 00054 sbi(DDRB, 1); <span class="comment">// set SCK as output</span> |
||
| 62 | 00055 cbi(DDRB, 3); <span class="comment">// set MISO as input</span> |
||
| 63 | 00056 sbi(DDRB, 2); <span class="comment">// set MOSI as output</span> |
||
| 64 | 00057 sbi(DDRB, 0); <span class="comment">// SS must be output for Master mode to work</span> |
||
| 65 | 00058 <span class="preprocessor">#elif __AVR_ATmega8__</span> |
||
| 66 | 00059 <span class="preprocessor"></span> <span class="comment">// setup SPI I/O pins</span> |
||
| 67 | 00060 sbi(PORTB, 5); <span class="comment">// set SCK hi</span> |
||
| 68 | 00061 sbi(DDRB, 5); <span class="comment">// set SCK as output</span> |
||
| 69 | 00062 cbi(DDRB, 4); <span class="comment">// set MISO as input</span> |
||
| 70 | 00063 sbi(DDRB, 3); <span class="comment">// set MOSI as output</span> |
||
| 71 | 00064 sbi(DDRB, 2); <span class="comment">// SS must be output for Master mode to work</span> |
||
| 72 | 00065 <span class="preprocessor">#else</span> |
||
| 73 | 00066 <span class="preprocessor"></span> <span class="comment">// setup SPI I/O pins</span> |
||
| 74 | 00067 sbi(PORTB, 7); <span class="comment">// set SCK hi</span> |
||
| 75 | 00068 sbi(DDRB, 7); <span class="comment">// set SCK as output</span> |
||
| 76 | 00069 cbi(DDRB, 6); <span class="comment">// set MISO as input</span> |
||
| 77 | 00070 sbi(DDRB, 5); <span class="comment">// set MOSI as output</span> |
||
| 78 | 00071 sbi(DDRB, 4); <span class="comment">// SS must be output for Master mode to work</span> |
||
| 79 | 00072 <span class="preprocessor">#endif</span> |
||
| 80 | 00073 <span class="preprocessor"></span> |
||
| 81 | 00074 <span class="comment">// setup SPI interface :</span> |
||
| 82 | 00075 <span class="comment">// master mode</span> |
||
| 83 | 00076 sbi(SPCR, MSTR); |
||
| 84 | 00077 <span class="comment">// clock = f/4</span> |
||
| 85 | 00078 <span class="comment">// cbi(SPCR, SPR0);</span> |
||
| 86 | 00079 <span class="comment">// cbi(SPCR, SPR1);</span> |
||
| 87 | 00080 <span class="comment">// clock = f/16</span> |
||
| 88 | 00081 cbi(SPCR, SPR0); |
||
| 89 | 00082 sbi(SPCR, SPR1); |
||
| 90 | 00083 <span class="comment">// select clock phase positive-going in middle of data</span> |
||
| 91 | 00084 cbi(SPCR, CPOL); |
||
| 92 | 00085 <span class="comment">// Data order MSB first</span> |
||
| 93 | 00086 cbi(SPCR,DORD); |
||
| 94 | 00087 <span class="comment">// enable SPI</span> |
||
| 95 | 00088 sbi(SPCR, SPE); |
||
| 96 | 00089 |
||
| 97 | 00090 |
||
| 98 | 00091 <span class="comment">// some other possible configs</span> |
||
| 99 | 00092 <span class="comment">//outp((1<<MSTR)|(1<<SPE)|(1<<SPR0), SPCR );</span> |
||
| 100 | 00093 <span class="comment">//outp((1<<CPHA)|(1<<CPOL)|(1<<MSTR)|(1<<SPE)|(1<<SPR0)|(1<<SPR1), SPCR );</span> |
||
| 101 | 00094 <span class="comment">//outp((1<<CPHA)|(1<<MSTR)|(1<<SPE)|(1<<SPR0), SPCR );</span> |
||
| 102 | 00095 |
||
| 103 | 00096 <span class="comment">// clear status</span> |
||
| 104 | 00097 inb(SPSR); |
||
| 105 | 00098 spiTransferComplete = TRUE; |
||
| 106 | 00099 |
||
| 107 | 00100 <span class="comment">// enable SPI interrupt</span> |
||
| 108 | 00101 <span class="preprocessor"> #ifdef SPI_USEINT</span> |
||
| 109 | 00102 <span class="preprocessor"></span> sbi(SPCR, SPIE); |
||
| 110 | 00103 <span class="preprocessor"> #endif</span> |
||
| 111 | 00104 <span class="preprocessor"></span>} |
||
| 112 | 00105 <span class="comment">/*</span> |
||
| 113 | 00106 <span class="comment">void spiSetBitrate(u08 spr)</span> |
||
| 114 | 00107 <span class="comment">{</span> |
||
| 115 | 00108 <span class="comment"> outb(SPCR, (inb(SPCR) & ((1<<SPR0)|(1<<SPR1))) | (spr&((1<<SPR0)|(1<<SPR1)))));</span> |
||
| 116 | 00109 <span class="comment">}</span> |
||
| 117 | 00110 <span class="comment">*/</span> |
||
| 118 | 00111 <span class="keywordtype">void</span> spiSendByte(u08 data) |
||
| 119 | 00112 { |
||
| 120 | 00113 <span class="comment">// send a byte over SPI and ignore reply</span> |
||
| 121 | 00114 <span class="preprocessor"> #ifdef SPI_USEINT</span> |
||
| 122 | 00115 <span class="preprocessor"></span> <span class="keywordflow">while</span>(!spiTransferComplete); |
||
| 123 | 00116 spiTransferComplete = FALSE; |
||
| 124 | 00117 <span class="preprocessor"> #else</span> |
||
| 125 | 00118 <span class="preprocessor"></span> <span class="keywordflow">while</span>(!(inb(SPSR) & (1<<SPIF))); |
||
| 126 | 00119 <span class="preprocessor"> #endif</span> |
||
| 127 | 00120 <span class="preprocessor"></span> |
||
| 128 | 00121 outb(SPDR, data); |
||
| 129 | 00122 } |
||
| 130 | 00123 |
||
| 131 | 00124 u08 spiTransferByte(u08 data) |
||
| 132 | 00125 { |
||
| 133 | 00126 <span class="preprocessor"> #ifdef SPI_USEINT</span> |
||
| 134 | 00127 <span class="preprocessor"></span> <span class="comment">// send the given data</span> |
||
| 135 | 00128 spiTransferComplete = FALSE; |
||
| 136 | 00129 outb(SPDR, data); |
||
| 137 | 00130 <span class="comment">// wait for transfer to complete</span> |
||
| 138 | 00131 <span class="keywordflow">while</span>(!spiTransferComplete); |
||
| 139 | 00132 <span class="preprocessor"> #else</span> |
||
| 140 | 00133 <span class="preprocessor"></span> <span class="comment">// send the given data</span> |
||
| 141 | 00134 outb(SPDR, data); |
||
| 142 | 00135 <span class="comment">// wait for transfer to complete</span> |
||
| 143 | 00136 <span class="keywordflow">while</span>(!(inb(SPSR) & (1<<SPIF))); |
||
| 144 | 00137 <span class="preprocessor"> #endif</span> |
||
| 145 | 00138 <span class="preprocessor"></span> <span class="comment">// return the received data</span> |
||
| 146 | 00139 <span class="keywordflow">return</span> inb(SPDR); |
||
| 147 | 00140 } |
||
| 148 | 00141 |
||
| 149 | 00142 u16 spiTransferWord(u16 data) |
||
| 150 | 00143 { |
||
| 151 | 00144 u16 rxData = 0; |
||
| 152 | 00145 |
||
| 153 | 00146 <span class="comment">// send MS byte of given data</span> |
||
| 154 | 00147 rxData = (spiTransferByte((data>>8) & 0x00FF))<<8; |
||
| 155 | 00148 <span class="comment">// send LS byte of given data</span> |
||
| 156 | 00149 rxData |= (spiTransferByte(data & 0x00FF)); |
||
| 157 | 00150 |
||
| 158 | 00151 <span class="comment">// return the received data</span> |
||
| 159 | 00152 <span class="keywordflow">return</span> rxData; |
||
| 160 | 00153 } |
||
| 161 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by |
||
| 162 | <a href="http://www.doxygen.org/index.html"> |
||
| 163 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
| 164 | </body> |
||
| 165 | </html> |
Powered by WebSVN v2.8.3