Heat pump web IR bridge

I began developing an IR remote for my air‑to‑air heat pump, because the stock remote offers only an on/off timer and no temperature scheduling. While my Pokitto-based IR remote also lacks timer features (for now), it lets me control the heat pump from anywhere via the Internet!

Above is an overview of the system architecture. Pokitto talks to a local computer using serial over USB. The local computer runs local.html in a web browser and employs Web Serial API for serial communication. A remote computer runs remote.html, which writes commands to a ‘command’-file on the web server and continuously polls a ‘status’-file for responses from Pokitto. Likewise, the local computer polls the ‘command’-file for new instructions and writes status updates from Pokitto to the ‘status’-file.

IR protocol for the heat pump uses 38 kHz square‑wave bursts separated by short gaps for logical 0 and longer gaps for logical 1. While there are easier ways to implement an IR transmitter on Pokitto, I wanted to try Pokitto’s State Configurable Timer (SCT). The SCT can be split into two independent 16-bit timers: one generates the 38 kHz carrier square-wave, and the other toggles between ‘pause’ (carrier off) and ‘burst’ (carrier on) states to encode the data bits.


I used Raspberry Pi Pico -based logic analyzer (GitHub - gusmanb/logicanalyzer: 24 channel, 100Msps logic analyzer hardware and software) to study the IR protocol and capture transmissions from the original remote. It was also invaluable when debugging the Pokitto IR transmitter code. In the images above, there is an IR receiver module attached to the Pico and an IR LED attached to the Pokitto. IR receiver’s output and two Pokitto pins are connected to logic analyzer input channels on the Pico.

Pokitto C++ source code and the web pages are on github: github.com/jpfli/HeatPumpWebIRBridge

5 Likes

A nice project! Are you planning to implement Pokitto-only (no network connectivity, just connects to heat-pump via the IR) heat-pump controller SW also? For e.g. Pokitto scheduling different temperatures for different time of day. IIRC Pokittos internal clock needed some adjustment time to time.

1 Like

Yes, my plan is to add temperature scheduling. I’m considering using the Real-Time Clock - I’ll need to test it and see how good the long‑term accuracy is.

3 Likes

Hello, hello! Started working on this project again.

The idea of using DMA has intrigued me for long. Recently I realized, that DMA could be used to handle the IR transmission. Even though the idea of DMA is simple, somehow it always seemed kind of complicated and intimidating to actually try it in practice, but now I finally had a good reason to learn DMA.

I found some DMA examples for Raspberry Pi Pico (in MicroPython), played with those for a while, then moved back to Pokitto and started studying the DMA controller chapter in the datasheet. The first problem I had, was that I couldn’t get Pokitto MicroPython REPL working, and I really wanted an interactive interface for easier experimentation. After considering the options, I thought writing my own minimal REPL with Python-like sytax was the most motivating option.

The REPL currently supports basic arithmetic and logic operations, single data type (32-bit integer), user defined variables, as well as accessing memory and peripheral registers through built-in arrays (mem8, mem16 and mem32). It’s missing features like functions, loops and conditional branching, yet it’s got everything you need to experiment with MCU peripherals like timers and DMA.

My REPL running “Hello world” LED blinking script:
REPL-20

Source code: GitHub - A minimal Python-like REPL for Pokitto


Now that I had a working REPL, I was able to study DMA and write a Python script that uses timer paced DMA to modulate PWM output that drives an IR LED and sends a command to my heat pump. It is handled entirely by DMA - No help from the main processor needed after initiating transmission. Pretty cool!

Adding similar functionality to the C++ program should now be easy.

Below are two Python scripts I wrote to do the IR transmission. dma.py configures DMA channels and sctimer.py configures SCTimer0 as two 16-timers. One generates 38 kHz carrier wave and the other is used to pace DMA transfer and also to modulate the carrier wave.

dma.py
# User data base address
U = 0x1000_1800

# Channel configuration table base address (256-byte aligned)
C = U + 0x200

# ========================================================================
# RELOAD DESCRIPTORS
# ========================================================================
# Each descriptor is 16 bytes:
#   +0x0 Transfer configuration
#   +0x4 Source data end address
#   +0x8 Destination end address
#   +0xc Link to next descriptor

# Loads SCTimer0 halt condition register
mem32[C + 0x20] = 0x0000_0201   # 32-bit, 1 transfer
mem32[C + 0x24] = U + 0x180
mem32[C + 0x28] = 0x5000_c00c
mem32[C + 0x2c] = 0

# Destination: SCTimer0 L-counter MATCH1 register (sets burst length)
mem32[C + 0x30] = 0x0001_110b   # 16-bit, 2 transfers
mem32[C + 0x34] = U + 0x142
mem32[C + 0x38] = 0x5000_c204
mem32[C + 0x3c] = C + 0x40

# Destination: SCTimer0 L-counter MATCH1 register (sets burst length)
mem32[C + 0x40] = 0x0043_0109   # 16-bit, 68 transfers
mem32[C + 0x44] = U + 0x142
mem32[C + 0x48] = 0x5000_c204
mem32[C + 0x4c] = 0

# ========================================================================
# TRANSMISSION DATA - Period Lengths for IR Modulation
# ========================================================================
# Data (21°C):
# [0b0011_0000_1010_0000_0000_0110_0000_1010,    ; word 1 low half
#  0b0110_0000_0000_0000_0000_0000_0000_1101,    ; word 1 high half
#  0b0011_0000_1010_0000_0000_0110_0000_1110,    ; word 2 low half
#  0b0000_0010_0000_0000_0000_0000_0000_1111]    ; word 2 high half

z = 8*46 - 1    # 0: burst + short pause
o = 8*88 - 1    # 1: burst + long pause

# Word 1 low half
mem16[U + 0x000] = z
mem16[U + 0x002] = z
mem16[U + 0x004] = o
mem16[U + 0x006] = o
mem16[U + 0x008] = z
mem16[U + 0x00a] = z
mem16[U + 0x00c] = z
mem16[U + 0x00e] = z
mem16[U + 0x010] = o
mem16[U + 0x012] = z
mem16[U + 0x014] = o
mem16[U + 0x016] = z
mem16[U + 0x018] = z
mem16[U + 0x01a] = z
mem16[U + 0x01c] = z
mem16[U + 0x01e] = z
mem16[U + 0x020] = z
mem16[U + 0x022] = z
mem16[U + 0x024] = z
mem16[U + 0x026] = z
mem16[U + 0x028] = z
mem16[U + 0x02a] = o
mem16[U + 0x02c] = o
mem16[U + 0x02e] = z
mem16[U + 0x030] = z
mem16[U + 0x032] = z
mem16[U + 0x034] = z
mem16[U + 0x036] = z
mem16[U + 0x038] = o
mem16[U + 0x03a] = z
mem16[U + 0x03c] = o
mem16[U + 0x03e] = z

# Footer
mem16[U + 0x040] = z
mem16[U + 0x042] = o
mem16[U + 0x044] = z

# Pause 20 ms
mem16[U + 0x046] = 8*(24+760) - 1

# Word 1 high half
mem16[U + 0x048] = z
mem16[U + 0x04a] = o
mem16[U + 0x04c] = o
mem16[U + 0x04e] = z
mem16[U + 0x050] = z
mem16[U + 0x052] = z
mem16[U + 0x054] = z
mem16[U + 0x056] = z
mem16[U + 0x058] = z
mem16[U + 0x05a] = z
mem16[U + 0x05c] = z
mem16[U + 0x05e] = z
mem16[U + 0x060] = z
mem16[U + 0x062] = z
mem16[U + 0x064] = z
mem16[U + 0x066] = z
mem16[U + 0x068] = z
mem16[U + 0x06a] = z
mem16[U + 0x06c] = z
mem16[U + 0x06e] = z
mem16[U + 0x070] = z
mem16[U + 0x072] = z
mem16[U + 0x074] = z
mem16[U + 0x076] = z
mem16[U + 0x078] = z
mem16[U + 0x07a] = z
mem16[U + 0x07c] = z
mem16[U + 0x07e] = z
mem16[U + 0x080] = o
mem16[U + 0x082] = o
mem16[U + 0x084] = z
mem16[U + 0x086] = o

# Pause 40 ms
mem16[U + 0x088] = 8*(24+1520) - 1

# Word 2 sync burst (9 ms) followed by 4.5 ms pause
mem16[U + 0x08a] = 8*(342+171) - 1

# Word 2 low half
mem16[U + 0x08c] = z
mem16[U + 0x08e] = z
mem16[U + 0x090] = o
mem16[U + 0x092] = o
mem16[U + 0x094] = z
mem16[U + 0x096] = z
mem16[U + 0x098] = z
mem16[U + 0x09a] = z
mem16[U + 0x09c] = o
mem16[U + 0x09e] = z
mem16[U + 0x0a0] = o
mem16[U + 0x0a2] = z
mem16[U + 0x0a4] = z
mem16[U + 0x0a6] = z
mem16[U + 0x0a8] = z
mem16[U + 0x0aa] = z
mem16[U + 0x0ac] = z
mem16[U + 0x0ae] = z
mem16[U + 0x0b0] = z
mem16[U + 0x0b2] = z
mem16[U + 0x0b4] = z
mem16[U + 0x0b6] = o
mem16[U + 0x0b8] = o
mem16[U + 0x0ba] = z
mem16[U + 0x0bc] = z
mem16[U + 0x0be] = z
mem16[U + 0x0c0] = z
mem16[U + 0x0c2] = z
mem16[U + 0x0c4] = o
mem16[U + 0x0c6] = o
mem16[U + 0x0c8] = o
mem16[U + 0x0ca] = z

# Footer
mem16[U + 0x0cc] = z
mem16[U + 0x0ce] = o
mem16[U + 0x0d0] = z

# Pause 20 ms
mem16[U + 0x0d2] = 8*(24+760) - 1

# Word 2 high half
mem16[U + 0x0d4] = z
mem16[U + 0x0d6] = z
mem16[U + 0x0d8] = z
mem16[U + 0x0da] = z
mem16[U + 0x0dc] = z
mem16[U + 0x0de] = z
mem16[U + 0x0e0] = o
mem16[U + 0x0e2] = z
mem16[U + 0x0e4] = z
mem16[U + 0x0e6] = z
mem16[U + 0x0e8] = z
mem16[U + 0x0ea] = z
mem16[U + 0x0ec] = z
mem16[U + 0x0ee] = z
mem16[U + 0x0f0] = z
mem16[U + 0x0f2] = z
mem16[U + 0x0f4] = z
mem16[U + 0x0f6] = z
mem16[U + 0x0f8] = z
mem16[U + 0x0fa] = z
mem16[U + 0x0fc] = z
mem16[U + 0x0fe] = z
mem16[U + 0x100] = z
mem16[U + 0x102] = z
mem16[U + 0x104] = z
mem16[U + 0x106] = z
mem16[U + 0x108] = z
mem16[U + 0x10a] = z
mem16[U + 0x10c] = o
mem16[U + 0x10e] = o
mem16[U + 0x110] = o
mem16[U + 0x112] = o

# Final period (single burst after last bit)
mem16[U + 0x114] = z

# Burst length settings
mem16[U + 0x140] = 8*342 - 1    # Sync burst lenght (9 ms)
mem16[U + 0x142] = 8*24 - 1     # Data burst lenght (632 us)

# Halt condition register reload value
mem32[U + 0x180] = 0x0002_0002  # Halt both counters on next reload

# ========================================================================
# DMA CONTROLLER INITIALIZATION
# ========================================================================

# Enable clock for DMA
mem32[0x4004_8080] = mem32[0x4004_8080] | 0x2000_0000

