00001 /*! \file spi.c \brief SPI interface driver. */ 00002 //***************************************************************************** 00003 // 00004 // File Name : 'spi.c' 00005 // Title : SPI interface driver 00006 // Author : Pascal Stang - Copyright (C) 2000-2002 00007 // Created : 11/22/2000 00008 // Revised : 06/06/2002 00009 // Version : 0.6 00010 // Target MCU : Atmel AVR series 00011 // Editor Tabs : 4 00012 // 00013 // NOTE: This code is currently below version 1.0, and therefore is considered 00014 // to be lacking in some functionality or documentation, or may not be fully 00015 // tested. Nonetheless, you can expect most functions to work. 00016 // 00017 // This code is distributed under the GNU Public License 00018 // which can be found at http://www.gnu.org/licenses/gpl.txt 00019 // 00020 //***************************************************************************** 00021 00022 #include <avr/io.h> 00023 #include <avr/signal.h> 00024 #include <avr/interrupt.h> 00025 00026 #include "spi.h" 00027 00028 // Define the SPI_USEINT key if you want SPI bus operation to be 00029 // interrupt-driven. The primary reason for not using SPI in 00030 // interrupt-driven mode is if the SPI send/transfer commands 00031 // will be used from within some other interrupt service routine 00032 // or if interrupts might be globally turned off due to of other 00033 // aspects of your program 00034 // 00035 // Comment-out or uncomment this line as necessary 00036 #define SPI_USEINT 00037 00038 // global variables 00039 volatile u08 spiTransferComplete; 00040 00041 // SPI interrupt service handler 00042 #ifdef SPI_USEINT 00043 SIGNAL(SIG_SPI) 00044 { 00045 spiTransferComplete = TRUE; 00046 } 00047 #endif 00048 00049 // access routines 00050 void spiInit() 00051 { 00052 // setup SPI I/O pins 00053 sbi(PORTB, 7); // set SCK hi 00054 sbi(DDRB, 7); // set SCK as output 00055 cbi(DDRB, 6); // set MISO as input 00056 sbi(DDRB, 5); // set MOSI as output 00057 sbi(DDRB, 4); // SS must be output for Master mode to work 00058 00059 // setup SPI interface : 00060 // clock = f/16 00061 // select clock phase positive-going in middle of data 00062 // master mode 00063 // enable SPI 00064 outp((1<<MSTR)|(1<<SPE)|(1<<SPR0), SPCR ); 00065 00066 // some other possible configs 00067 //outp((1<<CPHA)|(1<<CPOL)|(1<<MSTR)|(1<<SPE)|(1<<SPR0)|(1<<SPR1), SPCR ); 00068 //outp((1<<CPHA)|(1<<MSTR)|(1<<SPE)|(1<<SPR0), SPCR ); 00069 00070 // clear status 00071 inp(SPSR); 00072 spiTransferComplete = TRUE; 00073 00074 // enable SPI interrupt 00075 #ifdef SPI_USEINT 00076 sbi(SPCR, SPIE); 00077 #endif 00078 } 00079 00080 void spiSendByte(u08 data) 00081 { 00082 // send a byte over SPI and ignore reply 00083 if(spiTransferComplete) 00084 { 00085 spiTransferComplete = FALSE; 00086 outp(data, SPDR); 00087 } 00088 } 00089 00090 u08 spiTransferByte(u08 data) 00091 { 00092 // send the given data 00093 if(spiTransferComplete) 00094 { 00095 spiTransferComplete = FALSE; 00096 outp(data, SPDR); 00097 } 00098 // wait for transfer to complete 00099 #ifdef SPI_USEINT 00100 while(!spiTransferComplete); 00101 #else 00102 while(!(inp(SPSR) & (1<<SPIF))); 00103 // *** reading of the SPSR and SPDR are crucial 00104 // *** to the clearing of the SPIF flag 00105 // *** in non-interrupt mode 00106 //inp(SPDR); 00107 // set flag 00108 spiTransferComplete = TRUE; 00109 #endif 00110 // return the received data 00111 return inp(SPDR); 00112 } 00113 00114 u16 spiTransferWord(u16 data) 00115 { 00116 u16 rxData = 0; 00117 00118 // send MS byte of given data 00119 rxData = (spiTransferByte((data>>8) & 0x00FF))<<8; 00120 // send LS byte of given data 00121 rxData |= (spiTransferByte(data & 0x00FF)); 00122 00123 // return the received data 00124 return rxData; 00125 }
1.3-rc2