Subversion Repositories NaviCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
2
* File Name          : 91x_gpio.c
3
* Author             : MCD Application Team
4
* Date First Issued  : 05/18/2006 : Version 1.0
5
* Description        : This file provides all the GPIO software functions.
6
********************************************************************************
7
* History:
8
* 05/22/2007 : Version 1.2
9
* 05/24/2006 : Version 1.1
10
* 05/18/2006 : Version 1.0
11
********************************************************************************
12
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
14
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
15
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
16
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
17
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18
*******************************************************************************/
19
 
20
/* Includes ------------------------------------------------------------------*/
21
#include "91x_gpio.h"
22
#include "91x_scu.h"
23
 
24
/* Private typedef -----------------------------------------------------------*/
25
/* Private define ------------------------------------------------------------*/
26
 
27
/* Private macro -------------------------------------------------------------*/
28
/* Private variables ---------------------------------------------------------*/
29
/* Private function prototypes -----------------------------------------------*/
30
   static u8 GPIO_GetGPIONumber(GPIO_TypeDef* GPIOx);
31
   static u16 GPIO_GetAnaloClearBits(u8 GPIO_ANAChannel);
32
 
33
/* Private functions ---------------------------------------------------------*/
34
 
35
/*******************************************************************************
36
* Function Name  : GPIO_DeInit
37
* Description    : Deinitializes the GPIOx peripheral registers to their default
38
*                  reset values.
39
* Input          : GPIOx: where x can be (0..9) to select the GPIO peripheral.
40
* Output         : None
41
* Return         : None
42
*******************************************************************************/
43
void GPIO_DeInit(GPIO_TypeDef* GPIOx)
44
{
45
  /* Reset the GPIO registers values */
46
  if(GPIOx == GPIO0)
47
  {
48
    SCU_APBPeriphReset(__GPIO0,ENABLE);
49
    SCU_APBPeriphReset(__GPIO0,DISABLE);
50
    SCU->GPIOTYPE[0x00] = 0x0000 ;
51
    SCU->GPIOOUT[0x00]  = 0x0000;
52
    SCU->GPIOIN[0x00]   = 0x0000;
53
  }
54
 
55
   if(GPIOx == GPIO1)
56
  {
57
    SCU_APBPeriphReset(__GPIO1,ENABLE);
58
    SCU_APBPeriphReset(__GPIO1,DISABLE);
59
    SCU->GPIOTYPE[0x01] = 0x0000 ;
60
    SCU->GPIOOUT[0x01]  = 0x0000;
61
    SCU->GPIOIN[0x01]   = 0x0000;
62
  }
63
 
64
   if(GPIOx == GPIO2)
65
  {
66
    SCU_APBPeriphReset(__GPIO2,ENABLE);
67
    SCU_APBPeriphReset(__GPIO2,DISABLE);
68
    SCU->GPIOTYPE[0x02] = 0x0000 ;
69
    SCU->GPIOOUT[0x02]  = 0x0000;
70
    SCU->GPIOIN[0x02]   = 0x0000;
71
  }
72
 
73
   if(GPIOx == GPIO3)
74
  {
75
    SCU_APBPeriphReset(__GPIO3,ENABLE);
76
    SCU_APBPeriphReset(__GPIO3,DISABLE);
77
    SCU->GPIOTYPE[0x03] = 0x0000 ;
78
    SCU->GPIOOUT[0x03]  = 0x0000;
79
    SCU->GPIOIN[0x03]   = 0x0000;
80
  }
81
 
82
   if(GPIOx == GPIO4)
83
  {
84
    SCU_APBPeriphReset(__GPIO4,ENABLE);
85
    SCU_APBPeriphReset(__GPIO4,DISABLE);
86
    SCU->GPIOTYPE[0x04] = 0x0000 ;
87
    SCU->GPIOOUT[0x04]  = 0x0000;
88
    SCU->GPIOIN[0x04]   = 0x0000;
89
    SCU->GPIOANA = 0x00;
90
  }
91
 
92
   if(GPIOx == GPIO5)
93
  {
94
    SCU_APBPeriphReset(__GPIO5,ENABLE);
95
    SCU_APBPeriphReset(__GPIO5,DISABLE);
96
    SCU->GPIOTYPE[0x05] = 0x0000 ;
97
    SCU->GPIOOUT[0x05]  = 0x0000;
98
    SCU->GPIOIN[0x05]   = 0x0000;
99
  }
100
 
101
   if(GPIOx == GPIO6)
102
  {
103
    SCU_APBPeriphReset(__GPIO6,ENABLE);
104
    SCU_APBPeriphReset(__GPIO6,DISABLE);
105
    SCU->GPIOTYPE[0x06] = 0x0000 ;
106
    SCU->GPIOOUT[0x06]  = 0x0000;
107
    SCU->GPIOIN[0x06]   = 0x0000;
108
  }
109
 
110
   if(GPIOx == GPIO7)
111
  {
112
    SCU_APBPeriphReset(__GPIO7,ENABLE);
113
    SCU_APBPeriphReset(__GPIO7,DISABLE);
114
    SCU->GPIOTYPE[0x07] = 0x0000;
115
    SCU->GPIOOUT[0x07]  = 0x0000;
116
    SCU->GPIOIN[0x07]   = 0x0000;
117
  }
118
 
119
   if(GPIOx == GPIO8)
120
  {
121
    SCU_APBPeriphReset(__GPIO8,ENABLE);
122
    SCU_APBPeriphReset(__GPIO8,DISABLE);
123
    SCU->GPIOTYPE[0x08] = 0x0000;
124
    SCU->GPIOEMI = 0x00;
125
  }
126
 
127
   if(GPIOx == GPIO9)
128
  {
129
    SCU_APBPeriphReset(__GPIO9,ENABLE);
130
    SCU_APBPeriphReset(__GPIO9,DISABLE);
131
    SCU->GPIOTYPE[0x09] = 0x0000;
132
    SCU->GPIOEMI = 0x00;
133
  }
134
}
135
/*******************************************************************************
136
* Function Name  : GPIO_Init
137
* Description    : Initializes the GPIOx peripheral according to the specified
138
*                  parameters in the GPIO_InitStruct .
139
* Input          :- GPIOx: where x can be (0..9) to select the GPIO peripheral.
140
*                 - GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
141
*                   contains the configuration information for the specified GPIO
142
*                   peripheral.
143
* Output         : None
144
* Return         : None
145
*******************************************************************************/
146
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
147
{
148
  /* Select pin direction */
149
  u8 PinNumber = 0;
150
  u8 Counter = 0;
151
  u8 GPIO_Number = 0;
152
 
153
  GPIO_Number = GPIO_GetGPIONumber(GPIOx);
154
 
155
  if(GPIO_InitStruct->GPIO_Direction == GPIO_PinOutput)
156
  {
157
  GPIOx->DDR |= GPIO_InitStruct->GPIO_Pin;
158
  }
159
  else
160
  {
161
   GPIOx->DDR &= ~GPIO_InitStruct->GPIO_Pin;
162
  }
163
 
164
   for (Counter = 0; Counter < 8;Counter++)
165
    {
166
     /*Search pin number*/
167
     PinNumber = (GPIO_InitStruct->GPIO_Pin & (1 <<Counter));
168
     if((PinNumber >> Counter) == 1)
169
     {
170
        if (GPIO_Number < 8)
171
        {
172
          /*Output ALternate 0*/
173
          SCU->GPIOOUT[GPIO_Number] &= ~(0x3 <<(Counter *2));
174
          if(GPIO_InitStruct->GPIO_Alternate == GPIO_OutputAlt1)
175
          {
176
            /*Output ALternate 1*/
177
            SCU->GPIOOUT[GPIO_Number] |= 1 << (Counter *2);
178
          }
179
          if(GPIO_InitStruct->GPIO_Alternate == GPIO_OutputAlt2)
180
          {
181
            /*Output ALternate 2*/
182
            SCU->GPIOOUT[GPIO_Number] |= 0x2 << (Counter *2);
183
          }
184
          if(GPIO_InitStruct->GPIO_Alternate == GPIO_OutputAlt3)
185
          {
186
            /*Output ALternate 3*/
187
            SCU->GPIOOUT[GPIO_Number] |= 0x3 << (Counter *2);
188
          }
189
 
190
          /*IP Connected disable*/
191
          SCU->GPIOIN[GPIO_Number] &= ~(0x1 << Counter) ;
192
          if(GPIO_InitStruct->GPIO_IPConnected == GPIO_IPConnected_Enable)
193
          {
194
            /*IP Connected enable*/
195
            SCU->GPIOIN[GPIO_Number] |= 0x1 << Counter;
196
          }
197
        }
198
 
199
       /*Type configuration: PushPull or Open Collector*/
200
        SCU->GPIOTYPE[GPIO_Number] &= ~(0x1 << Counter) ;
201
       if(GPIO_InitStruct->GPIO_Type == GPIO_Type_OpenCollector)
202
       {
203
         /*Open Drain configuration*/
204
        SCU->GPIOTYPE[GPIO_Number] |= 0x1 << Counter;
205
       }    
206
    }
207
 }
208
}
209
 
210
/*******************************************************************************
211
* Function Name  : GPIO_StructInit
212
* Description    : Initialize the GPIO Init Structure parameters
213
* Input          : GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure
214
*                  which will be initialized.
215
* Output         : None
216
* Return         : None
217
*******************************************************************************/
218
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
219
{
220
  /* Reset GPIO init structure parameters values */
221
  GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_All;
222
  GPIO_InitStruct->GPIO_Direction = GPIO_PinInput;
223
  GPIO_InitStruct->GPIO_Type = GPIO_Type_PushPull;
224
  GPIO_InitStruct->GPIO_IPConnected = GPIO_IPConnected_Disable;
225
  GPIO_InitStruct->GPIO_Alternate = GPIO_InputAlt1;
226
}
227
 
228
/*******************************************************************************
229
* Function Name  : GPIO_ReadBit
230
* Description    : Reads the specified port pin
231
* Input          : - GPIOx: where x can be (0..9) to select the GPIO peripheral.
232
*                : - GPIO_Pin: the Pin number. This parameter can be GPIO_Pin_x
233
*                    where x can be (0..7).
234
* Output         : None
235
* Return         : The port pin value
236
*******************************************************************************/
237
u8 GPIO_ReadBit(GPIO_TypeDef* GPIOx, u8 GPIO_Pin)
238
{
239
  if ((((GPIOx->DR[GPIO_Pin<<2])) & GPIO_Pin) != Bit_RESET )
240
  {
241
    return Bit_SET;
242
  }
243
  else
244
  {
245
    return Bit_RESET;
246
  }
247
}
248
 
