Jumping to the USB bootloader from software

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using an at90usb1287 and need to jump to the bootloader based on a command over the USB without changing any fuses. Right now I believe it actually wants the reset line to go low to jump there. Can I use the watchdog timer to reset the uC to do this? Looks like I might be able to put it into a loop while the watchdog resets it...is this valid way to do things? Using atmel DFU

Chief Tinkerer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

USB bootloaders are more complex than "normal" AVR bootloaders as they usually involve using the USB interface itself and the bootloader may be enumerating as some completely different class to the main app. As such you don't really want an AVR-USB to enter the bootloader at power on because inserting the device into PC would then involve it enumerating as HID/generic/CDC or whatever the bootloader uses at first then when the decision is made not to do bootloading but to run the app code you'd then need to be able to say "oh, no forget, that, I didn't mean to enumerate as one of those, what I really am is a ...". This works both ways so it' not quite as simple as jumping back and forth between boot and app code as it is on a regular AVR where the fact that the UART might become active while it's in "bootloader mode" just goes by un-noticed. This is why AVR-USB have the added complication of the HWB signal and it's usually the case the user has do do something like holding down a button to assert that signal at power on to say "this time I really want you to enumerate as a bootloading device". So while it may be possible to jump back from an app that has already enumerated as "something" to a bootloader that will enumerate as "something else" you need to consider how you are going to handle this.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Right now I have a device that has a physical HWB and RESET button to enter bootloader. It works quite well, but the bootloader is only needed by us (the supplier).

This new device is a field serviceable joystick to operate industrial equipment. We want the end user to be able to plug the joystick into a computer and flash new firmware from our custom software utility. When the bootloader is not in use, the usb will enumerate as an HID (most likely) to display calibration data and output the joystick axis variables. With this in mind, it would be advantageous to force the device into bootloader mode from a trigger in the software so users can upgrade the firmware. It will obviously enumerate as a new device, but we can catch this in the software. What is the preferred way to do such a thing and jump to the bootloader? Hopefully I have given enough information.

Chief Tinkerer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can't you just advise the users to "plug it in with button X held"? Where button X is some button that holds HWB?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Can't you just advise the users to "plug it in with button X held"? Where button X is some button that holds HWB?

I'll have to see if that is a viable option. The only buttons accessible to the user would be on the faceplate of the joystick. Some joystick won't even have buttons on them because they have other devices in the faceplate (thumb slider modules, etc). We want the field mechanics to avoid dismantling the unit to service it as it would be more work to take it out of the machine. The boards inside are potted anyhow so a surface mount button is out of the question.

Our joystick example below has no buttons.

I guess we could have the HWB pin externally broken out to the wiring harness and have the user jumper it to ground or something.

Chief Tinkerer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd wait for our man in Norway to stumble past as he's bound to know the "pretty" solution to this one.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your help Clawson.

Some updated info:
I talked to the guys and they are willing to break out the HWB pin so it can be externally shorted to make things easier. Now with this in mind, it will need a hardware reset to enter the loader. Is this something I can do in the code? I tried to use the watchdog and it wasn't able to make it jump to the bootloader. It must be looking for an actual electrical low on the RST pin.

Chief Tinkerer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Can't you just advise the users to "plug it in with button X held"? Where button X is some button that holds HWB?

I was thinking the same thing until I saw the picture. I am assuming the joystick is connected to potentiometers or the like to relay position as opposed to snap action buttons inside.

Are the position indicators connected to an off chip processor that could send a signal that the AVR could see on powerup, hence jump to bootloader mode?

Just an idea

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
clawson wrote:
Can't you just advise the users to "plug it in with button X held"? Where button X is some button that holds HWB?

I was thinking the same thing until I saw the picture. I am assuming the joystick is connected to potentiometers or the like to relay position as opposed to snap action buttons inside.

Are the position indicators connected to an off chip processor that could send a signal that the AVR could see on powerup, hence jump to bootloader mode?

Just an idea

We are going to make the HWB external, so that shouldn't be an issue anymore. But to answer your question, the position is calculated using a 3d hall effect sensor. I don't think it would be a good idea to have the joystick go into the bootloader based on it's position as the operator may start the machine with the joystick not in the neutral position.

Chief Tinkerer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah I kind of guessed that, hence my half hearted question. Thanks for being kind and not ripping me apart ;)

