int main(void) { // Enable the constant latency sub power mode to minimize the time it takes // for the SPIS peripheral to become active after the CSN line is asserted // (when the CPU is in sleep mode). NRF_POWER->TASKS_CONSTLAT = 1; LEDS_CONFIGURE(BSP_LED_0_MASK); LEDS_OFF(BSP_LED_0_MASK); APP_ERROR_CHECK(NRF_LOG_INIT()); NRF_LOG_PRINTF("SPIS example\n"); nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG(SPIS_INSTANCE); spis_config.csn_pin = SPIS_CS_PIN; APP_ERROR_CHECK(nrf_drv_spis_init(&spis, &spis_config, spis_event_handler)); while(1) { memset(m_rx_buf, 0, m_length); spis_xfer_done = false; APP_ERROR_CHECK(nrf_drv_spis_buffers_set(&spis, m_tx_buf, m_length, m_rx_buf, m_length)); while (!spis_xfer_done) { __WFE(); } LEDS_INVERT(BSP_LED_0_MASK); } }
uint32_t spi_slave_example_init(void) { uint32_t err_code; nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG(SPIS_INSTANCE_NUMBER); spis_config.miso_pin = SPIS_MISO_PIN; spis_config.mosi_pin = SPIS_MOSI_PIN; spis_config.sck_pin = SPIS_SCK_PIN; spis_config.csn_pin = SPIS_CSN_PIN; spis_config.mode = NRF_DRV_SPIS_MODE_0; spis_config.bit_order = NRF_DRV_SPIS_BIT_ORDER_LSB_FIRST; spis_config.def = DEF_CHARACTER; spis_config.orc = ORC_CHARACTER; err_code = nrf_drv_spis_init(&m_spis, &spis_config, spi_slave_event_handle); APP_ERROR_CHECK(err_code); //Initialize buffers. spi_slave_buffers_init(m_tx_buf, m_rx_buf, (uint16_t)TX_BUF_SIZE); //Set buffers. err_code = nrf_drv_spis_buffers_set(&m_spis, m_tx_buf, sizeof(m_tx_buf), m_rx_buf, sizeof(m_rx_buf)); APP_ERROR_CHECK(err_code); return NRF_SUCCESS; }
/* ser_phy API function */ uint32_t ser_phy_open(ser_phy_events_handler_t events_handler) { uint32_t err_code; nrf_drv_spis_config_t spi_slave_config; nrf_drv_spis_event_t event; if (m_trans_state != SPI_RAW_STATE_UNKNOWN) { return NRF_ERROR_INVALID_STATE; } if (events_handler == NULL) { return NRF_ERROR_NULL; } //one ppi channel and one gpiote channel are used to drive RDY line m_spi_slave_raw_config.pin_req = SER_PHY_SPI_SLAVE_REQ_PIN; m_spi_slave_raw_config.pin_rdy = SER_PHY_SPI_SLAVE_RDY_PIN; m_spi_slave_raw_config.ppi_rdy_ch = SER_PHY_SPI_PPI_RDY_CH; m_spi_slave_raw_config.gpiote_rdy_ch = SER_PHY_SPI_GPIOTE_RDY_CH; spi_slave_gpio_init(); #ifndef _SPI_5W_ spi_slave_gpiote_init(); spi_slave_ppi_init(); #endif spi_slave_config.miso_pin = SPIS_MISO_PIN; spi_slave_config.mosi_pin = SPIS_MOSI_PIN; spi_slave_config.sck_pin = SPIS_SCK_PIN; spi_slave_config.csn_pin = SPIS_CSN_PIN; spi_slave_config.mode = NRF_DRV_SPIS_MODE_0; spi_slave_config.bit_order = NRF_DRV_SPIS_BIT_ORDER_LSB_FIRST; spi_slave_config.def = SER_PHY_SPI_DEF_CHARACTER; spi_slave_config.orc = SER_PHY_SPI_ORC_CHARACTER; spi_slave_config.csn_pullup = NRF_GPIO_PIN_PULLUP; spi_slave_config.irq_priority = NRF_APP_PRIORITY_LOW; //keep /CS high when init nrf_gpio_cfg_input(spi_slave_config.csn_pin, NRF_GPIO_PIN_PULLUP); err_code = nrf_drv_spis_init(&m_spis, &spi_slave_config, spi_slave_event_handle); APP_ERROR_CHECK(err_code); if (err_code == NRF_SUCCESS) { m_ser_phy_callback = events_handler; m_trans_state = SPI_RAW_STATE_SETUP_HEADER; event.evt_type = NRF_DRV_SPIS_EVT_TYPE_MAX; //force transition for dummy event event.rx_amount = 0; event.tx_amount = 0; spi_slave_event_handle(event); } return err_code; }
void spi_format(spi_t *obj, int bits, int mode, int slave) { if (bits != 8) { error("Only 8-bits SPI is supported\r\n"); } if (mode > 3) { error("SPI format error\r\n"); } spi_info_t *p_spi_info = SPI_INFO(obj); if (slave) { nrf_drv_spis_mode_t spi_modes[4] = { NRF_DRV_SPIS_MODE_0, NRF_DRV_SPIS_MODE_1, NRF_DRV_SPIS_MODE_2, NRF_DRV_SPIS_MODE_3, }; nrf_drv_spis_mode_t new_mode = spi_modes[mode]; // If the peripheral is currently working as a master, the SDK driver // it uses needs to be switched from SPI to SPIS. if (p_spi_info->master) { nrf_drv_spi_uninit(MASTER_INST(obj)); } // I the SPI mode has to be changed, the SDK's SPIS driver needs to be // re-initialized (there is no other way to change its configuration). else if (p_spi_info->spi_mode != (uint8_t)new_mode) { nrf_drv_spis_uninit(SLAVE_INST(obj)); } else { return; } p_spi_info->spi_mode = (uint8_t)new_mode; p_spi_info->master = false; p_spi_info->flag.readable = false; // Initialize SDK's SPIS driver with the new configuration. nrf_drv_spis_config_t config; prepare_slave_config(&config, p_spi_info); (void)nrf_drv_spis_init(SLAVE_INST(obj), &config, m_slave_event_handlers[SPI_IDX(obj)]); // Prepare the slave for transfer. p_spi_info->tx_buf = NRF_DRV_SPIS_DEFAULT_ORC; nrf_drv_spis_buffers_set(SLAVE_INST(obj), (uint8_t const *)&p_spi_info->tx_buf, 1, (uint8_t *)&p_spi_info->rx_buf, 1); } else // master { nrf_drv_spi_mode_t spi_modes[4] = { NRF_DRV_SPI_MODE_0, NRF_DRV_SPI_MODE_1, NRF_DRV_SPI_MODE_2, NRF_DRV_SPI_MODE_3, }; nrf_drv_spi_mode_t new_mode = spi_modes[mode]; // If the peripheral is currently working as a slave, the SDK driver // it uses needs to be switched from SPIS to SPI. if (!p_spi_info->master) { nrf_drv_spis_uninit(SLAVE_INST(obj)); } // I the SPI mode has to be changed, the SDK's SPI driver needs to be // re-initialized (there is no other way to change its configuration). else if (p_spi_info->spi_mode != (uint8_t)new_mode) { nrf_drv_spi_uninit(MASTER_INST(obj)); } else { return; } p_spi_info->spi_mode = (uint8_t)new_mode; p_spi_info->master = true; p_spi_info->flag.busy = false; // Initialize SDK's SPI driver with the new configuration. nrf_drv_spi_config_t config; prepare_master_config(&config, p_spi_info); (void)nrf_drv_spi_init(MASTER_INST(obj), &config, m_master_event_handlers[SPI_IDX(obj)]); } }