Can I use the USB port if I flash custom firmware onto the arduboy fx?

I am a little bit worried that I might break the device.

I want to write a game from scratch without using the Arduboy2 and Arduino framework. As a learning experience for working with embedded devices. I basically want to fully initialize the hardware myself and see how that goes.

Can I actually brick the Arduboy FX somehow? Or can I always just flash the games back to it using Arduino IDE?

I don’t know if the FX has the fuses set properly to protect the bootloader. Many of the original Arduboys didn’t protect the bootloader, so it was possible to overwrite it. If this happens you need an In-Circuit Serial Programmer (ICSP) to restore the bootloader.

If your bootloader protection fuses are set properly, you shouldn’t be able to “break” anything. At worst, you will need to use the reset button to load a new program via USB from the IDE.

You can use the sketch in the following post to see if your bootloader is properly protected.

1 Like

Just to check, are you worried about overwriting the bootloader or are you actually planning to write a new bootloader?

If you’re only worried about overwriting the bootloader when uploading some software, what @MLXXXp says applies.

If you’re planning to create a new bootloader as well then it might be a different story.

(If your goal is only to avoid relying on Arduino, Arduboy2, and/or avr-libc then you don’t need to write a new bootloader because those aren’t part of the bootloader. The bootloader is an entirely separate entity.)

There are a few things that could possibly actually damage your Arduboy, that may be more likely to happen if you’re working directly with the hardware. For example:

  • If you were to set a pin that a button is on as an output and set it high and then push and hold the button, you would be shorting a high output to ground. This would cause high current to flow through the pin, with a chance that you could “burn it out”.

  • (Even using the Arduboy or Arduino libraries) if a programming error causes continual writes to an EEPROM location at high speed, you could fairly quickly exceed the EEPROM write limit, causing further writes to that location to fail to hold the intended value.

1 Like

I think the issue is that I am not sure what the difference is. I never really used Arduino.

My goal is to write a library similar to Arduboy2 but without the direct dependency on Arduino. As far as I understood, Arduino does already hardware initialization procedures for microcontrollers.

I want this to be a learning experience on how to work with hardware. So I would have to initialize the uC, look at how the pins are used in the Arduboy, set them up properly etc.

What I am concerned is the fact that I flash my own code onto the device. If I decide to go back to the classic game selection screen for my Arduboy FX, is that possible after flashing my own games onto it?

I think this applies here. I am not entirely sure what the bootloader is and does. But I think writing my own library and games should not somehow change that. My current understanding is that the bootloader is what makes it possible to upload sketches from the Arduino IDE to the device. Basically the thing that actually flashes the code. And I was concerned if I flash my own software without going through the Arduino IDE (like using ravedude, avrdude), I might break stuff.

It’s actually nothing to do with Arduino, it’s to do with the AVR chips. (I expect other Arduino-supported chips have some similar arrangement, though I’m not certain.)

Yes, it is. Though there is a bit more to it as well…

Program memory (i.e. the flash memory, not the RAM or EEPROM) on AVR chips is divided into two sections: ‘bootloader space’ and ‘user space’.

When you boot a ‘classic’ Arduboy it more or less goes straight into ‘user mode’ (it might briefly run in bootloader mode, I’m not certain). That’s because of the way the bootloader is programmed. The original Arduboy uses the ‘Cathy’ bootloader, as do the majority of ATmega32u4s (as far as I’m aware).

When you boot an Arduboy FX it usually goes into the bootloader mode first (that menu you see showing you all the games on the chip is actually the bootloader). That’s a custom bootloader written by @Mr.Blinky. (I’m not sure what the official name is, if there is one.) It can also be configured to boot straight into the last loaded game. (Again, I don’t know if that means it boots straight into ‘user mode’ or makes a brief detour into ‘bootloader mode’ first.)

When the chip runs in ‘bootloader mode’ it runs the ‘bootloader code’ (machine code stored in ‘bootloader space’), and when the chip runs in ‘user mode’ it runs the ‘user code’ (machine code stored in the ‘user space’).

The bootloader mode is responsible for handling the uploading of a ‘user mode’ program, and thus is the part that implements the serial communication over USB. Theoretically you could upload code to an AVR chip through other means, it just happens to be the case that the majority use USB to do this. (Importantly, an AVR chip can only overwrite user space when in bootloader mode.)

