Hi,
I'm trying to establish an SPI communication between the 2553 (master) and the 2231 (slave) in order to be able to toggle the P1.0 LED on the slave.
I've found some code provided by TI, so most of what will follow has been inspired by them.
Since I don't have several slaves, I only use : MOSI and CLK.
I actually made it work when both launchpads are plugged in to the computer. But when 1 of them is plugged to another computer or on a battery, the SPI communication stops (fails).
I assume it was a ground issue, so I connected the ground from one to the ground of the other one, but still the communication failed even when both are plugged to the same computer...
It's been a day of debugging and I can't find any solution.
Can someone help me please?
Here's the code :
MASTER :
void init_SPI(void) {
P1DIR |= BIT5;
P1SEL |= BIT5 + BIT7;
P1SEL2 |= BIT5 + BIT7;
UCB0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC;
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 |= 0x02; // /2
UCB0BR1 = 0; //
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
__delay_cycles(75); // Wait for slave to initialize
}
void init_timer_1s(void)
{
TA0CTL = TASSEL_2 | ID_3 | MC_3;
TA0CCR0 = 0xFFFF;
}
void timer_wait(void)
{
TA0R = 0x00;
while (!(TA0CTL & TAIFG))
;
TA0CTL &= ~TAIFG;
}
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1Mhz
DCOCTL = CALDCO_1MHZ; // Set DCO to 1Mhz
init_SPI();
init_timer_1s();
__enable_interrupt();
while (1) {
while (!(IFG2 & UCB0TXIFG))
;
UCB0TXBUF = data;
timer_wait();
while (!(IFG2 & UCB0TXIFG))
;
UCB0TXBUF = data;
timer_wait();
}
}
SLAVE :
void init_SPI(void)
{
USICTL0 |= USIPE7 + USIPE6 + USIPE5 + USIOE; /* Port, SPI slave */
USICTL1 |= USIIE; /* Counter interrupt, flag remains set */
USICTL0 &= ~USISWRST; /* USI released for operation */
USISRL = P1IN; /* init-load data */
USICNT = 8; /* init-load counter : nb de bits a recevoir */
}
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; /* stop watchdog timer */
init_SPI();
P1DIR |= 0x01;
__enable_interrupt();
while (1)
{
//
}
}
#pragma vector=USI_VECTOR
__interrupt void universal_serial_interface(void)
{
switch (USISRL)
{
case 0xA1:
P1OUT |= 0x01;
break;
case 0xB1:
P1OUT &= ~0x01;
break;
}
USISRL = P1IN; // re-load data
USICNT = 8; // re-load counter