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.

BEAGLEBK: SPI usage on TI RTOS + beaglebone black

Part Number: BEAGLEBK

Hi, 

I am having trouble from the past week trying to use SPI instance 1 on beaglebone black. 

I have used the following to initialize SPI on board:

void Board_SPI_init(void)
{
    UINT32 spi_status=0;
    spi_status = PRCMModuleEnable(CHIPDB_MOD_ID_MCSPI, 1 , NULL);
    if(S_PASS == spi_status )
    {
        spi_status = PINMUXModuleConfig(CHIPDB_MOD_ID_MCSPI, 1, NULL);
    }
    if(spi_status)
    {
        UART_printf("\n config successful");
    }
    SPI_HWAttrs spiCfg;
    SPI_socGetInitCfg(SPI1, &spiCfg);
    SPI_socSetInitCfg(SPI1, &spiCfg);
}

I am using the following task to test my SPI: 

/**
 *  @brief Function spi_test_task : Execute read on SPI bus
 *
 *  @param[in]         arg0, arg1: Arguments ( Currently not used)
 *  @retval            none
 */
void spi_test_task(UArg arg0, UArg arg1)
{
    appPrint("\n spi_test task started");
    SPI_Params   spiParams;              /* SPI params structure */
    SPI_Handle      hwHandle;            /* SPI handle */
    SPI_Transaction transaction;         /* SPI transaction structure */
    uint32_t        len=1;
    uint16_t        txBuf, rxBuf;
    uint32_t        count = 0;
    uint32_t        terminateXfer = 0;
    bool            retVal;
    uint32_t stat=0;
    stat=12;
    GPIO_write(0,1);

    Board_SPI_init();
    SPI_init();
    /* Default SPI configuration parameters */
    SPI_Params_init(&spiParams);
    spiParams.frameFormat  = SPI_POL0_PHA1;

    /* TODO: Add SPI functionality test here */


    hwHandle = (SPI_Handle)SPI_open(1, &spiParams);

    txBuf = 0x5400;                     /* Test data */
    len = sizeof(txBuf);

        transaction.txBuf = (uint16_t *) &txBuf;
        transaction.rxBuf = (uint16_t *) &rxBuf;
        transaction.count = len;
        transaction.arg = (void *)&terminateXfer;
        GPIO_write(0, 0);
       stat = SPI_transfer(hwHandle, &transaction);

    Task_sleep(10);
    appPrint("\n spi_test task ended");
    Task_exit();
}

I am using a Analog Discovery 2 logic analyzer to observe the waveform. Screenshot shows the outputs, I am not able to see any data. Clock looks very unusual and I am driving Chip select low before transfer (as suggested for my SPI slave device - DRV88889-Q1 (also from TI). Please guide me, is there something I am missing here?

  

Clock keeps varying, sometimes I get only two triangular signals, other times a random sequence of triangle signals.

I am looking forward to your response.

Thank you,

Arshiya Tabassum

 

  • Hi Arshiya,

    We suggest using our PDK examples as starting points. You can find documentation on those here: http://software-dl.ti.com/processor-sdk-rtos/esd/docs/latest/rtos/index_device_drv.html#mcspi-qspi-ospi

    I recommend you take a look at MCSPI_SlaveMode_MasterExample_bbbAM335x_armExampleProject which can be found in the PDK.

    If you have further questions after looking at that, let me know!

    Thanks,

    Max

  • Can I use the example src file "main_mcspi_slave_mode" as it is and able to run it on my BeagleBone Black?

  • The example suggests that we need two BBB board to test it. I dont have two boards of BBB, can I use one BBB (master) and one DRV8889-Q1 (SPI device0 as  Slave? Please respond,its urgent!

  • Also, as mentioned in the example steps:

     *  \file   main_mcspi_slave_mode.c
     *
     *  \brief  The application Demonstrate the MCSPI slave mode functionality.
     *          This application transmits data from master to slave and at the
     *          same instance data from slave will be transferred to master.
     *
     *  \details
     *          For K2x and AM3/AM4/AM5 devices, to demonstrate the example two
     *          boards are needed one board acts as master and another as slave.
     *
     *          Following is the Pin connection information:
     *          MasterSPI_SCLK----SlaveSPI_SCLK
     *          MasterSPI_D0------SlaveSPI_D1
     *          MasterSPI_D1------SlaveSPI_D0
     *          MasterSPI_CS------SlaveSPI_CS
     *
     *          To run the example:
     *          1) Connect the master and slave boards as per above pin connections.
     *          2) Two consoles. One for master and another for slave.
     *          3) Run slave application binary and then master application binary.
     *

    I dont have a binary to run for the other slave device because it is just a driver chip (DRV8889-Q1) I am trying to read a register of DRV8889-Q1 using SPI.

    Please help me with this. I apologize for the separate messages sent earlier.

    Thank you,

    Arshiya Tabassum 

  • Hi Arshiya,

    You don't necessarily need to run the code on both devices. I just suggest that you first compare the way in which you initialized your SPI driver to the way that we do it in the example code. If you have questions about discrepancies, let me know here.

    Thanks,

    Max

  • I am using the same initialization sequence as the example. I am trying to use SPI0 with a slave device DRV889-Q1 by TI. My clock is reflected like this as shown in the picture and my receive buffer has a value 0xffff as shown in the screenshot. Please help me solve this issue. 

     Hi Max, 

    Here is my SPI initialization routine:

    void BBB_Board_SPI_Init(void)
    {
        Board_SPI_init();                //SPI Instance initialization
        SPI_init();                      //SPI init
        SPI_peripheral_init();           //SPI Parameters initialization
    
    }
    
    void Board_SPI_init(void)
    {
        UINT32 spi_status=0;
        Board_SoCInfo socInfo;
        spi_status = PRCMModuleEnable(CHIPDB_MOD_ID_MCSPI, 0 , NULL);
        if(S_PASS == spi_status )
        {
            spi_status = PINMUXModuleConfig(CHIPDB_MOD_ID_MCSPI, 0, NULL);
        }
        if(spi_status)
        {
            UART_printf("\n config successful");
        }
        SPI_HWAttrs spiCfg;
    
         SPI_socGetInitCfg(SPI0, &spiCfg);
        /* Update the SPI functional clock based on CPU clock*/
         Board_getSoCInfo(&socInfo);
         if(socInfo.sysClock != BOARD_SYS_CLK_DEFAULT)
         {
            spiCfg.inputClkFreq = socInfo.sysClock/SPI_MODULE_CLOCK_DIVIDER;
         }
         SPI_socSetInitCfg(SPI0, &spiCfg);
    }
    
    static void SPI_peripheral_init(void)
    {
      SPI_Params_init(&Spi_params_var);
      Spi_params_var.transferMode = SPI_TRANSFER_MODE;
      Spi_params_var.transferTimeout= SPI_DEFAULT_TRANSFER_TIMEOUT;
      Spi_params_var.transferCallbackFxn = NULL;
      Spi_params_var.mode = SPI_MODE;
      Spi_params_var.bitRate = SPI_DEFAULT_BITRATE;
      Spi_params_var.dataSize = SPI_DEFAULT_DATASIZE;
      Spi_params_var.frameFormat = SPI_POLARITY_PHASE;
      Spi_handler_var = SPI_open(SPI_INSTANCE_MOTOR, &Spi_params_var);
      if(!Spi_handler_var)
      {
          SPI_log("\n Error initializing SPI \n");
      }
    }

    Here is the application code:

    void spi_test_task(UArg arg0, UArg arg1)
    {
        appPrint("\n spi_test task started");
        SPI_Params   spiParams;              /* SPI params structure */
        SPI_Handle      hwHandle;            /* SPI handle */
        SPI_Transaction transaction;         /* SPI transaction structure */
        uint32_t        len=1;
        uint16_t        txBuf, rxBuf;
        bool            retVal;
        uint32_t stat=0;
        
        GPIO_write(nSCS_INDEX,1);
    
        MOT_EnDisSleepMode(DRV_AWAKE);         //Awaken SPI slave device
        pin_state =GPIO_read(nSLEEP_INDEX);
    
        MOT_EnDisDriver(DRV_ON);                //Enable SPI slave device outputs
        pin_state =GPIO_read(DRVOFF_INDEX);
    
        Board_SPI_init();
        SPI_init();
        /* Default SPI configuration parameters */
        SPI_Params_init(&spiParams);
        spiParams.frameFormat  = SPI_POL0_PHA1;
        spiParams.transferMode = SPI_TRANSFER_MODE;
        spiParams.transferTimeout= SPI_DEFAULT_TRANSFER_TIMEOUT;
        spiParams.transferCallbackFxn = NULL;
        spiParams.mode = SPI_MODE;
        spiParams.bitRate = SPI_DEFAULT_BITRATE;
        spiParams.dataSize = SPI_DEFAULT_DATASIZE;
    
        /* TODO: Add SPI functionality test here */
        hwHandle = (SPI_Handle)SPI_open(0, &spiParams);
    
        txBuf = 0x5400;                     /* Test data */
        len = sizeof(txBuf);
    
            transaction.txBuf = (uint16_t*)&txBuf;
            transaction.rxBuf =  (uint16_t*)&rxBuf;
            transaction.count = len;
         
            GPIO_write(nSCS_INDEX, 0);                           //Pull Chip select low before SPI transfer
           stat = SPI_transfer(hwHandle, &transaction);
    
        Task_sleep(10);
        appPrint("\n spi_test task ended");
        Task_exit();
    }

    Please let me know if I am mising something here. I would really appreciate any help in this issue.

    Thank you,

    Arshiya

     

  • Hi Arshiya,

    You seem to be setting all of the SPI_Params to their default values. Are you sure that these are the values you need?

    Specifically, I'm not sure what the values of SPI_POLARITY_PHASE, and SPI_MODE are.

    Thanks,

    Max

  • Hi Max, 

    The issue with clock is resolved but I am still not able to read the register from a SPI device. 

    And you are right, I am not calling SPI_Params init function now.

    Please see the screenshot of my analyzer.

    Let me know what am missing here, that would be really helpful.

    Thank you,

    Arshiya Tabassum

  • Hi Arshiya,

    "And you are right, I am not calling SPI_Params init function now"

    Would you be able to share your most up-to-date initialization code?

    Thanks,

    Max

  • Sure, here is the updated initialization sequence.

    void BBB_Board_SPI_Init(void)
    {
        Board_SPI_init();                //SPI Instance initialization
        SPI_init();                      //SPI init
        SPI_peripheral_init();           //SPI Parameters initialization
    
    }
    void Board_SPI_init(void)
    {
        UINT32 spi_status=0;
        Board_SoCInfo socInfo;
       // GPIO_write(nSCS_INDEX, 0);
        spi_status = PRCMModuleEnable(CHIPDB_MOD_ID_MCSPI, 1, NULL);
        if(S_PASS == spi_status )
        {
            spi_status = PINMUXModuleConfig(CHIPDB_MOD_ID_MCSPI, 1, NULL);
        }
        if(spi_status)
        {
            UART_printf("\n config successful");
        }
        SPI_HWAttrs spiCfg;
    
         SPI_socGetInitCfg(1, &spiCfg);
        /* Update the SPI functional clock based on CPU clock*/
         Board_getSoCInfo(&socInfo);
         if(socInfo.sysClock != BOARD_SYS_CLK_DEFAULT)
         {
            spiCfg.inputClkFreq = socInfo.sysClock/SPI_MODULE_CLOCK_DIVIDER;
         }
         SPI_socSetInitCfg(1, &spiCfg);
    }
    static void SPI_peripheral_init(void)
    {
     // SPI_Params_init(&Spi_params_var);
      Spi_params_var.transferMode = SPI_TRANSFER_MODE;
      Spi_params_var.transferTimeout= SPI_DEFAULT_TRANSFER_TIMEOUT;
      Spi_params_var.transferCallbackFxn = NULL;
      Spi_params_var.mode = SPI_MODE;
      Spi_params_var.bitRate = SPI_DEFAULT_BITRATE;
      Spi_params_var.dataSize = SPI_DEFAULT_DATASIZE;
      Spi_params_var.frameFormat = SPI_POLARITY_PHASE;
      Spi_handler_var = SPI_open(1, &Spi_params_var);
      if(!Spi_handler_var)
      {
          SPI_log("\n Error initializing SPI \n");
      }
    }

    My test task:

    void spi_test_task(UArg arg0, UArg arg1)
    {
        appPrint("\n spi_test task started");
        SPI_Params   spiParams;              /* SPI params structure */
        SPI_Handle      hwHandle;            /* SPI handle */
        SPI_Transaction transaction;         /* SPI transaction structure */
        uint32_t        len=1;
        uint16_t        tx1, tx2;
        uint16_t        rxBuf;
    
    
        pin_state = MOT_ReadnFAULTPin();
    
        MOT_EnDisSleepMode(DRV_AWAKE);                   // Awaken from standby mode
        pin_state =GPIO_read(nSLEEP_INDEX);
    
        MOT_EnDisDriver(DRV_ON);                         // Enable driver outputs
        pin_state =GPIO_read(DRVOFF_INDEX);
        GPIO_write(nSCS_INDEX,0);
        BBB_Board_SPI_Init();
    
        tx1 = 0x5400;                     /* Test data */
        len = sizeof(tx1);
        tx2 = 0x0000;
        /* TODO: Add SPI functionality test here */
        hwHandle = (SPI_Handle)SPI_open(1, &spiParams);
    
        transaction.txBuf = &tx1;
        transaction.rxBuf =  &rxBuf;
        transaction.count = len;
        pin_state = SPI_transfer(hwHandle, &transaction);
        //transaction.txBuf = &tx2;
          transaction.rxBuf =  &rxBuf;
          transaction.count = len;
        while (1)
        {
        pin_state = SPI_transfer(hwHandle, &transaction);
        }
    
    
    
        appPrint("\n spi_test task ended");
    
    }

  • Hi Arshiya,

    I think there is an issue where you're calling SPI_Open twice. You're opening it in SPI_Peripheral_Init and then again in spi_test_task.

    You should only perform this action once.

    Thanks,

    Max