Contiki 2.5
i2cmaster.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
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 Institute 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  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * I2C communication device drivers for Zolertia Z1 sensor node.
36  * \author
37  * Enric M. Calvo, Zolertia <ecalvo@zolertia.com>
38  * Marcus Lundén, SICS <mlunden@sics.se>
39  */
40 
41 #include "i2cmaster.h"
42 
43 signed char tx_byte_ctr, rx_byte_ctr;
44 unsigned char rx_buf[2];
45 unsigned char* tx_buf_ptr;
46 unsigned char* rx_buf_ptr;
47 unsigned char receive_data;
48 unsigned char transmit_data1;
49 unsigned char transmit_data2;
50 volatile unsigned int i; // volatile to prevent optimization
51 
52 //------------------------------------------------------------------------------
53 // void i2c_receiveinit(unsigned char slave_address,
54 // unsigned char prescale)
55 //
56 // This function initializes the USCI module for master-receive operation.
57 //
58 // IN: unsigned char slave_address => Slave Address
59 // unsigned char prescale => SCL clock adjustment
60 //-----------------------------------------------------------------------------
61 void
62 i2c_receiveinit(u8_t slave_address) {
63  UCB1CTL1 = UCSWRST; // Enable SW reset
64  UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
65  UCB1CTL1 = UCSSEL_2 | UCSWRST; // Use SMCLK, keep SW reset
66  UCB1BR0 = I2C_PRESC_400KHZ_LSB; // prescaler for 400 kHz data rate
67  UCB1BR1 = I2C_PRESC_400KHZ_MSB;
68  UCB1I2CSA = slave_address; // set slave address
69 
70  UCB1CTL1 &= ~UCTR; // I2C Receiver
71 
72  UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
73  UCB1I2CIE = UCNACKIE;
74 #if I2C_RX_WITH_INTERRUPT
75  UC1IE = UCB1RXIE; // Enable RX interrupt if desired
76 #endif
77 }
78 
79 //------------------------------------------------------------------------------
80 // void i2c_transmitinit(unsigned char slave_address,
81 // unsigned char prescale)
82 //
83 // Initializes USCI for master-transmit operation.
84 //
85 // IN: unsigned char slave_address => Slave Address
86 // unsigned char prescale => SCL clock adjustment
87 //------------------------------------------------------------------------------
88 void
89 i2c_transmitinit(u8_t slave_address) {
90  UCB1CTL1 |= UCSWRST; // Enable SW reset
91  UCB1CTL0 |= (UCMST | UCMODE_3 | UCSYNC); // I2C Master, synchronous mode
92  UCB1CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
93  UCB1BR0 = I2C_PRESC_400KHZ_LSB; // prescaler for 400 kHz data rate
94  UCB1BR1 = I2C_PRESC_400KHZ_MSB;
95  UCB1I2CSA = slave_address; // Set slave address
96 
97  UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
98  UCB1I2CIE = UCNACKIE;
99  UC1IE = UCB1TXIE; // Enable TX ready interrupt
100 }
101 
102 //------------------------------------------------------------------------------
103 // void i2c_receive_n(unsigned char byte_ctr, unsigned char * rx_buf)
104 // This function is used to start an I2C communication in master-receiver mode WITHOUT INTERRUPTS
105 // for more than 1 byte
106 // IN: unsigned char byte_ctr => number of bytes to be read
107 // OUT: unsigned char rx_buf => receive data buffer
108 // OUT: int n_received => number of bytes read
109 //------------------------------------------------------------------------------
110 static volatile u8_t rx_byte_tot = 0;
111 u8_t
112 i2c_receive_n(u8_t byte_ctr, u8_t *rx_buf) {
113 
114  rx_byte_tot = byte_ctr;
115  rx_byte_ctr = byte_ctr;
116  rx_buf_ptr = rx_buf;
117 
118  while ((UCB1CTL1 & UCTXSTT) || (UCB1STAT & UCNACKIFG)) // Slave acks address or not?
119  PRINTFDEBUG ("____ UCTXSTT not clear OR NACK received\n");
120 
121 #if I2C_RX_WITH_INTERRUPT
122  PRINTFDEBUG(" RX Interrupts: YES \n");
123 
124  // SPECIAL-CASE: Stop condition must be sent while receiving the 1st byte for 1-byte only read operations
125  if(rx_byte_tot == 1){ // See page 537 of slau144e.pdf
126  dint();
127  UCB1CTL1 |= UCTXSTT; // I2C start condition
128  while(UCB1CTL1 & UCTXSTT) // Waiting for Start bit to clear
129  PRINTFDEBUG ("____ STT clear wait\n");
130  UCB1CTL1 |= UCTXSTP; // I2C stop condition
131  eint();
132  }
133  else{ // all other cases
134  UCB1CTL1 |= UCTXSTT; // I2C start condition
135  }
136  return 0;
137 
138 #else
139  u8_t n_received = 0;
140 
141  PRINTFDEBUG(" RX Interrupts: NO \n");
142 
143  UCB1CTL1 |= UCTXSTT; // I2C start condition
144 
145  while (rx_byte_ctr > 0){
146  if (UC1IFG & UCB1RXIFG) { // Waiting for Data
147  rx_buf[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
148  rx_byte_ctr--;
149  UC1IFG &= ~UCB1RXIFG; // Clear USCI_B1 RX int flag
150  n_received++;
151  }
152  }
153  UCB1CTL1 |= UCTXSTP; // I2C stop condition
154  return n_received;
155 #endif
156 }
157 
158 
159 //------------------------------------------------------------------------------
160 // u8_t i2c_busy()
161 //
162 // This function is used to check if there is communication in progress.
163 //
164 // OUT: unsigned char => 0: I2C bus is idle,
165 // 1: communication is in progress
166 //------------------------------------------------------------------------------
167 u8_t
168 i2c_busy(void) {
169  return (UCB1STAT & UCBBUSY);
170 }
171 
172 /*----------------------------------------------------------------------------*/
173 /* Setup ports and pins for I2C use. */
174 
175 void
176 i2c_enable(void) {
177  I2C_PxSEL |= (I2C_SDA | I2C_SCL); // Secondary function (USCI) selected
178  I2C_PxSEL2 |= (I2C_SDA | I2C_SCL); // Secondary function (USCI) selected
179  I2C_PxDIR |= I2C_SCL; // SCL is output (not needed?)
180  I2C_PxDIR &= ~I2C_SDA; // SDA is input (not needed?)
181  I2C_PxREN |= (I2C_SDA | I2C_SCL); // Activate internal pull-up/-down resistors
182  I2C_PxOUT |= (I2C_SDA | I2C_SCL); // Select pull-up resistors
183 }
184 
185 /*----------------------------------------------------------------------------*/
186 //------------------------------------------------------------------------------
187 // void i2c_transmit_n(unsigned char byte_ctr, unsigned char *field)
188 //
189 // This function is used to start an I2C communication in master-transmit mode.
190 //
191 // IN: unsigned char byte_ctr => number of bytes to be transmitted
192 // unsigned char *tx_buf => Content to transmit. Read and transmitted from [0] to [byte_ctr]
193 //------------------------------------------------------------------------------
194 static volatile u8_t tx_byte_tot = 0;
195 void
196 i2c_transmit_n(u8_t byte_ctr, u8_t *tx_buf) {
197  tx_byte_tot = byte_ctr;
198  tx_byte_ctr = byte_ctr;
199  tx_buf_ptr = tx_buf;
200  UCB1CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
201 }
202 
203 /*----------------------------------------------------------------------------*/
204 #ifdef __IAR_SYSTEMS_ICC__
205 #pragma vector=USCIAB1TX_VECTOR
206 __interrupt void
207 #else
208 interrupt (USCIAB1TX_VECTOR)
209 #endif
210 i2c_tx_interrupt (void) {
211  // TX Part
212  if (UC1IFG & UCB1TXIFG) { // TX int. condition
213  if (tx_byte_ctr == 0) {
214  UCB1CTL1 |= UCTXSTP; // I2C stop condition
215  UC1IFG &= ~UCB1TXIFG; // Clear USCI_B1 TX int flag
216  }
217  else {
218  UCB1TXBUF = tx_buf_ptr[tx_byte_tot - tx_byte_ctr];
219  tx_byte_ctr--;
220  }
221  }
222  // RX Part
223 #if I2C_RX_WITH_INTERRUPT
224  else if (UC1IFG & UCB1RXIFG){ // RX int. condition
225  if (rx_byte_ctr == 0){
226  // Only for 1-byte transmissions, STOP is handled in receive_n_int
227  if (rx_byte_tot != 1)
228  UCB1CTL1 |= UCTXSTP; // I2C stop condition
229 
230  UC1IFG &= ~UCB1RXIFG; // Clear USCI_B1 RX int flag. XXX Just in case, check if necessary
231  }
232  else {
233  rx_buf_ptr[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
234  rx_byte_ctr--;
235  }
236  }
237 #endif
238 }
239 
240 #ifdef __IAR_SYSTEMS_ICC__
241 #pragma vector=USCIAB1RX_VECTOR
242 __interrupt void
243 #else
244 interrupt (USCIAB1RX_VECTOR)
245 #endif
246 i2c_rx_interrupt(void) {
247  if (UCB1STAT & UCNACKIFG){
248  PRINTFDEBUG("!!! NACK received in RX\n");
249  UCB1CTL1 |= UCTXSTP;
250  UCB1STAT &= ~UCNACKIFG;
251  }
252 }
253 
254