This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430F5438A: Sending SPI message from master to slave

Hello,

In the experiment board I want to send SPI message from UCB1 to UCB0.

UCB0 is configured as SPI slave with the following code:

P3SEL |= BIT1;                     // P3.1 UCB0SIMO (Slave in, master out – USCI_B0 SPI mode)
P3SEL |= BIT2;                     // P3.2 UCB0SOMI (Slave out, master in – USCI_B0 SPI mode)
P3SEL |= BIT3;                     // P3.3 UCB0CLK  (Clock in\out)

UCB0CTL1 |= UCSWRST;                                        // **Put state machine in reset**
UCB0CTL0 |= UCSYNC+UCCKPL+UCMSB;           // 3-pin, 8-bit SPI slave, Clock polarity high, MSB
UCB0CTL1 &= ~UCSWRST;                                   // **Initialize USCI state machine**
UCB0IE |= UCRXIE;                                                 // Enable USCI_A0 RX interrupt

UCB1 is configured as SPI master with the following code:

 P3SEL |= BIT7;                       // P3.7 UCB1SIMO (Slave in, master out – USCI_B1 SPI mode)
 P5SEL |= BIT4;                       // P5.4 UCB1SOMI (Slave out, master in – USCI_B1 SPI mode)
 P5SEL |= BIT5;                       // P5.5 UCB1CLK  (Clock in\out)

 UCB1CTL1 |= UCSWRST;                                                 // Put state machine in reset**
 UCB1CTL0 |= UCMST+UCSYNC+UCCKPL+UCMSB;     // 3-pin, 8-bit SPI master, Clock polarity high,MSB
 UCB1CTL1 |= UCSSEL_2;                                                // SMCLK
 UCB1BR0 = 0x02;                                                             // /2
 UCB1BR1 = 0;                                                                  //
 UCB1CTL0 = 0;                                                               // No modulation
 UCB1CTL1 &= ~UCSWRST;                                         // **Initialize USCI state machine**

Then I tried to send one byte from UCB1 to UCB0 with:

while (!(UCB1IFG&UCTXIFG));
UCB1TXBUF = 5;

But the MCU hangs in the "while" line.

The wires on the board are connected in the following way:

Port x,y                             RF3                             

P3.7 (UCB1SIMO)           P3.2 (15, UCB0SOMI)

P5.4 (UCB1SOMI)           P3.1 (18, UCB0SIMO)

P5.5 (UCB1CLK)             P3.3 (16, UCB0CLK)

Can you tell what am I doing wrong ?

Thank you in advance,

Z.V


P5.4Pdklfklfkldfioprio

  • Hello,

    I added the line:
    __delay_cycles (100);

    at the end of the master init.

    When I ran the code, I succeeded to send one byte.
    But then the while hanged.
    The receive interrupt was not called:

    #pragma vector=USCI_B0_VECTOR
    __interrupt void USCI_B0_ISR(void)
    {
    switch(__even_in_range(UCB0IV,4))
    {
    case 0:
    break; // Vector 0 - no interrupt
    case 2: // Vector 2 - RXIFG
    break;
    case 4:
    break; // Vector 4 - TXIFG
    default:
    break;
    }
    }

    Can you help ?

    Thank you,
    Z.V
  • Hello Z.V,

    P3.7 should connect to P3.1 and P5.4 to P3.2. Also initialize UCB1 (master) before UCB0 (slave) so that the master starts the SPI CLK in time for the slave to recognize it upon starting.

    Regards,
    Ryan
  • Hi Bryan, All,

    I connected the SIMO, SOMI lines but I still have the same problem.
    In the following code, UCB0 is master and UCB1 is slave.

    I want to send 100 bytes from master to slave.
    Only the first byte is sent and then the MCU hangs in the line:

    while (!(UCB0IFG & UCTXIFG));

    The ISR:  USCI_B1_ISR is not called.

    Can you help ?

    Best regards,
    Z.V



    #include <msp430f5438a.h> int Recv=0; /************************************************************************************* mcu_to_a2d_init Description: Initialize SPI channel from FPGA to MCU. MCU is slave. Channel: UCB0 *************************************************************************************/ int master_init (void) { P3SEL |= BIT1; // P3.1 UCB0SIMO (Slave in, master out – USCI_B0 SPI mode) P3SEL |= BIT2; // P3.2 UCB0SOMI (Slave out, master in – USCI_B0 SPI mode) P3SEL |= BIT3; // P3.3 UCB0CLK (Clock in\out) UCB0CTL1 |= UCSWRST; // Put state machine in reset** UCB0CTL0 |= UCMST+UCSYNC+UCCKPL+UCMSB; // 3-pin, 8-bit SPI master, Clock polarity high, MSB UCB0CTL1 |= UCSSEL_2; // SMCLK UCB0BR0 = 0x02; // /2 UCB0BR1 = 0; // UCB0CTL0 = 0; // No modulation UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** return 0; } /************************************************************************************* fpga_to_mcu_init Description: Initialize SPI channel from MCU to A2D. MCU is master. Channel: UCB1 *************************************************************************************/ int slave_init (void) { P3SEL |= BIT7; // P3.7 UCB1SIMO (Slave in, master out – USCI_B1 SPI mode) P5SEL |= BIT4; // P5.4 UCB1SOMI (Slave out, master in – USCI_B1 SPI mode) P5SEL |= BIT5; // P5.5 UCB1CLK (Clock in\out) UCB1CTL1 |= UCSWRST; // **Put state machine in reset** UCB1CTL0 |= UCSYNC+UCCKPL+UCMSB; // 3-pin, 8-bit SPI slave, // Clock polarity high, MSB UCB1CTL1 &= ~UCSWRST; // **Initialize USCI state machine** UCB1IE |= UCRXIE; // Enable USCI_B1 RX interrupt return 0; } /*************************************************************************************/ int main (void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer master_init (); slave_init (); __delay_cycles(1000); // Wait for slave to initialize __bis_SR_register(GIE); // Enable interrupts spi_send_msg (); return 0; } /*************************************************************************************/ int spi_send_msg (void) { unsigned char MST_Data; int i; for (i=0;i<100;i++) { while (!(UCB0IFG & UCTXIFG)); UCB0TXBUF = 5; } return 0; } /*************************************************************************************/ #pragma vector=USCI_B1_VECTOR __interrupt void USCI_B1_ISR(void) { char MST_Data; switch(__even_in_range(UCB1IV,4)) { case 0: break; // Vector 0 - no interrupt case 2: // Vector 2 - RXIFG MST_Data = UCB1RXBUF; Recv++; break; case 4: break; // Vector 4 - TXIFG default: break; } } /*************************************************************************************/ #pragma vector=USCI_B0_VECTOR __interrupt void USCI_B0_ISR(void) { char MST_Data; switch(__even_in_range(UCB0IV,4)) { case 0: break; // Vector 0 - no interrupt case 2: // Vector 2 - RXIFG MST_Data = UCB0RXBUF; break; case 4: break; // Vector 4 - TXIFG default: break; } }

  • In line 20 you completely clear out your USCI_B SPI mode settings. Delete or comment that line to get your code working.

    Regards,
    Ryan
  • Hi Ryan,

    Thank you very much !

    Now it works.

    Best regards,

    Z.V

**Attention** This is a public forum