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

nRF51822 TWI + s110

I tried all the examples TWI code on my device and a dev kit nRF51-DK. Nothing appeared to work, either I get a into the WDT handler or the nRF51 resets.

While combing through the forums I stumbled upon a file posted dated 2009 called sd_twi_master.c

I tried it on my device and it appears to work.

Can someone explain what is the state of the affairs regarding TWI and s110, and what do I need to do to use the twi code from the latest SDK that doesn't causes issues.

thanks,

  • Hello, I would recommend to use the TWI master hardware driver. Some documentation can be found here.

    This driver run in two different modes: blocking mode and non-blocking mode. If an interrupt handler is not provided when initializing the twi event then the mode will be blocking mode. It will not return until the task is completed.

    err_code = nrf_drv_twi_init(&twi, NULL, NULL); //No interrupt handler provided
    err_code =  nrf_drv_twi_tx(&twi, SLAVE_ADDRESS, tx_Data. sizeof(tzx_data), false); //Will  always return NRF_SUCCESS, BEFORE the transmission is completed!
    err_code = nrf_drv_twi_rx(&twi, SLAVE_ADDRESS, tx_data, sizeof(tx_data), false); //Will always return NRF_SUCCESS, BEFORE the transmission is completed!
    

    It is also possible to provide an interrupt handler to the twi driver. Then the events can handled there. The event handler below is taken from another post here on devzone. It is very handy if one want to test the I2C connection, because it lights up if an I2C device is found.

     TWI event handler. Processing TWI events originating from nrf_drv_twi_tx()
    static void twi_event_handler(nrf_drv_twi_evt_t *evt){     
        // Light up LED 1 if device found
        if(evt->type == NRF_DRV_TWI_TX_DONE)
        {
            nrf_gpio_pin_clear(LED_1);
        }
    }
    
    err_code = nrf_drv_twi_init(&twi, NULL, twi_event_handler); //Setting up the event handler
    

    I am currently writing a blog post about how twi can be implemented on nRF5x chips, but it is not ready yet. I will update this post when I am done. In the meantime you can have a look at a previous post where TWI is implemented.

    EDIT: For anyone interested I have now made a blog post which describe how twi can be implemented on the nrf52 chip. It can be found here.

Related