NRFX_TWIS_EVT_GENERAL_ERROR

NRFX_TWIS_EVT_GENERAL_ERROR

I am thinking of implementing TWI slave functionality in the nRF52840.

The event handler is written as follows.

void twis_event_handler(nrf_drv_twis_evt_t const * p_event)
{
     static bool address_received = false;  // レジスタアドレスが受信されたかどうかを追跡

    switch (p_event->type)
    {
        case NRFX_TWIS_EVT_READ_REQ:
            // マスターから読み取り要求があった場合
            nrf_drv_twis_tx_prepare(&m_twis, &m_twi_register[m_register_address], TWI_BUFFER_SIZE - m_register_address);
            NRF_LOG_INFO("Read request: register address = 0x%02X", m_register_address);
            break;

        case NRFX_TWIS_EVT_WRITE_REQ:
            if (!address_received) {
                NRF_LOG_INFO("Preparing to receive register address...");
                nrf_drv_twis_rx_prepare(&m_twis, &m_register_address, 1);
            } else {
                NRF_LOG_INFO("Register address received: 0x%02X", m_register_address);
                if (m_register_address < TWI_BUFFER_SIZE) {
                    NRF_LOG_INFO("Preparing to write data to register 0x%02X", m_register_address);
                    nrf_drv_twis_rx_prepare(&m_twis, &m_twi_register[m_register_address], TWI_BUFFER_SIZE - m_register_address);
                } else {
                    NRF_LOG_ERROR("Invalid register address: 0x%02X", m_register_address);
                }
            }
            break;

        case NRFX_TWIS_EVT_WRITE_DONE:
            if (!address_received) {
                address_received = true;
                NRF_LOG_INFO("Register address 0x%02X received", m_register_address);
            } else {
                address_received = false;
                NRF_LOG_INFO("Data written to register 0x%02X: 0x%02X", m_register_address, m_twi_register[m_register_address]);
            }
            break;

        case NRFX_TWIS_EVT_READ_DONE:
            NRF_LOG_INFO("Read completed");
            break;

        case NRFX_TWIS_EVT_READ_ERROR:
            NRF_LOG_ERROR("TWIS EVT READ ERROR");
            break;

        case NRFX_TWIS_EVT_WRITE_ERROR:
            NRF_LOG_ERROR("TWIS EVT WRITE ERROR");
            break;
        
        case NRFX_TWIS_EVT_GENERAL_ERROR:
            NRF_LOG_ERROR("TWIS EVT GENERAL ERROR");
            break;

        default:
            break;
    }
}

Read operations from the master work fine, but write operations result in NRFX_TWIS_EVT_GENERAL_ERROR.

As a result, values ​​cannot be written to the specified register.

What is the problem?

  • Hello,

    A logic analyzer trace here could be useful, but if that is not possible, can you for instance set a breakpoint in nrfx_twis_state_machine() in nrfx_twis.c to find which exact event is occurring? It should be one of these I assume:

    /** @brief TWIS events. */
    typedef enum
    {
        NRF_TWIS_EVENT_STOPPED   = offsetof(NRF_TWIS_Type, EVENTS_STOPPED),   /**< TWIS stopped. */
        NRF_TWIS_EVENT_ERROR     = offsetof(NRF_TWIS_Type, EVENTS_ERROR),     /**< TWIS error. */
        NRF_TWIS_EVENT_RXSTARTED = offsetof(NRF_TWIS_Type, EVENTS_RXSTARTED), /**< Receive sequence started. */
        NRF_TWIS_EVENT_TXSTARTED = offsetof(NRF_TWIS_Type, EVENTS_TXSTARTED), /**< Transmit sequence started. */
        NRF_TWIS_EVENT_WRITE     = offsetof(NRF_TWIS_Type, EVENTS_WRITE),     /**< Write command received. */
        NRF_TWIS_EVENT_READ      = offsetof(NRF_TWIS_Type, EVENTS_READ)       /**< Read command received. */
    } nrf_twis_event_t;

    It could be useful to confirm if the event is NRF_TWIS_EVENT_TXSTARTED, NRF_TWIS_EVENT_RXSTARTED or NRF_TWIS_EVENT_ERROR. I assume it's NRF_TWIS_EVENT_ERROR, so next step is also checking the error source register is helpful: https://docs.nordicsemi.com/bundle/ps_nrf52840/page/twis.html#register.ERRORSRC 

    Also check out: "The receive buffer is located in RAM at the address specified in the RXD.PTR register. The TWI slave will only be able to receive as many bytes as specified in the RXD.MAXCNT register. If the TWI master tries to send more bytes to the slave than it can receive, the extra bytes are discarded and NACKed by the slave. If this happens, an ERROR event will be generated." This is described here: https://docs.nordicsemi.com/bundle/ps_nrf52840/page/twis.html#ariaid-title4 

    Kenneth

Related