1 year ago
#229630
Erik
MSP430F5137 SPI-UART Bridge
I am working on a SPI-UART bridge connection with an MSP430F5133 to RHD2132. The code was compiled using Code Compose Studio. This is the current code I have but I am still having issues
#include <msp430.h>
#include <stdint.h>
uint16_t convert_intan(uint8_t channel); // Function that receives 16 bit
uint16_t transfer_16_bit_MOSI(uint16_t command); // Function that recieves data
void calibrate_intan(void); // Function that send calibrate command
void initialize_clk(void); // Clock initialize function
void initialize_pin(void); // Pin initialize function
int main(void)
{
uint8_t j =0; // Shifting variable
uint16_t data[99]; // Values stored from transfer
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
initialize_pin(); // Initialize pin configuration
initialize_clk(); // Initialize clock configuration
calibrate_intan(); // Calibration
data[j] = convert_intan(0); // Initial data transfer
while(1)
{
data[j] = convert_intan(63);
j++;
if(j == 31)
{j = 0;}
}
return 0;
}
void initialize_clk(void)
{
UCSCTL3 |= SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_5; // Select DCO range 16MHz operation
UCSCTL2 = FLLD_1 + 249; // Set DCO Multiplier for 8MHz
// (N + 1) * FLLRef = Fdco
// (249 + 1) * 32768 = 8192000Hz
// Set FLL Div = fDCOCLK/2
__bic_SR_register(SCG0); // Enable the FLL control loop
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x03; // 32k/9600 - 3.41
UCA0BR1 = 0x00; //
UCA0MCTL = 0x06; // Modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE + UCTXIE; // enable interrupt flags
UCB0CTL1 |= UCSWRST; // Set in reset state so that config can be added
UCB0CTL0 = UCMST+UCSYNC+UCCKPL+UCMSB; // 3-pin, 8-bit SPI master
UCB0CTL1 = UCSSEL_2; // SMCLK
UCB0BR0 = 0x02; // SPI same clock as SMCLK/1 (8Mhz)
UCB0BR1 = 0x00; //
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** Effectively starting SPI communication, now only need to pull CS low when sending data
UCB0IE |= UCRXIE + UCTXIE; // enable interrupt flags
do
{
UCSCTL7 &= ~(XT1LFOFFG + DCOFFG); // Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
__delay_cycles(100000); // Delay for Osc to stabilize
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
}
void initialize_pin(void)
{
PMAPPWD = 0x02D52; // Get write-access to port mapping register
P1MAP2 = PM_UCB0SOMI; // Map UCB0SOMI output to P1.2
P1MAP3 = PM_UCB0SIMO; // Map UCB0SIMO output to P1.3
P1MAP4 = PM_UCB0CLK; // Map UCB0CLK output to P1.4
P2MAP6 = PM_UCA0RXD; // Map UCA0RXD output to P2.6
P2MAP7 = PM_UCA0TXD; // Map UCA0TXD output to P2.7
PMAPPWD = 0; // Lock port mapping registers
P1DIR |= BIT0; // Set as output pin [1.4:CS]
P1DIR |= BIT2 + BIT3 + BIT4; // Set UCIB_0 [1.2:SIMO][1.3:MOSI][1.4:SCLK]
P2DIR |= BIT7; // Set as TX output [2.7:DMAE0]
P3DIR |= BIT7; // Set as output [3.7:SMCLK]
P1OUT &= ~BIT0; // Sets CS high
P1SEL |= BIT2 + BIT3 + BIT4; // Enable SPI function
P2SEL |= BIT6 + BIT7; // Enable UART function
P3SEL |= BIT7; // Enable SPI CLK
P5SEL |= 0x03; // Enable XT1 pins
}
void calibrate_intan(void)
{
uint16_t result,temp;
uint8_t m =9;
result = transfer_16_bit_MOSI(0x5500); // Calibrate on datasheet is command: (0101 0101 0000 0000) which is equivalent to 0x5500
for(; m > 0; m--) // Sends 9 dummy commands to complete calibration cycle
{temp = transfer_16_bit_MOSI(0x0010);}
}
uint16_t convert_intan(uint8_t channel)
{
uint16_t command; // 16 bit senting
uint8_t static const convert_mask = 0x00; // Mask to write to keep "00" at MSBits
command = (((convert_mask | channel) << 8 ) | (0x00)); // Creates command
return transfer_16_bit_MOSI(command);
}
uint16_t transfer_16_bit_MOSI(uint16_t command)
{
uint8_t hi = ((command>>8)&0xff);
uint8_t lo = ((command>>0)&0xff);
uint8_t bytes_RX_buffer[2]; // Stores received data
P1OUT |= BIT0; // Sets CS low
while (!(UCB0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
{UCB0TXBUF = hi;} // Send first 8 bits of command over SPI to Slave
while (!(UCB0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
{UCB0TXBUF = lo;} // Send last 8 bits of command over SPI to Slave
P1OUT &= ~BIT0; // Sets CS high
_delay_cycles(15);
P1OUT |= BIT0; // Sets CS low
while (!(UCB0IFG&UCRXIFG)); // USCI_B0 RX Received?
{bytes_RX_buffer[1] = UCB0RXBUF;} // Store received data
while (!(UCB0IFG&UCRXIFG)); // USCI_B0 RX Received?
{bytes_RX_buffer[0] = UCB0RXBUF;} // Store received data
P1OUT &= ~BIT0; // Sets CS high
while (!(UCA0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
{UCA0TXBUF = bytes_RX_buffer[0];} // Send first 8 bits of command over UART
while (!(UCA0IFG&UCTXIFG)); // USCI_B0 TX buffer ready?
{UCA0TXBUF = bytes_RX_buffer[1];} // Send last 8 bits of command over UART
return ((bytes_RX_buffer[1] << 8) | bytes_RX_buffer[0]);
}
The rx and tx buffers require 8 bits hence the conversions and currently I am looking to obtain a frequency of 8Mhz for the SPI sclk. I am not sure if it is hardware or software, the code compiles but I don't receive data from the slave.
c++
uart
spi
msp430
code-composer
0 Answers
Your Answer