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.

[参考译文] MSP-EXP430G2ET:建立 SPI 通信

Guru**** 2455360 points
Other Parts Discussed in Thread: DRV8711, BOOST-DRV8711

请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/1459546/msp-exp430g2et-establishing-spi-communication

器件型号:MSP-EXP430G2ET
主题中讨论的其他器件:BOOST-DRV8711、DRV8711

工具与软件:

您好!

好的、我目前正在尝试 在 msp430G2 LaunchPad 和  BOOST-DRV8711之间建立 SPI 通信。 数据正从 msp430G2 (主器件)中发送、这是正确的、但当我尝试从 drv8711 IC (从器件)中读取数据时、不会读取任何数据。  

您能回顾一下我的规范并指导我走向正确的方向吗? 另外、如果您需要任何进一步的文档、请告诉我。  

以下 是用于建立 SPI 通信的当前代码:

#include <msp430.h>
#include <stdint.h>

#define DUMMY   0x0000

#define SLAVE_CS_OUT    P2OUT
#define SLAVE_CS_DIR    P2DIR
#define SLAVE_CS_PIN    BIT0

//*********************************
// General SPI State Machine*******
//*********************************

typedef enum SPI_ModeEnum{
    IDLE_MODE,
    TX_REG_ADDRESS_MODE,
    RX_REG_ADDRESS_MODE,
    TX_DATA_MODE,
    RX_DATA_MODE,
    TIMEOUT_MODE
} SPI_Mode;

/* Used to track the state of the software state machine*/
SPI_Mode MasterMode = IDLE_MODE;

uint8_t SendUCA0Data(uint8_t val) {
    while (!(IFG2 & UCA0TXIFG));  // Wait for TX buffer readiness
    UCA0TXBUF = val;              // Send the data byte
    while (!(IFG2 & UCA0RXIFG));  // Wait for RX buffer to fill
    return UCA0RXBUF;             // Return received data byte
}

SPI_Mode DRV8711_Write(uint8_t reg_addr, uint16_t data){
    MasterMode = TX_REG_ADDRESS_MODE;

    uint16_t command = (0 << 15) | ((reg_addr & 0x07) << 12) | (data & 0x0FFF);  // Construct 16-bit command

    SLAVE_CS_OUT |= SLAVE_CS_PIN;     // Activate the slave (CS high)

    SendUCA0Data((command >> 8) & 0xFF);  // Send upper 8 bits
    SendUCA0Data(command & 0xFF);         // Send lower 8 bits

    //__bis_SR_register(CPUOFF + GIE);              // Enter LPM0 w/ interrupts

    SLAVE_CS_OUT &= ~SLAVE_CS_PIN;    // Deactivate the slave (CS low)

    return MasterMode;
}

uint16_t DRV8711_Read(uint8_t reg_addr) {
    uint16_t command = (1 << 15) | ((reg_addr & 0x07) << 12);  // Construct 16-bit command for read
    uint16_t received_data = 0;

    SLAVE_CS_OUT |= SLAVE_CS_PIN;     // Activate the slave (CS high)

    // Send the command (Read request)
    SendUCA0Data((command >> 8) & 0xFF);  // Send upper 8 bits
    SendUCA0Data(command & 0xFF);         // Send lower 8 bits

    // Receive the data
    received_data = ((uint16_t)SendUCA0Data(DUMMY) << 8);  // Send dummy and receive upper 8 bits
    received_data |= SendUCA0Data(DUMMY);                 // Send dummy and receive lower 8 bits

    SLAVE_CS_OUT &= ~SLAVE_CS_PIN;    // deactivate the slave (CS low)

    return received_data & 0x0FFF;  // Return only the 12 data bits
}


//*******************************
// Device Initialization ********
//*******************************