With the external button, what happens if the unit is installed in the macine already? Wont it suck to have to remove the unit to do a firmware update?

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps HWB and RESET could be connected to port pins through one-shots.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you have to stick with the Atmel DFU bootloader, you can jumper /HWB to ground permanently and break out /RESET - this would mean that ANY external reset would start the bootloader. I don't believe the official Atmel DFU bootloader supports a software jump, but you can try it anyway using a scheme similar to the one I recommend in LUFA:

http://www.fourwalledcubicle.com...

Alternatively, you can use the LUFA DFU class bootloader, which is compatible with Atmel's FLIP but does support software jumps to start it.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

this would mean that ANY external reset would start the bootloader.

Do you mean a forced reset would but a power on wouldn't? If not then surely when you just plug the thing into USB and it powers up you don't want it to normally enter the bootloader do you? Or maybe you do but your thought is that once there it does not enumerate as a bootloading class but sees valid app code and goes onto enter that which then enumerates as the joystick HID?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Do you mean a forced reset would but a power on wouldn't? If not then surely when you just plug the thing into USB and it powers up you don't want it to normally enter the bootloader do you?

Yes, the bootloader would be started when the watchdog times out, or /RESET is pulled low, but NOT on power on. This is the scheme currently used on the latest "Teensy" boards and other USB AVR boards that have a single button to enter the bootloader.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The way that I've solved this is by using the watchdog to reset the AVR and then intercepting the startup early by having a function in the .init3 section that jumps to the bootloader if required.

void Boot_StartBootloader(void);
void Boot_BootloaderCheck(void) ATTR_INIT_SECTION(3);

/* Magic key for activating bootloader */
volatile uint32_t Boot_MagicKey ATTR_NO_INIT;

/* Address of bootloader is defined by linker */
extern void bootloader_start(void) __attribute__ ((section (".bootloader"), noreturn));

/* Reboot and run bootloader */
void Boot_StartBootloader(void)
{
    /* Disable all interrupts */
    __asm__ __volatile__ ("cli" ::)

    /* Set the bootloader key to the magic value and enable the watchdog to
    force a timeout to reset the AVR. Upon reset, the AVR will execute
    Boot_BootloaderCheck() before main() and if the magic key is set then
    will call the bootloader.
    NOTE: BOOTRST fuse should not be programmed */
    Boot_MagicKey = MAGIC_BOOT_KEY;
	
    /* Re-enable watchdog and wait for it to reset us */
    wdt_enable(WDTO_500MS);
    for(;;);
}

/* Check if bootloader should be run */
void Boot_BootloaderCheck(void)
{
    // If the reset source was the watchdog and the key is correct, clear it and jump to the bootloader
    if ((MCUSR & (1<<WDRF)) && (Boot_MagicKey == MAGIC_BOOT_KEY))
    {
        /* Move interrupt vectors to start of bootloader flash section
           IVSEL=0 vectors at start of Flash, IVSEL=1 vectors at start of bootloader flash section (as set by BOOTSZ fuses) */
        uint8_t Mcucr = MCUCR;
        MCUCR = Mcucr | (1 << IVCE);
        MCUCR = Mcucr | (1 << IVSEL);
		
        /* Clear boot key to force next reset into application code */
        Boot_MagicKey = 0;
		
        /* Jump to bootloader */
        bootloader_start();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

abcminiuser wrote:
Quote:

Do you mean a forced reset would but a power on wouldn't? If not then surely when you just plug the thing into USB and it powers up you don't want it to normally enter the bootloader do you?

Yes, the bootloader would be started when the watchdog times out, or /RESET is pulled low, but NOT on power on. This is the scheme currently used on the latest "Teensy" boards and other USB AVR boards that have a single button to enter the bootloader.

- Dean :twisted:

That's good to know as I was under the impression that a power on would allow it to jump to the bootloader (provided HWB was grounded). I'll try forcing it to jump to the bootloader in .init3 and see what happens. (Thanks jonsole)

I would love to use LUFA, but we need to avoid it because we want don't want to use ISP in production to reflash all the units - and the Atmel DFU comes preinstalled on all the chips.

Dean when are you going to convince Atmel to throw the lufa DFU on the new ic's? :lol:

Chief Tinkerer