249
/*******************************************************************************
250
* Function Name  : GPIO_Read
251
* Description    : Reads the specified GPIO data port
252
* Input          : - GPIOx: where x can be (0..9) to select the GPIO peripheral.
253
* Output         : None
254
* Return         : GPIO data port word value.
255
*******************************************************************************/
256
u8 GPIO_Read(GPIO_TypeDef* GPIOx)
257
{
258
  return (GPIOx->DR[0x3FC]);
259
}
260
 
261
/*******************************************************************************
262
* Function Name  : GPIO_WriteBit
263
* Description    : Sets or clears the selected data port bit.
264
* Input          : - GPIOx: where x can be (0..9) to select the GPIO peripheral.
265
*                  - GPIO_Pin: the Pin number. This parameter can be GPIO_Pin_x
266
*                    where x can be (0..7).
267
*                  - BitVal: this parameter specifies the value to be written
268
*                    to the selected bit.
269
*                    BitVal must be one of the BitAction enum values:
270
*                       - Bit_RESET: to clear the port pin
271
*                       - Bit_SET: to set the port pin
272
* Output         : None
273
* Return         : None
274
*******************************************************************************/
275
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, u8 GPIO_Pin, BitAction BitVal)
276
{
277
  if(BitVal == Bit_SET)
278
  {
279
    GPIOx->DR[GPIO_Pin <<2] = GPIO_Pin;
280
  }
281
  else
282
  {
283
    GPIOx->DR[GPIO_Pin <<2] = 0x00;
284
  }
285
}
286
 
287
/*******************************************************************************
288
* Function Name  : GPIO_Write
289
* Description    : Writes the passed value in the selected data GPIOx port
290
*                  register.
291
* Input          :- GPIOx: where x can be (0..9) to select the GPIO peripheral.
292
*                 - PortVal: the value to be written to the data port register.
293
* Output         : None
294
* Return         : None
295
*******************************************************************************/
296
void GPIO_Write(GPIO_TypeDef* GPIOx, u8 PortVal)
297
{
298
  GPIOx->DR[0x3FC] = PortVal;
299
}
300
 
301
/*******************************************************************************
302
* Function Name  : GPIO_EMIConfig
303
* Description    : Enables or disables GPIO 8 and 9 in EMI mode.
304
* Input          : - NewState: new state of the EMI.
305
*                   This parameter can be: ENABLE or DISABLE.
306
* Output         : None
307
* Return         : None
308
*******************************************************************************/
309
void GPIO_EMIConfig(FunctionalState NewState)
310
{
311
  if(NewState == ENABLE)
312
  {
313
    SCU->GPIOEMI = 0x01;
314
  }
315
  else
316
  {
317
    SCU->GPIOEMI = 0x00;
318
  }
319
}
320
 
