/* 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;
}
/* ser_phy API function */
uint32_t ser_phy_open(ser_phy_events_handler_t events_handler)
{
    uint32_t           err_code;
    spi_slave_config_t spi_slave_config;
    spi_slave_evt_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

    err_code = spi_slave_evt_handler_register(spi_slave_event_handle);

    if (err_code == NRF_SUCCESS)
    {

        spi_slave_config.pin_miso         = SPIS_MISO_PIN;
        spi_slave_config.pin_mosi         = SPIS_MOSI_PIN;
        spi_slave_config.pin_sck          = SPIS_SCK_PIN;
        spi_slave_config.pin_csn          = SPIS_CSN_PIN;
        spi_slave_config.mode             = SPI_MODE_0;
        spi_slave_config.bit_order        = SPIM_LSB_FIRST;
        spi_slave_config.def_tx_character = SER_PHY_SPI_DEF_CHARACTER;
        spi_slave_config.orc_tx_character = SER_PHY_SPI_ORC_CHARACTER;

        //keep /CS high when init
        nrf_gpio_cfg_input(spi_slave_config.pin_csn, NRF_GPIO_PIN_PULLUP);
        err_code = spi_slave_set_cs_pull_up_config(GPIO_PIN_CNF_PULL_Pullup);
        APP_ERROR_CHECK(err_code);

        err_code = spi_slave_init(&spi_slave_config);

        if (err_code == NRF_SUCCESS)
        {
            m_ser_phy_callback = events_handler;

            m_trans_state   = SPI_RAW_STATE_SETUP_HEADER;
            event.evt_type  = SPI_SLAVE_EVT_TYPE_MAX; //force transition for dummy event
            event.rx_amount = 0;
            event.tx_amount = 0;
            spi_slave_event_handle(event);

        }
    }
    return err_code;
}