2010-11-07 08:38:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* I2C communication device drivers for Zolertia Z1 sensor node.
|
|
|
|
* \author
|
|
|
|
* Enric M. Calvo, Zolertia <ecalvo@zolertia.com>
|
|
|
|
* Marcus Lundén, SICS <mlunden@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "i2cmaster.h"
|
2012-03-07 00:14:54 +00:00
|
|
|
#include "isr_compat.h"
|
2010-11-07 08:38:51 +00:00
|
|
|
|
2014-10-20 12:21:06 +00:00
|
|
|
signed char tx_byte_ctr, rx_byte_ctr;
|
2010-11-07 08:38:51 +00:00
|
|
|
unsigned char rx_buf[2];
|
2014-10-20 12:21:06 +00:00
|
|
|
unsigned char *tx_buf_ptr;
|
|
|
|
unsigned char *rx_buf_ptr;
|
2010-11-07 08:38:51 +00:00
|
|
|
unsigned char receive_data;
|
|
|
|
unsigned char transmit_data1;
|
|
|
|
unsigned char transmit_data2;
|
2014-10-20 12:21:06 +00:00
|
|
|
unsigned char prescale_lsb = I2C_PRESC_Z1_LSB;
|
|
|
|
unsigned char prescale_msb = I2C_PRESC_Z1_MSB;
|
|
|
|
volatile unsigned int i; /* volatile to prevent optimization */
|
2010-11-07 08:38:51 +00:00
|
|
|
|
2014-10-20 12:21:06 +00:00
|
|
|
/* ------------------------------------------------------------------------------
|
|
|
|
* Change the data rate prior initializing transmission or reception
|
|
|
|
* ----------------------------------------------------------------------------- */
|
2010-11-07 08:38:51 +00:00
|
|
|
void
|
2014-10-20 12:21:06 +00:00
|
|
|
i2c_setrate(uint8_t p_lsb, uint8_t p_msb)
|
|
|
|
{
|
|
|
|
prescale_lsb = p_lsb;
|
|
|
|
prescale_lsb = p_msb;
|
|
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------------
|
|
|
|
* This function initializes the USCI module for master-receive operation.
|
|
|
|
* ----------------------------------------------------------------------------- */
|
|
|
|
void
|
|
|
|
i2c_receiveinit(uint8_t slave_address)
|
|
|
|
{
|
|
|
|
UCB1CTL1 = UCSWRST; /* Enable SW reset */
|
|
|
|
UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; /* I2C Master, synchronous mode */
|
|
|
|
UCB1CTL1 = UCSSEL_2 | UCSWRST; /* Use SMCLK, keep SW reset */
|
|
|
|
UCB1BR0 = prescale_lsb; /* prescaler (default 400 kHz) */
|
|
|
|
UCB1BR1 = prescale_msb;
|
|
|
|
UCB1I2CSA = slave_address; /* set slave address */
|
|
|
|
UCB1CTL1 &= ~UCTR; /* I2C Receiver */
|
|
|
|
UCB1CTL1 &= ~UCSWRST; /* Clear SW reset, resume operation */
|
2010-11-07 08:38:51 +00:00
|
|
|
UCB1I2CIE = UCNACKIE;
|
|
|
|
#if I2C_RX_WITH_INTERRUPT
|
2014-10-20 12:21:06 +00:00
|
|
|
UC1IE = UCB1RXIE; /* Enable RX interrupt if desired */
|
2010-11-07 08:38:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
2014-10-20 12:21:06 +00:00
|
|
|
/* ------------------------------------------------------------------------------
|
|
|
|
* Initializes USCI for master-transmit operation.
|
|
|
|
* ------------------------------------------------------------------------------ */
|
2010-11-07 08:38:51 +00:00
|
|
|
void
|
2014-10-20 12:21:06 +00:00
|
|
|
i2c_transmitinit(uint8_t slave_address)
|
|
|
|
{
|
|
|
|
UCB1CTL1 |= UCSWRST; /* Enable SW reset */
|
|
|
|
UCB1CTL0 |= (UCMST | UCMODE_3 | UCSYNC); /* I2C Master, synchronous mode */
|
|
|
|
UCB1CTL1 = UCSSEL_2 + UCSWRST; /* Use SMCLK, keep SW reset */
|
|
|
|
UCB1BR0 = prescale_lsb; /* prescaler (default 400 kHz) */
|
|
|
|
UCB1BR1 = prescale_msb;
|
|
|
|
UCB1I2CSA = slave_address; /* Set slave address */
|
|
|
|
UCB1CTL1 &= ~UCSWRST; /* Clear SW reset, resume operation */
|
2010-11-07 08:38:51 +00:00
|
|
|
UCB1I2CIE = UCNACKIE;
|
2014-10-20 12:21:06 +00:00
|
|
|
UC1IE = UCB1TXIE; /* Enable TX ready interrupt */
|
2010-11-07 08:38:51 +00:00
|
|
|
}
|
2014-10-20 12:21:06 +00:00
|
|
|
/* ------------------------------------------------------------------------------
|
|
|
|
* This function is used to start an I2C communication in master-receiver mode WITHOUT INTERRUPTS
|
|
|
|
* for more than 1 byte
|
|
|
|
* ------------------------------------------------------------------------------ */
|
2012-02-20 19:32:05 +00:00
|
|
|
static volatile uint8_t rx_byte_tot = 0;
|
|
|
|
uint8_t
|
2014-10-20 12:21:06 +00:00
|
|
|
i2c_receive_n(uint8_t byte_ctr, uint8_t *rx_buf)
|
|
|
|
{
|
2010-11-07 08:38:51 +00:00
|
|
|
|
|
|
|
rx_byte_tot = byte_ctr;
|
|
|
|
rx_byte_ctr = byte_ctr;
|
2014-10-20 12:21:06 +00:00
|
|
|
rx_buf_ptr = rx_buf;
|
2010-11-07 08:38:51 +00:00
|
|
|
|
2014-10-20 12:21:06 +00:00
|
|
|
while((UCB1CTL1 & UCTXSTT) || (UCB1STAT & UCNACKIFG)) /* Slave acks address or not? */
|
|
|
|
PRINTFDEBUG("____ UCTXSTT not clear OR NACK received\n");
|
2010-11-07 08:38:51 +00:00
|
|
|
|
|
|
|
#if I2C_RX_WITH_INTERRUPT
|
|
|
|
PRINTFDEBUG(" RX Interrupts: YES \n");
|
|
|
|
|
2014-10-20 12:21:06 +00:00
|
|
|
/* SPECIAL-CASE: Stop condition must be sent while receiving the 1st byte for 1-byte only read operations */
|
|
|
|
if(rx_byte_tot == 1) { /* See page 537 of slau144e.pdf */
|
2010-11-07 08:38:51 +00:00
|
|
|
dint();
|
2014-10-20 12:21:06 +00:00
|
|
|
UCB1CTL1 |= UCTXSTT; /* I2C start condition */
|
|
|
|
while(UCB1CTL1 & UCTXSTT) /* Waiting for Start bit to clear */
|
|
|
|
PRINTFDEBUG("____ STT clear wait\n");
|
|
|
|
UCB1CTL1 |= UCTXSTP; /* I2C stop condition */
|
2010-11-07 08:38:51 +00:00
|
|
|
eint();
|
2014-10-20 12:21:06 +00:00
|
|
|
} else { /* all other cases */
|
|
|
|
UCB1CTL1 |= UCTXSTT; /* I2C start condition */
|
2010-11-07 08:38:51 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
2012-02-20 19:32:05 +00:00
|
|
|
uint8_t n_received = 0;
|
2010-11-07 08:38:51 +00:00
|
|
|
|
|
|
|
PRINTFDEBUG(" RX Interrupts: NO \n");
|
|
|
|
|
2014-10-20 12:21:06 +00:00
|
|
|
UCB1CTL1 |= UCTXSTT; /* I2C start condition */
|
2010-11-07 08:38:51 +00:00
|
|
|
|
2014-10-20 12:21:06 +00:00
|
|
|
while(rx_byte_ctr > 0) {
|
|
|
|
if(UC1IFG & UCB1RXIFG) { /* Waiting for Data */
|
2010-11-07 08:38:51 +00:00
|
|
|
rx_buf[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
|
|
|
|
rx_byte_ctr--;
|
2014-10-20 12:21:06 +00:00
|
|
|
UC1IFG &= ~UCB1RXIFG; /* Clear USCI_B1 RX int flag */
|
2010-11-07 08:38:51 +00:00
|
|
|
n_received++;
|
|
|
|
}
|
|
|
|
}
|
2014-10-20 12:21:06 +00:00
|
|
|
UCB1CTL1 |= UCTXSTP; /* I2C stop condition */
|
2010-11-07 08:38:51 +00:00
|
|
|
return n_received;
|
|
|
|
#endif
|
|
|
|
}
|
2014-10-20 12:21:06 +00:00
|
|
|
/* ------------------------------------------------------------------------------
|
|
|
|
* This function is used to check if there is communication in progress.
|
|
|
|
* ------------------------------------------------------------------------------ */
|
2012-02-20 19:32:05 +00:00
|
|
|
uint8_t
|
2014-10-20 12:21:06 +00:00
|
|
|
i2c_busy(void)
|
|
|
|
{
|
|
|
|
return UCB1STAT & UCBBUSY;
|
2010-11-07 08:38:51 +00:00
|
|
|
}
|
2014-10-20 12:21:06 +00:00
|
|
|
/*----------------------------------------------------------------------------
|
|
|
|
* Setup ports and pins for I2C use.
|
|
|
|
* ------------------------------------------------------------------------------ */
|
2010-11-07 08:38:51 +00:00
|
|
|
|
|
|
|
void
|
2014-10-20 12:21:06 +00:00
|
|
|
i2c_enable(void)
|
|
|
|
{
|
|
|
|
I2C_PxSEL |= (I2C_SDA | I2C_SCL); /* Secondary function (USCI) selected */
|
|
|
|
I2C_PxSEL2 |= (I2C_SDA | I2C_SCL); /* Secondary function (USCI) selected */
|
|
|
|
I2C_PxDIR |= I2C_SCL; /* SCL is output (not needed?) */
|
|
|
|
I2C_PxDIR &= ~I2C_SDA; /* SDA is input (not needed?) */
|
|
|
|
I2C_PxREN |= (I2C_SDA | I2C_SCL); /* Activate internal pull-up/-down resistors */
|
|
|
|
I2C_PxOUT |= (I2C_SDA | I2C_SCL); /* Select pull-up resistors */
|
2010-11-07 08:38:51 +00:00
|
|
|
}
|
2012-05-09 08:54:21 +00:00
|
|
|
void
|
2014-10-20 12:21:06 +00:00
|
|
|
i2c_disable(void)
|
|
|
|
{
|
|
|
|
I2C_PxSEL &= ~(I2C_SDA | I2C_SCL); /* GPIO function selected */
|
|
|
|
I2C_PxSEL2 &= ~(I2C_SDA | I2C_SCL); /* GPIO function selected */
|
|
|
|
I2C_PxREN &= ~(I2C_SDA | I2C_SCL); /* Deactivate internal pull-up/-down resistors */
|
|
|
|
I2C_PxOUT &= ~(I2C_SDA | I2C_SCL); /* Select pull-up resistors */
|
2012-05-09 08:54:21 +00:00
|
|
|
}
|
2014-10-20 12:21:06 +00:00
|
|
|
/* ------------------------------------------------------------------------------
|
|
|
|
* This function is used to start an I2C communication in master-transmit mode.
|
|
|
|
* ------------------------------------------------------------------------------ */
|
2012-02-20 19:32:05 +00:00
|
|
|
static volatile uint8_t tx_byte_tot = 0;
|
2010-11-07 08:38:51 +00:00
|
|
|
void
|
2014-10-20 12:21:06 +00:00
|
|
|
i2c_transmit_n(uint8_t byte_ctr, uint8_t *tx_buf)
|
|
|
|
{
|
2010-11-07 08:38:51 +00:00
|
|
|
tx_byte_tot = byte_ctr;
|
|
|
|
tx_byte_ctr = byte_ctr;
|
2014-10-20 12:21:06 +00:00
|
|
|
tx_buf_ptr = tx_buf;
|
|
|
|
UCB1CTL1 |= UCTR + UCTXSTT; /* I2C TX, start condition */
|
2010-11-07 08:38:51 +00:00
|
|
|
}
|
|
|
|
/*----------------------------------------------------------------------------*/
|
2012-03-07 00:14:54 +00:00
|
|
|
ISR(USCIAB1TX, i2c_tx_interrupt)
|
|
|
|
{
|
2014-10-20 12:21:06 +00:00
|
|
|
/* TX Part */
|
|
|
|
if(UC1IFG & UCB1TXIFG) { /* TX int. condition */
|
|
|
|
if(tx_byte_ctr == 0) {
|
|
|
|
UCB1CTL1 |= UCTXSTP; /* I2C stop condition */
|
|
|
|
UC1IFG &= ~UCB1TXIFG; /* Clear USCI_B1 TX int flag */
|
|
|
|
} else {
|
2010-11-07 08:38:51 +00:00
|
|
|
UCB1TXBUF = tx_buf_ptr[tx_byte_tot - tx_byte_ctr];
|
|
|
|
tx_byte_ctr--;
|
|
|
|
}
|
|
|
|
}
|
2014-10-20 12:21:06 +00:00
|
|
|
/* RX Part */
|
2010-11-07 08:38:51 +00:00
|
|
|
#if I2C_RX_WITH_INTERRUPT
|
2014-10-20 12:21:06 +00:00
|
|
|
else if(UC1IFG & UCB1RXIFG) { /* RX int. condition */
|
2013-01-30 13:15:16 +00:00
|
|
|
rx_buf_ptr[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
|
|
|
|
rx_byte_ctr--;
|
2014-10-20 12:21:06 +00:00
|
|
|
if(rx_byte_ctr == 1) { /* stop condition should be set before receiving last byte */
|
|
|
|
/* Only for 1-byte transmissions, STOP is handled in receive_n_int */
|
|
|
|
if(rx_byte_tot != 1) {
|
|
|
|
UCB1CTL1 |= UCTXSTP; /* I2C stop condition */
|
|
|
|
}
|
|
|
|
UC1IFG &= ~UCB1RXIFG; /* Clear USCI_B1 RX int flag. XXX Just in case, check if necessary */
|
2010-11-07 08:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-07 00:14:54 +00:00
|
|
|
ISR(USCIAB1RX, i2c_rx_interrupt)
|
|
|
|
{
|
|
|
|
if(UCB1STAT & UCNACKIFG) {
|
2010-11-07 08:38:51 +00:00
|
|
|
PRINTFDEBUG("!!! NACK received in RX\n");
|
|
|
|
UCB1CTL1 |= UCTXSTP;
|
|
|
|
UCB1STAT &= ~UCNACKIFG;
|
|
|
|
}
|
|
|
|
}
|