micropython: add micropython component

This commit is contained in:
KY-zhang-X
2022-09-29 12:10:37 +08:00
parent 1514f1cb9b
commit dd76146324
2679 changed files with 354110 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
import pyb
if not hasattr(pyb, "Accel"):
print("SKIP")
raise SystemExit
accel = pyb.Accel()
print(accel)
accel.x()
accel.y()
accel.z()
accel.tilt()
accel.filtered_xyz()

View File

@@ -0,0 +1 @@
<Accel>

View File

@@ -0,0 +1,63 @@
from pyb import ADC, Timer
adct = ADC(16) # Temperature 930 -> 20C
print(str(adct)[:19])
adcv = ADC(17) # Voltage 1500 -> 3.3V
print(adcv)
# read single sample; 2.5V-5V is pass range
val = adcv.read()
assert val > 1000 and val < 2000
# timer for read_timed
tim = Timer(5, freq=500)
# read into bytearray
buf = bytearray(b"\xff" * 50)
adcv.read_timed(buf, tim)
print(len(buf))
for i in buf:
assert i > 50 and i < 150
# read into arrays with different element sizes
import array
arv = array.array("h", 25 * [0x7FFF])
adcv.read_timed(arv, tim)
print(len(arv))
for i in arv:
assert i > 1000 and i < 2000
arv = array.array("i", 30 * [-1])
adcv.read_timed(arv, tim)
print(len(arv))
for i in arv:
assert i > 1000 and i < 2000
# Test read_timed_multi
arv = bytearray(b"\xff" * 50)
art = bytearray(b"\xff" * 50)
ADC.read_timed_multi((adcv, adct), (arv, art), tim)
for i in arv:
assert i > 60 and i < 125
# Wide range: unsure of accuracy of temp sensor.
for i in art:
assert i > 15 and i < 200
arv = array.array("i", 25 * [-1])
art = array.array("i", 25 * [-1])
ADC.read_timed_multi((adcv, adct), (arv, art), tim)
for i in arv:
assert i > 1000 and i < 2000
# Wide range: unsure of accuracy of temp sensor.
for i in art:
assert i > 50 and i < 2000
arv = array.array("h", 25 * [0x7FFF])
art = array.array("h", 25 * [0x7FFF])
ADC.read_timed_multi((adcv, adct), (arv, art), tim)
for i in arv:
assert i > 1000 and i < 2000
# Wide range: unsure of accuracy of temp sensor.
for i in art:
assert i > 50 and i < 2000

View File

@@ -0,0 +1,5 @@
<ADC on 16 channel=
<ADC on 17 channel=17>
50
25
30

View File

@@ -0,0 +1,31 @@
from pyb import Pin, ADCAll
pins = [Pin.cpu.A0, Pin.cpu.A1, Pin.cpu.A2, Pin.cpu.A3]
# set pins to IN mode, init ADCAll, then check pins are ANALOG
for p in pins:
p.init(p.IN)
adc = ADCAll(12)
for p in pins:
print(p)
# set pins to IN mode, init ADCAll with mask, then check some pins are ANALOG
for p in pins:
p.init(p.IN)
adc = ADCAll(12, 0x70003)
for p in pins:
print(p)
# init all pins to ANALOG
adc = ADCAll(12)
print(adc)
# read all channels
for c in range(19):
print(type(adc.read_channel(c)))
# call special reading functions
print(0 < adc.read_core_temp() < 100)
print(0 < adc.read_core_vbat() < 4)
print(0 < adc.read_core_vref() < 2)
print(0 < adc.read_vref() < 4)

View File

@@ -0,0 +1,32 @@
Pin(Pin.cpu.A0, mode=Pin.ANALOG)
Pin(Pin.cpu.A1, mode=Pin.ANALOG)
Pin(Pin.cpu.A2, mode=Pin.ANALOG)
Pin(Pin.cpu.A3, mode=Pin.ANALOG)
Pin(Pin.cpu.A0, mode=Pin.ANALOG)
Pin(Pin.cpu.A1, mode=Pin.ANALOG)
Pin(Pin.cpu.A2, mode=Pin.IN)
Pin(Pin.cpu.A3, mode=Pin.IN)
<ADCAll>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
True
True
True
True

View File

@@ -0,0 +1,39 @@
# Test board-specific items on PYBv1.x
import os, pyb
if not "PYBv1." in os.uname().machine:
print("SKIP")
raise SystemExit
# test creating UART by id/name
for bus in (1, 2, 3, 4, 5, 6, 7, "XA", "XB", "YA", "YB", "Z"):
try:
pyb.UART(bus, 9600)
print("UART", bus)
except ValueError:
print("ValueError", bus)
# test creating SPI by id/name
for bus in (1, 2, 3, "X", "Y", "Z"):
try:
pyb.SPI(bus)
print("SPI", bus)
except ValueError:
print("ValueError", bus)
# test creating I2C by id/name
for bus in (2, 3, "X", "Y", "Z"):
try:
pyb.I2C(bus)
print("I2C", bus)
except ValueError:
print("ValueError", bus)
# test creating CAN by id/name
for bus in (1, 2, 3, "YA", "YB", "YC"):
try:
pyb.CAN(bus, pyb.CAN.LOOPBACK)
print("CAN", bus)
except ValueError:
print("ValueError", bus)

