Skip to content

Commit 2753d3a

Browse files
committed
Move CDC code to separate python file
1 parent 6c8cf25 commit 2753d3a

File tree

2 files changed

+110
-99
lines changed

2 files changed

+110
-99
lines changed

mbed/mbed.py

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import zipfile
3535
import argparse
3636
import tempfile
37+
from mbed_cdc import mbed_cdc
3738

3839

3940
# Application version
@@ -1752,103 +1753,6 @@ def formaturl(url, format="default"):
17521753
return url
17531754

17541755

1755-
def cdc(port, reset=False, sterm=False, baudrate=9600, timeout=10):
1756-
from serial import Serial, SerialException
1757-
1758-
def get_instance(*args, **kwargs):
1759-
try:
1760-
serial_port = Serial(*args, **kwargs)
1761-
serial_port.flush()
1762-
except Exception as e:
1763-
error("Unable to open serial port connection to \"%s\"" % port)
1764-
return False
1765-
return serial_port
1766-
1767-
def cdc_reset(serial_instance):
1768-
try:
1769-
serial_instance.sendBreak()
1770-
except:
1771-
try:
1772-
serial_instance.setBreak(False) # For Linux the following setBreak() is needed to release the reset signal on the target mcu.
1773-
except:
1774-
result = False
1775-
1776-
def cdc_term(serial_instance):
1777-
import serial.tools.miniterm as miniterm
1778-
1779-
term = miniterm.Miniterm(serial_instance, echo=True)
1780-
term.exit_character = '\x03'
1781-
term.menu_character = '\x14'
1782-
term.set_rx_encoding('UTF-8')
1783-
term.set_tx_encoding('UTF-8')
1784-
def cli_writer():
1785-
menu_active = False
1786-
while term.alive:
1787-
try:
1788-
c = term.console.getkey()
1789-
except KeyboardInterrupt:
1790-
c = '\x03'
1791-
if not term.alive:
1792-
break
1793-
if menu_active:
1794-
term.handle_menu_key(c)
1795-
menu_active = False
1796-
elif c == term.menu_character:
1797-
menu_active = True # next char will be for menu
1798-
elif c == '\x02' or c == '\x12': # ctrl+b/ctrl+r sendbreak
1799-
cdc_reset(term.serial)
1800-
elif c == '\x03' or c == '\x1d': # ctrl+c/ctrl+]
1801-
term.stop()
1802-
term.alive = False
1803-
break
1804-
elif c == '\x05': # ctrl+e
1805-
term.echo = not term.echo
1806-
elif c == '\x08': # ctrl+e
1807-
print term.get_help_text()
1808-
elif c == '\t': # tab/ctrl+i
1809-
term.dump_port_settings()
1810-
else:
1811-
text = c
1812-
for transformation in term.tx_transformations:
1813-
text = transformation.tx(text)
1814-
term.serial.write(term.tx_encoder.encode(text))
1815-
if term.echo:
1816-
echo_text = c
1817-
for transformation in term.tx_transformations:
1818-
echo_text = transformation.echo(echo_text)
1819-
term.console.write(echo_text)
1820-
term.writer = cli_writer
1821-
action('--- Terminal on {p.name} - {p.baudrate},{p.bytesize},{p.parity},{p.stopbits} ---\n'.format(p=term.serial))
1822-
action('--- Quit: CTRL+C | Reset: CTRL+B | Echo: CTRL+E ---')
1823-
action('--- Info: TAB | Help: Ctrl+H | Menu: Ctrl+T ---')
1824-
term.start()
1825-
try:
1826-
term.join(True)
1827-
except KeyboardInterrupt:
1828-
pass
1829-
term.join()
1830-
term.close()
1831-
1832-
result = False
1833-
serial_port = get_instance(port, baudrate=baudrate, timeout=timeout)
1834-
if serial_port:
1835-
serial_port.reset_input_buffer()
1836-
if reset:
1837-
cdc_reset(serial_port)
1838-
result = True
1839-
1840-
if sterm:
1841-
if not serial_port.is_open:
1842-
serial_port = get_instance(port, baudrate=baudrate, timeout=timeout)
1843-
try:
1844-
cdc_term(serial_port)
1845-
result = True
1846-
except:
1847-
pass
1848-
1849-
return result
1850-
1851-
18521756
# Subparser handling
18531757
parser = argparse.ArgumentParser(prog='mbed',
18541758
description="Command-line code management tool for ARM mbed OS - http://www.mbed.com\nversion %s\n\nUse 'mbed <command> -h|--help' for detailed help.\nOnline manual and guide available at https://github.com/ARMmbed/mbed-cli" % ver,
@@ -2566,7 +2470,7 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
25662470
error("Unable to flash the target board connected to your system.", 1)
25672471

25682472
if flash or sterm:
2569-
if not cdc(detected['port'], reset=flash, sterm=sterm):
2473+
if not mbed_cdc(detected['port'], reset=flash, sterm=sterm):
25702474
error("Unable to reset the target board connected to your system.\nThis might be caused by an old interface firmware.\nPlease check the board page for new firmware.", 1)
25712475

25722476
program.set_defaults(target=target, toolchain=tchain)
@@ -2774,7 +2678,7 @@ def detect(reset=False, sterm=False):
27742678
action("Detected unknown target connected to \"%s\" and using com port \"%s\"" % (target['mount'], target['serial']))
27752679
else:
27762680
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (target['name'], target['mount'], target['serial']))
2777-
cdc(target['serial'], reset=reset, sterm=sterm)
2681+
mbed_cdc(target['serial'], reset=reset, sterm=sterm)
27782682

