Arduino vs. "Bare Metal" programming - Page 1
Forum section: electronics / beginners

author: jgalak
author_rank: Regular Contributor
timestamp: December 08, 2017, 01:58:11 pm
content: Another newbie question:I'm fairly familiar, at this point, with working with Arduinos. However, much that I'm reading suggests that there are advantages to coding "bare metal" for the same AVR chips. So what are those advantages? What does being able to skip the bootloader get me?As one line of consideration, most of the projects I'm currently playing with benefit from minimal power. So if using bare metal coding can somehow help reduce power consumption, that's a definite plus.Please note that I'm only talking software - the hardware side, as far as I can tell, is pretty interchangeable. I should be able to put an Arduino bootloader onto darn near any AVR Mega chip (and I think it's been compiled for the Tiny, too), and, conversely, I believe I can write bare metal code onto an Arduino (or compatible) board, simply overwriting the bootloader. If either of these assumptions are incorrect, please let me know.

author: Mr. Scram
author_rank: Super Contributor
timestamp: December 08, 2017, 02:05:01 pm
content: Quote from: jgalak on December 08, 2017, 01:58:11 pmIt's not just the bootloader, it's the libraries too. Timing would be an example where the Arduino environment can screw you over. If you want exact, or near as can be with AVR exact, you will pull your hair out if you use the Arduino environment. There are all sorts of things being done under the surface for you that seemingly randomly throw things off on the surface. If you take control of these things, you can have much tighther control over timing matters.

