Saturday, June 25, 2016

Getting started with Atmel ARM processor: SAM D21 bare chip

ARM processors are really awesome piece of hardware; they are powerful and with the right setup; they are also easy to program. Considering also that they are very cheap, it is easy to understand why they are a great platform for different types of projects.

I did start recently to experiment with the Atmel SAM D21G; while looking for a more powerful microcontroller, that could replace the ATMega used in many board like the Arduino. The Arduino Zero has as main microcontroller, an Atmel SAM D 21; which is able to be programmed via Arduino IDE. Awesome.

Getting started


Next step is to make my own design; so I started with the bare chip. They are sold in various store, so you won't have much of a problem to find them. The chip is sold as QFP or QFN, with different number of pins; pick the size and version that satisfy your needs; I did start with a 48 pin QFP

Atmel SAM-D Page

First problem: the chip is very small, and unless you use a breakout board, it is not possible to make any connection. You have another option: there are adapters from QFP to DIP; which allow you to use a simple breadboard to start, while you decide the design and all the other details of your design.
The adapter is easy to find; although Ebay seems to have the cheapest of all.



TQFP48 QFP48 to DIP48 Programming Adapter Socket

You also need a breadboard, which will host the temporary circuit, some capacitors (the schematic posted below will show the values for each capacitor), male/male and male/female wires and a regulated 3.3V adapter, since the chip maximum voltage is 3.6V.

The last thing that you need is software to program the chip, and hardware to talk to the chip via computer. Since the chip does not have yet the bootloader; even if you connect the USB connections from the chip, you can't do anything with it (the computer won't even see it as peripheral in some case).

As software, you need 2 items:
- Bootloader (to put code on the chip via serial or USB port)
- IDE to put the bootloader on the chip.

The bootloader is provided by Atmel; good news! You get the whole package, including the source code, so you can modify it as you prefer. Dowload it from the Atmel site

Atmel SAM-BA Bootloader

The IDE is also provided by Atmel; you need to create an account on the site, and it is free. Download it and install the software.

The hardware is what will put the software on your chip. For the SAM-D21, you can get an Atmel-ICE. This allow you to put code and bootloader on the chip, and also to debug in realtime; which is super useful.




Atmel ICE debugger

 Keep in mind, that the base version of the ICE, has a 10 pin connector with a very small pitch (0.25), compared to the regular .5 pitch, found in most connectors. So if you want to use the 10 pin connector, either you need to buy the more expensive kit (120 Vs 65, but you get various adapters, which cover multiple cases, so you can program different chips).
The alternative is to get some adapter, that change the pitch from .25 to .5, make your own cable or use the 6 pin connector (more about this later on). It is up to you to decide if the extra connectors are worth the 50 dollars extra on the device price.

Power up the chip

 Now it is time to connect the chip; the first thing is to connect the pins to the breadboard (you can use headers or solder the wires directly on the adapter). In the beginning, you will need to connect the pins related to power and ground.
The 48 pin version, has 4 set of pins, plus Core

- Pin 5 and 6 for analog Vin and GND
- Pin 17 and 18 for Digital Vin and GND
- Pin 35 and 36 for the second Digital Vin and GND
- Pin 42 and 44 for the main Vin and GND
- Pin 43 for VDD Core (it has no related ground)



It is important to know that you need to connect ALL the power and ground lines, and you also need capacitors, to stabilize the input to the processor. The diagram below show the values of the capacitors and how to connect them.




 A good approach is to put one capacitor (either 0.1 microFarad or 1 microFarad) for each line; I did follow this approach and the system is stable.
 Connect the regulated 3.3V source and the chip is ready to go. Do not power up yet the chip.

Software setup and Bootloader

 The software setup is pretty simple; download Atmel Studio and install it; once done, run it for the first time. Keep in mind that Atmel Studio works only on Windows, so keep that in mind.

