Debian Codegraph @

samples/iso-ir-single

raw
1 #!/usr/bin/env python3
2
3 from threading import Timer
4 from struct import unpack
5
6 import gi
7 gi.require_versions({'GLib': '2.0', 'Hinawa': '4.0', 'Hinoko': '1.0'})
8 from gi.repository import GLib, Hinawa, Hinoko
9
10 class IsoIrSingle(Hinoko.FwIsoIrSingle):
11 def new(self, path, channel, maximum_bytes_per_payload):
12 self.allocate(path, channel, 8)
13
14 # Use 10msec interval for I/O operation.
15 self.map_buffer(maximum_bytes_per_payload, 240)
16
17 def destroy(self):
18 self.unmap_buffer()
19 self.release()
20
21 def queue_packet(self):
22 schedule_interrupt = False
23
24 self.accumulated_packet_count += 1
25 if self.accumulated_packet_count % self.packets_per_interrupt == 0:
26 schedule_interrupt = True
27
28 self.register_packet(schedule_interrupt)
29
30 def begin(self, dispatcher, duration):
31 _, self.__src = self.create_source()
32 self.__src.attach(dispatcher.get_context())
33 self.__dispatcher = dispatcher
34
35 # Roughly finish event loop.
36 self.__timer = Timer(duration, lambda dispatcher: dispatcher.quit(),
37 args=(dispatcher,))
38
39 tags = Hinoko.FwIsoCtxMatchFlag.TAG0 | \
40 Hinoko.FwIsoCtxMatchFlag.TAG1 | \
41 Hinoko.FwIsoCtxMatchFlag.TAG2 | \
42 Hinoko.FwIsoCtxMatchFlag.TAG3
43
44 # Start one half of second later. Scheduling is available with lower 2 bits of second field.
45 cycle_time = Hinawa.CycleTime.new()
46 clock_id = 4 #CLOCK_MONOTONIC_RAW
47 _, cycle_time = self.read_cycle_time(clock_id, cycle_time)
48 (sec, cycle, tick) = cycle_time.get_fields()
49 sec, cycle = self.__increment_cycle(sec, cycle, 4000)
50 cycle_match = (sec & 0x3, cycle)
51
52 self.accumulated_packet_count = 0
53 self.packets_per_interrupt = 80
54
55 # Schedule for some isochronous cycles.
56 for i in range(self.packets_per_interrupt * 2):
57 self.queue_packet()
58
59 self.__timer.start()
60 self.start(cycle_match, 0, tags)
61
62 @staticmethod
63 def __increment_cycle(sec, cycle, addend):
64 cycle += addend
65 if cycle >= 8000:
66 cycle %= 8000
67 sec += 1
68 if sec >= 128:
69 sec %= 128
70 return (sec, cycle)
71
72 def finish(self):
73 self.__src.destroy()
74 self.stop()
75
76 def do_stopped(self, error):
77 self.__dispatcher.quit()
78 if error:
79 self.__timer.cancel()
80 print(error)
81
82 @staticmethod
83 def __ohci1394_tstamp_to_isoc_cycle(tstamp):
84 sec = (tstamp & 0x0000e000) >> 13
85 cycle = tstamp & 0x00001fff
86 return (sec, cycle)
87
88 def do_interrupted(self, sec, cycle, header, header_length, count):
89 headers = unpack('>{0}I'.format(header_length // 4), header)
90 for i in range(count):
91 iso_header, tstamp = headers[i * 2:(i + 1) * 2]
92
93 sec, cycle = self.__ohci1394_tstamp_to_isoc_cycle(tstamp)
94
95 data = self.get_payload(i)
96 frames = unpack('>{0}I'.format(len(data) // 4), data)
97
98 print('{0:d},{1:d},{2:08x},{3}'.format(sec, cycle, iso_header, i))
99 for i, frame in enumerate(frames):
100 print(' {0:2d}: {1:08x}'.format(i, frame))
101
102 self.queue_packet()
103
104 # 2 CIP headers + 3 quadlet per data block * 8 data block
105 channel = 1
106 maximum_bytes_per_payload = (2 + 3 * 8) * 4
107
108 ctx = IsoIrSingle()
109 ctx.new('/dev/fw0', channel, maximum_bytes_per_payload)
110
111 dispatcher = GLib.MainLoop.new(None, False)
112
113 ctx.begin(dispatcher, 4)
114 dispatcher.run()
115 ctx.finish()
116 ctx.destroy()
117
118 del dispatcher
119 del ctx