Watchdog and CodeVisionAVR

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.

Just a quick question. I see several references to setting up the WDTCSR register in the following way:

WDTCSR = (1<<WDE) | (1<<WDP2); //250msec timeout 

But I can't find any header files where the registers are defined - neither in AVRStudio4 or CodeVisionAVR. Where can I find the definitions for these registers? I can of course write them myself, based on the chip datasheet. But if the definitions is already part of some package I haven't installed, then it would be a bit redundant.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The definitions are in the ASM .include files, or the partdefinition .xml files. Alongside the Studio4 in "/Program Files/Atmel".

CodeVision does not supply the sfr bit-values. Avr-gcc and ImageCraft do. You can get them yourself from the data sheet. I find it useful to print the one or two page table of SFRs.

CodeVision does supply watchdog functions. Search the Help.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the quick reply.

I'm still struggling a bit with changing the watchdog prescaler bits while the program is running.

It seems that some times I'm allowed to change the bits and other times I'm not. Even though there really shouldn't be a reason for it. Just moving the position of a function in the main .c file will make a difference. One position and I'm allowed to change the bits. Another position and the bits remain the same.

I see that it's difficult to get what my problem is. It's difficult for me to explain also, since there are so many variables here, so please bare with me. :-) Do you know about the circumstances for the watchdog? What will affect the ability to change the prescaler bits or not?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

CodeVision does not supply the sfr bit-values.

Though this thread may prove useful:

https://www.avrfreaks.net/index.p...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry. CV does a Watchdog initialisation via the CodeWizard, but does not seem to have specific library functions.

But you just follow the data sheet. Or if you are porting "working" code, you just #define the relevant bit-values.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bassmus wrote:
It seems that some times I'm allowed to change the bits and other times I'm not.

Do you realise that you have to follow a strict timed sequence to change the watchdog configuration? In the case of the mega168 (I expect others are similar or the same), quoting from the datasheet:
Quote:
To further ensure program security, alterations to the Watchdog set-up must follow timed sequences. The sequence for clearing WDE and changing time-out configuration is as follows:

1. In the same operation, write a logic one to the Watchdog change enable bit (WDCE) and WDE. A logic one must be written to WDE regardless of the previous value of the WDE
bit.

2. Within the next four clock cycles, write the WDE and Watchdog prescaler bits (WDP) as desired, but with the WDCE bit cleared. This must be done in one operation.

Christopher Hicks
==

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Do you realise that you have to follow a strict timed sequence to change the watchdog configuration?

Thus, use the Wizard-generated pattern and embed into your code. For one, it takes care of optimization settings. Also, to enforce the change interrupts must be disabled. The Wizard code won't do that as it is known that global interrupts are disabled at the program head.

// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/2k
// Watchdog Timer interrupt: Off
#pragma optsize-
#asm("wdr")
WDTCSR=0x18;
WDTCSR=0x08;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which can be done quite happily by most C Compilers. avr-gcc probably needs -O1 and above.

Alternatively a small ASM snippet or function can ensure compliance.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
avr-gcc probably needs -O1 and above.

Or use perhaps which has handcrafted Asm to guarantee the requirements:

http://www.gnu.org/savannah-chec...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Probably not the most efficient method, but here is what I use in Codevison for the mega88:

    // Enable the watchdog timer, set for 8 seconds
    #asm
        cli
        wdr
        push r16
        ldi r16, 0x18
        sts WDTCSR, r16
        ldi r16, 0x29
        sts WDTCSR, r16
        pop r16
    #endasm

As a note, this will end up with the global interrupts disabled.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks a bunch for the help! And sorry for not replying earlier. The problem was, as cmhicks points out, that when writing the code in C, the code is simply not fast enough. I have four clock cycles after setting the WDCE bit, before the HW resets the bit again, and it was anyone's guess if I made it in time or not.

Is it safe to say that my problem was in fact a compiler error then? :-D

In any case, I wrote an ASM snippet based on the datasheet:

#asm
    cli  ; Disable global interrupts
    wdr  ; Reset watchdog timer

    ; Setting Watchdog Change Enable bit 
    lds r22, WDTCSR  ; Copying WDTCSR register contents into register 22
    ori  r22, 0x18   ; ORing 0x18 (WDCE and WDE bits) into register 22
    sts WDTCSR, r22  ; Copying register 22 contents into WDTCSR register 

    ; Setting new Watchdog Prescale bits
    ldi r22, 0x0e    ; Copying 0x0d (WDE, WDP2 and WDP1 bits = 1 second)
                     ; into register 22
    sts WDTCSR, r22  ; Copying register 22 contents into WDTCSR register

    sei  ; Enable global interrupts
#endasm

I changed the register from r16 to r22, since the CodeVision documentation doesn't list r16 as a register that can freely be accessed from within the assembly tags. I don't need a memory conflict right now.. :-)

But I'm curious.. I cannot access my various C definitions from inside the #asm #endasm tags. This is obvious to most, I guess. But is it any way to access a C defined variabled from inside the #asm #endasm tags? It would make my code a lot more readable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lee's reply is probably the most useful. Mimic the Wizard sequence.

I tend to have "Size Optimisation" on most of the time. It is the default. But this is clearly a case where size matters. You can always examine the LST or ASM file to see the generated code.

Re visible C: the Help article explains this. But in simple terms, an ASM global name is the C name with a prepended underscore. Local names are going to refer to a register or an entry in a Stack Frame. I suppose you could .def the register temporarily. But I would be wary of AvrAsm2.

The simplest thing is to inspect the library source code and mimic that.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The problem was, as cmhicks points out, that when writing the code in C, the code is simply not fast enough. I have four clock cycles after setting the WDCE bit, before the HW resets the bit again, and it was anyone's guess if I made it in time or not.

Quote:

Is it safe to say that my problem was in fact a compiler error then?

Using the approach from the Wizard-produced snippet, it will in fact meet the timing requirements every time. No guess from anyone needed.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But this is clearly a case where size matters. You can always examine the LST or ASM file to see the generated code.

If there is only one sequence in the code, "set it and forget it", then with CV it won't matter Size or Speed.

If, as in this case, there are multiple sequences in the code--OP wants to change the timing during operation--then the Size optimizer will create small functions for common code sequences, as long as as little as one word is saved. Thus, the protective manipulations in the preprocessor.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.