How to write a custom reset() function in C for AVR-Studio?

六月ゝ 毕业季﹏ 提交于 2019-12-24 05:04:08

问题


so I've been assigned the task of writing a custom reset() function for an AVR compilation.

I'm given this info - "Atmega128 and Pic24e have the reset interrupt at the program address 0x0. Write a function reset() to reset a program. I've also heard an easy way to force your system to reboot is by sending it into an infinite loop.

Call me crazy, but is this as simple as writing:

function reset(){
  goto 0x00000000;
}

Not sure if you can do that, and not looking for a complex explanation, as I'm pretty sure this is a generic question. Short and sweet if you can :)


回答1:


goto 0x00000000 starts the program over but with all SFR not initialized, and the interrupts not initialized. Errors may happen depending on the complexity of your code. You should not use goto for software reset, that's bad way.

Instead AVR Libc Reference Manual specifies the usage of watchdog timer for software reset. By using avr/wdt you could easily enable watchdog timer.

#include <avr/wdt.h>

#define soft_reset()        \
do                          \
{                           \
    wdt_enable(WDTO_15MS);  \
    for(;;)                 \
    {                       \
    }                       \
} while(0)

from AVR Libc

CAUTION! Older AVRs will have the watchdog timer disabled on a reset. For these older AVRs, doing a soft reset by enabling the watchdog is easy, as the watchdog will then be disabled after the reset. On newer AVRs, once the watchdog is enabled, then it stays enabled, even after a reset! For these newer AVRs a function needs to be added to the .init3 section (i.e. during the startup code, before main()) to disable the watchdog early enough so it does not continually reset the AVR.

To disable watchdog at start time.

#include <avr/wdt.h>

// Function Pototype
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));


// Function Implementation
void wdt_init(void)
{
    MCUSR = 0;
    wdt_disable();

    return;
}

.init3 is executed before main function, look at Memory Sections for more details.

I'm given this info - "Atmega128 and Pic24e have the reset interrupt at the program address 0x0.

For most cases yes, but if you are using bootloader, the start address may defers.



来源:https://stackoverflow.com/questions/32802221/how-to-write-a-custom-reset-function-in-c-for-avr-studio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!