Contiki 3.x
spi-arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /**
30  * \addtogroup cc2538
31  * @{
32  *
33  * Implementation of the low-level SPI primitives such as waiting for the TX
34  * FIFO to be ready, inserting into the TX FIFO, etc.
35  * @{
36  */
37 /**
38  * \file
39  * Header file for the cc2538 SPI commands
40  */
41 #ifndef SPI_ARCH_H_
42 #define SPI_ARCH_H_
43 
44 #include "dev/ssi.h"
45 
46 #define SPI_WAITFORTxREADY() do { \
47  while(!(REG(SSI0_BASE + SSI_SR) & SSI_SR_TNF)); \
48 } while(0)
49 
50 #define SPI_TXBUF REG(SSI0_BASE + SSI_DR)
51 
52 #define SPI_RXBUF REG(SSI0_BASE + SSI_DR)
53 
54 #define SPI_WAITFOREOTx() do { \
55  while(REG(SSI0_BASE + SSI_SR) & SSI_SR_BSY); \
56 } while(0)
57 
58 #define SPI_WAITFOREORx() do { \
59  while(!(REG(SSI0_BASE + SSI_SR) & SSI_SR_RNE)); \
60 } while(0)
61 
62 #ifdef SPI_FLUSH
63 #error "You must include spi-arch.h before spi.h for the CC2538."
64 #endif
65 #define SPI_FLUSH() do { \
66  SPI_WAITFOREORx(); \
67  while (REG(SSI0_BASE + SSI_SR) & SSI_SR_RNE) { \
68  SPI_RXBUF; \
69  } \
70 } while(0)
71 
72 #define SPI_CS_CLR(port, pin) do { \
73  GPIO_CLR_PIN(GPIO_PORT_TO_BASE(port), GPIO_PIN_MASK(pin)); \
74 } while(0)
75 
76 #define SPI_CS_SET(port, pin) do { \
77  GPIO_SET_PIN(GPIO_PORT_TO_BASE(port), GPIO_PIN_MASK(pin)); \
78 } while(0)
79 /*---------------------------------------------------------------------------*/
80 /** \name Arch-specific SPI functions
81  * @{
82  */
83 
84 /**
85  * \brief Configure a GPIO to be the chip select pin
86  */
87 void spi_cs_init(uint8_t port, uint8_t pin);
88 
89 /** \brief Enables the SPI peripheral
90  */
91 void spi_enable(void);
92 
93 /** \brief Disables the SPI peripheral
94  * \note Call this function to save power when the SPI is unused.
95  */
96 void spi_disable(void);
97 
98 /**
99  * \brief Configure the SPI data and clock polarity and the data size.
100  *
101  * This function configures the SSI peripheral to use a particular SPI
102  * configuration that a slave device requires. It should always be called
103  * before using the SPI bus as another driver could have changed the settings.
104  *
105  * See section 19.4.4 in the CC2538 user guide for more information.
106  *
107  * \param frame_format Set the SSI frame format. Use SSI_CR0_FRF_MOTOROLA,
108  * SSI_CR0_FRF_TI, or SSI_CR0_FRF_MICROWIRE.
109  * \param clock_polarity In Motorola mode, set whether the clock is high or low
110  * when idle. Use SSI_CR0_SPO or 0.
111  * \param clock_phase In Motorola mode, select whether data is valid on the
112  * first or second edge of the clock. Use SSI_CR0_SPH or 0.
113  * \param data_size The number of bits in each "byte" of data. Must be
114  * between 4 and 16, inclusive.
115  */
116 void spi_set_mode(uint32_t frame_format, uint32_t clock_polarity,
117  uint32_t clock_phase, uint32_t data_size);
118 
119 /** @} */
120 
121 #endif /* SPI_ARCH_H_ */
122 
123 /**
124  * @}
125  * @}
126  */
void spi_cs_init(uint8_t port, uint8_t pin)
Configure a GPIO to be the chip select pin.
Definition: spi.c:107
Header file for the cc2538 Synchronous Serial Interface.
void spi_enable(void)
Enables the SPI peripheral.
Definition: spi.c:116
void spi_set_mode(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.
Definition: spi.c:129
void spi_disable(void)
Disables the SPI peripheral.
Definition: spi.c:123