Skip to content

Getting files onto NRF52 #912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
No1089 opened this issue Jun 7, 2018 · 31 comments
Closed

Getting files onto NRF52 #912

No1089 opened this issue Jun 7, 2018 · 31 comments

Comments

@No1089
Copy link

No1089 commented Jun 7, 2018

Hi,

I finally received my J-link and programmed three different NRF52 boards, but on the one I want to use it most (Ruuvi tag), I am not sure how to load the code. It does not have a serial connection that is enabled(and I have no idea how to get it running), nor a USB port. The WebREPL via webBluetooth is working, but none of the REPL file transfer programs (or Ampy) supports that. I do have a Bluefruit LE Friend (UART) but for that I first need the NRF52 to connect etc.

Is it possible to use SWD to upload the code, instead of transferring the files?

@deshipu
Copy link

deshipu commented Jun 7, 2018

I think the WebREPL had a file upload functionality?

@No1089
Copy link
Author

No1089 commented Jun 7, 2018

@deshipu I'm an idiot. I was using the Espruino webREPL - just googled micropython WebREPL - and yes it has that functionality. The documentation on the NRF52 could use some editing - a few steps were missing/unexplained. Thanks.

@No1089 No1089 closed this as completed Jun 7, 2018
@No1089
Copy link
Author

No1089 commented Jun 7, 2018

https://micropython.org/webrepl/? does not allow me to use WebBluetooth :(
This is what I was using: https://glennrub.github.io/webbluetooth/micropython/repl/

@No1089 No1089 reopened this Jun 7, 2018
@jerryneedell
Copy link
Collaborator

There are instructions for adding support for the WebBluetooth here.
https://github.com/adafruit/circuitpython/tree/master/ports/nrf
It is not enabled by default so you will have to create a custom build.

@No1089
Copy link
Author

No1089 commented Jun 7, 2018

I have done so, and I can access the REPL with this: (https://glennrub.github.io/webbluetooth/micropython/repl/), but unlike (https://micropython.org/webrepl/), there is no option to upload a file so I can get my code running.

@jerryneedell
Copy link
Collaborator

Ah - sorry -- I just reread your earlier posts and see that.
Its's not pretty, but I suppose you could compile your code in as a "frozen" module.

@No1089
Copy link
Author

No1089 commented Jun 7, 2018

@jerryneedell Apologies for my ignorance - but how do I create a frozen module?

@jerryneedell
Copy link
Collaborator

jerryneedell commented Jun 7, 2018

It's been awhile since I played with this, but there is an example here:
https://github.com/adafruit/circuitpython/tree/master/ports/nrf/freeze
This is compiled into your build. I think any .py file in the "freeze" folder will also get compiled in. You can then invoke it from the REPL. I don't recall if it gets stored as "freeze/test" or as test.
If you look at help('modules')
should should see it. I'll try to provide an example tonight or tomorrow if you have not gotten it working first.

@jerryneedell
Copy link
Collaborator

jerryneedell commented Jun 7, 2018

see: https://github.com/adafruit/circuitpython/blob/master/ports/nrf/Makefile#L360
for how the Makefile handles it.
in line 36 FROZEN_MPY_DIR = freeze

@tannewt tannewt added this to the 4.0 milestone Jun 7, 2018
@jerryneedell
Copy link
Collaborator

Here is an example executing the freeze/test.py build into the current master

Adafruit CircuitPython 3.0.0-beta.0-43-gde61bd0 on 2018-06-02; Bluefruit nRF52 Feather with NRF52832
>>> 
>>> 
>>> import test
>>> test.hello()
Hello nrf52!
>>> 

@jerryneedell
Copy link
Collaborator

jerryneedell commented Jun 7, 2018

better example- I copied thhese files to the freeze folder"

erryneedell@Ubuntu-Macmini:~/projects/adafruit_github/circuitpython_master/ports/nrf$ cat freeze/blinky.py 
import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED2)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)
jerryneedell@Ubuntu-Macmini:~/projects/adafruit_github/circuitpython_master/ports/nrf$ cat freeze/ble_scan.py 

from ubluepy import Scanner, constants

def display_scan_results(scan_entries):
    for e in scan_entries:
        print("ADDR:  ", e.addr())
        print("TYPE:  ", e.addr_type())
        print("RSSI:  ", e.rssi())

        # Parse the contents of the advertising packet
        scan = e.getScanData()
        if scan:
            for s in scan:
                # Convert byte array to hex format string
                hex = ' '.join('0x%02X' % b for b in s[2])
                # Display enum value and hex string together
                print('\t{}: {}'.format(s[1], hex))

        # Line break between record sets
        print("")

# Scan 1s for advertising devices in range
s = Scanner()
scan_res = s.scan(1000)

# Display the scan results
display_scan_results(scan_res)

rebuilt and uploaded then executed them:

Adafruit CircuitPython 3.0.0-beta.0-60-g74ced17 on 2018-06-07; Bluefruit nRF52 Feather with NRF528
32
>>> import blinky