View File

@@ -0,0 +1,29 @@
UART 1
UART 2
UART 3
UART 4
ValueError 5
UART 6
ValueError 7
UART XA
UART XB
UART YA
UART YB
ValueError Z
SPI 1
SPI 2
ValueError 3
SPI X
SPI Y
ValueError Z
I2C 2
ValueError 3
I2C X
I2C Y
ValueError Z
CAN 1
CAN 2
ValueError 3
CAN YA
CAN YB
ValueError YC

View File

@@ -0,0 +1,314 @@
try:
from pyb import CAN
except ImportError:
print("SKIP")
raise SystemExit
from array import array
import micropython
import pyb
# test we can correctly create by id (2 handled in can2.py test)
for bus in (-1, 0, 1, 3):
try:
CAN(bus, CAN.LOOPBACK)
print("CAN", bus)
except ValueError:
print("ValueError", bus)
CAN(1).deinit()
can = CAN(1)
print(can)
# Test state when de-init'd
print(can.state() == can.STOPPED)
can.init(CAN.LOOPBACK, num_filter_banks=14)
print(can)
print(can.any(0))
# Test state when freshly created
print(can.state() == can.ERROR_ACTIVE)
# Test that restart can be called
can.restart()
# Test info returns a sensible value
print(can.info())
# Catch all filter
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
can.send("abcd", 123, timeout=5000)
print(can.any(0), can.info())
print(can.recv(0))
can.send("abcd", -1, timeout=5000)
print(can.recv(0))
can.send("abcd", 0x7FF + 1, timeout=5000)
print(can.recv(0))
# Test too long message
try:
can.send("abcdefghi", 0x7FF, timeout=5000)
except ValueError:
print("passed")
else:
print("failed")
# Test that recv can work without allocating memory on the heap
buf = bytearray(10)
l = [0, 0, 0, 0, memoryview(buf)]
l2 = None
micropython.heap_lock()
can.send("", 42)
l2 = can.recv(0, l)
assert l is l2
print(l, len(l[4]), buf)
can.send("1234", 42)
l2 = can.recv(0, l)
assert l is l2
print(l, len(l[4]), buf)
can.send("01234567", 42)
l2 = can.recv(0, l)
assert l is l2
print(l, len(l[4]), buf)
can.send("abc", 42)
l2 = can.recv(0, l)
assert l is l2
print(l, len(l[4]), buf)
micropython.heap_unlock()
# Test that recv can work with different arrays behind the memoryview
can.send("abc", 1)
print(bytes(can.recv(0, [0, 0, 0, 0, memoryview(array("B", range(8)))])[4]))
can.send("def", 1)
print(bytes(can.recv(0, [0, 0, 0, 0, memoryview(array("b", range(8)))])[4]))
# Test for non-list passed as second arg to recv
can.send("abc", 1)
try:
can.recv(0, 1)
except TypeError:
print("TypeError")
# Test for too-short-list passed as second arg to recv
can.send("abc", 1)
try:
can.recv(0, [0, 0, 0])
except ValueError:
print("ValueError")
# Test for non-memoryview passed as 4th element to recv
can.send("abc", 1)
try:
can.recv(0, [0, 0, 0, 0, 0])
except TypeError:
print("TypeError")
# Test for read-only-memoryview passed as 4th element to recv
can.send("abc", 1)
try:
can.recv(0, [0, 0, 0, memoryview(bytes(8))])
except ValueError:
print("ValueError")
# Test for bad-typecode-memoryview passed as 4th element to recv
can.send("abc", 1)
try:
can.recv(0, [0, 0, 0, memoryview(array("i", range(8)))])
except ValueError:
print("ValueError")
del can
# Testing extended IDs
print("==== TEST extframe=True ====")
can = CAN(1, CAN.LOOPBACK)
# Catch all filter, but only for extframe's
can.setfilter(0, CAN.MASK32, 0, (0, 0), extframe=True)
print(can)
try:
can.send("abcde", 0x7FF + 1, timeout=5000, extframe=True)
except ValueError:
print("failed")
else:
r = can.recv(0)
if r[0] == 0x7FF + 1 and r[4] == b"abcde":
print("passed")
else:
print("failed, wrong data received")
# Test filters
for n in [0, 8, 16, 24]:
filter_id = 0b00001000 << n
filter_mask = 0b00011100 << n
id_ok = 0b00001010 << n
id_fail = 0b00011010 << n
can.clearfilter(0, extframe=True)
can.setfilter(0, pyb.CAN.MASK32, 0, (filter_id, filter_mask), extframe=True)
can.send("ok", id_ok, timeout=3, extframe=True)
if can.any(0):
msg = can.recv(0)
print((hex(filter_id), hex(filter_mask), hex(msg[0]), msg[1], msg[4]))
can.send("fail", id_fail, timeout=3, extframe=True)
if can.any(0):
msg = can.recv(0)
print((hex(filter_id), hex(filter_mask), hex(msg[0]), msg[1], msg[4]))
del can
# Test RxCallbacks
print("==== TEST rx callbacks ====")
can = CAN(1, CAN.LOOPBACK)
can.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4))
can.setfilter(1, CAN.LIST16, 1, (5, 6, 7, 8))
def cb0(bus, reason):
print("cb0")
if reason == 0:
print("pending")
if reason == 1:
print("full")
if reason == 2:
print("overflow")
def cb1(bus, reason):
print("cb1")
if reason == 0:
print("pending")
if reason == 1:
print("full")
if reason == 2:
print("overflow")
def cb0a(bus, reason):
print("cb0a")
if reason == 0:
print("pending")
if reason == 1:
print("full")
if reason == 2:
print("overflow")
def cb1a(bus, reason):
print("cb1a")
if reason == 0:
print("pending")
if reason == 1:
print("full")
if reason == 2:
print("overflow")
can.rxcallback(0, cb0)
can.rxcallback(1, cb1)
can.send("11111111", 1, timeout=5000)
can.send("22222222", 2, timeout=5000)
can.send("33333333", 3, timeout=5000)
can.rxcallback(0, cb0a)
can.send("44444444", 4, timeout=5000)
can.send("55555555", 5, timeout=5000)
can.send("66666666", 6, timeout=5000)
can.send("77777777", 7, timeout=5000)
can.rxcallback(1, cb1a)
can.send("88888888", 8, timeout=5000)
print(can.recv(0))
print(can.recv(0))
print(can.recv(0))
print(can.recv(1))
print(can.recv(1))
print(can.recv(1))
can.send("11111111", 1, timeout=5000)
can.send("55555555", 5, timeout=5000)
print(can.recv(0))
print(can.recv(1))
del can
# Testing asynchronous send
print("==== TEST async send ====")
can = CAN(1, CAN.LOOPBACK)
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
while can.any(0):
can.recv(0)
can.send("abcde", 1, timeout=0)
print(can.any(0))
while not can.any(0):
pass
print(can.recv(0))
try:
can.send("abcde", 2, timeout=0)
can.send("abcde", 3, timeout=0)
can.send("abcde", 4, timeout=0)
can.send("abcde", 5, timeout=0)
except OSError as e:
if str(e) == "16":
print("passed")
else:
print("failed")
pyb.delay(500)
while can.any(0):
print(can.recv(0))
# Testing rtr messages
print("==== TEST rtr messages ====")
bus1 = CAN(1, CAN.LOOPBACK)
while bus1.any(0):
bus1.recv(0)
bus1.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4))
bus1.setfilter(1, CAN.LIST16, 0, (5, 6, 7, 8), rtr=(True, True, True, True))
bus1.setfilter(2, CAN.MASK16, 0, (64, 64, 32, 32), rtr=(False, True))
bus1.send("", 1, rtr=True)
print(bus1.any(0))
bus1.send("", 5, rtr=True)
print(bus1.recv(0))
bus1.send("", 6, rtr=True)
print(bus1.recv(0))
bus1.send("", 7, rtr=True)
print(bus1.recv(0))
bus1.send("", 16, rtr=True)
print(bus1.any(0))
bus1.send("", 32, rtr=True)
print(bus1.recv(0))
# test HAL error, timeout
print("==== TEST errors ====")
can = pyb.CAN(1, pyb.CAN.NORMAL)
try:
can.send("1", 1, timeout=50)
except OSError as e:
print(repr(e))

View File

@@ -0,0 +1,74 @@
ValueError -1
ValueError 0
CAN 1
ValueError 3
CAN(1)
True
CAN(1, CAN.LOOPBACK, auto_restart=False)
False
True
[0, 0, 0, 0, 0, 0, 0, 0]
True [0, 0, 0, 0, 0, 0, 1, 0]
(123, False, False, 0, b'abcd')
(2047, False, False, 0, b'abcd')
(0, False, False, 0, b'abcd')
passed
[42, False, False, 0, <memoryview>] 0 bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
[42, False, False, 0, <memoryview>] 4 bytearray(b'1234\x00\x00\x00\x00\x00\x00')
[42, False, False, 0, <memoryview>] 8 bytearray(b'01234567\x00\x00')
[42, False, False, 0, <memoryview>] 3 bytearray(b'abc34567\x00\x00')
b'abc'
b'def'
TypeError
ValueError
TypeError
ValueError
ValueError
==== TEST extframe=True ====
CAN(1, CAN.LOOPBACK, auto_restart=False)
passed
('0x8', '0x1c', '0xa', True, b'ok')
('0x800', '0x1c00', '0xa00', True, b'ok')
('0x80000', '0x1c0000', '0xa0000', True, b'ok')
('0x8000000', '0x1c000000', '0xa000000', True, b'ok')
==== TEST rx callbacks ====
cb0
pending
cb0
full
cb0a
overflow
cb1
pending
cb1
full
cb1a
overflow
(1, False, False, 0, b'11111111')
(2, False, False, 1, b'22222222')
(4, False, False, 3, b'44444444')
(5, False, False, 0, b'55555555')
(6, False, False, 1, b'66666666')
(8, False, False, 3, b'88888888')
cb0a
pending
cb1a
pending
(1, False, False, 0, b'11111111')
(5, False, False, 0, b'55555555')
==== TEST async send ====
False
(1, False, False, 0, b'abcde')
passed
(2, False, False, 0, b'abcde')
(3, False, False, 0, b'abcde')
(4, False, False, 0, b'abcde')
==== TEST rtr messages ====
False
(5, False, True, 4, b'')
(6, False, True, 5, b'')
(7, False, True, 6, b'')
False
(32, False, True, 9, b'')
==== TEST errors ====
OSError(110,)

View File

@@ -0,0 +1,25 @@
try:
from pyb import CAN
CAN(2)
except (ImportError, ValueError):
print("SKIP")
raise SystemExit
# Testing rtr messages
bus2 = CAN(2, CAN.LOOPBACK)
while bus2.any(0):
bus2.recv(0)
bus2.setfilter(0, CAN.LIST32, 0, (1, 2), rtr=(True, True), extframe=True)
bus2.setfilter(1, CAN.LIST32, 0, (3, 4), rtr=(True, False), extframe=True)
bus2.setfilter(2, CAN.MASK32, 0, (16, 16), rtr=(False,), extframe=True)
bus2.setfilter(2, CAN.MASK32, 0, (32, 32), rtr=(True,), extframe=True)
bus2.send("", 1, rtr=True, extframe=True)
print(bus2.recv(0))
bus2.send("", 2, rtr=True, extframe=True)
print(bus2.recv(0))
bus2.send("", 3, rtr=True, extframe=True)
print(bus2.recv(0))
bus2.send("", 4, rtr=True, extframe=True)
print(bus2.any(0))

View File

@@ -0,0 +1,4 @@
(1, True, True, 0, b'')
(2, True, True, 1, b'')
(3, True, True, 2, b'')
False

View File

@@ -0,0 +1,18 @@
import pyb
if not hasattr(pyb, "DAC"):
print("SKIP")
raise SystemExit
dac = pyb.DAC(1)
print(dac)
dac.noise(100)
dac.triangle(100)
dac.write(0)
dac.write_timed(bytearray(10), 100, mode=pyb.DAC.NORMAL)
pyb.delay(20)
dac.write(0)
# test buffering arg
dac = pyb.DAC(1, buffering=True)
dac.write(0)

View File

@@ -0,0 +1 @@
DAC(1, bits=8)

View File

@@ -0,0 +1,17 @@
import pyb
# test basic functionality
ext = pyb.ExtInt("X5", pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_DOWN, lambda l: print("line:", l))
ext.disable()
ext.enable()
print(ext.line())
ext.swint()
# test swint while disabled, then again after re-enabled
ext.disable()
ext.swint()
ext.enable()
ext.swint()
# disable now that the test is finished
ext.disable()

View File

@@ -0,0 +1,3 @@
4
line: 4
line: 4

View File

@@ -0,0 +1,16 @@
import pyb
from pyb import I2C
# test we can correctly create by id
for bus in (-1, 0, 1):
try:
I2C(bus)
print("I2C", bus)
except ValueError:
print("ValueError", bus)
i2c = I2C(1)
i2c.init(I2C.CONTROLLER, baudrate=400000)
print(i2c.scan())
i2c.deinit()

View File

@@ -0,0 +1,4 @@
ValueError -1
ValueError 0
I2C 1
[]

View File

@@ -0,0 +1,23 @@
# use accelerometer to test i2c bus
import pyb
from pyb import I2C
if not hasattr(pyb, "Accel"):
print("SKIP")
raise SystemExit
accel_addr = 76
pyb.Accel() # this will init the MMA for us
i2c = I2C(1, I2C.CONTROLLER, baudrate=400000)
print(i2c.scan())
print(i2c.is_ready(accel_addr))
print(i2c.mem_read(1, accel_addr, 7, timeout=500))
i2c.mem_write(0, accel_addr, 0, timeout=500)
i2c.send(7, addr=accel_addr)
i2c.recv(1, addr=accel_addr)

View File

@@ -0,0 +1,3 @@
[76]
True
b'\x01'

View File

@@ -0,0 +1,54 @@
# test I2C errors, with polling (disabled irqs) and DMA
import pyb
from pyb import I2C
if not hasattr(pyb, "Accel"):
print("SKIP")
raise SystemExit
# init accelerometer
pyb.Accel()
# get I2C bus
i2c = I2C(1, I2C.CONTROLLER, dma=True)
# test polling mem_read
pyb.disable_irq()
i2c.mem_read(1, 76, 0x0A) # should succeed
pyb.enable_irq()
try:
pyb.disable_irq()
i2c.mem_read(1, 77, 0x0A) # should fail
except OSError as e:
pyb.enable_irq()
print(repr(e))
i2c.mem_read(1, 76, 0x0A) # should succeed
# test polling mem_write
pyb.disable_irq()
i2c.mem_write(1, 76, 0x0A) # should succeed
pyb.enable_irq()
try:
pyb.disable_irq()
i2c.mem_write(1, 77, 0x0A) # should fail
except OSError as e:
pyb.enable_irq()
print(repr(e))
i2c.mem_write(1, 76, 0x0A) # should succeed
# test DMA mem_read
i2c.mem_read(1, 76, 0x0A) # should succeed
try:
i2c.mem_read(1, 77, 0x0A) # should fail
except OSError as e:
print(repr(e))
i2c.mem_read(1, 76, 0x0A) # should succeed
# test DMA mem_write
i2c.mem_write(1, 76, 0x0A) # should succeed
try:
i2c.mem_write(1, 77, 0x0A) # should fail
except OSError as e:
print(repr(e))
i2c.mem_write(1, 76, 0x0A) # should succeed

