A-Lone-Developer
A-Lone-Developer

Reputation: 21

ESP8266_NONOS_SDK vs Arduino Framework - Interrupt Differences?

Just for the record, I am a C# developer and tend to focus more on api's and backends, firmware development / C / C++ is EXTRERMELY new to me!

I have been asked to port our existing ESP8266_NONOS_SDK project over to a newer framework for compatability and ease of use (the nonos_sdk is obsolete). After quite a bit of reading and reviewing I determined that the Arduino framework using PlatformIO as the best option (for me).

For the most part the transition worked out OK, I was able to create a project, add libraries i need, and generally get most of the base functionality working (IE: Connect to wifi and allow for OTA).

Now that i've proven the concept i need to move on to porting over our Interrupt's which handle reading data from the Receiver in our device, and this is where I get stuck.

    LOCAL void ICACHE_FLASH_ATTR gpio_intr_handler(void \*dummy)
    {
    static uint32 pulseCount, i;
    static uint8 invCRC;
    uint8 crcCalculated;
    volatile uint32 temp = 2500;
    uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);

    // if the interrupt was by GPIO14
    if (gpio_status & BIT(WROOM_DATA_IN_GPIO))
    {
        // disable interrupt for GPIO14
        gpio_pin_intr_state_set(GPIO_ID_PIN(WROOM_DATA_IN_GPIO), GPIO_PIN_INTR_DISABLE);
        // clear interrupt status for GPIO14
        GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(WROOM_DATA_IN_GPIO));
    
        // Calculate pulse width
        current_time = get_long_systime(); // system_get_time();
        pulseWidth = current_time - previous_time;
        previous_time = current_time;
        if (pulseWidth > 100)
        {
            pulseTime[pulseCount] = pulseWidth;
            pulseCount++;
        }
         }
    // Omitted as it's just calculations that I cannot share
    
    out:
        if (pulseWidth > MAX_WIDTH || pulseCount > MAX_PULSES)
        {
    
            pulseCount = 0;
            byteIndex = 0;
            bitIndex = 0;
            tag_alarm = 0;
            tag_batt = 0;
            tag_tlm = 0;
            for (i = 0; i < 5; i++)
                tagId[i] = 0;
        }
        // Reactivate interrupts for GPIO14
        gpio_pin_intr_state_set(GPIO_ID_PIN(WROOM_DATA_IN_GPIO), GPIO_PIN_INTR_ANYEDGE);

    }

This is the GPIO interrupt for the "Receiver" on GPIO pin 14, and this is copied from the ESP8266_nonos_sdk project where it works very well, however when I port this over to my arduino project, I get vastly different results.

Below is the updated Interrupt handler in my new arduino based project

    LOCAL void IRAM_ATTR gpio_intr_handler()
    {
    static uint32 pulseCount, i;
    static uint8 invCRC;
    uint8 crcCalculated;
    volatile uint32 temp = 2500;

    uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
    if (gpio_status & BIT(WROOM_DATA_IN_GPIO))
    {
        Serial.println("");
    
        // Disable GPIO14 Interrupt
        Serial.println("Detach Interrupt");
        detachInterrupt(digitalPinToInterrupt(WROOM_DATA_IN_GPIO));
    
        // clear interrupt status for GPIO14
        Serial.println("Clear GPIO Interrupt");
        // GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(WROOM_DATA_IN_GPIO));
        GPIO_OUTPUT_SET(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(WROOM_DATA_IN_GPIO));
    
        current_time = micros(); // system_get_rtc_time();
        pulseWidth = (current_time - previous_time);
        previous_time = current_time;
    
        Serial.print("Current Time: ");
        Serial.print(current_time);
        Serial.print(" Previous Time: ");
        Serial.print(previous_time);
        Serial.print(" Pulse Width: ");
        Serial.print(pulseWidth);
        Serial.print(" Pulse Count: ");
        Serial.println(pulseCount);
    
        if (pulseWidth > 100)
        {
            pulseTime[pulseCount] = pulseWidth;
            pulseCount++;
    
            Serial.print("Updated Pulse Count: ");
            Serial.println(pulseCount);
        }
    
        if (pulseWidth > MAX_WIDTH || pulseCount > MAX_PULSES)
        {
            pulseCount = 0;
            byteIndex = 0;
            bitIndex = 0;
            tag_alarm = 0;
            tag_batt = 0;
            tag_tlm = 0;
            for (i = 0; i < 5; i++)
                tagId[i] = 0;
        }
        attachInterrupt(digitalPinToInterrupt(USER_GPIO_IN), gpio_intr_handler, CHANGE);
     }

    }

Now, in the arduino framework version, I was able to add Serial.print so i could print out the current_time, previous_time and pulseWidth variables, but the data does not make any sense, this screenshot is the data that i'm seeing.

Serial Console Screenshot

As I said I am extremely new to firmware development and I am using ChatGPT to guide me, and it was seeming like everything was working perfectly, until i got to this interrupt handler, this is the last big piece that I need to figure out.

I have tried repeatedly using different ways of "getting system time" but always end up with the current and previous time's being identical and the pulseWidth being some crazy high value that does not make sense.

Upvotes: 1

Views: 78

Answers (0)

Related Questions