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

nRF52840 SDK16 - Problem with TWI

Hi everyone, 

I try to write using I2C but I fail and I cannot figure out the issue

This is my code snippet.

uint8_t my_data[2];
  my_data[0] = 0x00;
  my_data[0] = 0x01;
  rslt = reg_write(reg_addr, my_data, 2, dev);
  
 --------------------------------------------------------------------------------------------
 
  uint8_t reg_write(uint8_t reg_addr, uint8_t *data, uint16_t len, struct BQ27421_dev *dev) {
  
  /*Call the I2C write function*/
  dev->write(dev->chip_id, reg_addr, data, len);
                  
}

//write() is a function pointer to Acc_i2c_Write()
 --------------------------------------------------------------------------------------------

int8_t Acc_i2c_Write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) {

 
  int8_t rslt = 0;
  uint8_t data[len + 1];
  data[0] = reg_addr; 
  for (uint16_t i = 0; i < len; i++) {
    data[i + 1] = reg_data[i]; 
  }

  rslt = nrf_drv_twi_tx(&m_twi, dev_id, data, len + 1, false); 
  APP_ERROR_CHECK(rslt);



  return rslt;
}

When I run it it returns the following error

00> <error> app: ERROR 2 [NRF_ERROR_SOFTDEVICE_NOT_ENABLED] at C:\Users\n.antoniou\Dropbox\IoT\Nordic\nRF52840\nRF5_SDK_16.0.0\Ingenious\BLE_Peripheral\BLE_IMU_v4\main.c:484
00> PC at: 0x00034529
00> <error> app: End of error report

It fails inside the nrf_drv_twi_tx() function.. When I set the len parameter to one then it runs normally but I want to write two bytes..

Any ideas?

Parents Reply Children
  • Hi Jared,

    I enable the Softdevice before initializing the TWI driver and finally my BMX160 and BQ27421 I2C sensors. This is my main()

    int main(void) {
    
      log_init(); // na - Initialize the logger module
    
      createInterrupt();                             // Create the interrupts
      timers_init();                                 // na - Initialize the Timer Library
      power_management_init();                       // Initializing power management
      ble_stack_init();                              // BLE Stack initiallization
      sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE); // na - Enabling the DCDC converter for lower current consumption after calling ble_stack_init - You can enable the DC/DC at any time after you have enabled the SoftDevice
      gap_params_init();                             // GAP initiallization, the Generic Access Profile, specifies the ways devices advertise, discover and connect to each other
      gatt_init();                                   // GATT initiallization, the Generic Attribute Profile, is the one that handles attributes, characteristics and services
      services_init();                               // Initialize the services
      advertising_init();                            // Initilize the advertising
    
      conn_params_init();  // Initialize the connection parameters
      peer_manager_init(); // Initialize the Peer Manager Module. The peer manager module handles encryption, pairing, and bonding
    
      
      twi_init();
      Acc_delay_ms(50);
    
      BMI160_init();
      Acc_delay_ms(100);
    
    
      BQ27421_initialize(); // na - Initialize the battery monitor
    
      //***************SAADC-FLEXI FORCE***************
      nrf_gpio_range_cfg_output(pinStartOut, pinStopOut); // na - pinout for EB v.1
      selectMuxPin(callback_counter);                     // na - set the control pin of MUX to zero
      saadc_init();                                       // na -  consumes high current when the saadc is initiated. Check this example to minimize the consumption https://github.com/NordicPlayground/nRF52-ADC-examples/blob/master/saadc_low_power/main.c
    
      //---------------------------------------------------------------------------------------------------------------------------
    
      nrf_gpio_cfg_output(vibrator); // na - configure vibrator as OUTPUT
      nrf_gpio_pin_clear(vibrator);  // na - initialize the pin to low (no vibration)
      nrf_gpio_cfg_output(CTRL);     // na - configure the CTRL pin as output (this pin set and unset converter's LOAD output)
      nrf_gpio_pin_set(CTRL);        // Enable the LOAD output of buck converter (TPS62740) by setting high its CTRL pin
      nrf_delay_ms(10);              // na - add a small delay to give some time to complete the pin configuration
    
      /** @brief : RAM retention - Writes the NRF_POWER->RAM[index].POWERSET register
       *
       *  @param[in] index : Contains the index in the NRF_POWER->RAM[index].POWERSET register to write to.
       *  @param[in] ram_powerset : Contains the word to write to the NRF_POWER->RAM[index].POWERSET register.
       *
       *  @retval : NRF_SUCCESS
       */
      ret_code_t err_code;
      err_code = sd_power_ram_power_set(RAM_AHB_slave, POWER_RAM_POWER_S0RETENTION_On << POWER_RAM_POWER_S0RETENTION_Pos); // na - retain RAM AHB 2, index 0.
      APP_ERROR_CHECK(err_code);
    
    
      // Enter main loop.
      for (;;) {
    
        // Enter system On sleep mode
        idle_state_handle(); // na - The Power Management library is safer to use than the inline functions __SEV, __WFE, __WFI especially when SoftDevice is used.
      }
    }
    

    Thanks

  • Hi. 

    Please share the whole file. The error is reporting that it asserts at line 484 in main.c

Related