Jump to content
43oh

Yet another I2C newbie having problems


Recommended Posts

Hey gang,

 

I'm slowly working my way towards supreme greatness and world-overlord status, but before I achieve that I thought I would set up some working tools in my toolbelt. So among the first things I wanted was a Pimmel-proof I2C setup. I'm using an MSP430g2553 chip, and I found some sample code that @@_Murphy_ created found here [1]. Paired with the wonderful serial printf combo found in this thread [2], I thought I had the makings of the awesomeness that is me. I soldered up a simple version of the 3eeprom board found on dangerousprototypes [3], tested it with a bus pirate to ensure it works and set about to conquer the world with my g2553 chip.

 

Below is my simple little test program that aims to replicate the test case of the DP 3EEPROM.

#include <msp430.h>
#include "I2C_MSP430.h"
#include "serial.h"

void printf(char *, ...);

void main(void)
{
    char c;
    //uint8_t dev_address = 0xa0;
    uint8_t dev_address = 0b10100000;
    uint8_t reg_address = 0x00;
    uint8_t data[3];
		int i;

    // Disable watchdog 
    WDTCTL = WDTPW + WDTHOLD;

    // Use 1 MHz DCO factory calibration
    DCOCTL = 0;
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ;
            
    // Setup the serial port
    // Serial out: P1.1 (BIT1)
    // Serial in:  P1.2 (BIT2)
    // Bit rate:   9600 (CPU freq / bit rate)  
    serial_setup (BIT1, BIT2, 1000000 / 9600);
    
    // Send a string

    printf("Write some bytes...\r\n");
    I2CbeginTransmission(dev_address);
    I2Cwrite(0);
    I2Cwrite(0);
    I2Cwrite(3);
    I2Cwrite(2);
    I2Cwrite(1);
    I2CendTransmission();
    
    printf("Move read pointer...\r\n");
    I2CbeginTransmission(dev_address);
    I2Cwrite(0x0);
    I2Cwrite(0x0);
    I2CendTransmission();
    
    printf("Read some bytes...\r\n");
    dev_address = 0b10100001;
  	I2CrequestFrom(dev_address, 3);
	  for (i = 0; i < 3; i++)
	  {
			data[i] = I2Cread();
		}
		printf("%i %i %i \r\n", data[0], data[1], data[2]);

    for (; {                  // Do forever
        c = serial_getc ();     // Get a char
        serial_putc (c);        // Echo it back
    }
}

The first issue I ran into was that it seemed to hang in the I2CendTransmission function whenever it tried to put the chip into low power. I commented out those lines and the program moved on, but I am not getting the results I expected

 

Sample output in my serial console:

Write some bytes...
Move read pointer...
Read some bytes...
0 0 0

Does anyone with kung-fu prowess have any idea what I'm doing wrong? I admit to being fairly new to actually trying to use I2C. I've tried my device address with the read/write bit set to one and zero, and it didn't seem to change the result.

 

Here is my setup; Windows 7, using commandline compilation with msp430-gcc compiler shipped with Energia.

 

***EDIT - REMOVED THIS CODE SINCE IT IS SUPERCEDED IN LATER POST***

I've attached my simple little project with a Makefile in case you're morbidly interested...

 

Many thanks,

 

Keith

 

[1] - http://forum.43oh.com/topic/3216-need-help-with-i2c-using-msp430g2553/?p=33924

[2] - http://forum.43oh.com/topic/1289-tiny-printf-c-version/

[3] - http://dangerousprototypes.com/docs/3EEPROM_explorer_board#24AA_I2C_EEPROM

Edited by pimmel
Link to post
Share on other sites

I should mention that I have my SCL line on P1.6 and my SDA line on P1.7...is that even correct? Everything I found (including the data sheet) seemed to indicate that those were the two pins to use, but I couldn't really follow the I2C code and find an explicit declaration of using those pins.

 

One more quick edit; I have 10k pull-up resistors on the SDA and SCL lines and I have the jumpers to the LEDs on the LP removed.

Link to post
Share on other sites

Okay, so I tried to use the TI I2C library found here [1], but again I'm running into a brick wall

 

My main routine code shown below

#include <msp430.h>
#include "TI_USCI_I2C_master.h"
#include "serial.h"

void printf(char *, ...);

void main(void)
{
    char c;
    unsigned int dev_address = 0b10100000;
    unsigned int reg_address = 0x00;
    unsigned int data[3];
    unsigned char array[5] = { 0x0, 0x0, 0x3, 0x2, 0x1};
    unsigned char store[3] = { 0x0, 0x0, 0x0};
    int i;

    // Disable watchdog 
    WDTCTL = WDTPW + WDTHOLD;

    // Use 1 MHz DCO factory calibration
    DCOCTL = 0;
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ;
            
    // Setup the serial port
    // Serial out: P1.1 (BIT1)
    // Serial in:  P1.2 (BIT2)
    // Bit rate:   9600 (CPU freq / bit rate)  
    serial_setup (BIT1, BIT2, 1000000 / 9600);
    printf("Can we write to serial???\r\n");
    
    // Send a string
    TI_USCI_I2C_transmitinit(dev_address,0xa);
    printf("1\r\n");
    while ( TI_USCI_I2C_notready() ); // wait for bus to be free
    printf("2\r\n");
    //if ( TI_USCI_I2C_slave_present(dev_address) )    // slave address may differ from
    //{                                         // initialization
      printf("Write some bytes...\r\n");
      while ( TI_USCI_I2C_notready() );         // wait for bus to be free
      printf("3\r\n");
      TI_USCI_I2C_transmit(5,array);       // start transmitting 
      printf("4\r\n");
      while ( TI_USCI_I2C_notready() );         // wait for bus to be free

      printf("Move read pointer...\r\n");
      TI_USCI_I2C_transmitinit(dev_address,0xa);
      printf("6\r\n");
      while ( TI_USCI_I2C_notready() );         // wait for bus to be free
      printf("7\r\n");
      TI_USCI_I2C_transmit(2,array);       // start transmitting 
      printf("8\r\n");
      while ( TI_USCI_I2C_notready() );         // wait for bus to be free
        
      printf("Read some bytes...\r\n");
      TI_USCI_I2C_receiveinit(dev_address, 0xa);   // init receiving with USCI 
      while ( TI_USCI_I2C_notready() );         // wait for bus to be free
      TI_USCI_I2C_receive(3,store);
      while ( TI_USCI_I2C_notready() );         // wait for bus to be free
    //}
    printf("%i %i %i \r\n", store[0], store[1], data[2]);

    for (; {                  // Do forever
        c = serial_getc ();     // Get a char
        serial_putc (c);        // Echo it back
    }
}

With my serial console spitting out the following

Can we write to serial???
1
2
Write some bytes...
3
4
Move read pointer...
6
7
8
Read some bytes...
0 0 -8454

I know I'm missing something obvious, as folks are using I2C regularly :smile: Does anyone spot the error of my ways?

 

[1] - http://www.ti.com/general/docs/litabsmultiplefilelist.tsp?literatureNumber=slaa382

Link to post
Share on other sites

I downloaded the latest version of Energia, and now I actually got somewhere! Wootwoot....

 

My ino file

#include "Wire.h"

void setup()
{
  // put your setup code here, to run once:
  Wire.begin();
  Serial.begin(9600);
 
  Serial.print("write some bytes\n"); 
  Wire.beginTransmission(0b10100000);
  Wire.write(0);
  Wire.write(0);
  Wire.write(67);  // Capital 'C'
  Wire.write(65);  // Capital 'A'
  Wire.write(66);  // Capital 'B'
  Wire.endTransmission();
  
  Serial.print("move the read pointer\n");
  Wire.beginTransmission(0x50);
  Wire.write(0);
  Wire.write(0);
  Wire.endTransmission();
  
  Serial.print("read some bytes\n");
  Wire.requestFrom(0x50,3);
  
  while(Wire.available())
  {
    char c = Wire.read();
    Serial.print(c);
  }
}

void loop()
{
  // put your main code here, to run repeatedly:
  
}

Output in a serial terminal:

write some bytes
move the read pointer
read some bytes
CAB

Real sexy, huh? So now I know that the coms and the wiring work, I need to figure out what is going on in the other I2C attempts above.

Link to post
Share on other sites

...and the final post in this series involves success reading and writing via I2C outside of Energia, using a very modest addition to the neat I2C library created by @@V0JT4, found here [1]. When I tried compiling it with mspgcc, it complained about _swap_bytes being undefined, so I just knocked up a real quick implementation for it. It looks like it is used for 16bit addressing, but as I'm only using 8bit addressing at the moment, I haven't had an opportunity for it to fail magnificently!
 
So now I have my starting point for playing around with the si570 I2C-controlled oscillator: makefile, mspgcc, i2c library, serial debugging capability.
 
If anyone is interested in the code, I've attached it to this final post...
 
 test3.zip
 
[1] - http://forum.43oh.com/topic/1772-universal-ripple-control-receiver/?p=16781

 

And if you're really bored, I blogged about it here.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...