LTlib LaurTec Library  4.0.3
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
module_SPI.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.1
5 Created on Date : 09/03/2016
6 Last update : 28/11/2015
7 
8 CopyRight 2006-2015 all rights are reserved
9 
10 ********************************************************
11 SOFTWARE LICENSE AGREEMENT
12 ********************************************************
13 
14 The usage of the supplied software imply the acceptance of the following license.
15 
16 The software supplied herewith by Mauro Laurenti (the Author) is intended for
17 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
18 The software is owned by the Author, and is protected under applicable
19 copyright laws. All rights are reserved.
20 Any use in violation of the foregoing restrictions may subject the
21 user to criminal sanctions under applicable laws, as well as to civil liability
22 for the breach of the terms and conditions of this license.
23 Commercial use is forbidden without a written acknowledgement with the Author.
24 Personal or educational use is allowed if the application containing the
25 following software doesn't aim to commercial use or monetary earning of any kind.
26 
27 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
28 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
29 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
30 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
31 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
32 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
33 
34 ********************************************************
35 PURPOSES
36 ********************************************************
37 
38 This library contains all the functions that are handy for controlling
39 the SPI module
40 *******************************************************************************/
41 
42 #ifdef __XC8
43  #include <xc.h>
44 #endif
45 
46 #include "module_SPI.h"
47 
48 
49 //************************************************************
50 // SPI_open Implementation
51 //************************************************************
52 void SPI1_open (unsigned char device_type){
53  unsigned char data;
54 
55  //IO pin initialization
56  if (device_type == SPI_MASTER_DEVICE){
57  SPI1_SCK_LINE_TRIS = PIN_AS_OUTPUT;
58  SPI1_SDI_LINE_TRIS = PIN_AS_INPUT;
59  SPI1_SDO_LINE_TRIS = PIN_AS_OUTPUT;
60  SPI1_SS_LINE_TRIS = PIN_AS_OUTPUT;
61  }
62 
63  if (device_type == SPI_SLAVE_DEVICE){
64  SPI1_SCK_LINE_TRIS = PIN_AS_INPUT;
65  SPI1_SDI_LINE_TRIS = PIN_AS_INPUT;
66  SPI1_SDO_LINE_TRIS = PIN_AS_OUTPUT;
67  SPI1_SS_LINE_TRIS = PIN_AS_INPUT;
68 
69  //SS initialization by Slave mode
70  SSPCON1 &= 0xF0;
71  SSPCON1 |= SPI1_SS_PIN;
72 
73  }
74 
75  //Clear the register by reading it out
76  data = SSPBUF;
77 
78  //Clear history
79  PIR1bits.SSPIF = 0;
80  SSPCON1bits.WCOL = 0;
81  SSPCON1bits.SSPOV = 0;
82 
83  //Data sampling in the midle of the bit
84  SSPSTATbits.SMP = 0x00;
85 
86  //Enable SPI module
87  SSPCON1bits.SSPEN = 0x01;
88 }
89 
90 #ifdef SPI_MODULE_TYPE_2
91  void SPI2_open (unsigned char device_type){
92  unsigned char data;
93 
94  //IO pin initialization
95  if (device_type == SPI_MASTER_DEVICE){
96  SPI2_SCK_LINE_TRIS = PIN_AS_OUTPUT;
97  SPI2_SDI_LINE_TRIS = PIN_AS_INPUT;
98  SPI2_SDO_LINE_TRIS = PIN_AS_OUTPUT;
99  SPI2_SS_LINE_TRIS = PIN_AS_OUTPUT;
100  }
101 
102  if (device_type == SPI_SLAVE_DEVICE){
103  SPI2_SCK_LINE_TRIS = PIN_AS_INPUT;
104  SPI2_SDI_LINE_TRIS = PIN_AS_INPUT;
105  SPI2_SDO_LINE_TRIS = PIN_AS_OUTPUT;
106  SPI2_SS_LINE_TRIS = PIN_AS_INPUT;
107 
108  //SS initialization by Slave mode
109  SSP2CON1 &= 0xF0;
110  SSP2CON1 |= SPI2_SS_PIN;
111 
112  }
113 
114  //Clear the register by reading it out
115  data = SSP2BUF;
116 
117  //Clear history
118  PIR3bits.SSP2IF = 0;
119  SSP2CON1bits.WCOL = 0;
120  SSP2CON1bits.SSPOV = 0;
121 
122  //Data sampling in the midle of the bit
123  SSP2STATbits.SMP = 0x00;
124 
125  //Enable SPI module
126  SSP2CON1bits.SSPEN = 0x01;
127  }
128 #endif
129 
130 
131 //************************************************************
132 // SPI1_close Implementation
133 //************************************************************
134 void SPI1_close (void){
135 
136  //Enable SPI module
137  SSPCON1bits.SSPEN = 0x00;
138 }
139 
140 #ifdef SPI_MODULE_TYPE_2
141  void SPI2_close (void){
142 
143  //Enable SPI module
144  SSP2CON1bits.SSPEN = 0x00;
145  }
146 #endif
147 
148 //************************************************************
149 // SPI1_baudrate Implementation
150 //************************************************************
151 void SPI1_baudrate (unsigned char baudrate){
152 
153  SSPCON1 &= 0b11111100;
154  SSPCON1 = baudrate;
155 }
156 
157 #ifdef SPI_MODULE_TYPE_2
158  void SPI2_baudrate (unsigned char baudrate){
159 
160  SSP2CON1 &= 0b11111100;
161  SSP2CON1 = baudrate;
162  }
163 #endif
164 
165 //************************************************************
166 // SPI1_mode Implementation
167 //************************************************************
168 void SPI1_mode (unsigned char communication_mode){
169 
170  SSPSTATbits.CKE = 0x00;
171 
172 
173  switch (communication_mode){
174  case SPI_MODE_0: // Data is sent on the rising edge
175  SSPSTATbits.CKE = 0x01;
176  SSPCON1bits.CKP = 0x00;
177  break;
178 
179  case SPI_MODE_1:
180  SSPSTATbits.CKE = 0x00;
181  SSPCON1bits.CKP = 0x00;
182  break;
183 
184  case SPI_MODE_2:
185  SSPSTATbits.CKE = 0x01;
186  SSPCON1bits.CKP = 0x01;
187  break;
188 
189  case SPI_MODE_3:
190  SSPSTATbits.CKE = 0x00;
191  SSPCON1bits.CKP = 0x01;
192  break;
193  }
194 }
195 
196 #ifdef SPI_MODULE_TYPE_2
197  void SPI2_mode (unsigned char communication_mode){
198 
199  SSP2STATbits.CKE = 0x00;
200 
201  switch (communication_mode){
202  case SPI_MODE_0: // Data is sent on the rising edge
203  SSP2STATbits.CKE = 0x01;
204  SSP2CON1bits.CKP = 0x00;
205  break;
206 
207  case SPI_MODE_1:
208  SSP2STATbits.CKE = 0x00;
209  SSP2CON1bits.CKP = 0x00;
210  break;
211 
212  case SPI_MODE_2:
213  SSP2STATbits.CKE = 0x01;
214  SSP2CON1bits.CKP = 0x01;
215  break;
216 
217  case SPI_MODE_3:
218  SSP2STATbits.CKE = 0x00;
219  SSP2CON1bits.CKP = 0x01;
220  break;
221  }
222  }
223 #endif
224 
225 //************************************************************
226 // SPI1_write_byte Implementation
227 //************************************************************
228 signed char SPI1_write_byte (unsigned char byte_to_send){
229 
230  unsigned char data;
231 
232  //Clear the register by reading it out
233  data = SSPBUF;
234 
235  //Clear history
236  PIR1bits.SSPIF = 0;
237  SSPCON1bits.WCOL = 0;
238 
239  //Send the data out
240  SSPBUF = byte_to_send;
241 
242  //Check for bus collision
243  if (SSPCON1bits.WCOL == 0x01){
244  return (-1);
245  }
246 
247  //Wait the end of transmission
248  while(!SPI1_is_TX_over());
249 
250  //No error occured
251  return (0);
252 }
253 
254 #ifdef SPI_MODULE_TYPE_2
255  signed char SPI2_write_byte (unsigned char byte_to_send){
256 
257  unsigned char data;
258 
259  //Clear the register by reading it out
260  data = SSP2BUF;
261 
262  //Clear history
263  PIR3bits.SSP2IF = 0;
264  SSP2CON1bits.WCOL = 0;
265 
266  //Send the data out
267  SSP2BUF = byte_to_send;
268 
269  //Check for bus collision
270  if (SSP2CON1bits.WCOL == 0x01){
271  return (1);
272  }
273 
274  //Wait end of transmission
275  while(!SPI2_is_TX_over());
276 
277  //No error occured
278  return (0);
279  }
280 #endif
281 
282 
283 //************************************************************
284 // SPI1_read_byte Implementation
285 //************************************************************
286 unsigned char SPI1_read_byte (void){
287 
288  unsigned char data;
289  data = SSPBUF;
290 
291  //Clear history
292  PIR1bits.SSPIF = 0;
293 
294  //Write 0x00 to initiate a write
295  SSPBUF = 0x00;
296 
297  //Wait end of transmission
298  while(!SPI1_is_TX_over());
299 
300  //Return the new byte out of the 0x00 sent out
301  return (SSPBUF);
302 }
303 
304 #ifdef SPI_MODULE_TYPE_2
305  unsigned char SPI2_read_byte (void){
306 
307  unsigned char data;
308  data = SSP2BUF;
309 
310  //Clear history
311  PIR3bits.SSP2IF = 0;
312 
313  //Write 0x00 to initiate a write
314  SSP2BUF = 0x00;
315 
316  //Wait end of transmission
317  while(!SPI2_is_TX_over());
318 
319  //Return the new byte out of the 0x00 sent out
320  return (SSP2BUF);
321  }
322 #endif