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.

DRV8704EVM: Sample code to use with MSP430 Launchpad

Part Number: DRV8704EVM
Other Parts Discussed in Thread: BOOST-DRV8711, ENERGIA, DRV8711, DRV8704

Hi,

I'm trying to communicate with the DRV8704EVM via SPI with a MSP430 Launchpad.  However, the response from my SPI transfer is 0xFFFF no matter to what register I try to read from. 

Is there a sample code to communicate between the DRV8704EVM and a MSP430 LaunchPad?

Thank you,

Roberto

  • Hi Roberto,

    Can you provide a scope capture of the SCS, SCLK, SDATI, and SDATO signals during a SPI transaction?

  • Hi Roberto,

    Similar SPI code can be found at  

    This code is for the BOOST-DRV8711, which has a similar SPI interface.

  • Hi Rick, 

    Thank you for the pointer to the BOOS-DRV8711 firmware. However, that code is for CCS, is there sample code for using Energia instead?

    Thank you,

    Roberto 

  • Hi Roberto,

    We do not have Energia examples for the DRV8711. As you pointed out, the code is for CCS.

    I suggest performing an online search for example Energia SPI code.

  • Hi Rick,

    I've looked at Energia SPI sample code and was not able to read from the register properly. This are my singnal captures (sorry that they are split into multiple images only have a 2 channel oscilloscope). I'm trying to read from the TORQUE Register. 

  • Hi Roberto,

    No need to be sorry about multiple images. You provided the information needed to move forward.

    There are two issues to address from the images:

    1) The MOSI signal does not appear to correct for a read of the TORQUE register (Address 0x01h). This appears to be a read of Address 0x07h since the first 4 bits are high.

    2) The SCLK timing may be too fast for the SDATO pullup resistor. Notice the SDATO signal is approximately 50% of the high value.

    Please try the following:

    1) Reduce the SCLK frequency to 1/2 the current frequency to allow the pullup to rise to a valid logic high.

    2) Try reading the DRIVE (0x06h) register instead of the TORQUE register. The pattern is easier to determine if it is correct. The data read back on MISO should be 0xFA5h

  • Hi Rick,

    Setting the serial clock to 4 MHz solved the SPI communication issue.  I can now read/write from/to the DRV8704 using code in ENERGIA. 

    Attached is the code I'm using for reference for others. 

      
    #include <SPI.h>
    
    // DRV8704 Pin definition for MSP430 LaunchPad
    // most launchpads have a red LED
    #define LED RED_LED
    #define scsPin P8_1
    #define sleepPin P6_6
    
    // DRV8704 Register Address
    const int CTRL = 0x00;
    const int TORQUE = 0x10;
    const int OFF = 0x20;
    const int BLANK = 0x30;
    const int DECAY = 0x40;
    const int DRIVE = 0x60;
    const int STATUS = 0x70;
    
    // SPI Settings 
    // 4 MHz Clock, MSB first, mode 0
    // 4 MHz clock speed => one SPI clock pulse every 250ns, which is the MIN clock speed for DRV8704 chip
    SPISettings settingsA(4000000, MSBFIRST, SPI_MODE0);
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
    
      // Start SPI Library
      SPI.begin();
    
      pinMode(scsPin, OUTPUT);
      pinMode(LED, OUTPUT);
      pinMode(sleepPin, OUTPUT);
    
      // Pull up SLEEP pin to enable DRV8704
      digitalWrite(sleepPin, HIGH);
      
    }
    
    void loop() {
      
      digitalWrite(LED, HIGH);
    
      // READ the DRIVE register example (default response 0xFA5h)
      Serial.println("EXAMPLE 1: read from DRIVE Register, output should be 0xFA5h \n");
      unsigned int readData = readRegister(DRIVE);
      Serial.print("Read Data: ");
      Serial.print(readData,BIN);
      Serial.print(" | ");
      Serial.println(readData,HEX);
    
      Serial.println("-----------------------------------\n");
      
      delay(1000);
    
      // Write example to TORQUE register (from default 0xFFh to 0x01h)
      Serial.println("EXAMPLE 2: Write to TORQUE Register value 0x07D \n");
      bool writeBool = writeRegister(TORQUE,0x07D);
      Serial.print("Write success: ");
      Serial.println(writeBool);
    
      // Check if register was properly written
      unsigned int readData2 = readRegister(TORQUE);
      Serial.print("Read Data TORQUE: ");
      Serial.print(readData2,BIN);
      Serial.print(" | ");
      Serial.println(readData2,HEX);
    
      Serial.println("-----------------------------------\n");
    }
    
    void openSPI(){
      SPI.beginTransaction(settingsA);  // begin SPI transaction with custom settings
      digitalWrite(scsPin,HIGH);        // take the SCS pin high to select the chip
    }
    
    void closeSPI(){
      digitalWrite(scsPin,LOW);     // take the chip select low to de-select
      SPI.endTransaction();         // end SPI transaction
    }
    
    //Read from or write to register 
    unsigned int readRegister(byte thisRegister) {
      
      byte inByteHigh = 0;                 // incoming byte from the SPI
      byte inByteLow = 0;                 // result to return
      unsigned int result = 0x000;      // 12 data bits
      
      unsigned int dataToSend = thisRegister;   
      dataToSend |= 0x80;                         // set MSB to READ (1)
      
      openSPI();
      
      inByteHigh = SPI.transfer(dataToSend);     // send the deivce the register you want to read and read the first byte
      inByteLow = SPI.transfer(0x00);           // send a value of 0 to read the second byte returned
    
      closeSPI();
    
      // Combine the 2 bytes received 
      result = inByteHigh << 8;        // shift the first byte left 8 bits, to get a 16 bit unsigned int 
      result = result | inByteLow;    // combine the second byte with the first byte:
      result = result & ~0xF000;    // clear bits 16-13, only leave the 12 data bits
      
      //return the results:
      return(result);
    }
    
    bool writeRegister(byte thisRegister, unsigned int value) {
     
      bool writeSuccess = false;
     
      unsigned int dataToSend = thisRegister << 8;   // shift the first byte left 8 bits, to get a 16 bit unsigned int 
      dataToSend &= ~0x8000;                         // set MSB to WRITE (0)
    
      dataToSend = dataToSend | value;         // combine register address and data to write
      byte byteHigh = dataToSend >> 8;         // 8 most left bits
      byte byteLow = dataToSend & 0xff;        // 8 most right bits
      
      openSPI(); 
      
      // Send data to desired register
      SPI.transfer(byteHigh);
      SPI.transfer(byteLow);
      
      closeSPI();
    
      // Read register to check if it was written properly
      if(readRegister(thisRegister) == value){
        writeSuccess = true;
      }
      
      //return the results:
      return(writeSuccess);
    }

    Thank you,

    Roberto 

  • Hi Roberto,

    Thank you for following up with your findings.