# Configure DMA Trigger Sources
mem32[0x4002_8000] = 8          # Channel 0 trigger source = SCT0_DMA0
mem32[0x4002_8004] = 8          # Channel 1 trigger source = SCT0_DMA0

# DMA controller base address
D = 0x5000_4000

mem32[D + 0x00] = 1             # Enable DMA controller
mem32[D + 0x08] = C             # Set channel configuration table address
mem32[D + 0x20] = 0b0011        # Enable DMA channels 0 and 1

# Configure channel settings
# bit 1 HWTRIGEN=1 (hardware triggering enabled)
# bit 4 TRIGPOL=1 (active high/rising edge)
# bit 6 TRIGBURST=1 (burst transfer)
# bit 11:8 BURSTPOWER=0 (burst size=1)
mem32[D + 0x400] = 0x052        # Channel 0
mem32[D + 0x410] = 0x052        # Channel 1

# ========================================================================
# CHANNEL DESCRIPTORS
# ========================================================================
# Each descriptor is 16 bytes:
#   +0x0 Reserved
#   +0x4 Source data end address
#   +0x8 Destination end address
#   +0xc Link to next descriptor

# Channel 0 descriptor: Loads SCTimer0 L-counter MATCH0 register (period length)
mem32[C + 0x04] = U + 0x114
mem32[C + 0x08] = 0x5000_c200
mem32[C + 0x0c] = C + 0x20

# Channel 1 descriptor: Loads SCTimer0 L-counter MATCH1 register (burst length)
mem32[C + 0x14] = U + 0x142
mem32[C + 0x18] = 0x5000_c204
mem32[C + 0x1c] = C + 0x30

# ========================================================================
# TRANSFER CONFIGURATION
# ========================================================================
# bit 0 CFGVALID=1 (descriptor is valid and executable)
# bit 1 RELOAD=1 (enable automatic reload after transfer completion)
# bit 2 SWTRIG=0 (hardware trigger, not software)
# bit 3 CLRTRIG=1 (clear trigger after completion)
# bit 9:8 WIDTH=1 (0:8-bit, 1:16-bit, 2:32-bit)
# bit 13:12 SRCINC
# bit 15:14 DSTINC
# bit 25:16 XFERCOUNT (number of transfers -1)
mem32[D + 0x408] = 0x0089_110b  # Channel 0
mem32[D + 0x418] = 0x0043_010b  # Channel 1
sctimer.py
# ========================================================================
# SYSTEM CONFIGURATION - Clock and Reset Control
# ========================================================================
mem32[0x4004_8080] = mem32[0x4004_8080] | 0x8000_0000 # Enable clock for SCTimer
mem32[0x4004_8004] = mem32[0x4004_8004] | 0x200       # Clear SCTimer reset

# Change PIO1_19 function to SCTimer0 output 0
mem32[0x4004_4060 + 4*19] = (mem32[0x4004_4060 + 4*19] & ~0x1f) | 2

# ========================================================================
# SCTIMER INITIALIZATION
# ========================================================================
T = 0x5000_c000                                       # SCTimer0 base

mem32[T + 0x04] = 0x0004_0004                         # Halt counters
mem32[T + 0x00] = mem32[T + 0x00] | 0x0006_0000       # Enable AUTOLIMIT

# Enable DMA request 0 on L-counter reload
mem32[T + 0x5c] = 0x4000_0000

# ========================================================================
# EVENT CONFIGURATION - State Machine Setup
# ========================================================================

# ------------------------------------------------------------------------
# H-Counter Events - Carrier Generation
# ------------------------------------------------------------------------

# Event 2: H-Event, triggered on MATCH1
mem32[T + 0x314] = 0x1011
mem32[T + 0x310] = 0b00000001 # Enabled only in state 0
mem32[T + 0x500] = 0b000100   # Event sets OUT0

# Event 3: H-Event, triggered on MATCH0
mem32[T + 0x31c] = 0x1010
mem32[T + 0x318] = 0b11111111 # Always enabled
mem32[T + 0x504] = 0b001000   # Event clears OUT0

