Author Topic: Want to program Atmel AVR MCUs directly, where do I start?  (Read 11283 times)

0 Members and 1 Guest are viewing this topic.

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
I'm aware of different ways to load Arduino on an Atmega, have not tried it yet and probably will (have some on the way) but ultimately, I'd like to learn how to program them directly in their native language (C I think?  or C++?  I'm hoping C++).  From the reading I did, I need to buy a programmer, such as a USBtinyISP and connect it to the proper pins of the MCU as per instructions and USB into the computer.  There are ways to use a parallel or serial port but rather not rely on that as it's harder and harder to find computers that have those ports.

Then a program like avr-gcc is used to compile the code into a binary, then avrdude is used to load it on the chip.  Am I on the right path here?

One thing I have not found though is actual programming tutorials. Like, showing how the framework works.  How do you set pin parameters, general syntax, that kind of thing.   Just looking for a good resource to get started.

Also, I'm open to other MCU platforms too, but ultimately I want to use a platform that will be easy to expand on and can use in actual projects that I may potentially decide to market at some point, so it also has to be a platform that would allow me to do that (Ex: no need to buy expensive license or anything of that sort).  I know there's PIC too, have not read up much on those yet.
 

Offline Esposch T. Tapir

  • Contributor
  • Posts: 30
  • Country: au
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #1 on: May 12, 2015, 12:29:03 am »
I'm aware of different ways to load Arduino on an Atmega, have not tried it yet and probably will (have some on the way) but ultimately, I'd like to learn how to program them directly in their native language (C I think?  or C++?  I'm hoping C++).  From the reading I did, I need to buy a programmer, such as a USBtinyISP and connect it to the proper pins of the MCU as per instructions and USB into the computer.  There are ways to use a parallel or serial port but rather not rely on that as it's harder and harder to find computers that have those ports.

Then a program like avr-gcc is used to compile the code into a binary, then avrdude is used to load it on the chip.  Am I on the right path here?

One thing I have not found though is actual programming tutorials. Like, showing how the framework works.  How do you set pin parameters, general syntax, that kind of thing.   Just looking for a good resource to get started.

Also, I'm open to other MCU platforms too, but ultimately I want to use a platform that will be easy to expand on and can use in actual projects that I may potentially decide to market at some point, so it also has to be a platform that would allow me to do that (Ex: no need to buy expensive license or anything of that sort).  I know there's PIC too, have not read up much on those yet.

Hey!  I'm assuming you're another beginner like me!
Most microcontrollers are programmed in C, although some do C++ (I think AVR does?  I'm not sure.  Either way, most programmers use C because it's more intuitive for the types of things you'll be doing with an MCU).   If you've worked with Arduino before, you're in luck - because the Arduino language is essentially C with a couple of libraries added on.

Regarding compiling and all, the manufacturer will provide their own IDE.  For AVR It's Atmel Studio.  Go into that, hit compile then load and everything should be handled for you.  At least usually.  I have an XMEGA Xplained-A3BU board, which uses a bootloader (USB).  So when I compile the binary in Atmel Studio I have to load it in a piece of software called FLIP and flash the board through that.

Hardware wise, the best way to do it is by getting an existing dev board.  The one I have is fantastic, but a little expensive (was AUD$40).

Pin parameters, general syntax etc. should be given in tutorials and the datasheet.  For AVR, they're sorted into "ports" of length 8 bit.  Each port is a struct with a byte for pin direction, output data, input data, output configuration, interrupts etc.  All explained under the "I/O ports" section of the manual.

If you want to save a bit of cash but still get acquainted with a "proper" MCU architecture, I strongly recommend Cypress's PSoC.
Fantastic support, the best (free!) IDE I've ever used (it has this circuit layout thing that's fantastic), quite simple to use, some of the most versatile hardware on the market (It actually has a tiny little CPLD-esque thing on chip!), the dev boards cost as little as US$4 and come with a USB programmer.  A little bit easier than AVR, too.

PSoC 4 chips are relatively cheap if you plan to go to market - about as much as a PIC32MX or XMEGA A4U, however they don't do USB.

And a single comment on PIC, because I've not used them much: the official starter kits are absolutely woeful.

No matter which way you go, though, good on you for getting away from Arduino.
There are so many far, far better options out there.

EDIT: One other thing!  If you're going to get a PSoC4 board, get the 4200.  The 4100 has no programmable digital and the 4000 lacks an ADC and DAC as well (or something similar - either way the 4100 and 4000 are only for people who plan to develop commercial applications on lower end chips). 
« Last Edit: May 12, 2015, 12:32:54 am by Esposch T. Tapir »
Bachelor of Engineering (ECSE) with Honours.
Every time I write a line of code or build a circuit I am reminded that, in the grand scheme of things, I know bugger all.
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #2 on: May 12, 2015, 12:34:55 am »
I forgot to mention I'm on Linux, so I want to use open source methods.  I would guess at programs like Atmel Studio are probably Windows only right? If it uses straight C, does it also mean I can't use classes?  Ex: say I wrote a C++ program and I just want to adapt it on a MCU, I can't just take it as is with classes, but would have to rewrite using just functions right?  What about stuff like the STL, is it typically also used, or do they tend to use their own frameworks?  Obviously some commands like cout wont really work since a MCU has no concept of a screen unless I explicitly code such thing into it and use something such as a LCD.

That's another thing too I'm not stuck on atmel, I'm open to any platform that will be easy to work with in Linux.   Though.. I kinda did jump the gun and ordered 10 Atmega328Ps so hoping to stick with that platform for now. :P
« Last Edit: May 12, 2015, 12:39:09 am by Red Squirrel »
 

Offline JoeN

  • Frequent Contributor
  • **
  • Posts: 991
  • Country: us
  • We Buy Trannies By The Truckload
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #3 on: May 12, 2015, 12:44:33 am »
FYI:  The only "native language" any processor has are the binary codes that it responds to which make the processor change its internal state, which is to say, do work.  This native language is called machine code.  Nobody writes machine code because it is a waste of time to do so and very accident prone.  A close cousin is assembly language which is just the machine code made slightly more human readable and writable and with addresses represented by labels which are calculated down to proper addresses when the assembly is done.  This is a huge benefit for the human and creates no performance problems in terms of the machine code generated.  All other languages, C included, are equal in the sense that they are not the processor's native language.  They are just another type of code which gets translated to that final machine code before the processor is programmed.  But C is very popular and makes efficient use of machine resources.  All of these higher level languages have inherent problems with the efficiency of the machine code generated compared to the machine code generated from a program in assembly language written by a skilled programmer.  However, C probably suffers the least of the higher level languages because the designers purposefully kept it very close to the hardware level and the core language is not abstracted very far out from that level.

Practical answer:  Atlem's free compiler supports both C and C++ and has a lot of libraries to help you write code to use the specific peripherals on those chips.  I think you will be happy with their free compiler.  I guess this is the recommended programmer these days:  http://www.atmel.com/tools/atatmel-ice.aspx   I ddin't see your no-Windows requirement.  Atmel Studio is a Windows application.  The core compiler without the IDE can be run on Linux and I know there are Linux drivers for the programmers.  I am not super familiar with that, someone else can give you pointers I bet.
« Last Edit: May 12, 2015, 01:38:24 am by JoeN »
Have You Been Triggered Today?
 

Offline Esposch T. Tapir

  • Contributor
  • Posts: 30
  • Country: au
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #4 on: May 12, 2015, 12:50:01 am »
Not sure which ones do Linux, but I believe Atmel Studio is Windows only since it uses Visual Studio.
Straight C means no classes, and I don't even know what an STL is.  Even though I know AVR has some C++ compilers, I honestly think learning C would be a useful skill.  It shouldn't be hard if you already know C++, and it lets you know exactly what your micro is doing.  The types of tasks that people typically use micros for are typically make more sense when written in C.  They're much more sequential than PC type software.  They do less but need to do it in a tight loop.

cout doesn't work, but you can make a printf()-esque function by using a sprintf() to a temporary variable then writing the variable to LCD screens or UARTs.

I'd honestly recommend cutting your losses and switching platforms if you bought bare 328P chips.  XMEGA, PSoC, PIC32, STMF3 are far better platforms and the chips are cheaper too.  The only advantage 328P has is Arduino compatibility.  Look into PSoC.
Bachelor of Engineering (ECSE) with Honours.
Every time I write a line of code or build a circuit I am reminded that, in the grand scheme of things, I know bugger all.
 

Offline ralphd

  • Frequent Contributor
  • **
  • Posts: 445
  • Country: ca
    • Nerd Ralph
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #5 on: May 12, 2015, 01:09:19 am »
I wrote a blog post when I got started programming AVRs.
http://nerdralph.blogspot.ca/2013/08/getting-started-with-arduino-avr.html
Unthinking respect for authority is the greatest enemy of truth. Einstein
 

Offline Len

  • Frequent Contributor
  • **
  • Posts: 547
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #6 on: May 12, 2015, 02:00:44 am »
I started out with Arduino and it was really easy to switch to C/C++ because the Arduino compiler is based on avr-gcc. When you install the Arduino software you also get the C compiler and related tools (make, avrdude, etc). You can even mix the two by using avr-gcc macros in an Arduino program to fiddle with the MCU control registers.
DIY Eurorack Synth: https://lenp.net/synth/
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #7 on: May 12, 2015, 02:02:41 am »
Actually before I jump the gun on any platform, is there a good overview somewhere of the various platforms with pros and cons?  Idealy I want to pick a platform that has a Linux friendly workflow (either officially or through 3rd party tools) and has a large variety of different chips that follow the same work flow.  I figured Atmel was actually cheaper considering it was a few bucks per chip, though, but honestly I did not look very hard at other platforms, not being aware of what else there is.  I heard of PIC and that's about all.

I'm fairly comfortable with programming so once I figure out the work flow the rest should be simple enough as long as I can find the proper resources on MCU specific code I need to know (like what function/command I need to run to do something like set a pin option or value or read a value or use i2c etc...)
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #8 on: May 12, 2015, 04:22:22 am »
Red Squirrel, Linux and AVR do fit well. Do a search for 'avr gcc linux' or 'avr freaks linux' and you will find many resources and examples.

Another option is to install the Arduino IDE. It will also install the gcc tool chain, avrdude programmer, etc, so you can use it from the IDE or from the command line.  Arduino BTW gives you access to full blown C/C++ programming and it's up to you what you do by calling libraries and what you do by direct register manipulation.  This is the easiest approach and it doesn't restrict you if you want also to write low level stuff.

If you want a more modern architecture you may want to look into an ARM chips. You get more bang for the buck and a simpler memory model (e.g. with AVR, string literals such as "xyz" also consume by default precious RAM space and are accessed differently than ROM only strings. Yack).

I have had very good results with NXP LPC11U35. It's a small low cost MCU with USB support that can be programed using file drag and drop (it mounts itself as a virtual disk) and is supported by a free LPCXpresso/Eclipse IDE and a $20 optional hardware debugger (if you want breakpoints and single stepping). LPCXpresso uses gcc and standard make files so if you prefer to type 'make all' in the command line it will work as well. After you will master this MCU,  making your own pcbs with that MCU is simple and require only minimal glue components.

https://github.com/zapta/arm/tree/master/pro-mini

If you you want off the shelf Arduino like libraries you can find tons of them on mbed.org, together with source code. mbed.org can also create your initial project that you then export to your toolcahin of choice (e.g. LPCXpresso).

Other ARM vendors that you may want to check for tool chains and Linux support are ST, TI and Freescale. 
 

Offline Farley

  • Regular Contributor
  • *
  • Posts: 88
  • Country: us
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #9 on: May 12, 2015, 04:39:09 am »
A couple years ago I used Eclipse running in Ubuntu Linux to develop for AVRs. I found it worked well. It can be a bit of a chore to get it all running. However, I think the entire process has been improved since then.

These links (in no particular order) can get you started.

http://www.instructables.com/id/How-to-get-started-with-Eclipse-and-AVR/?ALLSTEPS
http://avr-eclipse.sourceforge.net/wiki/index.php/The_AVR_Eclipse_Plugin
http://avr-eclipse.sourceforge.net/wiki/index.php/The_AVR_GCC_Toolchain

You will also need a programmer. I have the Atmel ISP MKII but I don't think it's available anymore. It's the only programmer I have experience with but there are apparently several clones out there that may work.

 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4196
  • Country: us
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #10 on: May 12, 2015, 08:30:56 am »
Well, you know, you can stick with the Arduino IDE and bootloader, and write "bare metal" programs just fine.  Just provide main() instead of loop() and setup(), and ignore all of the nice friendly library functions that arduino provides.  This would allow you to program AVRs, assorted ARM chips, TI MSP430 chips (using Energia) and Microchip PIC32 chips (using MPIDE), using basically the same UI.

The Arduino IDE is pretty sucky (primitive editor, no debugging), but is wonderfully easy to install, and not too awful when coupled with an external editor.
The alternative is probably the vendor-provided tools, which means Visual Studio based for AVR (Atmel Studio), Netbeans for PIC (MPLAB X), and probably Eclipse (TI CCS) for the ARM and TI chips.  Or you can set up a CLI-based environment.

Eclipse is probably the closest thing to a linux environment that supports multiple architectures, but you'll have to do a fair amount of research and effort for each chip that you want to try to use.

 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11534
  • Country: my
  • reassessing directives...
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #11 on: May 12, 2015, 11:23:10 am »
Well, you know, you can stick with the Arduino IDE and bootloader, and write "bare metal" programs just fine...The Arduino IDE is pretty sucky (primitive editor, no debugging)
not just that, the real kicker is there is a constant time overhead during compile time. compiling a very simple app takes ages. changing one letter and recompile takes the same time of overhead cant they reuse unchanged obj files? :palm: i cant imagine how one can come up with a descent complexity of an app in Arduino IDE, maybe they debug it with brain, pencil and lot of papers :palm: fwiw, i program avr/arduino board in AVR Studio, bare metal c/c++. hex uploaded using avrisp mk2 for bare metal chip and avrdude for arduino board.
« Last Edit: May 12, 2015, 11:25:23 am by Mechatrommer »
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline con-f-use

  • Supporter
  • ****
  • Posts: 807
  • Country: at
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #12 on: May 12, 2015, 11:23:34 am »
Not having read anything before my post:

For just programming (as opposed to debugging) any uTiny will do fine. I can recommend the uISP seeing as a beginner, you're bound to screw up fuse bits.

Using it on Linux is incredibly easy. You plug it in,* compile your C-Code with avr-gcc and use avrdude to flash the program onto the controller. See Ladyada AVRTutorial. Here is an example workflow:

Code: [Select]
sudo apt-get install gcc-avr avrdude avr-libc # NEEDED ONLY FOR INSTALLATION
avr-gcc -Wall -Os -mmcu=attiny26 -c demo.c -o demo.o
avr-gcc -Wall -Os -mmcu=attiny26 -c demo_help.c -o demo_help.o
avr-gcc -mmcu=attiny26 -o demo.elf demo.o demo_help.o
avr-objcopy -j .text -j .data -O ihex demo.elf demo.hex
# Be very VERY CAREFUL the following step:
avrdude -c usbtiny -p attiny26 -U hfuse:w:0x17:m -U lfuse:w:0x71:m  # MIGHT NOT BE NEEDED
avrdude -c usbtiny -p attiny26 -U flash:w:demo.hex:i

Here the program consists of two source files 'demo.c' and 'demo_help.c'. There is tons of good tutorials on programming AVRs and flashing them. Just use Google.

*On some Linux distributions you don't have access to the virtual serial port of your programmer in e.g. '/dev/usbTiny01'. To mitigate  this, you can either run avrdude with super-user rights (stupid), add yourself to the appropriate group with access or write a custom udev-rule.

« Last Edit: May 12, 2015, 11:32:01 am by con-f-use »
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #13 on: May 12, 2015, 02:39:14 pm »
Thanks for all the info.  I'll do some further reading, think I'll stick with AVR as it sounds rather Linux friendly given the tools like avr-gcc / avrdude.  I prefer just using a text editor as well and making scripts for compiling etc anyway so no need for IDEs.

Oh and if I want to get into ARM stuff is it the same process or is that a totally different workflow?
« Last Edit: May 12, 2015, 02:40:53 pm by Red Squirrel »
 

Offline JackP

  • Regular Contributor
  • *
  • Posts: 185
  • Country: gb
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #14 on: May 12, 2015, 02:44:47 pm »
I thought C++ (or C plus one in C notation) was just C with classes added (the classes are the one thing added).
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #15 on: May 12, 2015, 03:38:17 pm »
That's basically it, and the way memory management is done (using new/delete instead of malloc for example)
 

Offline Brutte

  • Frequent Contributor
  • **
  • Posts: 614
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #16 on: May 12, 2015, 07:03:26 pm »
You can develop for AVR8's with Eclipse of course.

Eclipse does support whole avr-gcc toolchain, with gdb, avarice, simulavr, avrdude etc. It also supports nice IO view to some extent (via embsysreg plug-in), flashing memory, boot and lock bits. The serious problem with that architecture is that all AVR8 debugging tools are proprietary and there are not too many hackers willing to sniff and reverse-engineer Atmel's classified protocols.

So my advice - do not support their proprietary attitude and let AVR8's RIP.
Alternatively if you are an avrfreak and cannot fall asleep without atmega16 under the pillow, you can build your own JTAG clone, play with it for a weekend and after that scrap it and let AVR8's RIP.

Do not waste your time with AVR8's, go for ARMv7M (a.k.a "ARM Cortex M3") which are fully supported by gazillions of toolchains, IDEs and vendors.
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #17 on: May 12, 2015, 08:43:39 pm »
Arm does sound interesting too, but isin't that overkill for stuff that could be done by something like an atmega?  I imagine they use more power and cost more, too.    Though if it's more open source friendly I'll find more resources so there's that.

I could also do way more with them such as something that can run Linux directly, that could be fun.  Basically my own Pi and put whatever other peripherals I want like ethernet.   I'll have to do some research.
« Last Edit: May 12, 2015, 09:05:42 pm by Red Squirrel »
 

Offline JackP

  • Regular Contributor
  • *
  • Posts: 185
  • Country: gb
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #18 on: May 12, 2015, 09:15:17 pm »
ARM can be cheaper, some are low power. Their is a jump in complexity (Im still getting my head around register access) but being able to turn on the many peripherals when needed saves power. Before using a library, I recommend a simple Led blinking exercise setting up the beast on your own. Follow a tutorial on YouTube.
Disclaimer: I'm a newbie, just sharing my experience
 

Offline Brutte

  • Frequent Contributor
  • **
  • Posts: 614
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #19 on: May 12, 2015, 09:31:37 pm »
Arm does sound interesting too, but isin't that overkill for stuff that could be done by something like an atmega?

You talk like a 60-yo (no offence if you are younger but also do not treat that as a complement if you are older than that).

Quote
I imagine they use more power
I doubt if that is the rule. Also for battery operation in sleep mode that is not the case. IIRC my AVRButterfly used 15uA while my STM32L-Discovery uses around 12uA (LCD+RTC running).

Quote
and cost more, too.

Visit some distributor, like digikey.com and compare (MOQ = 1 ) cheapest reasonable (>32k) ARMv7M with cheapest reasonable ATMega (or cheapest ATXmega).
Any conclusions?

You can also find some cheaper tinies in ARM world (called Cortex M0) but these are PITA for learning because of limited resources and crappy debugging support.


Quote
I could also do way more with them such as something that can run Linux directly
That would be hard if not impossible to run linux on ARMv7M or on AVR8, although I have heard there are some crazy freaks that succeeded.

Quote
Basically my own Pi
Pi is not ARMv7M but either ARM11 or ARMv7A (depending on the revision).

You know, Saab made fighter jets and passenger cars and you clearly do not see the difference  :palm:
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #20 on: May 12, 2015, 10:56:37 pm »
My bad then, I just figured a "computer" cpu architecture that is used in phones and tablets and small computers would be way more powerful/expensive than micro controller architecture and overkill to drive small stuff like sensors or leds or w/e. But yeah it seems there are some that are actually cheaper and smaller, and overall there is quite a wide selection from doing a quick check on digikey.

I'm just trying to learn, no need to insult me.

I think I'll stick to AVR for now though given I bought those chips, can always move to arm later once I feel the need to expand more.
« Last Edit: May 12, 2015, 11:00:24 pm by Red Squirrel »
 

Offline varesa

  • Contributor
  • Posts: 26
  • Country: fi
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #21 on: May 13, 2015, 04:42:04 pm »
My bad then, I just figured a "computer" cpu architecture that is used in phones and tablets and small computers would be way more powerful/expensive than micro controller architecture and overkill to drive small stuff like sensors or leds or w/e. But yeah it seems there are some that are actually cheaper and smaller, and overall there is quite a wide selection from doing a quick check on digikey.

There is a big difference between ARM Cortex and ARM Cortex-M chips.

ARM Cortex are the CPUs you find in something like a Raspberry pi. You can get for example a quad core multi-gigahertz one that runs linux/windows/etc.
ARM Cortex-M are the microcontrollers similar to e.g. atmegas or pics, though still a bit more powerful. Many of these operate around 32-200MHz max.


I use STM32 chips on linux. If you want a fancy IDE eclipse takes a bit of work to set up but after that works nicely with support for debugging etc.
You can also use the tools like gcc, gdb and make on the command line if you're used to that. There almost no setup required.
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #22 on: May 13, 2015, 06:37:36 pm »
Ah ok, don't really know much about Arm series so did not know that.  I'm more of a text editor kind of person so using the command line compiler will work fine for me.
 

Offline varesa

  • Contributor
  • Posts: 26
  • Country: fi
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #23 on: May 13, 2015, 07:19:26 pm »
If you decide to try ARM out here are a few pointers to get started on linux:

GCC compiler by ARM:
https://launchpad.net/gcc-arm-embedded
Quote
As part of its ongoing commitment to maintaining and enhancing GCC compiler support for the ARM architecture, ARM is maintaining a GNU toolchain with a GCC source branch targeted at Embedded ARM Processors, namely Cortex-R/Cortex-M processor families, covering Cortex-M0, Cortex-M3, Cortex-M4, Cortex-M0+, Cortex-M7, Cortex-R4, Cortex-R5 and Cortex-R7. As part of this, ARM will, at regular intervals, release binaries pre-built and tested from the ARM embedded branch. The improvements will be freely available for integration into 3rd party toolchains, and for direct download by end-users.

Your distro repositories might include it already.


On-Chip Debugger:
http://openocd.org/
Quote
The Open On-Chip Debugger (OpenOCD) aims to provide debugging, in-system programming and boundary-scan testing for embedded target devices.
Also can be found in repos. For example supports the intergrated st-link v2 that is found on the cheap STM32Discovery development boards.
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #24 on: June 18, 2015, 05:32:20 am »
I finally got my programmer from Amazon so as a first basic test today I loaded an arduino project directly on a chip.  I was having trouble when I realized I had to connect both VCC/ground pins as I had originally assumed you did not have to.   Just a basic led blinker, I made a few changes to the code to change the blink sequence, just to show that the process works.    Considering the simplicity of this I may actually just stick to arduino for now as it's much easier to find code examples and there are a lot of libraries I can get too.  I still can't seem to find much as far as verbose examples for coding directly to the chips, only little bits and pieces here.  The datasheet alone does not really help the actual code, though it does have some basic examples.   For example, how I do pwm, SPI, i2c, read from the ADC etc. Have not been able to find much on that.

I'm thinking maybe I need a good book, any recommendations?  Though I may also just stick to arduino for now, once I can find a command line way of doing it even better as I wont even need to use the GUI.  For bigger projects I will want to use my tabbed text editor anyway and split stuff in seperate header files and what not.
 

Offline amyk

  • Super Contributor
  • ***
  • Posts: 8240
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #25 on: June 18, 2015, 05:34:16 am »
You could start with this: :)
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4196
  • Country: us
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #26 on: June 18, 2015, 06:57:36 pm »
Printed Books seem to have trouble keeping up with the rate of change that happens in the Arduino Community.
Ordinarily, I'd recommend a book like https://store-jwm4nhbo.mybigcommerce.com/an-arduino-workshop-book/ based largely on the author's contributions over at AVRFreaks, but... it seems to be based on pre-Uno Arduinos and pre-1.0 IDE versions :-(  Uno is 5y old at this point, but even the last six months have seen substantial changes, and print is slow.   I guess this is a general problem with high tech, and even more so with open source designs. :-(
 

Offline Red SquirrelTopic starter

  • Super Contributor
  • ***
  • Posts: 2748
  • Country: ca
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #27 on: June 20, 2015, 09:35:41 pm »
Arduino is no issue there's tons of resources online on how to do stuff.  Was talking more about if I do move on to coding them directly.  Learning all the functions, built in variables etc...  Most tutorials I found just touch on very basics and that's it. 

Though using arduino but with the programmer seems to be the best of both worlds, as I'm still putting a program on the chip directly without needing an arduino board  but with access to all the arduino code base which has more online documentation available.
 

Offline Quarlo Klobrigney

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #28 on: August 20, 2015, 11:39:30 pm »
http://www.avrfreaks.net/forums/tutorials Be warned the search box is a bit wonky.

Install Studio 4.1x not 6 and AVRGCC
You can program and simulate in Studio in either assembly or C

Also youtube is your friend
Voltage does not flow, nor does voltage go.
 

Offline bufo333

  • Newbie
  • Posts: 2
  • Country: us
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #29 on: August 21, 2015, 10:22:17 am »
I recently wanted to learn to program AVR MCU's using the data sheet directly and not use any Arduino code. I purchases the following book which has been excellent http://www.amazon.com/Make-Programming-Learning-Software-Hardware/dp/1449355781/ref=sr_1_5?ie=UTF8&qid=1440151544&sr=8-5&keywords=avr. This book takes you through the process of setting up the gcc toolchain and your c Makefile manually on Windows, Linux, and Mac. It takes your through the atmegaxx8p hardware one peripheral at a time and shows you how to manipulate registers directly.

Oh and did I mention you can use notepad/VI/Emacs/textedit/.... plain old text editors to write your c code and run make install from the command line to read your make file, compile and link the code and upload to the Arduino using an ICSP programmer

Some of the benefits to this are:
  • More efficient code (setting a pin output high takes 40 cycles in Arduino vs 1 cycle setting the register directly)
  • The ability to read and set multiple pins at once
  • More readable code once you get used to the register names / shorter code
  • The ability to configure counters/timers how you want and not how Arduino wants
  • Better ability to control the peripherals directly such as (using interrupts for ADC results, doing hardware serial/SPI/I2c (reduced code and cycles * for example: lots of Arduino code especially Adafruit modules states they don't support hardware serial yet*
  • More complete understanding of the code, and improved programming skill as everything is a matter of bit shifting or masking


And finally I will leave you with blink code for an attiny85 that uses the watchdog timer to blink an led every second and sleep %99 of the time. this chip uses microamps of power and has a battery life of years compared to most Arduino projects. And you can look up what is actually happening by reading the data sheet as all  the registers are defined there.

Also note most boot loaders such as the one used on the Adafruit Gemma/Trinket and other Arduino boot loaders do not support the use of this watchdog timer.

Code: [Select]
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>

volatile int f_wdt=1;

ISR(WDT_vect)
{
if (f_wdt == 0)
    {
f_wdt=1;
    }
}


void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_disable();
power_all_enable();
}


int main(void)
{
//Disable interrupts
cli();

// Configure ports 0 & 1 for output
DDRB = ((1<< DDB1) | (1<< DDB0));

// Configure initial state of pin 1 to HIGH
PORTB= 1 << DDB1;

// Set Watchdog Reset flag to 0
MCUSR &= ~(1<< WDRF);
WDTCR = 0x00;

// set watchdog enable and change enable bits to HIGH
WDTCR |= (1<< WDCE) | (1<< WDE);

// Enable Watchdog timer interrupt
WDTCR |= 1<<  WDIE;

// set watchdog prescaler values to 1.0 seconds
WDTCR |= (1<< WDP2) | (1<< WDP1);

// Enable Interrupts
sei();

for(;;) {
  // put your main code here, to run repeatedly:
if (f_wdt==1)
{
PORTB ^= ( (1 << PORTB1) | (1 << PORTB0));
f_wdt = 0;
enterSleep();
 }
}
return 0;
}


« Last Edit: August 21, 2015, 10:24:58 am by bufo333 »
 

Online bingo600

  • Super Contributor
  • ***
  • Posts: 1976
  • Country: dk
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #30 on: August 21, 2015, 01:25:36 pm »
For avrdude and fuses , i usually use this site

http://www.engbedded.com/fusecalc

It also gives the avrdude "hex fuse values"


/Bingo
 

Offline Quarlo Klobrigney

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: Want to program Atmel AVR MCUs directly, where do I start?
« Reply #31 on: August 22, 2015, 01:33:09 pm »
Programmers Notepad is a good editor to use. It recognizes the file format and is also able to color the text accordingly. If printing it will output formatted color text. Try that in notepad. If coupled with tools it can make as well. Google is your friend.
Voltage does not flow, nor does voltage go.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf