This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Configuration and successful I2C communication using USI in MSP430G2xx

Other Parts Discussed in Thread: MSP430G2230, BQ32000

Dear all,

 Recently, I have been working on the MSP430G2230 variant of the MSP430 family and I have come accross several posts where people keep asking about glitches about USI module while communicating using the I2C with I2C slaves. However, after several painstaking debugging and thanks to TI Wiki and other common webpages on the similar topic, I have been able to communicate successfully with I2C slaves using the normal write, and repeated start based routines. Although I have not tried multimaster mode (knowing that Arbitation control is there in these USI variants), but I am sharing my part of the source code and the header. Hope it benifits someone there:

But a few points I would like to say before I post the code and the header.

There are two files in the archive:

1.  IFT_USI_I2C.h : this contains the definitions and is the header for the source code

2. IFT_USI_I2C.c : contains the following functions:

extern void ConfigUSIForI2CMaster(int); // type = 1 for non interrupt and 2 = Polling

This function basically configures the USI module for interrupt and non interrupt based modes of operation. For interrupt based mode, you will have to include the ISRs in the main function and is not included in the archive since that can be obtained from the "resource explorer" examples and can be modified separately. For the polling mode, the operation is based on the USIIFG flag and its setting and resetting has been taken care of for proper operation and I2C specification meets. the argument values are explained in the comments itself.

extern int WriteTo24CxxEEPROM(unsigned char ,unsigned char ,unsigned short ,unsigned char ); // Writes 1 Byte to EEPROM AT24xx series

This function writes ONE byte (8 bit) to a provided I2C 24Cxx eeprom family MCU (only upto 24C02, 24C04 onwards needs >8 bits of internal address management that was not been taken care of because the user can easily go through the routine and do the same) internal address. The last argument is the value in hex to be written inside the EEPROM

extern int WriteTo24CxxEEPROMMultibytes(unsigned char ,unsigned char ,unsigned short ,int , unsigned char *); // Writes an Array data to EEPROM, return NACK error code 1

This is just identical to the previous one, except that this function can write from an Array pointed by the pointer unsigned char * to the series of locations inside the 24Cxx for the specified number of bytes in the "int" argument. Please note that you will have to take care of the no. of bytes you write using this fucntion obeying the maximum no. of bytes that can be written in Page Mode. Several 24Cxx chips have several page mode boundaries and exceeding them will cause over write of memory due to roll over. The return integer is 1 if some data is not acknowledged by slave, else 0 is returned! You can always add on more options if you want :-)

extern unsigned char ReadFrom24CxxEEPROM(unsigned char ,unsigned char ,unsigned short ); // Reads a single byte to provided Addr

This is a similar function as described above, apart from the fact that this function can only READ from a EEPROM location and return the read data. The first argument is the chip address (that depends upton the first 4 fixed digits of 1010 for24Cxx and last 3 bits are hardwired and user selectable), usually for A0 = A1 = A2 = ground, it becomes 0xA0, second argument is the chip type = 2 for 24C02, 4 for24C04 and so on. The last argument is the address from where the value needs to be read. The read data is returned back.


extern int ReadFrom24CxxEEPROMMultiBytes(unsigned char ,unsigned char ,unsigned short ,int , unsigned char *); // Reads a multiple bytes to provided Array

Similar to the previous one, except that the no. of multibytes to be read has to be mentioned in the second last argument "int". Again please keep in mind that the no. of data to be read in page mode must be taken care of. Finally the data is dumped in the Array using the pointer in the last argument to the provided location.


extern int SetRTC(unsigned char,unsigned char,int,volatile unsigned char *); // used to set any RTC to the intial values //pointer and checks unack condition


extern int ReadFromRTCMultiBytes(unsigned char ,unsigned short ,int ,volatile unsigned char *);

The SetRTC and ReadFromRTCMultiBytes are almost same as the EEPROMWrite and EEPROMReadMultiBytes, but they were created keeping in mind that you can add on more functionalities needed by specific I2C RTCs. Actually I was using the TI BQ32000 that has the facility of trickle charging, bypass of FET, Calibration for the crystal oscillation and a special function register. All these settings can be incorporated in the SetRTC function and can be made handy. This is left to the user! And for other 3rd Party RTCs, you can add on more features too.

The motive of these libraries is to provide the basic framework which has been tested and is working fine. I have successfully tested the codes using MSP430G2230, BQ32000 RTC and 24C02.

I hope others will be benefitted.

I have also found  this link   to be very helpful in explaining the USI configuration for Read, Write and Repeated Start Reads:

IFT_USI_I2C source codes.zip

> P.S. Sorry for any mistakes and bad programming techniques (if any) in the codes. I just shared what I could do!

**Attention** This is a public forum