PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include "pic24_spi.h"
31 #include "pic24_util.h"
32 
33 // Only include if this UART exists.
34 #if (NUM_SPI_MODS >= 1)
35 
36 // Documentation for this file. If the \file tag is not present,
37 // this file will not be documented.
38 // Note: place this comment below the #if NUM_I2C_MODS so Doxygen
39 // will only see it once.
40 /** \file
41  * This file contains routines which configure and
42  * use the SPI module on the PIC24 uC.
43  * \see pic24_spi.h for more details.
44  */
45 
46 
47 
48 //SPI Receive Overflow freezes the SPI module
49 void checkRxErrorSPI1() {
50  if (SPI1STATbits.SPIROV) {
51  //clear the error
52  SPI1STATbits.SPIROV = 0;
53  reportError("SPI1 Receive Overflow\n");
54  }
55 }
56 
57 /**
58  * Writes a value to the SPIx output buffer, and returns the SPIx
59  * input value. This function waits until the entire transmission
60  * is complete so it can return the new input value. Whether or not
61  * 8-bits or 16-bits is sent depends on how the SPIx module is
62  * configured.
63  * \param u16_c Value to write to SPI TXBUF
64  * \return Value read from SPI RXBUF
65  */
66 
68  checkRxErrorSPI1();
69  _SPI1IF = 0; //clear interrupt flag since we are about to write new value
70  SPI1BUF = u16_c;
71  while (!_SPI1IF) { //wait for operation to complete
72  doHeartbeat();
73  };
74  return(SPI1BUF);
75 }
76 
77 #endif // #if (NUM_SPI_MODS >= 1)
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 /*
88  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
89  * All rights reserved.
90  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
91  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
92  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
93  *
94  * Permission to use, copy, modify, and distribute this software and its
95  * documentation for any purpose, without fee, and without written agreement is
96  * hereby granted, provided that the above copyright notice, the following
97  * two paragraphs and the authors appear in all copies of this software.
98  *
99  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
100  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
101  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
102  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103  *
104  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
105  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
106  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
107  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
108  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
109  *
110  * Please maintain this header in its entirety when copying/modifying
111  * these files.
112  *
113  *
114  */
115 
116 #include "pic24_spi.h"
117 #include "pic24_util.h"
118 
119 // Only include if this UART exists.
120 #if (NUM_SPI_MODS >= 2)
121 
122 // Documentation for this file. If the \file tag is not present,
123 // this file will not be documented.
124 // Note: place this comment below the #if NUM_I2C_MODS so Doxygen
125 // will only see it once.
126 /** \file
127  * This file contains routines which configure and
128  * use the SPI module on the PIC24 uC.
129  * \see pic24_spi.h for more details.
130  */
131 
132 
133 
134 //SPI Receive Overflow freezes the SPI module
135 void checkRxErrorSPI2() {
136  if (SPI2STATbits.SPIROV) {
137  //clear the error
138  SPI2STATbits.SPIROV = 0;
139  reportError("SPI2 Receive Overflow\n");
140  }
141 }
142 
143 /**
144  * Writes a value to the SPIx output buffer, and returns the SPIx
145  * input value. This function waits until the entire transmission
146  * is complete so it can return the new input value. Whether or not
147  * 8-bits or 16-bits is sent depends on how the SPIx module is
148  * configured.
149  * \param u16_c Value to write to SPI TXBUF
150  * \return Value read from SPI RXBUF
151  */
152 
153 uint16_t ioMasterSPI2(uint16_t u16_c) {
154  checkRxErrorSPI2();
155  _SPI2IF = 0; //clear interrupt flag since we are about to write new value
156  SPI2BUF = u16_c;
157  while (!_SPI2IF) { //wait for operation to complete
158  doHeartbeat();
159  };
160  return(SPI2BUF);
161 }
162 
163 #endif // #if (NUM_SPI_MODS >= 2)
164 
165 
166 
167 
168 
169 
170 
171 
172