321
/*******************************************************************************
322
* Function Name  : GPIO_ANAPinConfig
323
* Description    : Enables or disables pins from GPIO 4 in Analogue mode.
324
* Input          :- GPIO_ANAChannel: selects the ADC channel pin.
325
*                   This parameter can be one of the following values:
326
*                      GPIO_ANAChannel0
327
*                      GPIO_ANAChannel1
328
*                      GPIO_ANAChannel2
329
*                      GPIO_ANAChannel3
330
*                      GPIO_ANAChannel4
331
*                      GPIO_ANAChannel5
332
*                      GPIO_ANAChannel6
333
*                      GPIO_ANAChannel7
334
*                      GPIO_ANAChannelALL
335
*                 - NewState: new state of the port pin.
336
*                   This parameter can be: ENABLE or DISABLE.
337
* Output         : None
338
* Return         : None
339
*******************************************************************************/
340
void GPIO_ANAPinConfig(u8 GPIO_ANAChannel, FunctionalState NewState)
341
{
342
  if(NewState == ENABLE)
343
  {
344
     SCU->GPIOOUT[4] &= GPIO_GetAnaloClearBits(GPIO_ANAChannel);
345
     SCU->GPIOIN[4]  &= ~GPIO_ANAChannel;
346
     SCU->GPIOANA    |= GPIO_ANAChannel;
347
  }
348
  else
349
  {
350
    SCU->GPIOANA &= ~GPIO_ANAChannel;
351
  }
352
}
353
 
354
/*******************************************************************************
355
* Function Name  : GPIO_GetGPIONumber
356
* Description    : searche the GPIO number.
357
* Input          : GPIOx: where x can be (0..9) to select the GPIO peripheral.
358
* Output         : None
359
* Return         : GPIO number
360
*******************************************************************************/
361
u8 GPIO_GetGPIONumber(GPIO_TypeDef* GPIOx)
362
{
363
  if(GPIOx == GPIO1)
364
  {
365
    return  1;
366
  }
367
  if(GPIOx == GPIO2)
368
  {
369
    return  2;
370
  }
371
  if(GPIOx == GPIO3)
372
  {
373
    return  3;
374
  }
375
  if(GPIOx == GPIO4)
376
  {
377
    return  4;
378
  }
379
  if(GPIOx == GPIO5)
380
  {
381
    return  5;
382
  }
383
  if(GPIOx == GPIO6)
384
  {
385
    return  6;
386
  }
387
  if(GPIOx == GPIO7)
388
  {
389
    return  7;
390
  }
391
  if(GPIOx == GPIO8)
392
  {
393
    return  8;
394
  }
395
  if(GPIOx == GPIO9)
396
  {
397
    return  9;
398
  }
399
  return 0;
400
}
401
/*******************************************************************************
402
* Function Name  : GPIO_GetAnaloClearBits
403
* Description    : Clear the corresponding bits in the SCU_OUT register.
404
* Input          : GPIO_ANAChannel: selects the ADC channel pin.
405
*                   This parameter can be one of the following values:
406
*                      GPIO_ANAChannel0
407
*                      GPIO_ANAChannel1
408
*                      GPIO_ANAChannel2
409
*                      GPIO_ANAChannel3
410
*                      GPIO_ANAChannel4
411
*                      GPIO_ANAChannel5
412
*                      GPIO_ANAChannel6
413
*                      GPIO_ANAChannel7
414
*                      GPIO_ANAChannelALL
415
* Output         : None
416
* Return         : reset value in SCU_OUT register
417
*******************************************************************************/
418
u16 GPIO_GetAnaloClearBits(u8 GPIO_ANAChannel)
419
{
420
  if(GPIO_ANAChannel == GPIO_ANAChannel0)
421
  {
422
    return 0xFFFC;
423
  }
424
  if(GPIO_ANAChannel == GPIO_ANAChannel1)
425
  {
426
    return 0xFFF3;
427
  }
428
  if(GPIO_ANAChannel == GPIO_ANAChannel2)
429
  {
430
    return 0xFFCF;
431
  }
432
  if(GPIO_ANAChannel == GPIO_ANAChannel3)
433
  {
434
    return 0xFF3F;
435
  }
436
  if(GPIO_ANAChannel == GPIO_ANAChannel4)
437
  {
438
    return 0xFCFF;
439
  }
440
  if(GPIO_ANAChannel == GPIO_ANAChannel5)
441
  {
442
    return 0xF3FF;
443
  }
444
  if(GPIO_ANAChannel == GPIO_ANAChannel6)
445
  {
446
    return 0xCFFF;
447
  }
448
  if(GPIO_ANAChannel == GPIO_ANAChannel7)
449
  {
450
    return 0x3FFF;
451
  }
452
  /*Default vaule: return GPIO_ANAChannelALL*/
453
  return 0x0000;
454
}
455
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
456