i2cmaster.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  */
00032 
00033 /**
00034  * \file
00035  *         I2C communication device drivers for Zolertia Z1 sensor node.
00036  * \author
00037  *         Enric M. Calvo, Zolertia <ecalvo@zolertia.com>
00038  *         Marcus Lundén, SICS <mlunden@sics.se>
00039  */
00040 
00041 #include "i2cmaster.h"
00042 
00043 signed   char tx_byte_ctr, rx_byte_ctr;
00044 unsigned char rx_buf[2];
00045 unsigned char* tx_buf_ptr;
00046 unsigned char* rx_buf_ptr;
00047 unsigned char receive_data;
00048 unsigned char transmit_data1;
00049 unsigned char transmit_data2;
00050 volatile unsigned int i;        // volatile to prevent optimization
00051 
00052 //------------------------------------------------------------------------------
00053 // void i2c_receiveinit(unsigned char slave_address, 
00054 //                              unsigned char prescale)
00055 //
00056 // This function initializes the USCI module for master-receive operation. 
00057 //
00058 // IN:   unsigned char slave_address   =>  Slave Address
00059 //       unsigned char prescale        =>  SCL clock adjustment 
00060 //-----------------------------------------------------------------------------
00061 void
00062 i2c_receiveinit(u8_t slave_address) {
00063   UCB1CTL1 = UCSWRST;                    // Enable SW reset
00064   UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC;  // I2C Master, synchronous mode
00065   UCB1CTL1 = UCSSEL_2 | UCSWRST;         // Use SMCLK, keep SW reset
00066   UCB1BR0  = I2C_PRESC_400KHZ_LSB;       // prescaler for 400 kHz data rate
00067   UCB1BR1  = I2C_PRESC_400KHZ_MSB;
00068   UCB1I2CSA = slave_address;             // set slave address
00069 
00070   UCB1CTL1 &= ~UCTR;                     // I2C Receiver 
00071 
00072   UCB1CTL1 &= ~UCSWRST;                  // Clear SW reset, resume operation
00073   UCB1I2CIE = UCNACKIE;
00074 #if I2C_RX_WITH_INTERRUPT
00075   UC1IE = UCB1RXIE;                      // Enable RX interrupt if desired
00076 #endif
00077 }
00078 
00079 //------------------------------------------------------------------------------
00080 // void i2c_transmitinit(unsigned char slave_address, 
00081 //                               unsigned char prescale)
00082 //
00083 // Initializes USCI for master-transmit operation. 
00084 //
00085 // IN:   unsigned char slave_address   =>  Slave Address
00086 //       unsigned char prescale        =>  SCL clock adjustment 
00087 //------------------------------------------------------------------------------
00088 void
00089 i2c_transmitinit(u8_t slave_address) {
00090   UCB1CTL1 |= UCSWRST;                     // Enable SW reset
00091   UCB1CTL0 |= (UCMST | UCMODE_3 | UCSYNC); // I2C Master, synchronous mode
00092   UCB1CTL1  = UCSSEL_2 + UCSWRST;          // Use SMCLK, keep SW reset
00093   UCB1BR0   = I2C_PRESC_400KHZ_LSB;        // prescaler for 400 kHz data rate
00094   UCB1BR1   = I2C_PRESC_400KHZ_MSB;
00095   UCB1I2CSA = slave_address;               // Set slave address
00096 
00097   UCB1CTL1 &= ~UCSWRST;                    // Clear SW reset, resume operation
00098   UCB1I2CIE = UCNACKIE;
00099   UC1IE = UCB1TXIE;                        // Enable TX ready interrupt
00100 }
00101 
00102 //------------------------------------------------------------------------------
00103 // void i2c_receive_n(unsigned char byte_ctr, unsigned char * rx_buf)
00104 // This function is used to start an I2C communication in master-receiver mode WITHOUT INTERRUPTS
00105 // for more than 1 byte
00106 // IN:   unsigned char byte_ctr   =>  number of bytes to be read
00107 // OUT:  unsigned char rx_buf     =>  receive data buffer
00108 // OUT:  int n_received           =>  number of bytes read
00109 //------------------------------------------------------------------------------
00110 static volatile u8_t rx_byte_tot = 0;
00111 u8_t
00112 i2c_receive_n(u8_t byte_ctr, u8_t *rx_buf) {
00113 
00114   rx_byte_tot = byte_ctr;
00115   rx_byte_ctr = byte_ctr;
00116   rx_buf_ptr  = rx_buf;
00117 
00118   while ((UCB1CTL1 & UCTXSTT) || (UCB1STAT & UCNACKIFG))        // Slave acks address or not?
00119     PRINTFDEBUG ("____ UCTXSTT not clear OR NACK received\n");
00120 
00121 #if I2C_RX_WITH_INTERRUPT
00122   PRINTFDEBUG(" RX Interrupts: YES \n");
00123 
00124   // SPECIAL-CASE: Stop condition must be sent while receiving the 1st byte for 1-byte only read operations
00125   if(rx_byte_tot == 1){                 // See page 537 of slau144e.pdf
00126     dint();
00127     UCB1CTL1 |= UCTXSTT;                // I2C start condition
00128     while(UCB1CTL1 & UCTXSTT)           // Waiting for Start bit to clear
00129       PRINTFDEBUG ("____ STT clear wait\n");
00130     UCB1CTL1 |= UCTXSTP;                // I2C stop condition
00131     eint();
00132   }
00133   else{                                 // all other cases
00134     UCB1CTL1 |= UCTXSTT;                // I2C start condition
00135   }
00136   return 0;
00137 
00138 #else
00139   u8_t n_received = 0;
00140 
00141   PRINTFDEBUG(" RX Interrupts: NO \n");
00142 
00143   UCB1CTL1 |= UCTXSTT;          // I2C start condition
00144 
00145   while (rx_byte_ctr > 0){
00146     if (UC1IFG & UCB1RXIFG) {   // Waiting for Data
00147       rx_buf[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
00148       rx_byte_ctr--;
00149       UC1IFG &= ~UCB1RXIFG;     // Clear USCI_B1 RX int flag      
00150       n_received++;
00151     }
00152   }
00153   UCB1CTL1 |= UCTXSTP;          // I2C stop condition
00154   return n_received;
00155 #endif
00156 }
00157 
00158 
00159 //------------------------------------------------------------------------------
00160 // u8_t i2c_busy()
00161 //
00162 // This function is used to check if there is communication in progress. 
00163 //
00164 // OUT:  unsigned char  =>  0: I2C bus is idle, 
00165 //                          1: communication is in progress
00166 //------------------------------------------------------------------------------
00167 u8_t
00168 i2c_busy(void) {
00169   return (UCB1STAT & UCBBUSY);
00170 }
00171 
00172 /*----------------------------------------------------------------------------*/
00173 /* Setup ports and pins for I2C use. */
00174 
00175 void
00176 i2c_enable(void) {
00177   I2C_PxSEL |= (I2C_SDA | I2C_SCL);    // Secondary function (USCI) selected
00178   I2C_PxSEL2 |= (I2C_SDA | I2C_SCL);   // Secondary function (USCI) selected
00179   I2C_PxDIR |= I2C_SCL;                // SCL is output (not needed?)
00180   I2C_PxDIR &= ~I2C_SDA;               // SDA is input (not needed?)
00181   I2C_PxREN |= (I2C_SDA | I2C_SCL);    // Activate internal pull-up/-down resistors
00182   I2C_PxOUT |= (I2C_SDA | I2C_SCL);    // Select pull-up resistors
00183 }
00184 
00185 /*----------------------------------------------------------------------------*/
00186 //------------------------------------------------------------------------------
00187 // void i2c_transmit_n(unsigned char byte_ctr, unsigned char *field)
00188 //
00189 // This function is used to start an I2C communication in master-transmit mode. 
00190 //
00191 // IN:   unsigned char byte_ctr   =>  number of bytes to be transmitted
00192 //       unsigned char *tx_buf    =>  Content to transmit. Read and transmitted from [0] to [byte_ctr]
00193 //------------------------------------------------------------------------------
00194 static volatile u8_t tx_byte_tot = 0;
00195 void
00196 i2c_transmit_n(u8_t byte_ctr, u8_t *tx_buf) {
00197   tx_byte_tot = byte_ctr;
00198   tx_byte_ctr = byte_ctr;
00199   tx_buf_ptr  = tx_buf;
00200   UCB1CTL1 |= UCTR + UCTXSTT;      // I2C TX, start condition
00201 }
00202 
00203 /*----------------------------------------------------------------------------*/
00204 interrupt (USCIAB1TX_VECTOR)   
00205 i2c_tx_interrupt (void) {
00206   // TX Part
00207   if (UC1IFG & UCB1TXIFG) {        // TX int. condition
00208     if (tx_byte_ctr == 0) {
00209       UCB1CTL1 |= UCTXSTP;         // I2C stop condition
00210       UC1IFG &= ~UCB1TXIFG;        // Clear USCI_B1 TX int flag
00211     }
00212     else {
00213       UCB1TXBUF = tx_buf_ptr[tx_byte_tot - tx_byte_ctr];
00214       tx_byte_ctr--;
00215     }
00216   }
00217   // RX Part
00218 #if I2C_RX_WITH_INTERRUPT
00219   else if (UC1IFG & UCB1RXIFG){    // RX int. condition
00220     if (rx_byte_ctr == 0){
00221       // Only for 1-byte transmissions, STOP is handled in receive_n_int
00222       if (rx_byte_tot != 1) 
00223         UCB1CTL1 |= UCTXSTP;       // I2C stop condition
00224 
00225       UC1IFG &= ~UCB1RXIFG;        // Clear USCI_B1 RX int flag. XXX Just in case, check if necessary
00226     }
00227     else {
00228       rx_buf_ptr[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
00229       rx_byte_ctr--;
00230     }
00231   }
00232 #endif
00233 }
00234 
00235 interrupt(USCIAB1RX_VECTOR)
00236 i2c_rx_interrupt(void) {
00237   if (UCB1STAT & UCNACKIFG){
00238     PRINTFDEBUG("!!! NACK received in RX\n");
00239     UCB1CTL1 |= UCTXSTP;
00240     UCB1STAT &= ~UCNACKIFG;
00241   }
00242 }
00243 
00244 

Generated on Mon Apr 11 14:23:45 2011 for Contiki 2.5 by  doxygen 1.6.1