00001 /****************************************************************************** 00002 ** Copyright (C) 2010 00003 ** Silvia Izquierdo Rosas <silvia.izquierdo.rosas@gmail.com> 00004 ** Christian Blanco Charro <khristian996@gmail.com> 00005 ** 00006 ** This library is free software; you can redistribute it and/or 00007 ** modify it under the terms of the GNU Lesser General Public 00008 ** License as published by the Free Software Foundation; either 00009 ** version 2.1 of the License, or (at your option) any later version. 00010 00011 ** This library is distributed in the hope that it will be useful, 00012 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 ** Lesser General Public License for more details. 00015 00016 ** You should have received a copy of the GNU Lesser General Public 00017 ** License along with this library; if not, write to the Free Software 00018 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 ******************************************************************************/ 00020 00031 //----------------------------------------------------------------------------- 00032 // Define 00033 //----------------------------------------------------------------------------- 00034 #ifndef SPI_H 00035 #define SPI_H 00036 00037 //----------------------------------------------------------------------------- 00038 // typedefs 00039 // ---------------------------------------------------------------------------- 00040 00042 typedef enum { 00043 SPISLAVE=0, 00044 SPIMASTER=1 00045 } SPINMC; 00046 00048 typedef enum{ 00049 SPILBKDIS=0, 00050 SPILBKENA=1 00051 } SPILBK; 00052 00053 // ---------------------------------------------------------------------------- 00054 //Implemented Functions 00055 // ---------------------------------------------------------------------------- 00056 00057 //----------------------------------------------------------------------------- 00069 //----------------------------------------------------------------------------- 00070 void ConfigureSPIA(void(*interruptRX)(void), void(*interruptTX)(void), 00071 SPINMC networkModeControl, SPILBK spiLoopBack, Uint32 speed, 00072 Uint16 SPIcharLength, Uint16 txFIFO, Uint16 rxFIFO); 00073 void InitSpiaGpio(void); 00074 00075 static inline void StartTransmission(void); 00076 static inline void EndTransmission(void); 00077 static inline void SPIATxPIEAck(void); 00078 static inline void SPIARxPIEAck(void); 00079 static inline void SendDataCharSpiA(volatile char *a); 00080 static inline void SendDataCharSpiANow(volatile char *a); 00081 static inline void SendDataIntSpiA(volatile Uint16 *a); 00082 static inline void SendDataIntSpiANow(volatile Uint16 *a); 00083 static inline void SendDataIQSpiA(volatile Uint32 *a); 00084 static inline void SendDataIQSpiANow(volatile Uint32 *a); 00085 00086 // ---------------------------------------------------------------------------- 00087 00088 // ---------------------------------------------------------------------------- 00089 // static inline functions 00090 // ---------------------------------------------------------------------------- 00091 00092 00093 static inline void StartTransmission(void) 00094 { 00095 SpiaRegs.SPICCR.bit.SPISWRESET=1; // Enable SPI 00096 SpiaRegs.SPIFFTX.bit.TXFIFO=1; 00097 SpiaRegs.SPIFFRX.bit.RXFIFORESET=1; 00098 } 00099 00100 // ---------------------------------------------------------------------------- 00101 00102 00103 static inline void EndTransmission(void) 00104 { 00105 SpiaRegs.SPICCR.bit.SPISWRESET=0; // Disable SPI 00106 SpiaRegs.SPIFFTX.bit.TXFIFO=0; 00107 SpiaRegs.SPIFFRX.bit.RXFIFORESET=0; 00108 } 00109 00110 // ---------------------------------------------------------------------------- 00111 00112 static inline void SPIATxPIEAck(void) 00113 { 00114 SpiaRegs.SPIFFTX.bit.TXFFINTCLR=1; // Clear Interrupt flag 00115 PieCtrlRegs.PIEACK.all|=0x20; // Issue PIE ACK 00116 } 00117 00118 // ---------------------------------------------------------------------------- 00119 00120 static inline void SPIARxPIEAck(void) 00121 { 00122 SpiaRegs.SPIFFRX.bit.RXFFOVFCLR=1; // Clear Overflow flag 00123 SpiaRegs.SPIFFRX.bit.RXFFINTCLR=1; // Clear Interrupt flag 00124 PieCtrlRegs.PIEACK.all|=0x20; // Issue PIE ack 00125 } 00126 00127 // ---------------------------------------------------------------------------- 00128 00129 static inline void SendDataCharSpiA(volatile char *a) 00130 { 00131 //Receives a pointer to the number 00132 while (SpiaRegs.SPIFFTX.bit.TXFFST != 0) {} // si TXFFST=0 -> fifo is empty, ready to transmit TODO: Pablo Interrupts?? 00133 SpiaRegs.SPITXBUF = *a; 00134 } 00135 00136 // ---------------------------------------------------------------------------- 00137 00138 static inline void SendDataIntSpiA(volatile Uint16 *a) 00139 { 00140 //Receives a pointer to the number 00141 int i; 00142 for(i=0;i<2;i++) { 00143 while (SpiaRegs.SPIFFTX.bit.TXFFST != 0) {} // si TXFFST=0 -> fifo is empty, ready to transmit TODO: Pablo Interrupts?? 00144 SpiaRegs.SPITXBUF = (*a >> 8*i & 0x00FF); 00145 } 00146 } 00147 00148 // ---------------------------------------------------------------------------- 00149 00150 static inline void SendDataIQSpiA(volatile Uint32 *a) 00151 { 00152 //Receives a pointer to the number 00153 int i; 00154 for(i=0;i<4;i++) { 00155 while (SpiaRegs.SPIFFTX.bit.TXFFST != 0) {} // si TXFFST=0 -> fifo is empty, ready to transmit TODO: Pablo Interrupts?? 00156 SpiaRegs.SPITXBUF = (*a >> 8*i & 0x00FF); 00157 } 00158 } 00159 00160 // ---------------------------------------------------------------------------- 00161 00162 static inline void SendDataCharSpiANow(volatile char *a) 00163 { 00164 //Receives a pointer to the number 00165 SpiaRegs.SPITXBUF = *a; 00166 } 00167 00168 // ---------------------------------------------------------------------------- 00169 00170 static inline void SendDataIntSpiANow(volatile Uint16 *a) 00171 { 00172 //Receives a pointer to the number 00173 int i; 00174 for(i=0;i<2;i++) { 00175 SpiaRegs.SPITXBUF = (*a >> 8*i & 0x00FF); 00176 } 00177 } 00178 00179 // ---------------------------------------------------------------------------- 00180 00181 static inline void SendDataIQSpiANow(volatile Uint32 *a) 00182 { 00183 //Receives a pointer to the number 00184 int i; 00185 for(i=0;i<4;i++) { 00186 SpiaRegs.SPITXBUF = (*a >> 8*i & 0x00FF); 00187 } 00188 } 00189 00190 // ---------------------------------------------------------------------------- 00191 00192 #endif 00193 //----------------------------------------------------------------------------- 00194 // End Of File 00195 //-----------------------------------------------------------------------------