Skip to main content

ESPCLOCK4 - Generating Tick Pulses

Hardware

I settled on the ESP32 Mini for this project:


A couple of reasons why I chose this board:

- It has a much smaller form factor than the canonical ESP32 dev board, yet have a good number of pins exposed through its double-row header layout. 

- It is one of the cheaper ESP32 dev boards. I got mine for ~A$6 (including shipping) on AliExpress.

Generating Tick Pulses using the ULP

The platformio.ini file looks like this:

[platformio]
default_envs = wemos_d1_mini32

[env:wemos_d1_mini32]
platform = espressif32
framework = arduino
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git
board = wemos_d1_mini32
upload_speed = 912600
monitor_speed = 115200

Here's the test code:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Make 60 ticks on a non-sweeping clock and stop

#include <Arduino.h>
#include <esp32/ulp.h>
#include <driver/adc.h>
#include <driver/rtc_io.h>

// Named indices into RTC_SLOW_MEM
enum { 
  TICK_PIN,    // tick pin: 1 or 2
  TICK_COUNT,  // stop ticking after certain count to check that we have no missed ticks
};

#define RTCVAR_OFFSET           2000                      // RTC vars starts here in RTC_SLOW_MEM (32-bit/8KB => max range = 2047)
#define RTCVAR(var)             (RTC_SLOW_MEM[RTCVAR_OFFSET+var] & UINT16_MAX)
#define RTCVAR_SET(var, value)  RTC_SLOW_MEM[RTCVAR_OFFSET+var] = value
#define TICK_INTERVAL           1000000 - 40000           // delay between calls to  ULP code in usec (1sec - 40msec)
#define TICKPIN1                RTCIO_GPIO25_CHANNEL
#define TICKPIN2                RTCIO_GPIO27_CHANNEL
#define TICKPIN1_GPIO           GPIO_NUM_25 
#define TICKPIN2_GPIO           GPIO_NUM_27 

// Load RTCMEM[var] into reg
#define X_RTC_LOAD(var, reg) \
  I_MOVI(R3, RTCVAR_OFFSET+var), \
I_LD(reg, R3, 0) // Save reg value into RTCMEM[var] #define X_RTC_SAVE(var, reg) \ I_MOVI(R3, RTCVAR_OFFSET+var), \
I_ST(reg, R3, 0) // Save constant value into RTCMEM[var] #define X_RTC_SAVEI(var, value) \ I_MOVI(R3, RTCVAR_OFFSET+var), \
I_MOVI(R0, value), \ I_ST(R0, R3, 0) // Set GPIO pin value to level #define X_GPIO_SET(pin, level) \ I_WR_REG_BIT(RTC_GPIO_OUT_REG, RTC_GPIO_IN_NEXT_S+pin, level) // Delay 40ms (64000 cycles = 8ms) #define X_TICK_LEN() \ I_DELAY(64000), \ I_DELAY(64000), \ I_DELAY(64000), \ I_DELAY(64000), \ I_DELAY(64000) // Branch labels enum { _INC_TICK_COUNT, _PULSE_TICKPIN_2, }; const ulp_insn_t ulp_code[] = { // Stop after 60 ticks X_RTC_LOAD(TICK_COUNT, R0), M_BL(_INC_TICK_COUNT, 60), I_END(), I_HALT(), M_LABEL(_INC_TICK_COUNT), I_ADDI(R0, R0, 1), X_RTC_SAVE(TICK_COUNT, R0), // Decide which pin to tick X_RTC_LOAD(TICK_PIN, R0), M_BGE(_PULSE_TICKPIN_2, 2), // Pulse tick pin 1 X_GPIO_SET(TICKPIN1, 1), X_TICK_LEN(), X_GPIO_SET(TICKPIN1, 0), X_RTC_SAVEI(TICK_PIN, 2), I_HALT(), M_LABEL(_PULSE_TICKPIN_2), // Pulse tick pin 2 X_GPIO_SET(TICKPIN2, 1), X_TICK_LEN(), X_GPIO_SET(TICKPIN2, 0), X_RTC_SAVEI(TICK_PIN, 1), I_HALT() }; void init_vars() { RTCVAR_SET(TICK_PIN, 1); RTCVAR_SET(TICK_COUNT, 0); } void init_gpio_pin(gpio_num_t pin, rtc_gpio_mode_t state, int level) { rtc_gpio_init(pin); rtc_gpio_set_direction(pin, state); rtc_gpio_set_level(pin, level); } void init_gpio() { rtc_gpio_isolate(GPIO_NUM_12); // Reduce current drain through pullups/pulldowns rtc_gpio_isolate(GPIO_NUM_15); esp_deep_sleep_disable_rom_logging(); // Suppress boot messages init_gpio_pin(TICKPIN1_GPIO, RTC_GPIO_MODE_OUTPUT_ONLY, LOW); init_gpio_pin(TICKPIN2_GPIO, RTC_GPIO_MODE_OUTPUT_ONLY, LOW); } void init_ulp() { size_t size = sizeof(ulp_code) / sizeof(ulp_insn_t); ulp_set_wakeup_period(0, TICK_INTERVAL); ulp_process_macros_and_load(0, ulp_code, &size); ulp_run(0); } void setup() { // Reduce CPU frequency to save power setCpuFrequencyMhz(80); esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause(); if (cause != ESP_SLEEP_WAKEUP_ULP) { // Initial startup init_vars(); init_gpio(); init_ulp(); } // Power off RTC FAST MEM during deep sleep to save power esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF); esp_deep_sleep_start(); } void loop() { }

