Root/lua/examples/lua_blink_led/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
30int
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  return 0;
37}
38
39void
40jz_gpio_as_input (JZ_PIO * pio, unsigned int o)
41{
42  pio->PXFUNC = (1 << (o));
43  pio->PXSELC = (1 << (o));
44  pio->PXDIRC = (1 << (o));
45}
46
47void
48jz_gpio_as_irq (JZ_PIO * pio, unsigned int o)
49{
50  pio->PXFUNC = (1 << (o));
51  pio->PXSELS = (1 << (o));
52  pio->PXDIRC = (1 << (o));
53}
54
55int
56jz_gpio_set_pin (JZ_PIO * pio, unsigned int o)
57{
58  pio->PXDATS = (1 << (o));
59  return 0;
60}
61
62int
63jz_gpio_clear_pin (JZ_PIO * pio, unsigned int o)
64{
65  pio->PXDATC = (1 << (o));
66  return 0;
67}
68
69void
70jz_gpio_out (JZ_PIO * pio, unsigned int o, unsigned int val)
71{
72  if (val == 0)
73    pio->PXDATC = (1 << (o));
74  else
75    pio->PXDATS = (1 << (o));
76}
77
78unsigned int
79jz_gpio_get_pin (JZ_PIO * pio, unsigned int o)
80{
81  return (pio->PXPIN & (1 << o)) ? 1 : 0;
82}
83
84int
85jz_gpio_as_func (JZ_PIO * pio, unsigned int o, int func)
86{
87  switch (func)
88    {
89    case 0:
90      pio->PXFUNS = (1 << o);
91      pio->PXTRGC = (1 << o);
92      pio->PXSELC = (1 << o);
93      pio->PXPES = (1 << o);
94      return 1;
95
96    case 1:
97      pio->PXFUNS = (1 << o);
98      pio->PXTRGC = (1 << o);
99      pio->PXSELS = (1 << o);
100      pio->PXPES = (1 << o);
101      return 1;
102
103    case 2:
104      pio->PXFUNS = (1 << o);
105      pio->PXTRGS = (1 << o);
106      pio->PXSELC = (1 << o);
107      pio->PXPES = (1 << o);
108      return 1;
109    }
110  return 0;
111}
112
113JZ_PIO *
114jz_gpio_map (int port)
115{
116  JZ_PIO *pio;
117
118  pio = (JZ_PIO *) jz_mmap (JZ_GPIO_BASE);
119  pio = (JZ_PIO *) ((unsigned int) pio + port * 0x100);
120
121  return pio;
122}
123

Archive Download this file

Branches:
master



interactive