1#!/usr/bin/env python3 2# Copyright 2019 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Detects attached stm32f429i-disc1 boards connected via mini usb.""" 16 17import logging 18import typing 19from typing import Optional 20 21import coloredlogs # type: ignore 22import serial.tools.list_ports 23 24# Vendor/device ID to search for in USB devices. 25_ST_VENDOR_ID = 0x0483 26_DISCOVERY_MODEL_ID = 0x374B 27 28_LOG = logging.getLogger('stm32f429i_detector') 29 30 31class BoardInfo(typing.NamedTuple): 32 """Information about a connected dev board.""" 33 34 dev_name: str 35 serial_number: Optional[str] 36 37 38def detect_boards() -> list: 39 """Detect attached boards, returning a list of Board objects.""" 40 boards = [] 41 all_devs = serial.tools.list_ports.comports() 42 for dev in all_devs: 43 if dev.vid == _ST_VENDOR_ID and dev.pid == _DISCOVERY_MODEL_ID: 44 boards.append( 45 BoardInfo(dev_name=dev.device, serial_number=dev.serial_number) 46 ) 47 return boards 48 49 50def main(): 51 """This detects and then displays all attached discovery boards.""" 52 53 # Try to use pw_cli logs, else default to something reasonable. 54 try: 55 import pw_cli.log # pylint: disable=import-outside-toplevel 56 57 pw_cli.log.install() 58 except ImportError: 59 coloredlogs.install( 60 level='INFO', 61 level_styles={'debug': {'color': 244}, 'error': {'color': 'red'}}, 62 fmt='%(asctime)s %(levelname)s | %(message)s', 63 ) 64 65 boards = detect_boards() 66 if not boards: 67 _LOG.info('No attached boards detected') 68 for idx, board in enumerate(boards): 69 _LOG.info('Board %d:', idx) 70 _LOG.info(' - Port: %s', board.dev_name) 71 _LOG.info(' - Serial #: %s', board.serial_number or '<not set>') 72 73 74if __name__ == '__main__': 75 main() 76