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 result = host.run(lsusb_cmd).stdout.strip() 52 if not 'Atmel Corp. atmega32u4 DFU bootloader' in result: 53 message = 'Not an expected chip: %s' % result 54 logging.error(message) 55 raise error.TestFail(message) 56 57 # Update the keyboard map. 58 local_path = os.path.join(self.bindir, 'test_data', 'keyboard.hex') 59 host.send_file(local_path, '/tmp') 60 logging.info('Updating the keyboard map...') 61 host.run('dfu-programmer atmega32u4 erase --force') 62 host.run('dfu-programmer atmega32u4 flash /tmp/keyboard.hex') 63 64 # Reset the chip. 65 servo.set_get_all(['atmega_rst:on', 66 'sleep:%f' % self._ATMEGA_RESET_DELAY, 67 'atmega_rst:off']) 68 69 # Check the result of lsusb. 70 time.sleep(self._USB_PRESENT_DELAY) 71 result = host.run(lsusb_cmd).stdout.strip() 72 if 'LUFA Keyboard Demo' in result: 73 logging.info('Update successfully!') 74 else: 75 message = 'Update failed; got the result: %s' % result 76 logging.error(message) 77 raise error.TestFail(message) 78 79 finally: 80 # Restore the default settings. 81 # Select the chip on the USB mux unless using Servo V4 82 if 'servo_v4' not in servo.get_servo_version(): 83 servo.set('usb_mux_sel4', 'on') 84