// calibrating the clock to 16MHz
void initClockTo16MHz()
{
    if (CALBC1_16MHZ==0xFF)                   // If calibration constant erased
    {
        while(1);                             // do not load, trap CPU!!
    }
    DCOCTL = 0;                               // Select lowest DCOx and MODx settings
    BCSCTL1 = CALBC1_16MHZ;                   // Set DCO
    DCOCTL = CALDCO_16MHZ;
}

void initGPIO() {
    // Configure GPIO for SPI
    P1SEL = BIT1 + BIT2 + BIT4;  // Assign SPI pins
    P1SEL2 = BIT1 + BIT2 + BIT4; // pins 1.1(MISO), 1.2(MOSI), and 1.4(CLK)

    // Configure chip select pin
    SLAVE_CS_DIR |= SLAVE_CS_PIN;   // GPIO pin 2.0 is CS
    SLAVE_CS_OUT &= ~SLAVE_CS_PIN;  // CS low by default
}

void initSPI() {
    UCA0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC;  // SPI master, MSB first, synchronous

    UCA0CTL1 |= UCSSEL_2;                     // Use SMCLK
    UCA0BR0 = 0x03;                           // SPI clock divider ~ 5.3MHz
    UCA0BR1 = 0;
    UCA0MCTL = 0;                             // No modulation must be cleared for SPI

    UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**

}


//*****************************
// Main ***********************
// Sends messages *************
//*****************************
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;  // Stop watchdog timer

    initClockTo16MHz(); // calibrate clock to 16MHz
    initGPIO();         // initiate the GPIO pins
    initSPI();          // initiate SPI commands

    //Write to a DRV8711 register
    uint8_t reg_addr = 0x00;    // register 0x00
    uint16_t write_data = 0xC39;  //data transmitted 
    DRV8711_Write(reg_addr, write_data);

    reg_addr = 0x01;    // register 0x01
    write_data = 0xA13;  // data transmitted 
    DRV8711_Write(reg_addr, write_data);

    //Read from a DRV8711 register
    uint8_t reg_addr = 0x07;    // register 0x07
    uint16_t read_data = DRV8711_Read(reg_addr);


}

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    查看 Boost-DRV8711用户指南(SLVU967A)图3、 G2 Launchpad 连接显示为:

    SCS -> P2.3  

    SCK -> P1.5  (UCB0CLK)

    SDO -> P1.6  (UCB0SOMI)

    SDI  -> P1.7  (UCB0SIMO)

    因此、您应该使用 UCB0、而不是 UCA0。

    ---

    > #define SLAVE_CS_PIN BIT0

    要使 SCS=P2.3、这个值应为

    > #define SLAVE_CS_PIN BIT3  // SCS -> P2.3

    ---

    > SendUCA0Data ((命令>> 8)& 0xFF);//发送高8位
    > SendUCA0Data (命令& 0xFF);//发送低8位

    DRV8711_READ ()发送16位命令、然后发送16位虚拟。 通过查看 DRV8711数据表(SLVSC40H)图18、响应位是在第一个(1+3)请求位之后开始的、因此只需要前16位。 这可能如下所示:

    > Received_data =(SendUCB0Data ((命令>> 8)& 0xFF)<< 8);//发送较高的8位并返回高位
    > Received_data |=(SendUCB0Data (命令和0xFF)<< 0);//发送低8位并返回低位

    (完全不进行虚拟交换)。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    好的、我将实施这个。  

    如果我在引脚之间使用跳线电缆(跳线电缆~ 12cm)、您认为信号会拾取不必要的噪声吗?  

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您将使用哪种电机? 我记得有刷电机可能有很高的噪声。

    特别是对于 SPI:我在6MHz 看到过6英寸电缆的拖尾(传播延迟伪影)。  尝试 稍微降低 SPI 的速度(可能为1或2MHz)。 这些是短暂的事务、因此 SPI 速度可能不是实际问题。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我使用的是步进电机。 好的、我要更改为较低的频率、看看它是如何工作的。  

    感谢你的帮助。