High Voltage Coils

electrical and mechanical engineering

Home - Tags: xmega

Tag: xmega

How to use Atmel’s “atprogram” cli command line interface to program a production file into an xmega

Posted on 12. September 2017 in low voltage experiments

Hi all,
a very long headline for this simple task, but it hits the spot! Former Atmel (now Microchip) provides a very powerful back end programmer, intended to be used within a production line, to automate the programming process, without having to use Atmel Studio.

What are the benefits of using a production file? Very simple, have the user signatures, eeprom, flash and fuses combined in a single file. Then write a little batch script to burn this elf image into multiple micro controllers.

 

1.) How to generate a production file?

Open your project with Atmel Studio, then enter the “Device Programming” dialog. There is a own tab called “production file”. Use the lower half of the dialog, below the “Save to ELF production file” line. Choose a valid hex file for the flash, optional a eeprom and user signitures file. Then choose which of the files should be stored from the check boxes. Note that it is also possible to store the fuses and lockbits. If checked, the fuses and lockbits of the currently connected micro controller will be stored, so make sure they are configured correctly. Click on the “Save” button to store the production file.

 

2.) How to flash a production file from Atmel Studio?

Test your production file by using the upper part of the dialog. Enter a filename into “Program device from ELF production file”. Next choose which part of the production file you want to flash, all none existing parts will be grayed out and are not select able! Click on “Program” to flash the whole device with a single mouse click, very handy ūüėČ

 

3.) How to flash a production file from a batch scrip without Atmel Studio?

Create an empty text file and name it “start_flash.bat”. Copy the textfile provided below, alter your device and tool, then start flashing.

@echo off
cd "C:Program Files (x86)AtmelStudio7.0atbackend"
c:
atprogram -t atmelice -i pdi -d atxmega32a4 program -c -fl -fs --verify -f "d:mytestfile.elf"
pause

After execution will result in the following console output.

 

A little bit of explanation:

“@echo off” will silence the console directory output after each script command, when removed, the result will be much more unreadable

“cd” will change the directory to the atbackend path of the Atmel Studio installation folder, make sure this folder matches your installation path

“c:” is just here for the purpose if you start your batch script from a different drive then c:

“atprogram” is used to do the heavy lifting
“-t atmelice” means which tool i want to use, enter “atprogram list”¬† to see all available tools, for example my output
simulator       No serialnumber found
atmelice        J41800059125
“-i pdi” means i want to use the pdi interface for programming
“-d atxmega32a4” is my target microcontroller

“program” means i want to flash the contents of a file
“-c” perform a chip erase
“-fl” write flash
“-fs” write fuses
“–verify” after programming, memory contents need to match the target file
“-f <filepath>” this is my production file

4.) I want additional information about the features of atprogram!

use the console commands
“atprogram help” to get a full list of features.
It is also possible to get additional information about a single command, for example
“atprogram help program” will give you detailed information about all command line parameters available for the “program” command.

Atmel xmega signal / sinusgenerator using DAC

Posted on 14. December 2015 in low voltage experiments

Hi all,

after several thoughts about how to create a sound with an xmega32a4 microcontroller from ATMEL, i detected two easy approaches to stick to, PWM (puls width modulation) or DAC (Digital Analog Converter). After thinking about the most flexible approach, i choose the DAC.

My code consists of 3 parts.

  1. static sinus lookup table, precalculated 12bit values, stored in a 16 bit word array
  2. A timer, which is acting as the sample rate clock, for example 48kHz
  3. The DAC, which will receive new values if the timer overflows

The hardware wiring is very simple, PORTB2 (DAC Channel 0 output) directly connected to an audio amplifier.

DAC working at 48kHz sample frequency

#include <avr/io.h>
#include "driver/avr_compiler.h"
#include "driver/driver_clksys.h"

const uint16_t sin1kHz[48] = {2048,2315,2578,2831,3071,3294,3495,3672,3821,3939,4025,4077,4095,4077,4025,3939,3821,3672,3495,3294,3072,2831,2578,2315,2048,1781,1518,1265,1025,802,601,424,275,157,71,19,1,19,71,157,275,424,601,802,1024,1265,1518,1781};

