00001 /* 00002 * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")" 00003 * All rights reserved. 00004 * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University) 00005 * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University) 00006 * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University) 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation for any purpose, without fee, and without written agreement is 00010 * hereby granted, provided that the above copyright notice, the following 00011 * two paragraphs and the authors appear in all copies of this software. 00012 * 00013 * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR 00014 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00015 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS" 00016 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00020 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 00021 * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO 00022 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." 00023 * 00024 * Please maintain this header in its entirety when copying/modifying 00025 * these files. 00026 * 00027 * 00028 */ 00029 00030 00036 /*** I N C L U D E S *************************************************/ 00037 #include "esos_pic24_spi.h" 00038 00039 /*** G L O B A L S *************************************************/ 00040 struct stTask __stChildTaskSPI; 00041 uint16 __esos_spi_u16s[2]; 00042 00043 /*** T H E C O D E *************************************************/ 00044 00045 /********************************************************* 00046 * Public functions intended to be called by other files * 00047 *********************************************************/ 00048 00049 // Documentation for this file. If the \file tag is not present, 00050 // this file will not be documented. 00051 // Note: place this comment below the #if NUM_I2C_MODS so Doxygen 00052 // will only see it once. 00057 /* 00058 Transaction: Writes \em u16_cnt words stored in 00059 buffer \em *pu16_out to SPI device, while reading \em u16_cnt words from 00060 SPI device placing results into buffer \em *pu16_in 00061 \note Assumes that SPI peripheral has been properly configured. 00062 \note The SPI peripheral setup determines whether 8-bit or 16-bit data 00063 is written. 00064 \param pu16_out Pointer to buffer containing data to send. If \em pu16_out is \em NULLPTR this function will send zeroes to the SPI device and only "read" 00065 \param pu16_in Pointer to buffer to catch incoming data. If \em pu16_in is \em NULLPTR this function will only "write" the SPI device 00066 \param u16_cnt Number of words to send 00067 */ 00068 ESOS_CHILD_TASK( __esos_pic24_xferNSPI1, uint16* pu16_out, uint16* pu16_in, uint16 u16_cnt) { 00069 static uint16* pu16_tempPtrIn; 00070 static uint16* pu16_tempPtrOut; 00071 static uint16 u16_tempCnt, u16_i; 00072 static uint8 u8_isReading, u8_isWriting; 00073 uint16 u16_scratch; 00074 00075 ESOS_TASK_BEGIN(); 00076 pu16_tempPtrOut=pu16_out; 00077 pu16_tempPtrIn=pu16_in; 00078 u16_tempCnt=u16_cnt; 00079 if (pu16_tempPtrOut == NULLPTR) 00080 u8_isWriting = FALSE; 00081 else 00082 u8_isWriting = TRUE; 00083 00084 if (pu16_tempPtrIn == NULLPTR) 00085 u8_isReading = FALSE; 00086 else 00087 u8_isReading = TRUE; 00088 00089 // clear the overflow flag, just in case it is set 00090 if (SPI1STATbits.SPIROV) SPI1STATbits.SPIROV = 0; 00091 //clear SPI interrupt flag since we are about to write new value to SPI 00092 _SPI1IF = 0; 00093 /* read SPI1BUF to clear SPI_RX_BUFFER_FULL bit just in case previous 00094 SPI use did not read the SPI1BUF that last time! 00095 */ 00096 u16_scratch = SPI1BUF; 00097 for (u16_i=0; u16_i < u16_tempCnt; u16_i++) { 00098 if (u8_isWriting) { 00099 SPI1BUF = *pu16_tempPtrOut; 00100 pu16_tempPtrOut++; 00101 } else { 00102 SPI1BUF = 0; 00103 } // end isWriting 00104 00105 /* Seen some strange behavior checking _SPI1IF like the 00106 * hardware support library. The following method is valid 00107 * and appears to work in all cases. 00108 */ 00109 // wait for TX word to be copied to SPI1SR 00110 ESOS_TASK_WAIT_WHILE( SPI1STAT & SPI_TX_BUFFER_FULL ); 00111 // wait for RX word to be copied from SPI1SR 00112 ESOS_TASK_WAIT_UNTIL( SPI1STAT & SPI_RX_BUFFER_FULL ); 00113 // read the word from SPI (clears SPI_RX_BUFFER_FULL bit) 00114 u16_scratch = SPI1BUF; 00115 if (u8_isReading) { 00116 *pu16_tempPtrIn = u16_scratch; 00117 pu16_tempPtrIn++; 00118 } // end isReading 00119 } // end for() 00120 ESOS_TASK_END(); 00121 } // end __esos_pic24_xferNSPI1 00122