Hello Guys,
I am trying to interface MSP430FR5994 LP with LIS3DSH accelerometer over SPI communication as this is my first hands on attempt on this set of hardware. I am successfully transmitting data at 1MHz. All I am trying to do is read register (WHO_AM_I) from address 0x0F which is read only device id and should always return 0x3F. So after sending dummy byte as 0x00 it always returns 0x00. I tried sending other test data but it always replies with 0s. And I am not sure what could be wrong. Please help, thanks in advance.
/*
* File: main.c
* Author: Anand
*/
// ***** includes *****
#include <stdio.h>
#include <stdint.h>
#include "msp430fr5994.h"
#include "spi.h"
// ***** defines *****
#define SPI_TIMEOUT 1234
uint16_t who_am_i;
// ***** private function prototypes *****
void main(void);
void clock_init(void);
// ***** private function definitions *****
/*
* @brief XT1 Setup
* @param None
* @return None
*/
void clock_init (void)
{
CSCTL0_H = CSKEY_H; // Unlock CS registers -----|
// |
CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz |
CSCTL1 &= ~DCORSEL; // |
// |
CSCTL2 = SELA__LFXTCLK; // LFXTCLK when available, otherwise VLOCLK |
CSCTL2 |= SELS__DCOCLK; // DCOCLK |
CSCTL2 |= SELM__DCOCLK; // DCOCLK |
// |
CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // Set all dividers |
// |
CSCTL4 &= ~LFXTOFF; // LFXT is on if LFXT is selected |
// |
do // |
{ // |
CSCTL5 &= ~LFXTOFFG; // Clear XT1 fault flag |
SFRIFG1 &= ~OFIFG; // |
} // |
while (SFRIFG1 & OFIFG); // Test fault flag |
// |
CSCTL0_H = 0; // Lock CS registers -------|
}
/*
* @brief This is the main function
* @param None
* @return None
*/
void main (void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop the watch dog timer
// Configure GPIO
spi_b_gpio_init();
// Initialize the clock
clock_init();
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
// Initialize the USCI_B1 for SPI operation
spi_b_init();
P8DIR = 0xFF; // Port 8 as output
P8OUT = 0x00; // Port 8 as low outout
UCB1TXBUF = 0x0F | 0x80; // Transmit characters | R/W bit
uint16_t spi_timeout = SPI_TIMEOUT;
while((UCB1STATW && UCBUSY) && (spi_timeout > 0))
{
spi_timeout--;
}
if(spi_timeout == 0)
{
printf("spi 1 transmit failed \n");
}
UCB1TXBUF = 0x00; // Transmit characters
spi_timeout = SPI_TIMEOUT;
while((UCB1STATW && UCBUSY) && (spi_timeout > 0))
{
spi_timeout--;
}
if(spi_timeout == 0)
{
printf("spi 2 transmit failed \n");
}
else
{
printf("spi 2 transmit time: %u \n", spi_timeout);
}
who_am_i = UCB1RXBUF;
printf("exiting main \n");
return ;
}





