Contiki 3.x
pic32_spi.c
Go to the documentation of this file.
1 /*
2  * Contiki PIC32 Port project
3  *
4  * Copyright (c) 2012,
5  * Scuola Superiore Sant'Anna (http://www.sssup.it) and
6  * Consorzio Nazionale Interuniversitario per le Telecomunicazioni
7  * (http://www.cnit.it).
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the Institute nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /**
38  * \addtogroup pic32 PIC32 Contiki Port
39  *
40  * @{
41  */
42 
43 /**
44  * \file pic32_spi.c
45  * \brief SPI interface for PIC32MX (pic32mx795f512l)
46  * \author Giovanni Pellerano <giovanni.pellerano@evilaliv3.org>
47  * \date 2012-03-21
48  */
49 
50 /*
51  * PIC32MX795F512L - Specific Functions
52  *
53  * All the functions in this part of the file are specific for the
54  * pic32mx795f512l that is characterized by registers' name that differ from
55  * the 3xx and 4xx families of the pic32mx.
56  */
57 
58 #define __SPI_CODE_TEST__ 0
59 
60 #if __SPI_CODE_TEST__
61 #define __USE_SPI__ 1
62 #define __USE_SPI_PORT1__ 1
63 #define __USE_SPI_PORT1A__ 1
64 #define __USE_SPI_PORT2A__ 1
65 #define __USE_SPI_PORT3A__ 1
66 #endif /* __SPI_CODE_TEST__ */
67 
68 #ifdef __USE_SPI__
69 
70 #include <pic32_spi.h>
71 #include <pic32_clock.h>
72 #include <pic32_irq.h>
73 
74 #include <p32xxxx.h>
75 
76 /*
77  * PIC32MX795F512L - Specific Functions
78  *
79  * All the functions in this part of the file are specific for the
80  * pic32mx795f512l that is characterized by registers' name that differ from
81  * the 3xx and 4xx families of the pic32mx.
82  */
83 
84 #define IS_MASTER(flags) ((flags) & SPI_MASTER)
85 
86 /*---------------------------------------------------------------------------*/
87 #define SPI_PORT(XX, YY) \
88  int8_t \
89  pic32_spi##XX##_init(uint32_t baudrate, uint32_t flags) \
90  { \
91  \
92  IEC##YY##CLR = _IEC##YY##_SPI##XX##EIE_MASK | \
93  _IEC##YY##_SPI##XX##TXIE_MASK | \
94  _IEC##YY##_SPI##XX##RXIE_MASK; \
95  \
96  IFS##YY##CLR = _IFS##YY##_SPI##XX##EIF_MASK | \
97  _IFS##YY##_SPI##XX##TXIF_MASK | \
98  _IFS##YY##_SPI##XX##RXIF_MASK; \
99  \
100  SPI##XX##BRG = pic32_clock_calculate_brg(2, baudrate); \
101  \
102  SPI##XX##CON = 0; \
103  \
104  /* Flag parsing */ \
105  \
106  if(!IS_MASTER(flags)) { \
107  return -SPI_ERR_UNIMPLEMENTED; \
108  } \
109  \
110  SPI##XX##CON = flags | _SPI##XX##CON_ON_MASK; \
111  \
112  return SPI_NO_ERRORS; \
113  } \
114  \
115  int8_t \
116  pic32_spi##XX##_close() \
117  { \
118  SPI##XX##CONCLR = _SPI##XX##CON_ON_MASK; \
119  return SPI_NO_ERRORS; \
120  } \
121  \
122  int8_t \
123  pic32_spi##XX##_write(const uint8_t *data, uint32_t len) \
124  { \
125  uint32_t i; \
126  uint32_t dummy; \
127  \
128  for(i = 0; i < len; ++i) { \
129  SPI##XX##STATCLR = _SPI##XX##STAT_SPIROV_MASK; \
130  while(!SPI##XX##STATbits.SPITBE) { \
131  ; \
132  } \
133  ASM_DIS_INT; /* fix errata 44 */ \
134  SPI##XX##BUF = data[i]; \
135  ASM_EN_INT; /* fix errata 44 */ \
136  while(!SPI##XX##STATbits.SPIRBF) { \
137  ; \
138  } \
139  SPI##XX##STATCLR = _SPI##XX##STAT_SPIROV_MASK; \
140  dummy = SPI##XX##BUF; \
141  } \
142  \
143  return SPI_NO_ERRORS; \
144  } \
145  \
146  int8_t \
147  pic32_spi##XX##_read(uint8_t *data, uint32_t len) \
148  { \
149  uint32_t i; \
150  \
151  for(i = 0; i < len; ++i) { \
152  SPI##XX##STATCLR = _SPI##XX##STAT_SPIROV_MASK; \
153  while(!SPI##XX##STATbits.SPITBE) { \
154  ; \
155  } \
156  SPI##XX##BUF = 0; \
157  while(!SPI##XX##STATbits.SPIRBF) { \
158  ; \
159  } \
160  SPI##XX##STATCLR = _SPI##XX##STAT_SPIROV_MASK; \
161  data[i] = SPI##XX##BUF & 0x00FF; \
162  } \
163  \
164  return SPI_NO_ERRORS; \
165  }
166 /*---------------------------------------------------------------------------*/
167 
168 #ifdef __USE_SPI_PORT1__
169 SPI_PORT(1, 0)
170 #endif /* __USE_SPI_PORT1__ */
171 
172 #ifdef __USE_SPI_PORT1A__
173 SPI_PORT(1A, 0)
174 #endif /* __USE_SPI_PORT1A__ */
175 
176 #ifdef __USE_SPI_PORT2A__
177 SPI_PORT(2A, 1)
178 #endif /* __USE_SPI_PORT2A__ */
179 
180 #ifdef __USE_SPI_PORT3A__
181 SPI_PORT(3A, 1)
182 #endif /* __USE_SPI_PORT3A__ */
183 
184 #endif /* __USE_SPI__ */
185 
186 /** @} */
SPI interface for PIC32MX (pic32mx795f512l)
INTERRUPT interface for PIC32MX (pic32mx795f512l)
CLOCK interface for PIC32MX (pic32mx795f512l)