def test_avr(): avr = Avr(mcu='atmega48', f_cpu=8000000) eq_(avr.f_cpu, 8000000) eq_(avr.mcu, 'atmega48') eq_(avr.pc, 0) avr.step(1) eq_(avr.pc, 2)
def test_timer_GC(): avr = Avr(mcu='atmega88', f_cpu=1000000) callbackMock = Mock(return_value=0) t = weakref.ref(avr.timer(callbackMock, cycle=10)) gc.collect() assert_that(t(), is_(none()), "Orphan Timer didn't get garbage collected.") avr.step(100) assert_that(callbackMock.call_count, equal_to(0), "Number of IRQ callback invocations.") avr.terminate()
def test_timer_cancel(): avr = Avr(mcu='atmega88', f_cpu=1000000) callbackMock = Mock(return_value=200) timer = avr.timer(callbackMock, cycle=50) avr.step(10) callbackMock.assert_not_called() timer.cancel() avr.step(1000) callbackMock.assert_not_called() avr.terminate()
def test_timer_simple(): avr = Avr(mcu='atmega88', f_cpu=8000000) # Callback method mocked out. callbackMock = Mock(return_value=0) #Schedule callback at 20uSec. # cycles = avr->frequency * (avr_cycle_count_t)usec / 1000000; timer = avr.timer(callbackMock, uSec=20) assert_that(timer.status(), close_to(8000000*20/1000000, 10), "uSec to cycles convertion") avr.step(1000) eq_(avr.state, cpu_Running, "mcu is not running") eq_(callbackMock.call_count, 1, "number of calback invocations") avr.terminate()
def test_timer_GC(): avr = Avr(mcu='atmega88', f_cpu=1000000) callbackMock = Mock(return_value=0) #Don't let avr object to keep the callback referenced. t = weakref.ref(avr.timer(callbackMock, cycle=10, keep_alive=False)) gc.collect() assert_that(t(), is_(none()), "Orphan Timer didn't get garbage collected.") avr.step(100) assert_that(callbackMock.call_count, equal_to(0), "Number of IRQ callback invocations.") #Now let avr object keep the callback alive. t = weakref.ref(avr.timer(callbackMock, cycle=110, keep_alive=True)) gc.collect() assert_that(t(), is_ (not_none()), "Avr object didn't kept Timer callback alive.") avr.step(1000) assert_that(callbackMock.call_count, equal_to(1), "Number of IRQ callback invocations.") avr.terminate() gc.collect() assert_that(t(), is_(none()), "Orphan Timer didn't get garbage collected even after Avr is terminated.")
def run_test(f_cpu, mcu, timeout, gdb, testfile): avr = Avr(mcu=mcu, f_cpu=f_cpu) fw = Firmware(testfile) avr.load_firmware(fw) if gdb: avr.gdb_port = 1234 avr_gdb_init(avr.backend) avr.state = cpu_Stopped t_start = time.time() while not avr.peek(GPIOR0) & TEST_COMPLETE: if timeout and time.time() - t_start > timeout: raise click.ClickException('Timeout') avr.step(100) if avr.uart.buffer: print(''.join(avr.uart.buffer)) sys.exit(0 if avr.peek(GPIOR0) & TEST_SUCCESS else 1)
def test_timer_reoccuring(): avr = Avr(mcu='atmega48', f_cpu=8000000) # Callback method mocked out. It will register another callback # at 200 cycles and then cancel by returning 0. callbackMock = Mock(side_effect = [200, 0]) timer = avr.timer(callbackMock) avr.step(10) eq_(avr.state, cpu_Running, "mcu is not running") callbackMock.assert_not_called() # Request first timer callback at 100 cycles timer.set_timer_cycles(100) # Run long enought to ensure callback is canceled by returning 0 on the second invocation. avr.step(1000) eq_(avr.state, cpu_Running, "mcu is not running") eq_(callbackMock.call_count, 2, "number of calback invocations") lastCallFirstArg = callbackMock.call_args[0][0] assert_that(lastCallFirstArg, close_to(200, 10), "The last cycle number received in the callback doesn't match the requested one") avr.terminate()
# Create simavr inbuilt button part. b = Button(avr) # Get the button's output port and attach it to the AVR input. pysimavr.connect.avr_connect_irq(b.getirq(0), A3IRQ) prevState = -1 i = 0 # Main loop. try: while True: # Do multiple simavr simulation steps synchronously. # Alternatively inbuilt pysimavr background execution thread can be used. avr.step(1000) time.sleep(0.01) # Print any simavr mcu state change. if avr.state != prevState: prevState = avr.state print("Avr state: {}".format(avr.states[avr.state])) # Simavr got to a final state. Terminate the main loop since it can't proceed any further. if avr.state in (cpu_Done, cpu_Crashed): break i += 1 # Simulate button press once in a while. if i % 200 == 50: print("Press")
from pysimavr.avr import Avr if __name__ == "__main__": avr = Avr(mcu='atmega48', f_cpu=8000000) print(avr.pc) avr.step(1) print(avr.pc) avr.step(1) print(avr.pc) avr.terminate()
from pysimavr.avr import Avr avr=Avr(mcu='atmega48',f_cpu=8000000) avr.step(1) print avr.pc
def run_sim(): avr = Avr(mcu='atmega48', f_cpu=8000000) avr.step(1) print avr.pc
# Create simavr inbuilt button part. b = Button(avr); # Get the button's output port and attach it to the AVR input. pysimavr.connect.avr_connect_irq(b.getirq(0), A3IRQ) prevState = -1 i = 0 # Main loop. try: while True: # Do multiple simavr simulation steps synchronously. # Alternatively inbuilt pysimavr background execution thread can be used. avr.step(1000); time.sleep(0.01) # Print any simavr mcu state change. if avr.state != prevState: prevState = avr.state print("Avr state: {}".format(avr.states[avr.state])); # Simavr got to a final state. Terminate the main loop since it can't proceed any further. if avr.state in (cpu_Done, cpu_Crashed): break; i += 1 # Simulate button press once in a while. if i % 200 == 50: print("Press")