Blue LED blinking
(entered control-C to terminate)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "blinky.py", line 10, in <module>
KeyboardInterrupt: 
>>> import ble_scan
ADDR:   c8:69:cd:1c:5d:f8
TYPE:   0
RSSI:   -84
        AD_TYPE_FLAGS: 0x1A
        AD_TYPE_MANUFACTURER_SPECIFIC_DATA: 0x4C 0x00 0x10 0x05 0x01 0x10 0xC8 0x70 0xC0

ADDR:   c8:69:cd:1c:5d:f8
TYPE:   0
RSSI:   -84

lots more followed....

@jerryneedell
Copy link
Collaborator

jerryneedell commented Jun 7, 2018

building code into "freeze" is certainly not ideal for testing but it does work if you have no other way to get code uploaded. It would be great to have upload capability added to the BLE REPL

Good luck!

@No1089
Copy link
Author

No1089 commented Jun 8, 2018

@jerryneedell Fantastic! Thank you. And thank you for the examples. I just had a chance to test and it is working perfectly! Now at least I can get going, I don't mind that it takes a while currently.

@No1089
Copy link
Author

No1089 commented Jun 8, 2018

Next question - how do I get a file in frozen to run on boot? As Main.py in root does?

@deshipu
Copy link

deshipu commented Jun 8, 2018

This is a bit hacky, but you could create a main.py that imports it with:

with open("main.py", "w") as f:
    f.write("import yourfile")

@jerryneedell
Copy link
Collaborator

but how can you get the main.py file loaded :-(
I tried just naming the frozen file main.py, but that did not work. It runs if I "import main"

@jerryneedell
Copy link
Collaborator

never mind - I see what you did there ;-)

@jerryneedell
Copy link
Collaborator

@deshipu suggestion works great!

@jerryneedell
Copy link
Collaborator

jerryneedell commented Jun 8, 2018

then to remove it:
control-c and enter REPL

import os
os.remove("main.py")

nice!

@No1089
Copy link
Author

No1089 commented Jun 8, 2018

Really sorry for my ignorance here, but @deshipu - I created a main.py with the code you posted and I am just getting an error when I try to run it. I'm assuming I should be doing something different. Could you perhaps post an example as you did previously @jerryneedell ? I typed the command in directly, but no luck.

@jerryneedell
Copy link
Collaborator

jerryneedell commented Jun 8, 2018

Assuming you have blinky.py in the freeze folder and have loaded the version of CP with it so you can run

import  blinky

ok
then open REPL and at the prompt type.

with open("main.py", "w") as f:
    f.write("import blinky")

then reboot and it should run on boot. Demo in a few minutes.

@jerryneedell
Copy link
Collaborator

jerryneedell commented Jun 8, 2018

here is a demo:

Press any key to enter the REPL. Use CTRL-D to reload.

Adafruit CircuitPython 3.0.0-beta.0-75-gc01ce17 on 2018-06-08; Bluefruit nRF52 Feather with NRF52832
>>>
>>> with open("main.py","w") as f:
...     f.write("import blinky")
...
...
...
13
>>>
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
main.py output:



LED is blinking

Note: you need to keep hitting "ENTER" 3 tines after typing the 2nd line to get it to execute

@No1089
Copy link
Author

No1089 commented Jun 8, 2018

When the ... appears, do I just wait or press enter?
This is what I am seeing:
>>> with open("main.py", "w") as f: ... f.write("import blinky") ... ... ... Traceback (most recent call last): File "<stdin>", line 2, in <module> OSError: 5 >>>

@jerryneedell
Copy link
Collaborator

hit enter 3 times to execute the write -- looks like you did that and got OSError 5 -- that is an I/O error.
No idea why.

@No1089
Copy link
Author

No1089 commented Jun 8, 2018

Tried it with a different board just now - Feather52 instead of the RuuviTag - same problem (OSError: 5). Heading home and will check again. Thanks for the help so far.

@jerryneedell
Copy link
Collaborator

FYI - I am using a feather52 as well. Good luck!

@jerryneedell
Copy link
Collaborator

One difference - I am not using the BLE REPL -- using USB for the feather52. Not sure if or why it may matter.

@deshipu
Copy link

deshipu commented Jun 8, 2018

Well, you put it on the filesystem.

@jerryneedell
Copy link
Collaborator

I just confirmed that when using BLE REPL - attempting to write fails with OSError 5

@jerryneedell
Copy link
Collaborator

jerryneedell commented Jun 8, 2018

It looks like there is some difference in the FS initialization when BLE REPL is enabled. I noticed that at some point in my testing, it recreated the FS leaving only a lib folder. This done in supervisor/filesystem.h if no FS is found.
So whatever is going on , when BLE REPL is enabled, the FS appears to be read-only to python while via the USB REPL it is writeable. As I recall, this was a bit of a mystery anyway.
Note
in the nrf build
CIRCUITPY_BOOT_OUTPUT_FILE is not defined.

//#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"

the is a lot going on with the FS at startup. Still trying to understand what is different under BLE REPL

@tannewt tannewt modified the milestones: 4.0.0 - Bluetooth, 4.0 Beta Jul 10, 2018
@tannewt
Copy link
Member

tannewt commented Dec 7, 2018

Closing this in favor of #1010

@tannewt tannewt closed this as completed Dec 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants