Canary Embedded Software

From ELC Wiki
Jump to: navigation, search

The Canary Load Indicator uses an ATMega328 micro-controller which is based on an AVR 8-Bit processing core. Confusingly, there is another micro-controller from Atmel (now owned by Microchip) that has the name ATMega328P, and then on top of this, both of these slightly different micro-controllers have package suffixes added just to make things even more confusing, of course they had to contain "P" as well. As an example: ATMega328-PU, ATMega328P-PU.

In reality, and fortunately for us, the 2 devices are so similar the software and pins are compatible with each other. The main difference for the ATMega328P is that it has a special low-power control peripheral which can be used to save a bit of extra power. The main problem you'll encounter is when you come to program the devices as they identify themselves to the programmer as different parts. We'll come back to this later on.

Resources

There are a large community of people using this kind of micro-controller and others with the same core technology, and in actual fact, the well know Arduino platform uses the same family of processors. This makes it a particularly good choice for open-source projects as there are countless online resources that already exist and community forums where beginner/intermediate/expert users all ask questions and contribute. A quick online search will give plenty of results but a few useful ones to keep an eye on are:

AVRFreaks

https://www.avrfreaks.net/ This is an excellent forum about everything AVR.

Arduino

https://www.arduino.cc/ Although we're not yet using the arduino platform, their website contains lots of information about the ATmega328P.

At some point I'll get around to "porting" the software to the Arduino platform so that the more simple programming tools can be used but for now the following sections will explain how to program standard C/C++ code into the ATMega328 using a programming/debug tool known as the Atmel ICE.

Development Environment

Atmel Studio IDE
Atmel ICE Basic

Atmel/Microchip provide a bespoke Integrated Development Environment (IDE) for use with their products. This is a computer software that allows you to write code in C/C++, compile it into machine code, then interface with their programming tools such as the Atmel ICE (in-circuit emulator), which gives you programming and debugging functionality.

Atmel studio can be downloaded free of charge from the Microchip website

You will also need the Atmel ICE Basic to program the micro-controller. This can be purchased from an electronics supplier such as Mouser or Farnell.

We are looking into other ways to program these devices without the use of the Atmel ICE but these have not been fully assessed yet. One method could be to use an Arduino to download the code, or to rewrite the code using the Arduino IDE. There are also a large number of basic AVR flash programmers available on the market most of which are compatible with an application called AVRDude. Keep an eye on the news section of this Wiki for updates.

Setting up Atmel Studio

Make sure you have downloaded and installed Atmel Studio and the Atmel ICE drivers. The Canary can be programmed using a pre-compiled .elf file which can be downloaded from the software section here. Once you have the .elf file you can continue to the next section.

If you want to do code development or debugging you'll need the project file and source code.

  1. Download the zipped version of the Canary Chic Project from the software page or clone the Git repository.
  2. Go to File->Open->Project/Solution
  3. Navigate to the CanaryChic solution file and click open.

Connecting and Detecting the Micro-controller

After you've carried out the testing section of the PCB assembly guide, you can insert the micro-controller into the IC socket and power on the PCB.

As the micro-controller is fully isolated from the mains by the power-supply and the opto-coupler in the zero-crossing detection circuit, it is possible to program the micro-controller whilst running from mains power.

Even though the microcontroller is isolated from the mains supply, it should still be considered dangerous. Connect the programming tool before you turn on the power and avoid touching the PCB until you have removed the power. Familiarize yourself with the dangerous areas of the PCB.

  1. Connect your Atmel ICE to the ICSP connector, taking note of which pin is pin 1. On the Atmel-ICE cable, the bump should be facing away from the center of the PCB.
    CCRev1ICEConnect.jpg
  2. Go to Tools->Device Programming
    CCRev1ToolsProgramming.png
  3. The "Atmel ICE" tool should be selected from the drop-down menu.
  4. This is the point where specifying the exactly the right micro-controller is important. Check the writing on the actual IC itself to confirm whether you are using an ATMega328 or an ATMega328P. (An ATMega328-PU is an ATMega328 with a -PU package).
  5. Select the correct device from the drop-down menu.
  6. Click "Apply" then the "Read" button next to "Device Signature" to read the ID out of the hardware. If the hardware is working properly and the devices is correctly identified the device code should be displayed. It should also display the voltage.
    CCRev1MCUSelect.png

Fuse Bits

ATMega micro-controllers have a system to configure certain operational parameters known as Fuse bits. They are registers that can be programmed using the Atmel ICE and will hold their value until they are reprogrammed. These registers control very low level settings of the micro-controller such as the clocking configuration and the operational mode of the debug/programming port. In very bad situations it is possible to program these registers in a way which makes the device very difficult to recover so it's a good idea to be careful when you do.

It is possible to use a .elf production programming file to program the fuse bits as well as the code. If you do not wish to do any software development, modification or debugging, this is the easiest way to program the device. If you wish to use this method, continue to the programming section.

If you wish to program the device using a .hex or .srec file or you wish to carry out further software development you must program the fuse bits manually.

You can only program the fuse bits when the micro-controller debug port is in SPI mode.

Program the fuse bits as shown in the image below. If you wish to do software development, you will also need to check the DWEN fuse bit (DebugWire Enable). CCRev1Fuses.png

If you wish to edit or debug the software, you need to enable the DebugWire interface. This means setting the correct Fuse bit in the next steps. If you just wish to program the micro-controller using the ".elf" file you can continue to the next section.

  1. Choose the "Fuses" item in the panel on the left hand side and set your fuse settings as shown in the image.
    CCRev1FusesDWEN.png
  2. Click program. When the operation is complete, you can close the window.
  3. At this point you must switch the power to the device off and then on again to reset the microcontroller.
  4. Press the green triangle to load the code into the micro-controller.

Programming with a Production File

If you wish to load pre-built code into your CanaryChic, you can use one of the .elf files which are available in the software section of this wiki. This file contains the data which is stored in the micro-controller's Flash Memory and the Fuse bit settings.

  1. Connect to the micro-controller as discussed in the previous section.
  2. Choose "Production File" from the panel on the left.
  3. Browse for the .elf file you downloaded.
  4. Check the "Flash", "Fuses", "Erase memory before programming" and "Verify programmed content" checkboxes.
    CCRev1ElfProg.png
  5. Click "Program"