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.

CCS/AFE4300: Problem with AFE4300 SPI COM

Part Number: AFE4300
Other Parts Discussed in Thread: MSP430G2553,

Tool/software: Code Composer Studio

Hey guys,

I need your help working with the AFE4300 and a MSP430G2553. I prgrammed the SPI communication and want to test it by writing into a register and read it via SPI, but it doesn't work..

Here my code:

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;	                                                        // Stop watchdog timer

	if(CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF)                                      // If cal constants erased, trap CPU
	{
	    while(1);
	}

	BCSCTL1 = CALBC1_1MHZ;                                                              // If calibration constant erased. Do not load, trap CPU!
	DCOCTL = CALDCO_1MHZ;                                                               // Set DCO step + modulation

    AFE4300_RESET_LOW;                                                                  // Pin 2.6 is Reset Pin
    P2DIR |=  BIT2;                                                                     // AFE4300 Reset Pin is an OUTPUT Pin

    AFE4300_RESET_HIGH;
    AFE4300_SPI_init();
    AFE4300_init();

    uint16_t data_rcv[1] = {0};                                       // Check the register via SPI
    data_rcv[0] = AFE4300_readRegister(BCM_DAC_FREQ);

    if(0x0020 == data_rcv[0]) blink();                               

}
//####################################################################################################################################################################################
//                                                                      AFE4300_SPI_init()
// Initialize SPI-UCB0 of the MSP430G2553


// MAXIMALE CLOCKFREQUENZ 4MHz!
void AFE4300_SPI_init(void)
{
    AFE4300_CS_HIGH;                                                                 // SlaveSelect PIN
    P2DIR |=  BIT0;                                                                  // SlaveSelect PIN
    P1SEL |=  BIT6 | BIT7 | BIT5;                                                    // Select MISO, MOSI, SCK
    P1SEL2 |=  BIT6 | BIT7 | BIT5;                                                    // Reconfigure Pins to use them for SPI

    UCB0CTL1 = UCSWRST;                                                              // Software reset enable
    UCB0CTL0 |= UCMST + UCMSB + UCCKPH + UCSYNC;                           // Mastermode, MSB first, ClockPhase 1, 3-pin SPI, Synchronous mode (Clock Polarity 0, SPIMODE1)
    UCB0CTL1 |= UCSSEL_2;                                                            // SMCLK as the source clock for the module with the UCSSEL_2 bit
    UCB0BR0  |= 0x02;                                                                // Bit clock prescaler low byte
    UCB0BR1   = 0;                                                                   // Bit clock prescaler high byte

    UCB0CTL1 &= ~UCSWRST;                                                            // Initialize USCI state machine

}

//####################################################################################################################################################################################
//                                                                      AFE4300_init()
// Initialize the AFE4300

void AFE4300_init(void)
{
   AFE4300_reset();                                                                  // Reset the AFE4300

  __delay_cycles(DELAY_1ms);

  AFE4300_write(BCM_DAC_FREQ, 0x0020);                                                // Sets the frequency of the BCM excitation current source (now: 32kHz)
  AFE4300_write(MISC1_REGISTER, 0x0000);
  AFE4300_write(MISC2_REGISTER, 0xFFFF);
  AFE4300_write(MISC3_REGISTER, 0x0030);
  AFE4300_write(DEVICE_CONTROL_1, 0x600E);                                            // Body composition measurement front-end power-down bit AND Power up of front-end (PDB)
  AFE4300_write(DEVICE_CONTROL_2, 0x1800);                                            // IQ_DEMOD_CLK must be four times the BCM_DAC_FREQ (IQ_DEMOD_CLK = fCLK / (IQ_DEMOD_CLK_DIV_FAC) = BCM_DAC_FREQ × 4) (now: Divider is 8)
  AFE4300_write(ADC_CONTROL_REGISTER_1, 0x41C0);                                      // ADC_DATA_RATE: 128 SPS and ADC_PD and ADC_MEAS_MODE: differential (set Bit 15 for ADC_CONV_MODE)
  AFE4300_write(ADC_CONTROL_REGISTER_2, 0x0063);                                      // Output of the body composition measurement front-end (OUTP_FILT/OUTM_FILT)
  AFE4300_write(WEIGHT_SCALE_CONTROL, 0x0000);                                        // Not used
  AFE4300_write(ISW_MATRIX, 0x0000);                                                  // All switches open (internal MUX)
  AFE4300_write(VSW_MATRIX, 0x0000);                                                  // All switches open (internal MUX)
  //AFE4300_write(IQ_MODE_ENABLE, 0x800);                                               // Set Bit 11 for IQ (0: Full-Wave-Rectifier mode)
  AFE4300_write(IQ_MODE_ENABLE, 0x000);                                               // Set Bit 11 for IQ (0: Full-Wave-Rectifier mode)

  __delay_cycles(DELAY_1ms);
}

