Beispiel #1
0
    def simulate(self):
        elf = self.cc.output

        # run
        firmware = Firmware(elf)
        avr = Avr(mcu=self.cc.mcu, f_cpu=self.cc.f_cpu)
        avr.load_firmware(firmware)
#        udpReader = UdpReader()
#        udp = Udp(avr)
#        udp.connect()
#        udpReader.start()

        simvcd = None
        if self.vcd:
            simvcd = VcdFile(avr, period=1000, filename=self.vcd)
            connect_pins_by_rule('''
                                avr.D0 ==> vcd
                                avr.D1 ==> vcd
                                avr.D2 ==> vcd
                                avr.D3 ==> vcd
                                avr.D4 ==> vcd
                                avr.D5 ==> vcd
                                avr.D6 ==> vcd
                                avr.D7 ==> vcd

                                avr.B0 ==> vcd
                                avr.B1 ==> vcd
                                avr.B2 ==> vcd
                                avr.B3 ==> vcd
                                avr.B4 ==> vcd
                                avr.B5 ==> vcd
                                ''',
                                 dict(
                                 avr=avr,
                                 ),
                                 vcd=simvcd,
                                 )
            simvcd.start()

# not working
#        if self.serial_in:
#            avr.uart.send_string(self.serial_in)
        avr.move_time_marker(self.timespan)

        while avr.time_passed() < self.timespan * 0.99:
            time.sleep(0.05)

        if simvcd:
            simvcd.terminate()
#        udpReader.terminate()

        log.debug('cycles=%s' % avr.cycle)
        log.debug('mcu time=%s' % avr.time_passed())
#        time.sleep(1)
        self.serial_data = avr.uart.buffer
        self.serial = ''.join(self.serial_data)
        avr.terminate()
Beispiel #2
0
def test_background_thread():
    cc = AvrGcc(mcu=mcu)
    cc.build('int main(){}')
    fw = Firmware(cc.output)
    avr = Avr(mcu=mcu, firmware=fw, f_cpu=8000000)
    eq_(avr.state, cpu_Running, "mcu is not running")
    avr.goto_cycle(100)
    eq_(avr.state, cpu_Running, "mcu is not running")
    avr.terminate()
Beispiel #3
0
def test_background_thread():
    cc = AvrGcc(mcu=mcu)
    cc.build('int main(){}')
    fw = Firmware(cc.output)
    avr = Avr(mcu=mcu, firmware=fw, f_cpu=8000000)    
    eq_(avr.state, cpu_Running, "mcu is not running")
    avr.goto_cycle(100)
    eq_(avr.state, cpu_Running, "mcu is not running")
    avr.terminate()
Beispiel #4
0
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()
Beispiel #5
0
def test_fw_1():
    cc = AvrGcc(mcu=mcu)
    cc.build('int main(){}')
    fw = Firmware(cc.output)

    avr = Avr(mcu=mcu, firmware=fw, f_cpu=8000000)
    eq_(avr.f_cpu, 8000000)
    eq_(avr.frequency, avr.f_cpu)
    eq_(avr.mcu, 'atmega48')
    avr.terminate()
Beispiel #6
0
def test_fw_1():
    cc = AvrGcc(mcu=mcu)
    cc.build('int main(){}')
    fw = Firmware(cc.output)

    avr = Avr(mcu=mcu, firmware=fw, f_cpu=8000000)
    eq_(avr.f_cpu, 8000000)
    eq_(avr.frequency, avr.f_cpu)
    eq_(avr.mcu, 'atmega48')
    avr.terminate()
Beispiel #7
0
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()
Beispiel #8
0
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()
Beispiel #9
0
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()
Beispiel #10
0
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()
Beispiel #11
0
def test_custom_logger():
    loggerMethod = Mock()
    #Register custom callback method for simav logs
    logger.init_simavr_logger(loggerMethod)
    avr = Avr(mcu='atmega48', f_cpu=8000000)
    #Let the simavr run in background until it sadly crashes on ramend
    avr.run() 
    while avr.cycle < 8000 and avr.state == cpu_Running :
        time.sleep(0.1)
        
    # Expected:
    #('Starting atmega48 - flashend 0fff ramend 02ff e2end 00ff\n', 3)
    #('atmega48 init\n', 3)
    #('atmega48 reset\n', 3)
    #('avr_sadly_crashed\n', 1)
    eq_(loggerMethod.call_count, 4, "number of callback invocations")
    loggerMethod.assert_called_with('avr_sadly_crashed\n', 1)    
    avr.terminate()
