I would like to aggressively power off the board with the USER button.
The plan:
- pressing USR button resets the board
- press&hold for 2 seconds powers off the board
When a pin is activated, external IC cuts-off the main power to processor.
The issue:
SDcard is still plugged in and the OS complains about "Eject the USB flash disc first before removing device".
also,
my Mac crashes in the process 1 in 6 times (probably due to USB drivers or SD card?)
Question:
- How to remove SD card from being used as mass storage?
See pyb.usb_mode('CDC+HID') which does not work in this context.
- How to prepare the micropython and the processor for being shut down aggressively from within an IRQ ?
- is there a better timing method for Switch() callback?
- how could I call more complicated functions for LED PWM pulsing from within the Switch() callback IRQ routine?
Thank you!
Code: Select all
import pyb
####### Init Timers ###############
micros = pyb.Timer(2, prescaler=83, period=0x3fffffff)
micros.counter(0)
### power-off pin, when high, the main power is cut to processor
POWER_OFF = Pin.cpu.B1.init(pyb.Pin.OUT_PP, pull=pyb.Pin.PULL_DOWN, af=-1)
sw = pyb.Switch()
# BTN_callback
def BTN_callback():
timeout=2000000 #microseconds
pyb.LED(1).toggle()
pyb.LED(2).toggle()
pyb.LED(3).toggle()
micros.counter(0)
while (sw() and (micros.counter()<timeout) ):
pyb.LED(3).toggle()
pyb.LED(1).toggle()
if (micros.counter()>=timeout):
print('Switching OFF:\n')
pyb.sync()
pyb.usb_mode('CDC+HID')
pyb.sync()
PIN_OFF.high() ## this is where the power is lost
else:
print("Reset here::")
pyb.LED(1).on()
pyb.LED(2).off()
pyb.LED(3).off()
sw.callback(BTN_callback)