Can anyone please help with how I can interface MSP430G2553 with Winbond SPI (W25Q80BV)? I am trying to use the USCI. I've tried on my own and have also tried referencing examples online but whenever I run in Debug mode the variable I defined to store the value coming back from the SPI just reads 255 '\xff'. Does anyone have working code that can help?
Here is the code I am using so far (keep in mind it is really not optimized as I just want to get something working):
#include "msp430g2553.h";
unsigned char addressValue;
/*
* main.c
*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
initSPI();
addressValue = read(0x00);
while(1);
return 0;
}
// Configure MSP430 for SPI
/**
* Using UCA0
*
* MSP_PIN 1.1 = SPI_PIN 2 (Data Out)
* MSP_PIN 1.2 = SPI_PIN 5 (Data In)
* MSP_PIN 1.4 = SPI_PIN 6 (CLK)
* MSP_PIN 1.5 = SPI_PIN 1 (CS)
*
*/
void initSPI() {
// PIN Setup
P1OUT |= BIT5;
P1DIR |= BIT5;
P1SEL = BIT1 | BIT2 | BIT4;
P1SEL2 = BIT1 | BIT2 | BIT4;
/**
* Setup the UCACTL (UCA Control) Registers
* This sets the microcontroller to use SPI interface
*/
UCA0CTL1 = UCSWRST; // Reset mode
UCA0CTL0 |= UCCKPH + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI Master (Most sig bit first)
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 |= 0x02; // /2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
}
unsigned char read(unsigned char regAddress) {
// CS High
//P1OUT |= BIT5;
// CS Low
P1OUT &= ~BIT5;
/**
* Send Read command to SPI
*/
while(!(IFG2 & UCA0TXIFG)); // TX BUFFER READY?
UCA0TXBUF = 0x03; // Tells SPI we want to do a read (03h)
/**
* Send Address where you wish to get data
*/
while(!(IFG2 & UCA0TXIFG)); // TX BUFFER READY?
UCA0TXBUF = regAddress;
int count;
/**
* Send dummy bits
*
*/
for(count = 0; count < 7; count++) {
while(!(IFG2 & UCA0TXIFG)); // TX BUFFER READY?
UCA0TXBUF = 0x00;
}
unsigned char result;
/**
* Read value from the SPI
*
*/
while(!(IFG2 & UCA0RXIFG)); // RX BUFFER READY?
result = UCA0RXBUF;
// Raise CS to Deselect drive
P1OUT |= BIT5;
return result;
}
// ************************* END CODE
Here is the SPEC for the Winbond SPI https://cdn-shop.adafruit.com/datasheets/W25Q80BV.pdf
I took this next screenshot from the spec as it explains the SPI read process:
Finally - Here is a part of what I have stored on the SPI. My goal in the long run is to try and start with the first bit and read in each bit until I get to a certain length.