Subversion Repositories NaviCtrl

Rev

Rev 1 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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