blog.world3.net

Using the XMEGA DMA controller

04/11/2011 – 11:47

Finally got the XMEGA DMA controller on an ATxmega128A3 working. There is not much example code for it and the datasheet is not very clear, so I wrote this.

The datasheet and examples don’t really explain the terminology well. A transaction is the complete cycle of the DMA controller. To start a transaction you load up the control registers and enable the DMA channel. It will then respond to triggers. The transaction ends when TRFCNT * REPCNT bytes have been transferred.

A block is a way of dividing the transaction up to fit your buffer size. If you just want to fill one buffer from start to finish you can set the block size in TRFCNT to the full size of your buffer. There are not many situations where you would want to use blocks smaller than one whole transaction, so for simplicity you can often just ignore them and look at transactions only.

The burst length is the number of bytes copied in one go by the DMA controller and is set with the BURSTLEN bits in CTRLA. Often it is used to read/write 16 bit SFRs such as the ADC result or the DAC output level. You always want to access those 16 bit words in one go rather than as two 8 bit bytes and the burst length lets you do that.

Now comes the poorly documented bit. First we have the single shot mode bit in CTRLA (bit 2 SINGLE). If set every time the DMA channel is triggered it transfers a single burst of data, e.g. 2 bytes if you set BURSTLEN to 2BYTE. If it isn’t set the DMA channel does into free running mode and simply transfers bursts as fast as possible until the transaction ends. So if you want to save ADC readings into a buffer like I am you want single shot mode triggered by the ADC.

Next let’s look at repeat mode. In repeat mode the DMA controller transfers REPCNT blocks of data. To keep it simple let’s say you only have one buffer and intend to fill it in one go, so you set the block size to the buffer size. If you are not in repeat mode the buffer will be filled once and then the transaction will end and the DMA channel will be disabled. If you are in repeat mode the DMA channel will perform REPCNT block transfers, and since you set your block size to the buffer size that means it is the number of times the buffer will be filled consecutively. If you are in repeat mode and you set REPCNT to zero the DMA channel will repeat forever.

Now keep in mind that even with repeat mode enabled the DMA controller still has to be triggered. In normal mode one trigger transfers an entire buffer’s worth of data, and in single shot mode each trigger transfers one burst of data.

Finally we have double buffering mode. Double buffering basically alternates between filling two buffers. When one transaction is complete the DMA channel automatically stops and its partner starts. This action is independent of repeat mode, and will set the transaction complete flag so that you can detect when it happens.

Note 1. You need to look at the transaction complete flags, not the DMA channel active or pending flags, if you want to know when the channel has finished and the buffer is ready for processing.

Note 2. If you want to use a timer to trigger a DMA channel then you must enable that timer’s interrupt. You can give it a null interrupt handler (e.g. ISR(TCC1_OVF_vect){} or just RETI in assembler) but it does have to actually be enabled and triggering, otherwise the DMA channel will not be triggered either. That doesn’t apply to other peripherals such as the ADC, it seems to be just the timers.

PORTA.DIRCLR = PIN7_bm;
PORTA.OUTCLR = PIN7_bm;

ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc | ADC_CH_GAIN_1X_gc;
ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN7_gc;
adc_wait_8mhz(&ADCA);
ADCA.EVCTRL = ADC_SWEEP_0_gc | ADC_EVSEL_0123_gc | ADC_EVACT_CH0_gc; // event channel 0 triggers ADC channel 0
ADCA.CTRLA |= ADC_ENABLE_bm;

// set TCC1 to 11024Hz overflow, actually 11019.2838Hz (-0.052% error)
TCC1.CTRLA = 0; // stop if running
TCC1.CNT = 0;
TCC1.PER = 0x02D5;

EVSYS.CH0MUX = EVSYS_CHMUX_TCC1_OVF_gc; // trigger on timer overflow

// reset DMA controller
DMA.CTRL = 0;
DMA.CTRL = DMA_RESET_bm;
while ((DMA.CTRL & DMA_RESET_bm) != 0)
;
// configure DMA controller
DMA.CTRL = DMA_CH_ENABLE_bm | DMA_DBUFMODE_CH01_gc; // double buffered with channels 0 and 1

