I am using 2 MSP430F149 processors and they are communicating to each other via SPI USART1. I am having troubles sycronizing the communication without adding delays. See the dalay added below to avoid receiving duplicate data. Do I have to enable the receive interrupt on the master then poll (IFG2 & URXIFG1) ?
Also what is the difference between "(U0TCTL & TXEPT)" and "(U0IFG & UTXIFG0)"? When do you use one vs the other?
Thanks for any help...
Tom
--- #1 Master Processor
void usart0_init()
{
P3SEL &= ~0x30;
P3SEL |=0x0E; //P3.1 (SIMO), P3.2 (SOMI), P3.3 (UCLK)
P3DIR |=0x0B; //P3.0 , P3.1 (SIMO), and P3.3 (UCLK) are outputs
P3DIR &= ~0x04; //P3.2 (SOMI) is an input
U0CTL = SWRST; //Must set SWRST bit 1st before changing
U0CTL = CHAR + SYNC + SWRST; //8-bit char length, SPI mode
U0TCTL = 0; //reset U0TCTL
U0CTL |= MM; //USART is Master
U0TCTL = SSEL1; //Select SMCLK as BRCLK
U0TCTL |= STC; //3-pin SPI mode
P3OUT |= 0x01; //make sure cs line starts high
U0MCTL = 0;
ME1 = USPIE0; //enable the SPI mode for USART0
U0BR0=0x16; //baudrate
U0BR1=0; //baudrate
U0CTL &= ~SWRST; //Reset SWRST bit to activate USART
}
--- #1 Master Processor start RECEIVE ---
if(AUX_PROC_RX_HIGH) //Port Interrupt occurred
{
SLAVE_ENABLE;
for(i=0;i<length;i++)
{
usart0Putch(DUMMY);
while(!(U0TCTL & TXEPT));
brief_pause(100); //WHY is a delay needed?
if(i == 0)
cmd = U0RXBUF;
else
rec[i - 1]=U0RXBUF;
if(!AUX_PROC_RX_HIGH)
{
usart0Putch(DUMMY);
while(!(U0TCTL & TXEPT));
rec[i]=U0RXBUF;
break;
}
}
SLAVE_DISABLE;
*count = i;
}
--- #1 Master Processor end RECEIVE ---
--- #1 Master Processor
int usart0Putch(char ch)
{
while (!(U0IFG & UTXIFG0)) // wait for TX buffer to empty
continue; // also either WDOG() or swap()
TXBUF0 = ch;
return (uint8_t)ch;
}
--- #2 Slave Processor
void qspi0_init()
{
P3SEL |=0x0E; //P3.1 (SIMO), P3.2 (SOMI), P3.3 (UCLK)
P3DIR |=0x0B; //P3.0 , P3.1 (SIMO), and P3.3 (UCLK) are outputs
P3DIR &= ~0x01; //Make STE0 an input
P3SEL |= 0x01; //Activate STE0
P3DIR &= ~0x04; //P3.2 (SOMI) is an input
U0CTL = SWRST; //Must set SWRST bit 1st before changing
U0CTL = CHAR + SYNC + SWRST; //8-bit char length, SPI mode
U0MCTL = 0;
ME1 = USPIE0; //enable the SPI mode for USART0
U0BR0=0x02; //baudrate
U0BR1=0; //baudrate
U0CTL &= ~SWRST; //Reset SWRST bit to activate USART
IE1 |= URXIE0;
}
--- #2 Slave Processor start SEND ---
timerX(1000); //setup timeout
t = usart0Putch(cmd); //send the cmd first
READY_TO_TX; //wake up the main processor with port interrupt
while(timerXDelay)
{
timer = 0;
do
{
t = usart0Getch();
}while((t == -1) && (timer++ < 4000));
if(t != -1)
break;
}
for(i=0;i<length;i++)
{
usart0Putch(send[i]);
timer = 0;
do
{
t = usart0Getch();
}while((t == -1) && (timer++ < 4000));
}
TX_DONE;
--- #2 Processor end SEND ---
--- #2 Slave Processor
int usart0Putch(char ch)
{
long timer;
timer = 0;
while (!(U0IFG & UTXIFG0) && (timer < 10000)) // wait for TX buffer to empty
{
timer++;
}
TXBUF0 = ch;
if(timer == 10000)
return -1;
return (uint8_t)ch;
}
--- #2 Slave Processor
int usart0Getch(void)
{
uint8_t ch;
if (usart0_rx_insert_idx == usart0_rx_extract_idx) // check if character is available
return -1;
ch = usart0_rx_buffer[usart0_rx_extract_idx++]; // get character, bump pointer
usart0_rx_extract_idx %= USART0_RX_BUFFER_SIZE; // limit the pointer
return ch;
}