author: Maxlor
author_rank: Frequent Contributor
timestamp: December 08, 2017, 02:27:47 pm
content: It's not an either or, actually. You can do bare metal programming from inside the Arduino environment, if you like. For example, you don't have to use digitalWrite() and digitalRead() to use your IO pins, you can also access the registers directly. The advantage of doing so is that your code will be much faster; the disadvantage is that you then also have to do the configuration yourself and get it right, and of course the pin numbers (or even register names) will be different for every chip, you have to take that into account yourself if you're using more than one type of chip.It's similar for other peripherals; for example, the analogWrite() function depends on interrupts being enabled, and uses the timers at a fixed frequency to run the PWM. You can disable interrupts and configure the timer registers yourself for more flexibility; as a result of course, the built-in arduino stuff that depended on those interrupts no longer works and you have to take care of that stuff yourself if it is important (one example comes to mind: on a Leonardo, the built-in serial communication stops working if you disable interrupts or change the timer configuration.)If you need low power operation, then yeah, reconfiguring the chip is a good idea because then you can run at low clock speeds (although be aware, if you have processing to do, it's often more efficient overall to switch the clock to a high speed for a short time, then going back to sleep, as opposed to running at a low speed all the time.) You don't have to remove the Arduino bootloader if you don't want to however; if you disable/overwrite the Arduino configuration in setup(), you achieve mostly the same thing as if you had programmed the chip without any Arduino stuff involved.Btw, it might be interesting to look at the Arduino source code to see how they configure the MCU exactly, and what they do in their high level function calls such as digitalWrite(). It's not very difficult to understand.

author: Nusa
author_rank: Super Contributor
timestamp: December 08, 2017, 02:31:45 pm
content: Quote from: jgalak on December 08, 2017, 01:58:11 pm"bare metal" as you call it simply means talking directly to the hardware registers yourself, rather than using library routines to do so. The advantages are eliminating the sometimes huge performance overhead of the libraries and having full control over what happens. The disadvantages are that you need to know a LOT more about the hardware, the learning curve will be much steeper, and your programming skills need to be up to the task. You'll also have more opportunities to make mistakes.The main advantage of eliminating the bootloader is that you now have the entire flash memory space for your program. The big disadvantage is that you lose the ability program the avr over the serial interface. You'll have to program via the ISP header instead.

author: Mr. Scram
author_rank: Super Contributor
timestamp: December 08, 2017, 02:41:23 pm
content: Quote from: Nusa on December 08, 2017, 02:31:45 pmIt's not a huge hassle to program via the ISP. It needs some minor figuring out, but then it's fairly straightforward. I think I actually prefer it, because it makes the board super simple.

author: Buriedcode
author_rank: Super Contributor
timestamp: December 08, 2017, 03:53:52 pm
content: Quote from: Mr. Scram on December 08, 2017, 02:41:23 pmIt requires an ISP programmer of course, which tend to be very cheap these days.

author: phil from seattle
author_rank: Super Contributor
timestamp: December 08, 2017, 04:40:31 pm
content: Quote from: Mr. Scram on December 08, 2017, 02:41:23 pmI use both Arduino IDE and Atmel Studio. I agree that ISP isn't hard but it takes you down one of several more complex paths. At the far end lies Atmel Studio. While Atmel Studio is quite powerful, it is amazingly confusing in an ADHD-triggering kind of way. Unless one needs the power of it (for debugging, for example), I'd stay away.Unless you need the bootloader space or want to fiddle with fuses, I'd stick with the Arduino bootloader.

author: jgalak
author_rank: Regular Contributor
timestamp: December 08, 2017, 05:06:07 pm
content: Just to clarify, based on what I've seen on other sites and videos: I can, if needed, program entirely in AVR Studio, and then upload the program to the chip through the Arduino USB interface, right? I'd obviously lose the space the bootloader takes up, but otherwise I'm not losing anything, not getting any additional issues from the arduino code? It runs just as if it was installed on a bare chip via ISP?(I think I actually own an ISP programmer somewhere, but never used it - bought it to have the ability to do in-system debugging, but never needed it)

author: Mr. Scram
author_rank: Super Contributor
timestamp: December 08, 2017, 05:09:45 pm
content: Quote from: jgalak on December 08, 2017, 05:06:07 pmYou can do the reverse too. Use the convenience of the Arduino IDE and program your device via ISP. That way you get to use all the space and don't have the bootloader delay.

author: phil from seattle
author_rank: Super Contributor
timestamp: December 08, 2017, 05:20:11 pm
content: ISP doesn't equal in circuit debugging. This is an area where atmel has be schizoid for many years. But, using ISP doesn't get you debug.Yes, you can use AS7 to program Arduino developed code. Unless you need the bootloader space, I don't really understand why. Using AS7 for that is like killing a fly with a nuke. If you want debugging, you need to use something like Atmel ICE.

author: Mr. Scram
author_rank: Super Contributor
timestamp: December 08, 2017, 05:25:23 pm
content: Quote from: phil from seattle on December 08, 2017, 05:20:11 pmI don't see why you wouldn't. Once set up it's dead easy and eliminates the whole USB thing with a number of potential points of failure. Plus you get to build super simple boards, as you only need a few traces and a header.Also, Atmel Studio isn't directly related to the Arduino bootloader. You can use the Arduino IDE and omit the bootloader. You don't need Atmel Studio for eliminating it.

author: jgalak
author_rank: Regular Contributor
timestamp: December 08, 2017, 05:35:34 pm
content: Quote from: phil from seattle on December 08, 2017, 05:20:11 pmHuh. Didn't realize that. The ICE, at $140, is much more money (though not ridiculously so). Probably won't bother until/unless I find I really need it. I'm used to working without real debuggers (not in the embedded projects), just by printing messages, so not a huge deal.

author: Mr. Scram
author_rank: Super Contributor
timestamp: December 08, 2017, 05:46:38 pm
content: Quote from: jgalak on December 08, 2017, 05:35:34 pmThe Atmel ICE is stupidly expensive compared to other programmers and nothing special. They do occasionally have discounts, though.Do I see a 25% discount until januari 1st?http://www.microchipdirect.com/product/search/all/ATATMEL-ICE

author: brucehoult
author_rank: Super Contributor
timestamp: December 08, 2017, 06:07:47 pm
content: I think the bootloader only decides whether to run the existing code or download new code. I don't think it does a lot of setup, and certainly nothing you can't reverse/overwrite once you get into your own code.Most of the setup is done in whatever runtime library and main() you've linked into your program, and that's fully under your control.The only reason to get rid of the bootloader is if you really can't afford that 1.5 KB of flash space.

author: Mr. Scram
author_rank: Super Contributor
timestamp: December 08, 2017, 06:16:08 pm
content: Quote from: brucehoult on December 08, 2017, 06:07:47 pmIt takes slightly longer before your code starts running too. That can be important in some cases.

author: Fire Doger
author_rank: Regular Contributor
timestamp: December 08, 2017, 06:43:55 pm
content: Programming depends on your application, for some stuff you need Assembly (like the 10$ ebay function generator) and for others any kind will work fine.BUT, knowing how to program in bare metal means that you know everything about your chip which is a must if you want to use it in full power or least energy or as robust as possible etc... And of course do whatever you want without searching for libraries.Even if you don't want to code in bare metal knowledge will definitely makes your sketches much better.For bare metal a real debugger and logic analyzer/scope is a must I think, you will waste a lot of time hunting occasional bugs without it. Start with the basics for now, registers, clock, timers.Good luck

author: phil from seattle
author_rank: Super Contributor
timestamp: December 08, 2017, 07:09:35 pm
content: Atmel ICE Basic is $90. So yes, it's still expensive but if you need debugging, it's kind of the only game in town. Don't get me started on the bone head strategy of keeping debugWire a secret. What's so bad about 3rd party interfaces?  don't buy the full ICE, get the basic versionYou can buy the bare ICE board for $50 but it doesn't come with a case or the ribbon cable. The ribbon cable cost $23-ish so you can cut costs a little. There are ways to cut that cost but perhaps for a different thread.One of my biggest complaints on the ICE itself is the stupid PITA 50 mil pitch connector/ribbon cable they use. Super fragile. I made an adapter board that allows me to use standard 100 mil pitch cables and holds the ICE in place so I don't have to touch their ribbon cable. But what's so hard about producing an ICE with standard 100 mil pitch headers???By the way, another advantage of the USB cable approach of Arduino is you don't need a separate power source like you do with the ICE.

author: ez24
author_rank: Super Contributor
timestamp: December 08, 2017, 08:28:55 pm
content: Quote from: jgalak on December 08, 2017, 01:58:11 pmSomething to think about. TI's MSP430 boards are low power, they make a big deal about this. Their IDE is free (last year it was $500) and is excellent (good debugger). Also TI (or someone) has an Arduino compatible IDE for their MSP430s. It is called Energia,http://energia.nu/.I have not tried it.My experience was I started with Arduino (and failed) and most forum members said "bare metal". I had no idea what this meant but I tried STM32 and that was way too advance for me. It took me a week to install the IDE and tools and I never could blink the LED. Then I went to PIC and I don't remember why that did not work. Anyway I ended up with a MSP430 (sometimes TI has good sales on them) and started with their IDE. I worked on it for a couple of months trying to program registers. Now I know what "bare metal" means. I had a good experience with TI's IDE and board.Unfortunately there are not as many examples for the MSP430 as the Arduino (I am very thick headed and need lots of examples) and I gave up on the MSP430 but I am glad I tried because I now know how registers work. I decided that it was too advance for me.Now I bought an Arduino kit that has lessons (by Elegoo) and I am working on them. One thing in back of my mind is if I need a low power device, I can use MSP430 with Energia. I have not used Energia. I think I will post a question on it, just to see what others think.Another possiblity is I might learn a lesson on the Arduino with Arduino IDE and then try the lesson on a MSP430 with Energia and if I have a clear head try the same lesson with bare metal. So there are options.One disadvantage to me is there are lots of different MSP430 versions so examples on one board is limited.

author: Nusa
author_rank: Super Contributor
timestamp: December 08, 2017, 09:16:00 pm
content: It depends on what you mean by minimal power. You need something to last for a week on batteries, or a year on the same batteries? Both are possible, depending on what you need to accomplish. For extreme savings, you usually need to look at and minimize the hardware as well as the software. Even something as simple as the power LED is a power hog in those circumstances.Here's as good a place to start reading as any:https://learn.sparkfun.com/tutorials/reducing-arduino-power-consumptionMost processors in this category have extreme low power modes. It's not just an Arduino thing. Arduino typically has more examples to look at.

author: jgalak
author_rank: Regular Contributor
timestamp: December 08, 2017, 09:23:39 pm
content: Quote from: Nusa on December 08, 2017, 09:16:00 pmOne of the current projects I'm looking at is High Altitude Balloon systems. Those carry batteries, and may or may not carry solar for recharge (depending on how long the balloon stays up). It seems to be less about duration and more about peak currents - there's only so much power available. Especially when a transmitter powers up.

author: Nusa
author_rank: Super Contributor
timestamp: December 08, 2017, 09:48:32 pm
content: Quote from: jgalak on December 08, 2017, 09:23:39 pmNow there's a case where you may have conflicting power goals depending on altitude and climate. Saving power vs keeping consumer-grade components and batteries warm and dry (condensation is evil!) enough to operate.

author: jgalak
author_rank: Regular Contributor
timestamp: December 09, 2017, 01:32:02 am
content: It's easier to add heat than take it away. Adding a small resistive heater of some sort can always be done. Or, for a short flight, chemical heat packs or something, though that's less ideal.

author: retrolefty
author_rank: Super Contributor
timestamp: December 09, 2017, 01:54:58 am
content: The original question was about using Arduino IDE supplied library functions Vs learning and usinguser written 'register level' functions.The answer is of course you can utilize both as your application requires. If you decide that a specific arduino function is a 'bottleneck' in your program you can certainly read the library function source code and look where you might speed it up, or shrink it's size, or lower power consumption, or any combination. But do expect to lose a little 'portability' between the various AVR mega chips if that is important to you.The gcc compiler already has the low level read and write to registers notations. This capability has little relationship to if one uses a bootloader or ISP to program the chip, and anyway the Arduino IDE does offer both options to load programs.

author: ez24
author_rank: Super Contributor
timestamp: December 09, 2017, 03:26:34 am
content: Quote from: jgalak on December 08, 2017, 09:23:39 pmDoes anyone remember the last balloonist that posted here? They tracked their balloons from Canada (I think) to over Europe. I lost the link. It was interesting to see the path of the balloons.Thanks

author: jgalak
author_rank: Regular Contributor
timestamp: December 09, 2017, 04:30:27 am
content: Quote from: ez24 on December 09, 2017, 03:26:34 amFriend of mine has launched a micro balloon - two mylar party balloons attached to a tracker - that went all the way around the world. Of course it took him about a dozen attempts before he got that.

author: ez24
author_rank: Super Contributor
timestamp: December 09, 2017, 05:19:23 am
content: Quote from: jgalak on December 09, 2017, 04:30:27 amDid they post maps somewhere?

author: jgalak
author_rank: Regular Contributor
timestamp: December 09, 2017, 03:12:12 pm
content: Quote from: ez24 on December 09, 2017, 05:19:23 amhttps://hojoham.blogspot.com/2016/10/flight-wisp1c12.html

author: Raj
author_rank: Frequent Contributor
timestamp: December 12, 2017, 04:15:02 pm
content: Quote from: Buriedcode on December 08, 2017, 03:53:52 pmYou could always shove done resistors in your parallel port and program using itAbout the main topic, you can usually, pretty easily adapt/reverse engineer the libraries to work with atmel studio.I did actually successfully do it for mlx90614 Library

author: Mr. Scram
author_rank: Super Contributor
timestamp: December 12, 2017, 04:22:47 pm
content: Quote from: Raj on December 12, 2017, 04:15:02 pmAnd you'll learn something too.

author: retrolefty
author_rank: Super Contributor
timestamp: December 12, 2017, 05:20:28 pm
content: Quote from: Raj on December 12, 2017, 04:15:02 pmEasiest ISP programer for working inside the Arduino IDE is simply another arduino AVR board (such as a $3 arduino nano clone board and a 6 pin ribbon connector) loaded with the supplied example arduinoISP sketch. Works fine and there is no simpler ISP programmer solution.

author: phil from seattle
author_rank: Super Contributor
timestamp: December 12, 2017, 06:15:51 pm
content: Quote from: retrolefty on December 12, 2017, 05:20:28 pmTruDat. I've used exactly that combo a number of times. Lots of tutorial-esque pages out there on this topic. I even got a PCB made up to simplify the wiring and include the heartbeat, etc LEDs. If anybody wants one, I can share it on OSHPark. It costs about $8 for 3 boards.

author: Mr. Scram
author_rank: Super Contributor
timestamp: December 12, 2017, 06:21:18 pm
content: Quote from: retrolefty on December 12, 2017, 05:20:28 pmYou can buy a ready made programmer for the same money. Reducing the things that can go wrong is well worth $3. Your cognitive load is probably high enough as it is.

author: rrinker
author_rank: Super Contributor
timestamp: December 12, 2017, 08:49:21 pm
content: Depends, I slapped one together for ATTiny85's using parts that came with the Arduino kits I had - even the shield board. The only thing I have to buy to make one for ATMega328's is a ZIF socket.I have an Uno, a Mega, and a handful of Nanos to experiment and prototype with, but my projects will use raw chips. 328's are very cheap,a s long as you get ones without the bootloader already burned in and do it yourself.

author: Mr. Scram
author_rank: Super Contributor
timestamp: December 12, 2017, 09:04:44 pm
content: Quote from: rrinker on December 12, 2017, 08:49:21 pmEven if you value your time at a terrible rate, it's hard to slap something together for under $3. Unless you want to do it for the sake of doing it, of course.

author: jgalak
author_rank: Regular Contributor
timestamp: December 28, 2017, 09:58:09 pm
content: So I just recently downloaded the latest Arduino IDE and tried opening a .c file written for Atmega processors. I got an error message saying that it could only open files created in the IDE. Is this new? My impression from earlier in the thread was that I could use bare-metal code, and upload to the chip via the Arduino bootloader. If the IDE won't open it, how do I accomplish this?

author: retrolefty
author_rank: Super Contributor
timestamp: December 29, 2017, 12:59:06 am
content: Quote from: jgalak on December 28, 2017, 09:58:09 pmI believe that Arduino IDE sketch source files are type ***.INOYou will have to paste your source code(s) into a new .INO sketch. You will also have to deal withthe built in Main function that the IDE automatically creates for a sketch.

author: westfw
author_rank: Super Contributor
timestamp: December 29, 2017, 09:20:29 am
content: An Arduino "sketch" is a bit like the "project" of most other IDEs, and requires that you have a file name "xxx.ino" in a directory of the same name ("xxx")If you want to write a bare-metal C program, use the IDE to create a "new" sketch, empty it completely, and then use the "new tab" button to add main.c, and put your C code there: