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 .

  • You need to review and verify that every SDK function that you use, that return an error code, has that error code passed to an APP_ERROR_CHECK, or other error handling.

    For example, in your code you have the following lines:

    err_code = nrf_drv_twi_tx(&m_twi, MX30105_ADDR,&FIFO_Write_PointerADD, 1, false);
    nrf_delay_ms(5);
    err_code = nrf_drv_twi_rx(&m_twi, MX30105_ADDR, &FIFO_Write_Pointer,1);
    nrf_delay_ms(5);
    if (err_code != NRF_SUCCESS)
    {
        NRF_LOG_INFO(" ERROR IN Write Pointer:  0x%x", FIFO_Write_Pointer);
        NRF_LOG_FLUSH();
        error = 1;
    }

    Here you see that the first of these two returned error codes are never actually checked or used for anything, before it is immediately overwritten.
    If this error code should ever come back with a value != NRF_SUCCESS, you will never know about it and proceed as usual, which could break the continuation of your program.

    Best regards,
    Karl

  • nd I strongly recommend that you look into how you may move 98% of your current main() code into interrupt handles or separate functions outside of main()

    how can I do this please

  • You can start by moving all function definitions outside of main(), for example. Then, you need to determine which of them should be called as part of an interrupt service routine, and then implement that routine.

    Are you familiar with interrupt service routines / IRQ's / callback functions?
    If not, I recommend that you familiarize yourself with this subject through either a video series or blog series on the topic, for example. The scope of it is too broad to cover here in a comment, but in essence; Interrupts lets the CPU be alerted to events that has occurs outside of the regular program flow (main), so that it can jump to a different section of the code to handle this event, before resuming its regular program flow (main). Interrupts are the cornerstone of an event-triggered embedded system.

    Bet regards,
    Karl

  • when I add APP_ERROR_CHECK I have got this error 

  • you need to determine which of them should be called as part of an interrupt service routine, and then implement that routine.

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

Related