Arduino Due in Microchip Studio Part 1

Detailing my bumbling attempts to get everything working without the complexity of learning ASF. To follow this guide, you’ll need: A Due (obviously), Microchip Studio (formerly known as Atmel Studio, but it’s the same thing) which is free, and an Atmel-ICE Debugger and Programmer. The last item is expensive, but there are probably cheaper alternatives? Let me know in the comments. I know you can use the regular Arduino USB port by setting up a Studio “External Tool” to use bossac.exe – but it’s not as seamless and you won’t get the benefits of “proper” debugging, with breakpoints, watchlists, and such. This is one of the main reasons for wanting to use Studio, rather than Arduino IDE, in the first place.

Connecting it up

If your Due already has the mini-JTAG connector – small male pins in a 5×2 grid spaced at 1.27mm (0.05-inch), then you can use that: connect the small ribbon cable between the Atmel-ICE SAM port (connector is keyed so it will only plug in the correct way round) to the DUE’s JTAG connector with the pin-1 end of the cable (red) nearest to the DUE’s barrel-jack power connector.

If you bought a cheap clone DUE, then the JTAG connector may not be fitted. You can buy a connector and solder it in, or you can connect to the row of four normally-spaced (0.1-inch) male pins close by the JTAG holes (near the Due’s “pins” labelled 11, 12, 13). Depending on which clone you bought, the connector may be labelled DEBUG or DBUG. Easiest way, using the leads that came with the Atmel-ICE, is to use the ‘squid’ cable which has separate labelled female connectors at one end (pin 10 has the label ‘0’). If, like me, you’ve already dedicated that cable to connecting up your Arduino Mega 2560 in JTAG debug mode, then an alternative is to link from the Atmel-ICE SAM port to the supplied, odd-shaped, multi connector board using the ribbon cable, and then four male-to-female patch wires connecting the Due’s DEBUG pins to the multi-connector AVR JTAG port as follows:

1 – GND3 – GND
3 – TMS2 – TMS (SWDIO)
4 – RST10 – nSRST
(Due’s 3.3 V pin or IOREF)1 – VTG

Pin 1 on the DEBUG connector is the one farthest away from the barrel-jack-and-USB-port end of the Due. The last, 3.3V connection, in the table above, allows the Atmel-ICE to sense and measure the Due’s working voltage, and is required for the Atmel-ICE to work. You can also use the Due’s IOREF pin – close to its 3.3V one – on the Due, IOREF and 3.3V are connected.

Whether you connected by ribbon cable to the JTAG connector, or to the DEBUG connector, the Atmel-ICE won’t power the Due: you’ll still need to connect the Due to a power source via its barrel jack or one of its USB leads. I recommend using a USB lead from your PC to the Due’s programming port – that will power the Due, and also allow easy use of the serial port for your programs to display stuff.

The photo shows my TAIJI-UINO Due. It’s pretty much the same as a standard Due, with the following differences:

  • Programming port USB has been moved off board onto a separate adapter board (but with the adapter board fitted it’s functionally the same).
  • Extra pins unavailable on a standard Due bought out to a header where an Ethernet adapter board can be fitted.
  • LED on “pin 13” omitted.

Installing Microchip Studio

Just download it and install with the defaults. Don’t bother to install the ASF (Advanced Software Framework) – you can always add it later – but if you have already installed it, it doesn’t matter.

First program – minimal blink

As a change from blinking the LED on “pin 13”, let’s make the TX LED next to the Due’s native USB port blink. This LED isn’t actually connected to any special USB pins or hardware – it’s just a LED connected via a resistor to one of the ATSAM3X8E’s GPIO pins. We look at the Arduino Due Schematic and see the LED is connected to PA21. If you’d rather make the usual “pin 13” blink then study the schematic to find that “pin 13” is PB27, and make the obvious changes.

Fire up your Microchip (or Atmel) Studio, select New Project. In the resulting New Project window, click on GCC C++ Executable Project. In the Name text box at the bottom, give it a name like DueTxBlinky. Click on OK.

In the resulting Device Selection window, you need to choose ATSAM3X8E. Quickest way is to start typing that in, in the search text box. Click on ATSAM3X8E when it appears in the left hand pane, and click OK. After thinking about it for a while, Microchip Studio should present you with this ready-made main.cpp window.

If you’re coming from the Arduino IDE, just remember that any code you’d normally put inside your setup() function goes immediately after the SytemInit(); and any code you’d normally put inside your loop() goes in the while (1) loop.

But first, let’s make sure that our programmer/debugger is connected to the Due, so we’re ready to run. Click on the Project menu up at the top and click on DueTxBlinky properties… (or whatever you named it). Now you get an extra tab next to your main.cpp* one. In the left hand side “menu” of that tab, click on Tool. Now use the Selected debugger/programmer drop-down to select your Atmel-ICE (or other debug tool you may be using). In the Interface drop-down select SWD for now. Leave everything else alone. Now go to the Tools menu at the TOP of the screen, and click on Device Programming. You should see this:

Now click the Apply button, leave the resulting SWD Clock slider where it and click the Set button, then click the Read button next to Device signature. If you see a Device signature (unique to each Due, I think) and voltage, then congratulations! You’re ready to program!

Now you can close the Device Programming window, and the DueTxBlinky (or whatever you named it) tab, so you’re back to seeing your main.cpp. It’s probably a good idea to save your project now – choose Save All from the File menu, or click on the little “pair of floppy disks” icon up in the toolbar.

Now the changes to main.cpp to actually make the LED blink. First, we need to disable the watchdog. This is something built into the chip and initialized by the SystemInit() code – if we don’t do anything with the watchdog, it will reset everything about eighteen seconds after our program starts. For now we can just switch it off by putting this line after the SystemInit();


Now we want the equivalent of the Arduino IDE’s pinMode(pin, OUTPUT); which looks like this for our chosen pin, PA21:

    PIOA->PIO_PER |= (1 << 21); // enable IO
    PIOA->PIO_OER |= (1 << 21); // set as output
    PIOA->PIO_PUDR |= (1 << 21); // disable internal pull-up
    PIOA->PIO_OWER |= (1 << 21); // enable output write

Okay, it seems a bit complicated, but don’t worry, we’ll be hiding all that stuff away in a function for part 2.

Now, inside the while (1) loop we want to the equivalents of digitalWrite(pin, HIGH); and digitalWrite(pin, LOW); with some delays in-between which looks like this:

while (1) 
    PIOA->PIO_ODSR |= (1 << 21); // make pin HIGH
    PIOA->PIO_ODSR &= ~(1 << 21); // make pin LOW

We’ve not got our timers running yet, so for delay() we’ll just make the chip count up to a big number. We’ll put our delay() routine up above main(void), so that the compiler finds it before it’s needed, and without having to declare it separately. The whole main.cpp now looks like this:

Note the asm(“nop”); inside the delay loop – without it, the LED will still blink, but much too fast to see. This is because the complier tries to improve our program, thinking, “this loop doesn’t do anything useful, so I’ll prune it away!” The asm(“nop”); just inserts a do-nothing assembler instruction into the loop – but because the compiler doesn’t understand what’s going on inside the assembler, the compliler then leaves our loop alone.

Now you can compile the program (Build menu, Build Solution, or just press F7). Once you’ve got rid of any typos, you’ll see “Build succeeded.” down at the bottom of the screen in the “Output” window.

Now to send the program to the Due, choose Start Without Debugging from the Debug menu, and you should see a blinking LED on your Due!

In part two, we’ll make a neater way of setting up an output, and get digital inputs working.







Leave a Reply

Your email address will not be published. Required fields are marked *