Beispiel #12
0
def test_custom_logger():
    loggerMethod = Mock()
    #Register custom callback method for simav logs
    logger.init_simavr_logger(loggerMethod)
    avr = Avr(mcu='atmega48', f_cpu=8000000)
    #Let the simavr run in background until it sadly crashes on ramend
    avr.run()
    while avr.cycle < 8000 and avr.state == cpu_Running:
        time.sleep(0.1)

    # Expected:
    #('Starting atmega48 - flashend 0fff ramend 02ff e2end 00ff\n', 3)
    #('atmega48 init\n', 3)
    #('atmega48 reset\n', 3)
    #('avr_sadly_crashed\n', 1)
    eq_(loggerMethod.call_count, 4, "number of callback invocations")
    loggerMethod.assert_called_with('avr_sadly_crashed\n', 1)
    avr.terminate()
Beispiel #13
0
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.")
Beispiel #14
0
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()
Beispiel #15
0
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()
Beispiel #16
0
class Controller:
    def __init__(self, sock, mcu_name, freq=16000000, version=1):
        self.mcu_name = mcu_name
        self.mcu = Avr(mcu=mcu_name, f_cpu=freq)
        self.socket = sock
        self.fw_path = None
        self.version = version
        self.bind_callback_for_digit_pins(atmega_328_digit_pin_table)
        self.vcd = None
        self.connect_vcd()

    def upload_firmware(self, fw_path):
        self.fw_path = fw_path
        fw = Firmware(fw_path)
        self.mcu.load_firmware(fw)

    def connect_vcd(self):
        self.vcd = VcdFile(self.mcu, filename='out' + str(self.version))
        connect_pins_by_rule('''
                            avr.D0 ==> vcd
                            avr.D1 ==> vcd
                            avr.D2 ==> vcd
                            avr.D3 ==> vcd
                            avr.D4 ==> vcd
                            avr.D5 ==> vcd
                            avr.D6 ==> vcd
                            avr.D7 ==> vcd

                            avr.B0 ==> vcd
                            avr.B1 ==> vcd
                            avr.B2 ==> vcd
                            avr.B3 ==> vcd
                            avr.B4 ==> vcd
                            avr.B5 ==> vcd
                            ''',
                             dict(avr=self.mcu),
                             vcd=self.vcd)

    def bind_callback_for_digit_pins(self, ports):
        def port_callback(irq, new_val):
            msg = "change " + self.mcu_name + ' ' + irq.name[
                0] + " " + irq.name[1] + " " + str(new_val)
            self.socket.send_msg(msg)

        callback = Mock(side_effect=port_callback)

        for port in ports:
            p = self.mcu.irq.ioport_register_notify(callback,
                                                    (port[0], int(port[1:])))
            p.get_irq().name = port

    def new_pin_val(self, port_pin, new_val):
        # type: ((str, int), int) -> None

        irq = self.mcu.irq.getioport(port_pin)
        avr_raise_irq(irq, new_val)

    def run(self):
        self.vcd.start()
        self.mcu.run()

    def pause(self):
        self.vcd.stop()
        self.mcu.pause()

    def terminate(self):
        if self.vcd:
            self.vcd.terminate()
        self.mcu.terminate()
Beispiel #17
0
    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")
                b.press(10000)
                # Press with autorelease

            # if i % 100 == 0:b.up();
            # if i % 100 == 50:b.down();

    finally:
        avr.terminate()
    print("end")
Beispiel #18
0
    def simulate(self):
        if not self.external_elf:
            elf = self.cc.output
        else:
            elf = self.external_elf

        # run
        firmware = Firmware(elf)
        avr = Avr(mcu=self.cc.mcu, f_cpu=self.cc.f_cpu)
        avr.uart.char_logger = self.serial_char_logger
        avr.uart.line_logger = self.serial_line_logger
        avr.load_firmware(firmware)