# Event 4: H-Event, set state=1 on OUT1 rising edge
mem32[T + 0x324] = 0xe470
mem32[T + 0x320] = 0b11111111 # Always enabled

# Event 5: H-Event, set state=0 on OUT1 falling edge
mem32[T + 0x32c] = 0x6870
mem32[T + 0x328] = 0b11111111 # Always enabled

# ------------------------------------------------------------------------
# L-Counter Events - Carrier Modulation
# ------------------------------------------------------------------------

# Event 0: L-Event, triggered on MATCH1
mem32[T + 0x304] = 0x1001
mem32[T + 0x300] = 0b11111111 # Always enabled
mem32[T + 0x508] = 0b000001   # Event sets OUT1

# Event 1: L-Event, triggered on MATCH0
mem32[T + 0x30c] = 0x1000
mem32[T + 0x308] = 0b11111111 # Always enabled
mem32[T + 0x50c] = 0b000010   # Event clears OUT1

# ========================================================================
# MATCH REGISTER CONFIGURATION
# ========================================================================
s = 158                       # Prescale factor for L-Counter
t = 8*s                       # H-Counter period ticks (=1264 => 37975 Hz at 48 MHz)
p = 8*43                      # L-Counter base period unit (=344 => 1132 us)

# ------------------------------------------------------------------------
# H-Counter Match Registers - Carrier Period Length and Pulse Width
# ------------------------------------------------------------------------
mem16[T + 0x102] = t - 1            # Period length
mem16[T + 0x202] = mem16[T + 0x102] # Reload value
mem16[T + 0x106] = 3*t/4 - 1        # Pulse width (25 %)
mem16[T + 0x206] = mem16[T + 0x106] # Reload value

# ------------------------------------------------------------------------
# L-Counter Match Registers - Sync Burst and First Data Bit
# ------------------------------------------------------------------------
mem16[T + 0x100] = 8*(342+171) - 1 # First period (9 ms sync burst + 4.5 ms pause)
mem16[T + 0x104] = 8*342 - 1       # Initial 9 ms sync burst
mem16[T + 0x200] = 8*46 - 1        # First data bit (=mem16[U + 0x000])
mem16[T + 0x204] = 8*24 - 1        # Carrier burst length for data bit

# ========================================================================
# FINAL COUNTER CONFIGURATION AND STARTUP
# ========================================================================
mem32[T + 0x0c] = 0           # Reset halt condition
mem32[T + 0x44] = 0           # Reset state

# ------------------------------------------------------------------------
# Set Prescaler and Start Counters
# ------------------------------------------------------------------------
# bit 2 HALT_L=0 (start L-Counter)
# bit 3 CLRCTR_L=1
# bit 12:5 PRE_L=157 (prescale factor -1 for L-Counter)
# bit 18 HALT_H=0 (start H-Counter)
# bit 19 CLRCTR_H=1
mem32[T + 0x04] = 0x0008_0008 | (s - 1)*32
1 Like

I’ve updated the C++ program to employ DMA. Modification went smoothly since I already had a working Python script that uses DMA to handle the IR transmission. You can find link to the source code in the first post.

State Configurable Timer (SCT) is used as before, configured as two separate timers: one produces the 38 kHz carrier square-wave, while the other modulates the carrier (on/off) to form sync and bit bursts, and the pauses between bursts.

What’s changed is that instead of IRQs, the modulating timer now triggers a DMA request for each sync or bit period, and the DMA then copies new reload values for the next period into the timer Match Reload Registers (MATCHREL0 and MATCHREL1). When the entire transmission is complete, there is a final linked DMA operation that writes to the timer Halt Condition Register, allowing the DMA to automatically turn off the timers.

I’m already thinking some other possible uses for DMA…

2 Likes

REPL worked in the early days of Micropython in Pokitto. I was actually using it the very first time I get python to work in Pokitto :slight_smile: . As the frozen bytecode is so much faster, and took less resources, it has been used ever since. There is a compiler flag for REPL but that has not been used since the very beginning so it has probably just broken without no one notising.