Contiki 2.5
i2c.c
1 /*
2  * Copyright (c) 2011, Hedde Bosman <heddebosman@incas3.eu>
3  *
4  * I2C communication device drivers for mc1322x
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id$
31  */
32 
33 #include "i2c.h"
34 
35 #include <stdio.h>
36 
37 static int8_t tx_byte_ctr;
38 static int8_t rx_byte_ctr;
39 
40 static uint8_t* tx_buf_ptr;
41 static uint8_t* rx_buf_ptr;
42 
43 
44 volatile unsigned int i; // volatile to prevent optimization
45 
46 static inline void i2c_send_byte(void) {
47  *I2CDR = *(tx_buf_ptr++); // set new byte, MCF is automatically cleared
48  tx_byte_ctr--;
49 }
50 static inline void i2c_recv_byte(void) {
51  *(rx_buf_ptr++) = *I2CDR;
52  rx_byte_ctr--;
53 }
54 
55 //------------------------------------------------------------------------------
56 // void i2c_receiveinit(uint8_t slave_address,
57 // uint8_t prescale)
58 //
59 // This function initializes the USCI module for master-receive operation.
60 //
61 // IN: uint8_t slave_address => Slave Address
62 // uint8_t prescale => SCL clock adjustment
63 //-----------------------------------------------------------------------------
64 //static volatile uint8_t rx_byte_tot = 0;
65 void i2c_receiveinit(uint8_t slave_address, uint8_t byte_ctr, uint8_t *rx_buf) {
66  // wait for bus to be free before setting MSTA (assuming we're in a multi-master environment or still sending something else)
67  while(i2c_busy()) /* wait */;
68 
69  // assert: rx_byte_ctr <= 0
70  // assert: tx_byte_ctr <= 0
71  tx_buf_ptr = 0;
72  tx_byte_ctr = 0; // indicate that nothing is to be received
73  rx_byte_ctr = byte_ctr;
74  rx_buf_ptr = rx_buf;
75 
76  // clockdiv
77  //*I2CFDR = 0x20; // 150 khz for redbee econotag
78 
79  // assume being master, thus no addres has to be set
80  *I2CCR = I2C_MEN |
81 #ifdef I2C_NON_BLOCKING
82  I2C_MIEN |
83 #endif
84  I2C_MSTA | I2C_MTX | I2C_RXAK; // start condition is triggered
85 
86  // write out address of slave
87  *I2CDR = (slave_address & 0x7f) <<1 | 0x01;
88 
89 #ifndef I2C_NON_BLOCKING
90  i2c_receive();
91 #endif
92 }
93 
94 //------------------------------------------------------------------------------
95 // void i2c_transmitinit(uint8_t slave_address,
96 // uint8_t prescale)
97 //
98 // Initializes USCI for master-transmit operation.
99 //
100 // IN: uint8_t slave_address => Slave Address
101 // uint8_t prescale => SCL clock adjustment
102 //------------------------------------------------------------------------------
103 //static volatile uint8_t tx_byte_tot = 0;
104 void i2c_transmitinit(uint8_t slave_address, uint8_t byte_ctr, uint8_t *tx_buf) {
105  // wait for bus to be free before setting MSTA (assuming we're in a multi-master environment or still sending something else)
106  while(i2c_busy()) /* wait */;
107 
108  // assert: rx_byte_ctr <= 0
109  // assert: tx_byte_ctr <= 0
110  rx_buf_ptr = 0;
111  rx_byte_ctr = 0; // indicate that nothing is to be received
112  tx_byte_ctr = byte_ctr;
113  tx_buf_ptr = tx_buf;
114 
115  // clockdiv
116  //*I2CFDR = 0x20; // 150 khz for redbee econotag
117 
118  // assume being master, thus no addres has to be set
119  *I2CCR = I2C_MEN |
120 #ifdef I2C_NON_BLOCKING
121  I2C_MIEN |
122 #endif
123  I2C_MSTA | I2C_MTX ; // start condition is triggered
124 
125  // write out address of slave
126  *I2CDR = (slave_address & 0x7f) <<1;
127 
128 #ifndef I2C_NON_BLOCKING
129  i2c_transmit();
130 #endif
131 }
132 
133 /*----------------------------------------------------------------------------*/
134 /*- blocking counterparts of interrupt hanlder function ---------------------*/
135 /*----------------------------------------------------------------------------*/
136 #ifndef I2C_NON_BLOCKING
137 /*----------------------------------------------------------------------------*/
138 uint8_t i2c_receive() {
139  while(rx_byte_ctr > 0) {
140  // busy wait
141  while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;
142 
143  if (rx_byte_ctr == 1) { // receiving next-to-last byte, thus turn off auto-ack for stop condition
144  *I2CCR |= I2C_TXAK;
145  }
146 
147  if (*I2CSR & I2C_MCF) {
148  i2c_recv_byte(); // read new byte
149  }
150 
151  if (*I2CSR & I2C_MAL) {
152  *I2CSR &= ~I2C_MAL; // should be cleared in software
153  printf("*** ERROR I2C: Arbitration lost\n");
154  // Arbitration lost; ERROR?
155  }
156  }
157 
158  while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;
159  if (*I2CSR & I2C_RXAK) {
160  // NO acknoledge byte received
161  printf("*** ERROR I2C: No ack received\n");
162  }
163  if (*I2CSR & I2C_MAL) {
164  *I2CSR &= ~I2C_MAL; // should be cleared in software
165  printf("*** ERROR I2C: Arbitration lost\n");
166  // Arbitration lost; ERROR?
167  }
168 
169  *I2CCR &= ~I2C_MSTA; // stop condition
170 }
171 /*----------------------------------------------------------------------------*/
172 void i2c_transmit() {
173  while(tx_byte_ctr > 0) {
174  // busy wait
175  while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;
176 
177  if (*I2CSR & I2C_RXAK) {
178  // NO acknoledge byte received
179  printf("*** ERROR I2C: No ack received\n");
180  }
181 
182  if (*I2CSR & I2C_MCF) {
183  i2c_send_byte();
184  }
185 
186  if (*I2CSR & I2C_MAL) {
187  *I2CSR &= ~I2C_MAL; // should be cleared in software
188  printf("*** ERROR I2C: Arbitration lost\n");
189  // Arbitration lost; ERROR?
190  }
191 
192  // clear MIF
193  *I2CSR &= ~I2C_MIF;
194  }
195 
196  while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;
197  if (*I2CSR & I2C_RXAK) {
198  // NO acknoledge byte received
199  printf("*** ERROR I2C: No ack received\n");
200  }
201  if (*I2CSR & I2C_MAL) {
202  *I2CSR &= ~I2C_MAL; // should be cleared in software
203  printf("*** ERROR I2C: Arbitration lost\n");
204  // Arbitration lost; ERROR?
205  }
206 
207  *I2CCR &= ~I2C_MSTA; // stop condition
208 }
209 #endif
210 /*----------------------------------------------------------------------------*/
211 
212 
213 /*----------------------------------------------------------------------------*/
214 /* force SCL to become bus master when sda is still low (can occur after sys reset */
215 /*----------------------------------------------------------------------------*/
216 void i2c_force_reset(void) {
217  uint8_t tmp;
218  *I2CCR = 0x20;
219  *I2CCR = 0xA0;
220  tmp = *I2CDR;
221  // return to module state Master?
222 }
223 
224 
225 /*----------------------------------------------------------------------------*/
226 /*- check if we're still in the process of sending something ----------------*/
227 /*----------------------------------------------------------------------------*/
228 uint8_t i2c_transferred(void) {
229  return (!i2c_busy() && rx_byte_ctr == 0 && tx_byte_ctr == 0);
230 }
231 
232 
233 /*----------------------------------------------------------------------------*/
234 /* check if there is communication in progress on the i2c bus. */
235 /*----------------------------------------------------------------------------*/
236 uint8_t i2c_busy(void) {
237  return ((*I2CSR & I2C_MBB) > 0); // bit 5 high = busy
238 }
239 
240 
241 /*----------------------------------------------------------------------------*/
242 /* Setup ports and pins for I2C use. */
243 /*----------------------------------------------------------------------------*/
244 void i2c_enable(void) {
245  // enable clock signal to i2c module
246  *I2CCKER = I2C_CKEN; // enable
247 
248  enable_irq(I2C);
249 
250  *I2CFDR = 0x20; // clockdiv: 150 khz for redbee econotag
251  *I2CADR = 0x01; // our slave address (not used; we're master)
252  // then enable i2c module
253  *I2CCR |= I2C_MEN | // module-enable, auto-ack = on
254 #ifdef I2C_NON_BLOCKING
255  I2C_MIEN | //module-interrupt-enable
256 #endif
257  0;
258 
259  // then switch gpio pins to i2c
260  *GPIO_FUNC_SEL0 |= (0x01 << (I2C_SCL*2)) | (0x01 << (I2C_SDA*2)); // GPIO 12, 13 to i2c
261  // and enable pull-up resistors
262  *GPIO_PAD_PU_EN0 |= (0x01 << I2C_SCL) | (0x01 << I2C_SDA); // Activate internal pull-up/-down resistors
263  *GPIO_PAD_PU_SEL0 |= (0x01 << I2C_SCL) | (0x01 << I2C_SDA); // select pull-up resistors for ports
264 }
265 
266 /*----------------------------------------------------------------------------*/
267 /* Reset ports and pins for GPIO use. */
268 /*----------------------------------------------------------------------------*/
269 void i2c_disable(void) {
270  // all control values are set off
271  *I2CCR = 0;
272  // clock is turned off
273  *I2CCKER = ~I2C_CKEN;
274 
275  // then switch gpio pins to gpio
276  *GPIO_FUNC_SEL0 &= ~(0x01 << (I2C_SCL*2)) | (0x01 << (I2C_SDA*2)); // GPIO 12, 13 to i2c
277  // and disable resistors
278  *GPIO_PAD_PU_EN0 &= ~(0x01 << I2C_SCL) | (0x01 << I2C_SDA); // Deactivate internal pull-up/-down resistors
279 
280  disable_irq(I2C);
281 }
282 
283 
284 /*----------------------------------------------------------------------------*/
285 /*- i2c interrupt handler --------------------------------------------------*/
286 /*----------------------------------------------------------------------------*/
287 #ifdef I2C_NON_BLOCKING
288 void i2c_isr (void) {
289  uint8_t dummy;
290  if (*I2CSR & I2C_MIF) { // interrupt is from i2c
291  if (*I2CSR & I2C_MCF) { // one byte transferred/received. will be cleared automatically when I2CDR is written or I2CSR read
292  if (tx_buf_ptr != 0) { // we're sending
293  if (*I2CSR & I2C_RXAK) {
294  // NO acknoledge byte received
295  printf("*** ERROR I2C: No ack received\n");
296  }
297 
298  if (tx_byte_ctr > 0) { // tx
299  i2c_send_byte(); // set new byte, MCF is automatically cleared
300  } else {
301  *I2CCR &= ~I2C_MSTA; // generate stop condition
302  }
303  } else { //if (rx_buf_ptr != 0) { // receive
304  if (rx_byte_ctr == 1) { // receiving next-to-last byte, thus turn off auto-ack for stop condition
305  *I2CCR |= I2C_TXAK;
306  }
307  if (*I2CCR & I2C_MTX) { // address byte was just sent
308  *I2CCR &= ~I2C_MTX; // switch to receive mode
309  dummy = *I2CDR; // dummy read to throw away the address from register
310 
311  } else if (rx_byte_ctr > 0) {
312  i2c_recv_byte(); // read new byte
313  } else {
314  *I2CCR &= ~I2C_MSTA; // generate stop condition
315  }
316 
317  }
318  }
319  if (*I2CSR & I2C_MAL) {
320  *I2CSR &= ~I2C_MAL; // should be cleared in software
321  printf("*** ERROR I2C: Arbitration lost\n");
322  // Arbitration lost; reset..
323  rx_byte_ctr = tx_byte_ctr = 0;
324  *I2CCR &= ~I2C_MSTA; // generate stop condition
325  }
326 
327  // clear MIF
328  *I2CSR &= ~I2C_MIF;
329  }
330 }
331 #endif