This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to close the spi interface

hi, now ,i have save the power,so need to close the spi interface at some time. but when i call follow function (hal_spi_disable). it see not close the spi interface,

if any issue about this?


uint32_t hal_spi_disable(SPIModuleNumber module_number)
{
    switch (module_number)
    {
        case SPI0:
            NRF_SPI0->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos);
            break;
        case SPI1:
            NRF_SPI1->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos);
            break;
        default:
            return NRF_ERROR_INVALID_PARAM;
    }
    
    return NRF_SUCCESS;
}

  • Hi Steven,

    Setting NRF_SPIx->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); will disable the peripheral, and then free up the GPIOs. Are you not able to re-configure the GPIOs afterwards? Can you please provide a bit more detailed information about the issue you are seeing?

    Best regards Håkon

  • hi,

    when i disable the spi interface , i have reconfigure the GPIO. i will check my system .

    1. follow hal_spi.c file , so it will be not reconfigure i/o when disable the spi interface.

    // Disable SPI when it's not in use spi_base->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos);

    
    bool hal_spi_tx_rx(uint32_t *spi_base_address, uint16_t transfer_size, const uint8_t *tx_data, uint8_t *rx_data)
    {
        uint32_t counter = 0;
        uint16_t number_of_txd_bytes = 0;
        uint32_t SEL_SS_PINOUT;
    
        NRF_SPI_Type *spi_base = (NRF_SPI_Type *)spi_base_address;
    
        SEL_SS_PINOUT = spi_base == NRF_SPI0 ? spi0_pins.ss_pin : spi1_pins.ss_pin;
        
        // Enable SPI
        spi_base->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);
    
        /* enable slave (slave select active low) */
        nrf_gpio_pin_clear(SEL_SS_PINOUT);
    
        while(number_of_txd_bytes < transfer_size)
        {
            spi_base->TXD = (uint32_t)(tx_data[number_of_txd_bytes]);
    
            /* Wait for the transaction complete or timeout (about 10ms - 20 ms) */
            while ((spi_base->EVENTS_READY == 0U) && (counter < TIMEOUT_COUNTER))
            {
                counter++;
            }
    
            if (counter == TIMEOUT_COUNTER)
            {
                /* timed out, disable slave (slave select active low) and return with error */
                nrf_gpio_pin_set(SEL_SS_PINOUT);
                spi_base->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos);
                return false;
            }
            else
            {   /* clear the event to be ready to receive next messages */
                spi_base->EVENTS_READY = 0U;
            }
    
            rx_data[number_of_txd_bytes] = (uint8_t)spi_base->RXD;
            number_of_txd_bytes++;
        };
    
        /* disable slave (slave select active low) */
        nrf_gpio_pin_set(SEL_SS_PINOUT);
    
        // Disable SPI when it's not in use
        spi_base->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos);
        
        return true;
    }
    
    
    
  • In your initial post, you mention that you disable SPI to save power. Is the issue that you still have high power consumption? Or is it that the GPIOs are not released from the peripheral?

    Best regards Håkon

Related