Contiki-Inga 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 #define SPI_SEL_PORT_BASE GPIO_PORT_TO_BASE(SPI_SEL_PORT)
52 #define SPI_SEL_PIN_MASK GPIO_PIN_MASK(SPI_SEL_PIN)
53 
54 /* Default: Motorola mode 3 with 8-bit data words */
55 #ifndef SPI_CONF_PHASE
56 #define SPI_CONF_PHASE SSI_CR0_SPH
57 #endif
58 #ifndef SPI_CONF_POLARITY
59 #define SPI_CONF_POLARITY SSI_CR0_SPO
60 #endif
61 #ifndef SPI_CONF_DATA_SIZE
62 #define SPI_CONF_DATA_SIZE 8
63 #endif
64 
65 #if SPI_CONF_DATA_SIZE < 4 || SPI_CONF_DATA_SIZE > 16
66 #error SPI_CONF_DATA_SIZE must be set between 4 and 16 inclusive.
67 #endif
68 
69 /**
70  * \brief Initialize the SPI bus.
71  *
72  * This SPI init() function uses the following #defines to set the pins:
73  * SPI_CLK_PORT SPI_CLK_PIN
74  * SPI_MOSI_PORT SPI_MOSI_PIN
75  * SPI_MISO_PORT SPI_MISO_PIN
76  * SPI_SEL_PORT SPI_SEL_PIN
77  *
78  * This sets the mode to Motorola SPI with the following format options:
79  * SPI_CONF_PHASE: 0 or SSI_CR0_SPH
80  * SPI_CONF_POLARITY: 0 or SSI_CR0_SPO
81  * SPI_CONF_DATA_SIZE: 4 to 16 bits
82  */
83 void
84 spi_init(void)
85 {
86  spi_enable();
87 
88  /* Start by disabling the peripheral before configuring it */
89  REG(SSI0_BASE + SSI_CR1) = 0;
90 
91  /* Set the IO clock as the SSI clock */
92  REG(SSI0_BASE + SSI_CC) = 1;
93 
94  /* Set the mux correctly to connect the SSI pins to the correct GPIO pins */
95  ioc_set_sel(SPI_CLK_PORT, SPI_CLK_PIN, IOC_PXX_SEL_SSI0_CLKOUT);
96  ioc_set_sel(SPI_MOSI_PORT, SPI_MOSI_PIN, IOC_PXX_SEL_SSI0_TXD);
97  REG(IOC_SSIRXD_SSI0) = (SPI_MISO_PORT * 8) + SPI_MISO_PIN;
98  ioc_set_sel(SPI_SEL_PORT, SPI_SEL_PIN, IOC_PXX_SEL_SSI0_FSSOUT);
99 
100  /* Put all the SSI gpios into peripheral mode */
101  GPIO_PERIPHERAL_CONTROL(SPI_CLK_PORT_BASE, SPI_CLK_PIN_MASK);
102  GPIO_PERIPHERAL_CONTROL(SPI_MOSI_PORT_BASE, SPI_MOSI_PIN_MASK);
103  GPIO_PERIPHERAL_CONTROL(SPI_MISO_PORT_BASE, SPI_MISO_PIN_MASK);
104  GPIO_PERIPHERAL_CONTROL(SPI_SEL_PORT_BASE, SPI_SEL_PIN_MASK);
105 
106  /* Disable any pull ups or the like */
107  ioc_set_over(SPI_CLK_PORT, SPI_CLK_PIN, IOC_OVERRIDE_DIS);
108  ioc_set_over(SPI_MOSI_PORT, SPI_MOSI_PIN, IOC_OVERRIDE_DIS);
109  ioc_set_over(SPI_MISO_PORT, SPI_MISO_PIN, IOC_OVERRIDE_DIS);
110  ioc_set_over(SPI_SEL_PORT, SPI_SEL_PIN, IOC_OVERRIDE_DIS);
111 
112  /* Configure the clock */
113  REG(SSI0_BASE + SSI_CPSR) = 2;
114 
115  /* Put the ssi in Motorola SPI mode using the provided format options */
116  REG(SSI0_BASE + SSI_CR0) = SPI_CONF_PHASE | SPI_CONF_POLARITY | (SPI_CONF_DATA_SIZE - 1);
117 
118  /* Enable the SSI */
119  REG(SSI0_BASE + SSI_CR1) |= SSI_CR1_SSE;
120 }
121 /*---------------------------------------------------------------------------*/
122 void
124 {
125  /* Enable the clock for the SSI peripheral */
126  REG(SYS_CTRL_RCGCSSI) |= 1;
127 }
128 /*---------------------------------------------------------------------------*/
129 void
131 {
132  /* Gate the clock for the SSI peripheral */
133  REG(SYS_CTRL_RCGCSSI) &= ~1;
134 }
135 /** @} */