title: Enabling I2C interrupt within timer interrupt on nRF52 Arduino
body: <p>I am writing a program for an nRF52 based board using the Redbear Arduino Library. Effectively treating my board as a BLE Nano 2.</p>
  
  <p>I have a timer that ticks every x milliseconds, for example 50ms.</p>
  
  <p>Inside that timer I would like to read data from an I2C sensor and add the reading to a buffer.</p>
  
  <p>I am aware that by default, when inside the timer ISR, interrupts are disabled. I would like to know how to briefly re-enable the I2C interrupt, and get the sensor reading, then disable the interrupts again.</p>
  
  <p>The interval between sensor readings is critical, and I don't just want to set a flag in the timer ISR as I don't know how long it will be before that flag is checked.</p>
  
  <p>Please can someone instruct me on how to breifly enable the I2C interrupts from within a timer ISR?</p>
  
  <p>I have experimented with:</p>
  
  <pre><code>__disable_irq();
  
  __enable_irq();
  
  NVIC_EnableIRQ(SPI0_TWI0_IRQn);
  
  NRF_TWI0-&gt;INTENSET = 1;
  </code></pre>
  
  <p>No joy with any of these, I understand that some of them only work from certain locations in software, so were not functioning correctly within an ISR.</p>
  
  <pre><code>void setup() {
    // put your setup code here, to run once
    NVIC_SetPriority (TIMER0_IRQn, 2);
    NVIC_SetPriority (TIMER1_IRQn, 2);
    NVIC_SetPriority (TIMER2_IRQn, 2);
    NVIC_SetPriority (SPI0_TWI0_IRQn, 3);
    NVIC_SetPriority (SPI1_TWI1_IRQn, 3);
    Serial.begin(9600);
    Serial.println("Start ");
    Serial.print("Priority of TWI0: ");
    Serial.println(NVIC_GetPriority (SPI0_TWI0_IRQn));
    Serial.println(NVIC_GetPriority (SPI1_TWI1_IRQn));
    Serial.print("Priority of Ticker: ");
    Serial.println(NVIC_GetPriority (TIMER0_IRQn));
    Serial.println(NVIC_GetPriority (TIMER1_IRQn));
    Serial.println(NVIC_GetPriority (TIMER2_IRQn));
  
  
    Wire.begin();
    __disable_irq();
    __enable_irq();
    ticker1s.attach(task_handle, 1);
    Wire.requestFrom(0x02,6);
  
  }
  
  void task_handle(void) {
    __enable_irq();
    Serial.println("Task handle ");
    Serial.println("-IRQ enable status: ");
    Serial.println(NVIC_GetEnableIRQ(SPI0_TWI0_IRQn));
    Serial.println(NVIC_GetEnableIRQ(SPI1_TWI1_IRQn));
    NVIC_EnableIRQ(SPI0_TWI0_IRQn);
    NRF_TWI0-&gt;INTENSET = 1;
  
    NVIC_EnableIRQ(SPI1_TWI1_IRQn);
    NRF_TWI1-&gt;INTENSET = 1;
    Serial.println("-IRQ enable status: ");
    Serial.println(NVIC_GetEnableIRQ (SPI0_TWI0_IRQn));
    Serial.println(NVIC_GetEnableIRQ (SPI1_TWI1_IRQn));
    delay(1000);
    Wire.requestFrom(0x02,6);
  
  }
  </code></pre>