/*******************************************************************************
 * @fn          bspSpiOpen
 *
 * @brief       Open the RTOS SPI driver
 *
 * @param       none
 *
 * @return      none
 */
void bspSpiOpen(void)
{
  if (hSpiPin != NULL)
  {
    // Remove IO configuration of SPI lines
    PIN_close(hSpiPin);

    hSpiPin = NULL;
  }

  if (spiHandle == NULL)
  {
    /*  Configure SPI as master, 1 mHz bit rate*/
    SPI_Params_init(&spiParams);
    spiParams.bitRate = 1000000;
    spiParams.mode         = SPI_MASTER;
    spiParams.transferMode = SPI_MODE_BLOCKING;

    /* Attempt to open SPI. */
    spiHandle = SPI_open(Board_SPI0, &spiParams);

    if (spiHandle == NULL)
    {
      Task_exit();
    }
  }

  nUsers++;
}
void spiLcd_Init()
{
    SPI_Handle handle;
    SPI_Params params;
    SPI_Transaction transaction;
    //PIN_Id csnPin1  = PIN_ID(Board_CSN_1);
    uint8_t txBuf[] = "Hello World";    // Transmit buffer

    // Init SPI and specify non-default parameters
    SPI_Params_init(&params);
    params.bitRate     = 1000000;
    params.frameFormat = SPI_POL1_PHA1;
    params.mode        = SPI_MASTER;
    
    // Configure the transaction
    transaction.count = sizeof(txBuf);
    transaction.txBuf = txBuf;
    transaction.rxBuf = NULL;

    // Open the SPI and perform transfer to the first slave
    handle = SPI_open(Board_SPI0, &params);
    
    SPI_transfer(handle, &transaction);
    
    // Then switch chip select pin and perform transfer to the second slave
    //SPI_control(handle, SPICC26XXDMA_SET_CSN_PIN, &csnPin1);
    //SPI_transfer(handle, &transaction);

    

}
Esempio n. 3
0
bool LIS3DH_Initialize(void)
{
    SPI_Params spiParams;
    SPI_init();
    // Init SPI and specify non-default parameters
    SPI_Params_init(&spiParams);

    spiParams.mode                     = SPI_MASTER;
    spiParams.transferMode             = SPI_MODE_BLOCKING; //SPI_MODE_CALLBACK; //SPI_MODE_CALLBACK; // SPI_MODE_BLOCKING
    spiParams.transferCallbackFxn      = sbp_spiCallback;
    spiParams.bitRate                  = 800000;
    spiParams.frameFormat              = SPI_POL1_PHA1;// Clock to be normally high, write on the falling edge and capture data on the rising edge //SPI_POL1_PHA1; (clock is normally high) //SPI_POL0_PHA0; (clock is normally low)
//    spiParams.dataSize				   = 16;
    spiHandle = SPI_open(MOTIONSNS_SPI, &spiParams);
/*    params.bitRate     = 1000000;
    params.frameFormat = SPI_POL1_PHA1;
    params.mode        = SPI_MASTER;
*/
    // Open the SPI and perform the transfer
//    handle = SPI_open(MOTIONSNS_SPI, &params);

	PINCC26XX_setOutputEnable(SPI_CS, 1);
	PINCC26XX_setOutputValue(SPI_CS, 1);

	PINCC26XX_setOutputEnable(SPI_MISO, 0);

    return LIS3DH_VerifyCommunication();

}
/*
 *  ======== spi_Open ========
 *  SimpleLink Host Driver API to open a SPI communication interface.
 */
