Starting with a samd21 without bootloader (bare chip)

Hi, I did purchase few of these chips, to experiment, after getting an Arduino zero (the specs says that it use a SAMD21G18 I believe).

The problem is that the chips arrived as single chips, so I do not have a breakout board to use, to put the bootloader on it.

How do you actually program chips, when all that you have is the chip itself? Is there a hardware device that allow me to put the bootloader on the chip, via Atmel studio, or the only way is to actually put the chip on a pcb and expose either the USB or UART ports, to communicate with a pc?

This is my first approach at such task; I assumed that the chip was sold with a bootloader already installed on the chip; at factory level; which would save me the hassle to design a PCB which expose either the USB or UART ports.

Also, is there a guide that show how to use the SAMD21 on a PCB, like what kind of capacitors and resistors are needed for each pin? I did check few design, and they are fundamentally different...without understanding why such components are placed there; it is kinda hard for me to design my own.

Thanks!

What good is a chip like that if it's not on a PCB? It's not like you can use a naked chip... Typically people simply include the required connections to program it on the final PCB (which also saves your ass when you softbrick it during development). You'll often see PCBs in commercial products with a small group of mysterious unused pads or holes in the board - these are (in some cases) programming pins.

There exist sockets of some sort for the purpose of programming the chips without a PCB (that's what would be done if you ordered the chip pre-programmed), but they're eye-wateringly expensive for large packages like that.

I don't happen to know what the procedure for programming the SAM chips is. It's not the same as the AVR's.

Agree DrAzzy; the idea was not to leave it off the board; but to program the chip, so I can prototype, since I do not have yet the final version of the PCB (just starting, to be honest).

The idea is to leave the connections to the programming pins on the final board, but for now; I need a way to put code on the chip and start to couple the other parts together.

Thanks for the hint; I was able to find a socket converter, to plug the bare chip on a breadboard; it was not that bad for the QFP 48 version; I think I saw it for 20 dollars on Ebay, without headers.

So the SAM chips do not use a standard AVR with Atmel Studio? I was looking at the one sold by Sparkfun (pocket AVR ISP programmer), or the one from Adafruit (USBtinyISP AVR); I have to double check on the Atmel site, to see what are they suggesting to use.

No - those programmers will not work. Those are for Atmel's AVR line, but their SAM line is not AVR, it's ARM, and it uses a different programming scheme. Atmel Studio does support the SAM line.

The typical solution for that is to solder the part down to a general purpose breakout board (those are available for all the common packages), and work with that.

The usual procedure would be to put in on the “final” PCB with the JTAG/SWD pins broken out to (one of) the standard connector(s), and use a JTAG programmer like Atmel ICE to load code on it. SWD uses 2 pins plus RESET.

IIRC, SAMD21 chips may ship with the SAM-BA bootloader already installed. (http://www.atmel.com/images/atmel-42366-sam-ba-bootloader-for-sam-d21_applicationnote_at07175.pdf) But since the D21 can erase the bootloader, it would not be a good idea to rely on that entirely.

I see; so I need a breakout first.

Just to be clear, if I use a specific Atmel programmer like the ICE, do I need to put a bootloader or can I just put the code on the chip via Atmel Studio? I am confused since my assumption is that a bootloader is needed when you re-write the code often and use it like a zero, while if I program the chip only once, I do not need the bootloader, right?

You can program the flash directly, in which case you will not need a bootloader.

Thanks; will try that too; even if a bootloader is simpler probably.

Does anyone know where I can see an example of how to program the SAMD21?

I do have everything set up and did create a new project, but I can't find anywhere the examples that show how do you write an "hello world" to blink a led for example...I can't figure out what to include, which registers to call (does the D21 use port C and AHB and RCC registers too, to drive the GPIO?).

I did look at some examples for ARM, but they were not for the D21; and Atmel studio has no examples specifically for that chip series.

Does anyone know where I can see an example of how to program the SAMD21?
I do have everything set up and did create a new project, but I can’t find anywhere the examples that show how do you write an “hello world” to blink a led for example…

There’s a SAMD21 Xplained LED Toggle example. It uses ASF, which is Atmel’s set of hardware-obscuring libraries (without being … “simplifying” - I really don’t like ASF.)

I can’t figure out what to include

Normally you include either <sam.h> or <asf.h>, and they’ll pull in the appropriate definitions for the processor configured in the AS project.

which registers to call (does the D21 use port C and AHB and RCC registers too, to drive the GPIO?).

RCC is an STM32 thing, right? “ARM compatibility” stops at the edge of the “core”, so things like clock control and gpio are handled differently from vendor to vendor, and even differently between different families from the same vendor (SAMD from Atmel is significantly different than SAM3 from Atmel. (sigh.))
The SAMD GPIO has its clocks turned on my default, so a minimal blink is very minimal indeed.
Here’s a Blink for SAMD10 (in assembly!); it probably works for SAMD21 as well, though you might need to adjust port and bit…
https://github.com/WestfW/Minimal-ARM/blob/master/Atmel/samd10asmBlink/samd10asmBlink/main.S

My other SAMD10 experiments might be more useful; they duplicate a bit of the Arduino core for the SAMD10 Xplained Mini board:
https://github.com/WestfW/SAMD10-experiments

Thanks a lot westfw!

I did open the example that you mentioned, but it makes no sense to me, since It use LED_0, not the pin name or number; which means that somehwere in the asf.h, which is included in the project, there is some sort of assignment with the pins? I have no idea about ASF; I guess I have to start there.

I will remember to add asf.h and sam.h; hopefully intellisense in atmel studio will be helpful while I browse the content that these headers bring in.

RCC is an STM32 thing, right? “ARM compatibility” stops at the edge of the “core”, so things like clock control and gpio are handled differently from vendor to vendor, and even differently between different families from the same vendor (SAMD from Atmel is significantly different than SAM3 from Atmel. (sigh.))

Yep, there is a very nice ARM tutorial that use STM32; I assumed that being all ARM chips, they would respond to the same set of commands and registers, to do basic IO, but from your reply it seems that it is not the case at all :frowning: Now I understand why people prefer to use a bootloader with Arduino IDE.

Thanks for posting the samples; I will study them, although I know minimal amount of ASM; I usually write code with C++, so it will be interesting to figure out the assembly.

I am astounded that there is no formal documentation about the usage of these microcontrollers; it would not take much to add a simpled document with few examples, that show how to use the chip… is not like any designer will ever use the Xplained board for anything, so sooner or later you have to learn how to drive the bare chip with nothing on it :frowning: There are a ton of documents, but the one missing is probably the one that you would expect :frowning:

I assumed that being all ARM chips, they would respond to the same set of commands and registers, to do basic IO, but from your reply it seems that it is not the case at all :(

Yep. The "core" is the same; interrupts, instruction set, basic bus structure. But the only thing resembling a "peripheral" that is part of the core is the "systick timer." (and having that be mostly standard across multiple vendors IS pretty nice!)

Now I understand why people prefer to use a bootloader with Arduino IDE.

The bootloader is largely irrelevant - it's just a way to get code onto the chip. The Arduino IDE does provide a useful "abstraction" that can be implemented on MANY processors, but that's a result of it being very simplified, and not supporting most of the features of the fancier processors.

I know minimal amount of ASM

Only the minimal blink is in ASM; the "Experiments" project is all in C; be sure to read the wiki pages and follow the references; there's some good stuff in there.

the example that you mentioned, but it makes no sense to me, since It use LED_0, not the pin name or number

ASF attempts to push a particular project structure. "asf.h" ends up including various configuration files, including "board.h" that defines items present on a particular board (similar to "variant.h" in the arduino world.) One of the nice things about using Arduino Studio is that you can right-click on a symbol like "LED_0_PIN" and select "goto implementation" to see where and how it is defined. (and one of the sad things about ASF is how many times you'll have to do that before getting to the actual definition. Some of it is "inline" and nicely optimizes away before the final object code...)

I am astounded that there is no formal documentation about the usage of these microcontrollers

Huh? There's a 1000+ page datasheet that is the primary reference. As it is for AVRs. It's not too bad, as datasheets go. And there's lots of ASF documentation, plus all the source code, plus all the examples (200+ for SAMD21) If there are not "beginner tutorials" suitable for "advanced Arduino users" to branch into "more serious" programming, that's probably because the ARM chips (and boards) have been around for a lot less time.

is not like any designer will ever use the Xplained board for anything

Huh? (again!) Professional developers use eval boards all the time, for learning the basics of a new chip (or even "early development" of their own software, before their hardware is ready.) That's one of the things that these boards are FOR.
Proven hardware with a bunch of working SW examples that you can look at carefully to see how things work.

One of the things unusual about the structure of the Atmel symbol definition is that since the registers for Ports on a device are nicely arranged in the address space, they use "PORT[n]" instead of PORTA/PORTB/etc (even though the datasheet talks about A/B/C.) If you look at my SAMD10 code, that's further simplified because there is only one port (each port is 32bits (not all of which show up on pins.))

The bootloader is largely irrelevant - it's just a way to get code onto the chip. The Arduino IDE does provide a useful "abstraction" that can be implemented on MANY processors, but that's a result of it being very simplified, and not supporting most of the features of the fancier processors.

I guess I did use a poor wording there; my point was that I wish that it was as easy as using Arduino IDE, at least for the basics. With their abstraction, they made a breeze to interact with microcontrollers, and it really help when you move to a different chip. Obviously for more advanced features, you must specialize with one specific model of microcontrollers. It remind me of the old days where you need to learn ASM to write good code (which was mostly the Motorola one or the Intel one).

ASF attempts to push a particular project structure. "asf.h" ends up including various configuration files, including "board.h" that defines items present on a particular board (similar to "variant.h" in the arduino world.) One of the nice things about using Arduino Studio is that you can right-click on a symbol like "LED_0_PIN" and select "goto implementation" to see where and how it is defined. (and one of the sad things about ASF is how many times you'll have to do that before getting to the actual definition. Some of it is "inline" and nicely optimizes away before the final object code...)

I miss the Arduino way: you just need to know which pin, based on the pre-made list of microcontrollers included in the IDE.

From your experience, would you say that it would be convenient to use the "import arduino project", in Atmel Studio, since that would automatically assign everything for me? I would like to learn but the lack of tutorials that are generic enough; is giving me hard time.

Huh? There's a 1000+ page datasheet that is the primary reference. As it is for AVRs. It's not too bad, as datasheets go. And there's lots of ASF documentation, plus all the source code, plus all the examples (200+ for SAMD21)

Yep, but I do not recall examples for the specific chip, that show how to do anything. I did find a lot of explanations about the inner workings; which are good if you did work in the past with a similar microcontoller; but if you are a total newbie, it is not really walking you through the basics.

Not blamig Atmel; the fault is on my side; but they do not put any effort in trying to address also to people that are totally new to the subject. This is probably the greatest achievement that made Arduino, the worldwide platform that it is now.

Huh? (again!) Professional developers use eval boards all the time, for learning the basics of a new chip (or even "early development" of their own software, before their hardware is ready.) That's one of the things that these boards are FOR.

I see; so the issue is my lack of understanding, because from what I get from your comment, I assume that the code written for the example shall work 1:1 on a bare chip (assuming that you put buttons and LED in the same place where they are addressed in the eval board).

I did start with Arduino, so probably my lack of knowledge is to blame. As long as a chip support the Arduino IDE, it just work with the same code.

I did look at the examples coming with Atmel studio and I did not have any luck in having them working with my bare chip; most likely because I need to learn where to look for pin assignments probably. Time to get some generic tutorials and see if I can get something out of these :slight_smile: