Author Topic: [Semi Beginner] Please explain the difference between these AVR watchdog modes  (Read 6264 times)

0 Members and 1 Guest are viewing this topic.

Offline microbugTopic starter

  • Frequent Contributor
  • **
  • Posts: 563
  • Country: gb
  • Electronics Enthusiast
I am working with the ATtiny10. I have used the Arduino before and have some general programming knowledge, this is my first non-Arduino MCU program. The project will run on a CR2032 battery so it needs to have a very low power consumption. I have already turned off the ADC, comparator, timer and digital input buffers so that the power consumption is lower when active, and now I am trying to get a very low power 'sleep' mode (called power-down by the datasheet to distinguish from other modes).

What I want the MCU to do is go into power-down for 8 seconds (WDP3..0 = 0b1001) with the watchdog timer running. The watchdog should wake it up, check whether it has gone into power-down enough times, and if so, make a sound before going back to power-down. If not, just go back to power-down.

The watchdog can only run for a max of 8 seconds.

I have attached a screengrab from the ATtiny10 datasheet. From it, it seems like what I need is WDE = 0 and WDIE = 1 to enable interrupt mode, but I don't understand the difference between what the datasheet calls 'Interrupt Mode' and 'Interrupt and System Reset Mode'.

Can anyone explain in a more accessible way than the datasheet does?
 

Offline bobcat

  • Regular Contributor
  • *
  • Posts: 94
  • Country: us
Interrupt mode triggers the watchdog interrupt only.
Reset mode resets the MPU like at power on.
 

Offline ralphd

  • Frequent Contributor
  • **
  • Posts: 445
  • Country: ca
    • Nerd Ralph
This explains it better than I can...
https://www.sparkfun.com/tutorials/309
Unthinking respect for authority is the greatest enemy of truth. Einstein
 

Offline ralphd

  • Frequent Contributor
  • **
  • Posts: 445
  • Country: ca
    • Nerd Ralph
And I'd avoid the ramless tinies, as they are not well supporred by gcc.
The tiny13a is cheap (30c in small qty), and well supported by gcc and avr-libc.
Unthinking respect for authority is the greatest enemy of truth. Einstein
 

Offline microbugTopic starter

  • Frequent Contributor
  • **
  • Posts: 563
  • Country: gb
  • Electronics Enthusiast
Thanks for the tutorial!

It's a bit late for me to change MCU, as I'm programming the finished hardware. That said, if I run into problems with RAM size / program space, I might make a new set of PCBs and get a different ATtiny. I don't see much difference between the ATtiny13a and 10 -- just that the ATtiny10 has 32 bytes of RAM instead of 64.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Quote
tiny13a is cheap (30c in small qty)
Where?  Most places seem to want about twice that.  (Wah!  I want my two-bit ($0.25) attiny11s back!)

Unless you're building lots, I'd spring for a tiny85.

But the "brain dead" attinys DO have RAM these days.  They got rid of some registers, but a tiny10 has a whole 32bytes of RAM as well.
I think just because Atmel wanted to appease the C users.

WDT Interrupt means cause an interrupt.  Reset mode causes a reset.
"Interrupt AND reset" mode will have the first WDT expiration cause an interrupt, and if you don't tickle the WDT properly, it will cause a RESET the second time the timer expires.
 

Offline microbugTopic starter

  • Frequent Contributor
  • **
  • Posts: 563
  • Country: gb
  • Electronics Enthusiast
Thanks for the clarification, westfw. As it happens, I do have some ATtiny85s in PDIP packages, so if this goes horribly wrong on the ATtiny10 then I could redo the PCBs and get the program working on the ATtiny85s in the meantime. That would be a bit of a pain though as I'd have to get some ATtiny85s in MLF packages as well — space is a limiting factor here.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
The Attiny10 are great for LED blinkies. But using C is a challenge. You must use some tricks:
- You must attribute main to not stack any return frames. You'll lose valuable ram if you don't.
- You shouldn't call any routine.
- You shouldn't use nested interrupts.
- Interrupts should not use stack. (no locals)

And you'll need some knowledge of assembler, because you will need to inspect the compiler output if it doesn't work.

Last but not least, do not upgrade compiler for the project ever again when the program is finished. A newer compiler can generate completely different output that won't work anymore.
Keep a backup of the compiler toolchain.
 

Offline microbugTopic starter

  • Frequent Contributor
  • **
  • Posts: 563
  • Country: gb
  • Electronics Enthusiast
I think I am just about sorted for this project now, since the code is all working and is nearly finished.

 Just to clarify:
