Contiki-NG
spi-arch-legacy.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, University of Michigan.
3  *
4  * Copyright (c) 2015, Weptech elektronik GmbH
5  * Author: Ulf Knoblich, ulf.knoblich@weptech.de
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 /**
34  * \addtogroup cc2538-spi
35  * @{
36  *
37  * \file
38  * Header file for the cc2538 SPI driver, including macros for the
39  * implementation of the low-level SPI primitives such as waiting for the TX
40  * FIFO to be ready, inserting into the TX FIFO, etc.
41  *
42  * It supports the usage of SSI_NUM_INSTANCES instances by providing new
43  * functions calls like
44  *
45  * - spix_init(uint8_t instance)
46  * - spix_enable(uint8_t instance)
47  * - spix_disable(uint8_t instance)
48  * - spix_set_mode(unit8_t instance, ...)
49  *
50  * and new macros like
51  *
52  * - SPIX_WAITFORTxREADY(x)
53  * - SPIX_WAITFOREOTx(x)
54  * - SPIX_WAITFOREORx(x)
55  * - SPIX_FLUSH(x)
56  *
57  * Some of the old functions and macros are still supported.
58  * When using these deprecated functions, the SSI module to use
59  * has to be be selected by means of the macro SPI_CONF_DEFAULT_INSTANCE.
60  *
61  * This SPI driver depends on the following defines:
62  *
63  * For the SSI0 module:
64  *
65  * - SPI0_CKL_PORT
66  * - SPI0_CLK_PIN
67  * - SPI0_TX_PORT
68  * - SPI0_TX_PIN
69  * - SPI0_RX_PORT
70  * - SPI0_RX_PIN
71  *
72  * For the SSI1 module:
73  *
74  * - SPI1_CKL_PORT
75  * - SPI1_CLK_PIN
76  * - SPI1_TX_PORT
77  * - SPI1_TX_PIN
78  * - SPI1_RX_PORT
79  * - SPI1_RX_PIN
80  */
81 #ifndef SPI_ARCH_LEGACY_H_
82 #define SPI_ARCH_LEGACY_H_
83 
84 #include "contiki.h"
85 
86 #include "dev/ssi.h"
87 /*---------------------------------------------------------------------------*/
88 /* The SPI instance to use when using the deprecated SPI API. */
89 #ifdef SPI_CONF_DEFAULT_INSTANCE
90 #if SPI_CONF_DEFAULT_INSTANCE > (SSI_INSTANCE_COUNT - 1)
91 #error Invalid SPI_CONF_DEFAULT_INSTANCE: valid values are 0 and 1
92 #else
93 #define SPI_DEFAULT_INSTANCE SPI_CONF_DEFAULT_INSTANCE
94 #endif
95 #endif
96 /*---------------------------------------------------------------------------*/
97 /* Default values for the clock rate divider */
98 #ifdef SPI0_CONF_CPRS_CPSDVSR
99 #define SPI0_CPRS_CPSDVSR SPI0_CONF_CPRS_CPSDVSR
100 #else
101 #define SPI0_CPRS_CPSDVSR 2
102 #endif
103 
104 #ifdef SPI1_CONF_CPRS_CPSDVSR
105 #define SPI1_CPRS_CPSDVSR SPI1_CONF_CPRS_CPSDVSR
106 #else
107 #define SPI1_CPRS_CPSDVSR 2
108 #endif
109 /*---------------------------------------------------------------------------*/
110 /* New API macros */
111 #define SPIX_WAITFORTxREADY(spi) do { \
112  while(!(REG(SSI_BASE(spi) + SSI_SR) & SSI_SR_TNF)) ; \
113 } while(0)
114 #define SPIX_BUF(spi) REG(SSI_BASE(spi) + SSI_DR)
115 #define SPIX_WAITFOREOTx(spi) do { \
116  while(REG(SSI_BASE(spi) + SSI_SR) & SSI_SR_BSY) ; \
117 } while(0)
118 #define SPIX_WAITFOREORx(spi) do { \
119  while(!(REG(SSI_BASE(spi) + SSI_SR) & SSI_SR_RNE)) ; \
120 } while(0)
121 #define SPIX_FLUSH(spi) do { \
122  while(REG(SSI_BASE(spi) + SSI_SR) & SSI_SR_RNE) { \
123  SPIX_BUF(spi); \
124  } \
125 } while(0)
126 #define SPIX_CS_CLR(port, pin) do { \
127  GPIO_CLR_PIN(GPIO_PORT_TO_BASE(port), GPIO_PIN_MASK(pin)); \
128 } while(0)
129 #define SPIX_CS_SET(port, pin) do { \
130  GPIO_SET_PIN(GPIO_PORT_TO_BASE(port), GPIO_PIN_MASK(pin)); \
131 } while(0)
132 /*---------------------------------------------------------------------------*/
133 /* Deprecated macros provided for compatibility reasons */
134 #ifdef SPI_DEFAULT_INSTANCE
135 #define SPI_WAITFORTxREADY() SPIX_WAITFORTxREADY(SPI_DEFAULT_INSTANCE)
136 #define SPI_TXBUF SPIX_BUF(SPI_DEFAULT_INSTANCE)
137 #define SPI_RXBUF SPI_TXBUF
138 #define SPI_WAITFOREOTx() SPIX_WAITFOREOTx(SPI_DEFAULT_INSTANCE)
139 #define SPI_WAITFOREORx() SPIX_WAITFOREORx(SPI_DEFAULT_INSTANCE)
140 #ifdef SPI_FLUSH
141 #error You must include spi-arch-legacy.h before spi-legacy.h for the CC2538
142 #else
143 #define SPI_FLUSH() SPIX_FLUSH(SPI_DEFAULT_INSTANCE)
144 #endif
145 #define SPI_CS_CLR(port, pin) SPIX_CS_CLR(port, pin)
146 #define SPI_CS_SET(port, pin) SPIX_CS_SET(port, pin)
147 #endif /* #ifdef SPI_DEFAULT_INSTANCE */
148 /*---------------------------------------------------------------------------*/
149 /** \name Arch-specific SPI functions
150  * @{
151  */
152 
153 /**
154  * \brief Initialize the SPI bus for the instance given
155  *
156  * This sets the mode to Motorola SPI with the following format options:
157  * Clock phase: 1; data captured on second (rising) edge
158  * Clock polarity: 1; clock is high when idle
159  * Data size: 8 bits
160  *
161  * Use spix_set_mode() to change the spi mode.
162  */
163 void spix_init(uint8_t spi);
164 
165 /**
166  * \brief Enables the SPI peripheral for the instance given
167  */
168 void spix_enable(uint8_t spi);
169 
170 /**
171  * \brief Disables the SPI peripheral for the instance given
172  * \note Call this function to save power when the SPI is unused.
173  */
174 void spix_disable(uint8_t spi);
175 
176 /**
177  * \brief Configure the SPI data and clock polarity and the data size for the
178  * instance given
179  *
180  * This function configures the SSI peripheral to use a particular SPI
181  * configuration that a slave device requires. It should always be called
182  * before using the SPI bus as another driver could have changed the settings.
183  *
184  * See section 19.4.4 in the CC2538 user guide for more information.
185  *
186  * \param spi The SSI instance to use.
187  * \param frame_format Set the SSI frame format. Use SSI_CR0_FRF_MOTOROLA,
188  * SSI_CR0_FRF_TI, or SSI_CR0_FRF_MICROWIRE.
189  * \param clock_polarity In Motorola mode, set whether the clock is high or low
190  * when idle. Use SSI_CR0_SPO or 0.
191  * \param clock_phase In Motorola mode, select whether data is valid on the
192  * first or second edge of the clock. Use SSI_CR0_SPH or 0.
193  * \param data_size The number of bits in each "byte" of data. Must be
194  * between 4 and 16, inclusive.
195  */
196 void spix_set_mode(uint8_t spi, uint32_t frame_format,
197  uint32_t clock_polarity, uint32_t clock_phase,
198  uint32_t data_size);
199 
200 /**
201  * \brief Sets the SPI clock frequency of the given SSI instance.
202  *
203  * \param spi SSI instance
204  * \param freq Frequency (Hz)
205  */
206 void spix_set_clock_freq(uint8_t spi, uint32_t freq);
207 
208 /**
209  * \brief Configure a GPIO to be the chip select pin.
210  *
211  * Even if this function does not depend on the SPI instance used, we rename
212  * it to reflect the new naming convention.
213  */
214 void spix_cs_init(uint8_t port, uint8_t pin);
215 
216 /** @} */
217 
218 #endif /* SPI_ARCH_LEGACY_H_ */
219 
220 /**
221  * @}
222  */
void spix_init(uint8_t spi)
Initialize the SPI bus for the instance given.
Definition: spi-legacy.c:213
void spix_disable(uint8_t spi)
Disables the SPI peripheral for the instance given.
Definition: spi-legacy.c:286
Header file for the cc2538 Synchronous Serial Interface.
void spix_set_mode(uint8_t spi, uint32_t frame_format, uint32_t clock_polarity, uint32_t clock_phase, uint32_t data_size)
Configure the SPI data and clock polarity and the data size for the instance given.
Definition: spi-legacy.c:295
void spix_set_clock_freq(uint8_t spi, uint32_t freq)
Sets the SPI clock frequency of the given SSI instance.
Definition: spi-legacy.c:323
void spix_cs_init(uint8_t port, uint8_t pin)
Configure a GPIO to be the chip select pin.
Definition: spi-legacy.c:354
void spix_enable(uint8_t spi)
Enables the SPI peripheral for the instance given.
Definition: spi-legacy.c:277