//####################################################################################################################################################################################
//                                                                      AFE4300_reset()
// Reset of the AFE4300 (reset to get all the internal registers to their default state)
// Resetting the device is done by applying a zero pulse in the RST line for more than 20 ns after the power is stable for 5 ms

void AFE4300_reset(void)
{
  AFE4300_RESET_LOW;                                                                // Applying a zero to the Reset Pin
  __delay_cycles(DELAY_10ms);                                                        // Wait
  AFE4300_RESET_HIGH;                                                               // Applying Voltage to the Reset Pin
}

//####################################################################################################################################################################################
//                                                                      AFE4300_write()
// Write in registers of the AFE4300 via SPI
//
// @ addr     : Address of the register
// @ data     : Unsigned 8-Bit-Data

void AFE4300_write(uint8_t addr, uint16_t data)
{
  uint8_t firstByte = (uint16_t)(data >> 8);
  uint8_t secondByte = (uint16_t)data;

  AFE4300_CS_LOW;                                                                   // Slave-Select Pin LOW, start the SPI transmission

  AFE4300_SPI_transmit(addr);                                                       // Send address
  AFE4300_SPI_transmit(firstByte);                                                       // Send data to address
  AFE4300_SPI_transmit(secondByte);

  AFE4300_CS_HIGH;                                                                  // Slave-Select Pin HIGH, end the SPI transmission

  __delay_cycles(DELAY_1us);                                                        // Wait
}

//####################################################################################################################################################################################
//                                                                     AFE4300_SPI_transmit()
// Send and receive via SPI
//
// @ data    : Unsigned 8-Bit-Data which will be transmitted

unsigned char AFE4300_SPI_transmit(unsigned char data)
{
    UCB0TXBUF = data;                                                               // Send data
    while( UCB0STAT & UCBUSY );
    return UCB0RXBUF;                                                               // return received data --> Receive-data buffer
}

//####################################################################################################################################################################################
//                                                                     AFE4300_readRegister()
// Read a register of the AFE4300

uint16_t AFE4300_readRegister(uint8_t address)
{
  address = address & 0x1F;                                                         // Lower 5 bits specify real address
  address = address | 0x20;                                                         // Bit 6 of the address is the read(1) and write(0) bit

  AFE4300_CS_LOW;
  AFE4300_SPI_transmit(address);
  uint8_t spiReceiveFirst  = AFE4300_SPI_transmit(0x00);
  uint8_t spiReceiveSecond = AFE4300_SPI_transmit(0x00);
  AFE4300_CS_HIGH;

  uint16_t spiReceive = (spiReceiveFirst << 8) | (spiReceiveSecond);            // Combine the two received bytes into an unsigned int
  return spiReceive;
}

//####################################################################################################################################################################################
//                                                                     AFE4300_read_ResultRegister()
// Read the result register of the AFE4300

uint16_t AFE4300_read_ResultRegister()
{
    return AFE4300_readRegister(ADC_DATA_RESULT);
}

Could you please help me?
When I try to compare the send and read data, nothing happen (a LED should blink)



Best regards,

Simon

**Attention** This is a public forum