본문 바로가기
STMicro STM32

STM32에서 IWDG(WatchDog) 사용하기

by 소나무기운 2022. 5. 12.
반응형

[2022/05/12] First Start.

 

소나무 기운 ,  전자제품 개발/생산

STM32에서 IWDG(WatchDog) 사용하기

STM32에는 두 가지의 watch dog이 있어요.

IWDG : Independant Watchdog, down counter가 있고 이 counter가 0이 되면 MCU가 reset이 됩니다. 그러니 0이 되기 전에 값을 새로운 값으로 채워 넣어야 합니다.

WWDG : Window Watchdog, counter값을 조정할 수 있는 구간이 있어서 좀 더 까다롭게 조건을 줄 수 있어요. 다음에 좀 알아보도록 하죠.

 

간단한 예제를 만들어 사용법을 알아보도록 할께요.

 

STM32 has two watch dogs.

IWDG : Independent Watchdog, down counter, and if the counter becomes 0, MCU isreset. Therefore, you must populate the new value before it reaches zero.

WWDG : Window Watchdog, There are sections where you can adjust the counter values, which can five you more difficult conditions. Let's find out later.

 

I'll make a simple example and see how to use it.

 

 

예제 프로젝트 기능 설명 ( Example Project Feature Description )

IWDG를 활성화하고 main 함수에서 주기적으로 IWDG를 clear 해 줍니다.

만약에 MCU에 문제(MCU가 멈추거나 소프트웨어가 무한루프에 빠졌을 경우)가 생겨서 IWDG를 clear 해 주지 못한다면 시스템은 IWDG에 의해서 reset 될 것입니다.

 

Enables IWDG and clears IWDG periodically in the main function.

if there is a problem with the MCU (when the CMU stops or the software falls into an infinite loop), and the IWDG cannot clear the IWDG, the system will be reset by the IWDG.

 

main 루틴이 반복하는 시간을 1초 이내로 되도록 프로그램을 설계하여 반복하도록 합니다. 이때 IWDG는 2초로 설정하도록 합니다. main 루틴에 문제가 발생하여 1초마다 진해하는 clear작업을 진행하지 못하면 2초가 되는 시점체 IWDG가 동작하여 MCU를 reset 합니다.

시간 값은 필요에 따라 변경합니다.

 

Design and repeat the program so that the main routine repeats within 1 second. Set the IWDG to 2 seconds. if there is a problem with the main routine and the clear action cannot be performed every 1 second, the 2 second IWDG will operate to reset the MCU.

Change the time value as needed.

 

 

IWDG 시간 계산 방법 ( To calculate IWDG time )

Clock Configuration을 보면 현재 IWDG에 사용하는 클럭을 볼 수 있습니다.If you look at the clock Configuration, you can see the clock currently used for the IWDG.

IWDG의 clock 40KHz

 

Clock : 40KHz

Prescaler : 4

Reload : 4095

일 경우에 계산방법은, calculation method is

 

40KHz의 주기 25mS입니다. Period 25mS at 40kHz.

4 * 4095 * 25mS = 409.500mS입니다.

 

그러니 409.5mS 보다 빠르게 reload값을 다시 넣어주어야 합니다.

So you have to put the reload value back in faster than 409.5mS

 

 

 

Cube IDE를 이용하여 IWDG 설정( Set up IWDG with Cube IDE )

STM32F103을 선택하여 NUCLEO보드의 MCU를 선택합니다.

Select STM32F103 to select the MCU of the NUCLEO board.

적용 디바이스

IWDG 항목을 설정할 수 있습니다.

You can set IWDG entries.

 

IWDG 설정 화면

설정할 것은 prescaler값과 reload value 값을 설정하게 됩니다.

당연히 Activated도 체크해 주어야겠지요.

나머지 옵션 값은 그대로 유지합니다.

 

What you want to set is the prescaler value and the reload value.

Of cures, you have to check Activated as well.

Leave the remaining option values intact.

 

저장을 하면 코드를 생성할 것인지를 물어봅니다.

생성된 코드를 분석해 보겠습니다.

 

Ask if you want to generate code when you save it's saved.

Let's analyze the generated code.

 

Main함수에서의 초기화 ( Initialization in the Main function )

Main함수에서는 IWDG를 초기화하는 코드가 삽입되어 있습니다.

The Main function inserts a code that initializes the IWDG.

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_IWDG_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

MX_IWDG_Init(); 함수가 호출되어 처리합니다.

MX_IWDG_Init(); function is called and processed.

 

좀 더 따라가 보죠. 아래와 같이 hiwdg구조체를 가지고 초기화하도록 되어 있습니다.

Let's keep up. It is supposed to be initialized with hiwdb structure as shown below.

/**
  * @brief IWDG Initialization Function
  * @param None
  * @retval None
  */
