DOC: Explain how to return a value from a coroutine, and use a value returned from one
@@ -27,6 +27,29 @@ Coroutines may also yield other coroutines:
for i in range(10):
yield wait_10ns()
+Coroutines can return a value, so that they can be used by other coroutines.
+Before python 3.3, this requires a `ReturnValue` to be raised.
+
+.. code-block:: python
+
+ @cocotb.coroutine
+ def get_signal(clk, signal):
+ yield RisingEdge(clk)
+ raise ReturnValue(signal.value)
+
+ @cocotb.coroutine
+ def get_signal_python_33(clk, signal):
+ # newer versions of python can use return normally
+ yield RisingEdge(clk)
+ return signal.value
+
+ @cocotb.coroutine
+ def check_signal_changes(dut):
+ first = yield get_signal(dut.clk, dut.signal)
+ second = yield get_signal(dut.clk, dut.signal)
+ if first == second:
+ raise TestFailure("Signal did not change")
+
Coroutines may also yield a list of triggers to indicate that execution should
resume if *any* of them fires:
