Skip to content

Commit

Permalink
Merge pull request #5 from adafruit/touch1200
Browse files Browse the repository at this point in the history
Touch1200
  • Loading branch information
dhalbert authored Aug 22, 2018
2 parents 713ec9b + dbaa8be commit 06491d3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 42 deletions.
60 changes: 32 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,80 @@
and the `nordicsemi` library.

This package is derived from the Nordic Semiconductor ASA package
https://github.com/NordicSemiconductor/pc-nrfutil, version 0.5.3.
THe code has been converted from Python 2 to Python 3.
[pc-nrfutil](https://github.com/NordicSemiconductor/pc-nrfutil), version 0.5.3.
The code has been converted from Python 2 to Python 3.

The executable `nrfutil` has been renamed to `adafruit-nrfutil` to distinguish it from the
original executable.

This tool can be used used with the [Adafruit nRF52 Feather](https://www.adafruit.com/product/3406)
This tool can be used with the [Adafruit nRF52 Feather](https://www.adafruit.com/product/3406)
to flash firmware images onto the device using the simple serial port.

This library is written for Python 3.5+. It is no longer Python 2 compatible!

# Installation

## Prerequisites

- Python3
- pip3

Run the following commands to make `adafruit-nrfutil` available from the command line
or to development platforms like the Arduino IDE or CircuitPython:

### OS X and Linux
## Installing from PyPI

To install in user space in your home directory:
This is recommended method, to install latest version

```
$ pip3 install --user adafruit-nrfutil
```

## Installing from Source

Use this method if you have issue installing with PyPi or want to modify the tool. First clone this repo and go into its folder.

```
$ git clone https://github.com/adafruit/Adafruit_nRF52_nrfutil.git
$ cd Adafruit_nRF52_nrfutil
```

Note: following commands use `python3`, however if you are on Windows, you may need to change it to `python` since windows installation of python 3.x still uses the name python.exe

To install in user space in your home directory:

```
$ pip3 install -r requirements.txt
$ python3 setup.py install --user
$ python3 setup.py install
```

If you get permission errors when running `pip3 install`, your `pip3` is older
or is set to try to install in the system directories. In that case use the
`--user` flag:

```
$ cd Adafruit_nRF52_nrfutil
$ pip3 install -r --user requirements.txt
$ python3 setup.py install --user
$ python3 setup.py install
```

If you want to install in system directories (generally not recommended):
```
$ cd Adafruit_nRF52_nrfutil
$ sudo pip3 install -r requirements.txt
$ sudo python3 setup.py install
```

Note: When installing requirements if you encounter the message
**Cannot uninstall 'six'. It is a distutils installed project ...**,
you may need to add `--ignore-installed six` when running pip.

### Windows

#### Option 1: Pre-Built Binary

A pre-built 32-bit version of adafruit-nrfutil.exe is included as part of this repo in the
`binaries/win32` folder. You can use this pre-built binary by adding it to your
systems `$PATH` variable

#### Option 2: Build nrfutil from Source

Make sure that you have **Python 3.5** available on your system.
### Create self-contained binary

To generate a self-contained Windows `.exe` version of the utility (Windows only),
run these commands in a CMD window:
To generate a self-contained executable binary of the utility (Windows and MacOS), run these commands:

```
pip3 install pyinstaller
cd Adafruit_nRF52_nrfuil
cd Adafruit_nRF52_nrfutil
pip3 install -r requirements.txt
cd Adafruit_nRF52_nrfutil\nordicsemi
pyinstaller __main__.py --onefile --clean --name adafruit-nrfutil
```
You will find the .exe in `Adafruit_nRF52_nrfutil\nordicsemi\adafruit-nrfutil.exe`.
You will find the .exe in `Adafruit_nRF52_nrfutil\nordicsemi\dist\adafruit-nrfutil` ( with `.exe` if you are on windows).
Copy or move it elsewhere for your convenience, such as directory in your %PATH%.

# Usage
Expand Down
13 changes: 9 additions & 4 deletions nordicsemi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,21 @@ def update_progress(progress=0, done=False, log_message=""):
@click.option('-sb', '--singlebank',
help='Single band bootloader to skip firmware activating delay, default: Dual bank',
type=click.BOOL,
default=False,
is_flag=True)
@click.option('-t', '--touch',
help='Open port with specified baud then close it, before uploading',
type=click.INT,
default=0)

def serial(package, port, baudrate, flowcontrol, singlebank):
def serial(package, port, baudrate, flowcontrol, singlebank, touch):
"""Program a device with bootloader that support serial DFU"""
serial_backend = DfuTransportSerial(port, baudrate, flowcontrol, singlebank)
serial_backend = DfuTransportSerial(port, baudrate, flowcontrol, singlebank, touch)
serial_backend.register_events_callback(DfuEvent.PROGRESS_EVENT, update_progress)
dfu = Dfu(package, dfu_transport=serial_backend)

click.echo("Upgrading target on {1} with DFU package {0}. Flow control is {2}, {3} bank mode"
.format(package, port, "enabled" if flowcontrol else "disabled", "Single" if singlebank else "Dual"))
click.echo("Upgrading target on {1} with DFU package {0}. Flow control is {2}, {3} bank, Touch {4}"
.format(package, port, "enabled" if flowcontrol else "disabled", "Single" if singlebank else "Dual", touch if touch > 0 else "disabled"))

try:
dfu.dfu_send_images()
Expand Down
37 changes: 28 additions & 9 deletions nordicsemi/dfu/dfu_transport_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class DfuTransportSerial(DfuTransport):
DEFAULT_FLOW_CONTROL = False
DEFAULT_SERIAL_PORT_TIMEOUT = 1.0 # Timeout time on serial port read
SERIAL_PORT_OPEN_WAIT_TIME = 0.1
NRF52_RESET_WAIT_TIME = 0.1
TOUCH_RESET_WAIT_TIME = 1.5 # Wait time for device into DFU mode
DTR_RESET_WAIT_TIME = 0.1
ACK_PACKET_TIMEOUT = 1.0 # Timeout time for for ACK packet received before reporting timeout through event system
SEND_INIT_PACKET_WAIT_TIME = 0.5 # 1.0 # Time to wait before communicating with bootloader after init packet is sent

Expand All @@ -72,12 +73,13 @@ class DfuTransportSerial(DfuTransport):
FLASH_PAGE_SIZE = 4096 # 4K for nrf52
DFU_PACKET_MAX_SIZE = 512 # The DFU packet max size

def __init__(self, com_port, baud_rate=DEFAULT_BAUD_RATE, flow_control=DEFAULT_FLOW_CONTROL, single_bank=False, timeout=DEFAULT_SERIAL_PORT_TIMEOUT):
def __init__(self, com_port, baud_rate=DEFAULT_BAUD_RATE, flow_control=DEFAULT_FLOW_CONTROL, single_bank=False, touch=0, timeout=DEFAULT_SERIAL_PORT_TIMEOUT):
super(DfuTransportSerial, self).__init__()
self.com_port = com_port
self.baud_rate = baud_rate
self.flow_control = 1 if flow_control else 0
self.single_bank = single_bank
self.touch = touch
self.timeout = timeout
self.serial_port = None
self.total_size = 167936 # default is max application size
Expand All @@ -88,23 +90,40 @@ def __init__(self, com_port, baud_rate=DEFAULT_BAUD_RATE, flow_control=DEFAULT_F
def open(self):
super(DfuTransportSerial, self).open()

# Touch is enabled, disconnect and reconnect
if self.touch > 0:
try:
touch_port = Serial(port=self.com_port, baudrate=self.touch, rtscts=self.flow_control, timeout=self.timeout)
except Exception as e:
raise NordicSemiException("Serial port could not be opened on {0}. Reason: {1}".format(self.com_port, e))

# Wait for serial port stable
time.sleep(DfuTransportSerial.SERIAL_PORT_OPEN_WAIT_TIME)

touch_port.close()
logger.info("Touched serial port %s", self.com_port)

# Wait for device go into DFU mode and fully enumerated
time.sleep(DfuTransportSerial.TOUCH_RESET_WAIT_TIME)

try:
self.serial_port = Serial(port=self.com_port, baudrate=self.baud_rate, rtscts=self.flow_control, timeout=self.timeout)
except Exception as e:
raise NordicSemiException("Serial port could not be opened on {0}. Reason: {1}".format(self.com_port, e))

logger.info("Opened serial port %s", self.com_port)

# Wait for the system to reset
# Wait for serial port stable
time.sleep(DfuTransportSerial.SERIAL_PORT_OPEN_WAIT_TIME)

# Toggle DTR to reset the board and enter DFU mode
self.serial_port.setDTR(False)
time.sleep(0.05)
self.serial_port.setDTR(True)
# Toggle DTR to reset the board and enter DFU mode (only if touch is not used)
if self.touch == 0:
self.serial_port.setDTR(False)
time.sleep(0.05)
self.serial_port.setDTR(True)

# Delay to allow device to boot up
time.sleep(DfuTransportSerial.NRF52_RESET_WAIT_TIME)
# Delay to allow device to boot up
time.sleep(DfuTransportSerial.DTR_RESET_WAIT_TIME)

def close(self):
super(DfuTransportSerial, self).close()
Expand Down
2 changes: 1 addition & 1 deletion nordicsemi/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@

""" Version definition for nrfutil. """

NRFUTIL_VERSION = "0.5.3.post8"
NRFUTIL_VERSION = "0.5.3.post9"

0 comments on commit 06491d3

Please sign in to comment.