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.

CC3120MOD: interfacing with STM32 Using SPI by Callback mode (Interrupt mode for data transaction) ?

Part Number: CC3120MOD
Other Parts Discussed in Thread: CC3100, CC3120

Hello,

I am working on CC3120MOD and interfacing to STM32 using SPI. Is it possible to implement SPI in callback mode (interrupt mode)? 

I have gone through the MSP32P4 example which is using Blocking Mode (Polling mode) for SPI implementation. I have also checked CC3100  interfacing example with STM32 discovery that is also using SPI in Blocking Mode(Polling mode).

Is there any way to implement the SPI driver in callback mode (interrupt mode)? Because it will block SPI_read/write function until it complete transaction.

/**********************************Code from cc_pal.c*************/

Fd_t spi_Open(char *ifName,
              unsigned long flags)
{
    void *lspi_hndl;
    SPI_Params SPI_Config;

    /* Initialize the WiFi driver */
    WiFi_init();

    /* If we could not initialize the device bail out with an error code */
    if(curDeviceConfiguration == NULL)
    {
        return (-1);
    }

    /* Initialize the SPI config structure */
    SPI_Params_init(&SPI_Config);
    SPI_Config.transferMode = SPI_MODE_BLOCKING;                   //////////////////////////////////Blocking Mode (Polling Mode) /////////////////////////////////////////////////////////
    SPI_Config.mode = SPI_MASTER;
    SPI_Config.bitRate = curDeviceConfiguration->spiBitRate;
    SPI_Config.dataSize = 8;
    SPI_Config.frameFormat = SPI_POL0_PHA0;

    /* Open SPI interface with SPI_Config parameters */
    lspi_hndl = SPI_open(curDeviceConfiguration->spiIndex, &SPI_Config);

    if(NULL == lspi_hndl)
    {
        return (-1);
    }
    else
    {
        return ((Fd_t) lspi_hndl);
    }
}

  • Hi Shubham,

    You can change SPI_MODE_BLOCKING to SPI_MODE_CALLBACK. Please see the API documentation for more details: dev.ti.com/.../_s_p_i_8h.html

    Best regards,
    Kristen
  • Thank you for your reply Kristen,

    But my point is that how spi will call back to CC3120 (sl_IfWrite or any other function )APIs?

    If I implement call back mode, then what will be the callback function for the host drive API of cc3120?

    How we link call back fuction of spi (transaction complete call back) to Host driver API of cc3120.

    Could you just provide example of cc3120 for call back mode in spi. Because I have only found blocking mode in sdk example of cc3120.

  • Hi Shubham,

    For an example of using TI Drivers to set SPI to callback mode, view the "spislave" example in the CC32xx SDK.

    Best regards,
    Kristen
  • Hello Kristen,

    I think you did not get my point. I want to know that how spi will call the Host driver of CC3120 in callback mode.

    If you see below code of CC3120 from cc_pal.c (porting folder of SDK CC3120 ). It is having a while loop for Read and write both.It will stuck in this loop until transaction complete. But I want to implement interface  porting driver of CC3120 for host  in callback mode So I have to remove while loop and provide the callback.Then where I will add callback functionality in this porting driver (below code). 

    If I provide callback functionality in spi interface then how SPI interface driver call back to CC3120 hsot api (upper layer). Because there is nowhere mention in programming guide of CC3120 about callback method for spi write and read.

    In porting driver comment it is mention that int spi_Write() could be implemented as zero copy and return only upon successful completion of writing the whole buffer? Please check this comment below I have mentioned the comment below from porting driver.

    /*!
        \brief attempts to write up to len bytes to the SPI channel
    
        \param             fd            -    file descriptor of an opened SPI channel
    
        \param            pBuff        -     points to first location to start getting the data from
    
        \param            len            -    number of bytes to write to the SPI channel
    
        \return            upon successful completion, the function shall return 0.
                        Otherwise, -1 shall be returned
    
        \sa             spi_Open , spi_Read
        \note            This function could be implemented as zero copy and return only upon successful completion
                        of writing the whole buffer, but in cases that memory allocation is not too tight, the
                        function could copy the data to internal buffer, return back and complete the write in
                        parallel to other activities as long as the other SPI activities would be blocked untill
                        the entire buffer write would be completed
        \warning
     */
    extern int spi_Write(Fd_t fd,
                         unsigned char *pBuff,
                         int len);

    /***********************************************/

    int spi_Read(Fd_t fd,
                 unsigned char *pBuff,
                 int len)
    {
        SPI_Transaction transact_details;
        int read_size = 0;
    
        ASSERT_CS();
    
        /* check if the link SPI has been initialized successfully */
        if(fd < 0)
        {
            DEASSERT_CS();
            return (-1);
        }
    
        transact_details.txBuf = NULL;
        transact_details.arg = NULL;
        transact_details.rxBuf = (void*) (pBuff);
    
        while(len > 0)
        {
            if(len > curDeviceConfiguration->maxDMASize)
            {
                transact_details.count = curDeviceConfiguration->maxDMASize;
            }
            else
            {
                transact_details.count = len;
            }
    
            if(SPI_transfer((SPI_Handle) fd, &transact_details))
            {
                read_size += transact_details.count;
                len = len - transact_details.count;
                transact_details.rxBuf = ((unsigned char *) (transact_details.rxBuf)
                                          + read_size);
            }
            else
            {
                DEASSERT_CS();
                return (-1);
            }
        }
    
        DEASSERT_CS();
    
        return (read_size);
    }
    
    int spi_Write(Fd_t fd,
                  unsigned char *pBuff,
                  int len)
    {
        SPI_Transaction transact_details;
        int write_size = 0;
    
        ASSERT_CS();
    
        /* check if the link SPI has been initialized successfully */
        if(fd < 0)
        {
            DEASSERT_CS();
            return (-1);
        }
    
        transact_details.rxBuf = NULL;
        transact_details.arg = NULL;
        transact_details.txBuf = (void*) (pBuff);
    
        while(len > 0)
        {
            if(len > curDeviceConfiguration->maxDMASize)
            {
                transact_details.count = curDeviceConfiguration->maxDMASize;
            }
            else
            {
                transact_details.count = len;
            }
    
            if(SPI_transfer((SPI_Handle) fd, &transact_details))
            {
                write_size += transact_details.count;
                len = len - transact_details.count;
                transact_details.txBuf = ((unsigned char *) (transact_details.txBuf)
                                          + transact_details.count);
            }
            else
            {
                DEASSERT_CS();
                return (-1);
            }
        }
    
        DEASSERT_CS();
    
        return (write_size);
    }

  • Hi,

    I see you asked the same question in this thread: e2e.ti.com/.../743399

    I will close this thread out so we can resolve your question in your new thread.

    Best regards,
    Kristen