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()
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()
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()
class TestBase(unittest.TestCase): def setUp(self): self.first = True self.startAvr(); pass def tearDown(self): self.uartBridge.terminate() self.avr.terminate() pass def messageSanityCheck(self, bytes, commandId, expectedSize, first): ''' Check that the response has correct size, message Id, checksum @param bytes: list of bytes @param commandId: message identifier @param expectedSize: expected size of the response @param first: is is a first response @raise exception: unittest.TestCase.assertEqual @return: None ''' if (expectedSize != None): self.assertEqual(len(bytes), expectedSize, "Message length "+str(len(bytes))+", expected size "+str(expectedSize)); self.assertEqual(bytes[0], 0x7f, "1st sync byte is "+str(bytes[0])); self.assertEqual(bytes[1], 0xef, "2nd sync byte is "+str(bytes[1])); if (first): self.assertEqual(bytes[2], commandId, "Message id "+str(bytes[2])); else: self.assertEqual(bytes[2], commandId | 0x80, "Message id "+str(bytes[2])); self.assertEqual(bytes[3], expectedSize-4, "Payload size is "+str(bytes[3])); bytesWithoutChecksum = bytes[:-1] csExpected = calculateChecksum(bytesWithoutChecksum) csActual = bytes[len(bytes)-1] self.assertEqual(csActual, csExpected, "Checksum is "+hex(csActual)+" instead "+hex(csExpected)); def startAvr(self): self.avr = Avr(mcu='atmega88',f_cpu=8000000) self.udp = Udp(self.avr) self.udp.connect() self.uartBridge = UartBridge() firmware = Firmware(ELFNAME) self.avr.load_firmware(firmware); self.uartBridge.start(); self.avr.run() #self.time_passed(); def time_passed(self, timeout=0.99): while self.avr.time_passed() < 0.1 * timeout: time.sleep(0.05) def sendCommand(self, command, payload): syncBytes = [0x7f, 0xef]; command = syncBytes + command + [len(payload)+1] + payload; cs = calculateChecksum(command) command = command + [cs]; s = str(bytearray(command)) self.uartBridge.send_str(s); self.time_passed(0.05*len(s))