Skip to content

Commit

Permalink
Merge pull request contiki-os#825 from alignan/i2c_changes
Browse files Browse the repository at this point in the history
Add function to change I2C baudrate and push relevant configuration valu...
  • Loading branch information
Nicolas Tsiftes committed Oct 21, 2014
2 parents 0d39ee9 + ef6c351 commit 10d4446
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 231 deletions.
14 changes: 4 additions & 10 deletions examples/z1/test-light-ziglet.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,13 @@
#include "dev/i2cmaster.h"
#include "dev/light-ziglet.h"


#if 1
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif


#if 0
#define PRINTFDEBUG(...) printf(__VA_ARGS__)
#else
#define PRINTFDEBUG(...)
#endif


#define SENSOR_READ_INTERVAL (CLOCK_SECOND)
#define SENSOR_READ_INTERVAL (CLOCK_SECOND / 2)

PROCESS(test_process, "Test light ziglet process");
AUTOSTART_PROCESSES(&test_process);
Expand All @@ -70,7 +61,10 @@ PROCESS_THREAD(test_process, ev, data)

uint16_t light;

/* Initialize driver and set a slower data rate */

light_ziglet_init();
i2c_setrate(I2C_PRESC_100KHZ_LSB, I2C_PRESC_100KHZ_MSB);

while(1) {
etimer_set(&et, SENSOR_READ_INTERVAL);
Expand Down
236 changes: 108 additions & 128 deletions platform/z1/dev/i2cmaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,98 +41,90 @@
#include "i2cmaster.h"
#include "isr_compat.h"

signed char tx_byte_ctr, rx_byte_ctr;
signed char tx_byte_ctr, rx_byte_ctr;
unsigned char rx_buf[2];
unsigned char* tx_buf_ptr;
unsigned char* rx_buf_ptr;
unsigned char *tx_buf_ptr;
unsigned char *rx_buf_ptr;
unsigned char receive_data;
unsigned char transmit_data1;
unsigned char transmit_data2;
volatile unsigned int i; // volatile to prevent optimization
unsigned char prescale_lsb = I2C_PRESC_Z1_LSB;
unsigned char prescale_msb = I2C_PRESC_Z1_MSB;
volatile unsigned int i; /* volatile to prevent optimization */

//------------------------------------------------------------------------------
// void i2c_receiveinit(unsigned char slave_address,
// unsigned char prescale)
//
// This function initializes the USCI module for master-receive operation.
//
// IN: unsigned char slave_address => Slave Address
// unsigned char prescale => SCL clock adjustment
//-----------------------------------------------------------------------------
/* ------------------------------------------------------------------------------
* Change the data rate prior initializing transmission or reception
* ----------------------------------------------------------------------------- */
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 = I2C_PRESC_400KHZ_LSB; // prescaler for 400 kHz data rate
UCB1BR1 = I2C_PRESC_400KHZ_MSB;
UCB1I2CSA = slave_address; // set slave address

UCB1CTL1 &= ~UCTR; // I2C Receiver

UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
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 */
UCB1I2CIE = UCNACKIE;
#if I2C_RX_WITH_INTERRUPT
UC1IE = UCB1RXIE; // Enable RX interrupt if desired
UC1IE = UCB1RXIE; /* Enable RX interrupt if desired */
#endif
}

//------------------------------------------------------------------------------
// void i2c_transmitinit(unsigned char slave_address,
// unsigned char prescale)
//
// Initializes USCI for master-transmit operation.
//
// IN: unsigned char slave_address => Slave Address
// unsigned char prescale => SCL clock adjustment
//------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------
* Initializes USCI for master-transmit operation.
* ------------------------------------------------------------------------------ */
void
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 = I2C_PRESC_400KHZ_LSB; // prescaler for 400 kHz data rate
UCB1BR1 = I2C_PRESC_400KHZ_MSB;
UCB1I2CSA = slave_address; // Set slave address

UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
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 */
UCB1I2CIE = UCNACKIE;
UC1IE = UCB1TXIE; // Enable TX ready interrupt
UC1IE = UCB1TXIE; /* Enable TX ready interrupt */
}