#        udpReader = UdpReader()
#        udp = Udp(avr)
#        udp.connect()
#        udpReader.start()

        simvcd = None
        if self.vcd:
            simvcd = VcdFile(avr, period=1000, filename=self.vcd)
            connect_pins_by_rule('''
                                avr.D0 ==> vcd
                                avr.D1 ==> vcd
                                avr.D2 ==> vcd
                                avr.D3 ==> vcd
                                avr.D4 ==> vcd
                                avr.D5 ==> vcd
                                avr.D6 ==> vcd
                                avr.D7 ==> vcd

                                avr.B0 ==> vcd
                                avr.B1 ==> vcd
                                avr.B2 ==> vcd
                                avr.B3 ==> vcd
                                avr.B4 ==> vcd
                                avr.B5 ==> vcd
                                ''',
                                 dict(
                                 avr=avr,
                                 ),
                                 vcd=simvcd,
                                 )
            simvcd.start()

# not working
#        if self.serial_in:
#            avr.uart.send_string(self.serial_in)

        if self.fps:
            dt_real = 1. / self.fps
            dt_mcu = dt_real * self.speed
            count = int(self.timespan * self.fps / self.speed)
            for _ in range(count):
                time.sleep(dt_real)

        avr.goto_time(self.timespan)
        while avr.time_passed() < self.timespan * 0.99:
            time.sleep(0.05)

        if simvcd:
            simvcd.terminate()
#        udpReader.terminate()

        log.debug('cycles=%s' % avr.cycle)
        log.debug('mcu time=%s' % avr.time_passed())
#        time.sleep(1)
        self.serial_data = avr.uart.buffer
        self.serial = ''.join(self.serial_data)
        avr.terminate()
Beispiel #19
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")                
                b.press(10000);  # Press with autorelease
            
            # if i % 100 == 0:b.up();
            # if i % 100 == 50:b.down();
    
    finally: avr.terminate()   
    print("end")
Beispiel #20
0
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()
Beispiel #21
0
    def simulate(self):
        if not self.external_elf:
            elf = self.cc.output
        else:
            elf = self.external_elf

        # run
        firmware = Firmware(elf)
        avr = Avr(mcu=self.cc.mcu, f_cpu=self.cc.f_cpu)
        avr.uart.char_logger = self.serial_char_logger
        avr.uart.line_logger = self.serial_line_logger
        avr.load_firmware(firmware)
        #        udpReader = UdpReader()
        #        udp = Udp(avr)
        #        udp.connect()
        #        udpReader.start()

        simvcd = None
        if self.vcd:
            simvcd = VcdFile(avr, period=1000, filename=self.vcd)
            connect_pins_by_rule(
                '''
                                avr.D0 ==> vcd
                                avr.D1 ==> vcd
                                avr.D2 ==> vcd
                                avr.D3 ==> vcd
                                avr.D4 ==> vcd
                                avr.D5 ==> vcd
                                avr.D6 ==> vcd
                                avr.D7 ==> vcd

                                avr.B0 ==> vcd
                                avr.B1 ==> vcd
                                avr.B2 ==> vcd
                                avr.B3 ==> vcd
                                avr.B4 ==> vcd
                                avr.B5 ==> vcd
                                ''',
                dict(avr=avr, ),
                vcd=simvcd,
            )
            simvcd.start()

# not working
#        if self.serial_in:
#            avr.uart.send_string(self.serial_in)

        if self.fps:
            dt_real = 1. / self.fps
            dt_mcu = dt_real * self.speed
            count = int(self.timespan * self.fps / self.speed)
            for _ in range(count):
                time.sleep(dt_real)

        avr.goto_time(self.timespan)
        while avr.time_passed() < self.timespan * 0.99:
            time.sleep(0.05)

        if simvcd:
            simvcd.terminate()
#        udpReader.terminate()

        log.debug('cycles=%s' % avr.cycle)
        log.debug('mcu time=%s' % avr.time_passed())
        #        time.sleep(1)
        self.serial_data = avr.uart.buffer
        self.serial = ''.join(self.serial_data)
        avr.terminate()