Root/Examples/sram_gpio/src/jz47xx_gpio.c

1/*
2  JZ47xx GPIO at userspace
3
4  Copyright (C) 2010 Andres Calderon andres.calderon@emqbit.com
5                                 
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23
24#include <jz47xx_gpio.h>
25#include <jz47xx_mmap.h>
26
27
28#define JZ_GPIO_BASE 0x10010000
29
30void
31jz_gpio_as_output (JZ_PIO * pio, unsigned int o)
32{
33  pio->PXFUNC = (1 << (o));
34  pio->PXSELC = (1 << (o));
35  pio->PXDIRS = (1 << (o));
36}
37
38void
39jz_gpio_as_input (JZ_PIO * pio, unsigned int o)
40{
41  pio->PXFUNC = (1 << (o));
42  pio->PXSELC = (1 << (o));
43  pio->PXDIRC = (1 << (o));
44}
45
46void
47jz_gpio_as_irq (JZ_PIO * pio, unsigned int o)
48{
49  pio->PXFUNC = (1 << (o));
50  pio->PXSELS = (1 << (o));
51  pio->PXDIRC = (1 << (o));
52}
53
54void
55jz_gpio_set_pin (JZ_PIO * pio, unsigned int o)
56{
57  pio->PXDATS = (1 << (o));
58}
59
60void
61jz_gpio_clear_pin (JZ_PIO * pio, unsigned int o)
62{
63  pio->PXDATC = (1 << (o));
64}
65
66void
67jz_gpio_out (JZ_PIO * pio, unsigned int o, unsigned int val)
68{
69  if (val == 0)
70    pio->PXDATC = (1 << (o));
71  else
72    pio->PXDATS = (1 << (o));
73}
74
75unsigned int
76jz_gpio_get_pin (JZ_PIO * pio, unsigned int o)
77{
78  return (pio->PXPIN & (1 << o)) ? 1 : 0;
79}
80
81int
82jz_gpio_as_func (JZ_PIO * pio, unsigned int o, int func)
83{
84  switch (func)
85    {
86    case 0:
87      pio->PXFUNS = (1 << o);
88      pio->PXTRGC = (1 << o);
89      pio->PXSELC = (1 << o);
90      pio->PXPES = (1 << o);
91      return 1;
92
93    case 1:
94      pio->PXFUNS = (1 << o);
95      pio->PXTRGC = (1 << o);
96      pio->PXSELS = (1 << o);
97      pio->PXPES = (1 << o);
98      return 1;
99
100    case 2:
101      pio->PXFUNS = (1 << o);
102      pio->PXTRGS = (1 << o);
103      pio->PXSELC = (1 << o);
104      pio->PXPES = (1 << o);
105      return 1;
106    }
107  return 0;
108}
109
110JZ_PIO *
111jz_gpio_map (int port)
112{
113  JZ_PIO *pio;
114
115  pio = (JZ_PIO *) jz_mmap (JZ_GPIO_BASE);
116  pio = (JZ_PIO *) ((unsigned int) pio + port * 0x100);
117
118  return pio;
119}
120

Archive Download this file

Branches:
master



interactive