summaryrefslogtreecommitdiffstats
path: root/uc_str912/prj_blinky_simple_startup/str91x_lib/src/91x_gpio.c
blob: f2d094199164b697611ae82a02715155d6dacad5 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 91x_gpio.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file provides all the GPIO 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_gpio.h"
#include "91x_scu.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
   static u8 GPIO_GetGPIONumber(GPIO_TypeDef* GPIOx);

/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : GPIO_DeInit
* Description    : Deinitializes the GPIOx peripheral registers to their default
*                  reset values.
* Input          : GPIOx: where x can be (0..9) to select the GPIO peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_DeInit(GPIO_TypeDef* GPIOx)
{

  /* Reset the GPIO registers values */
  if(GPIOx == GPIO0)
  {
    SCU_APBPeriphReset(__GPIO0,ENABLE);
    SCU_APBPeriphReset(__GPIO0,DISABLE);
    SCU->GPIOTYPE[0x00] = 0x0000 ;
    SCU->GPIOOUT[0x00]  = 0x0000;
    SCU->GPIOIN[0x00]   = 0x0000;
  }

   if(GPIOx == GPIO1)
  {
    SCU_APBPeriphReset(__GPIO1,ENABLE);
    SCU_APBPeriphReset(__GPIO1,DISABLE);
    SCU->GPIOTYPE[0x01] = 0x0000 ;
    SCU->GPIOOUT[0x01]  = 0x0000;
    SCU->GPIOIN[0x01]   = 0x0000;
  }

   if(GPIOx == GPIO2)
  {
    SCU_APBPeriphReset(__GPIO2,ENABLE);
    SCU_APBPeriphReset(__GPIO2,DISABLE);
    SCU->GPIOTYPE[0x02] = 0x0000 ;
    SCU->GPIOOUT[0x02]  = 0x0000;
    SCU->GPIOIN[0x02]   = 0x0000;
  }

   if(GPIOx == GPIO3)
  {
    SCU_APBPeriphReset(__GPIO3,ENABLE);
    SCU_APBPeriphReset(__GPIO3,DISABLE);
    SCU->GPIOTYPE[0x03] = 0x0000 ;
    SCU->GPIOOUT[0x03]  = 0x0000;
    SCU->GPIOIN[0x03]   = 0x0000;
  }

   if(GPIOx == GPIO4)
  {
    SCU_APBPeriphReset(__GPIO4,ENABLE);
    SCU_APBPeriphReset(__GPIO4,DISABLE);
    SCU->GPIOTYPE[0x04] = 0x0000 ;
    SCU->GPIOOUT[0x04]  = 0x0000;
    SCU->GPIOIN[0x04]   = 0x0000;
    SCU->GPIOANA = 0x00;
  }

   if(GPIOx == GPIO5)
  {
    SCU_APBPeriphReset(__GPIO5,ENABLE);
    SCU_APBPeriphReset(__GPIO5,DISABLE);
    SCU->GPIOTYPE[0x05] = 0x0000 ;
    SCU->GPIOOUT[0x05]  = 0x0000;
    SCU->GPIOIN[0x05]   = 0x0000;
  }

   if(GPIOx == GPIO6)
  {
    SCU_APBPeriphReset(__GPIO6,ENABLE);
    SCU_APBPeriphReset(__GPIO6,DISABLE);
    SCU->GPIOTYPE[0x06] = 0x0000 ;
    SCU->GPIOOUT[0x06]  = 0x0000;
    SCU->GPIOIN[0x06]   = 0x0000;
  }

   if(GPIOx == GPIO7)
  {
    SCU_APBPeriphReset(__GPIO7,ENABLE);
    SCU_APBPeriphReset(__GPIO7,DISABLE);
    SCU->GPIOOUT[0x07]  = 0xAAAA;
    SCU->GPIOOUT[0x07]  = 0x0000;
    SCU->GPIOIN[0x07]   = 0x0000;
  }

   if(GPIOx == GPIO8)
  {
    SCU_APBPeriphReset(__GPIO8,ENABLE);
    SCU_APBPeriphReset(__GPIO8,DISABLE);
    SCU->GPIOEMI = 0x00;
  }

   if(GPIOx == GPIO9)
  {
    SCU_APBPeriphReset(__GPIO9,ENABLE);
    SCU_APBPeriphReset(__GPIO9,DISABLE);
    SCU->GPIOEMI = 0x00;
  }
}
/*******************************************************************************
* Function Name  : GPIO_Init
* Description    : Initializes the GPIOx peripheral according to the specified
*                  parameters in the GPIO_InitStruct .
* Input          :- GPIOx: where x can be (0..9) to select the GPIO peripheral.
*                 - GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
*                   contains the configuration information for the specified GPIO
*                   peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
  /* Select pin direction */
  u8 PinNumber = 0;
  u8 Counter = 0;
  u8 GPIO_Number = 0;

  GPIO_Number = GPIO_GetGPIONumber(GPIOx);


  if(GPIO_InitStruct->GPIO_Direction == GPIO_PinOutput)
  {
  GPIOx->DDR |= GPIO_InitStruct->GPIO_Pin;
  }
  else
  {
   GPIOx->DDR &= ~GPIO_InitStruct->GPIO_Pin;
  }

   for (Counter = 0; Counter < 8;Counter++)
    {
     /*Search pin number*/
     PinNumber = (GPIO_InitStruct->GPIO_Pin & (1 <<Counter));
     if((PinNumber >> Counter) == 1)
     {
        /*Output ALternate 0*/
        SCU->GPIOOUT[GPIO_Number] &= ~(0x3 <<(Counter *2));
        if(GPIO_InitStruct->GPIO_Alternate == GPIO_OutputAlt1)
        {
          /*Output ALternate 1*/
          SCU->GPIOOUT[GPIO_Number] |= 1 << (Counter *2);
        }
        if(GPIO_InitStruct->GPIO_Alternate == GPIO_OutputAlt2)
        {
          /*Output ALternate 2*/
          SCU->GPIOOUT[GPIO_Number] |= 0x2 << (Counter *2);
        }
        if(GPIO_InitStruct->GPIO_Alternate == GPIO_OutputAlt3)
        {
          /*Output ALternate 3*/
          SCU->GPIOOUT[GPIO_Number] |= 0x3 << (Counter *2);
        }

       /*Type configuration: PushPull or Open Collector*/
        SCU->GPIOTYPE[GPIO_Number] &= ~(0x1 << Counter) ;
       if(GPIO_InitStruct->GPIO_Type == GPIO_Type_OpenCollector)
       {
         /*Open Drain configuration*/
        SCU->GPIOTYPE[GPIO_Number] |= 0x1 << Counter;
       }

       /*IP Connected disable*/
       SCU->GPIOIN[GPIO_Number] &= ~(0x1 << Counter) ;
       if(GPIO_InitStruct->GPIO_IPConnected == GPIO_IPConnected_Enable)
       {
         /*IP Connected enable*/
         SCU->GPIOIN[GPIO_Number] |= 0x1 << Counter;
       }
    }
 }
}