- What do you mean by 'stack return frames'? Do you just mean that main should return void?
- What is a routine? Should I just avoid libraries if I can code it myself (e.g., implement sleep mode / watchdog without their respective libraries)?
- Why shouldn't interrupts use the stack (they don't in my code, anyway)?

Thanks for the info!
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
- It is not necessary for main to have a return address since it won't ever return. Saves you a costly 2 bytes (iirc) of stack.
- Any jump to a subroutine or function causes a stackframe with a return address and register stacking. The ATTiny10 does not have much room for this in the 32 byte volatile memory.
You can use inlines or macro's.
- As I said, there is only 32 bytes of "ram", keep an eye for stack overflows.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Quote
Any jump to a subroutine or function causes a stackframe with a return address and register stacking. The ATTiny10 does not have much room for this in the 32 byte volatile memory.
I disagree with your missive to avoid function calls.  You have 32 bytes of RAM, and that's pretty much what it's for.  In total, 16 registers and 32 bytes of RAM compares very favorably with other ultra-small uCs (like the PIC10F206, which has 1 register, 24bytes of RAM, and a 2-word return stack.")  The usual avr-gcc function call only pushes the return address; the ABI and register allocator prevent the need for stack frames or register stacking (usually.)  So you should be able to have a "reasonable" depth of function calling without problems.  The compiler may inline your functions anyway.

You do especially have to watch interrupts, since those WILL normally stack extra registers (and not very efficiently! :-()  Especially avoid calling other functions from ISRs.

(All for avr-gcc; other compilers may vary!)

Quote
you will need to inspect the compiler output
Always a good idea for very limited systems!

Quote
do not upgrade compiler for the project ever again
Well, you CAN, but you'll need to repeat that "inspect the compiler output" step again.  And hope that you commented the code well enough that you can modify it as needed.
 

Offline ralphd

  • Frequent Contributor
  • **
  • Posts: 445
  • Country: ca
    • Nerd Ralph
The tiny4/5/9/10 are a different architecture than the others.  Some explaination here:
https://github.com/cpldcpu/u-wire/blob/master/README.md
Unthinking respect for authority is the greatest enemy of truth. Einstein
 

Offline ralphd

  • Frequent Contributor
  • **
  • Posts: 445
  • Country: ca
    • Nerd Ralph
Unthinking respect for authority is the greatest enemy of truth. Einstein
 

Offline ralphd

  • Frequent Contributor
  • **
  • Posts: 445
  • Country: ca
    • Nerd Ralph
The tiny4/5/9/10 also have a funky programming mode so a $2 usbasp wont work like it does for all the other attinies and atmegas.
Unthinking respect for authority is the greatest enemy of truth. Einstein
 

Offline ralphd

  • Frequent Contributor
  • **
  • Posts: 445
  • Country: ca
    • Nerd Ralph
Quote
tiny13a is cheap (30c in small qty)
Where?  Most places seem to want about twice that.  (Wah!  I want my two-bit ($0.25) attiny11s back!)

Unless you're building lots, I'd spring for a tiny85.

I just did a quick search on Aliexpress for the tiny85-ssu, and for 659c you can't even get 10.  That would cost you a little over $8.  If I need more than the tiny13, the t85 is OK, but for $15 you can get 10 Atmega168 Pro Minis with hardware SPI, UART, lots more IO, ...
Unthinking respect for authority is the greatest enemy of truth. Einstein
 

Offline microbugTopic starter

  • Frequent Contributor
  • **
  • Posts: 563
  • Country: gb
  • Electronics Enthusiast
Actually, if you update the firmware (which I've always failed at doing) on the USBASP then it does support TPI. You can buy ones where the seller has updated the firmware (at an additional cost!). I'm using an Arduino sketch which I found online; seems to be working fine.

Aliexpress can be hit and miss for parts (I have got 10 Pro Minis using 328s which work fine). Since I would only need to respin the PCBs if I switched to an MSP430, I'd probably just get some samples from TI rather than rack up a sub-£20 delivery charge from Farnell.
« Last Edit: June 01, 2015, 03:45:29 am by microbug »
 

Offline ralphd

  • Frequent Contributor
  • **
  • Posts: 445
  • Country: ca
    • Nerd Ralph
On Aliexpress I usually buy one or two at first from sellers if I am unsure of the product.  And I rarely buy anything worth more than $10.  If something is $16 on aliexpress vs $18 or even $20 from Newark or Mouser, then Ill get it from the official distributor.
Unthinking respect for authority is the greatest enemy of truth. Einstein
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf