Contiki 3.x
board-i2c.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-i2c
33  * @{
34  *
35  * \file
36  * Board-specific I2C driver for the Sensortag-CC26xx
37  */
38 /*---------------------------------------------------------------------------*/
39 #include "contiki-conf.h"
40 #include "ti-lib.h"
41 #include "board-i2c.h"
42 #include "lpm.h"
43 
44 #include <string.h>
45 #include <stdbool.h>
46 /*---------------------------------------------------------------------------*/
47 #define NO_INTERFACE 0xFF
48 /*---------------------------------------------------------------------------*/
49 static uint8_t slave_addr = 0x00;
50 static uint8_t interface = NO_INTERFACE;
51 /*---------------------------------------------------------------------------*/
52 static bool
53 accessible(void)
54 {
55  /* First, check the PD */
56  if(ti_lib_prcm_power_domain_status(PRCM_DOMAIN_SERIAL)
57  != PRCM_DOMAIN_POWER_ON) {
58  return false;
59  }
60 
61  /* Then check the 'run mode' clock gate */
62  if(!(HWREG(PRCM_BASE + PRCM_O_I2CCLKGR) & PRCM_I2CCLKGR_CLK_EN)) {
63  return false;
64  }
65 
66  return true;
67 }
68 /*---------------------------------------------------------------------------*/
69 void
71 {
72  /* First, make sure the SERIAL PD is on */
73  ti_lib_prcm_power_domain_on(PRCM_DOMAIN_SERIAL);
74  while((ti_lib_prcm_power_domain_status(PRCM_DOMAIN_SERIAL)
75  != PRCM_DOMAIN_POWER_ON));
76 
77  /* Enable the clock to I2C */
78  ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_I2C0);
79  ti_lib_prcm_load_set();
80  while(!ti_lib_prcm_load_get());
81 
82  /* Enable and initialize the I2C master module */
83  ti_lib_i2c_master_init_exp_clk(I2C0_BASE, ti_lib_sys_ctrl_clock_get(),
84  true);
85 }
86 /*---------------------------------------------------------------------------*/
87 static bool
88 i2c_status()
89 {
90  uint32_t status;
91 
92  status = ti_lib_i2c_master_err(I2C0_BASE);
93  if(status & (I2C_MSTAT_DATACK_N_M | I2C_MSTAT_ADRACK_N_M)) {
94  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
95  }
96 
97  return status == I2C_MASTER_ERR_NONE;
98 }
99 /*---------------------------------------------------------------------------*/
100 void
102 {
103  interface = NO_INTERFACE;
104 
105  if(accessible()) {
106  ti_lib_i2c_master_disable(I2C0_BASE);
107  }
108 
109  ti_lib_prcm_peripheral_run_disable(PRCM_PERIPH_I2C0);
110  ti_lib_prcm_load_set();
111  while(!ti_lib_prcm_load_get());
112 
113  /*
114  * Set all pins to GPIO Input and disable the output driver. Set internal
115  * pull to match external pull
116  *
117  * SDA and SCL: external PU resistor
118  * SDA HP and SCL HP: MPU PWR low
119  */
120  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SDA_HP);
121  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SDA_HP, IOC_IOPULL_DOWN);
122  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SCL_HP);
123  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SCL_HP, IOC_IOPULL_DOWN);
124 
125  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SDA);
126  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SDA, IOC_IOPULL_UP);
127  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SCL);
128  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SCL, IOC_IOPULL_UP);
129 }
130 /*---------------------------------------------------------------------------*/
131 bool
132 board_i2c_write(uint8_t *data, uint8_t len)
133 {
134  uint32_t i;
135  bool success;
136 
137  /* Write slave address */
138  ti_lib_i2c_master_slave_addr_set(I2C0_BASE, slave_addr, false);
139 
140  /* Write first byte */
141  ti_lib_i2c_master_data_put(I2C0_BASE, data[0]);
142 
143  /* Check if another master has access */
144  while(ti_lib_i2c_master_bus_busy(I2C0_BASE));
145 
146  /* Assert RUN + START */
147  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
148  while(ti_lib_i2c_master_busy(I2C0_BASE));
149  success = i2c_status();
150 
151  for(i = 1; i < len && success; i++) {
152  /* Write next byte */
153  ti_lib_i2c_master_data_put(I2C0_BASE, data[i]);
154  if(i < len - 1) {
155  /* Clear START */
156  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
157  while(ti_lib_i2c_master_busy(I2C0_BASE));
158  success = i2c_status();
159  }
160  }
161 
162  /* Assert stop */
163  if(success) {
164  /* Assert STOP */
165  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
166  while(ti_lib_i2c_master_busy(I2C0_BASE));
167  success = i2c_status();
168  while(ti_lib_i2c_master_bus_busy(I2C0_BASE));
169  }
170 
171  return success;
172 }
173 /*---------------------------------------------------------------------------*/
174 bool
176 {
177  bool success;
178 
179  /* Write slave address */
180  ti_lib_i2c_master_slave_addr_set(I2C0_BASE, slave_addr, false);
181 
182  /* Write first byte */
183  ti_lib_i2c_master_data_put(I2C0_BASE, data);
184 
185  /* Check if another master has access */
186  while(ti_lib_i2c_master_bus_busy(I2C0_BASE));
187 
188  /* Assert RUN + START + STOP */
189  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
190  while(ti_lib_i2c_master_busy(I2C0_BASE));
191  success = i2c_status();
192 
193  return success;
194 }
195 /*---------------------------------------------------------------------------*/
196 bool
197 board_i2c_read(uint8_t *data, uint8_t len)
198 {
199  uint8_t i;
200  bool success;
201 
202  /* Set slave address */
203  ti_lib_i2c_master_slave_addr_set(I2C0_BASE, slave_addr, true);
204 
205  /* Check if another master has access */
206  while(ti_lib_i2c_master_bus_busy(I2C0_BASE));
207 
208  /* Assert RUN + START + ACK */
209  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
210 
211  i = 0;
212  success = true;
213  while(i < (len - 1) && success) {
214  while(ti_lib_i2c_master_busy(I2C0_BASE));
215  success = i2c_status();
216  if(success) {
217  data[i] = ti_lib_i2c_master_data_get(I2C0_BASE);
218  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
219  i++;
220  }
221  }
222 
223  if(success) {
224  while(ti_lib_i2c_master_busy(I2C0_BASE));
225  success = i2c_status();
226  if(success) {
227  data[len - 1] = ti_lib_i2c_master_data_get(I2C0_BASE);
228  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
229  while(ti_lib_i2c_master_bus_busy(I2C0_BASE));
230  }
231  }
232 
233  return success;
234 }
235 /*---------------------------------------------------------------------------*/
236 bool
237 board_i2c_write_read(uint8_t *wdata, uint8_t wlen, uint8_t *rdata, uint8_t rlen)
238 {
239  uint32_t i;
240  bool success;
241 
242  /* Set slave address for write */
243  ti_lib_i2c_master_slave_addr_set(I2C0_BASE, slave_addr, false);
244 
245  /* Write first byte */
246  ti_lib_i2c_master_data_put(I2C0_BASE, wdata[0]);
247 
248  /* Check if another master has access */
249  while(ti_lib_i2c_master_bus_busy(I2C0_BASE));
250 
251  /* Assert RUN + START */
252  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
253  while(ti_lib_i2c_master_busy(I2C0_BASE));
254  success = i2c_status();
255 
256  for(i = 1; i < wlen && success; i++) {
257  /* Write next byte */
258  ti_lib_i2c_master_data_put(I2C0_BASE, wdata[i]);
259  if(i < wlen - 1) {
260  /* Clear START */
261  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
262  while(ti_lib_i2c_master_busy(I2C0_BASE));
263  success = i2c_status();
264  }
265  }
266  if(!success) {
267  return false;
268  }
269 
270  /* Set slave address for read */
271  ti_lib_i2c_master_slave_addr_set(I2C0_BASE, slave_addr, true);
272 
273  /* Assert ACK */
274  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
275 
276  i = 0;
277  while(i < (rlen - 1) && success) {
278  while(ti_lib_i2c_master_busy(I2C0_BASE));
279  success = i2c_status();
280  if(success) {
281  rdata[i] = ti_lib_i2c_master_data_get(I2C0_BASE);
282  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
283  i++;
284  }
285  }
286 
287  if(success) {
288  while(ti_lib_i2c_master_busy(I2C0_BASE));
289  success = i2c_status();
290  if(success) {
291  rdata[rlen - 1] = ti_lib_i2c_master_data_get(I2C0_BASE);
292  ti_lib_i2c_master_control(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
293  while(ti_lib_i2c_master_bus_busy(I2C0_BASE));
294  }
295  }
296 
297  return success;
298 }
299 /*---------------------------------------------------------------------------*/
300 void
301 board_i2c_select(uint8_t new_interface, uint8_t address)
302 {
303  slave_addr = address;
304 
305  if(accessible() == false) {
307  }
308 
309  if(new_interface != interface) {
310  interface = new_interface;
311 
312  ti_lib_i2c_master_disable(I2C0_BASE);
313 
314  if(interface == BOARD_I2C_INTERFACE_0) {
315  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SDA, IOC_NO_IOPULL);
316  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SCL, IOC_NO_IOPULL);
317  ti_lib_ioc_pin_type_i2c(I2C0_BASE, BOARD_IOID_SDA, BOARD_IOID_SCL);
318  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SDA_HP);
319  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SCL_HP);
320  } else if(interface == BOARD_I2C_INTERFACE_1) {
321  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SDA_HP, IOC_NO_IOPULL);
322  ti_lib_ioc_io_port_pull_set(BOARD_IOID_SCL_HP, IOC_NO_IOPULL);
323  ti_lib_ioc_pin_type_i2c(I2C0_BASE, BOARD_IOID_SDA_HP, BOARD_IOID_SCL_HP);
324  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SDA);
325  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SCL);
326  }
327 
328  /* Enable and initialize the I2C master module */
329  ti_lib_i2c_master_init_exp_clk(I2C0_BASE, ti_lib_sys_ctrl_clock_get(),
330  true);
331  }
332 }
333 /*---------------------------------------------------------------------------*/
334 /** @} */
#define BOARD_IOID_SCL_HP
Interface 1 SCL: MPU.
Definition: board.h:180
Header file with macros which rename TI CC26xxware functions.
bool board_i2c_write(uint8_t *data, uint8_t len)
Burst write to an I2C device.
Definition: board-i2c.c:132
#define BOARD_IOID_SDA
I2C IOID mappings.
Definition: board.h:177
bool board_i2c_write_read(uint8_t *wdata, uint8_t wlen, uint8_t *rdata, uint8_t rlen)
Write and read in one operation.
Definition: board-i2c.c:237
void board_i2c_shutdown()
Stops the I2C peripheral and restores pins to s/w control.
Definition: board-i2c.c:101
bool board_i2c_write_single(uint8_t data)
Single write to an I2C device.
Definition: board-i2c.c:175
bool board_i2c_read(uint8_t *data, uint8_t len)
Burst read from an I2C device.
Definition: board-i2c.c:197
void board_i2c_select(uint8_t new_interface, uint8_t address)
Select an I2C slave.
Definition: board-i2c.c:301
Header file for the Sensortag-CC26xx I2C Driver.
void board_i2c_wakeup()
Enables the I2C peripheral with defaults.
Definition: board-i2c.c:70
#define BOARD_IOID_SDA_HP
Interface 1 SDA: MPU.
Definition: board.h:179
#define I2C0_BASE
Peripheral I2C0 base address.
Definition: MKL25Z4.h:2200
#define BOARD_IOID_SCL
I2C IOID mappings.
Definition: board.h:149