Contiki 3.x
spi.c
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-spi
31  * @{
32  *
33  * \file
34  * Implementation of the cc2538 SPI peripheral
35  */
36 #include "contiki.h"
37 #include "reg.h"
38 #include "spi-arch.h"
39 #include "dev/ioc.h"
40 #include "dev/sys-ctrl.h"
41 #include "dev/spi.h"
42 #include "dev/ssi.h"
43 #include "dev/gpio.h"
44 
45 #define SPI_CLK_PORT_BASE GPIO_PORT_TO_BASE(SPI_CLK_PORT)
46 #define SPI_CLK_PIN_MASK GPIO_PIN_MASK(SPI_CLK_PIN)
47 #define SPI_MOSI_PORT_BASE GPIO_PORT_TO_BASE(SPI_MOSI_PORT)
48 #define SPI_MOSI_PIN_MASK GPIO_PIN_MASK(SPI_MOSI_PIN)
49 #define SPI_MISO_PORT_BASE GPIO_PORT_TO_BASE(SPI_MISO_PORT)
50 #define SPI_MISO_PIN_MASK GPIO_PIN_MASK(SPI_MISO_PIN)
51 
52 /**
53  * \brief Initialize the SPI bus.
54  *
55  * This SPI init() function uses the following #defines to set the pins:
56  * SPI_CLK_PORT SPI_CLK_PIN
57  * SPI_MOSI_PORT SPI_MOSI_PIN
58  * SPI_MISO_PORT SPI_MISO_PIN
59  *
60  * This sets the mode to Motorola SPI with the following format options:
61  * Clock phase: 1; data captured on second (rising) edge
62  * Clock polarity: 1; clock is high when idle
63  * Data size: 8 bits
64  */
65 void
66 spi_init(void)
67 {
68  spi_enable();
69 
70  /* Start by disabling the peripheral before configuring it */
71  REG(SSI0_BASE + SSI_CR1) = 0;
72 
73  /* Set the IO clock as the SSI clock */
74  REG(SSI0_BASE + SSI_CC) = 1;
75 
76  /* Set the mux correctly to connect the SSI pins to the correct GPIO pins */
77  ioc_set_sel(SPI_CLK_PORT, SPI_CLK_PIN, IOC_PXX_SEL_SSI0_CLKOUT);
78  ioc_set_sel(SPI_MOSI_PORT, SPI_MOSI_PIN, IOC_PXX_SEL_SSI0_TXD);
79  REG(IOC_SSIRXD_SSI0) = (SPI_MISO_PORT * 8) + SPI_MISO_PIN;
80 
81  /* Put all the SSI gpios into peripheral mode */
82  GPIO_PERIPHERAL_CONTROL(SPI_CLK_PORT_BASE, SPI_CLK_PIN_MASK);
83  GPIO_PERIPHERAL_CONTROL(SPI_MOSI_PORT_BASE, SPI_MOSI_PIN_MASK);
84  GPIO_PERIPHERAL_CONTROL(SPI_MISO_PORT_BASE, SPI_MISO_PIN_MASK);
85 
86  /* Disable any pull ups or the like */
87  ioc_set_over(SPI_CLK_PORT, SPI_CLK_PIN, IOC_OVERRIDE_DIS);
88  ioc_set_over(SPI_MOSI_PORT, SPI_MOSI_PIN, IOC_OVERRIDE_DIS);
89  ioc_set_over(SPI_MISO_PORT, SPI_MISO_PIN, IOC_OVERRIDE_DIS);
90 
91  /* Configure the clock */
92  REG(SSI0_BASE + SSI_CPSR) = 2;
93 
94  /* Configure the default SPI options.
95  * mode: Motorola frame format
96  * clock: High when idle
97  * data: Valid on rising edges of the clock
98  * bits: 8 byte data
99  */
100  REG(SSI0_BASE + SSI_CR0) = SSI_CR0_SPH | SSI_CR0_SPO | (0x07);
101 
102  /* Enable the SSI */
103  REG(SSI0_BASE + SSI_CR1) |= SSI_CR1_SSE;
104 }
105 /*---------------------------------------------------------------------------*/
106 void
107 spi_cs_init(uint8_t port, uint8_t pin)
108 {
110  ioc_set_over(port, pin, IOC_OVERRIDE_DIS);
113 }
114 /*---------------------------------------------------------------------------*/
115 void
117 {
118  /* Enable the clock for the SSI peripheral */
119  REG(SYS_CTRL_RCGCSSI) |= 1;
120 }
121 /*---------------------------------------------------------------------------*/
122 void
124 {
125  /* Gate the clock for the SSI peripheral */
126  REG(SYS_CTRL_RCGCSSI) &= ~1;
127 }
128 /*---------------------------------------------------------------------------*/
129 void spi_set_mode(uint32_t frame_format, uint32_t clock_polarity, uint32_t clock_phase, uint32_t data_size)
130 {
131  /* Disable the SSI peripheral to configure it */
132  REG(SSI0_BASE + SSI_CR1) = 0;
133 
134  /* Configure the SSI options */
135  REG(SSI0_BASE + SSI_CR0) = clock_phase | clock_polarity | frame_format | (data_size - 1);
136 
137  /* Re-enable the SSI */
138  REG(SSI0_BASE + SSI_CR1) |= SSI_CR1_SSE;
139 }
140 /** @} */
#define GPIO_PORT_TO_BASE(PORT)
Converts a port number to the port base address.
Definition: gpio.h:276
Header file for the cc2538 SPI commands.
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 with register and macro declarations for the cc2538 GPIO module.
#define SYS_CTRL_RCGCSSI
SSI[1:0] clocks - active mode.
Definition: sys-ctrl.h:69
#define SSI_CPSR
Clock divider.
Definition: ssi.h:63
Header file for the cc2538 Synchronous Serial Interface.
void ioc_set_over(uint8_t port, uint8_t pin, uint8_t over)
Set Port:Pin override function.
Definition: ioc.c:54
Header file with declarations for the I/O Control module.
void spi_enable(void)
Enables the SPI peripheral.
Definition: spi.c:116
#define GPIO_PERIPHERAL_CONTROL(PORT_BASE, PIN_MASK)
Configure the pin to be under peripheral control with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:206
#define IOC_SSIRXD_SSI0
SSI0 RX.
Definition: ioc.h:129
#define SSI_CC
Clock configuration.
Definition: ssi.h:69
#define IOC_OVERRIDE_DIS
Override Disabled.
Definition: ioc.h:226
#define SSI_CR0
Control register 0.
Definition: ssi.h:59
Header file with register manipulation macro definitions.
#define SSI0_BASE
Base address for SSI0.
Definition: ssi.h:52
void ioc_set_sel(uint8_t port, uint8_t pin, uint8_t sel)
Function select for Port:Pin.
Definition: ioc.c:60
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
Header file for the cc2538 System Control driver.
#define SSI_CR1
Control register 1.
Definition: ssi.h:60
#define GPIO_SOFTWARE_CONTROL(PORT_BASE, PIN_MASK)
Configure the pin to be software controlled with PIN_MASK of port with PORT_BASE. ...
Definition: gpio.h:214
#define GPIO_SET_OUTPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to output.
Definition: gpio.h:100
#define SSI_CR0_SPH
Serial clock phase (H)
Definition: ssi.h:146
#define SSI_CR0_SPO
Serial clock phase (O)
Definition: ssi.h:147
#define SSI_CR1_SSE
Synchronous serial port enable.
Definition: ssi.h:153
Basic SPI macros
#define GPIO_PIN_MASK(PIN)
Converts a pin number to a pin mask.
Definition: gpio.h:268
#define GPIO_SET_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE high.
Definition: gpio.h:107
void spi_init(void)
Initialize the SPI bus.
Definition: spi.c:48
void spi_disable(void)
Disables the SPI peripheral.
Definition: spi.c:123