S32K1xx Series Reference Manual
的pdf文档23章给出了看门狗的框图:
先不用细看, 下面结合PE配置看. 本文中用到LPUART2(PTA8, PTA9)
, 用的J-Link调试, S32K148_SDK 3.0.0.
打开S32 Design Studio for ARM, 接下来步骤:
双击工程名, 点击Components窗口Components目录下的 pin_mux:PinSetting
, LPUART中选择PTA8/PTA9, 并设置发送为Output:
Component Library中双击添加lpuart
组件, 配置lpuart, 选择LPUART2, 波特率115200:
Component Library中双击添加wdog
组件, 配置wdog:
WDOG_LPO_CLOCK
(128kHz默认, 可到clock_manager
中修改为32kHz或者1kHz)clock cycle
就变成了500Hz)Window Value~Timeout Value
之间才能喂狗, 条件苛刻)Timeout Value
为500, 也就是1s内喂狗就不会复位了, 注意Window Value
和Timeout Value
都是16位计数器, 值必须在[1, 65535], 否则无效(回到之前的值)添加LPUART2
的printf
支持:
/* User includes (#include below this line is not maintained by Processor Expert) */
#include
#include "ewl_misra_types.h"
#include "file_struc.h"
int_t __write_console(__file_handle handle, uchar_t * buffer, size_t * count)
{
uint32_t bytesRemain;
size_t bytes=*count;
uchar_t ret_car[]={"\r"};
LPUART_DRV_SendData(INST_LPUART2, buffer, bytes);
while(LPUART_DRV_GetTransmitStatus(INST_LPUART2, &bytesRemain) != STATUS_SUCCESS);
LPUART_DRV_SendData(INST_LPUART2, ret_car, 1);
while(LPUART_DRV_GetTransmitStatus(INST_LPUART2, &bytesRemain) != STATUS_SUCCESS);
return 0;
}
main函数中系统和引脚的初始化:
/* Write your code here */
/* For example: for(;;) { } */
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
串口的初始化:
LPUART_DRV_Init(INST_LPUART2, &lpuart2_State, &lpuart2_InitConfig0);
看门狗初始化和喂狗:
/* Initialize WDOG */
WDOG_DRV_Init(INST_WATCHDOG1, &watchdog1_Config0);
printf("watchdog enable!\r\n");
while (1)
{
printf("xixiheheheihei\r\n");
for (uint32_t i = 0; i < 2989000*2; i++); //about 2s
/* Reset Watchdog counter */
WDOG_DRV_Trigger(INST_WATCHDOG1);
}
2s后喂狗, 肯定来不及, 系统会复位, 调试运行试试:
可以看到, 约1s系统复位一次. 现在把延时改成约0.5s, 这样狗就能喂上了:
/* Initialize WDOG */
WDOG_DRV_Init(INST_WATCHDOG1, &watchdog1_Config0);
printf("watchdog enable!\r\n");
while (1)
{
printf("xixiheheheihei\r\n");
for (uint32_t i = 0; i < 2989000/2; i++); //about 0.5s
/* Reset Watchdog counter */
WDOG_DRV_Trigger(INST_WATCHDOG1);
}
https://download.csdn.net/download/weifengdq/12011905