/*******************************************************************************
* Function Name  : GPIO_StructInit
* Description    : Initialize the GPIO Init Structure parameters
* Input          : GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure
*                  which will be initialized.
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
{
  /* Reset GPIO init structure parameters values */
  GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_All;
  GPIO_InitStruct->GPIO_Direction = GPIO_PinInput;
  GPIO_InitStruct->GPIO_Type = GPIO_Type_PushPull;
  GPIO_InitStruct->GPIO_IPConnected = GPIO_IPConnected_Disable;
  GPIO_InitStruct->GPIO_Alternate = GPIO_InputAlt1;
}

/*******************************************************************************
* Function Name  : GPIO_ReadBit
* Description    : Reads the specified port pin
* Input          : - GPIOx: where x can be (0..9) to select the GPIO peripheral.
*                : - GPIO_Pin: the Pin number. This parameter can be GPIO_Pin_x
*                    where x can be (0..7).
* Output         : None
* Return         : The port pin value
*******************************************************************************/
u8 GPIO_ReadBit(GPIO_TypeDef* GPIOx, u8 GPIO_Pin)
{
 if ((((GPIOx->DR[GPIO_Pin<<2])) & GPIO_Pin) != Bit_RESET )
  {
    return Bit_SET;
  }
  else
  {
    return Bit_RESET;
  }
}

