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

Update: Line adapter for Ozito Blade Trimmer

Update (Dec 2021): If you access to a 3D printer, I would now recommend this solution , which makes it super easy to replace the trimmer line. I have been using it for a few months now with zero issue.

3D Printer Filament Joiner

I have been looking at various ways of joining 3D printing filaments. One method involves running one end of a filament through a short PTFE tubing, melting it with a lighter or candle, retracting it back into the tubing and immediately plunging the filament to be fused into the tubing: One problem with this method is that you can't really control the temperature at which you melt the filament, so you frequently end up with a brittle joint that breaks upon the slightest bend. Aliexpress even sells a contraption that works along the same line. As it uses a lighter or candle as well, it suffers from the same weakness. I am not even sure why you need a special contraption when a short PTFE tubing will work just as well. Another method involves using shrink tubing/aluminium foil, and a heat gun: But a heat gun is rather expensive, so I wanted to explore other alternatives. The candle + PTFE tubing method actually works quite well when you happen to melt it at the rig

Attiny85 timer programming using Timer1

This Arduino sketch uses Timer1 to drive the LED blinker: 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 /* * Program ATTiny85 to blink LED connected to PB1 at 1s interval. * Assumes ATTiny85 is running at 1MHz internal clock speed. */ #include <avr/io.h> #include <avr/wdt.h> #include <avr/sleep.h> #include <avr/interrupt.h> bool timer1 = false , led = true ; // Interrupt service routine for timer1 ISR(TIMER1_COMPA_vect) { timer1 = true ; } void setup() { // Setup output pins pinMode( 1 , OUTPUT); digitalWrite( 1 , led); set_sleep_mode(SLEEP_MODE_IDLE); // Setup timer1 to interrupt every second TCCR1 = 0 ; // Stop timer TCNT1 = 0 ; // Zero timer GTCCR = _BV(PSR1); // Reset prescaler OCR1A = 243 ; // T = prescaler / 1MHz = 0.004096s; OCR1A = (1s/T) - 1 = 243 OCR1C = 243 ; // Set to same value to reset timer1 to