i2c receive event

/**
* @brief TWI master driver event types.
*/
typedef enum
{
NRF_DRV_TWI_EVT_DONE, ///< Transfer completed event.
NRF_DRV_TWI_EVT_ADDRESS_NACK, ///< Error event: NACK received after sending the address.
NRF_DRV_TWI_EVT_DATA_NACK ///< Error event: NACK received after sending a data byte.
} nrf_drv_twi_evt_type_t;

is there any event triggers when i receive the data?

  • /**
     * Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
     *
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     *
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     *
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     *
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     *
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     *
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    /** @file
     *
     * @defgroup blinky_example_main main.c
     * @{
     * @ingroup blinky_example
     * @brief Blinky Example Application main file.
     *
     * This file contains the source code for a sample application to blink LEDs.
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf_delay.h"
    #include "boards.h"
    #include "DAC.h"
    
    #include "stdio.h"
    //#include "main.h"
    #include "nrf_drv_spi.h"
    #include "nrf_gpio.h"
    #include <string.h>
    //#include "nrf_twi.h"
    //#include "nrfx_twi.h"
    #include "nrf_drv_twi.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
     //#define TWI_ADDRESSES      127
     #define EEPROM_ADDR          0xA0
    
    #define SPI_INSTANCE   0 /**< SPI instance index. */
    nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    
     bool spi_xfer_done;
    
     #define TWI_INSTANCE  1
      nrf_drv_twi_t twi =  NRF_DRV_TWI_INSTANCE(TWI_INSTANCE);
     bool  m_xfer_done = false;
    
    
    void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
                           void *                    p_context)
    {
        spi_xfer_done = true;
      //  NRF_LOG_INFO("Transfer completed.");
      /*  if (m_rx_buf[0] != 0)
        {
            NRF_LOG_INFO(" Received:");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }*/
    }
    
    void Spi0_init(void)
    {
        nrf_drv_spi_config_t spi_config ;
        spi_config.ss_pin   = SPI_SS_PIN;
        spi_config.miso_pin = SPI_MISO_PIN;
        spi_config.mosi_pin = SPI_MOSI_PIN;
        spi_config.sck_pin  = SPI_SCK_PIN;
        spi_config.frequency = NRF_SPI_FREQ_4M;
        spi_config.mode = NRF_SPI_MODE_0;
        spi_config.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST;
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL));
    }
    
    void twi_event( nrf_drv_twi_evt_t const * p_event_twi ,void *p_context_twi)
    {
       switch(p_event_twi->type)
       {
         case NRF_DRV_TWI_EVT_DONE :
         m_xfer_done = true;
         break;
         default:
         break;
    
    
       }
    
    }
    void I2C_1_init(void)
    {
       
        uint32_t err_code;
    
        nrf_drv_twi_config_t const config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_TWI_FREQ_400K,
           .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
           .clear_bus_init     = false,
        };
    
       err_code = nrf_drv_twi_init(&twi,&config,NULL,NULL);
       APP_ERROR_CHECK(err_code);
       nrf_drv_twi_enable(&twi);
    }
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
          uint32_t err_code;
          // uint8_t address;
          // uint8_t sample_data;
           uint8_t Tx_buffer[3] = {0};
           uint8_t Rx_buffer[3] = {0};
        bool detected_device = false;
        /* Configure board. */
        bsp_board_init(BSP_INIT_LEDS);
       // Spi0_init();
        I2C_1_init();
        
       /*    err_code = nrf_drv_twi_rx(&twi, EEPROM_ADDR, &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();
        }*/
    
           Tx_buffer[0] = 0x00;
           Tx_buffer[1] = 0x00;
           Tx_buffer[2] = 0x12;
          err_code = nrf_drv_twi_tx(&twi, (EEPROM_ADDR >> 1), Tx_buffer, 3,0);
            if (err_code == NRF_SUCCESS)
            {
                detected_device = true;
                NRF_LOG_INFO("TWI device detected at address 0x%x.", EEPROM_ADDR);
            }
            NRF_LOG_FLUSH();
    
        if (!detected_device)
        {
            NRF_LOG_INFO("No device was found.");
            NRF_LOG_FLUSH();
        }
          nrf_delay_ms(1000);
    
          err_code = nrf_drv_twi_tx(&twi, (EEPROM_ADDR >> 1), Tx_buffer, 2,0);
          err_code = nrf_drv_twi_rx(&twi, (EEPROM_ADDR >> 1), Rx_buffer, 1);
          if (err_code == NRF_SUCCESS)
            {
                detected_device = true;
                NRF_LOG_INFO("TWI device detected at address 0x%x.",EEPROM_ADDR );
            }
            NRF_LOG_FLUSH();
    
        if (!detected_device)
        {
            NRF_LOG_INFO("No device was found.");
            NRF_LOG_FLUSH();
        }
        /* Toggle LEDs. */
        while (true)
        {
        
          /*  for (int i = 0; i < LEDS_NUMBER; i++)
            {
                bsp_board_led_invert(i);
                nrf_delay_ms(500);
            }*/
            
         /*   for(uint16_t i = 500; i <= 2500; i += 500)
    	{
    	     Dac_Input(channel_A,i);
    	     nrf_delay_ms(2000);
    	}*/
        }
    }
    
    /**
     *@}
     **/
    

    when i initialise the twi with event handler transmission part works fine but receiving wont happen but if i use polling it works fine.

    is there any events triggered when i receive the data?

  • Yes,

    you should receive NRF_DRV_TWI_EVT_DONE event and handle the event to check if you received any data like this

            case NRF_DRV_TWI_EVT_DONE:
                if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
                {
                    
                    app_specific_data_handler(m_sample);
                }
    
                break;

    Check the examples\peripheral\twi_sensor\main.c example for the reference of its usage.

  • if i put the breakpoint inorder to debug the code its showing question mark on breakpoint and no code for breakpoint why?

  • probably you are compiling the code for release version that optimizes away all the debug symbols making it hard for the debugger to track and trace code for stepping and breaking.

    If you are using Segger, then make sure that you selected debug version for to compile while you are developing your all. An example snapshot is like below

  • NRF_DRV_TWI_EVT_DONE this indicates tranfer complete event right?

Related