static void MX_IWDG_Init(void)
{

  /* USER CODE BEGIN IWDG_Init 0 */

  /* USER CODE END IWDG_Init 0 */

  /* USER CODE BEGIN IWDG_Init 1 */

  /* USER CODE END IWDG_Init 1 */
  hiwdg.Instance = IWDG;
  hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
  hiwdg.Init.Reload = 4095;
  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN IWDG_Init 2 */

  /* USER CODE END IWDG_Init 2 */

}

입력하는 내용으로는 Prescaler값과 Reload값으로 초기화합니다.

구조체를 살펴볼까요?

 

Initialize the contents as Prescaler value and Reload value.

Let's take a look at the structure.


/**
  * @brief  IWDG Init structure definition
  */
typedef struct
{
  uint32_t Prescaler;  /*!< Select the prescaler of the IWDG.
                            This parameter can be a value of @ref IWDG_Prescaler */

  uint32_t Reload;     /*!< Specifies the IWDG down-counter reload value.
                            This parameter must be a number between Min_Data = 0 and Max_Data = 0x0FFF */

} IWDG_InitTypeDef;

/**
  * @brief  IWDG Handle Structure definition
  */
typedef struct
{
  IWDG_TypeDef                 *Instance;  /*!< Register base address    */

  IWDG_InitTypeDef             Init;       /*!< IWDG required parameters */
} IWDG_HandleTypeDef;

인스턴스와 프리스케일러와 리로드 값으로 이루어졌습니다.

It consistes fo an istance and freescaler and relad value.

 

 

 

Main함수에서의 사용법 ( How to use in the Main Function )

사용방법은 아래와 같습니다.

 

메인 함수에서 주기적으로 호출되어 IWDG를 주기적으로 clear 해 줍니다.

main_process()에서 작업을 진행 중 빠져나오지 못하고 있다면 일정 시간 후에는 MCU는 Reset 될 것입니다.

 

Here is how to use it.

 

The main fuctions periodically clears the IWDG peridically.

The MCU reset after a certain period of time if the main_process() is unable to exit while working on it is in progress.

  while (1)
  {
    /* USER CODE END WHILE */
	  main_process();

    /* USER CODE BEGIN 3 */
	  HAL_IWDG_Refresh(&hiwdg);

  }

 

 

HAL 라이브러리의 내용을 보면 초기화했던 값으로 IWDG를 되돌리는 것입니다.

아래 소스를 보시면 됩니다.

 

If you look at the contents of the HAL library, it returns the IWDG to the value that you initialized.

You can look at the source below.

/**
  * @brief  Refresh the IWDG.
  * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
  *                the configuration information for the specified IWDG module.
  * @retval HAL status
  */
{
HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  /* Reload IWDG counter with value defined in the reload register */
  __HAL_IWDG_RELOAD_COUNTER(hiwdg);

  /* Return function status */
  return HAL_OK;
}

 

마무리

Watchdog은 혹시 모르는 시스템의 정지 현상 등을 개선하기 위하여 신뢰성을 높이기 위해서 사용합니다.

시스템의 정지 현상은 여러 가지 이유에서 올 수 있습니다.

하나는 하드웨어 적인 요소입니다. 외부에서 정전기 등의 노이즈가 발생하거나 외부 핀들의 입력 조건이 맞지 않아서 소프트웨어가 더 이상 진행되지 못하는 경우입니다.

 

두 번째는 소프트웨어 적인 요소입니다. 위에서 언급한 입력 조건에 따른 소프트웨어 대응 미숙으로 무한 루프에 갖추게 됩니다. 소프트웨어의 조건 판단 실수로 빠져나오지 못하는 경우입니다.

 

개발자가 생각하지 못한 문제의 발생 시 보완하는 역할을 하게 됩니다.

 

Watchdos is used to increase reliability to improbe system outages.

System outaghes can come for a number of reasons.

 

One is the hardware component. This is when there is noise from the electostatic lamp outside or the software cannot proceed any further because the input conditions fo the external pins do not meet.

 

The second is the software element. Due to the inexperience in responding to the software according to the input conditions mentioned above, you will be stuck in an infinite loop. This is a case where the software cannot escape due to a conditional error.

 

It serves to supplement proglems that developers have not thought of.

 

 

참고문헌

 

 

 
 

 

 

틀린 부분이나 질문은 댓글 달아주세요.

즐거운 하루 보내세요. 감사합니다.

 

 

반응형

'STMicro STM32' 카테고리의 다른 글

STM32Cube IDE output HEX file  (0) 2023.09.07
STM32CubeIDE 단축키 정리  (0) 2022.01.04
LTC2943 - Multicell Battery Gas Gauge  (0) 2021.12.12
Two ways to Flash loader with UART(RS232)  (0) 2021.12.01
[TMP117] c sample code ( stm32용 )  (0) 2021.10.30

댓글