PIC24 Support Libraries
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 
67 uint16_t ioMasterSPI1(uint16_t u16_c) {
68 
69  checkRxErrorSPI1();
70 #if defined(_SRXMPT) && defined(_SPIBEN)
71  //enhanced SPI module, need to handle possibility of enhanced SPI mode
72  if (!SPI1CON2bits.SPIBEN) {//check enhanced buffer mode bit
73  //legacy mode
74  _SPI1IF = 0; //clear interrupt flag since we are about to write new value
75  SPI1BUF = u16_c;
76  while (!_SPI1IF) { //wait for operation to complete
77  doHeartbeat();
78  }
79  } else {
80  //enhanced buffer mode
81  SPI1BUF = u16_c;
82  while (SPI1STATbits.SRXMPT) { //this flag is zero when RX buffer has data
83  doHeartbeat();
84  }
85  }
86 #else
87  //legacy mode
88  _SPI1IF = 0; //clear interrupt flag since we are about to write new value
89  SPI1BUF = u16_c;
90  while (!_SPI1IF) { //wait for operation to complete
91  doHeartbeat();
92  }
93 #endif
94  return(SPI1BUF);
95 }
96 
97 #endif // #if (NUM_SPI_MODS >= 1)
98 
99 
100 
101 
102 
103 /*
104  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
105  * All rights reserved.
106  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
107  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
108  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
109  *
110  * Permission to use, copy, modify, and distribute this software and its
111  * documentation for any purpose, without fee, and without written agreement is
112  * hereby granted, provided that the above copyright notice, the following
113  * two paragraphs and the authors appear in all copies of this software.
114  *
115  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
116  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
117  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
118  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
119  *
120  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
121  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
122  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
123  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
124  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
125  *
126  * Please maintain this header in its entirety when copying/modifying
127  * these files.
128  *
129  *
130  */
131 
132 #include "pic24_spi.h"
133 #include "pic24_util.h"
134 
135 // Only include if this UART exists.
136 #if (NUM_SPI_MODS >= 2)
137 
138 // Documentation for this file. If the \file tag is not present,
139 // this file will not be documented.
140 // Note: place this comment below the #if NUM_I2C_MODS so Doxygen
141 // will only see it once.
142 /** \file
143  * This file contains routines which configure and
144  * use the SPI module on the PIC24 uC.
145  * \see pic24_spi.h for more details.
146  */
147 
148 
149 
150 //SPI Receive Overflow freezes the SPI module
151 void checkRxErrorSPI2() {
152  if (SPI2STATbits.SPIROV) {
153  //clear the error
154  SPI2STATbits.SPIROV = 0;
155  reportError("SPI2 Receive Overflow\n");
156  }
157 }
158 
159 /**
160  * Writes a value to the SPIx output buffer, and returns the SPIx
161  * input value. This function waits until the entire transmission
162  * is complete so it can return the new input value. Whether or not
163  * 8-bits or 16-bits is sent depends on how the SPIx module is
164  * configured.
165  * \param u16_c Value to write to SPI TXBUF
166  * \return Value read from SPI RXBUF
167  */
168 
169 uint16_t ioMasterSPI2(uint16_t u16_c) {
170 
171  checkRxErrorSPI2();
172 #if defined(_SRXMPT) && defined(_SPIBEN)
173  //enhanced SPI module, need to handle possibility of enhanced SPI mode
174  if (!SPI2CON2bits.SPIBEN) {//check enhanced buffer mode bit
175  //legacy mode
176  _SPI2IF = 0; //clear interrupt flag since we are about to write new value
177  SPI2BUF = u16_c;
178  while (!_SPI2IF) { //wait for operation to complete
179  doHeartbeat();
180  }
181  } else {
182  //enhanced buffer mode
183  SPI2BUF = u16_c;
184  while (SPI2STATbits.SRXMPT) { //this flag is zero when RX buffer has data
185  doHeartbeat();
186  }
187  }
188 #else
189  //legacy mode
190  _SPI2IF = 0; //clear interrupt flag since we are about to write new value
191  SPI2BUF = u16_c;
192  while (!_SPI2IF) { //wait for operation to complete
193  doHeartbeat();
194  }
195 #endif
196  return(SPI2BUF);
197 }
198 
199 #endif // #if (NUM_SPI_MODS >= 2)
200 
201 
202 
203 
204 
void doHeartbeat(void)
Definition: pic24_util.c:104
uint16_t ioMasterSPI1(uint16_t u16_c)
Definition: pic24_spi.c:67
void reportError(const char *sz_errorMessage)
Definition: pic24_util.c:183