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.

Problem Using LoopBack mode with SPI

Other Parts Discussed in Thread: MSP430G2553

I am trying to verify that my SPI has been configured properly and that my receive interrupts are working. For some reason I can't get it to receive the byte that I send. I have enabled the loopback mode but it doesn't seem like it is actually receiving anything that I send. I am using an MSP430g2553

Here is my code: 

#include <msp430.h>				

void initSPI_USCIA(void);							//initializes SPI on USCIA0 for the sd card reader/writer
//void initI2C_USCIB(void);							//initializes I2C on USCIB0 for the ADC from ASI
void initLED(void);									//initializes led so we can toggle it
__interrupt void USCI0RX_ISR (void);

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


	while (!(IFG2 & UCA0TXIFG)){
		//wait for ready to transmit
	}
	UCA0TXBUF = 0x11;									//writes to spi data register

	_BIS_SR(LPM4_bits + GIE); 							// Enter LPM4 w/interrupts enabled

}



void initSPI_USCIA(void){
	UCA0CTL1 |= UCSWRST; 							//set software reset to 1 so we can configure SPI

	UCA0STAT |= UCLISTEN;							//the launchpad listens to itself (use for testing)
	UCA0CTL0 |= UCCKPH + UCMSB + UCMST + UCSYNC;	//clkphase,clkpolarity,MSBfirst,8-bit,master mode, 3-pin, synchronous SPI
	UCA0CTL1 |= UCSSEL_2;                   		//choose SMCLK as our clock. SMCLK is 1.1 MHz. We divise it with the baud register
	UCA0BR0 |= 0x02;                       			//Baud rate is 1.1MHZ/(2+1) = 366 KHz
	UCA0BR1 = 0;                            		//upper byte of br register. set to zero
	UCA0MCTL = 0;                           		//No modulation

	P1SEL |= (BIT1 | BIT2 | BIT4);			//configures pins for three pin SPI. MISO P1.1, MOSI P1.2, CLK P1.4
	P1SEL2 |= (BIT1 | BIT2 | BIT4);			//configures pins for their peripheral setting

	UCA0CTL1 &= !UCSWRST;                   //enables the SPI to work

	IE2 |= UCA0RXIE;                        // Enable USCI0 RX interrupt
}

void initLED(void){
		 P1DIR |= BIT0;                     //sets red led to output and turns it on
		 P1OUT &= !BIT0;				    //turn red led off
}

#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR (void){
	if(UCA0RXBUF == 0x11)
	{
		P1OUT = 0x01;				    //turn red led on
	}


}

  • Hi Erik,

    Could this be the problem:

    UCA0CTL1 &= !UCSWRST; //enables the SPI to work

    This should be

    UCA0CTL1 &= ~UCSWRST; //enables the SPI to work

    The ! operator will do a NOT on the value as a whole - so anything that is >=1 will become 0 I believe. That means you are ANDing the entire UCA0CTL1 register with 0 and completely zeroing out the register I suspect (meaning you lose your SPI setting). You could check this with the debugger  single-stepping and viewing the register change to see that the whole register is cleared rather than just the bit for UCSWRST.

    Using ~ instead is the bit-wise NOT so it will bit by bit flip every bit, instead of taking the value as a whole and flipping the whole value to 0 or 1. So using it with the bit-wise AND will cause only the UCSWRST bit to be cleared and retain the state of the rest of the bits in the register.

    It looks like you should probably correct the same ! vs ~ issue in a couple of other places in the code too (like  P1OUT &= !BIT0; should be P1OUT &= ~BIT0; or else you'll clear all the other P1OUT bits/pins too)

    Hope this helps get you on the right track!

    Regards,

    Katie

  • Yup that was the problem! Works fine now. Thank you so much Katie! :)

    - Erik 

**Attention** This is a public forum