/*******************************************************************************
* Function Name  : GPIO_Read
* Description    : Reads the specified GPIO data port
* Input          : - GPIOx: where x can be (0..9) to select the GPIO peripheral.
* Output         : None
* Return         : GPIO data port word value.
*******************************************************************************/
u8 GPIO_Read(GPIO_TypeDef* GPIOx)
{
  return (GPIOx->DR[0x3FC]);
}

/*******************************************************************************
* Function Name  : GPIO_WriteBit
* Description    : Sets or clears the selected data port bit.
* Input          : - GPIOx: where x can be (0..9) to select the GPIO peripheral.
*                  - GPIO_Pin: the Pin number. This parameter can be GPIO_Pin_x
*                    where x can be (0..7).
*                  - BitVal: this parameter specifies the value to be written
*                    to the selected bit.
*                    BitVal must be one of the BitAction enum values:
*                       - Bit_RESET: to clear the port pin
*                       - Bit_SET: to set the port pin
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, u8 GPIO_Pin, BitAction BitVal)
{
  if(BitVal == Bit_SET)
  {
    GPIOx->DR[GPIO_Pin <<2] = GPIO_Pin;
  }
  else
  {
    GPIOx->DR[GPIO_Pin <<2] = 0x00;
  }
}

/*******************************************************************************
* Function Name  : GPIO_Write
* Description    : Writes the passed value in the selected data GPIOx port
*                  register.
* Input          :- GPIOx: where x can be (0..9) to select the GPIO peripheral.
*                 - PortVal: the value to be written to the data port register.
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Write(GPIO_TypeDef* GPIOx, u8 PortVal)
{
  GPIOx->DR[0x3FC] = PortVal;
}

/*******************************************************************************
* Function Name  : GPIO_EMIConfig
* Description    : Enables or disables GPIO 8 and 9 in EMI mode.
* Input          : - NewState: new state of the EMI.
*                   This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_EMIConfig(FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
    SCU->GPIOEMI = 0x01;
  }
  else
  {
    SCU->GPIOEMI = 0x00;
  }
}

/*******************************************************************************
* Function Name  : GPIO_ANAPinConfig
* Description    : Enables or disables pins from GPIO 4 in Analogue mode.
* Input          :- GPIO_ANAChannel: selects the ADC channel pin.
*                   This parameter can be one of the following values:
*                      GPIO_ANAChannel0
*                      GPIO_ANAChannel1
*                      GPIO_ANAChannel2
*                      GPIO_ANAChannel3
*                      GPIO_ANAChannel4
*                      GPIO_ANAChannel5
*                      GPIO_ANAChannel6
*                      GPIO_ANAChannel7
*                      GPIO_ANAChannelALL
*                 - NewState: new state of the port pin.
*                   This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_ANAPinConfig(u8 GPIO_ANAChannel, FunctionalState NewState)
{

  if(NewState == ENABLE)
  {
    if(GPIO_ANAChannel == GPIO_ANAChannelALL)
    {
      SCU->GPIOOUT[4] = 0x0000;
      SCU->GPIOIN[4] = 0x00;
    }
    else
    {
      SCU->GPIOOUT[4] &= ~(0x3<<(GPIO_ANAChannel-1));
      SCU->GPIOIN[4] &= ~GPIO_ANAChannel;
    }
    SCU->GPIOANA |= GPIO_ANAChannel;

  }
  else
  {
    SCU->GPIOANA &= ~GPIO_ANAChannel;
  }
}

/*******************************************************************************
* Function Name  : GPIO_GetGPIONumber
* Description    : searche the GPIO number.
* Input          : GPIOx: where x can be (0..9) to select the GPIO peripheral.
* Output         : None
* Return         : GPIO number
*******************************************************************************/
u8 GPIO_GetGPIONumber(GPIO_TypeDef* GPIOx)
{
 
  if(GPIOx == GPIO1)
  {
    return  1;
  }
  if(GPIOx == GPIO2)
  {
    return  2;
  }
  if(GPIOx == GPIO3)
  {
    return  3;
  }
  if(GPIOx == GPIO4)
  {
    return  4;
  }
  if(GPIOx == GPIO5)
  {
    return  5;
  }
  if(GPIOx == GPIO6)
  {
    return  6;
  }
  if(GPIOx == GPIO7)
  {
    return  7;
  }
  if(GPIOx == GPIO8)
  {
    return  8;
  }
  if(GPIOx == GPIO9)
  {
    return  9;
  }
  return 0;
}
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
OpenPOWER on IntegriCloud