// channel 0
// **** TODO: reset dma channels
DMA.CH0.REPCNT = 0;
DMA.CH0.CTRLA = DMA_CH_BURSTLEN_2BYTE_gc | DMA_CH_SINGLE_bm | DMA_CH_REPEAT_bm; // ADC result is 2 byte 12 bit word
DMA.CH0.ADDRCTRL = DMA_CH_SRCRELOAD_BURST_gc | DMA_CH_SRCDIR_INC_gc | // reload source after every burst
DMA_CH_DESTRELOAD_TRANSACTION_gc | DMA_CH_DESTDIR_INC_gc; // reload dest after every transaction
DMA.CH0.TRIGSRC = DMA_CH_TRIGSRC_ADCA_CH0_gc;
DMA.CH0.TRFCNT = 2048; // always the number of bytes, even if burst length > 1
DMA.CH0.DESTADDR0 = (( (uint16_t) buffer_a) >> 0) & 0xFF;
DMA.CH0.DESTADDR1 = (( (uint16_t) buffer_a) >> 8) & 0xFF;
DMA.CH0.DESTADDR2 = 0;
DMA.CH0.SRCADDR0 = (( (uint16_t) &ADCA.CH0.RES) >> 0) & 0xFF;
DMA.CH0.SRCADDR1 = (( (uint16_t) &ADCA.CH0.RES) >> 8) & 0xFF;
DMA.CH0.SRCADDR2 = 0;

// channel 1
DMA.CH1.REPCNT = 0;
DMA.CH1.CTRLA = DMA_CH_BURSTLEN_2BYTE_gc | DMA_CH_SINGLE_bm | DMA_CH_REPEAT_bm; // ADC result is 2 byte 12 bit word
DMA.CH1.ADDRCTRL = DMA_CH_SRCRELOAD_BURST_gc | DMA_CH_SRCDIR_INC_gc | // reload source after every burst
DMA_CH_DESTRELOAD_TRANSACTION_gc | DMA_CH_DESTDIR_INC_gc; // reload dest after every transaction
DMA.CH1.TRIGSRC = DMA_CH_TRIGSRC_ADCA_CH0_gc;
DMA.CH1.TRFCNT = 2048;
DMA.CH1.DESTADDR0 = (( (uint16_t) buffer_b) >> 0) & 0xFF;
DMA.CH1.DESTADDR1 = (( (uint16_t) buffer_b) >> 8) & 0xFF;
DMA.CH1.DESTADDR2 = 0;
DMA.CH1.SRCADDR0 = (( (uint16_t) &ADCA.CH0.RES) >> 0) & 0xFF;
DMA.CH1.SRCADDR1 = (( (uint16_t) &ADCA.CH0.RES) >> 8) & 0xFF;
DMA.CH1.SRCADDR2 = 0;

DMA.CH0.CTRLA |= DMA_CH_ENABLE_bm;
TCC1.CTRLA = TC_CLKSEL_DIV1_gc; // start timer, and in turn ADC

for (i = 0; i < 20; i++)
{
while (!(DMA.INTFLAGS & DMA_CH0TRNIF_bm));
DMA.INTFLAGS = DMA_CH0TRNIF_bm;
TERM_tx_char('A');

while (!(DMA.INTFLAGS & DMA_CH1TRNIF_bm));
DMA.INTFLAGS = DMA_CH1TRNIF_bm;
TERM_tx_char('B');
}

In the example the code outputs ‘A’ or ‘B’ when each buffer is full.

By mojo | Posted in avr, electronics | Comments (0)

Democratic FAIL

31/10/2011 – 18:02

I made a couple of posts to Slashdot and decided to reproduce them here:

 

I wrote to my last MP (free to do online via The Work For You) to complain about the ever increasing internet surveillance. In the letter I pointed out that saving a single life, or even many lives, is not justification for the loss of privacy and rights. If saving a life came before all other considerations we would ban cars and shut down the road network. Given that I asked why she voted for the new laws.

Her response was along the lines of “it saved the life of a woman who was said she was going to commit suicide on Facebook because the police were able to backtrace her IP address”.

She lost her seat at the last election (I won’t say which because it would reveal where I live and I value my privacy). Good riddance.

I also emailed all my MEPs when the EU was debating extending copyright terms. The Greens and Lib Dems were in favour of reducing them, although we now know for certain that the Lib Dems are full of shit. The Tory MEP was in favour of an extension and as in your experience basically parroted the BMI press release.

So what it boils down to is that the Greens are the largest party who offer some prospect of representing my views, but they only have one MP. Protest is pointless as we have seen time and time again, the only exception being when enough people are fucked over badly enough for sustained violence such as the Poll Tax riots. The current lot outside St. Paul’s have just been evicted and will be history by the end of the week, and all the promises to ask the hard questions and have a debate by politicians are worthless. Most of the media hasn’t even bothered to report what they are protesting about, other than some vague hippies-against-capitalism bullshit.

The Government Petitions would be hilarious if they didn’t demonstrate just how far the three major parties are willing to go to shut you up and make sure your voice is never heard, except for at election time where people are guaranteed to vote for whoever will do the most to protect their wallet. The genius of the current system is that with one vote you have to pick both a local MP and who you want to form the government (assuming you are not fucked over by a coalition), so that one X on the voting slip has to cover every single issue you care about. Knowing that economics and taxes are the most important things all parties can afford to screw you on everything else. Labour started a war that 2 million people marched against and still got re-elected with a large majority.

Our democracy is fucked, we are fucked.

By mojo | Posted in politics | Comments (2)

Google can’t spell

24/09/2011 – 23:06

Been a bit slow with updates lately. Anyway, apparently Google can’t spell:

 

Google tries to make suggestions based on what it has scanned from web pages, but it seems that they didn’t filter out common errors… Google, U R TEH DUMB LOL

Still, Google docs is excellent. Now you can upload any filetype I have been backing up my photo raws there. Picasaweb only accepts JPEGs.

By mojo | Posted in idiots, Internet | Comments (0)

Fixing PHP in HTML files on Knet

21/07/2011 – 18:52

Knet recently updated something and broke my .htaccess file which was allowing PHP in .html files. Spent ages looking for a solution and eventually found this on a random blog post:

AddHandler application/x-httpd-php5 /cgi-sys/php5 .php .htm .html

Update 03/08/2011: They broke it again, this is now required to fix:

AddHandler application/x-httpd-php5 .htm .html
By mojo | Posted in Internet | Comments (0)

BBC twisting quotes again

30/06/2011 – 09:07

I had thought that maybe the problems with reporting the tsunami in Japan were partly due to the language barrier, but it looks to me more systematic than that. Spotted this gem today:

Headline: ‘Full blown crisis’

Quote: “The good news is, they are not allowing it to become a full-blown crisis,”

The headline implies the exact opposite of the quote. FFS. It doesn’t seem to say who wrote the article but stuff like this should never get past editors.

 

EDIT: After I complained it looks like the BBC have fixed the headline. Unfortunately I can’t find an archived copy of the page in its original form. Google’s cached copy has been updated but there is a copy/pasted version on the XFM website. Bottom of the 5th paragraph. In future I will take screenshots.

To be fair to the BBC they do at least do corrections when people complain.

 

EDIT: Found an original copy with the bad headline in it: http://news.myjoyonline.com/business/201106/68468.asp

By mojo | Posted in BBC, idiots | Comments (0)

Fix for Aero dropping out on Windows 7

19/06/2011 – 16:37

Sometimes when my machine resumes from sleep mode Aero doesn’t work and I lose transparency etc. It seems to be related to nForce chipsets… Remind me never to get another one of those, they are buggy as hell. If you check the application event logs there is an error 0x8898009b from DWM.

The fix is the following two commands to re-start the service:

Net Stop uxsms
Net Start uxsms
By mojo | Posted in windows | Comments (0)

Super injunctions and bloggers

23/05/2011 – 07:58

There appears to be a serious problem with super injunctions for bloggers. How can we comply with an injunction that is so secret we don’t even know it exists?

I heard the other day that Ryan Giggs was having an affair. Let’s use him as an example; say he took out a super injunction to prevent people reporting that online. How would I know it even exists? There does not appear to be any way I can find out that there is a gagging order on the information. The major newspapers are informed, but I wonder if all the smaller local ones are. Bloggers certainly are not.

It seems like the only choice for bloggers is to not write about anything that could be covered by a secret injunction, but clearly that is unacceptable as it would mean the end of free speech and amateur reporting. Not just online, but in newsletters and books too.

On Newsnight they were discussing the nature of these super injunctions and apparently many of them are in place to prevent blackmail. Random celebrity is threatened with “exposure” by someone so they try to prevent that person going to the papers. Chances are at least some of these cases are genuine blackmail where the victim has done nothing wrong and simply wishes to protect their reputation, but since we can’t know that when it does eventually leak out it is impossible to present both sides of the story. I generally don’t go in for this kind of tittle-tattle but people like Sir Fred Goodwin and the various private companies using super injunctions should be reported on and it is clearly in the public interest, and therefore the situation is intolerable.

By mojo | Posted in law | Comments (0)

Olympic Athlete’s Village site visit

21/03/2011 – 15:10

I visited the Athlete’s Village for the London 2012 Olympic Games. Not that much to see really, it is a building site… Behind schedule of course. The accommodation is the standard basic one floor apartment divided up with temporary partitions which will be removed before it is turned into council flats.

 

From Olympic site

You can see more here: https://picasaweb.google.com/mojo.chan/OlympicSite?authkey=Gv1sRgCIy6n_if3Pj_dA&feat=directlink

 

This Olympic themed post brought to you by:

The best ryokan (Japanese hotel) in Tokyo!

By mojo | Posted in Uncategorized | Comments (0)

FON is rubbish

03/03/2011 – 13:18

I decided to set the old FON router up before my trip to Japan this year. The idea is that you share your broadband connection via a wifi hotspot that the FON router provides and in return you can use any FON hotspot worldwide.

Before going I checked coverage. FON has done a deal with Livedoor in Japan so you can use any Livedoor access point for free. The map on the FON website shows lots of APs in the Tokyo area, particularly Akihabara where being able to check product specs online would be particularly handy.

The reality on the ground is very different. If these APs are there then their signals apparently don’t extend as far as street level. So far I have only been able to connect once while in Yokohama, and then the signal was so marginal it kept dropping out. There are lots of strong FLETS and NTT Docomo APs available but the former needs a permanent address in Japan for sign-up and the latter is very expensive.

FON is crap. I noticed that coverage with my own FON router was terrible before I went. It has a very small antenna and apparently low output power, pretty pathetic by today’s standards. Even the cheapest routers have multiple antennas and have the power/sensitivity turned up to 11 by default.

Don’t waste your time becoming a Fonero.

By mojo | Posted in Uncategorized | Comments (1)

Add Google Maps link to eBay listings

21/01/2011 – 16:43

Greasemonkey FTW. Add your location to the home declaration.

// ==UserScript==
// @name           Ebay-Location-Route
// @namespace      www.ebay.de
// @description    Adds a link for the item location on google maps
// @include       http://*ebay.*
// ==/UserScript==
// Version 0.4
// author: knoe, fixes by MoJo

//Configuration
var home = "mah house"; //home
var blank = false;                   //open link on a blank page?
//Config end

var ebayClass= new Array("titlePurchase", "sh-DlvryDtl");

for (var j = 0; j < ebayClass.length; j++)
{
 //check each element of the ebayClass
 for (var i = 0; i<document.getElementsByClassName(ebayClass[j]).length; i++)
 {

 //if the location has been found do the link stuff
 var toCheck = document.getElementsByClassName(ebayClass[j])[i].firstChild.data;
 if (toCheck.search("Item location") != -1)
 {
 var place = toCheck.slice(15);
 //the google map route link
 var linkRef = "http://maps.google.co.uk/maps?saddr="+home+"&daddr="+place;

 //create the link
 var link = document.createElement('a');
 link.href = linkRef;
 if (blank)
 link.target="_blank";
 var linkText = document.createTextNode(place);
 link.appendChild(linkText);

 document.getElementsByClassName(ebayClass[j])[i].firstChild.data="Item location: ";
 document.getElementsByClassName(ebayClass[j])[i].appendChild(link);

 return;
 }
 }
}
By mojo | Posted in Internet, software | Comments (0)
Page 2 of 1912345...10...»Last »
たとえ溺れても梦はゆめでしかない
  •  

    February 2012
    M T W T F S S
    « Jan    
     12345
    6789101112
    13141516171819
    20212223242526
    272829  
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Categories

    • audio (1)
    • avr (20)
    • BBC (1)
    • electronics (29)
    • genius (4)
    • hardware (22)
    • idiots (39)
    • Internet (21)
    • law (20)
    • microcontrollers (12)
    • networking (17)
    • politics (29)
    • privacy (19)
    • Retro Adapter (5)
    • security (17)
    • software (32)
    • Uncategorized (18)
    • windows (25)
  • Archives

    • February 2012
    • January 2012
    • December 2011
    • November 2011
    • October 2011
    • September 2011
    • July 2011
    • June 2011
    • May 2011
    • March 2011
    • January 2011
    • December 2010
    • November 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • November 2005
  • Links:

    Main site: world3.net

    Electronics: denki.world3.net

WordPress | Sandbox