User mode software can also do serial communication over USB, but this happens (as far as I understand) entirely separately to the communication the bootloader does. I.e. they do not share any code at all.

Note that overwriting the bootloader actually requires a different process to overwriting user space (typically that method is to use an ICSP as mentioned by @MLXXXp earlier).

In Rust, or in some other language?

What about avr-libc?

(Arduino’s AVR implementation depends upon avr-libc, and Arduboy2 relies on both, so the dependency graph would actually be a sort of triangle-shaped DAG.)

As far as I’m aware, the actual CPU doesn’t really require much initialisation.

I think Arduino might set up a few interrupts, but the actual main function is very bare bones:

Note that those setup and loop functions are the ones Arduino instructs programmers to define.

main is actually marked as a ‘weak symbol’, so it’s possible for a programmer to create a new main function and the compiler will then ignore Arduino’s main in favour of the programmer’s. (Note that this is a compiler feature, not a feature of the C++ language.)

As main is the entry point for any piece of C++ software, this implies that nothing else would be called before or after main and that you should be able to trace everything that occurs throughout the program from main. (With the exception of interrupt handlers, which must be defined separately.)

For the Arduboy, it’s actually Arduboy2 that sets up the majority of the hardware. In particular the screen, buttons and speakers. Arduino only handles the CPU because Arduino can’t predict what peripherals the user of an Arduino board will attach to the chip. An ‘Arduino board’ is actually fairly minimal in terms of defined hardware since it’s designed for rapid prototyping and flexibility where peripherals are concerned. (See Arduino Leonardo for the board the Arduboy is based on.)

As long as you haven’t overwritten the bootloader (which shouldn’t be possible to do by accident as long as the bootloader protection fuses are set) then any software you write will end up in the ‘user section’ and run in ‘user mode’, and as mentioned earlier that game select screen is actually the custom FX bootloader, so it should run and function properly regardless of what you upload into the ‘user section’.

I would however advise you to keep your device bootloading into the menu rather than using the ‘boot directly into game’ feature as a precaution. That way if you do end up making something that ‘misbehaves’, rebooting will take you to the bootloader instead of the ‘misbehaving’ user code.

The Arduino IDE actually just sends commands to avrdude.
If you enable verbose uploading you’ll get avrdude messages.

There isn’t actually anything particularly ‘magical’ about how uploading works.

Basically the uploading software just sends a bunch of part-text part-binary commands to the bootloader and the bootloader interprets and responds to them. That protocol is known as avr109. (See also this thread and this thread.)

I know about this because I’ve actually written a fully functional* uploader for Arduboy in C++.

(* When I say ‘fully functional’, it could handle all of the commands properly but I never bothered to make it parse command line arguments or handle COM-port hopping properly, so it was more of a proof of concept/library than a functioning program, which is one of the reasons I never actually published it.)

1 Like

Minor correction in the interest of avoiding confusion:
The original Arduboy uses the Arduino Leonardo version of the “Caterina” bootloader. @Mr.Blinky named his bootloaders “Cathy”.

The bootloader itself may do some hardware initialisation/manipulation and leave it in a state that could be different than the freshly reset microcontroller would be. I’m saying this just because a program that is loaded directly, without a bootloader included, (say, via an ICSP) might behave differently than if it was started from a bootloader, due to the program not initialising something that the bootloader would have.

2 Likes

These responses have been super useful! Thank you very much!

Yep, Rust. I have only used it as a general purpose language, but often heard that it’s a really great experience to program embedded systems with it :slight_smile: There are some crates available for avr like avr-device, atmega-hal and avr-hal.

That I did not know. This makes me definitely less worried then! :slight_smile: Thanks again to you all!

1 Like

Not by uploading a program over USB. The Arduboy FX always powers up with the (bootloader) menu. So you can always upload a new program.

The FX has no protection fuses set but the bootloader protects itself from being overwritten.

Cathy3K

ATM that is only possible by using an ICSP programmer to flash the 'boot with game ’ version of the bootloader. It could also be done by a custom program but that has yet to be written.

If that version of the bootloader was flashed, you could still start with the bootloader menu by holding down the down button while switching the FX on.

1 Like