Root/Software/xc3sprog/jz47xx_gpio.c

1/*
2 * JZ47xx GPIO lines
3 *
4 * Written 2010 by Andres Calderon andres.calderon@emqbit.com
5 */
6
7#include <stdio.h>
8#include <sys/mman.h>
9#include <fcntl.h>
10#include <stdlib.h>
11#include <termios.h>
12#include <unistd.h>
13
14#include "jz47xx_gpio.h"
15
16#define JZ_GPIO_BASE 0x10010000
17
18PJZ_PIO* pio;
19
20void
21jz_gpio_as_output (int port, unsigned int o)
22{
23  pio[port]->PXFUNC = (1 << (o));
24  pio[port]->PXSELC = (1 << (o));
25  pio[port]->PXDIRS = (1 << (o));
26}
27
28void
29jz_gpio_as_input (int port, unsigned int o)
30{
31  pio[port]->PXFUNC = (1 << (o));
32  pio[port]->PXSELC = (1 << (o));
33  pio[port]->PXDIRC = (1 << (o));
34}
35
36void
37jz_gpio_set_pin (int port, unsigned int o)
38{
39  pio[port]->PXDATS = (1 << (o));
40}
41
42void
43jz_gpio_clear_pin (int port, unsigned int o)
44{
45  pio[port]->PXDATC = (1 << (o));
46}
47
48void
49jz_gpio_out (int port, unsigned int o, unsigned int val)
50{
51  if (val == 0)
52    pio[port]->PXDATC = (1 << (o));
53  else
54    pio[port]->PXDATS = (1 << (o));
55}
56
57unsigned int
58jz_gpio_get_pin (int port, unsigned int o)
59{
60  return (pio[port]->PXPIN & (1 << o)) ? 1 : 0;
61}
62
63JZ_PIO *
64jz_gpio_map ()
65{
66  int fd;
67  int port;
68
69  JZ_PIO *pio_base;
70
71  pio = (PJZ_PIO*)malloc(sizeof(PJZ_PIO)*4);
72
73  if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1)
74    {
75      fprintf (stderr, "Cannot open /dev/mem.\n");
76      return 0;
77    }
78
79  pio_base = (JZ_PIO *) mmap (0, getpagesize (), PROT_READ | PROT_WRITE, MAP_SHARED, fd, JZ_GPIO_BASE);
80
81  if (pio_base == (JZ_PIO *) - 1)
82    {
83      fprintf (stderr, "Cannot mmap.\n");
84      return 0;
85    }
86    
87  for(port=0; port<4; port++)
88    pio[port] = (JZ_PIO *) ((unsigned int) pio_base + port * 0x100);
89
90  return pio_base;
91}
92

Archive Download this file

Branches:
master



interactive