iwdg.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "iwdg.h"
  2. INT32U g_dog = 0;
  3. OS_EVENT *iwdg_mutex = NULL;
  4. // 初始化独立看门狗
  5. // prer:分频数:0~7(只有低3位有效!)
  6. // rlr:自动重装载值,0~0XFFF.
  7. // 分频因子=4*2^prer.但最大值只能是256!
  8. // rlr:重装载寄存器值:低11位有效.
  9. // 时间计算(大概):Tout=((4*2^prer)*rlr)/32 (ms).
  10. void iwdg_init(void)
  11. {
  12. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); // 使能对IWDG->PR IWDG->RLR的写
  13. IWDG_SetPrescaler(IWDG_Prescaler_128); // 设置IWDG分频系数
  14. IWDG_SetReload(0X9C4); // 设置IWDG装载值
  15. IWDG_ReloadCounter(); // reload
  16. IWDG_Enable(); // 使能看门狗
  17. }
  18. void iwdg_feed(INT32U dog)
  19. {
  20. INT8U err = 0;
  21. INT32U all_dog = UART3_DOG | MISC_DOG;
  22. // INT32U all_dog = CURRENT_DOG | SOC_DOG | ADC_DOG | INCAN_RX_DOG | INCAN_TX_DOG | INCAN_INFO_DOG | CAN2_TX_DOG | CAN2_RX_DOG | FAULT_DIG_DOG | UART1_DOG | UART3_DOG | BALANCE_DOG | MISC_DOG | PROCESS_DOG;
  23. OSMutexPend(iwdg_mutex, 0, &err);
  24. g_dog |= dog;
  25. OSMutexPost(iwdg_mutex);
  26. if (g_dog == all_dog)
  27. {
  28. /* 重载IWDG计数 */
  29. IWDG_ReloadCounter();
  30. OSMutexPend(iwdg_mutex, 0, &err);
  31. g_dog = 0;
  32. OSMutexPost(iwdg_mutex);
  33. }
  34. }
  35. void iwdg_disable(void)
  36. {
  37. }