//------------------------------------------------------------------------------
// void i2c_receive_n(unsigned char byte_ctr, unsigned char * rx_buf)
// This function is used to start an I2C communication in master-receiver mode WITHOUT INTERRUPTS
// for more than 1 byte
// IN: unsigned char byte_ctr => number of bytes to be read
// OUT: unsigned char rx_buf => receive data buffer
// OUT: int n_received => number of bytes read
//------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------
* This function is used to start an I2C communication in master-receiver mode WITHOUT INTERRUPTS
* for more than 1 byte
* ------------------------------------------------------------------------------ */
static volatile uint8_t rx_byte_tot = 0;
uint8_t
i2c_receive_n(uint8_t byte_ctr, uint8_t *rx_buf) {
i2c_receive_n(uint8_t byte_ctr, uint8_t *rx_buf)
{

rx_byte_tot = byte_ctr;
rx_byte_ctr = byte_ctr;
rx_buf_ptr = rx_buf;
rx_buf_ptr = rx_buf;

while ((UCB1CTL1 & UCTXSTT) || (UCB1STAT & UCNACKIFG)) // Slave acks address or not?
PRINTFDEBUG ("____ UCTXSTT not clear OR NACK received\n");
while((UCB1CTL1 & UCTXSTT) || (UCB1STAT & UCNACKIFG)) /* Slave acks address or not? */
PRINTFDEBUG("____ UCTXSTT not clear OR NACK received\n");

#if I2C_RX_WITH_INTERRUPT
PRINTFDEBUG(" RX Interrupts: YES \n");

// 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
/* 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 */
dint();
UCB1CTL1 |= UCTXSTT; // I2C start condition
while(UCB1CTL1 & UCTXSTT) // Waiting for Start bit to clear
PRINTFDEBUG ("____ STT clear wait\n");
UCB1CTL1 |= UCTXSTP; // I2C stop condition
UCB1CTL1 |= UCTXSTT; /* I2C start condition */
while(UCB1CTL1 & UCTXSTT) /* Waiting for Start bit to clear */
PRINTFDEBUG("____ STT clear wait\n");
UCB1CTL1 |= UCTXSTP; /* I2C stop condition */
eint();
}
else{ // all other cases
UCB1CTL1 |= UCTXSTT; // I2C start condition
} else { /* all other cases */
UCB1CTL1 |= UCTXSTT; /* I2C start condition */
}
return 0;

Expand All @@ -141,98 +133,86 @@ i2c_receive_n(uint8_t byte_ctr, uint8_t *rx_buf) {

PRINTFDEBUG(" RX Interrupts: NO \n");

UCB1CTL1 |= UCTXSTT; // I2C start condition
UCB1CTL1 |= UCTXSTT; /* I2C start condition */

while (rx_byte_ctr > 0){
if (UC1IFG & UCB1RXIFG) { // Waiting for Data
while(rx_byte_ctr > 0) {
if(UC1IFG & UCB1RXIFG) { /* Waiting for Data */
rx_buf[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
rx_byte_ctr--;
UC1IFG &= ~UCB1RXIFG; // Clear USCI_B1 RX int flag
UC1IFG &= ~UCB1RXIFG; /* Clear USCI_B1 RX int flag */
n_received++;
}
}
UCB1CTL1 |= UCTXSTP; // I2C stop condition
UCB1CTL1 |= UCTXSTP; /* I2C stop condition */
return n_received;
#endif
}


//------------------------------------------------------------------------------
// uint8_t i2c_busy()
//
// This function is used to check if there is communication in progress.
//
// OUT: unsigned char => 0: I2C bus is idle,
// 1: communication is in progress
//------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------
* This function is used to check if there is communication in progress.
* ------------------------------------------------------------------------------ */
uint8_t
i2c_busy(void) {
return (UCB1STAT & UCBBUSY);
i2c_busy(void)
{
return UCB1STAT & UCBBUSY;
}

/*----------------------------------------------------------------------------*/
/* Setup ports and pins for I2C use. */
/*----------------------------------------------------------------------------
* Setup ports and pins for I2C use.
* ------------------------------------------------------------------------------ */

void
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
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 */
}

void
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
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 */
}

/*----------------------------------------------------------------------------*/
//------------------------------------------------------------------------------
// void i2c_transmit_n(unsigned char byte_ctr, unsigned char *field)
//
// This function is used to start an I2C communication in master-transmit mode.
//
// IN: unsigned char byte_ctr => number of bytes to be transmitted
// unsigned char *tx_buf => Content to transmit. Read and transmitted from [0] to [byte_ctr]
//------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------
* This function is used to start an I2C communication in master-transmit mode.
* ------------------------------------------------------------------------------ */
static volatile uint8_t tx_byte_tot = 0;
void
i2c_transmit_n(uint8_t byte_ctr, uint8_t *tx_buf) {
i2c_transmit_n(uint8_t byte_ctr, uint8_t *tx_buf)
{
tx_byte_tot = byte_ctr;
tx_byte_ctr = byte_ctr;
tx_buf_ptr = tx_buf;
UCB1CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
tx_buf_ptr = tx_buf;
UCB1CTL1 |= UCTR + UCTXSTT; /* I2C TX, start condition */
}

/*----------------------------------------------------------------------------*/
ISR(USCIAB1TX, i2c_tx_interrupt)
{
// 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 {
/* 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 {
UCB1TXBUF = tx_buf_ptr[tx_byte_tot - tx_byte_ctr];
tx_byte_ctr--;
}
}
// RX Part
/* RX Part */
#if I2C_RX_WITH_INTERRUPT
else if (UC1IFG & UCB1RXIFG){ // RX int. condition
else if(UC1IFG & UCB1RXIFG) { /* RX int. condition */
rx_buf_ptr[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
rx_byte_ctr--;
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
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 */
}
}
#endif
Expand Down
29 changes: 2 additions & 27 deletions platform/z1/dev/i2cmaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,7 @@ void i2c_transmitinit(uint8_t slave_address);
void i2c_transmit_n(uint8_t byte_ctr, uint8_t *tx_buf);

uint8_t i2c_busy(void);

//XXX Should these defines be in the contiki-conf.h to make it more platform-independent?
#define I2C_PxDIR P5DIR
#define I2C_PxIN P5IN
#define I2C_PxOUT P5OUT
#define I2C_PxSEL P5SEL
#define I2C_PxSEL2 P5SEL2
#define I2C_PxREN P5REN


#define I2C_SDA (1 << 1) //SDA == P5.1
#define I2C_SCL (1 << 2) //SCL == P5.2
#define I2C_PRESC_1KHZ_LSB 0x00
#define I2C_PRESC_1KHZ_MSB 0x20
#define I2C_PRESC_100KHZ_LSB 0x50
#define I2C_PRESC_100KHZ_MSB 0x00
#define I2C_PRESC_400KHZ_LSB 0x14
#define I2C_PRESC_400KHZ_MSB 0x00

// I2C configuration with RX interrupts
#ifdef I2C_CONF_RX_WITH_INTERRUPT
#define I2C_RX_WITH_INTERRUPT I2C_CONF_RX_WITH_INTERRUPT // XXX Move I2C_CONF_RX_WITH_INTERRUPT to contiki-conf.h or platform-conf.h
#else /* I2C_CONF_RX_WITH_INTERRUPT */
#define I2C_RX_WITH_INTERRUPT 1
#endif /* I2C_CONF_RX_WITH_INTERRUPT */

void i2c_setrate(uint8_t p_lsb, uint8_t p_msb);

#if 0
#include <stdio.h>
Expand All @@ -87,4 +62,4 @@ uint8_t i2c_busy(void);
#define PRINTFDEBUG(...)
#endif

#endif /* #ifdef I2CMASTER_H_ */
#endif /* #ifdef I2CMASTER_H_ */
Loading

0 comments on commit 10d4446

Please sign in to comment.