PIC24 Support Libraries
esos_pic24_spi.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 
31 /**
32  * \addtogroup ESOS_SPI_Service
33  * @{
34  */
35 
36 /*** I N C L U D E S *************************************************/
37 #include "esos_pic24_spi.h"
38 
39 /*** G L O B A L S *************************************************/
40 struct stTask __stChildTaskSPI;
41 uint16_t __esos_spi_u16s[2];
42 
43 /*** T H E C O D E *************************************************/
44 
45 /*********************************************************
46  * Public functions intended to be called by other files *
47  *********************************************************/
48 
49 // Documentation for this file. If the \file tag is not present,
50 // this file will not be documented.
51 // Note: place this comment below the #if NUM_I2C_MODS so Doxygen
52 // will only see it once.
53 /** \file
54  * SPI support functions. \see pic24_spi.h for details.
55  */
56 
57 /*
58 Transaction: Writes \em u16_cnt words stored in
59 buffer \em *pu16_out to SPI device, while reading \em u16_cnt words from
60 SPI device placing results into buffer \em *pu16_in
61 \note Assumes that SPI peripheral has been properly configured.
62 \note The SPI peripheral setup determines whether 8-bit or 16-bit data
63 is written.
64 \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"
65 \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
66 \param u16_cnt Number of words to send
67  */
68 ESOS_CHILD_TASK( __esos_pic24_xferNSPI1, uint16_t* pu16_out, uint16_t* pu16_in, uint16_t u16_cnt) {
69  static uint16_t* pu16_tempPtrIn;
70  static uint16_t* pu16_tempPtrOut;
71  static uint16_t u16_tempCnt, u16_i;
72  static uint8_t u8_isReading, u8_isWriting;
73  uint16_t u16_scratch;
74 
76  pu16_tempPtrOut=pu16_out;
77  pu16_tempPtrIn=pu16_in;
78  u16_tempCnt=u16_cnt;
79  if (pu16_tempPtrOut == NULLPTR)
80  u8_isWriting = FALSE;
81  else
82  u8_isWriting = TRUE;
83 
84  if (pu16_tempPtrIn == NULLPTR)
85  u8_isReading = FALSE;
86  else
87  u8_isReading = TRUE;
88 
89  // clear the overflow flag, just in case it is set
90  if (SPI1STATbits.SPIROV) SPI1STATbits.SPIROV = 0;
91  //clear SPI interrupt flag since we are about to write new value to SPI
92  _SPI1IF = 0;
93  /* read SPI1BUF to clear SPI_RX_BUFFER_FULL bit just in case previous
94  SPI use did not read the SPI1BUF that last time!
95  */
96  u16_scratch = SPI1BUF;
97  for (u16_i=0; u16_i < u16_tempCnt; u16_i++) {
98  if (u8_isWriting) {
99  SPI1BUF = *pu16_tempPtrOut;
100  pu16_tempPtrOut++;
101  } else {
102  SPI1BUF = 0;
103  } // end isWriting
104 
105  /* Seen some strange behavior checking _SPI1IF like the
106  * hardware support library. The following method is valid
107  * and appears to work in all cases.
108  */
109  // wait for TX word to be copied to SPI1SR
110  ESOS_TASK_WAIT_WHILE( SPI1STAT & SPI_TX_BUFFER_FULL );
111  // wait for RX word to be copied from SPI1SR
112  ESOS_TASK_WAIT_UNTIL( SPI1STAT & SPI_RX_BUFFER_FULL );
113  // read the word from SPI (clears SPI_RX_BUFFER_FULL bit)
114  u16_scratch = SPI1BUF;
115  if (u8_isReading) {
116  *pu16_tempPtrIn = u16_scratch;
117  pu16_tempPtrIn++;
118  } // end isReading
119  } // end for()
120  ESOS_TASK_END();
121 } // end __esos_pic24_xferNSPI1
122 
123 /**
124  * @}
125  */
#define ESOS_CHILD_TASK(taskname,...)
Definition: esos_task.h:247
#define ESOS_TASK_END()
Definition: esos_task.h:272
#define ESOS_TASK_WAIT_WHILE(cond)
Definition: esos_task.h:364
#define ESOS_TASK_WAIT_UNTIL(condition)
Definition: esos_task.h:336
#define ESOS_TASK_BEGIN()
Definition: esos_task.h:260
#define NULLPTR
Definition: all_generic.h:424
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
Definition: dataXferImpl.h:194