summaryrefslogtreecommitdiffstats
path: root/polycom_xmit/gpio.c
blob: 8a595ace6e202d4285e36fe472cbb13e0ec7be59 (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
#include "project.h"



static struct gpio
{
  int id;
  uint32_t pin;
  uint32_t func;
  uint32_t bit;
} gpios[] =
{
  {
  0, PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0, BIT0},
  {
  2, PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2, BIT2},
  {
  12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, BIT12},
  {
  13, PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13, BIT13},
  {
  14, PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14, BIT14},
  {
  15, PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15, BIT15}
};

#define N_GPIOS (sizeof(gpios)/sizeof(gpios[0]))

static os_timer_t gpio_timer;
static os_timer_t gpio_intr_timer;


void ICACHE_FLASH_ATTR
gpio_dispatch (void)
{
  uint32_t v = gpio_input_get ();
  msg_send (v);
}


static void ICACHE_FLASH_ATTR
gpio_intr_timer_cb (void *arg)
{
  gpio_dispatch ();
}


static void ICACHE_FLASH_ATTR
gpio_timer_cb (void *arg)
{
  gpio_dispatch ();
}


/* Docs say you can't call os_timer_arm in an interrupt, yet the examples do */

static void
gpio_intr_handler (void *arg)
{
  int i;
  uint32 status = GPIO_REG_READ (GPIO_STATUS_ADDRESS);


/* Clear any interrupts */
  for (i = 0; i < N_GPIOS; ++i)
    if (status & gpios[i].bit)
      GPIO_REG_WRITE (GPIO_STATUS_W1TC_ADDRESS, status & gpios[i].bit);

/*Arm the timer*/
  os_timer_disarm (&gpio_intr_timer);
  os_timer_setfn (&gpio_intr_timer, gpio_intr_timer_cb, NULL);
  os_timer_arm (&gpio_intr_timer, 5, 0);
}

void ICACHE_FLASH_ATTR
gpio_page (struct espconn *conn)
{
  char *page = os_zalloc (1024);
  char *ptr = page;
  int i;
  uint32_t j, v;

  if (!page)
    {
      webserver_send_reply (conn, 400, "text/html",
                            "<html><head></head><body>Out of memory</body></html>",
                            0);
      return;

    }

  ptr += os_sprintf (ptr, "<html><head>");

  ptr += os_sprintf (ptr, "<style>\n"
                     "table {\n"
                     "border-collapse: collapse;\n"
                     "}\n"
                     "table, th, td {\n"
                     "border: 1px solid black;\n" "}\n" "</style>");

  ptr += os_sprintf (ptr, "</head><body>");

  v = gpio_input_get ();
  ptr += os_sprintf (ptr, "<h2>GPIO: 0x%04x</h2><br/>", v);
  ptr += os_sprintf (ptr, "<table>");
  for (i = 0; i < 16; ++i)
    {
      j = 1 << i;
      ptr +=
        os_sprintf (ptr,
                    "<tr><td>%d</td><td>0x%04x</td><td>%s</td></tr>",
                    i, j, (j & v) ? "High" : "Low");
    }


  ptr += os_sprintf (ptr, "</table></body></html>");
  webserver_send_reply (conn, 200, "text/html", page, ptr - page);
  os_free (page);
}






void ICACHE_FLASH_ATTR
gpio_init (void)
{
  int i;

  ETS_GPIO_INTR_DISABLE ();
  ETS_GPIO_INTR_ATTACH (gpio_intr_handler, NULL);


  for (i = 0; i < N_GPIOS; ++i)
    {
      PIN_FUNC_SELECT (gpios[i].pin, gpios[i].func);
      PIN_PULLUP_EN (gpios[i].pin);

/* disable drivers */
      gpio_output_set (0, 0, 0, gpios[i].bit);

      gpio_register_set (GPIO_PIN_ADDR (gpios[i].id),
                         GPIO_PIN_INT_TYPE_SET (GPIO_PIN_INTR_DISABLE) |
                         GPIO_PIN_PAD_DRIVER_SET (GPIO_PAD_DRIVER_DISABLE) |
                         GPIO_PIN_SOURCE_SET (GPIO_AS_PIN_SOURCE));

      GPIO_REG_WRITE (GPIO_STATUS_W1TC_ADDRESS, gpios[i].bit);

      gpio_pin_intr_state_set (GPIO_ID_PIN (gpios[i].id),
                               GPIO_PIN_INTR_ANYEDGE);
    }

  ETS_GPIO_INTR_ENABLE ();

  os_timer_disarm (&gpio_timer);
  os_timer_setfn (&gpio_timer, (os_timer_func_t *) gpio_timer_cb, NULL);
  os_timer_arm (&gpio_timer, 10000, 1);
}