The 2 pins assigned to emit the tick pulses are GPIO25 and GPIO27. The ULP code generates 60 tick pulses (30 on each pin, 40ms pulse width) and stops. Since this is exactly 1 minute, it allows me to easily check if there are any missed ticks. The pulses are fed into the diode bridge in the ESPCLOCK3 circuit, and makes the analog clock tick as expected. 

Using the current meter, the current draw was determined to be 2.62mA.

Next, I used a precision side cutter to remove the red power LED that is always illuminated when power is applied and cannot be turned off.

After the power LED is removed, the current draw of the above code is now 1.37mA.

So we are off to a good start! The power consumption is on par with the ~1.2mA drawn on the ESPCLOCK3, which I am sure can be reduced further through optimization.

Some observations about ULP programming

- I_END() only ends the RTC timer. It does not stop the ULP code. To do that, it needs to be followed by a I_HALT() command.

- Initially, I was under the impression that ulp_set_wakeup_period() is like Javascript's setInterval() i.e. it calls the ULP repeatedly  at the specified interval eg. every second. Turns out I was wrong. The timer only starts counting down when I_HALT() is called. So if I want the ULP code to be called every second, and I know the ULP code takes ~40ms, then I need to subtract that from my wakeup period.

Jut for fun..

I connected the circuit to the sweeping clock that I modded previously, and modified the code slightly to pulse 16 times a second (8 pulses on each pin, 32ms pulse width, spaced 62.5ms apart).

The power draw is measured to be a whooping 14.7mA!

ESPCLOCK1 / ESPCLOCK2 / ESPCLOCK3 / ESPCLOCK4

Comments

Popular posts from this blog

Cooling mod for the X96 Air #2

Previously, I added a USB cooling fan to the X96 Air TV box . The problem with this mod is that the fan is always running, and it runs at full speed. Ideally, the fan should kick in only when the CPU temperature is above a certain threshold. It would be even better if there is a way to control the fan speed. Dan McDonald left me a comment pointing to his project on Github . He basically connected the fan to a USB relay that can be controlled by Python script. His project inspired me to make a similar mod that would make use of the spare D1 Mini boards I have lying around. The plan is to hook up the fan to a MOSFET (2N7000) and control it via PWM. Here's the very simple circuit: The code simply reads a single character from the serial port (0 - 9). 0 will turn the fan off, while 1 - 9 will generate a proportional PWM to drive the fan, with 1 being the lowest and 9 being the highest. Here's the Arduino code: #include <Arduino.h> void setup () { Serial . begin ( 9600 ...

Installing and customizing CoreELEC in X96 Air

I previously installed CoreELEC on another TV Box ( Ugoos X3 Pro ), which unfortunately died after only 9 months during the summer (due to the unit overheating, which I learned is a common problem for cheap Android TV boxes). So this time I purchased a X96 Air  (4GB/32Gb) and had to do the whole thing again. So this is a note-to-self in case I ever have to install CoreELEC again on some other device. Installation of CoreELEC is simple enough by following this guide . Basically, it involves downloading and writing the firmware to a microSD card using usbimager . Then insert the microSD card, reset the unit and hold the reset until the logo appears. The unit will then proceed to boot into CoreELEC. First thing is to connect to WiFi, then enable SSH. This allows me to login via ssh and execute: ceemmc -x from the terminal. This writes CoreELEC to the built-in eMMC storage, after which I am able to remove the microSD card and reboot the unit into CoreELEC via the built-in sto...

DC-DC Buck Stepdown Converter for ESP8266

I am working on a project that requires a step-down converter from 12V to 5V, that will then power a WeMOS D1 Mini. I saw this new mini buck converter based on the usual LM2596 MP2307 , so I thought I'd give it a try. Unfortunately, it didn't work. Although it is supposed to be able to supply up to 1.8A, the D1 Mini was not able to boot up. The 5V pin was being properly supplied, but the 3.3V pin measures at only ~1.3V. So I had to go back to my usual LM2596 module, which is much larger, but works to power the D1 Mini with a 12V source. Here's a great review of the mini buck converter I found while trying to figure out how to make it work. The fact that it has high quiescent current (~60mA) is also mentioned in a few other sources.