Contiki 3.x
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  */
31 
32 #include "i2c.h"
33 
34 #include <stdio.h>
35 
36 static int8_t tx_byte_ctr;
37 static int8_t rx_byte_ctr;
38 
39 static uint8_t* tx_buf_ptr;
40 static uint8_t* rx_buf_ptr;
41 
42 
43 volatile unsigned int i; // volatile to prevent optimization
44 
45 static inline void i2c_send_byte(void) {
46  *I2CDR = *(tx_buf_ptr++); // set new byte, MCF is automatically cleared
47  tx_byte_ctr--;
48 }
49 static inline void i2c_recv_byte(void) {
50  *(rx_buf_ptr++) = *I2CDR;
51  rx_byte_ctr--;
52 }
53 
54 //------------------------------------------------------------------------------
55 // void i2c_receiveinit(uint8_t slave_address,
56 // uint8_t prescale)
57 //
58 // This function initializes the USCI module for master-receive operation.
59 //
60 // IN: uint8_t slave_address => Slave Address
61 // uint8_t prescale => SCL clock adjustment
62 //-----------------------------------------------------------------------------
63 //static volatile uint8_t rx_byte_tot = 0;
64 void i2c_receiveinit(uint8_t slave_address, uint8_t byte_ctr, uint8_t *rx_buf) {
65  // wait for bus to be free before setting MSTA (assuming we're in a multi-master environment or still sending something else)
66  while(i2c_busy()) /* wait */;
67 
68  // assert: rx_byte_ctr <= 0
69  // assert: tx_byte_ctr <= 0
70  tx_buf_ptr = 0;
71  tx_byte_ctr = 0; // indicate that nothing is to be received
72  rx_byte_ctr = byte_ctr;
73  rx_buf_ptr = rx_buf;
74 
75  // clockdiv
76  //*I2CFDR = 0x20; // 150 khz for redbee econotag
77 
78  // assume being master, thus no addres has to be set
79  *I2CCR = I2C_MEN |
80 #ifdef I2C_NON_BLOCKING
81  I2C_MIEN |
82 #endif
83  I2C_MSTA | I2C_MTX | I2C_RXAK; // start condition is triggered
84 
85  // write out address of slave
86  *I2CDR = (slave_address & 0x7f) <<1 | 0x01;
87 
88 #ifndef I2C_NON_BLOCKING
89  i2c_receive();
90 #endif
91 }
92 
93 //------------------------------------------------------------------------------
94 // void i2c_transmitinit(uint8_t slave_address,
95 // uint8_t prescale)
96 //
97 // Initializes USCI for master-transmit operation.
98 //
99 // IN: uint8_t slave_address => Slave Address
100 // uint8_t prescale => SCL clock adjustment
101 //------------------------------------------------------------------------------
102 //static volatile uint8_t tx_byte_tot = 0;
103 void i2c_transmitinit(uint8_t slave_address, uint8_t byte_ctr, uint8_t *tx_buf) {
104  // wait for bus to be free before setting MSTA (assuming we're in a multi-master environment or still sending something else)
105  while(i2c_busy()) /* wait */;
106 
107  // assert: rx_byte_ctr <= 0
108  // assert: tx_byte_ctr <= 0
109  rx_buf_ptr = 0;
110  rx_byte_ctr = 0; // indicate that nothing is to be received
111  tx_byte_ctr = byte_ctr;
112  tx_buf_ptr = tx_buf;
113 
114  // clockdiv
115  //*I2CFDR = 0x20; // 150 khz for redbee econotag
116 
117  // assume being master, thus no addres has to be set
118  *I2CCR = I2C_MEN |
119 #ifdef I2C_NON_BLOCKING
120  I2C_MIEN |
121 #endif
122  I2C_MSTA | I2C_MTX ; // start condition is triggered
123 
124  // write out address of slave
125  *I2CDR = (slave_address & 0x7f) <<1;
126 
127 #ifndef I2C_NON_BLOCKING
128  i2c_transmit();
129 #endif
130 }
131 
132 /*----------------------------------------------------------------------------*/
133 /*- blocking counterparts of interrupt hanlder function ---------------------*/
134 /*----------------------------------------------------------------------------*/
135 #ifndef I2C_NON_BLOCKING
136 /*----------------------------------------------------------------------------*/
137 uint8_t i2c_receive() {
138  while(rx_byte_ctr > 0) {
139  // busy wait
140  while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;
141 
142  if (rx_byte_ctr == 1) { // receiving next-to-last byte, thus turn off auto-ack for stop condition
143  *I2CCR |= I2C_TXAK;
144  }
145 
146  if (*I2CSR & I2C_MCF) {
147  i2c_recv_byte(); // read new byte
148  }
149 
150  if (*I2CSR & I2C_MAL) {
151  *I2CSR &= ~I2C_MAL; // should be cleared in software
152  printf("*** ERROR I2C: Arbitration lost\n");
153  // Arbitration lost; ERROR?
154  }
155  }
156 
157  while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;
158  if (*I2CSR & I2C_RXAK) {
159  // NO acknoledge byte received
160  printf("*** ERROR I2C: No ack received\n");
161  }
162  if (*I2CSR & I2C_MAL) {
163  *I2CSR &= ~I2C_MAL; // should be cleared in software
164  printf("*** ERROR I2C: Arbitration lost\n");
165  // Arbitration lost; ERROR?
166  }
167 
168  *I2CCR &= ~I2C_MSTA; // stop condition
169 }
170 /*----------------------------------------------------------------------------*/
171 void i2c_transmit() {
172  while(tx_byte_ctr > 0) {
173  // busy wait
174  while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;
175 
176  if (*I2CSR & I2C_RXAK) {
177  // NO acknoledge byte received
178  printf("*** ERROR I2C: No ack received\n");
179  }
180 
181  if (*I2CSR & I2C_MCF) {
182  i2c_send_byte();
183  }
184 
185  if (*I2CSR & I2C_MAL) {
186  *I2CSR &= ~I2C_MAL; // should be cleared in software
187  printf("*** ERROR I2C: Arbitration lost\n");
188  // Arbitration lost; ERROR?
189  }
190 
191  // clear MIF
192  *I2CSR &= ~I2C_MIF;
193  }
194 
195  while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/;
196  if (*I2CSR & I2C_RXAK) {
197  // NO acknoledge byte received
198  printf("*** ERROR I2C: No ack received\n");
199  }
200  if (*I2CSR & I2C_MAL) {
201  *I2CSR &= ~I2C_MAL; // should be cleared in software
202  printf("*** ERROR I2C: Arbitration lost\n");
203  // Arbitration lost; ERROR?
204  }
205 
206  *I2CCR &= ~I2C_MSTA; // stop condition
207 }
208 #endif
209 /*----------------------------------------------------------------------------*/
210 
211 
212 /*----------------------------------------------------------------------------*/
213 /* force SCL to become bus master when sda is still low (can occur after sys reset */
214 /*----------------------------------------------------------------------------*/
215 void i2c_force_reset(void) {
216  uint8_t tmp;
217  *I2CCR = 0x20;
218  *I2CCR = 0xA0;
219  tmp = *I2CDR;
220  // return to module state Master?
221 }
222 
223 
224 /*----------------------------------------------------------------------------*/
225 /*- check if we're still in the process of sending something ----------------*/
226 /*----------------------------------------------------------------------------*/
227 uint8_t i2c_transferred(void) {
228  return (!i2c_busy() && rx_byte_ctr == 0 && tx_byte_ctr == 0);
229 }
230 
231 
232 /*----------------------------------------------------------------------------*/
233 /* check if there is communication in progress on the i2c bus. */
234 /*----------------------------------------------------------------------------*/
235 uint8_t i2c_busy(void) {
236  return ((*I2CSR & I2C_MBB) > 0); // bit 5 high = busy
237 }
238 
239 
240 /*----------------------------------------------------------------------------*/
241 /* Setup ports and pins for I2C use. */
242 /*----------------------------------------------------------------------------*/
243 void i2c_enable(void) {
244  // enable clock signal to i2c module
245  *I2CCKER = I2C_CKEN; // enable
246 
247  enable_irq(I2C);
248 
249  *I2CFDR = 0x20; // clockdiv: 150 khz for redbee econotag
250  *I2CADR = 0x01; // our slave address (not used; we're master)
251  // then enable i2c module
252  *I2CCR |= I2C_MEN | // module-enable, auto-ack = on
253 #ifdef I2C_NON_BLOCKING
254  I2C_MIEN | //module-interrupt-enable
255 #endif
256  0;
257 
258  // then switch gpio pins to i2c
259  *GPIO_FUNC_SEL0 |= (0x01 << (I2C_SCL*2)) | (0x01 << (I2C_SDA*2)); // GPIO 12, 13 to i2c
260  // and enable pull-up resistors
261  *GPIO_PAD_PU_EN0 |= (0x01 << I2C_SCL) | (0x01 << I2C_SDA); // Activate internal pull-up/-down resistors
262  *GPIO_PAD_PU_SEL0 |= (0x01 << I2C_SCL) | (0x01 << I2C_SDA); // select pull-up resistors for ports
263 }
264 
265 /*----------------------------------------------------------------------------*/
266 /* Reset ports and pins for GPIO use. */
267 /*----------------------------------------------------------------------------*/
268 void i2c_disable(void) {
269  // all control values are set off
270  *I2CCR = 0;
271  // clock is turned off
272  *I2CCKER = ~I2C_CKEN;
273 
274  // then switch gpio pins to gpio
275  *GPIO_FUNC_SEL0 &= ~(0x01 << (I2C_SCL*2)) | (0x01 << (I2C_SDA*2)); // GPIO 12, 13 to i2c
276  // and disable resistors
277  *GPIO_PAD_PU_EN0 &= ~(0x01 << I2C_SCL) | (0x01 << I2C_SDA); // Deactivate internal pull-up/-down resistors
278 
279  disable_irq(I2C);
280 }
281 
282 
283 /*----------------------------------------------------------------------------*/
284 /*- i2c interrupt handler --------------------------------------------------*/
285 /*----------------------------------------------------------------------------*/
286 #ifdef I2C_NON_BLOCKING
287 void i2c_isr (void) {
288  uint8_t dummy;
289  if (*I2CSR & I2C_MIF) { // interrupt is from i2c
290  if (*I2CSR & I2C_MCF) { // one byte transferred/received. will be cleared automatically when I2CDR is written or I2CSR read
291  if (tx_buf_ptr != 0) { // we're sending
292  if (*I2CSR & I2C_RXAK) {
293  // NO acknoledge byte received
294  printf("*** ERROR I2C: No ack received\n");
295  }
296 
297  if (tx_byte_ctr > 0) { // tx
298  i2c_send_byte(); // set new byte, MCF is automatically cleared
299  } else {
300  *I2CCR &= ~I2C_MSTA; // generate stop condition
301  }
302  } else { //if (rx_buf_ptr != 0) { // receive
303  if (rx_byte_ctr == 1) { // receiving next-to-last byte, thus turn off auto-ack for stop condition
304  *I2CCR |= I2C_TXAK;
305  }
306  if (*I2CCR & I2C_MTX) { // address byte was just sent
307  *I2CCR &= ~I2C_MTX; // switch to receive mode
308  dummy = *I2CDR; // dummy read to throw away the address from register
309 
310  } else if (rx_byte_ctr > 0) {
311  i2c_recv_byte(); // read new byte
312  } else {
313  *I2CCR &= ~I2C_MSTA; // generate stop condition
314  }
315 
316  }
317  }
318  if (*I2CSR & I2C_MAL) {
319  *I2CSR &= ~I2C_MAL; // should be cleared in software
320  printf("*** ERROR I2C: Arbitration lost\n");
321  // Arbitration lost; reset..
322  rx_byte_ctr = tx_byte_ctr = 0;
323  *I2CCR &= ~I2C_MSTA; // generate stop condition
324  }
325 
326  // clear MIF
327  *I2CSR &= ~I2C_MIF;
328  }
329 }
330 #endif
void i2c_enable(void)
Configure serial controller in I2C mode and set I2C speed.
Definition: i2c.c:52
void i2c_disable(void)
Configure serial controller in disabled mode.
Definition: i2c.c:76