1.目的
设置看门狗
2.分析
在项目中经常使用看门狗,当系统出错了,使系统复位。
3.平台:
协议栈版本:SDK8.0.0
编译软件:keil 5.14
硬件平台:nrf51822最小系统
例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4
4.步骤
#ifndef __WDT__H
#define __WDT__H
void wdt_start(void);
void wdt_stop(void);
void wdt_feed(void);
void wdt_init(void);
#endif
/* Copyright (c) [2014 Baidu]. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File Name :
* Author :
* Version : $Revision:$
* Date : $Date:$
* Description :
*
* HISTORY:
* Date | Modification | Author
* 28/03/2014 | Initial Revision |
*/
#include "nrf51.h"
#include "nrf51_bitfields.h"
#define RELOAD_COUNT (32768*60*3-1) //3 minutes
void wdt_init(void)
{
NRF_WDT->TASKS_START = 0;
NRF_WDT->CRV = RELOAD_COUNT;
NRF_WDT->CONFIG =
WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos |
WDT_CONFIG_SLEEP_Pause << WDT_CONFIG_SLEEP_Pos;
NRF_WDT->RREN = WDT_RREN_RR0_Enabled << WDT_RREN_RR0_Pos;
}
void wdt_start(void)
{
NRF_WDT->TASKS_START = 1;
}
void wdt_feed(void)
{
if(NRF_WDT->RUNSTATUS & WDT_RUNSTATUS_RUNSTATUS_Msk)
NRF_WDT->RR[0] = WDT_RR_RR_Reload;
}
void wdt_stop(void)
{
NRF_WDT->TASKS_START = 0;
}
上面 我们 设置看看门狗
1.溢出时间为3s
2.debuger仿真的时候看门狗停止
3.CPU睡眠的时候看门狗停止
注意:1.看门狗只要一启动就不能关闭了。不同通过wdt_stop()来关闭
2.如果在app启动了定时器。那在空中升级的程序也必须喂狗。
在main.c中
<strong>wdt_start();</strong>
// Enter main loop
for (;;) {
app_sched_execute();
<strong> wdt_feed();
</strong> power_manage();
}
这样系统不就复位了
注意:本人在操作flash后,马上初始化看门狗,开狗,看门狗居然没有用。。。不知道什么问题
2.开看门狗中断
wdt_init(void) 变为如下
void wdt_init(void)
{
NRF_WDT->TASKS_START = 0;
NRF_WDT->CRV = RELOAD_COUNT;
NRF_WDT->CONFIG =
WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos |
WDT_CONFIG_SLEEP_Run << WDT_CONFIG_SLEEP_Pos;
NRF_WDT->RREN = WDT_RREN_RR0_Enabled << WDT_RREN_RR0_Pos;
//¿ª¿´ÃŹ·ÖжÏ
NRF_WDT->INTENSET = WDT_INTENSET_TIMEOUT_Msk;
NVIC_ClearPendingIRQ(WDT_IRQn);
NVIC_SetPriority(WDT_IRQn, APP_IRQ_PRIORITY_LOW);
NVIC_EnableIRQ(WDT_IRQn);
}
void WDT_IRQHandler(void)
{
__NOP();
}
这样就可以看门狗溢出就会产生中断。。。