27792683
if unknown_found:
27802684
warning("If you're developing a new target, you can mock the device to continue your development. "

mbed/mbed_cdc.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from serial import Serial, SerialException
2+
import serial.tools.miniterm as miniterm
3+
4+
def mbed_cdc(port, reset=False, sterm=False, baudrate=9600, timeout=10, print_term_header=True):
5+
def get_instance(*args, **kwargs):
6+
try:
7+
serial_port = Serial(*args, **kwargs)
8+
serial_port.flush()
9+
except Exception as e:
10+
error("Unable to open serial port connection to \"%s\"" % port)
11+
return False
12+
return serial_port
13+
14+
def cdc_reset(serial_instance):
15+
try:
16+
serial_instance.sendBreak()
17+
except:
18+
try:
19+
serial_instance.setBreak(False) # For Linux the following setBreak() is needed to release the reset signal on the target mcu.
20+
except:
21+
result = False
22+
23+
def cdc_term(serial_instance):
24+
term = miniterm.Miniterm(serial_instance, echo=True)
25+
term.exit_character = '\x03'
26+
term.menu_character = '\x14'
27+
term.set_rx_encoding('UTF-8')
28+
term.set_tx_encoding('UTF-8')
29+
30+
def console_print(text):
31+
term.console.write('--- %s ---\n' % text)
32+
33+
def input_handler():
34+
menu_active = False
35+
while term.alive:
36+
try:
37+
c = term.console.getkey()
38+
except KeyboardInterrupt:
39+
c = '\x03'
40+
if not term.alive:
41+
break
42+
if menu_active:
43+
term.handle_menu_key(c)
44+
menu_active = False
45+
elif c == term.menu_character:
46+
console_print('[MENU]')
47+
menu_active = True # next char will be for menu
48+
elif c == '\x02' or c == '\x12': # ctrl+b/ctrl+r sendbreak
49+
console_print('[RESET]')
50+
cdc_reset(term.serial)
51+
elif c == '\x03' or c == '\x1d': # ctrl+c/ctrl+]
52+
console_print('[QUIT]')
53+
term.stop()
54+
term.alive = False
55+
break
56+
elif c == '\x05': # ctrl+e
57+
console_print('[ECHO %s]' % ('OFF' if term.echo else 'ON'))
58+
term.echo = not term.echo
59+
elif c == '\x08': # ctrl+e
60+
print term.get_help_text()
61+
elif c == '\t': # tab/ctrl+i
62+
term.dump_port_settings()
63+
else:
64+
text = c
65+
for transformation in term.tx_transformations:
66+
text = transformation.tx(text)
67+
term.serial.write(term.tx_encoder.encode(text))
68+
if term.echo:
69+
echo_text = c
70+
for transformation in term.tx_transformations:
71+
echo_text = transformation.echo(echo_text)
72+
term.console.write(echo_text)
73+
term.writer = input_handler
74+
75+
if print_term_header:
76+
console_print('Terminal on {p.name} - {p.baudrate},{p.bytesize},{p.parity},{p.stopbits}'.format(p=term.serial))
77+
console_print('Quit: CTRL+C | Reset: CTRL+B | Echo: CTRL+E')
78+
console_print('Info: TAB | Help: Ctrl+H | Menu: Ctrl+T')
79+
80+
term.start()
81+
82+
try:
83+
term.join(True)
84+
except KeyboardInterrupt:
85+
pass
86+
term.join()
87+
term.close()
88+
89+
90+
result = False
91+
serial_port = get_instance(port, baudrate=baudrate, timeout=timeout)
92+
if serial_port:
93+
serial_port.reset_input_buffer()
94+
if reset:
95+
cdc_reset(serial_port)
96+
result = True
97+
98+
if sterm:
99+
if not serial_port.is_open:
100+
serial_port = get_instance(port, baudrate=baudrate, timeout=timeout)
101+
try:
102+
cdc_term(serial_port)
103+
result = True
104+
except:
105+
pass
106+
107+
return result

0 commit comments

Comments
 (0)