Contiki 3.x
board-spi.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
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 copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*---------------------------------------------------------------------------*/
31 /**
32  * \addtogroup sensortag-cc26xx-spi
33  * @{
34  *
35  * \file
36  * Board-specific SPI driver common to the Sensortag and LaunchPad
37  */
38 /*---------------------------------------------------------------------------*/
39 #include "contiki.h"
40 #include "ti-lib.h"
41 #include "board-spi.h"
42 #include "board.h"
43 
44 #include <stdbool.h>
45 /*---------------------------------------------------------------------------*/
46 static bool
47 accessible(void)
48 {
49  /* First, check the PD */
50  if(ti_lib_prcm_power_domain_status(PRCM_DOMAIN_SERIAL)
51  != PRCM_DOMAIN_POWER_ON) {
52  return false;
53  }
54 
55  /* Then check the 'run mode' clock gate */
56  if(!(HWREG(PRCM_BASE + PRCM_O_SSICLKGR) & PRCM_SSICLKGR_CLK_EN_SSI0)) {
57  return false;
58  }
59 
60  return true;
61 }
62 /*---------------------------------------------------------------------------*/
63 bool
64 board_spi_write(const uint8_t *buf, size_t len)
65 {
66  if(accessible() == false) {
67  return false;
68  }
69 
70  while(len > 0) {
71  uint32_t ul;
72 
73  ti_lib_ssi_data_put(SSI0_BASE, *buf);
74  ti_lib_rom_ssi_data_get(SSI0_BASE, &ul);
75  len--;
76  buf++;
77  }
78 
79  return true;
80 }
81 /*---------------------------------------------------------------------------*/
82 bool
83 board_spi_read(uint8_t *buf, size_t len)
84 {
85  if(accessible() == false) {
86  return false;
87  }
88 
89  while(len > 0) {
90  uint32_t ul;
91 
92  if(!ti_lib_rom_ssi_data_put_non_blocking(SSI0_BASE, 0)) {
93  /* Error */
94  return false;
95  }
96  ti_lib_rom_ssi_data_get(SSI0_BASE, &ul);
97  *buf = (uint8_t)ul;
98  len--;
99  buf++;
100  }
101  return true;
102 }
103 /*---------------------------------------------------------------------------*/
104 void
106 {
107  if(accessible() == false) {
108  return;
109  }
110 
111  uint32_t ul;
112  while(ti_lib_rom_ssi_data_get_non_blocking(SSI0_BASE, &ul));
113 }
114 /*---------------------------------------------------------------------------*/
115 void
116 board_spi_open(uint32_t bit_rate, uint32_t clk_pin)
117 {
118  uint32_t buf;
119 
120  /* First, make sure the SERIAL PD is on */
121  ti_lib_prcm_power_domain_on(PRCM_DOMAIN_SERIAL);
122  while((ti_lib_prcm_power_domain_status(PRCM_DOMAIN_SERIAL)
123  != PRCM_DOMAIN_POWER_ON));
124 
125  /* Enable clock in active mode */
126  ti_lib_rom_prcm_peripheral_run_enable(PRCM_PERIPH_SSI0);
127  ti_lib_prcm_load_set();
128  while(!ti_lib_prcm_load_get());
129 
130  /* SPI configuration */
131  ti_lib_ssi_int_disable(SSI0_BASE, SSI_RXOR | SSI_RXFF | SSI_RXTO | SSI_TXFF);
132  ti_lib_ssi_int_clear(SSI0_BASE, SSI_RXOR | SSI_RXTO);
133  ti_lib_rom_ssi_config_set_exp_clk(SSI0_BASE, ti_lib_sys_ctrl_clock_get(),
134  SSI_FRF_MOTO_MODE_0,
135  SSI_MODE_MASTER, bit_rate, 8);
136  ti_lib_rom_ioc_pin_type_ssi_master(SSI0_BASE, BOARD_IOID_SPI_MISO,
137  BOARD_IOID_SPI_MOSI, IOID_UNUSED, clk_pin);
138  ti_lib_ssi_enable(SSI0_BASE);
139 
140  /* Get rid of residual data from SSI port */
141  while(ti_lib_ssi_data_get_non_blocking(SSI0_BASE, &buf));
142 }
143 /*---------------------------------------------------------------------------*/
144 void
146 {
147  /* Power down SSI0 */
148  ti_lib_rom_prcm_peripheral_run_disable(PRCM_PERIPH_SSI0);
149  ti_lib_prcm_load_set();
150  while(!ti_lib_prcm_load_get());
151 
152  /* Restore pins to a low-consumption state */
153  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SPI_MISO);
154  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SPI_MISO, IOC_IOPULL_DOWN);
155 
156  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SPI_MOSI);
157  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SPI_MOSI, IOC_IOPULL_DOWN);
158 
159  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SPI_CLK_FLASH);
160  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SPI_CLK_FLASH, IOC_IOPULL_DOWN);
161 }
162 /*---------------------------------------------------------------------------*/
163 /** @} */
Header file with macros which rename TI CC26xxware functions.
Header file for the Sensortag/LaunchPad SPI Driver.
#define SSI0_BASE
Base address for SSI0.
Definition: ssi.h:58
void board_spi_close()
Close the SPI interface.
Definition: board-spi.c:145
bool board_spi_write(const uint8_t *buf, size_t len)
Write to an SPI device.
Definition: board-spi.c:64
bool board_spi_read(uint8_t *buf, size_t len)
Read from an SPI device.
Definition: board-spi.c:83
void board_spi_open(uint32_t bit_rate, uint32_t clk_pin)
Initialize the SPI interface.
Definition: board-spi.c:116
void board_spi_flush()
Clear data from the SPI interface.
Definition: board-spi.c:105
#define BOARD_IOID_SPI_MOSI
SPI IOID mappings.
Definition: board.h:128