Skip to content

Add opt-in support for "LED_ENABLED" feature#15

Merged
volium merged 3 commits intomainfrom
led_feature_support
Feb 11, 2022
Merged

Add opt-in support for "LED_ENABLED" feature#15
volium merged 3 commits intomainfrom
led_feature_support

Conversation

@volium
Copy link
Owner

@volium volium commented Jan 20, 2022

Makes LED support "opt-in" by defining "LED_ENABLED". Feature takes 6 bytes when enabled.

Adds macros to handle active high and active low differences.

Compared to @osamuaoki's original implementation, moves turning on the LED to "run_bootloader", instead of "send_descriptor"; the behavior is pretty much the same, but it saves two bytes for the active-low case, since we only call cbi once when the bootloader becomes active (after setting T flag in SREG).

Removes old and unused LED_PIN declaration.

@volium
Copy link
Owner Author

volium commented Jan 20, 2022

@osamuaoki, it'd be great if you could review this. Thanks!

Makes LED support "opt-in" by defining "LED_ENABLED". Feature takes 6 bytes when enabled.
Adds macros to handle active high and active low differences.
Compared to Osamu's original implementation, moves turning on the LED to "run_bootloader", instead of "send_descriptor"; the behavior is pretty much the same, but it saves two bytes for the active-low case, since we only call `cbi` once when the bootloader becomes active (after setting T flag in SREG).
Removes old and unused LED_PIN declaration.
@volium volium force-pushed the led_feature_support branch from a8d97ce to e3cf9e1 Compare January 20, 2022 22:26
@osamuaoki
Copy link
Collaborator

Good idea to make this OPT-IN.

But I now see few issues. Before describing details, let me describe our situation.

  • nanoBoot (w/o LED) fits in 512B.
  • nanoBoot (w LED on Teensy2.0/Leonardo) fits in 512B -- 0df6c69 ("Update comments, fix typos", 2022-01-20)
  • nanoBoot (w LED on ProMicro is slightly over 512B -- uncomment to adopt from 0df6c69 ("Update comments, fix typos", 2022-01-20)
  • nanoBoot in sigprof's WIP branches has interesting feature but goes over 512B
  • LUFA HID bootloader needs 4096B (or other non-HID bootloaders are the same size)

Bootloader size can be 512B, 1024B, 2048B, 4096B.

In light of this, I now think adding opt-in for 1024B bootloader by including sigprof's WIP branch to publish device ID etc. is good idea for which extra features are added while keeping size small enough to address windows issues.

As for 0df6c69 ("Update comments, fix typos", 2022-01-20), I see issues for ProMicro for bootloader size which your code seems to address by moving "LED ON" earlier. But it comes with minor feature drop of LED behavior.

I had bad contact problem with USB connector. So I wanted USB device not just powered but also properly recognized by the host. So, I intended for LED is:

  1. PLUG-in USB. -> runs firmware
  2. The firmware may turn on/blink LED.
  3. Pressing RESET will turn off LED and start bootloader
  4. Only when bootloader makes some response to the host via USB after getting host request as OS level hot-plug handling, LED is turned on
  5. Start hid_bootloader_cli on host side
  6. After loading program from the host, bootloader turn off LED and jump to loaded firmware.

I thought checking proper OS level USB device discovery with LED helped me to avoid monitoring dmsg on linux host. If that is not available, LED is only power-on verification. I didn't see much functional value. Of course, bright LED is nice to have when ON ...

I recall my older PR #10 was including code size reduction idea from sigprof's repo so even promicro code fits into 512B. This is complicated and I skipped that part of code this time for cheery-picking.

In e3cf9e1 ("Add opt-in support for "LED_ENABLED" feature", 2022-01-20), you left my comment in line56 as:

; NOTE: This feature uses 6 bytes for active high and 8 bytes for active low

I think this is not consistent with your modification. It's all 6 bytes for me by glancing your change.

Any thought how you want to proceed?

Osamu

Moves "TURN_LED_ON" within "SET_CONFIGURATION" so LED is turned on ONLY when device has gone through enumeration.
Adds check for "LED_ACTIVE_LEVEL" to turn LED off at the beginning of "run_bootloader" if LED is active low. This has a 2-byte penalty which makes nanoBoot go beyond 512 bytes on ProMicro-compatible boards.
@volium
Copy link
Owner Author

volium commented Jan 24, 2022

Hey @osamuaoki, thank you for the feedback, I have addressed the issues you mentioned in the original commit.

With this last round of changes, you are correct, the code no longer fits on ProMicro-compatible boards, though. I think we should try to include at least one more optimization to make it fit everywhere, but I think we can/should do that on a separate PR, for now, with LED_ENABLED disabled, we are at 506 bytes on every board.

I have validated the change; moving TURN_LED_ON to be within the SET_CONFIGURATION section has the same effect as your original change (when it was within send_descriptor) but I think this makes the intent more clear, and should also avoid "most" false-positive enumerations.

I think if/when @sigprof makes more progress to include device descriptors (which I also think should be an opt-in feature), we should definitely allow the code to grow beyond the 512-byte limit (1024B).

Let me know what you think... thanks again!

@osamuaoki
Copy link
Collaborator

Hi #16 offer major size reduction.

As for descriptor, it may fit in.

I am not sure if removing "cli" is good idea, though. Maybe we can do descriptor without dropping cli.

YH is always 0. So may be we can use it as rZERO (Is there any good use of r0 now?)

Also, after real reset, registers should be zeroed. So XOR call to set registers to zero may be dropped.

(The caveat is we may get software reset by a simple JMP, then registers will not be zerod.)

rZERO, rONE, YH initialization should give us 6 bytes.

If size is tight, we can trim ID name from "nanoBoot" to "nB" (6 more bytes saved) :-)

Just a thought. I only have one unused ATmega32u4 board. ...

Good night ;-)

@osamuaoki
Copy link
Collaborator

Hi #16 is done which covers fix to promicro.

`set_watchdog_timer` was called in two places, but the parameter value
passed in r16 (the value used to unlock the watchdog configuration) was
the same in both cases.  Move the duplicated initialization of r16 into
the `set_watchdog_timer` function itself, saving 2 bytes.
@volium
Copy link
Owner Author

volium commented Feb 11, 2022

Hey @osamuaoki, thanks for taking another look at this change. I looked at PR #16 and I cherry-picked @sigprof's c2b4ba5 into this PR, so now LED can be enabled across ALL hardware variants without issues (512 bytes when using LED_ACTIVE_LEVEL 0).

I am going to merge this change now, so that we can work exclusively on the size optimizations and feature improvements separately.

@volium volium merged commit ce02b53 into main Feb 11, 2022
@volium volium deleted the led_feature_support branch February 11, 2022 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants