Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: Bump MicroPython to v1.22.2. #904

Merged
merged 2 commits into from
Mar 6, 2024
Merged

CI: Bump MicroPython to v1.22.2. #904

merged 2 commits into from
Mar 6, 2024

Conversation

Gadgetoid
Copy link
Member

@Gadgetoid Gadgetoid commented Feb 22, 2024

This patch release includes a number of fixes for RP2, notably:

  • rp2/rp2_dma: fix fetching 'write' buffers for writing not reading
  • rp2/machine_uart: fix event wait in uart.flush() and uart.read()
  • rp2: change machine.I2S and rp2.DMA to use shared DMA IRQ handlers

https://github.com/micropython/micropython/releases/tag/v1.22.2

Changes for release notes

@Gadgetoid
Copy link
Member Author

Gadgetoid commented Feb 27, 2024

We should prepare for this breaking change for our codebase by dropping these deprecated functions: micropython/micropython@9716171

Update: I have added a new branch for tracking MicroPython's master branch (would be cool if we could build this daily or something, which is what I'd wanted to do for a while) and dealing with these upcoming API breaks - #906

@Gadgetoid
Copy link
Member Author

Gadgetoid commented Feb 28, 2024

The change to shared DMA IRQ handlers may adversely affect our code: micropython/micropython@f53ee9f

The only place we used exclusive handlers is in the Hub 75 driver, here:

irq_set_exclusive_handler(DMA_IRQ_0, handler);
irq_set_exclusive_handler(DMA_IRQ_1, handler);

Drivers with irq_add_shared_handler:

@Gadgetoid
Copy link
Member Author

Produces a constant tone while running Galactic Unicorn display output-

(Code borrowed from https://github.com/miketeachman/micropython-i2s-examples/blob/master/examples/play_tone.py)

# The MIT License (MIT)
# Copyright (c) 2022 Mike Teachman
# https://opensource.org/licenses/MIT

# Purpose:  Play a pure audio tone out of a speaker or headphones
#
# - write audio samples containing a pure tone to an I2S amplifier or DAC module
# - tone will play continuously in a loop until
#   a keyboard interrupt is detected or the board is reset
#
# Blocking version
# - the write() method blocks until the entire sample buffer is written to I2S

import os
import math
import struct
from machine import I2S
from machine import Pin

import gc
import time
import random
from galactic import GalacticUnicorn, Channel
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN, PEN_P8
from ulab import numpy

"""
A random, computer effect.
Experiment with the damping, number of spawns and intensity to change the effect.
"""

# MAXIMUM OVERKILL
# machine.freq(250_000_000)

DAMPING_FACTOR = 0.95
NUMBER_OF_LIGHTS = 10
INTENSITY = 20

gu = GalacticUnicorn()
gu.set_brightness(0.5)
graphics = PicoGraphics(DISPLAY_GALACTIC_UNICORN, pen_type=PEN_P8)


# Fill palette with a yellow
r, g, b = (230, 150, 0)
PALETTE_ENTRIES = 255
for x in range(PALETTE_ENTRIES):
    _ = graphics.create_pen(r * x // PALETTE_ENTRIES, g * x // PALETTE_ENTRIES, b)


def update():
    computer[:] *= DAMPING_FACTOR

    # Spawn random drops
    for _ in range(NUMBER_OF_LIGHTS):
        x = random.randint(0, width - 1)
        y = random.randint(0, height - 1)
        computer[y][x] = random.randint(0, INTENSITY)


def draw():
    # Copy the effect to the framebuffer
    memoryview(graphics)[:] = numpy.ndarray(numpy.clip(computer, 0, 1) * (PALETTE_ENTRIES - 1), dtype=numpy.uint8).tobytes()
    gu.update(graphics)


width = GalacticUnicorn.WIDTH
height = GalacticUnicorn.HEIGHT
computer = numpy.zeros((height, width))

t_count = 0
t_total = 0


def update_supercomputer():
    global t_count, t_total
    
    t_total += 1
    if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP):
        gu.adjust_brightness(+0.01)

    if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN):
        gu.adjust_brightness(-0.01)

    if t_total > 30:
        update()
        draw()
        t_total = 0

def make_tone(rate, bits, frequency):
    # create a buffer containing the pure tone samples
    samples_per_cycle = rate // frequency
    sample_size_in_bytes = bits // 8
    samples = bytearray(samples_per_cycle * sample_size_in_bytes)
    volume_reduction_factor = 16
    range = pow(2, bits) // 2 // volume_reduction_factor
    
    if bits == 16:
        format = "<h"
    else:  # assume 32 bits
        format = "<l"
    
    for i in range(samples_per_cycle):
        sample = range + int((range - 1) * math.sin(2 * math.pi * i / samples_per_cycle))
        struct.pack_into(format, samples, i * sample_size_in_bytes, sample)
        
    return samples


# ======= I2S CONFIGURATION =======
SCK_PIN = 10
WS_PIN = 11
SD_PIN = 9
I2S_ID = 0
MUTE_PIN = 22
BUFFER_LENGTH_IN_BYTES = 4096
# ======= I2S CONFIGURATION =======

unmute = Pin(MUTE_PIN, Pin.OUT)
unmute.on()


# ======= AUDIO CONFIGURATION =======
TONE_FREQUENCY_IN_HZ = 440
SAMPLE_SIZE_IN_BITS = 16
FORMAT = I2S.MONO  # only MONO supported in this example
SAMPLE_RATE_IN_HZ = 22_050
# ======= AUDIO CONFIGURATION =======

audio_out = I2S(
    I2S_ID,
    sck=Pin(SCK_PIN),
    ws=Pin(WS_PIN),
    sd=Pin(SD_PIN),
    mode=I2S.TX,
    bits=SAMPLE_SIZE_IN_BITS,
    format=FORMAT,
    rate=SAMPLE_RATE_IN_HZ,
    ibuf=BUFFER_LENGTH_IN_BYTES,
)

samples = make_tone(SAMPLE_RATE_IN_HZ, SAMPLE_SIZE_IN_BITS, TONE_FREQUENCY_IN_HZ)

# continuously write tone sample buffer to an I2S DAC
print("==========  START PLAYBACK ==========")
try:
    while True:
        num_written = audio_out.write(samples)
        update_supercomputer()

except (KeyboardInterrupt,) as e:
    print("caught exception {} {}".format(type(e).__name__, e))


# cleanup
audio_out.deinit()
print("Done")

MicroPython's DMA class uses shared IRQ handlers, which would be
clobbered by Hub75's use of an exclusive handler.

Additionally clean up some dead code (DMA_IRQ_1??), more epxlicitly
clean up the claimed PIOs and programs, and do not use a fixed
DMA channel. This seems to have fixed a bug whereupon Hub75 would
hardlock on the 5th soft reset.
@Gadgetoid Gadgetoid merged commit 6eb0f90 into main Mar 6, 2024
28 checks passed
@Gadgetoid Gadgetoid deleted the ci/micropython-1.22.2 branch October 31, 2024 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant