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!

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

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

Hacking an analog clock to sync with NTP - Part 5

This is how it looks after I have put everything together. The Arduino sketch is available here . The 2 jumper wires soldered to the clock mechanism are connected to pins D0 and D1 on the ESP-12 (in any order). When the device first boots up, it presents an access point which can be connected to via the PC or smartphone. Once connected, the captive portal redirects the web browser to the configuration page:     A custom field has been added to the WiFi configuration page to enter the current clock time in HHMMSS format. Try to set the clock time to as close to the current time as possible using the radial dial at the back of the clock so the clock will have less work to do catching up. In the config page, the HTML5 Geolocation API is also used to obtain your current location (so if your web browser asks if you would like to share your location, answer "yes"). This is then passed to the Google Time Zone API to obtain the time and DST offset of your time z