Fd_t spi_Open(char *ifName, unsigned long flags)
{
    WiFiCC3100_Object *object = wifiHandle->object;
    SPI_Params         spiParams;

    SPI_Params_init(&spiParams);
    spiParams.bitRate = object->bitRate;
    spiParams.transferMode = SPI_MODE_CALLBACK;
    spiParams.transferCallbackFxn = WiFiCC3100_spiCallbackFxn;
    object->spiHandle = SPI_open(object->spiIndex, &spiParams);
    if (object->spiHandle == NULL) {
        Log_error1("WiFi SPI (%p) could not be initialized\n", object->spiIndex);
        return (-1);
    }

    object->spiState = WiFiCC3100_SPI_IDLE;

    Log_print1(Diags_USER1, "WiFi SPI (%p) open\n", (Fd_t) object->spiHandle);
    return ((Fd_t) object->spiHandle);
}
// -----------------------------------------------------------------------------
//! \brief      This routine initializes the transport layer and opens the port
//!             of the device.
//!
//! \param[in]  tRxBuf - pointer to NPI TL Tx Buffer
//! \param[in]  tTxBuf - pointer to NPI TL Rx Buffer
//! \param[in]  npiCBack - NPI TL call back function to be invoked at the end of 
//!             a SPI transaction                     
//!
//! \return     void
// -----------------------------------------------------------------------------
void NPITLSPI_initializeTransport(Char *tRxBuf, Char *tTxBuf, npiCB_t npiCBack)
{
    SPI_Params spiParams;

    TransportRxBuf = tRxBuf;
    TransportTxBuf = tTxBuf;
    npiTransmitCB = npiCBack;

    // Configure SPI parameters
    SPI_Params_init(&spiParams);

    // Slave mode
    spiParams.mode = SPI_SLAVE;
    spiParams.bitRate = SPI_SLAVE_BAUD_RATE;
    spiParams.frameFormat = SPI_POL1_PHA1;
    spiParams.transferMode = SPI_MODE_CALLBACK;
    spiParams.transferCallbackFxn = NPITLSPI_CallBack;

    // Attempt to open SPI
    spiHandle = SPI_open(NPI_SPI_CONFIG, &spiParams);
    return;
}
Esempio n. 6
0
/*!
 *  @brief  Function to initialize the CC26XX SPI peripheral specified by the
 *          particular handle. The parameter specifies which mode the SPI
 *          will operate.
 *
 *  The function will set a dependency on it power domain, i.e. power up the
 *  module and enable the clock. The IOs are allocated. Neither the SPI nor UDMA module
 *  will be enabled.
 *
 *  @pre    SPI controller has been initialized.
 *          Calling context: Task
 *
 *  @param  handle        A SPI_Handle
 *
 *  @param  params        Pointer to a parameter block, if NULL it will use
 *                        default values
 *
 *  @return A SPI_Handle on success or a NULL on an error or if it has been
 *          already opened
 *
 *  @sa     SPICC26XXDMA_close()
 */
SPI_Handle SPICC26XXDMA_open(SPI_Handle handle, SPI_Params *params)
{
    /* Use union to save on stack allocation */
    union {
        Semaphore_Params semParams;
        Hwi_Params hwiParams;
    } paramsUnion;
    SPI_Params               defaultParams;
    SPICC26XX_Object         *object;
    SPICC26XX_HWAttrs const  *hwAttrs;
    unsigned int             key;

    /* Get the pointer to the object and hwAttrs */
    object = handle->object;
    hwAttrs = handle->hwAttrs;

    /* Disable preemption while checking if the SPI is open. */
    key = Hwi_disable();

    /* Check if the SPI is open already with the base addr. */
    if (object->isOpen == true) {
        Hwi_restore(key);

        Log_warning1("SPI:(%p) already in use.", hwAttrs->baseAddr);

        return (NULL);
    }

    /* Mark the handle as being used */
    object->isOpen = true;
    Hwi_restore(key);

    /* If params are NULL use defaults */
    if (params == NULL) {
        /* No params passed in, so use the defaults */
        SPI_Params_init(&defaultParams);
        params = &defaultParams;
    }

    Assert_isTrue((params->dataSize >= 4) &&
                  (params->dataSize <= 16), NULL);

    /* Initialize the SPI object */
    object->currentTransaction = NULL;
    object->bitRate            = params->bitRate;
    object->dataSize           = params->dataSize;
    object->frameFormat        = params->frameFormat;
    object->mode               = params->mode;
    object->transferMode       = params->transferMode;
    object->transferTimeout    = params->transferTimeout;
    object->returnPartial      = false;
#ifdef SPICC26XXDMA_WAKEUP_ENABLED
    object->wakeupCallbackFxn  = NULL;
#endif

    /* Determine if we need to use an 8-bit or 16-bit framesize for the DMA */
    object->frameSize = (params->dataSize < 9) ? SPICC26XXDMA_8bit : SPICC26XXDMA_16bit;

    Log_print2(Diags_USER2,"SPI:(%p) DMA buffer incrementation size: %s",
                            hwAttrs->baseAddr,
                           (object->frameSize) ? (UArg)"16-bit" : (UArg)"8-bit");

    /* Register power dependency - i.e. power up and enable clock for SPI. */
    Power_setDependency(hwAttrs->powerMngrId);

    /* Configure the hardware module */
    SPICC26XXDMA_initHw(handle);

    /* CSN is initialized using hwAttrs initially, but can be re-configured later */
    object->csnPin = hwAttrs->csnPin;

    /* Configure IOs after hardware has been initialized so that IOs aren't */
    /* toggled unnecessary and make sure it was successful */
    if (!SPICC26XXDMA_initIO(handle)) {
        /* Trying to use SPI driver when some other driver or application
        *  has already allocated these pins, error! */
        Log_warning0("Could not allocate SPI pins, already in use.");

        /* Release power dependency - i.e. potentially power down serial domain. */
        Power_releaseDependency(hwAttrs->powerMngrId);

        /* Mark the module as available */
        key = Hwi_disable();
        object->isOpen = false;
        Hwi_restore(key);

        /* Signal back to application that SPI driver was not succesfully opened */
        return (NULL);
    }

    /* Create the Hwi for this SPI peripheral. */
    Hwi_Params_init(&paramsUnion.hwiParams);
    paramsUnion.hwiParams.arg = (UArg) handle;
    Hwi_construct(&(object->hwi), (int) hwAttrs->intNum, SPICC26XXDMA_hwiFxn, &paramsUnion.hwiParams, NULL);

    /* Check the transfer mode */
    if (object->transferMode == SPI_MODE_BLOCKING) {
        Log_print1(Diags_USER2, "SPI DMA:(%p) in SPI_MODE_BLOCKING mode",
                                 hwAttrs->baseAddr);

        /* Create a semaphore to block task execution for the duration of the
         * SPI transfer */
        Semaphore_Params_init(&paramsUnion.semParams);
        paramsUnion.semParams.mode = Semaphore_Mode_BINARY;
        Semaphore_construct(&(object->transferComplete), 0, &paramsUnion.semParams);

        /* Store internal callback function */
        object->transferCallbackFxn = SPICC26XXDMA_transferCallback;
    }
    else {
        Log_print1(Diags_USER2, "SPI DMA:(%p) in SPI_MODE_CALLBACK mode", hwAttrs->baseAddr);

        /* Check to see if a callback function was defined for async mode */
        Assert_isTrue(params->transferCallbackFxn != NULL, NULL);

        /* Save the callback function pointer */
        object->transferCallbackFxn = params->transferCallbackFxn;
    }

    /* Declare the dependency on the UDMA driver */
    object->udmaHandle = UDMACC26XX_open();

    /* Configure PIN driver for CSN callback in optional RETURN_PARTIAL slave mode */
    /* and/or optional wake up on CSN assert slave mode */
    if (object->mode == SPI_SLAVE) {
        PIN_registerIntCb(object->pinHandle, SPICC26XXDMA_csnCallback);
        PIN_setUserArg(object->pinHandle, (UArg) handle);
    }

    Log_print1(Diags_USER1, "SPI:(%p) opened", hwAttrs->baseAddr);

    /* Register notification functions */
#ifdef SPICC26XXDMA_WAKEUP_ENABLED
    Power_registerNotify(&object->spiPreObj, Power_ENTERING_STANDBY, (Fxn)spiPreNotify, (UInt32)handle, NULL );
#endif
    Power_registerNotify(&object->spiPostObj, Power_AWAKE_STANDBY, (Fxn)spiPostNotify, (UInt32)handle, NULL );

    return (handle);
}