Detecting USB connection in boot.py

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
DaveAZ
 
Posts: 23
Joined: Sun Apr 15, 2018 7:14 pm

Detecting USB connection in boot.py

Post by DaveAZ »

Is there a way to detect if the usb cable is plugged in and connected to a computer in the code that runs in the boot.py file?

I'm setting up the boot.py to either make the CP volume writable by CP code or by the computer. Currently I'm using one of the touchpads on the clue board to detemine where the readwrite flag should be true or false but, since we're planning to have the clue.py code log data to the volume when it's running on battery power, I was thinking that might be a simpler way to set it up for the students to use.

The touchpad being touched or not doesn't seem to be all that reliable. It works but it sometimes takes one or two extra resets (via the reset button) to put it into readonly mode). Here's the current boot.py code if that's helpful:

Code: Select all

from adafruit_clue import clue
import storage
print("Running boot.py")
print(clue.touch_2)
if (clue.touch_2):
    storage.remount("/", readonly=False)
    print("Circuit Python code can write to CP Volume")
else:
    storage.remount("/", readonly=True)
    print("Host Computer can write to CP Volume")
    print("Circuit Python code can only read from CP Volume")
Thanks,
Dave

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Detecting USB connection in boot.py

Post by dastels »

Is there a reason you aren't using one of the CLUE's buttons? That would avoid having to load up the clue module.

Unfortunately the CLUE doesn't provide a way to check the power voltage, which would let you determine if it was by USB or battery.

Dave

User avatar
DaveAZ
 
Posts: 23
Joined: Sun Apr 15, 2018 7:14 pm

Re: Detecting USB connection in boot.py

Post by DaveAZ »

I did first try using the A or B buttons to set the readwrite mode in boot.py but those if you are pressing either of those buttons during a hard reset (pressing the reset button) it puts switches from the CIRCUITPYTHON volume to the CLUEBOOT volume and you then have to reload the CP UF2 file on it to get back to the CIRCUITPYTHON volume.

It is unfortunate that the buttons don't work for this because that would certainly be cleaner.

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Detecting USB connection in boot.py

Post by dastels »

The buttons should work for this. Double-pressing reset gets you into the bootloader (CLUEBOOT), not the A/B buttons. Pressing reset will get out of the bootloader and back to CIRCUITPY. See the CLUE tutorial guide https://learn.adafruit.com/adafruit-clue, specifically https://learn.adafruit.com/adafruit-clue/circuitpython.

Dave

User avatar
DaveAZ
 
Posts: 23
Joined: Sun Apr 15, 2018 7:14 pm

Re: Detecting USB connection in boot.py

Post by DaveAZ »

I agree that double-clicking the reset button will get you into the bootloader and that's what I normally use but I have verified several times that holding down either the A or the B button when pressing reset a single time also puts you into the bootloader (CLUEBOOT.

Are you saying the Clue board you haven't does not behave that way? I'm trying to figure out if there was some update to the bootloader firmware that changed this behavior but I don't have an answer on that.

Thanks,
Dave

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Detecting USB connection in boot.py

Post by dastels »

Mine enters bootloader when reset with A or B held as well. I'm surprised by that.

After digging through the CLUE's bootloader source, it seems the nRF52840 bootloader has a few modes depending on how you want to do the loading. Alas, those modes are selected by the buttons A & B on the CLUE.

You could add a button to an input and read that. Or short input 0, 1, or 2 to GND with a wire or alligator clip jumper to read it as a button.

Dave

User avatar
DaveAZ
 
Posts: 23
Joined: Sun Apr 15, 2018 7:14 pm

Re: Detecting USB connection in boot.py

Post by DaveAZ »

Yes, I expeimented some with the touch sensor as part to detemrine if the CP volume should be read-only or readwrite but it was quite unreliable.

So instead, I wrote a boot.py file that blinks the LED and then looks for the user to press and hold the A button after after the LED blinks. That works very reliably.

Here's the code in case anyone would like to use it:

Code: Select all

"""CircuitPython Essentials Storage logging boot.py file"""
import board
import digitalio
import storage
import time

print("Running boot.py")
leds = digitalio.DigitalInOut(board.WHITE_LEDS)
leds.direction = digitalio.Direction.OUTPUT
leds.value = True
time.sleep(0.01)
leds.value = False

button_a = digitalio.DigitalInOut(board.BUTTON_A)
button_a.direction = digitalio.Direction.INPUT
button_a.pull = digitalio.Pull.UP
time.sleep(2)

if button_a.value == False:  # if button_a is pressed
    print("Circuit Python code can write to CP Volume")
    storage.remount("/", readonly = False)
else:
    button_a.remount("/", readonly = True)
    print("Host Computer can write to CP Volume")
    print("Circuit Python code can only read from CP Volume")
leds.value = False



User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: Detecting USB connection in boot.py

Post by dastels »

Nice!

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “CLUE Board”