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

Adding "Stereo Mixer" to Windows 7 with Conexant sound card

This procedure worked for my laptop (Thinkpad E530) with a Conexant 20671 sound card, but I suspect it will work for other sound cards in the Conexant family. I was playing with CamStudio to do a video capture of a Flash-based cartoon so that I can put it on the WDTV media player and play it on the big screen in the living room for my kids. The video capture worked brilliantly, but to do a sound capture, I needed to do some hacking. Apparently, there was this recording device called "Stereo Mixer" that was pretty standard in the Windows XP days. This allowed you to capture whatever was played to the speaker in all its digital glory. Then under pressure from various organizations on the dark side of the force, Microsoft and soundcard makers starting disabling this wonderful feature from Windows Vista onwards. So after much Googling around, I found out that for most sound cards, the hardware feature is still there, just not enabled on the software side. Unfortunately, to

Hacking a USB-C to slim tip adapter cable to charge the Thinkpad T450s

This hack is inspired by this post . A year ago, I bought an adapter cable for my wife's Thinkpad X1 Carbon (2nd Gen) that allows her to power her laptop with a 60W-capable portable battery (20V x 3A). A USB-C cable goes from the battery into the adapter, which converts it to the slim tip output required by the laptop. Everything works out of the box, so I didn't give much thought about it. Recently, I decided to buy a similar cable for my Thinkpad T450s. I know technically it should work because the T450s can go as low as 45W (20V x 2.25A) in terms of charging (though I have the 65W charger - 20V x 3.25A).  I went with another adapter cable because it was cheaper and also I prefer the single cable design. So imagine my surprise when the cable came and I plugged it into my laptop and it didn't work! The power manager just cycle in and out of charging mode before giving up with an error message saying there is not enough power. After much research and reading the Thinkwiki

Using Google Dashboard to manage your Android device backup

I used to use AppBrain/Fast Web Install to keep track of which apps I have installed on my phone, and to make it easier to reinstall those apps when the phone gets wiped or replaced. But AppBrain had been going down the tubes, and Fast Web Install had always been a hit-and-miss affair. Android's own "backup to the cloud" system had previously been even more unusable. There isn't a place where you can see what has been backed up. And when you setup a new phone with your Google account, you just have to wait and pray that your favorite apps will be restored to the phone. Typically all the stars have to be aligned just right for this to happen. More often than not, after waiting for an hour or so and nothing happens, you just curse under your breath and proceed to install your favorites apps manually via the Play Store. But I just looked again recently and was pleasantly surprised that things are much more civilized now. Firstly there is a place now where you can loo