|
| 1 | +"""note-python CircuitPython Outboard Firmware Update example. |
| 2 | +
|
| 3 | +This file contains a complete working sample for demonstrating Notecard |
| 4 | +Outboard Firmware Update from CircuitPython. It configures the Notecard to |
| 5 | +enable Outboard Firmware Update for the host, then blinks the built-in LED with |
| 6 | +a recognizable pattern that serves as the "payload" you update over-the-air. |
| 7 | +
|
| 8 | +To demonstrate an update: change BLINK_DELAY (and bump FIRMWARE_VERSION) below, |
| 9 | +build a new firmware image, upload it to Notehub, and apply the DFU action to |
| 10 | +the host. The blink speed (and reported version) will change once the update |
| 11 | +is applied. |
| 12 | +
|
| 13 | +This example targets the Blues Swan on a Notecarrier F. Cygnet does not support |
| 14 | +CircuitPython. |
| 15 | +""" |
| 16 | +import time |
| 17 | +import board |
| 18 | +import digitalio |
| 19 | +import notecard |
| 20 | +from notecard import card as card_helper |
| 21 | +from notecard import dfu as dfu_helper |
| 22 | +from notecard import hub as hub_helper |
| 23 | + |
| 24 | +# The unique Product Identifier for your device. Claim one in a Notehub project. |
| 25 | +productUID = "com.your-company.your-project" |
| 26 | + |
| 27 | +# The firmware version reported to Notehub via dfu.status. Bump this (and change |
| 28 | +# BLINK_DELAY) when you build a new image to update to. |
| 29 | +FIRMWARE_VERSION = "1.0.0" |
| 30 | + |
| 31 | +# Seconds the LED stays on/off. Change this to make the update visually obvious. |
| 32 | +BLINK_DELAY = 0.5 |
| 33 | + |
| 34 | + |
| 35 | +def configure_outboard_dfu(card): |
| 36 | + """Put the Notecard online and enable Outboard Firmware Update for the host. |
| 37 | +
|
| 38 | + Outboard Firmware Update requires the Notecard to be in "continuous" or |
| 39 | + "periodic" mode. On a Notecarrier F the DFU signals are routed over the |
| 40 | + Notecard's shared AUX pins, so card.dfu uses mode "aux" and card.aux is set |
| 41 | + to "off" to free those pins for DFU. |
| 42 | + """ |
| 43 | + hub_helper.set(card, product=productUID, mode="continuous", |
| 44 | + sn="circuitpython-notecard") |
| 45 | + |
| 46 | + # Enable Outboard Firmware Update and tell the Notecard the host MCU type. |
| 47 | + card_helper.dfu(card, name="stm32", on=True, mode="aux") |
| 48 | + |
| 49 | + # Free the AUX pins so they can be used for Outboard Firmware Update. |
| 50 | + card_helper.aux(card, mode="off") |
| 51 | + |
| 52 | + # Enable host DFU and report the running firmware version to Notehub. |
| 53 | + dfu_helper.status(card, on=True, version=FIRMWARE_VERSION) |
| 54 | + |
| 55 | + |
| 56 | +def main(): |
| 57 | + """Enable Outboard DFU, then blink the built-in LED as the update payload.""" |
| 58 | + i2c = board.I2C() |
| 59 | + card = notecard.OpenI2C(i2c, 0, 0, debug=True) |
| 60 | + |
| 61 | + configure_outboard_dfu(card) |
| 62 | + |
| 63 | + led = digitalio.DigitalInOut(board.LED) |
| 64 | + led.direction = digitalio.Direction.OUTPUT |
| 65 | + |
| 66 | + print("Running firmware version {}. Hit CTRL-C to stop.".format(FIRMWARE_VERSION)) |
| 67 | + while True: |
| 68 | + led.value = True |
| 69 | + time.sleep(BLINK_DELAY) |
| 70 | + led.value = False |
| 71 | + time.sleep(BLINK_DELAY) |
| 72 | + |
| 73 | + |
| 74 | +main() |
0 commit comments