View File

@@ -0,0 +1,4 @@
OSError(5,)
OSError(5,)
OSError(5,)
OSError(5,)

View File

@@ -0,0 +1,24 @@
import pyb
def test_irq():
# test basic disable/enable
i1 = pyb.disable_irq()
print(i1)
pyb.enable_irq() # by default should enable IRQ
# check that interrupts are enabled by waiting for ticks
pyb.delay(10)
# check nested disable/enable
i1 = pyb.disable_irq()
i2 = pyb.disable_irq()
print(i1, i2)
pyb.enable_irq(i2)
pyb.enable_irq(i1)
# check that interrupts are enabled by waiting for ticks
pyb.delay(10)
test_irq()

View File

@@ -0,0 +1,2 @@
True
True False

View File

@@ -0,0 +1,42 @@
import os, pyb
machine = os.uname().machine
if "PYBv1." in machine or "PYBLITEv1." in machine:
leds = [pyb.LED(i) for i in range(1, 5)]
pwm_leds = leds[2:]
elif "PYBD" in machine:
leds = [pyb.LED(i) for i in range(1, 4)]
pwm_leds = []
else:
print("SKIP")
raise SystemExit
# test printing
for i in range(3):
print(leds[i])
# test on and off
for l in leds:
l.on()
assert l.intensity() == 255
pyb.delay(100)
l.off()
assert l.intensity() == 0
pyb.delay(100)
# test toggle
for l in 2 * leds:
l.toggle()
assert l.intensity() in (0, 255)
pyb.delay(100)
# test intensity
for l in pwm_leds:
for i in range(256):
l.intensity(i)
assert l.intensity() == i
pyb.delay(1)
for i in range(255, -1, -1):
l.intensity(i)
assert l.intensity() == i
pyb.delay(1)

View File

@@ -0,0 +1,3 @@
LED(1)
LED(2)
LED(3)

View File

@@ -0,0 +1,13 @@
# test stm module
import stm
import pyb
# test storing a full 32-bit number
# turn on then off the A15(=yellow) LED
BSRR = 0x18
stm.mem32[stm.GPIOA + BSRR] = 0x00008000
pyb.delay(100)
print(hex(stm.mem32[stm.GPIOA + stm.GPIO_ODR] & 0x00008000))
stm.mem32[stm.GPIOA + BSRR] = 0x80000000
print(hex(stm.mem32[stm.GPIOA + stm.GPIO_ODR] & 0x00008000))

View File

@@ -0,0 +1,2 @@
0x8000
0x0

View File

@@ -0,0 +1,78 @@
import time
DAYS_PER_MONTH = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def is_leap(year):
return (year % 4) == 0
def test():
seconds = 0
wday = 5 # Jan 1, 2000 was a Saturday
for year in range(2000, 2034):
print("Testing %d" % year)
yday = 1
for month in range(1, 13):
if month == 2 and is_leap(year):
DAYS_PER_MONTH[2] = 29
else:
DAYS_PER_MONTH[2] = 28
for day in range(1, DAYS_PER_MONTH[month] + 1):
secs = time.mktime((year, month, day, 0, 0, 0, 0, 0))
if secs != seconds:
print(
"mktime failed for %d-%02d-%02d got %d expected %d"
% (year, month, day, secs, seconds)
)
tuple = time.localtime(seconds)
secs = time.mktime(tuple)
if secs != seconds:
print(
"localtime failed for %d-%02d-%02d got %d expected %d"
% (year, month, day, secs, seconds)
)
return
seconds += 86400
if yday != tuple[7]:
print(
"locatime for %d-%02d-%02d got yday %d, expecting %d"
% (year, month, day, tuple[7], yday)
)
return
if wday != tuple[6]:
print(
"locatime for %d-%02d-%02d got wday %d, expecting %d"
% (year, month, day, tuple[6], wday)
)
return
yday += 1
wday = (wday + 1) % 7
def spot_test(seconds, expected_time):
actual_time = time.localtime(seconds)
for i in range(len(actual_time)):
if actual_time[i] != expected_time[i]:
print(
"time.localtime(", seconds, ") returned", actual_time, "expecting", expected_time
)
return
print("time.localtime(", seconds, ") returned", actual_time, "(pass)")
test()
# fmt: off
spot_test( 0, (2000, 1, 1, 0, 0, 0, 5, 1))
spot_test( 1, (2000, 1, 1, 0, 0, 1, 5, 1))
spot_test( 59, (2000, 1, 1, 0, 0, 59, 5, 1))
spot_test( 60, (2000, 1, 1, 0, 1, 0, 5, 1))
spot_test( 3599, (2000, 1, 1, 0, 59, 59, 5, 1))
spot_test( 3600, (2000, 1, 1, 1, 0, 0, 5, 1))
spot_test( -1, (1999, 12, 31, 23, 59, 59, 4, 365))
spot_test( 447549467, (2014, 3, 7, 23, 17, 47, 4, 66))
spot_test( -940984933, (1970, 3, 7, 23, 17, 47, 5, 66))
spot_test(-1072915199, (1966, 1, 1, 0, 0, 1, 5, 1))
spot_test(-1072915200, (1966, 1, 1, 0, 0, 0, 5, 1))
spot_test(-1072915201, (1965, 12, 31, 23, 59, 59, 4, 365))
# fmt: on

