Skip to content

Commit 7e1542e

Browse files
committed
Perform pin toggling before startup and extend timeout
1 parent 8a4e08e commit 7e1542e

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

tests/api/test_connect.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,9 @@ async def test_connect_skip_bootloader_rts_dtr_pins(make_znp_server, mocker):
102102
mocker.patch.object(znp.nvram, "determine_alignment", new=CoroutineMock())
103103
mocker.patch.object(znp, "detect_zstack_version", new=CoroutineMock())
104104

105-
num_pings = 0
106-
107-
def ping_rsp(req):
108-
nonlocal num_pings
109-
num_pings += 1
110-
111-
if num_pings >= 3:
112-
return c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS)
113-
114-
# Ignore the first few pings so we trigger the pin toggling
115-
znp_server.reply_to(c.SYS.Ping.Req(), responses=ping_rsp)
105+
znp_server.reply_to(
106+
c.SYS.Ping.Req(), responses=[c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS)]
107+
)
116108

117109
await znp.connect(test_port=True)
118110

zigpy_znp/api.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
NETWORK_COMMISSIONING_TIMEOUT = 30
3939
BOOTLOADER_PIN_TOGGLE_DELAY = 0.15
4040
CONNECT_PING_TIMEOUT = 0.50
41-
CONNECT_PROBE_TIMEOUT = 5.0
41+
CONNECT_PROBE_TIMEOUT = 10
4242

4343

4444
class ZNP:
@@ -468,6 +468,16 @@ async def _skip_bootloader(self) -> c.SYS.Ping.Rsp:
468468
"""
469469

470470
async def ping_task():
471+
LOGGER.debug("Toggling RTS/DTR pins to skip bootloader or reset chip")
472+
473+
# The default sequence is DTR=false and RTS toggling false/true/false
474+
for dtr, rts in zip(
475+
self._znp_config[conf.CONF_CONNECT_DTR_STATES],
476+
self._znp_config[conf.CONF_CONNECT_RTS_STATES],
477+
):
478+
self._uart.set_dtr_rts(dtr=dtr, rts=rts)
479+
await asyncio.sleep(BOOTLOADER_PIN_TOGGLE_DELAY)
480+
471481
# First, just try pinging
472482
try:
473483
async with async_timeout.timeout(CONNECT_PING_TIMEOUT):
@@ -488,20 +498,8 @@ async def ping_task():
488498
except asyncio.TimeoutError:
489499
pass
490500

491-
# This is normally done just for Slaesh's CC2652RB stick
492-
LOGGER.debug("Toggling RTS/DTR pins to skip bootloader or reset chip")
493-
494-
# The default sequence is DTR=false and RTS toggling false/true/false
495-
for dtr, rts in zip(
496-
self._znp_config[conf.CONF_CONNECT_DTR_STATES],
497-
self._znp_config[conf.CONF_CONNECT_RTS_STATES],
498-
):
499-
self._uart.set_dtr_rts(dtr=dtr, rts=rts)
500-
await asyncio.sleep(BOOTLOADER_PIN_TOGGLE_DELAY)
501-
502501
# At this point we have nothing else to try, don't catch the timeout
503-
async with async_timeout.timeout(CONNECT_PING_TIMEOUT):
504-
return await self.request(c.SYS.Ping.Req())
502+
return await self.request(c.SYS.Ping.Req())
505503

506504
async with self.capture_responses([CatchAllResponse()]) as responses:
507505
ping_task = asyncio.create_task(ping_task())

zigpy_znp/zigbee/application.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ async def probe(cls, device_config: conf.ConfigType) -> bool:
135135
LOGGER.debug("Probing %s", znp._port_path)
136136

137137
try:
138-
async with async_timeout.timeout(PROBE_TIMEOUT):
139-
await znp.connect()
140-
141-
return True
138+
# `ZNP.connect` times out on its own
139+
await znp.connect()
142140
except Exception as e:
143141
LOGGER.debug(
144142
"Failed to probe ZNP radio with config %s", device_config, exc_info=e
145143
)
146144
return False
145+
else:
146+
return True
147147
finally:
148148
znp.close()
149149

0 commit comments

Comments
 (0)