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

TWIM issue

Hello ,
I am working on a project which contains I2C communication,
When I test the twi scanner example on my sensor and it works fine
But when I do the combination with the usbd_ble_uart example to send the data to uart I encountered a problem during an I2C read operation. My code stuck in this infinite loop

bool transmission_finished = false;
        do {
            if (nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_SUSPENDED))
            {
                transmission_finished = true;
            }

            if (nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_STOPPED))
            {
                nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_STOPPED);
                transmission_finished = true;
            }

            if (nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_ERROR))
            {
                nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_ERROR);

                bool lasttx_triggered = nrf_twim_event_check(p_twim, NRF_TWIM_EVENT_LASTTX);
                uint32_t shorts_mask = nrf_twim_shorts_get(p_twim);

                if (!(lasttx_triggered && (shorts_mask & NRF_TWIM_SHORT_LASTTX_STOP_MASK)))
                {
                    // Unless LASTTX event arrived and LASTTX_STOP shortcut is active,
                    // triggering of STOP task in case of error has to be done manually.
                    nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_RESUME);
                    nrf_twim_task_trigger(p_twim, NRF_TWIM_TASK_STOP);

                    // Mark transmission as not finished yet,
                    // as STOPPED event is expected to arrive.
                    // If LASTTX_SUSPENDED shortcut is active,
                    // NACK has been received on last byte sent
                    // and SUSPENDED event happened to be checked before ERROR,
                    // transmission will be marked as finished.
                    // In such case this flag has to be overwritten.
                    transmission_finished = false;
                }

                if (lasttx_triggered && (shorts_mask & NRF_TWIM_SHORT_LASTTX_SUSPEND_MASK))
                {
                    // When STOP task was triggered just before SUSPEND task has taken effect,
                    // SUSPENDED event may not arrive.
                    // However if SUSPENDED arrives it always arrives after ERROR.
                    // Therefore SUSPENDED has to be cleared
                    // so it does not cause premature termination of busy loop
                    // waiting for STOPPED event to arrive.
                    nrf_twim_event_clear(p_twim, NRF_TWIM_EVENT_SUSPENDED);

                    // Mark transmission as not finished yet,
                    // for same reasons as above.
                    transmission_finished = false;
                }
            }
        } while (!transmission_finished);

Although I tried to copy all the configuration performed in the example of twi scanner

Please Help me .

  • nikola said:
    when I add APP_ERROR_CHECK I have got this error 

    Great, this means that the function actually did return an error. Please view the entire error message, it will say which line of your code that generated the NRF_ERROR_INVALID_ADDR error. Then you will need to look at the API reference for that function, to determine why it would return this particular error.

    nikola said:
    I don't understand you very well Can you expalin to me in another way

    I am not sure that my written text answers here might be the best way for you to learn about interrupts and events. Perhaps it could help you to take a look at this third party video tutorial on the topic.
    I strongly recommend that you get a good understanding of this fundamental embedded C concept before you continue the development of your application, as it will be impossible to create an application with the functionality and complexity that you describe above without understanding and using interrupts and events.

    Best regards,
    Karl

Related