Tool/software: Code Composer Studio
I am new to embedded systems. I am trying to interface ICM20948 IMU sensor with MSP430FR6989 using SPI protocol. My requirement is to develop code from scratch without using MSP Driver libraries. Based on the example codes available in Resource Explorer, I have configured SPI protocol in the microcontroller. I am able to send data but I am unable to receive data from the IMU sensor. When connect MOSI and SOMI of microcontroller without IMU and initiate transmission, I am able to transmit and receive data. I am trying to read the register for WHO AM I value of the device.
Configuration:
MSP - Master mode
Clock - 7Mhz
Data latch - rising edge
Data transmission - falling edge
I have been struggling with this problem over 3 weeks, kindly help me by pointing out where I am going wrong in the following code.
Thanks in advance.
#include <msp430.h> volatile int ch1; unsigned int i; // GPIO Configuration void initGPIO() { // Test code for LED at P1.0 P8OUT &= ~BIT5; // Clear P1.0 output latch for a defined power-on state P8DIR |= BIT5; // Set P1.0 to output direction // USCI_A0 SPI configuration // P2.0,2.1,2.2,configured for SPI functionality P2SEL1 &= ~(BIT0 | BIT1 | BIT2); P2SEL0 |= BIT0 | BIT1 | BIT2; } void initClock() { // Clock System Setup CSCTL0_H = CSKEY >> 8; // Unlock CS registers CSCTL1 = 0x0005 | DCORSEL; // Set DCO to 7 MHz CSCTL2 = SELM__DCOCLK | SELS__DCOCLK | SELA__VLOCLK; CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // Set all dividers to 1 CSCTL4 = LFXTOFF | HFXTOFF; // Disable external oscillator CSCTL0_H = 0; // Lock CS registers } // SPI Configuration void initSPI() { UCA0CTLW0 = UCSWRST; // **Put state machine in reset** //UCA0CTLW0 |= UCMST | UCSYNC | UCCKPL | UCMSB; // 3-pin, 8-bit SPI master // Clock polarity high, MSB UCA0CTLW0 |= UCMST | UCSYNC | UCMSB; // 3-pin, 8-bit SPI master UCA0CTLW0 |= UCSSEL__SMCLK; // ACLK UCA0BR0 = 0; UCA0BR1 = 0; // UCA0MCTLW = 0; // No modulation UCA0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine** UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt } void spiWriteTest( int data) { P8OUT &= ~BIT5; // Slave Select low while(!(UCA0IFG&UCTXIFG)); UCA0TXBUF = data; // Initiate Transmission P8OUT |= BIT5; // Slave Select high } /** * main.c */ void main(void) { WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer initGPIO(); // Disable the GPIO power-on default high-impedance mode to activate // previously configured port settings PM5CTL0 &= ~LOCKLPM5; initClock(); initSPI(); __bis_SR_register(GIE); while(1) { spiWriteTest(0x7F); // Select register bank control register spiWriteTest(0x80); // Select register bank 0 spiWriteTest(0x00); // Read WHO AM I for(i=1000;i>0;i--); } } #pragma vector=USCI_A0_VECTOR __interrupt void USCI_A0_ISR(void) { while(!(UCA0IFG&UCRXIFG));// USCI_A0 TX buffer ready? ch1 = UCA0RXBUF; }