Debian Codegraph @

samples/iso-it

raw
1 #!/usr/bin/env python3
2
3 from threading import Timer
4 from struct import unpack,pack
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 IsoIt(Hinoko.FwIsoIt):
11 def new(self, path, channel, maximum_bytes_per_payload, packets_per_buffer):
12 # Use 8 byte header for CIP header.
13 cip_header_size = 8
14 self.cip_payload = [i for i in range(maximum_bytes_per_payload)]
15
16 self.allocate(path, Hinoko.FwScode.S400, channel, cip_header_size)
17
18 self.map_buffer(maximum_bytes_per_payload, packets_per_buffer)
19
20 self.channel = channel
21 self.packets_per_buffer = packets_per_buffer
22
23 def destroy(self):
24 self.unmap_buffer()
25 self.release()
26
27 def queue_packet(self, cip_header, cip_payload):
28 self.accumulated_packet_count += 1
29 if self.accumulated_packet_count % self.packets_per_interrupt == 0:
30 schedule_interrupt = True
31 else:
32 schedule_interrupt = False
33 self.register_packet(self.tag, self.sy, cip_header, cip_payload, schedule_interrupt)
34
35 def begin(self, dispatcher, packets_per_interrupt, duration):
36 _, self.__src = self.create_source()
37 self.__src.attach(dispatcher.get_context())
38 self.__dispatcher = dispatcher
39
40 # Roughly finish event loop.
41 self.__timer = Timer(duration, lambda dispatcher: dispatcher.quit(),
42 args=(dispatcher,))
43
44 self.tag = Hinoko.FwIsoCtxMatchFlag.TAG1
45 self.sy = 0
46
47 # Start 100 msec later. Scheduling is available with lower 2 bits of second field.
48 cycle_time = Hinawa.CycleTime.new()
49 clock_id = 4 # CLOCK_MONOTONIC_RAW
50 _, cycle_time = self.read_cycle_time(clock_id, cycle_time)
51 (sec, cycle, tick) = cycle_time.get_fields()
52 sec, cycle = self.__increment_cycle(sec, cycle, 800)
53 cycle_match = (sec & 0x3, cycle)
54
55 self.accumulated_packet_count = 0
56 self.packets_per_interrupt = packets_per_interrupt
57
58 # Schedule some isochronous cycles to skip.
59 for i in range(packets_per_interrupt * 2):
60 self.queue_packet(None, None)
61
62 self.__timer.start()
63 self.start(cycle_match)
64
65 @staticmethod
66 def __increment_cycle(sec, cycle, addend):
67 cycle += addend
68 if cycle >= 8000:
69 cycle %= 8000
70 sec += 1
71 if sec >= 128:
72 sec %= 128
73 return (sec, cycle)
74
75 def finish(self):
76 self.__src.destroy()
77 self.stop()
78
79 def do_stopped(self, error):
80 self.__dispatcher.quit()
81 if error:
82 self.__timer.cancel()
83 print(error)
84
85 @staticmethod
86 def __ohci1394_tstamp_to_isoc_cycle(tstamp):
87 sec = (tstamp & 0x0000e000) >> 13
88 cycle = tstamp & 0x00001fff
89 return (sec, cycle)
90
91 def do_interrupted(self, sec, cycle, header, header_length, count):
92 tstamps = unpack('>{0}I'.format(header_length // 4), header)
93 for i in range(count):
94 sec, cycle = self.__ohci1394_tstamp_to_isoc_cycle(tstamps[i])
95
96 # Schedule packet at the completed isochronous cycle plus packets per buffer. The header
97 # of packet has data for the completed isochronous cycle.
98 next_sec, next_cycle = self.__increment_cycle(sec, cycle, self.packets_per_buffer)
99 cip_header = pack('>2I', next_sec, next_cycle)
100 self.queue_packet(cip_header, self.cip_payload)
101
102 # Print the data of packet already sent.
103 iso_header = (len(self.cip_payload) << 16) | \
104 (self.tag << 14) | \
105 (self.channel << 8) | \
106 (0xa << 4) | \
107 self.sy
108 print('{0:2d},{1:4d},{2:08x},{3}'.format(sec, cycle, iso_header, i))
109
110 channel = 30
111 maximum_bytes_per_payload = 32
112 packets_per_buffer = 32
113 packets_per_interrupt = 4
114 duration = 4
115
116 ctx = IsoIt()
117 ctx.new('/dev/fw0', channel, maximum_bytes_per_payload, packets_per_buffer)
118
119 dispatcher = GLib.MainLoop.new(None, False)
120
121 ctx.begin(dispatcher, packets_per_interrupt, duration)
122 dispatcher.run()
123 ctx.finish()
124 ctx.destroy()
125
126 del dispatcher
127 del ctx