【K70EK_T7_MQX例程】015Watchdog(看门狗)

By Mcuzone

调试串口波特率:115200-uart2
基于MDKK70-EK_T7硬件平台

本例程先创建了一个看门狗定时器,循环加长test任务的时间,直到看门狗定时器溢出。
#define MAIN_TASK       10
#define TEST_TASK       11

extern void main_task(uint_32);
extern void test_task(uint_32);
extern void handle_watchdog_expiry(pointer);


const TASK_TEMPLATE_STRUCT  MQX_template_list[] = 
{
   /* Task Index, Function,   Stack, Priority,  Name,       Attributes,          Param, Time Slice */
    { MAIN_TASK,  main_task,  4000,  8,         "Main",     MQX_AUTO_START_TASK, 0,     0 },
    { TEST_TASK,  test_task,  4000,  9,         "Test",     0,                   0,     0 },
    { 0 }
};

LWSEM_STRUCT shutdown_sem;



/*FUNCTION*------------------------------------------------------
*
* Function Name  : handle_watchdog_expiry
* Returned Value : none
* Comments       :
*     This function is called when a watchdog has expired.
*END*-----------------------------------------------------------*/

void handle_watchdog_expiry
   (
      pointer td_ptr
   )
{
  _lwsem_post(&shutdown_sem);
}

/*FUNCTION*------------------------------------------------------
*
* Function Name  : waste_time
* Returned Value : input value times 10
* Comments       :
*     This function loops the specified number of times,
*     essentially wasting time.
*END*-----------------------------------------------------------*/

static _mqx_uint waste_time
   (
      _mqx_uint n
   )
{
   _mqx_uint        i;
   volatile uint_32 result;

   result = 0;
   for (i = 0; i < n; i++) {
      result += 1;
   }
   return result * 10;
}


/*TASK*----------------------------------------------------------
*
* Task Name : test_task
* Comments  : 
*   This task creates a watchdog, then loops, performing
*   work for longer and longer periods until the watchdog fires.
*END*-----------------------------------------------------------*/

void test_task
   (
      uint_32 initial_data
   )
{
   MQX_TICK_STRUCT ticks;
   _mqx_uint       result;
   _mqx_uint       n;

   _time_init_ticks(&ticks, 10);

   result = _watchdog_create_component(BSP_TIMER_INTERRUPT_VECTOR, 
      handle_watchdog_expiry);
   if (result != MQX_OK) {
      printf("\nError creating watchdog component.");
      _task_block();
   }

   n = 100;
   while (TRUE) {
      result = _watchdog_start_ticks(&ticks);
      n = waste_time(n);
      _watchdog_stop();
      printf("\n %d", n);
   }

}

/*TASK*----------------------------------------------------------
*
* Task Name : main_task
* Comments  : 
*   This task creates test task and waits until on semaphore from watchdog.
*   In case the watchdog fires, it performs action:
*          (prints "Watchdog expired" and shutdown mqx)
*   
*END*-----------------------------------------------------------*/

void main_task
   (
      uint_32 initial_data
   )
{
  _task_id test_task;
  
   _lwsem_create(&shutdown_sem, 0);
   test_task = _task_create(0, TEST_TASK,0);
   
   
   _lwsem_wait(&shutdown_sem);
   _task_destroy(test_task);
   
   printf("\nWatchdog expired");

   _task_block();
}






例程及示例:

链接: http://pan.baidu.com/s/1pJK04oN 密码: r19f
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值