kaipyroami 1 Posted April 9, 2012 Share Posted April 9, 2012 I am trying to get some SPI test code working. I want to send a 16 bit value to the Launch pad and have it return a predetermined 16 bit value. For example: Send 0x00F1 and receive 0x0035 from the Launchpad. I am currently only getting 0x0000 back and 0x00F1 or 0x00F0 depending on what is sent. It appears to be only echoing the values sent back to me instead of sending the correct response. Thanks! #include #include // Intrinsic functions #include // Integers of defined sizes volatile unsigned int SERVO_0 = 0x0045; volatile unsigned int SERVO_1 = 0x0035; void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer //Load 1Mhz Factory Calibrated Values. BCSCTL1 = CALBC1_1MHZ; // Set range DCOCTL = CALDCO_1MHZ; // Set DCO step and modulation USICTL0 |= USIPE7 + USIPE6 + USIPE5 + USIOE; // Port, SPI slave USICTL1 |= USIIE + USICKPH; // Counter interrupt, flag remains set USICTL0 &= ~USISWRST; // USI released for operation USISR = 0x0000; // init-load data, USISR-USI Shift Register, where the data is kept USICNT = USI16B | 16; // init-load counter, USICNT-USI Bit Count Register, number of bits to be sent while(1)//loop { _BIS_SR(GIE); // interrupt enable __low_power_mode_3(); // Only ACLK continues to run } } // *****************************USI interrupt service routine******************** #pragma vector=USI_VECTOR __interrupt void universal_serial_interface(void) { if (0xF0 == USISRL) //return values of servo signal from requested servo { USISR = SERVO_0; USICNT = USI16B | 16; } if (0xF1 == USISRL) { USISR = SERVO_1; USICNT = USI16B | 16; } else { USISR = 0x0000; USICNT = USI16B | 16; } } Quote Link to post Share on other sites
cde 334 Posted April 10, 2012 Share Posted April 10, 2012 So you have a master AND a slave msp430, or something else? What are you using as a master? kaipyroami 1 Quote Link to post Share on other sites
kaipyroami 1 Posted April 11, 2012 Author Share Posted April 11, 2012 I am currently using a buspirate as my SPI master. I am attempting to send a value to the MSP and have it (as the slave) return a value to me. Quote Link to post Share on other sites
Rickta59 589 Posted April 11, 2012 Share Posted April 11, 2012 Is the buspirate sending 16 bits of data? Is it set to send lsb or msb? Have you used the debugger to look at the value you are receiving? What SPI clock rate is the BP using? kaipyroami 1 Quote Link to post Share on other sites
kaipyroami 1 Posted April 11, 2012 Author Share Posted April 11, 2012 Currently I am sending two bytes consecutively, MSB. The clock is 400KHz. I am reading the correct received value on USISR with the debugger. I am thinking the problem lies in the processing of the input data and returning the value. Quote Link to post Share on other sites
oPossum 1,083 Posted April 11, 2012 Share Posted April 11, 2012 Not sure exactly what is wrong. Give this a try... // *****************************USI interrupt service routine******************** #pragma vector=USI_VECTOR __interrupt void universal_serial_interface(void) { switch(USISRL) { case 0xF0: USISR = SERVO_0; break; case 0xF1: USISR = SERVO_1; break; default: USISR = 0x0000; break; } USICNT = USI16B | 16; } The structure of this code ensures that the data latch is only read once, and the bit count is only set once. kaipyroami 1 Quote Link to post Share on other sites
cde 334 Posted April 11, 2012 Share Posted April 11, 2012 Could the msp not be processing fast enough? If the master is clocking the spi line before the msp has processed the data and loaded the usi buffer (same buffer for in and out, right?), the msp is just sending back whatever is in the buffer? kaipyroami 1 Quote Link to post Share on other sites
kaipyroami 1 Posted April 11, 2012 Author Share Posted April 11, 2012 Could the msp not be processing fast enough? If the master is clocking the spi line before the msp has processed the data and loaded the usi buffer (same buffer for in and out, right?), the msp is just sending back whatever is in the buffer? you might be on to something... oPossum: I am going to try your code tonight. Quote Link to post Share on other sites
Rickta59 589 Posted April 11, 2012 Share Posted April 11, 2012 Currently I am sending two bytes consecutively, MSB. The clock is 400KHz.I am reading the correct received value on USISR with the debugger. I am thinking the problem lies in the processing of the input data and returning the value. I think you need to send 4 bytes. The first 16 bits are shifted out when you send 2 bytes from the bus pirate. That returns your initial 0x0000 value. The next 16 bits will be returned when you send another 16 bits. Typically people use a dummy byte of 0xFF. You want to transfer 16 bits so you need to send 0xFFFF. -rick kaipyroami 1 Quote Link to post Share on other sites
kaipyroami 1 Posted April 18, 2012 Author Share Posted April 18, 2012 I was not waiting for the data to be loaded into the register. Thanks! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.