1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-02 20:48:30 +00:00
trezor-firmware/src/trezor/loop.py

149 lines
3.7 KiB
Python
Raw Normal View History

import utime
2016-04-29 19:19:25 +00:00
from uheapq import heappop, heappush
from .utils import type_gen
2016-04-29 12:49:47 +00:00
from . import msg
2016-04-29 20:24:27 +00:00
from . import log
if __debug__:
# For performance stats
import array
log_delay_pos = 0
log_delay_rb_len = const(10)
log_delay_rb = array.array('i', [0] * log_delay_rb_len)
TOUCH_START = const(1)
TOUCH_MOVE = const(2)
TOUCH_END = const(4)
HID_READ = const(8)
time_queue = []
time_queue_ctr = 0
blocked_events = 0
blocked_gen = None
def schedule(gen, data=None, time=None):
global time_queue_ctr
if not time:
time = utime.ticks_us()
heappush(time_queue, (time, time_queue_ctr, gen, data))
time_queue_ctr += 1
class Sleep():
def __init__(self, us):
self.time = utime.ticks_us() + us
class Select():
def __init__(self, events):
self.events = events
2016-04-29 23:20:57 +00:00
2016-05-02 14:06:08 +00:00
class Wait():
2016-05-02 14:06:08 +00:00
def __init__(self, gens, wait_for=1, exit_others=True):
self.wait_for = wait_for
self.exit_others = exit_others
self.received = 0
self.callback = None
self.gens = gens
for g in gens:
schedule(self._wait(g))
2016-05-02 14:06:08 +00:00
def _wait(self, gen):
if isinstance(gen, type_gen):
ret = yield from gen
else:
ret = yield gen
2016-04-29 23:20:57 +00:00
2016-05-02 14:06:08 +00:00
self.finish(gen, ret)
2016-04-29 23:20:57 +00:00
2016-05-02 14:06:08 +00:00
def finish(self, gen, result):
self.received += 1
2016-04-30 13:57:08 +00:00
2016-05-02 14:21:50 +00:00
if self.received == self.wait_for:
schedule(self.callback, (gen, result))
2016-05-02 14:06:08 +00:00
self.callback = None
2016-05-02 14:06:08 +00:00
if self.exit_others:
for g in self.gens:
if isinstance(gen, type_gen):
g.close()
def run_forever():
if __debug__:
global log_delay_pos, log_delay_rb, log_delay_rb_len
global blocked_events, blocked_gen
2016-04-29 19:19:25 +00:00
DELAY_MAX = const(1000000)
2016-04-29 19:19:25 +00:00
while True:
# Peek at how long we can sleep while waiting for an event
2016-04-29 19:19:25 +00:00
if time_queue:
t, _, _, _ = time_queue[0]
2016-04-29 19:19:25 +00:00
delay = t - utime.ticks_us()
else:
delay = DELAY_MAX
2016-04-29 19:19:25 +00:00
if __debug__:
# Adding current delay to ring buffer for performance stats
log_delay_rb[log_delay_pos] = delay
log_delay_pos = (log_delay_pos + 1) % log_delay_rb_len
message = msg.select(delay)
if message:
# Run interrupt handler right away, they have priority
event = message[0]
data = message
if blocked_events & event:
gen = blocked_gen
blocked_events = 0
blocked_gen = None
else:
log.info(__name__, 'No handler for event: %s', event)
continue
2016-04-29 19:19:25 +00:00
else:
# Run something from the time queue
if time_queue:
_, _, gen, data = heappop(time_queue)
else:
continue
2016-04-29 19:19:25 +00:00
try:
result = gen.send(data)
2016-04-29 19:19:25 +00:00
except StopIteration as e:
log.debug(__name__, '%s finished', gen)
2016-04-29 19:19:25 +00:00
continue
2016-04-29 23:20:57 +00:00
except Exception as e:
log.exception(__name__, e)
2016-04-29 23:20:57 +00:00
continue
2016-04-29 19:19:25 +00:00
if isinstance(result, Sleep):
# Sleep until result.time, call us later
schedule(gen, None, result.time)
elif isinstance(result, Select):
# Wait for one or more types of event
if blocked_gen:
blocked_gen.close()
blocked_gen = gen
blocked_events = result.events
2016-04-29 19:19:25 +00:00
elif isinstance(result, Wait):
# Register the origin generator as a waiting callback
result.callback = gen
2016-04-30 13:57:08 +00:00
elif result is None:
# Just call us asap
schedule(gen)
2016-04-29 23:20:57 +00:00
2016-04-30 13:57:08 +00:00
else:
raise Exception('Unhandled result %s by %s' % (result, gen))