How to Bootload ATMega328P at 3.3v / 12MHz?

Hi All and thanks in advance for the help!

I have created a few boards using the ATMega328P (-AU) at 5 volts but I am now trying to create one running at 3.3 volts so I don’t have to use vltage dividers on my logic interfaces.

I have looked at these two posts, which are helpful, but I still have a few questions:

https://forum.arduino.cc/?topic=596711#msg4052388

Based on my [probably too limited] understanding of the info in these posts, I have tried to upload the bootloader to my board as if it were an Arduino Pro Mini using the Arduino as ISP method. No luck.

Questions:

  • The first post tells me that I need to run the 328 at no more than 12 MHz:

  • Can anyone recommend a good external 12MHz resonator that is hand-solderable?
    I’m pretty good hand-soldering SMTs down to about 1206

  • Prior to loading a bootloader, I am guessing that the 328 runs off the internal clock
    If I supply 3.3v to it will it configure itself appropriately to receive my bootloader? Or
    perhaps it sees that I have an external clock hooked up and just uses that?

  • The second post tells me “You need an 8 MHz bootloader on the chip, if it is running at 8 MHz. You will need an ICSP programmer to do that.” (thanks jremington). Of course I am looking for 12MHz but point taken. I am assuming that an Arduino Mini or Pro Mini running at 3.3v must also be limited to 12MHz and therefore the Arduino as ISP should be trying to load my board with 3.3v settings (brownout detection < 3.3v and clock at 12MHz).

  • Bad assumption on my part?

  • Do I really need an ICSP programmer?

  • The first post also tells me that I can look at hardware\boards.txt. I am running the Arduino IDE on a Mac and I can’t find that. Can anyone tell me how to find that file?

Thanks!

vtmakr

Modify the optiboot make file by changing clock frequency to 12 MHz. Then build it. And burn the .hex file to the mcu. The optiboot can be found inside hardware folder in the Arduino ide's directory

Here's my thoughts based on limited knowledge and experience:

12MHz resonator

If I were adding an external component to generate the system clock, then I'd go for a crystal (+capacitors) over a resonator. I've always used crystals myself. If I recall correctly, resonators are not as accurate as crystals.

The 328P datasheet says that you are ok with 10MHz at 2.7V so I can see that 12MHz would be ok at 3.3V. Your main way of communicating with the 328P will be via its serial port so you need to make sure that the frequency you choose can be divided down to give you the baud rates you want with a reasonable degree of accuracy. If you want around 12MHz, then I'd suggest you go with the magic frequency of 11.0592MHz which gets you all the standard baud rates up to 230,400baud with 0% error.

Bootloader

With a virgin chip, it is factory configured to use its internal oscillator. It will ignore any external clock signals until you program the fuse bits to tell it which clock source to use. The datasheet says:

The device is shipped with internal RC oscillator at 8.0 MHz and with the fuse CKDIV8 programmed,
resulting in 1.0 MHz system clock. The start-up time is set to maximum, and the time-out period is
enabled: CKSEL=0010, SUT=10, CKDIV8=0. This default setting ensures that all users can make their
desired clock source setting using any available programming interface.

If I recall correctly, if you select an Arduino Pro Mini at 3.3V, the bootloader that is installed by the IDE expects the system clock to be 8MHz and it would configure the serial port for that. If you are actually running at 12MHz, then your baud rate dividers will be wrong and baud rates like 9600 baud will actually be 14400baud because your clock is running 50% faster.

ICSP Programmer

You need a way of loading the bootloader into your virgin chip. The ICSP programmer is the tool that does this. I think you can use an Arduino to do this but i've started using a dedicated ICSP programmer. They are very cheap to buy.

Finally, there are some pre-built bootloaders here. One of the ready built bootloaders is for a 328P with an 11.0592MHz crystal. There's a pre-built bootloader for a 12MHz crystal as well.

m_k_akash:
Modify the optiboot make file by changing clock frequency to 12 MHz. Then build it. And burn the .hex file to the mcu. The optiboot can be found inside hardware folder in the Arduino ide's directory

Thanks m_k_akash.

Unfortunately, your answer is way beyond my current experience level with the AVR stuff and bootloading. So I don't understand which file is the optiboot make file, how one specifies 12 MHz within it and how to initiate the build.

