1# Copyright 2016 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import logging 6import os 7import time 8 9from autotest_lib.client.common_lib import error 10from autotest_lib.server import test 11from autotest_lib.server.cros import servo_keyboard_utils 12 13 14class firmware_FlashServoKeyboardMap(test.test): 15 """A test to flash the keyboard map on servo.""" 16 version = 1 17 18 _ATMEGA_RESET_DELAY = 0.2 19 _USB_PRESENT_DELAY = 1 20 21 def run_once(self, host=None): 22 """Body of the test.""" 23 24 servo = host.servo 25 if host.run('hash dfu-programmer', ignore_status=True).exit_status: 26 raise error.TestError( 27 'The image is too old that does not have dfu-programmer.') 28 29 try: 30 servo.set_nocheck('init_usb_keyboard', 'on') 31 32 # Check the result of lsusb. 33 time.sleep(self._USB_PRESENT_DELAY) 34 lsusb_cmd = ('lsusb -d ' + 35 servo_keyboard_utils.ATMEL_USB_VENDOR_ID + ':') 36 result = host.run(lsusb_cmd).stdout.strip() 37 if ('LUFA Keyboard Demo' in result and 38 servo_keyboard_utils.is_servo_usb_wake_capable(host)): 39 logging.info('Already using the new keyboard map.') 40 return 41 42 # Boot AVR into DFU mode by enabling the HardWareBoot mode 43 # strapping and reset. 44 servo.set_get_all(['at_hwb:on', 45 'atmega_rst:on', 46 'sleep:%f' % self._ATMEGA_RESET_DELAY, 47 'atmega_rst:off', 48 'sleep:%f' % self._ATMEGA_RESET_DELAY, 49 'at_hwb:off']) 50 51 time.sleep(self._USB_PRESENT_DELAY) 52 result = host.run(lsusb_cmd).stdout.strip() 53 if not 'Atmel Corp. atmega32u4 DFU bootloader' in result: 54 message = 'Not an expected chip: %s' % result 55 logging.error(message) 56 raise error.TestFail(message) 57 58 # Update the keyboard map. 59 local_path = os.path.join(self.bindir, 'test_data', 'keyboard.hex') 60 host.send_file(local_path, '/tmp') 61 logging.info('Updating the keyboard map...') 62 host.run('dfu-programmer atmega32u4 erase --force') 63 host.run('dfu-programmer atmega32u4 flash /tmp/keyboard.hex') 64 65 # Reset the chip. 66 servo.set_get_all(['atmega_rst:on', 67 'sleep:%f' % self._ATMEGA_RESET_DELAY, 68 'atmega_rst:off']) 69 70 # Check the result of lsusb. 71 time.sleep(self._USB_PRESENT_DELAY) 72 result = host.run(lsusb_cmd).stdout.strip() 73 if 'LUFA Keyboard Demo' in result: 74 logging.info('Update successfully!') 75 else: 76 message = 'Update failed; got the result: %s' % result 77 logging.error(message) 78 raise error.TestFail(message) 79 80 finally: 81 # Restore the default settings. 82 # Select the chip on the USB mux unless using Servo V4 83 if 'servo_v4' not in servo.get_servo_version(): 84 servo.set('usb_mux_sel4', 'on') 85