View File

@@ -0,0 +1,46 @@
Testing 2000
Testing 2001
Testing 2002
Testing 2003
Testing 2004
Testing 2005
Testing 2006
Testing 2007
Testing 2008
Testing 2009
Testing 2010
Testing 2011
Testing 2012
Testing 2013
Testing 2014
Testing 2015
Testing 2016
Testing 2017
Testing 2018
Testing 2019
Testing 2020
Testing 2021
Testing 2022
Testing 2023
Testing 2024
Testing 2025
Testing 2026
Testing 2027
Testing 2028
Testing 2029
Testing 2030
Testing 2031
Testing 2032
Testing 2033
time.localtime( 0 ) returned (2000, 1, 1, 0, 0, 0, 5, 1) (pass)
time.localtime( 1 ) returned (2000, 1, 1, 0, 0, 1, 5, 1) (pass)
time.localtime( 59 ) returned (2000, 1, 1, 0, 0, 59, 5, 1) (pass)
time.localtime( 60 ) returned (2000, 1, 1, 0, 1, 0, 5, 1) (pass)
time.localtime( 3599 ) returned (2000, 1, 1, 0, 59, 59, 5, 1) (pass)
time.localtime( 3600 ) returned (2000, 1, 1, 1, 0, 0, 5, 1) (pass)
time.localtime( -1 ) returned (1999, 12, 31, 23, 59, 59, 4, 365) (pass)
time.localtime( 447549467 ) returned (2014, 3, 7, 23, 17, 47, 4, 66) (pass)
time.localtime( -940984933 ) returned (1970, 3, 7, 23, 17, 47, 5, 66) (pass)
time.localtime( -1072915199 ) returned (1966, 1, 1, 0, 0, 1, 5, 1) (pass)
time.localtime( -1072915200 ) returned (1966, 1, 1, 0, 0, 0, 5, 1) (pass)
time.localtime( -1072915201 ) returned (1965, 12, 31, 23, 59, 59, 4, 365) (pass)

View File

@@ -0,0 +1,33 @@
from pyb import Pin
p = Pin("X8", Pin.IN)
print(p)
print(p.name())
print(p.pin())
print(p.port())
p = Pin("X8", Pin.IN, Pin.PULL_UP)
p = Pin("X8", Pin.IN, pull=Pin.PULL_UP)
p = Pin("X8", mode=Pin.IN, pull=Pin.PULL_UP)
print(p)
print(p.value())
p.init(p.IN, p.PULL_DOWN)
p.init(p.IN, pull=p.PULL_DOWN)
p.init(mode=p.IN, pull=p.PULL_DOWN)
print(p)
print(p.value())
p.init(p.OUT_PP)
p.low()
print(p.value())
p.high()
print(p.value())
p.value(0)
print(p.value())
p.value(1)
print(p.value())
p.value(False)
print(p.value())
p.value(True)
print(p.value())

View File

@@ -0,0 +1,14 @@
Pin(Pin.cpu.A7, mode=Pin.IN)
A7
7
0
Pin(Pin.cpu.A7, mode=Pin.IN, pull=Pin.PULL_UP)
1
Pin(Pin.cpu.A7, mode=Pin.IN, pull=Pin.PULL_DOWN)
0
0
1
0
1
0
1

View File