If I figure that out, then I would have to figure out how to burn the resulting hex file to my board. So far, I have only ever used the Arduino as ISP method.

vtmakr

markd833:
Here's my thoughts based on limited knowledge and experience:

12MHz resonator

If I were adding an external component to generate the system clock, then I'd go for a crystal (+capacitors) over a resonator. I've always used crystals myself. If I recall correctly, resonators are not as accurate as crystals.

The 328P datasheet says that you are ok with 10MHz at 2.7V so I can see that 12MHz would be ok at 3.3V. Your main way of communicating with the 328P will be via its serial port so you need to make sure that the frequency you choose can be divided down to give you the baud rates you want with a reasonable degree of accuracy. If you want around 12MHz, then I'd suggest you go with the magic frequency of 11.0592MHz which gets you all the standard baud rates up to 230,400baud with 0% error.

...

Thank you markd833! Excellent points. I hadn't considered the baud rate so that will be super helpful and I'll go with the 11.0592.

Regarding resonator vs crystal, I have so far been using only resonators because the built-in caps save space. I agree that they are not as consistent as a crystal, but they have never caused me a problem.

I have a lot to learn about bootloading and will do my best with the info you provided, starting with the link you provided. But let me start here: Are the ICSP programmer and bootloader separate concepts and one simply has to select an appropriate programmer for the bootloader one wants, and presume that it will come with some instructions? Any further info you can provide me on how to get up to speed with this would be much appreciated.

vtmakr

The ICSP programmer is a separate piece of hardware that connects between your PC and your target board using 5 wires - MOSI, MISO, SCK, RESET, GND and sometimes an extra wire for the supply voltages. If you have an Arduino UNO, for example, the ICSP programmer connects to the 6-pin connector that's on the board.

The ICSP programmer is usually used to program a bootloader into a virgin AVR device such as the 328p. Once you have the bootloader programmed in, you don't need to use the ICSP programmer again unless you want to change the bootloader.

The bootloader is the first piece of code that runs when the chip powers up. In the case of Arduino, it's main job is to receive your sketch and store it in the flash memory.

If you get GitHub - MCUdude/MiniCore: Arduino hardware package for ATmega8, ATmega48, ATmega88, ATmega168, ATmega328 and ATmega328PB, you'll find more support (including pre-built bootloaders) for 12MHz (and 11.0592 MHz too!)

Thanks all for replies. And I will do my best to educate myself from the links you provided (thanks westfw and )

So, pls keep me honest so I am not running off half baked... If I understand it correctly the various components are:

  1. The bootloader which is as markd833 described (thanks!). I think this may be a .hex file?

  2. The programmer (including either an ISCP programmer or the Arduino as ISP method) which is responsible for
    (a) properly setting the fuses in the 328P, and
    (b) loading the bootloader into the 328P

  3. There must be some source that one modifies / adjusts for creating the bootloader .hex file?

  4. A make utility that compiles and links the bootloader .hex file from the source file?

  5. Optionally a fuse calculator that makes it a little bit easier to set the fuse values (high and low in the case of the 328P)?

If I have this right, then I suspect I could figure out #5 from the 328P datasheet.

But I am still quite in the dark on #'s 3 and 4. If anybody can point me in the right direction, much appreciated.

vtmakr

  1. There must be some source that one modifies / adjusts for creating the bootloader .hex file?

The source for the Arduino bootloader is in the Arduino install, in the …hardware/arduino/avr/bootloaders/optiboot/ directory. (exact location is OS and install-type dependent.)
There’s a newer version at GitHub - Optiboot/optiboot: Small and Fast Bootloader for Arduino and other Atmel AVR chips
The version used by minicore (may have additional tweaks) is MiniCore/avr/bootloaders/optiboot_flash at master · MCUdude/MiniCore · GitHub (but MiniCore also has pre-built .hex files for a stunning variety of Clock/Baud/etc combinations.)

  1. A make utility that compiles and links the bootloader .hex file from the source file?

It uses make. Since the Arduino IDE no longer uses make, you may need to install make separately from somewhere else. There may also be sensitivities to avr-gcc compiler version…

  1. Optionally a fuse calculator

https://www.engbedded.com/fusecalc/