Part Number: MSP430FR5969
Tool/software: Code Composer Studio
Hello everyone,
I'm using USCI_A1 SPI module to communicate with a slave. The problem fell in reading routine. I know that to receive a byte, the master must send a dummy byte for the module to actually activate the clock. The transmitting routine was successful, and the signals monitored by oscilloscope showed that the slave gave a byte back. But the data read from UCA1RXBUF was always 0x00. I don't know what is wrong with my program. So, please help me have a look at the following codes and I hope you can tell me where I got wrong.
//Variable declaration
static struct {
bool bPortInUse;
bool bNewDataReceived;
unsigned int indexRx;
unsigned int indexTx;
unsigned int uiBytesToSend;
uint8_t *pcRxBuffer;
uint8_t *pcTxBuffer;
} spiSM;
//This function is to transmit/receive data from the slave
//rxBuf, txBuf: pointers of receiving/transmitting data, respectively
//size: number of bytes to be received/transmitted
bool spi_transaction(uint8_t* rxBuf, uint8_t* txBuf, uint16_t size) {
spiSM.bNewDataReceived = false;
spiSM.indexRx = 0;
spiSM.indexTx = 0;
spiSM.uiBytesToSend = size;
spiSM.pcRxBuffer = rxBuf; // pcRxBuffer points to the receiving buffer
spiSM.pcTxBuffer = txBuf; // pcTxBuffer points to the transmitting buffer
SPI_En; //SPI disabled (UCA1CTLW0 &= ~UCSWRST)
UCA1IE |= UCTXIE | UCRXIE; // interrupts enabled but interrupt flags still = 0 because GIE is not set
do {
// Reset receive flag
spiSM.bNewDataReceived = false;
// Start transmission
TXBUF_Temp = spiSM.pcTxBuffer[spiSM.indexTx];
// Sleep until transmit/receive interrupt flags occur
__bis_SR_register(LPM3_bits|GIE);
// Move to next TX and RX index
spiSM.indexTx++;
spiSM.indexRx++;
spiSM.uiBytesToSend--;
} while(spiSM.uiBytesToSend); // repeat until receive all desired bytes
SPI_Dis; //SPI disabled (UCA1CTLW0 |= UCSWRST)
return true;
}
#pragma vector=USCI_A1_VECTOR
__interrupt void SPI_ISR(void) {
if(UCRXIFG){
spiSM.pcRxBuffer[spiSM.indexRx] = UCA1RXBUF;
spiSM.bNewDataReceived = true;
UCA1IFG &= ~UCRXIFG; // Not sure it is vital to write this command or not but just as being punctilious
__bic_SR_register_on_exit(LPM4_bits);
}
if(UCTXIFG){
UCA1TXBUF = TXBUF_Temp;
UCA1IFG &= ~UCTXIFG;
__bic_SR_register_on_exit(LPM3_bits|GIE); // just added recently
}
}
Here are the signals monitored on an oscilloscope. In this case I want to read the slave's FIFO status register. The reading routine is as the following picture.The green one is MOSI, the red one is CLK, and the blue one is MISO. It showed that the slave returned a data #0 (at the second 8-bit clock) after the master transmitted the command code, but UCA1RXBUF returned 0.
Thank you so much and look forward to your support.

