summaryrefslogtreecommitdiffstats
path: root/uc_str912/prj_template91x/str91x_lib/src/91x_wdg.c
blob: f933635d01222ab8aa147fbe07c9b403f1b3d454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 91x_wdg.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file provides all the WDG software functions.
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "91x_wdg.h"
#include "91x_scu.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/


/* WDG End of Count interrupt Flag */
#define WDG_FLAG_EC  0x0001


/* WDG End of Count interrupt request */
#define WDG_IT_EC    0x0001



/* WDG Start/Stop counter */
#define WDG_Counter_Start  0x0002
#define WDG_Counter_Stop   0xFFFD


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Registers reset value */
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************
* Function Name  : WDG_DeInit
* Description    : Deinitializes the WDG peripheral registers to their default
*                  reset values.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void WDG_DeInit(void)
{

  SCU_APBPeriphReset(__WDG, ENABLE);  /*WDG peripheral under Reset */
  SCU_APBPeriphReset(__WDG, DISABLE);  /*WDG peripheral Reset off*/
  
}

/*******************************************************************************
* Function Name  : WDG_StructInit
* Description    : Fills the WDG_InitTypeDef structure member with its reset
*                  value.
* Input          : WDG_InitStruct : pointer to a WDG_InitTypeDef structure
*                  which will be initialized.
* Output         : None
* Return         : None
*******************************************************************************/
void WDG_StructInit(WDG_InitTypeDef *WDG_InitStruct)
{
  /* Select the Watchdog running mode*/
  WDG_InitStruct->WDG_Mode = WDG_Mode_Timer;

  /* Select the source clock */
  WDG_InitStruct-> WDG_ClockSource = WDG_ClockSource_Apb;

   /* Initialize Prescaler */
  WDG_InitStruct->WDG_Prescaler =0xFF;

  /* Initialize Preload */
  WDG_InitStruct->WDG_Preload =0xFFFF;


}

/*******************************************************************************
* Function Name  : WDG_Init
* Description    : Initializes WDG  peripheral according to the specified
*                  parameters in the WDG_InitStruct.
* Input          : WDG_InitStruct: pointer to a WDG_InitTypeDef structure that
*                  contains the configuration information for the WDG peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void WDG_Init(WDG_InitTypeDef* WDG_InitStruct)
{


 if(WDG_InitStruct->WDG_ClockSource == WDG_ClockSource_Apb)
  {
    /* Select The APB clock as clock source */
    WDG->CR &= WDG_ClockSource_Apb;
  }

  else
  {
    /* Select the RTC clock as source */
    WDG->CR |= WDG_ClockSource_Rtc ;
  }


  /* Configure WDG Prescaler register value */
  WDG->PR = WDG_InitStruct->WDG_Prescaler;

  /* Configure WDG Pre-load register value */
  WDG->VR = WDG_InitStruct->WDG_Preload ;


  if(WDG_InitStruct->WDG_Mode == WDG_Mode_Timer)
  {
    /* Select Timer mode */
    WDG->CR &= WDG_Mode_Timer;
  }
  else
  {
    /* Select WDG mode */
    WDG->CR |= WDG_Mode_Wdg ;
  }


}

/*******************************************************************************
* Function Name  : WDG_Cmd
* Description    : Enables or disables the WDG peripheral.
* Input          : NewState: new state of the WDG peripheral (Newstate can be
*                  ENABLE or DISABLE)
* Output         : None
* Return         : None
*******************************************************************************/
void WDG_Cmd(FunctionalState NewState )
{
  if((WDG->CR & WDG_Mode_Wdg) == 0)
  {
    /* Timer mode */
    if(NewState == ENABLE)
    {
      /* Start timer by setting SC bit in Control register */
      WDG->CR |= WDG_Counter_Start;
    }
    else
    {
      /* Stop timer by clearning SC bit in Control register */
      WDG->CR &= WDG_Counter_Stop;
    }
  }
  else
  {
    /* Watchdog mode */
    if(NewState == ENABLE)
    {
      WDG->KR = WDG_KeyValue1;
      WDG->KR = WDG_KeyValue2;
    }
  }
}

/*******************************************************************************
* Function Name  : WDG_ITConfig
* Description    : Enables or disables the WDG End of Count(EC) interrupt.
* Input          : Newstate:  new state of the End of Count(EC) WDG interrupt.
*                  This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void WDG_ITConfig(FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
    /* Enable the End of Count interrupt */
    WDG->MR |= WDG_IT_EC;
  }
  else
  {
    /* Disable the End of Count interrupt */
    WDG->MR &= ~WDG_IT_EC;
  }
}

/*******************************************************************************
* Function Name  : WDG_GetCounter
* Description    : Gets the WDGs current counter value.
* Input          : None
* Output         : None
* Return         : The WDG current counter value
*******************************************************************************/
u16 WDG_GetCounter(void)
{
   return WDG->CNT;
}




/*******************************************************************************
* Function Name  : WDG_GetITStatus
* Description    : Checks whether the WDG End of Count(EC) interrupt is occured or not.
* Input          : None
* Output         : None
* Return         : The new state of WDG_IT (SET or RESET).
*******************************************************************************/
ITStatus WDG_GetITStatus(void)
{
  if(((WDG->SR & WDG_IT_EC) != RESET )&&((WDG->MR & WDG_IT_EC) != RESET ))
  {
    return SET;
  }
  else
  {
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : WDG_ClearITPendingBit
* Description    : Clears the WDG's End of Count(EC) interrupt pending bit.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void WDG_ClearITPendingBit(void)
{
 /* Clear the EC pending bit */
  WDG->SR &= ~WDG_IT_EC;

}

/*******************************************************************************
* Function Name  : WDG_ClearFlag
* Description    : Clears the WDG's End of Count(EC) Flag.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void WDG_ClearFlag(void)
{
 /* Clear the EC Flag */

  WDG->SR &= ~WDG_FLAG_EC;

}


/*******************************************************************************
* Function Name  : WDG_GetFlagStatus
* Description    : Checks whether the WDG End of Count(EC) flag is set or not.
* Input          : None
* Output         : None
* Return         : The new state of the WDG_FLAG (SET or RESET).
*******************************************************************************/
FlagStatus WDG_GetFlagStatus(void)
{
  if((WDG->SR & WDG_FLAG_EC) != RESET )
  {
    return SET;
  }
  else
  {
    return RESET;
  }
}



/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
OpenPOWER on IntegriCloud