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

TWI and BLE integration test

Dear, manager.

Hi,

I am currently testing the use of TWI and BLE together.

However, Data (24bit) is received and read through TWI every 4ms. BLE is only initialized, and TWI data becomes strange by simply turning on BLE, whether it is initialization or transmission.

I receive TWI 3 times each 8bit, but there seems to be no data.

Example)

Data1 Data2 Data3
56 155 135
56 116 66
56 154 128
56 155 62
56 173 131
56 120 179
56 99 95
56 201 53
0 11 98
0 11 64
0 11 41
0 11 43
0 11 158
0 11 139
0 11 81
0 11 81


How do I get the correct data?

I would appreciate your reply.

Thank you.

Regards.

Parents Reply
  • Thank you for your answer.

    Yes, when a BLE delay occurs, it affects the TWI data.

    When the MCU transfers the register to the sensor, data comes in three times in succession from the sensor.

    I think if there is a delay while receiving data, it can miss the TWI data pulse.

    And sorry but, we don't have the logic analyzer. However, with the scope, I was able to check only the continuity of the waveform.

Children
  • Daeho said:
    I think if there is a delay while receiving data, it can miss the TWI data pulse.

    That is not possible, all data is clocked from the nRF52832, so no data is lost on TWI. You should be able to zoom in on a scope to check the data of TWI bus, but a logic analyzer is preferred, since it can decode the data directly, else you will need to do it manually by comparing the clock and data line.

    May I ask where and how you have declared the TWI buffers in your code? 

    Kenneth

  • thank you for reply.

    When I read TWI I use the code below.

    void twiInit()
    {
      //TWI init function
      //TWI disable
      NRF_TWI0 -> ENABLE = 0;
    
      //TWI Pin map
      NRF_TWI0 -> PSELSCL = TWI_SCL;
      NRF_TWI0 -> PSELSDA = TWI_SDA;
    
      //frequency set
      NRF_TWI0 -> FREQUENCY = 0x06680000; //400kHz
      //NRF_TWI0 -> FREQUENCY = 0x01980000; //100kHz
    
      //TWI interrupt disable
      NRF_TWI0 -> INTENCLR = 0xFFFFFFFF;
    
      //Communication enable
      NRF_TWI0 -> ENABLE = 5;
    
      //Event clear
      NRF_TWI0 -> EVENTS_RXDREADY = 0;
      NRF_TWI0 -> EVENTS_TXDSENT = 0;
    }
    
    
    void ReadData(uint8_t reg_addr)
    {
      NRF_TWI0 -> ADDRESS = TI_address;  //Slave device set address
      NRF_TWI0 -> TASKS_STARTTX = 1;  //sned start
      NRF_TWI0 -> TXD = reg_addr; //send the read register address
      while(!NRF_TWI0 -> EVENTS_TXDSENT){}  //wait ACK
      NRF_TWI0 -> EVENTS_TXDSENT = 0;   //reset event flag
    
      NRF_TWI0 -> ADDRESS = TI_address;  //Slave device set address
      NRF_TWI0 -> TASKS_STARTRX = 1; //recive start
      while(NRF_TWI0 -> EVENTS_BB == 0){}  //wait Byte Boundary
      NRF_TWI0 -> EVENTS_BB = 0;  //reset BB flag after recive
      while(NRF_TWI0 -> EVENTS_RXDREADY == 0){} //wait read
      NRF_TWI0 -> EVENTS_RXDREADY = 0; //reset RXDREADY flag
      readData1 = NRF_TWI0 -> RXD;  //bling data buffer
    
      while(NRF_TWI0 -> EVENTS_BB == 0){}  //wait Byte Boundary
      NRF_TWI0 -> EVENTS_BB = 0;  //reset BB flag after recive
      while(NRF_TWI0 -> EVENTS_RXDREADY == 0){} //wait read
      NRF_TWI0 -> EVENTS_RXDREADY = 0; //reset RXDREADY flag
      readData2 = NRF_TWI0 -> RXD;  //bling data buffer
    
      while(NRF_TWI0 -> EVENTS_BB == 0){}  //wait Byte Boundary
      NRF_TWI0 -> EVENTS_BB = 0;  //reset BB flag after recive
      NRF_TWI0 -> TASKS_STOP = 1;   //NACK recive befor recive final data
      while(NRF_TWI0 -> EVENTS_RXDREADY == 0){} //wait read
      NRF_TWI0 -> EVENTS_RXDREADY = 0; //reset RXDREADY flag
      readData3 = NRF_TWI0 -> RXD;  //bling data buffer
    }
    

    The code that receives and handles an interrupt every 4ms is as follows. (Expressed in abbreviated form.)

    Interrupt handler() //4ms event
    {
        data_EN = 1;
    }
    
    Main()
    {
        if(data_EN == 1)  //Sensor Data ready, Data read
        { 
          data_EN = 0;
          //FIFO_PHASE, Data converter------------------------
          for(int j = 0; j < 4; j++)
          {
            ReadData(0xFF);
            sign = readData1 & 0xC0;  //0b11000000
            readData1 = readData1 & 0x3F;
            if(sign == 0xC0)
            {
              SensorData[j].TestData = -((readData1 << 16) + (readData2 << 8) + readData3);
            }
            else if(sign == 0x00)
            {
              SensorData[j].TestData = (readData1 << 16) + (readData2 << 8) + readData3;
            }
            data[j] = ((double)(SensorData[j].TestData) * (double)0.0005722045898);
          }
          //---------------------------------------------------
        }
    }

    Interrupt occurs every 4ms, receives a variable from the interrupt, reads and processes TWI data.

    When BLE is initialized and advertising starts or data is sent, the TWI data becomes strange.

    And changing the advertising interval to 1 (625 us) seems to be okay. However, BLE is not available.

    And even if the advertising time is reduced, it is thought that the problem will occur again during transmission.

    And when taken with a scope, it looks like the pictures below.

    When I look at the clock graph, it seems that the data comes in correctly might.

    please help, i need you help.

    Thank you.

    Regards.

  • So your scope is an Agilent DSOX1204A:

    It can do protocol decoding!

  • Can you also output the readData1, readData2, and readData3 to terminal?

    Kenneth

  • It's listed as a table in the first question, or are you referring to something else?

Related