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.

Using SPI onMSP430G2231

Hello,

I have a incomprehensible problem when I using SPI in 8 bits transfert on Launchpad with G2231:

When I write 8 in USICNT register  to send data, 11 or 12 SCK clock are sent!

I followed the datasheet to configure my device, what have I missed?

I configure the device like this:

USICTL0 |= USIPE5 + USIPE6 + USIPE7 + USIMST + USIOE;  // 3-pin, 8-bit SPI master
USICTL1 |= USIIE;                     // Counter interrupt, flag remains set
USICKCTL = USIDIV_2 + USISSEL_2;      
USICTL0 &= ~USISWRST;                 // USI released for operation

and this is how I send data:

USISR = data_to_send;			
USICNT = 0x08;				//init the counter to shift data
while(USICNT != 0 );

Thank you for help
  • what does this do? USICTL1 |= USIIE;
    Try to leave it out.

    As you only sending byte(s); preferable to use USISRL

    And try to wait for while( IFG flag) instead

    waitspi bit.b #USIIFG,USICTL1 ; wait until done.
    jz waitspi
  • Hi!

    Could you please post your complete code? Sometimes the initializations are located at wrong positions in the code. And as Tony already said, enabling the interrupts isn't a good idea if there is no ISR for it. But this is not visible from your code snippets. Is the GIE also set? Not visible, too. So please provide more of your code.

    Dennis
  • Thank for the fast replies,

    I didn't post all the code because I splited the code in several functions, I did a minimalist version to test only 2 SPI transfert. 

    This is still the same problem even without seting the ISR:

    #include <msp430.h> 
    
    #define	CSN_NRF		(0x0002)
    
    #define CSN_LOW()		P1OUT ^= CSN_NRF
    #define CSN_HIGH()		P1OUT |= CSN_NRF
    
    int main(void) {
        WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
    
        BCSCTL1 = CALBC1_1MHZ;
        DCOCTL = CALDCO_1MHZ;
        BCSCTL2 &= ~(DIVS_3);
    
        P1DIR |= CSN_NRF;	//en sortie
        CSN_HIGH();
    
    //init SPI
        USICTL0 |= USIPE5 + USIPE6 + USIPE7 + USIMST + USIOE;  // 3-pin, 8-bit SPI master
    	USICKCTL = USIDIV_2 + USISSEL_2;
    	USICTL0 &= ~USISWRST;                 // USI released for operation
    
    //send on SPI
    //once
    	CSN_LOW();
    	USISRL = 0xAA;			//write data to send
    	USICNT = 0x08;				//init the counter to shift data
    	while(USICNT != 0 );
    	CSN_HIGH();
    
    //twince
    	CSN_LOW();
    	USISRL = 0x55;			//write data to send
    	USICNT = 0x08;				//init the counter to shift data
    	while(USICNT != 0 );
    
    	CSN_HIGH();
    	while(1);
    
    }
    

    This is the capture of 2 transferts:

    I don't understand why the number of SCK is not 8 for each transfert!

  • change while(USICNT != 0 ) TO while(!(USICTL1&USIIFG));

    When USIIFG = 0 and USICNTx > 0, clock generation is enabled and the master will begin clocking out/in data
  • This

    #define CSN_LOW()   P1OUT ^= CSN_NRF

    is not right for setting the output low. It toggles the output. If it was '0' it becomes '1' and vice versa. For setting the output low, write

    #define CSN_LOW()   P1OUT &= ~CSN_NRF

  • Thanks,

    Now the SPI is working with changing while(USICNT != 0 ); by while(!(USICTL1&USIIFG));

    #include <msp430.h> 
    
    #define	CSN_NRF		(0x0002)
    
    #define CSN_LOW()   	P1OUT &= ~CSN_NRF
    #define CSN_HIGH()		P1OUT |= CSN_NRF
    
    int main(void) {
        WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
    
        BCSCTL1 = CALBC1_1MHZ;
        DCOCTL = CALDCO_1MHZ;
        BCSCTL2 &= ~(DIVS_3);
    
        P1DIR |= CSN_NRF;	//en sortie
        CSN_HIGH();
    
    //init SPI
        USICTL0 |= USIPE5 + USIPE6 + USIPE7 + USIMST + USIOE;  // 3-pin, 8-bit SPI master
    	USICKCTL = USIDIV_2 + USISSEL_2;
    	USICTL0 &= ~USISWRST;                 // USI released for operation
    
    //send on SPI
    //once
    	CSN_LOW();
    	USISRL = 0xAA;			//write data to send
    	USICNT = 0x08;				//init the counter to shift data
    	while(!(USICTL1&USIIFG));
    	CSN_HIGH();
    
    //twince
    	CSN_LOW();
    	USISRL = 0x55;			//write data to send
    	USICNT = 0x08;				//init the counter to shift data
    	while(!(USICTL1&USIIFG));
    
    	CSN_HIGH();
    	while(1);
    
    }
    

**Attention** This is a public forum