Skip to content

Instantly share code, notes, and snippets.

View boochow's full-sized avatar
🏠
Working from home

boochow

🏠
Working from home
View GitHub Profile
from mcu import mem32
from machine import Pin,Clock
# GPIO alternate functions
ALT0 = const(4)
def set_PCM_pin():
for pin in (18, 19, 20, 21):
reg = 0x20200000 + 4 * (pin // 10)
bit = (pin % 10) * 3
from machine import *
def test():
saw = []
sqw = []
tri = []
for i in range(64):
saw.append(i*4)
sqw.append(255 if i < 32 else 0)
tri.append(abs(i - 32)*8)
def test2():
saw = []
sqw = []
tri = []
for i in range(64):
saw.append(i*4)
sqw.append(255 if i < 32 else 0)
tri.append(abs(i - 32)*8)
p=PWM(0, pin=18, ms=MS_DISABLE, use_fifo=1, fifo_repeat=0)
def fifo_write(self, buf):
for b in buf:
while (mem32[0x2020c004] & 1):
pass
mem32[0x2020c018] = b
from time import sleep_ms
from machine import PWM
def test():
p=PWM(pin=18)
while True:
for i in range(p.range() // 2):
p.data(i)
sleep_ms(1)
for i in range(p.range() // 2 - 1, 0, -1):
p.data(i)
from time import sleep_ms
def test():
p=PWM(0, pin=18, ms=MS_ENABLE)
p.freq(1000)
p.range(DEFAULT_RANGE)
while True:
for i in range(DEFAULT_RANGE // 2):
p.data(i)
sleep_ms(1)
for i in range(DEFAULT_RANGE // 2 - 1, 0, -1):
@boochow
boochow / pwm.py
Last active October 8, 2018 10:33
# Hardware PWM
MS_DISABLE = const(0)
MS_ENABLE = const(1)
MODE_PWM = const(0)
MODE_SERIALIZER = const(1)
DEFAULT_RANGE = const(960)
class PWM:
# GPIO alternate functions
ALT0 = const(4)
ALT1 = const(5)
ALT5 = const(2)
Pin_AF_PWM = {12:(ALT0,0), 13:(ALT0,1), 18:(ALT5,0), 19:(ALT5,1), \
40:(ALT0,0), 41:(ALT0,1), 45:(ALT0,1), 52:(ALT1,0), 53:(ALT1,1)}
def select_PWM_pin(pin):
if pin in Pin_AF_PWM.keys():
from mcu import mem32
# PWM clock manager
CLK_GND = const(0)
CLK_OSC = const(1)
CLK_PLLA = const(4)
CLK_PLLC = const(5)
CLK_PLLD = const(6)
CLK_HDMI = const(7)
import gpu
import framebuf
import FBConsole
class RPiScreen(framebuf.FrameBuffer):
def __init__(self, width, height):
self.width = width
self.height = height
gpu.fb_init(width,height,screen_w=1920,screen_h=1080)
super().__init__(gpu.fb_data(),width,height,framebuf.RGB565)