@@ -0,0 +1,39 @@
# basic tests of pyb module
import pyb
# test delay
pyb.delay(-1)
pyb.delay(0)
pyb.delay(1)
start = pyb.millis()
pyb.delay(17)
print((pyb.millis() - start) // 5) # should print 3
# test udelay
pyb.udelay(-1)
pyb.udelay(0)
pyb.udelay(1)
start = pyb.millis()
pyb.udelay(17000)
print((pyb.millis() - start) // 5) # should print 3
# other
pyb.disable_irq()
pyb.enable_irq()
print(pyb.have_cdc())
pyb.sync()
print(len(pyb.unique_id()))
pyb.wfi()
pyb.fault_debug(True)
pyb.fault_debug(False)

View File

@@ -0,0 +1,4 @@
3
3
True
12

View File

@@ -0,0 +1,17 @@
# test pyb module on F405 MCUs
import os, pyb
if not "STM32F405" in os.uname().machine:
print("SKIP")
raise SystemExit
print(pyb.freq())
print(type(pyb.rng()))
# test HAL error specific to F405
i2c = pyb.I2C(2, pyb.I2C.CONTROLLER)
try:
i2c.recv(1, 1)
except OSError as e:
print(repr(e))

View File

@@ -0,0 +1,3 @@
(168000000, 168000000, 42000000, 84000000)
<class 'int'>
OSError(5,)

View File

@@ -0,0 +1,9 @@
# test pyb module on F411 MCUs
import os, pyb
if not "STM32F411" in os.uname().machine:
print("SKIP")
raise SystemExit
print(pyb.freq())

View File

@@ -0,0 +1 @@
(96000000, 96000000, 24000000, 48000000)

View File

@@ -0,0 +1,84 @@
import pyb, stm
from pyb import RTC
rtc = RTC()
rtc.init()
print(rtc)
# make sure that 1 second passes correctly
rtc.datetime((2014, 1, 1, 1, 0, 0, 0, 0))
pyb.delay(1002)
print(rtc.datetime()[:7])
def set_and_print(datetime):
rtc.datetime(datetime)
print(rtc.datetime()[:7])
# make sure that setting works correctly
set_and_print((2000, 1, 1, 1, 0, 0, 0, 0))
set_and_print((2000, 1, 31, 1, 0, 0, 0, 0))
set_and_print((2000, 12, 31, 1, 0, 0, 0, 0))
set_and_print((2016, 12, 31, 1, 0, 0, 0, 0))
set_and_print((2016, 12, 31, 7, 0, 0, 0, 0))
set_and_print((2016, 12, 31, 7, 1, 0, 0, 0))
set_and_print((2016, 12, 31, 7, 12, 0, 0, 0))
set_and_print((2016, 12, 31, 7, 13, 0, 0, 0))
set_and_print((2016, 12, 31, 7, 23, 0, 0, 0))
set_and_print((2016, 12, 31, 7, 23, 1, 0, 0))
set_and_print((2016, 12, 31, 7, 23, 59, 0, 0))
set_and_print((2016, 12, 31, 7, 23, 59, 1, 0))
set_and_print((2016, 12, 31, 7, 23, 59, 59, 0))
set_and_print((2099, 12, 31, 7, 23, 59, 59, 0))
# check that calibration works correctly
# save existing calibration value:
cal_tmp = rtc.calibration()
def set_and_print_calib(cal):
rtc.calibration(cal)
print(rtc.calibration())
set_and_print_calib(512)
set_and_print_calib(511)
set_and_print_calib(345)
set_and_print_calib(1)
set_and_print_calib(0)
set_and_print_calib(-1)
set_and_print_calib(-123)
set_and_print_calib(-510)
set_and_print_calib(-511)
# restore existing calibration value
rtc.calibration(cal_tmp)
# Check register settings for wakeup
def set_and_print_wakeup(ms):
try:
rtc.wakeup(ms)
wucksel = stm.mem32[stm.RTC + stm.RTC_CR] & 7
wut = stm.mem32[stm.RTC + stm.RTC_WUTR] & 0xFFFF
except ValueError:
wucksel = -1
wut = -1
print((wucksel, wut))
set_and_print_wakeup(0)
set_and_print_wakeup(1)
set_and_print_wakeup(4000)
set_and_print_wakeup(4001)
set_and_print_wakeup(8000)
set_and_print_wakeup(8001)
set_and_print_wakeup(16000)
set_and_print_wakeup(16001)
set_and_print_wakeup(32000)
set_and_print_wakeup(32001)
set_and_print_wakeup(0x10000 * 1000)
set_and_print_wakeup(0x10001 * 1000)
set_and_print_wakeup(0x1FFFF * 1000)
set_and_print_wakeup(0x20000 * 1000)
set_and_print_wakeup(0x20001 * 1000) # exception

View File

@@ -0,0 +1,40 @@
<RTC>
(2014, 1, 1, 1, 0, 0, 1)
(2000, 1, 1, 1, 0, 0, 0)
(2000, 1, 31, 1, 0, 0, 0)
(2000, 12, 31, 1, 0, 0, 0)
(2016, 12, 31, 1, 0, 0, 0)
(2016, 12, 31, 7, 0, 0, 0)
(2016, 12, 31, 7, 1, 0, 0)
(2016, 12, 31, 7, 12, 0, 0)
(2016, 12, 31, 7, 13, 0, 0)
(2016, 12, 31, 7, 23, 0, 0)
(2016, 12, 31, 7, 23, 1, 0)
(2016, 12, 31, 7, 23, 59, 0)
(2016, 12, 31, 7, 23, 59, 1)
(2016, 12, 31, 7, 23, 59, 59)
(2099, 12, 31, 7, 23, 59, 59)
512
511
345
1
0
-1
-123
-510
-511
(3, 0)
(3, 15)
(3, 65535)
(2, 32775)
(2, 65535)
(1, 32771)
(1, 65535)
(0, 32769)
(0, 65535)
(4, 31)
(4, 65535)
(6, 0)
(6, 65534)
(6, 65535)
(-1, -1)

View File

@@ -0,0 +1,16 @@
from pyb import Servo
servo = Servo(1)
print(servo)
servo.angle(0)
servo.angle(10, 100)
servo.speed(-10)
servo.speed(10, 100)
servo.pulse_width(1500)
print(servo.pulse_width())
servo.calibration(630, 2410, 1490, 2460, 2190)
print(servo.calibration())

View File

@@ -0,0 +1,3 @@
<Servo 1 at 1500us>
1500
(630, 2410, 1490, 2460, 2190)

View File

@@ -0,0 +1,35 @@
from pyb import SPI
# test we can correctly create by id
for bus in (-1, 0, 1, 2):
try:
SPI(bus)
print("SPI", bus)
except ValueError:
print("ValueError", bus)
spi = SPI(1)
print(spi)
spi = SPI(1, SPI.CONTROLLER)
spi = SPI(1, SPI.CONTROLLER, baudrate=500000)
spi = SPI(
1, SPI.CONTROLLER, 500000, polarity=1, phase=0, bits=8, firstbit=SPI.MSB, ti=False, crc=None
)
print(str(spi)[:32], str(spi)[53:]) # don't print baudrate/prescaler
spi.init(SPI.PERIPHERAL, phase=1)
print(spi)
try:
# need to flush input before we get an error (error is what we want to test)
for i in range(10):
spi.recv(1, timeout=100)
except OSError:
print("OSError")
spi.init(SPI.CONTROLLER)
spi.send(1, timeout=100)
print(spi.recv(1, timeout=100))
print(spi.send_recv(1, timeout=100))
spi.deinit()

View File

@@ -0,0 +1,10 @@
ValueError -1
ValueError 0
SPI 1
SPI 2
SPI(1)
SPI(1, SPI.CONTROLLER, baudrate= , polarity=1, phase=0, bits=8)
SPI(1, SPI.PERIPHERAL, polarity=1, phase=1, bits=8)
OSError
b'\xff'
b'\xff'

View File

@@ -0,0 +1,6 @@
from pyb import Switch
sw = Switch()
print(sw())
sw.callback(print)
sw.callback(None)

View File

@@ -0,0 +1 @@
False

View File

@@ -0,0 +1,19 @@
# check basic functionality of the timer class
import pyb
from pyb import Timer
tim = Timer(4)
tim = Timer(4, prescaler=100, period=200)
print(tim.prescaler())
print(tim.period())
tim.prescaler(300)
print(tim.prescaler())
tim.period(400)
print(tim.period())
# Setting and printing frequency
tim = Timer(2, freq=100)
print(tim.freq())
tim.freq(0.001)
print("{:.3f}".format(tim.freq()))

View File

@@ -0,0 +1,6 @@
100
200
300
400
100
0.001

View File

@@ -0,0 +1,54 @@
# check callback feature of the timer class
import pyb
from pyb import Timer
# callback function that disables the callback when called
def cb1(t):
print("cb1")
t.callback(None)
# callback function that disables the timer when called
def cb2(t):
print("cb2")
t.deinit()
# callback where cb4 closes over cb3.y
def cb3(x):
y = x
def cb4(t):
print("cb4", y)
t.callback(None)
return cb4
# create a timer with a callback, using callback(None) to stop
tim = Timer(1, freq=100, callback=cb1)
pyb.delay(5)
print("before cb1")
pyb.delay(15)
# create a timer with a callback, using deinit to stop
tim = Timer(2, freq=100, callback=cb2)
pyb.delay(5)
print("before cb2")
pyb.delay(15)
# create a timer, then set the freq, then set the callback
tim = Timer(4)
tim.init(freq=100)
tim.callback(cb1)
pyb.delay(5)
print("before cb1")
pyb.delay(15)
# test callback with a closure
tim.init(freq=100)
tim.callback(cb3(3))
pyb.delay(5)
print("before cb4")
pyb.delay(15)

View File

@@ -0,0 +1,8 @@
before cb1
cb1
before cb2
cb2
before cb1
cb1
before cb4
cb4 3

View File

@@ -0,0 +1,44 @@
from pyb import UART
# test we can correctly create by id
for bus in (-1, 0, 1, 2, 5, 6):
try:
UART(bus, 9600)
print("UART", bus)
except ValueError:
print("ValueError", bus)
uart = UART(1)
uart = UART(1, 9600)
uart = UART(1, 9600, bits=8, parity=None, stop=1)
print(uart)
uart.init(2400)
print(uart)
print(uart.any())
print(uart.write("123"))
print(uart.write(b"abcd"))
print(uart.writechar(1))
# make sure this method exists
uart.sendbreak()
# non-blocking mode
uart = UART(1, 9600, timeout=0)
print(uart.write(b"1"))
print(uart.write(b"abcd"))
print(uart.writechar(1))
print(uart.read(100))
# set rxbuf
uart.init(9600, rxbuf=8)
print(uart)
uart.init(9600, rxbuf=0)
print(uart)
# set read_buf_len (legacy, use rxbuf instead)
uart.init(9600, read_buf_len=4)
print(uart)
uart.init(9600, read_buf_len=0)
print(uart)

View File

@@ -0,0 +1,20 @@
ValueError -1
ValueError 0
UART 1
UART 2
ValueError 5
UART 6
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=0, timeout_char=3, rxbuf=64)
UART(1, baudrate=2400, bits=8, parity=None, stop=1, flow=0, timeout=0, timeout_char=7, rxbuf=64)
0
3
4
None
1
4
None
None
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=0, timeout_char=3, rxbuf=8)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=0, timeout_char=3, rxbuf=0)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=0, timeout_char=3, rxbuf=4)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=0, timeout_char=3, rxbuf=0)