The TWI problems continue ...

Hello all,

for the last few weeks I am trying to workout the i2c protocol with the sensor and the nrf52dk , so far none of the solution that I have considered work. More can be seen here: https://devzone.nordicsemi.com/f/nordic-q-a/84978/twi-reading-and-writing-basics/363652#363652

I am thinking can the speed of the processor be too fast for the sensor prototype, like the wires to the board may be too long, for example, for example, the same prototype works on the Arduino's Atmel omega just ok... 

Therefore, to test this out my question is, how to reduce the speed of the i2c to less than 100kHz and how to reduce the speed of the processor? Indeed, if you have other suggestions please do not hesitate.

Thank you in advance, looking forward to your suggestions, ideas, how to tackle this further.

Best.

  • Hello,

    how to reduce the speed of the i2c to less than 100kHz

    That is not possible, unfortunately. Please see:

    https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/twi.html?cp=4_2_0_48_7_9#register.FREQUENCY

    100kHz is the lowest possible.

    how to reduce the speed of the processor?

    Why would you want to do that? I don't see how that would solve anything.

    If you are using 100kHz TWI, then the length of the wires should not be an issue (unless you are using several meters of wire). Try attaching a logic analyzer to it, and see whether you get the expected result on the TWI. You will most likely see that the data at the end of your wires are the same as you are sending, as 100kHz isn't really that fast. 

    Looking quickly through the ticket that you linked to, I don't really think that the speed is related, but I guess you want to see whether that could be the issue, because it is not working.

    Perhaps you want to try the twi_scanner example, and see whether the sensor responds when the twi_scanner() example cycles through all the TWI addresses.

    Best regards,

    Edvin

  • Hello Edvin and thank you for your reply,

    indeed, the voltage drops that we were thinking about are not relevant, measured it on the current wiring setup, so far so good.

    Tested the address with the TWI Scanner example and got : 

    <info> app: TWI scanner started.
    <info> app: TWI device detected at address 0x60.

    Apparently, the sensor is found ok, btw please remind me where is the address on the TWI specifically defined? I want to check that in my application...

    I have easily copied the code from the TWI scanner into my application and interesting things pop out, not sure, if this is ok?

    TWI scanner started.<info> app: TWI scanner started.
    error code :    nothing?TWI device detected at address 0x1.<info> app: TWI device detected at address 0x1.
    TWI device detected at address 0x2.<info> app: TWI device detected at address 0x2.
    TWI device detected at address 0x3.<info> app: TWI device detected at address 0x3.
    TWI device detected at address 0x4.<info> app: TWI device detected at address 0x4.
    TWI device detected at address 0x5.<info> app: TWI device detected at address 0x5.
    TWI device detected at address 0x6.<info> app: TWI device detected at address 0x6.
    <info> app: TWI device detected at address 0x7.
    TWI device detected at address 0x8.TWI device detected at address 0x9.<info> app: TWI device detected at address 0x1C.
    TWI device detected at address 0x1d.<info> app: TWI device detected at address 0x1D.
    TWI device detected at address 0x1e.<info> app: TWI device detected at address 0x1E.
    TWI device detected at address 0x1f.<info> app: TWI device detected at address 0x1F.
    TWI device detected at address 0x20.<info> app: TWI device detected at address 0x20.
    TWI device detected at address 0x21.TWI device detected at address 0x22.TWI device detected at address 0x33.<info> app: TWI device detected at address 0x33.
    TWI device detected at address 0x34.<info> app: TWI device detected at address 0x34.
    TWI device detected at address 0x35.<info> app: TWI device detected at address 0x35.
    TWI device detected at address 0x36.<info> app: TWI device detected at address 0x36.
    TWI device detected at address 0x37.<info> app: TWI device detected at address 0x37.
    TWI device detected at address 0x38.TWI device detected at address 0x40.<info> app: TWI device detected at address 0x40.
    TWI device detected at address 0x41.<info> app: TWI device detected at address 0x41.
    TWI device detected at address 0x42.<info> app: TWI device detected at address 0x42.
    TWI device detected at address 0x43.TWI device detected at address 0x44.TWI device detected at address 0x50.<info> app: TWI device detected at address 0x50.
    TWI device detected at address 0x51.<info> app: TWI device detected at address 0x51.
    TWI device detected at address 0x52.<info> app: TWI device detected at address 0x52.
    TWI device detected at address 0x53.<info> app: TWI device detected at address 0x53.
    TWI device detected at address 0x54.<info> app: TWI device detected at address 0x6B.
    TWI device detected at address 0x6c.<info> app: TWI device detected at address 0x6C.
    TWI device detected at address 0x6d.<info> app: TWI device detected at address 0x6D.
    TWI device detected at address 0x6e.<info> app: TWI device detected at address 0x6E.
    TWI device detected at address 0x6f.<info> app: TWI device detected at address 0x6F.
    TWI device detected at address 0x70.<info> app: TWI device detected at address 0x70.
    <info> app: TWI device detected at address 0x76.
    TWI device detected at address 0x77.<info> app: TWI device detected at address 0x77.
    TWI device detected at address 0x78.<info> app: TWI device detected at address 0x78.
    TWI device detected at address 0x79.<info> app: TWI device detected at address 0x79.

    Best.

  • This is not the unmodified twi_scanner sample, right? It looks like it is finding a device on every address, which I assume is not actually the case. What does your application look like?

  • Not, this is the "modified scanner example", actually the code from the scanner example is used within my application to see the slave address... the output is give above ... 

    The ide of the application is to read the data from the sensor, namely it should enable the sensor triggering and fetch this data back to the uC via i2c, and that is it. In short, the main() setup, is the twi init() and sensor config, while during the while() loop the values from the sensor are read in constant manner and that is it, Can share the bits of code if it will help. At the moment, I am only after working i2c/twi ... and as you can see that salve_address is not explict, when accessing the sensor... where would you look for the slave address ? For example, according to the data sheet and twi scanner example, the address of the sensor is 0x60, this should be explicitly used within the rx and tx functions?

    Best.

  • does thw TWI part of your application look like this when it prints "TWI device detected at address 0xXX."?

    int main(void)
    {
        ret_code_t err_code;
        uint8_t address;
        uint8_t sample_data;
        bool detected_device = false;
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("TWI scanner started.");
        NRF_LOG_FLUSH();
        twi_init();
    
        for (address = 1; address <= TWI_ADDRESSES; address++)
        {
            err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
            if (err_code == NRF_SUCCESS)
            {
                detected_device = true;
                NRF_LOG_INFO("TWI device detected at address 0x%x.", address);
            }
            NRF_LOG_FLUSH();
        }
    
        if (!detected_device)
        {
            NRF_LOG_INFO("No device was found.");
            NRF_LOG_FLUSH();
        }
    
        while (true)
        {
            /* Empty loop. */
        }
    }

    Does nrf_drv_twi_rx() return NRF_SUCCESS on all addresses, address = 1; address <= TWI_ADDRESSES; address++ ?

Related