I'm using the MSP430G2553 (on the Launchpad) and trying to use it as a SPI<=>UART bridge,
tunneling all data traffic.
This works so far, my problem is that upon receiving a UART character the ISR also enters
the loop for sending a UART char, even if the condition if(IFG2 & UCB0RXIFG) should not be fullfilled.
So in my Windows terminal i receive a "0" character upon sending a uart character.
There is nothing connected to the Launchpad, so no reason a SPI receive should be fired.
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void) {
P1OUT |= BIT0; // LED on
if (IFG2 & UCA0RXIFG) { // UART Character received
while (!(IFG2 & UCB0TXIFG)); // Wait for SPI buffer ready
UCB0TXBUF = UCA0RXBUF; // Send Character to SPI
IFG2 &= ~UCA0RXIFG; // clear UART-Receive Flag
}
if (IFG2 & UCB0RXIFG) { // SPI Character received
while (!(IFG2 & UCA0TXIFG)); // Wait for UART buffer ready
UCA0TXBUF = UCB0RXBUF; // Send Character to UART
IFG2 &= ~UCB0RXIFG; // clear SPI-Receive Flag
}
P1OUT &= ~BIT0; // LED off
}
full code incl UART/SPI setup here:
#include "msp430g2553.h"
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
// UART:
P1DIR |= BIT0; // Set the LED on P1.0 as output
// SPI:
P1DIR |= BIT3; // Set P1.3 as "reset slave"
P1OUT = 0x00;
// UART set DCO (1MHz):
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
// UART + SPI config of P1:
// P1.1 = UART_RX, P1.2 = UART_TX, P1.4 = SPI_STE, P1.5 = SPI_CLK, P1.6 = SPI_SOMI, P1.7 = SPI_SIMO
P1SEL = BIT1+BIT2+BIT4+BIT5+BIT6+BIT7;
P1SEL2 = BIT1+BIT2+BIT4+BIT5+BIT6+BIT7;
// UART Setup (9600-8N1):
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz/9600 = 104.., lower 8 bit (UCA0BR = 16bit long)
UCA0BR1 = 0; // 1MHz/9600 = 104.., higher 8 bit
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
// SPI Setup (9600), 3-pin, 8-bit SPI master:
UCB0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC;
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 = 104; // 1MHz/9600 = 104.., lower 8 bit (UCB0BR = 16bit long)
UCB0BR1 = 0; // 1MHz/9600 = 104.., higher 8 bit
//UCA0MCTL = 0; // No modulation
UCB0CTL1 &= ~UCSWRST; // Start USCI - SPI
UCA0CTL1 &= ~UCSWRST; // Start USCI - UART
IE2 |= UCA0RXIE + UCB0RXIE; // Enable USCI_A0+B0 RX interrupt
P1OUT &= ~BIT3; // Now with SPI signals initialized,
P1OUT |= BIT3; // reset slave
__delay_cycles(75); // Wait for slave to initialize
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void) {
P1OUT |= BIT0; // LED on
if (IFG2 & UCA0RXIFG) { // UART Character received
while (!(IFG2 & UCB0TXIFG)); // Wait for SPI buffer ready
UCB0TXBUF = UCA0RXBUF; // Send Character to SPI
IFG2 &= ~UCA0RXIFG; // clear UART-Receive Flag
}
if (IFG2 & UCB0RXIFG) { // SPI Character received
while (!(IFG2 & UCA0TXIFG)); // Wait for UART buffer ready
UCA0TXBUF = UCB0RXBUF; // Send Character to UART
IFG2 &= ~UCB0RXIFG; // clear UART-Receive Flag
}
P1OUT &= ~BIT0; // LED off
}