The bootloader once downloaded, will be available as .hex file (for the SAM-D21G18A, the file is in the "load sam-ba/samd21g18a" folder.

Hardware programmer setup

The device is very simple; one side has the USB connector that goes to your computer, the other side has 2 connectors, marked AVR and SAM. You will need the SAM connector. The ICE is shipped with a small ribbon cable, which has a 10 pin (.25 pitch) on both sides, and a 6 pin (.5 pitch) connector, which is used mainly for SPI programming, while using AVR. This won't work with SAM port, unless you make some changes.

The first thing to notice is that pin 10 (reset), is not carried over to the 6 pin connector, so if you do not have available the 10 pin adaptor (from .25 pitch to .5 pitch), you need to make a hack on the cable.




 Expose the wire, from the leftover of the ribbon coming from the 10 pin connector, and solder a wire; this will be the wire to connect to the Reset pin of the SAM-D21.


 Now, for the pinout of the 6 pin connector, you have the following:



 Which correspond on the 6 pin connector as following (:






The plastic key will be on the left, if you look at the connector from the back. The VTG pin wire will be red.

Now you need to connect with wires, the pins from the socket adapter: SWDCLK is on pin 45, while SWDIO is on pin 46. SWO is not required, so you can let it be. The 2 GND goes to the GND of the breadboard, while VTG goes on the positive of the breadboard (where you supply the regulated 3.3V)

You also need the Reset pin from the Socket adapter, which is located on pin 40; connect this pin to the cable that you attached to the reset pin on the flat cable.

Once the connections are done, you are ready to start program the chip.


Chip programming - Install bootloader


You should have Atmel studio already up and running; now you can connect the USB cable from the ICE to the computer. The drivers will be installed automatically (be sure to install first Atmel studio, before you plug the ICE; otherwise Windows will have problems to find the drivers).

You will see a solid red light on the ICE. Now power up the socket adapter, and you should see a solid green light on the ICE, if the connections are correctly done.

Now in Atmel studio, go to the menu " Tools" and select "Device Programming".


In the tool dropdown, you will see the Atmel ICE, in the device, choose ATSAMD21G18A (in your case, whatever model you purchased); and as interface choose SWD and click Apply.

You will see settings appearing in the lower part of the window; just leave everything as is, and click "Set"


Now, click on "Read", under "Target Voltage", you should see 3.3V or a similar value. Now click "Read", under "Device Signature", and you should see the hex value of your blank SAM D21. Congrats!

If you have problems, check that the connections are correct, check that al the power lines and ground are connected; and verify that the pin from the ICE are correctly paired with the socket adapter pin. Be sure that the connector plugged in the ICE is in the SAM position, not AVR (you may break the ICE if the connector is mixed up, so be careful).
Check also that the socket adapter has the chip in the correct position (the chip has a circle on the top left corner to show where pin 1 is located; the socket usually have an identifier, like a triangle in the upper left corner. Pin 1 will be on the left row, pin 48 on the top row, so position the chip accordingly.


Once that the device signature is read, you can put the bootloader on it.
Go to the Memories section, and choose the hex file that you want to burn (there are 3 version: one for usb, one for UART and the third use both), based on your design, then click on "Program".


Congratulations! you should see a message on the bottom of the window that tell you that the bootloader has been burned correctly. Now you can program the device via UART or USB.

I will prepare a follow up where I will show how to write simple applications for the microcontroller, and how to upload them via USB or UART.

Tuesday, January 12, 2016

Beaglebone as simple GIT local server

While there are plenty of solution to post your code online; like Github and similar, sometimes you want to keep your code private.
GIT is a great tool to create and manage repository, used in many companies; you can easily install it on a small computer like a Beaglebone board, and have your own private server.


In my case, I used an old Beaglebone model A6 (this is the predecessor of the BeagleBone Black model, which is much more powerful). The board is small; it fits in an Altoids tin can and has enough power to run Debian with LXDE.


If you have an old computer, you can use that, but it is hard to beat the convenience of a single board computer; which require no monitor, and works just with a LAN cable and a USB charger.

The same would apply also for other single board computers; like a Raspberry Pi; I had one of these around so I did use it.

Setup the board

The first step is to install the operating system on the Beagleboard. It is pretty straight forward, the images are available on the Beagleboard site. I did use the Debian distribution.
Insert the sd card., boot the board and verify that the OS load.

The next step is to connect the board to the network; plug the ethernet cable and check on your router which IP did the board get. Once you have the IP, connect via ssh to the board. I use Teraterm on Windows; if you are on OSX or Linux, the terminal will suffice.
You may want to assign an IP in your router to your board; follow your router manual to find out how to do so.

The login for Debian is root without a password. You may want to create a new user at this point; which will be dedicated only to Git.

Now it is time to create the SSH key, pick a good password, even if you will be the only one to use this server.


ssh-keygen

The public and private keys will be saved in ~/.ssh; we will be using these in a bit.
Log out from root and log in with the newly created user, perform upgrade, update and download Git


sudo apt-get update
sudo apt-get upgrade 
sudo apt-get install git-core

Once you have Git on your machine, you can create a repository and initialize it. You can start with one, and create the others as you see fit. I am creating a "repository" folder in the home directory , with a .git folder for each repository

cd ~
mkdir repository
cd repository
mkdir repo1.git
cd repo1.git
git --bare init mkdir 

This will create the repository with no files in it.

The next step is to initialize the repository. To do so, we will create another folder, and a file in it, and then add the file to Git.


cd ~
mkdir temp_repo
cd temp_repo
echo first commit > init_me.txt
git init
git add init_me.txt

Before the commit can go trough, we need to set up some parameters for Git, to establish who is committing. add your email address and username of your choice

git config --global user.name "Yourname"
git config --global user.email email@domain.com

Now we can submit our first commit, After this, we can push the commit to the main repository to initalize it.
 
git commit -m 'initialize repo'
git remote add origin ssh://user@domain.com/~/repository/repo1.git
git push origin master

 At this point the repository is intialized. With this, we are done on the server for now, and it is time to move on the client.


 Client setup: Windows

For the client setup, there are various choices to make: you can use Windows, OSX or Linux clients; you can use a GUI or command line interface. In my case, I use Windows and Sourcetree, which is a free UI available for both OSX and Windows.

First thing to do, after install Sourcetree, is to set up the SSH key. Windows is not as straight forward as a Linux or OSX machine, so it is not enough to just cat the public key from your master, to your client autorized keys. On Windows you will need some extra software: Teraterm, Puttygen and Pageant.

Teraterm is the terminal client to connect to the server, Puttygen is used to create windows keys for SSH connection, and Pageant is used to keep the key in memory. There are other ways to do this, I did pick this way since it is straightforward and easy to implement.

First, connect to the Beaglebone via Teraterm; all that you need is to get the IP of the Beaglebone, and choose the SSH as connection type. Once done so, the connection start and you will be asked to type the username and password.
Once connected to the Beaglebone, copy the private key from the Beaglebone folder in "home/username/.ssh" to your local Windows machine. To do so, use the file menu in Teraterm, then select  "SSH SCP..."; you will see a window similar to this





Select the id_rsa and the destination, then press the "receive" button to copy the files on your Windows machine. Rename id_rsa to id_rsa.ppk
Windows sadly is not able to use the key, so we need Puttygen to create a new windows private key.
Run Puttygen and  load the id_rsa file, then push the "Generate" button and save the new key.

Last part is to load the newly created key in Pageant. Once launched, you will see an icon on the Windows toolbar in the notification area. right click on it and select "view keys". The list will be empty; press the "Add Key" button and load the new key.

Now you can use Sourcetree to clone the project from your Beaglebone, and push your commits to the Beaglebone.

In Sourcetree, select the "Clone/New" button, then add the url to the Beaglebone (using "ssh://" as protocol), replace IP and USERNAME with your Beaglebone IP and your Beaglebone username, define a destination and press "clone". This will clone the repository and you are set.




I did try with 4 clients (1 windows 2 Linux and 1 OSX) , and so far the Beaglebone is performing just fine. It is also require minimal current; and everything run out of a SD card; you can keep the board on for months without problems. You can also backup the whole repository when needed, simply attaching an external USB drive or copying directly the repository folder via network, to a NAS or other location.