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

SPI Slave with SoftDevice How-To or Example?

Hi,

I am trying to get the SPI Slave working but was hoping to find a how-to. Is there an example of the SPI Slave for the S110 Soft Device (7.3.0)?

Currently, I am able to successfully init the SPI Slave but it seems like the soft device is halting when it receives from a SPI master (another controller). I am not seeing it call the spi event handler. Any ideas? Is there any example of how it all comes together?

void spi_event_handler(spi_slave_evt_t evt)
{
     simple_uart_putstring((const uint8_t *)" \n\rHit This!");
    switch (evt.evt_type)
    {
        case SPI_SLAVE_BUFFERS_SET_DONE:
            simple_uart_putstring((const uint8_t *)" \n\rSerial Handler 1");
            if (has_pending_tx)
            {
                NRF_GPIO->OUTCLR = (1 << PIN_RDYN);
            }
            has_pending_tx = false;
            break;

        case SPI_SLAVE_XFER_DONE:
           simple_uart_putstring((const uint8_t *)" \n\rSerial Handler 2");
            NRF_GPIO->OUTSET = (1 << PIN_RDYN);
            if (doing_tx)
            {
                doing_tx = false;
                if (evt.tx_amount < tx_buffer.buffer[SERIAL_LENGTH_POS] + 2)
                {
                    /* master failed to receive our event. Re-send it. */
                                    
                                    /* ------------------UNCOMMENT THIS ----------------------------------*/
                    //serial_handler_event_send((serial_evt_t*)tx_buffer.buffer);
                }
            }
         
            break;

        default:
            /* no implementation necessary */
            break;
    }
}





static void enable_pin_listener(bool enable) {
    if (enable)
    {
        NRF_GPIOTE->EVENTS_IN[SERIAL_REQN_GPIOTE_CH]
= 0;
        NRF_GPIOTE->INTENSET = (1 << SERIAL_REQN_GPIOTE_CH);
    }
    else
    {
        NRF_GPIOTE->INTENCLR = (1 << SERIAL_REQN_GPIOTE_CH);
    } }


void serial_handler_init(void)



{

nrf_gpio_cfg_output(PIN_RDYN);
nrf_gpio_pin_set(PIN_RDYN);
serial_state = SERIAL_STATE_IDLE;

spi_slave_config_t spi_config;
spi_config.bit_order = SPIM_LSB_FIRST;
spi_config.mode = SPI_MODE_0;
spi_config.def_tx_character = 0;
spi_config.orc_tx_character = 0;
spi_config.pin_csn = PIN_CSN;
spi_config.pin_miso = PIN_MISO;
spi_config.pin_mosi = PIN_MOSI;
spi_config.pin_sck = PIN_SCK;


APP_ERROR_CHECK(spi_slave_init(&spi_config));
APP_ERROR_CHECK(spi_slave_evt_handler_register(spi_event_handler));


    NRF_GPIO->PIN_CNF[PIN_CSN] = (GPIO_PIN_CNF_SENSE_Disabled   << GPIO_PIN_CNF_SENSE_Pos)
                           | (GPIO_PIN_CNF_DRIVE_S0S1       << GPIO_PIN_CNF_DRIVE_Pos)
                           | (GPIO_PIN_CNF_PULL_Pulldown    << GPIO_PIN_CNF_PULL_Pos)
                           | (GPIO_PIN_CNF_INPUT_Connect    << GPIO_PIN_CNF_INPUT_Pos)
                           | (GPIO_PIN_CNF_DIR_Input        << GPIO_PIN_CNF_DIR_Pos);

NRF_GPIOTE->CONFIG[SERIAL_REQN_GPIOTE_CH] = (PIN_CSN << GPIOTE_CONFIG_PSEL_Pos)
                                          | (GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos)
                                          | (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos);


NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Msk;
NVIC_SetPriority(GPIOTE_IRQn, APP_IRQ_PRIORITY_LOW);
NVIC_ClearPendingIRQ(GPIOTE_IRQn);
NVIC_EnableIRQ(GPIOTE_IRQn);
enable_pin_listener(true);
}
Related