uint8_t idxsin = 0;
#define MAX_IDX 48

ISR (TCC0_CCA_vect)
{	// Timer overflow, put next sample into DAC
	DACB.CH0DATA = sin1kHz[idxsin];
	idxsin++;
	if (idxsin >= MAX_IDX) { idxsin = 0; }
}

int main(void)
{

	Config32MHzClock(); // set systemclock to 32MHz

	TCC0.CNT = 0; //Reset timer 0
	TCC0.PER = 167; // 21us @ 32MHz = ~48kHz
	TCC0.CTRLA = TC_CLKSEL_DIV4_gc; // Prescaler
	TCC0.INTCTRLB = TC_CCAINTLVL_LO_gc; // TCC0_CCA_vect, Compare Match

	PMIC.CTRL = PMIC_LOLVLEN_bm;
	sei();

	DACB.CTRLB = 0x00; // single channel operation PB2 only
	DACB.CTRLC = 0x08; // Vref = Analog Supply Voltage
	DACB.CTRLA = 0x04; // CH0EN = Enable Channel 0
	DACB.CTRLA |= 0x01; // ENABLE = Start the DAC

	while(1)
	{

	}
}

xmega_out2

1kHz @ 48kHz measured at uC output

The result is not bad, but feeding this to into my amplifier without additional filtering, results in¬† disturbing crackling noises. To reduce the “stair” effects, i choose to increase the sample frequency from 48kHz to 100kHz.

DAC working at 100kHz sample frequency

To make this work, i just need to reconfigure the timer parameters and sinus lookup table.

const uint16_t sin1kHz[100] = {2048,2177,2305,2432,2557,2681,2802,2920,3034,3145,3251,3353,3449,3540,3625,3704,3776,3842,3900,3951,3995,4031,4059,4079,4091,4095,4091,4079,4059,4031,3995,3951,3900,3842,3776,3704,3625,3540,3449,3353,3251,3145,3034,2920,2802,2681,2557,2432,2305,2177,2048,1919,1791,1664,1539,1415,1294,1176,1062,951,845,743,647,556,471,392,320,254,196,145,101,65,37,17,5,1,5,17,37,65,101,145,196,254,320,392,471,556,647,743,845,951,1062,1176,1294,1415,1539,1664,1791,1919};

uint8_t idxsin = 0;
#define MAX_IDX 100

ISR (TCC0_CCA_vect) 
{	
	DACB.CH0DATA = sin1kHz[idxsin];	
	idxsin++;
	if (idxsin >= MAX_IDX) { idxsin = 0; }
}	

int main(void)
{
	
	Config32MHzClock();	

	TCC0.CNT = 0;
	TCC0.PER = 157;	 // 10us @ 32MHz = ~100kHz	
	TCC0.CTRLA = TC_CLKSEL_DIV2_gc; // Prescaler
	TCC0.INTCTRLB = TC_CCAINTLVL_LO_gc; // TCC0_CCA_vect, bei Compare Match
	
	PMIC.CTRL = PMIC_LOLVLEN_bm;
	sei();	
	
	DACB.CTRLB = 0x00; // single channel operation PB2 only
	DACB.CTRLC = 0x08; // Vref = Analog Supply Voltage
	DACB.CTRLA = 0x04; // CH0EN = Enable Channel 0
	DACB.CTRLA |= 0x01; // ENABLE = Start the DAC
	
	while(1)
	{
		
	}	
}

xmega_out2

1kHz @ 100kHz measured at uC output

The result is not perfect, but the sinus at the amplifier output is completely clean, without any additional sidebands or crackling noises.

ls_out

signal after amplification measured at the loudspeaker

Performance measurements showed, the CPU load is at ~17% (time between samples = 10us @100kHz, time in ISR = 1,7us), which means that in theory, the sample rate may be pushed to 400kHz. For a better performance, i would recommend using the xmega DMA. A good implementation can be found at the AVR Xplain , Atmel AVR1508: XMEGA-A1 Xplained training – XMEGA DAC document, chapter “Task 4”.