Hi Tomas,
I'm still fighting with this. I remind you I have a MMA8652, a MSP430G2553 MCU and I use Code Composer Studio.
Today I know that the hardware is ok, because it worked fine with a Energia code example.
But it has been imposible to me to find, adapt or write a native code for a MSP430G2553. I know you are not TI technical representative, but would ask you if you have a basic example (read who_am_i) for this microprocessor.
I case you don't, I'll ask you please to send me the example for whatever MCU you use, and I'll try to translate it.
I've been taking a look to the "Bare metal example project", but i prefer to start with an ultra basic example if possible.
_________________________________________________________________________________________________
This is a trial, I've adapted it from a code example in the web. It executes until the end apparently ok, but the reading result "PRxData" is always 0x00. I tried different variations over this code but nothing. I feel it is quite close, but something is still wrong. Do I need to initialize ctrl_reg1 before reading WHO_AM_I register?
#include <msp430g2553.h>
int ctrl_reg1 = 0x2A;
int lcount = 0;
int TXByteCtr;
unsigned char palout;
unsigned char PRxData;
int Rx = 0;
char WHO_AM_I = 0x0D; //WHO_AM_I register address
char itgAddress = 0x1D;
void init_I2C(void);
void Transmit(void);
void Receive(void);
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0
init_I2C();
// while(1){
//Transmit process
Rx = 0;
TXByteCtr = 1;
palout= ctrl_reg1;
Transmit();
_delay_cycles(100);
//Transmit process
Rx = 0;
TXByteCtr = 1;
palout= 0x01;
Transmit();
_delay_cycles(100);
//Transmit process
Rx = 0;
TXByteCtr = 1;
palout= WHO_AM_I;
Transmit();
_delay_cycles(100);
//Receive process
Rx = 1;
Receive();
lcount++;
while(1);
//}
}
//-------------------------------------------------------------------------------
// The USCI_B0 data ISR is used to move received data from the I2C slave
// to the MSP430 memory. It is structured such that it can be used to receive
//-------------------------------------------------------------------------------
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if(Rx == 1){ // Master Recieve?
PRxData = UCB0RXBUF; // Get RX data
lcount++;
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
else{ // Master Transmit
if (TXByteCtr) // Check TX byte counter
{
UCB0TXBUF = palout; // Load TX buffer
TXByteCtr--; // Decrement TX byte counter
}
else
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
}
}
void init_I2C(void) {
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = itgAddress; // Slave Address is 069h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0RXIE + UCB0TXIE; //Enable RX and TX interrupt
}
void Transmit(void){
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
void Receive(void){
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 &= ~UCTR ; // Clear UCTR
UCB0CTL1 |= UCTXSTT; // I2C start condition
while (UCB0CTL1 & UCTXSTT); // Start condition sent?
UCB0CTL1 |= UCTXSTP; // I2C stop condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}