Author Topic: ATmega32U4: How can I use the HWB pin as IO but keep USB bootloader functionalit  (Read 2915 times)

0 Members and 1 Guest are viewing this topic.

Offline BlitzschnitzelTopic starter

  • Regular Contributor
  • *
  • Posts: 64
  • Country: de
So as far as I understand it, the HWB pin needs to be pulled down on power up and when the reset is pressed, so that the USB bootloader is used. Otherwise I can only program the chip via ISP programmer. Right?
However, I need 26 IO pins. So, every single pin including HWB. How, could I have both USB bootloader and IO on pin 33?
I am building a keyboard PCB and could just use a second switch next to the reset switch, but since HWB also needs to be low on powerup This could become annoying very quickly.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 13085
I presume you intend to scan a keyboard matrix?   If so 74HC138 (active low) or 74HC238 (active high) 3 to 8 line decoders are your friend. 

Use one to convert a three bit number in binary on three port pins to one of eight outputs active to scan your matrix saving you five pins, or two, with a four bit number to one of 16 outputs saving you 12 port pins.   If you need fewer scan lines, you save fewer pins, but  even one pin saved would get you off the hook for HWB.
« Last Edit: May 05, 2019, 02:23:03 am by Ian.M »
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 854
Quote
but since HWB also needs to be low on powerup This could become annoying very quickly.
The HWB needs to be low only when you want the bootloader to run. You put a weak pullup on that pin to prevent the bootloader from running. When you want it to run, then it gets pulled down (via switch). After the period where that pin is sampled on reset, you now have a normal io pin you can use. If you can't live with a weak pullup on that pin or have something pulling it down, then I guess you have a problem.
 

Offline BlitzschnitzelTopic starter

  • Regular Contributor
  • *
  • Posts: 64
  • Country: de
Thank you!
If you need fewer scan lines, you save fewer pins, but  even one pin saved would get you off the hook for HWB.
The matrix is already square, so I can't save any pins there. It is going to be a large keyboard.^^

The HWB needs to be low only when you want the bootloader to run. You put a weak pullup on that pin to prevent the bootloader from running.
Thanks. I guess I misread that part on powerup.
Other opensource keyboards have a constant pulldown on that pin. So, could it perhaps be as simple as in the attached picture?
In what ball park would a "weak" pullup resistor be? Weak mens high resistance right? Maybe I can find a two-stage switch like a camera shutter button so that I don't need a seperate switch.
« Last Edit: May 06, 2019, 03:58:31 am by Blitzschnitzel »
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 854
What firmware are you using. If the firmware has its own bootloader, then most likely it has its own method to trigger it. So maybe some are simply using the HWB pin to always trigger the bootloader, where the bootloader will determine whether it should jump to the app or run the bootloader app (depending on keys pressed, etc). Although I'm not sure why the boot reset vector fuse would not be used instead, if that was the case.

The fuses cannot be changed via the factory usb bootloader, so out of the box the 32u4 is set to use the HWB pin and the reset vector is 0.  In order to run the factory bootloader, the HWB pin needs to be low when the reset pin goes from low to high which basically changes the reset vector to the bootloader and the factory bootloader runs (and remains running).

If you do not have other means to program, then the factory usb bootloader will have to be used and a means to trigger it- the HWB pin. A weak pullup would just keep the HWB pin high- I don't know what value, but would not take much- 10K, 47K, 100K, whatever.  When you want to run the bootloader, pull the pin low before the reset. I don't know what your row/col scheme is, but I would guess the additional weak pullup should be harmless in most cases.

If you have a programmer for the mega32u4, then you can do whatever you want- use firmware that includes a bootloader, change the boot vector as wanted, eliminate the HWB usage, etc.

Your schematic - the HWB should have a resistor from it to Vcc, which means its pulled high. The switch is also connected to HWB, and the other side of the switch is connected to ground.  In other words that 10k resistor needs to move.
 

Offline BlitzschnitzelTopic starter

  • Regular Contributor
  • *
  • Posts: 64
  • Country: de
I am planing to use a Firmware called QMK. It works with DFU but also several other like the Arduino bootloader. I have absolutly no clue about bootloader programming, so at this stage I would like to avoid it. There are so many new things I am learning with this project already.^^
Your schematic - the HWB should have a resistor from it to Vcc, which means its pulled high.
I know, my hope was just that I might get away without a pullup. The col and row pins are the collumns and rows of my switch matrix and I am worried that when I mess with it, pressing the HWB switch might start to type a burst of characters before I can press the reset switch.
Can I perhaps use a capacitor to put a bit of a delay on the reset pin and pull both low with the same switch?
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 854
You can simply hold the reset down, then hold the HWB button down, release reset button, release HWB button, you are in bootloader.

You don't want that hwb floating on the reset- the hardware simply makes that pin an input while checking it, but does not enable a pullup so it could then go into the bootloader on any reset as you really don't know what voltage is on that pin when it is floating. I would assume a column input has its pullup enabled, and a row driver outputs low when active, so in either case a weak pullup on the hwb pin is harmless.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf