Friday, February 10, 2012

8bit timer issue on Proteus for ATmega169

I have noticed a glitch on behaviour of VSM Proteus with Timer0 and Timer2 (only 8 bit timers) of devices such as Atmega169 Atmega169P Atmega169PA Atmega329 and so on.
When a CTC (clear timer on compare match) is enabled the corresponding IRQ is called two times: once at the compare match and once one timer cycle (tick) later.
For example using WinAVR GCC compiler, I set up the following instructions:
// Normal Mode, CTC, 111= 1/1024 (page 163 of DS)
TCCR2A = 0b00001111;       
OCR2A = 0x7F;   //1024*(0x7f+1)/8Mhz = 16.384ms
bset(TIMSK2, OCIE2A); //Enable IRQ on compare A


Then on the IRQ I simply toggle one pin:

ISR(TIMER2_COMP_vect){  //16.384ms
    if(toggle){
        clr(DEBUG_PIN);           
    }else{
        set(DEBUG_PIN);
    }
    toggle = 1-toggle;
}


This is what I SHOULD obtain (confirmed by AVRstudio 4 Simulator and, above all, the REAL Hardware): A square wave with 16.384ms half period.

And this is what I obtain in Proteus:

Interrupt occurs when TCNT2 == OCR2A (then it is automatically cleared) and when TCNT2 == 1, thus the waveform will be almost always high or low (and the Interrupt routine will be executed twice in the same time interval).

WORKAROUND
Since the Interrupt Request is erroneously triggered when TCNT2 goes 1 outside the Interrupt routine, it is simply necessary to wait for it to go 1 INSIDE the Interrupt routine itself.
Just end up your routine as follows:

ISR(TIMER2_COMP_vect){  //16.384ms
    if(toggle){
        clr(DEBUG_PIN);           
    }else{
        set(DEBUG_PIN);
    }
    toggle = 1-toggle;


    while(TCNT2 < 1);
}


If the instructions in the routine take some time, there is a good chance that the last instruction is tested but not executed (and not much extra time is wasted). If the routine is short (as in the example) there is one timer tick waste at the end of the routine itself, but at least it will work correctly on emulator AND on real hardware.

There is no need for such a workaround for timer overflow interrupts.

I hope that this will be useful for someone. It took me a few hours to investigate and solve...
(and of course this is a test routine: if I wanted just to toggle one pin I would use the OC2A output without using the IRQ...)

Wednesday, December 8, 2010

J-Link

Here are some settings for a Jlink device I bought in from China. It seems not to recognize correctly the attached STM32 (Cortex-M3), so here are the manual configuration to make it work.
For my future reference and for anyone who may find it useful.

Friday, August 27, 2010

Best C editor for AVR, PIC, RENESAS, etc... ???

I've been spending a lot of time searching for the best C editor for MCU's.
There are a lot around the internet, many of them have many important features, but no one is still perfect for MCU's, because they are made for Windows' C so they provide class handling, inheritance, and so on...
"Must have" features for an editor should be:
  • Syntax highlight
  • Code folding
  • Self completion (like MS Intellisense)
  • Show Hints when mouse hovers a variable/function
  • Functions List (argh! this is a point I have to discuss!)
  • Functional Find in Files
  • Parenthesis matching
  • Hyperlink in comments (this is a point I have a great idea, later on this post)
  • Light size & portable
  • Free of charge. Possibly.

I tried: SlickEdit2010, PsPad, Programmer's notepad, Code::Blocks, Eclipse (wonderful AVR plugin but Java: as slow as death), UeStudio, Unired, Netbeans, HippoEdit, JFE and Codelite.

Everyone has many of the above features but none has them all. Slickedit is probably the best but it lacks of the last two features (a crappy illegal portable version exists but you won't be able to run any batch to compile using WinAvr).
Jfe is the lightest and portable one: you can find in workspace files even if files are not yet saved, but it remains a little steps behind (no code folding or hints on variables, just a sort of help when typing functions).

Ehm, one question: WHY DO FUNCTIONS LIST HAVE TO BE ORDERED ALPHABETICALLY???
I like only editors who show functions the same order I have typed them! When coding for a MCU you probably have one single file which contains all. No prototyping, just a list of methods.
I need an overview of the functions I wrote, not the alphabetical list of them. The upper part of my code is initializations, the middle part are interrupts, followed by low level functions then more high level ones who call the preceeding ones.
If you sort it alphabetically I lose control of who is calling who.

Ok, at the end of the day I decided that Codelite (without Windows' facilites, no wxWidgets and so on) is my choice. It is also opensource so I could make changes to SymbolView.dll to show functions not sorted alphabetically.
For version 2.6 (and not following).
You can install it (but copying the folder to another pc will make it portable), then go to C:\Program Files\Codelite\Plugins and replace the SymbolView.dll using the one you can find zipped here.
Then, after opening the program, go to Plugins menu -> Manage Plugins... and check the SymbolView plugin.
It is shown as a tab in the workspace pane, right click on that tab and choose "Detach".
Now finally you have functions list (and only functions, no macros, variables, etc...) ordered by line number...


It still lacks of hyperlink in comments. If a part of my code is a result of an Excel Sheet computation I would like to write:
// Details on computations: file://algorithm.xls
So when i hover the mouse on it, and click, the file opens.

Some of the above editors have this feature, but Codelite hasn't. Let's hope in future.

MY IDEA

This is my idea (who knows, someone in future will implement it): instead of placing just an hyperlink why can't I link the file directly?
Let me explain: I write some code with a clock and signal.
This is what I want to see on screen just for better documentation of what I'm doing:


And this is what is stored in real file (thus not interfering with compilation) opened with notepad:

WHY NO ONE EVER THOUGHT ABOUT IT? I'M SICK OF DOCUMENTING SIGNALS IN MY FILES WITH SLASH, BACKSLASH, UNDERSCORE & PIPE /_|\ !!!


Parallax has delightful fonts for Propeller MCU (must install Propeller Tool) which has many symbols, but it forces to save the file in unicode format... I like ANSI.

Damn. One of these days I'm going to do that myself...

HACKING DSO NANO 0201

Hi everybody.
A few weeks ago I bought the DSO 0201 oscilloscope (you can find it on eBay or in Italy you can buy it from www.futurashop.it).



It's awsome and open source.
You can find all the resources here: Google Code including schematics and source code for version 1.1.

Personally I think that best firmware version is the one named "Paul's 1.5" instead of latest 2.x
It's the one I currently use, but no source is provided because some parts of code are now copyright protected. What a pity.


HACKING!!!!

What if you wanted do some minor changes to an already compiled firmware? For example the beloved Paul's 1.5? Just need to know how to handle dfu files and a hex editor.

In the next steps I'll show you how to change the language of a version (I repacked it to Italian), and also how to edit some characters fonts.

1) Download for example, Paul's 1.5 dfu file (or from here, right click on file and save as...)

2) Open it with an Hex editor and trim away last 4 bytes (which represent the checksum) in this example: 21 1C B8 BB

3) Search for any string, for example "RUNNING" and go to that address.

4) Change the strings you want (the horrible "PAUSHED" should be "PAUSED" or the string in your language) from that point onward. Almost at the end of the file there are error messages. I noticed that if a string ends with many zeroes you can overwrite them (but leave at least one!) proved that the resulting string fits in the screen, once you upload the new file.

5) When you have done you must recompute the checksum. So save the modified file and zip or rar it. And open it (for example) using WinRar. You'll notice that CRC32 is computed as last column:


6) Now you must complement the CRC32. Open Windows Calculator in Scientific mode and select the Hex data input. Type in FFFFFFF - 116BA0B2 (eight times F minus the number you find in the rightmost column of WinRar).

7) Now, the resulting number must be added to the end of the edited dfu in reversed byte order (i.e. you obtain EE945F4D, at the end of the file you must add 4D 5F 94 EE).


8) Finally, open the DfuSe Demo for firmware upgrading, power on the unit with the minus key pressed to enter upgrade mode and upload the new file.

9) If you are Italian, you can download my translated version from the same address above, I also added the accented "a" (à) and made a better "delta" symbol. To understand how fonts were mapped I needed to download the 1.1 source code but I also made a pdf guide (Enter the font directory at the end...).

This is the result of the hacked Italian version:


TAGS: DSO NANO 0201 versione 1.5 v1.5 Italiana in Italiano