-
Notifications
You must be signed in to change notification settings - Fork 26
/
hidprogram
executable file
·88 lines (76 loc) · 2.43 KB
/
hidprogram
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
# encoding: utf8
#
# HID Download Tool
#
# Copyright (c) BekenCorp. (chunjian.tian@bekencorp.com). All rights reserved.
#
# This software may be distributed under the terms of the BSD license.
# See README for more details.
import sys
import os
import argparse
import bkutils
import hid
import threading
chip_map = {
"bk7231": bkutils.CHIP_TYPE_BK7231,
"bk7231s": bkutils.CHIP_TYPE_BK7231U,
"bk7231u": bkutils.CHIP_TYPE_BK7231U,
"bk7231n": bkutils.CHIP_TYPE_BK7231U,
"bk7221u": bkutils.CHIP_TYPE_BK7221U,
"bk7251": bkutils.CHIP_TYPE_BK7251,
}
# parse commandline arguments
def parse_args():
description = '''Beken HID Downloader.'''
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-c', '--chip',
default='bk7231u',
choices=chip_map.keys(),
help="chip type, defaults to bk7231u")
parser.add_argument('-m', '--mode',
choices=['soft', 'hard'],
default='hard',
help="SPI mode, defaults to hardware mode")
parser.add_argument('filename',
help='specify file_crc.bin')
args = parser.parse_args()
return args
# list all chips this tool supports
def show_all_chips():
print("Available:")
for c in chip_map.keys():
print("\t{}".format(c))
sys.exit(-1)
args = parse_args()
filename = args.filename
chip_type = args.chip
if chip_type not in chip_map:
print("Cannot find chip: {}".format(chip_type))
show_all_chips()
if not os.path.exists(filename):
print("file: {} not exist".format(filename))
sys.exit(-1)
spi_mode = 2 if args.mode == 'soft' else 0
# Detect SPI Hardware
spi_device_paths = []
for device in hid.enumerate():
if device['vendor_id']==0x10c4 and device["product_id"]==0x0033:
spi_device_paths.append(device['path'])
if not spi_device_paths:
print("No SPI hardward dectected")
sys.exit(-1)
# run hid in seperate thread
def hid_download_thread(index, path):
downloader = bkutils.HidDownloader(chip_map[chip_type], spi_mode, path=path, extra=index)
downloader.Download(filename)
threads = []
for index,path in enumerate(spi_device_paths):
t = threading.Thread(target=hid_download_thread, args=(index, path))
t.daemon = True
t.start()
threads.append(t)
# Wait for all threads to terminate
for t in threads:
t.join()