Harware Timer Constant WDT resets
Posted: Sun Jun 02, 2019 11:08 am
Greetings.
I've been playing around with the ESP8266 hardware timer using a NodeMCU v1.0. In the documentation (found here, p.24), it is mentioned that in autoload mode, the lower limit for FRC1_SOURCE is 50μs. However, using the following code to test that, I am unable to get stable operation with a STEP_TIME under 400μs. I am getting continuous WDT resets, sometimes back to back (always after setup() is done), or after a few seconds of operation. With a STEP_TIME of 400μs, I got a number of WDT resets followed by seemingly stable operation, but I need to go down to a STEP_TIME of 78μs, at which point there is absolutely no stable operation.
It seems like the WDT resets are software resets, as I am getting a full stack dump. I am in the process of installing the arduino stack trace decoder, but it is showing errors.
Stack dump:
I am using PlatformIO as an Atom plugin to code and flash.
In the main loop, I tried a delay of 10ms, 100ms (as shown), 1000ms, and no delay. Adding a delay was suggested by a colleague. It seems to do nothing. The commented out non-blocking delay was to slow the serial printing.
hw_timer.h
hw_timer.c
Am I missing anything? What am I doing wrong? I am starting to suspect the background WiFi processes.
Further investigation: After flashing this code, I flashed another code that did contain WiFi credentials of a network I am NOT in (i.e. the ESP8266 will not be able to connect), and flashing this code after worked flawlessly (even below the specs, tried 20μs on FRC1_SOURCE). If I flash a different firmware containing WiFi connection to a network that is accessible to the ESP8266 (i.e. the ESP8266 is able to connect), and then reflash the above exact code, it starts to crash again. This makes me more certain that the background WiFi functions are the cause, but puts me nowhere closer to solving the issue.
I've been playing around with the ESP8266 hardware timer using a NodeMCU v1.0. In the documentation (found here, p.24), it is mentioned that in autoload mode, the lower limit for FRC1_SOURCE is 50μs. However, using the following code to test that, I am unable to get stable operation with a STEP_TIME under 400μs. I am getting continuous WDT resets, sometimes back to back (always after setup() is done), or after a few seconds of operation. With a STEP_TIME of 400μs, I got a number of WDT resets followed by seemingly stable operation, but I need to go down to a STEP_TIME of 78μs, at which point there is absolutely no stable operation.
It seems like the WDT resets are software resets, as I am getting a full stack dump. I am in the process of installing the arduino stack trace decoder, but it is showing errors.
Stack dump:
Code: Select all
Exception (0):
epc1=0x40201018 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
>>>stack>>>
ctx: sys
sp: 3fffeba0 end: 3fffffb0 offset: 01a0
3fffed40: 4000437d 00000030 00000016 ffffffff
3fffed50: 60000200 00000008 30feefef 80000000
3fffed60: 20000000 3ffef2c8 80000000 203fd060
3fffed70: 80000000 3ffee378 00000002 3ffef2cc
3fffed80: 0000043c 003fd060 3ffe8204 00000030
3fffed90: 3ffedc20 00000001 3fffeee0 00000030
3fffeda0: 00000000 00000000 00000020 401047ea
3fffedb0: 00000000 00000000 00000020 401047ea
3fffedc0: 3ffe92d0 4010403c 3ffeca68 4000050c
3fffedd0: 40222ab5 00000030 0000001e ffffffff
3fffede0: 40222ab5 00000023 3fffdcc0 0000000b
3fffedf0: 3ffe87c0 00000000 00000000 000000b0
3fffee00: 000000e4 00009516 1a3292ac 3ffeea14
3fffee10: 000013ca 3ffee124 3ffe8204 00000030
3fffee20: 00000001 3ffedc20 000000a0 4010074c
3fffee30: 00000000 400042db 00000002 401001c4
3fffee40: 40004b31 3ffef26c 0000049c 003fd000
3fffee50: 40102018 3ffee186 00000003 401001c4
3fffee60: 40102249 3ffef26c 0000049c 00000000
3fffee70: 40221ecb 3ffee258 3ffef26c 402260ca
3fffee80: ffffff00 55aa55aa 00000016 00000020
3fffee90: 00000020 00000011 00000011 aa55aa55
3fffeea0: 000003fd 40226c2e 3ffeea14 000013ca
3fffeeb0: 3ffee124 0000f221 40222ad2 40226c0a
3fffeec0: 40222af3 3ffee124 3ffef26c 40222af6
3fffeed0: 00000006 3fffeee0 00000020 00000001
3fffeee0: 3ffed5f0 3ffeea18 40222bf9 3ffee040
3fffeef0: 40220fd1 00000009 e2ab13a3 8ecbd837
3fffef00: 3ffee186 00000200 402215d5 3ffee040
3fffef10: 40221f3b 00000003 00000008 00000001
3fffef20: 3ffef084 40235707 00000000 00000000
3fffef30: 3ffeca68 402350b2 3fffdab0 00000000
3fffef40: 3fffdcc0 3ffe93f0 00000000 00000000
3fffef50: 3ffe93f0 40000f49 3fffdab0 40000f49
3fffef60: 40000e19 40001878 00000002 3fffffb0
3fffef70: 3fffff10 aa55aa55 00000011 40100b94
3fffef80: 40100b9a 00000002 3fffffb0 01c32cff
3fffef90: 4010000d 9a834fa6 ae1893eb a651389c
3fffefa0: 401009d4 3fffef3c 40100975 3fffff68
3fffefb0: 3fffffc0 00000000 00000000 feefeffe
3fffefc0: feefeffe feefeffe feefeffe feefeffe
3fffefd0: feefeffe feefeffe feefeffe feefeffe
3fffefe0: feefeffe feefeffe feefeffe feefeffe
3fffeff0: feefeffe feefeffe feefeffe feefeffe
3ffff000: feefeffe feefeffe feefeffe feefeffe
3ffff010: feefeffe feefeffe feefeffe feefeffe
3ffff020: feefeffe feefeffe feefeffe feefeffe
3ffff030: feefeffe feefeffe feefeffe feefeffe
3ffff040: feefeffe feefeffe feefeffe feefeffe
3ffff050: feefeffe feefeffe feefeffe feefeffe
3ffff060: feefeffe feefeffe feefeffe feefeffe
3ffff070: feefeffe feefeffe feefeffe feefeffe
3ffff080: feefeffe feefeffe feefeffe feefeffe
3ffff090: feefeffe feefeffe feefeffe feefeffe
3ffff0a0: feefeffe feefeffe feefeffe feefeffe
3ffff0b0: feefeffe feefeffe feefeffe feefeffe
3ffff0c0: feefeffe feefeffe feefeffe feefeffe
3ffff0d0: feefeffe feefeffe feefeffe feefeffe
3ffff0e0: feefeffe feefeffe feefeffe feefeffe
3ffff0f0: feefeffe feefeffe feefeffe feefeffe
3ffff100: feefeffe feefeffe feefeffe feefeffe
3ffff110: feefeffe feefeffe feefeffe feefeffe
3ffff120: feefeffe feefeffe feefeffe feefeffe
3ffff130: feefeffe feefeffe feefeffe feefeffe
3ffff140: feefeffe feefeffe feefeffe feefeffe
3ffff150: feefeffe feefeffe feefeffe feefeffe
3ffff160: feefeffe feefeffe feefeffe feefeffe
3ffff170: feefeffe feefeffe feefeffe feefeffe
3ffff180: feefeffe feefeffe feefeffe feefeffe
3ffff190: feefeffe feefeffe feefeffe feefeffe
3ffff1a0: feefeffe feefeffe feefeffe feefeffe
3ffff1b0: feefeffe feefeffe feefeffe feefeffe
3ffff1c0: feefeffe feefeffe feefeffe feefeffe
3ffff1d0: feefeffe feefeffe feefeffe feefeffe
3ffff1e0: feefeffe feefeffe feefeffe feefeffe
3ffff1f0: feefeffe feefeffe feefeffe feefeffe
3ffff200: feefeffe feefeffe feefeffe feefeffe
3ffff210: feefeffe feefeffe feefeffe feefeffe
3ffff220: feefeffe feefeffe feefeffe feefeffe
3ffff230: feefeffe feefeffe feefeffe feefeffe
3ffff240: feefeffe feefeffe feefeffe feefeffe
3ffff250: feefeffe feefeffe feefeffe feefeffe
3ffff260: feefeffe feefeffe feefeffe feefeffe
3ffff270: feefeffe feefeffe feefeffe feefeffe
3ffff280: feefeffe feefeffe feefeffe feefeffe
3ffff290: feefeffe feefeffe feefeffe feefeffe
3ffff2a0: feefeffe feefeffe feefeffe feefeffe
3ffff2b0: feefeffe feefeffe feefeffe feefeffe
3ffff2c0: feefeffe feefeffe feefeffe feefeffe
3ffff2d0: feefeffe feefeffe feefeffe feefeffe
3ffff2e0: feefeffe feefeffe feefeffe feefeffe
3ffff2f0: feefeffe feefeffe feefeffe feefeffe
3ffff300: feefeffe feefeffe feefeffe feefeffe
3ffff310: feefeffe feefeffe feefeffe feefeffe
3ffff320: feefeffe feefeffe feefeffe feefeffe
3ffff330: feefeffe feefeffe feefeffe feefeffe
3ffff340: feefeffe feefeffe feefeffe feefeffe
3ffff350: feefeffe feefeffe feefeffe feefeffe
3ffff360: feefeffe feefeffe feefeffe feefeffe
3ffff370: feefeffe feefeffe feefeffe feefeffe
3ffff380: feefeffe feefeffe feefeffe feefeffe
3ffff390: feefeffe feefeffe feefeffe feefeffe
3ffff3a0: feefeffe feefeffe feefeffe feefeffe
3ffff3b0: feefeffe feefeffe feefeffe feefeffe
3ffff3c0: feefeffe feefeffe feefeffe feefeffe
3ffff3d0: feefeffe feefeffe feefeffe feefeffe
3ffff3e0: feefeffe feefeffe feefeffe feefeffe
3ffff3f0: feefeffe feefeffe feefeffe feefeffe
3ffff400: feefeffe feefeffe feefeffe feefeffe
3ffff410: feefeffe feefeffe feefeffe feefeffe
3ffff420: feefeffe feefeffe feefeffe feefeffe
3ffff430: feefeffe feefeffe feefeffe feefeffe
3ffff440: feefeffe feefeffe feefeffe feefeffe
3ffff450: feefeffe feefeffe feefeffe feefeffe
3ffff460: feefeffe feefeffe feefeffe feefeffe
3ffff470: feefeffe feefeffe feefeffe feefeffe
3ffff480: feefeffe feefeffe feefeffe feefeffe
3ffff490: feefeffe feefeffe feefeffe feefeffe
3ffff4a0: feefeffe feefeffe feefeffe feefeffe
3ffff4b0: feefeffe feefeffe feefeffe feefeffe
3ffff4c0: feefeffe feefeffe feefeffe feefeffe
3ffff4d0: feefeffe feefeffe feefeffe feefeffe
3ffff4e0: feefeffe feefeffe feefeffe feefeffe
3ffff4f0: feefeffe feefeffe feefeffe feefeffe
3ffff500: feefeffe feefeffe feefeffe feefeffe
3ffff510: feefeffe feefeffe feefeffe feefeffe
3ffff520: feefeffe feefeffe feefeffe feefeffe
3ffff530: feefeffe feefeffe feefeffe feefeffe
3ffff540: feefeffe feefeffe feefeffe feefeffe
3ffff550: feefeffe feefeffe feefeffe feefeffe
3ffff560: feefeffe feefeffe feefeffe feefeffe
3ffff570: feefeffe feefeffe feefeffe feefeffe
3ffff580: feefeffe feefeffe feefeffe feefeffe
3ffff590: feefeffe feefeffe feefeffe feefeffe
3ffff5a0: feefeffe feefeffe feefeffe feefeffe
3ffff5b0: feefeffe feefeffe feefeffe feefeffe
3ffff5c0: feefeffe feefeffe feefeffe feefeffe
3ffff5d0: feefeffe feefeffe feefeffe feefeffe
3ffff5e0: feefeffe feefeffe feefeffe feefeffe
3ffff5f0: feefeffe feefeffe feefeffe feefeffe
3ffff600: feefeffe feefeffe feefeffe feefeffe
3ffff610: feefeffe feefeffe feefeffe feefeffe
3ffff620: feefeffe feefeffe feefeffe feefeffe
3ffff630: feefeffe feefeffe feefeffe feefeffe
3ffff640: feefeffe feefeffe feefeffe feefeffe
3ffff650: feefeffe feefeffe feefeffe feefeffe
3ffff660: feefeffe feefeffe feefeffe feefeffe
3ffff670: feefeffe feefeffe feefeffe feefeffe
3ffff680: feefeffe feefeffe feefeffe feefeffe
3ffff690: feefeffe feefeffe feefeffe feefeffe
3ffff6a0: feefeffe feefeffe feefeffe feefeffe
3ffff6b0: feefeffe feefeffe feefeffe feefeffe
3ffff6c0: feefeffe feefeffe feefeffe feefeffe
3ffff6d0: feefeffe feefeffe feefeffe feefeffe
3ffff6e0: feefeffe feefeffe feefeffe feefeffe
3ffff6f0: feefeffe feefeffe feefeffe feefeffe
3ffff700: feefeffe feefeffe feefeffe feefeffe
3ffff710: feefeffe feefeffe feefeffe feefeffe
3ffff720: feefeffe feefeffe feefeffe feefeffe
3ffff730: feefeffe feefeffe feefeffe feefeffe
3ffff740: feefeffe feefeffe feefeffe feefeffe
3ffff750: feefeffe feefeffe feefeffe feefeffe
3ffff760: feefeffe feefeffe feefeffe feefeffe
3ffff770: feefeffe feefeffe feefeffe feefeffe
3ffff780: feefeffe feefeffe feefeffe feefeffe
3ffff790: feefeffe feefeffe feefeffe feefeffe
3ffff7a0: feefeffe feefeffe feefeffe feefeffe
3ffff7b0: feefeffe feefeffe feefeffe feefeffe
3ffff7c0: feefeffe feefeffe feefeffe feefeffe
3ffff7d0: feefeffe feefeffe feefeffe feefeffe
3ffff7e0: feefeffe feefeffe feefeffe feefeffe
3ffff7f0: feefeffe feefeffe feefeffe feefeffe
3ffff800: feefeffe feefeffe feefeffe feefeffe
3ffff810: feefeffe feefeffe feefeffe feefeffe
3ffff820: feefeffe feefeffe feefeffe feefeffe
3ffff830: feefeffe feefeffe feefeffe feefeffe
3ffff840: feefeffe feefeffe feefeffe feefeffe
3ffff850: feefeffe feefeffe feefeffe feefeffe
3ffff860: feefeffe feefeffe feefeffe feefeffe
3ffff870: feefeffe feefeffe feefeffe feefeffe
3ffff880: feefeffe feefeffe feefeffe feefeffe
3ffff890: feefeffe feefeffe feefeffe feefeffe
3ffff8a0: feefeffe feefeffe feefeffe feefeffe
3ffff8b0: feefeffe feefeffe feefeffe feefeffe
3ffff8c0: feefeffe feefeffe feefeffe feefeffe
3ffff8d0: feefeffe feefeffe feefeffe feefeffe
3ffff8e0: feefeffe feefeffe feefeffe feefeffe
3ffff8f0: feefeffe feefeffe feefeffe feefeffe
3ffff900: feefeffe feefeffe feefeffe feefeffe
3ffff910: feefeffe feefeffe feefeffe feefeffe
3ffff920: feefeffe feefeffe feefeffe feefeffe
3ffff930: feefeffe feefeffe feefeffe feefeffe
3ffff940: feefeffe feefeffe feefeffe feefeffe
3ffff950: feefeffe feefeffe feefeffe feefeffe
3ffff960: feefeffe feefeffe feefeffe feefeffe
3ffff970: feefeffe feefeffe feefeffe feefeffe
3ffff980: feefeffe feefeffe feefeffe feefeffe
3ffff990: feefeffe feefeffe feefeffe feefeffe
3ffff9a0: feefeffe feefeffe feefeffe feefeffe
3ffff9b0: feefeffe feefeffe feefeffe feefeffe
3ffff9c0: feefeffe feefeffe feefeffe feefeffe
3ffff9d0: feefeffe feefeffe feefeffe feefeffe
3ffff9e0: feefeffe feefeffe feefeffe feefeffe
3ffff9f0: feefeffe feefeffe feefeffe feefeffe
3ffffa00: feefeffe feefeffe feefeffe feefeffe
3ffffa10: feefeffe feefeffe feefeffe feefeffe
3ffffa20: feefeffe feefeffe feefeffe feefeffe
3ffffa30: feefeffe feefeffe feefeffe feefeffe
3ffffa40: feefeffe feefeffe feefeffe feefeffe
3ffffa50: feefeffe feefeffe feefeffe feefeffe
3ffffa60: feefeffe feefeffe feefeffe feefeffe
3ffffa70: feefeffe feefeffe feefeffe feefeffe
3ffffa80: feefeffe feefeffe feefeffe feefeffe
3ffffa90: feefeffe feefeffe feefeffe feefeffe
3ffffaa0: feefeffe feefeffe feefeffe feefeffe
3ffffab0: feefeffe feefeffe feefeffe feefeffe
3ffffac0: feefeffe feefeffe feefeffe feefeffe
3ffffad0: feefeffe feefeffe feefeffe feefeffe
3ffffae0: feefeffe feefeffe feefeffe feefeffe
3ffffaf0: feefeffe feefeffe feefeffe feefeffe
3ffffb00: feefeffe feefeffe feefeffe feefeffe
3ffffb10: feefeffe feefeffe feefeffe feefeffe
3ffffb20: feefeffe feefeffe feefeffe feefeffe
3ffffb30: feefeffe feefeffe feefeffe feefeffe
3ffffb40: feefeffe feefeffe feefeffe feefeffe
3ffffb50: feefeffe feefeffe feefeffe feefeffe
3ffffb60: feefeffe feefeffe feefeffe feefeffe
3ffffb70: feefeffe feefeffe feefeffe feefeffe
3ffffb80: feefeffe feefeffe feefeffe feefeffe
3ffffb90: feefeffe feefeffe feefeffe feefeffe
3ffffba0: feefeffe feefeffe feefeffe feefeffe
3ffffbb0: feefeffe feefeffe feefeffe feefeffe
3ffffbc0: feefeffe feefeffe feefeffe feefeffe
3ffffbd0: feefeffe feefeffe feefeffe feefeffe
3ffffbe0: feefeffe feefeffe feefeffe feefeffe
3ffffbf0: feefeffe feefeffe feefeffe feefeffe
3ffffc00: feefeffe feefeffe feefeffe feefeffe
3ffffc10: feefeffe feefeffe feefeffe feefeffe
3ffffc20: feefeffe feefeffe feefeffe feefeffe
3ffffc30: feefeffe feefeffe feefeffe feefeffe
3ffffc40: feefeffe feefeffe feefeffe feefeffe
3ffffc50: feefeffe feefeffe feefeffe feefeffe
3ffffc60: feefeffe feefeffe feefeffe feefeffe
3ffffc70: feefeffe feefeffe feefeffe feefeffe
3ffffc80: feefeffe feefeffe feefeffe feefeffe
3ffffc90: feefeffe feefeffe feefeffe feefeffe
3ffffca0: feefeffe feefeffe feefeffe feefeffe
3ffffcb0: feefeffe feefeffe feefeffe feefeffe
3ffffcc0: feefeffe feefeffe feefeffe feefeffe
3ffffcd0: feefeffe feefeffe feefeffe feefeffe
3ffffce0: feefeffe feefeffe feefeffe feefeffe
3ffffcf0: feefeffe feefeffe feefeffe feefeffe
3ffffd00: feefeffe feefeffe feefeffe feefeffe
3ffffd10: feefeffe feefeffe feefeffe feefeffe
3ffffd20: feefeffe feefeffe feefeffe feefeffe
3ffffd30: feefeffe feefeffe feefeffe feefeffe
3ffffd40: feefeffe feefeffe feefeffe feefeffe
3ffffd50: feefeffe feefeffe feefeffe feefeffe
3ffffd60: feefeffe feefeffe feefeffe feefeffe
3ffffd70: feefeffe feefeffe feefeffe feefeffe
3ffffd80: feefeffe feefeffe feefeffe feefeffe
3ffffd90: feefeffe feefeffe feefeffe feefeffe
3ffffda0: feefeffe feefeffe feefeffe feefeffe
3ffffdb0: feefeffe feefeffe feefeffe feefeffe
3ffffdc0: feefeffe feefeffe feefeffe feefeffe
3ffffdd0: feefeffe feefeffe feefeffe feefeffe
3ffffde0: feefeffe feefeffe feefeffe feefeffe
3ffffdf0: feefeffe feefeffe feefeffe feefeffe
3ffffe00: feefeffe feefeffe feefeffe feefeffe
3ffffe10: feefeffe feefeffe feefeffe feefeffe
3ffffe20: feefeffe feefeffe feefeffe feefeffe
3ffffe30: feefeffe feefeffe feefeffe feefeffe
3ffffe40: feefeffe feefeffe feefeffe feefeffe
3ffffe50: feefeffe feefeffe feefeffe feefeffe
3ffffe60: feefeffe feefeffe feefeffe 4000050c
3ffffe70: 4010141b 00000030 00000011 ffffffff
3ffffe80: 4010141b 00007a12 000003e8 00000000
3ffffe90: 00000007 00000000 000035af fffffffe
3ffffea0: ffffffff 3fffc6fc 00000001 00000064
3ffffeb0: 3ffee38c 00000000 3ffe87c0 00000030
3ffffec0: 00000003 00000000 0000002f 4000dc3c
3ffffed0: 00000000 000f2de0 00000000 3ffee2dc
3ffffee0: 00000000 3fffdad0 3ffee334 00000030
3ffffef0: feefeffe feefeffe feefeffe feefeffe
3fffff00: feefeffe feefeffe feefeffe feefeffe
3fffff10: feefeffe feefeffe feefeffe feefeffe
3fffff20: feefeffe feefeffe feefeffe 3ffeef4c
3fffff30: 40201ded 00000001 00000100 3ffee334
3fffff40: 3fffdad0 00000000 3ffee2dc 402025b8
3fffff50: 40101470 00012024 3ffee38c 00000000
3fffff60: 3ffe87c0 3ffee38c 3ffe8504 3ffee38c
3fffff70: 3fffdad0 3ffee334 40202a88 3fffefa0
3fffff80: 3ffee38c 3fffdad0 00000064 40201467
3fffff90: 00000000 00000000 3ffee304 4020253c
3fffffa0: 3fffdad0 00000000 3ffee304 40202b34
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(3,0)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v951aeffa
~ld
I am using PlatformIO as an Atom plugin to code and flash.
Code: Select all
#include <Arduino.h>
#include "hw_timer.h"
//Debug Setings; Uncomment first line to show debug
// #define IS_DEBUG
#ifdef IS_DEBUG
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
#define STEP_TIME 150 //for n levels, STEP_TIME = 10^6/(2f)(n), where f is the mains frequency
volatile unsigned long interruptTime = 0;
// unsigned long previousMillis = 0;
void timerISR();
void setup(void) {
Serial.begin(115200);
Serial.println("");
// INTERRUPT SETTINGS
noInterrupts();
hw_timer_init(FRC1_SOURCE, 1); //FRC1_SOURCE/NMI_SOURCE, NMI = Non-maskable interrupt
hw_timer_set_func(timerISR);
hw_timer_arm(STEP_TIME);
DEBUG_PRINTLN("Setup done!");
interrupts();
DEBUG_PRINTLN("Interrupt enabled!");
}
void loop(void) {
// unsigned long now = millis();
// if (now - previousMillis > 1000){
DEBUG_PRINTLN(interruptTime);
// previousMillis = now;
// }
delay(100);
}
void ICACHE_RAM_ATTR timerISR() {
interruptTime++;
}
In the main loop, I tried a delay of 10ms, 100ms (as shown), 1000ms, and no delay. Adding a delay was suggested by a colleague. It seems to do nothing. The commented out non-blocking delay was to slow the serial printing.
hw_timer.h
Code: Select all
#ifndef HW_TIMER_H
#define HW_TIMER_H
#ifdef __cplusplus
extern "C" {
#endif
#define US_TO_RTC_TIMER_TICKS(t) \
((t) ? \
(((t) > 0x35A) ? \
(((t)>>2) * ((APB_CLK_FREQ>>4)/250000) + ((t)&0x3) * ((APB_CLK_FREQ>>4)/1000000)) : \
(((t) *(APB_CLK_FREQ>>4)) / 1000000)) : \
0)
#define FRC1_ENABLE_TIMER BIT7
#define FRC1_AUTO_LOAD BIT6
//TIMER PREDIVED MODE
typedef enum {
DIVDED_BY_1 = 0, //timer clock
DIVDED_BY_16 = 4, //divided by 16
DIVDED_BY_256 = 8, //divided by 256
} TIMER_PREDIVED_MODE;
typedef enum { //timer interrupt mode
TM_LEVEL_INT = 1, // level interrupt
TM_EDGE_INT = 0, //edge interrupt
} TIMER_INT_MODE;
typedef enum {
FRC1_SOURCE = 0,
NMI_SOURCE = 1,
} FRC1_TIMER_SOURCE_TYPE;
void hw_timer_arm(u32 val);
void hw_timer_set_func(void (* user_hw_timer_cb_set)(void));
void hw_timer_init(FRC1_TIMER_SOURCE_TYPE source_type, u8 req);
//void ICACHE_RAM_ATTR blink_gpio(void);
//volatile bool state;
#ifdef __cplusplus
}
#endif
#endif /* HW_TIMER_H */
hw_timer.c
Code: Select all
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: hw_timer.c
*
* Description: hw_timer driver
*
* Modification history:
* 2014/5/1, v1.0 create this file.
*******************************************************************************/
//#include "c-types.h"
typedef __SIZE_TYPE__ size_t;
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "hw_timer.h"
/******************************************************************************
* FunctionName : hw_timer_arm
* Description : set a trigger timer delay for this timer.
* Parameters : uint32 val :
in autoload mode
50 ~ 0x7fffff; for FRC1 source.
100 ~ 0x7fffff; for NMI source.
in non autoload mode:
10 ~ 0x7fffff;
* Returns : NONE
*******************************************************************************/
void hw_timer_arm(u32 val)
{
RTC_REG_WRITE(FRC1_LOAD_ADDRESS, US_TO_RTC_TIMER_TICKS(val));
}
static void (* user_hw_timer_cb)(void) = NULL;
/******************************************************************************
* FunctionName : hw_timer_set_func
* Description : set the func, when trigger timer is up.
* Parameters : void (* user_hw_timer_cb_set)(void):
timer callback function,
* Returns : NONE
*******************************************************************************/
void hw_timer_set_func(void (* user_hw_timer_cb_set)(void))
{
user_hw_timer_cb = user_hw_timer_cb_set;
}
static void hw_timer_isr_cb(void)
{
if (user_hw_timer_cb != NULL) {
(*(user_hw_timer_cb))();
}
}
/******************************************************************************
* FunctionName : hw_timer_init
* Description : initilize the hardware isr timer
* Parameters :
FRC1_TIMER_SOURCE_TYPE source_type:
FRC1_SOURCE, timer use frc1 isr as isr source.
NMI_SOURCE, timer use nmi isr as isr source.
u8 req:
0, not autoload,
1, autoload mode,
* Returns : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR hw_timer_init(FRC1_TIMER_SOURCE_TYPE source_type, u8 req)
{
if (req == 1) {
RTC_REG_WRITE(FRC1_CTRL_ADDRESS,
FRC1_AUTO_LOAD | DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT);
} else {
RTC_REG_WRITE(FRC1_CTRL_ADDRESS,
DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT);
}
if (source_type == NMI_SOURCE) {
ETS_FRC_TIMER1_NMI_INTR_ATTACH(hw_timer_isr_cb);
} else {
ETS_FRC_TIMER1_INTR_ATTACH(hw_timer_isr_cb, NULL);
}
TM1_EDGE_INT_ENABLE();
ETS_FRC1_INTR_ENABLE();
}
//-------------------------------Test Code Below--------------------------------------
/*
#if 0
void hw_test_timer_cb(void)
{
static uint16 j = 0;
j++;
if ((WDEV_NOW() - tick_now2) >= 1000000) {
static u32 idx = 1;
tick_now2 = WDEV_NOW();
os_printf("b%u:%d\n", idx++, j);
j = 0;
}
//hw_timer_arm(50);
}
void ICACHE_FLASH_ATTR user_init(void)
{
hw_timer_init(FRC1_SOURCE, 1);
hw_timer_set_func(hw_test_timer_cb);
hw_timer_arm(100);
}
#endif
*/
/*
NOTE:
1 if use nmi source, for autoload timer , the timer setting val can't be less than 100.
2 if use nmi source, this timer has highest priority, can interrupt other isr.
3 if use frc1 source, this timer can't interrupt other isr.
*/
Am I missing anything? What am I doing wrong? I am starting to suspect the background WiFi processes.
Further investigation: After flashing this code, I flashed another code that did contain WiFi credentials of a network I am NOT in (i.e. the ESP8266 will not be able to connect), and flashing this code after worked flawlessly (even below the specs, tried 20μs on FRC1_SOURCE). If I flash a different firmware containing WiFi connection to a network that is accessible to the ESP8266 (i.e. the ESP8266 is able to connect), and then reflash the above exact code, it starts to crash again. This makes me more certain that the background WiFi functions are the cause, but puts me nowhere closer to solving the issue.