summaryrefslogtreecommitdiffstats
path: root/polycom_recv/gpio.c
blob: 33b3cda6e770dd4f8d2bdc005f0a8e2df363ec4a (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
#include "project.h"

static os_timer_t gpio_timer;


static struct gpio
{
  int id;
  uint32_t pin;
  uint32_t func;
  uint32_t bit;
  int pull;
  int state;
} gpios[] =
{
  {
  14, PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14, BIT14, 0},
  {
  12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, BIT12, 0},
  {
  13, PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13, BIT13, 0},
  {
  0, PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0, BIT0, 0}
};

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

static uint32_t mask;

static os_timer_t gpio_timer;
static os_timer_t gpio_intr_timer;

static int flash;


uint32_t ICACHE_FLASH_ATTR
gpio_read (void)
{
  return gpio_input_get () & mask;
}



static void ICACHE_FLASH_ATTR
gpio_dispatch (void)
{
  uint32_t v = 0;
  int i;

  for (i = 0; i < N_GPIOS; ++i)
    {
      if ((gpios[i].state == 1 && flash) || (gpios[i].state == 2))
        v |= gpios[i].bit;
    }
  gpio_output_set (v, (~v) & mask, mask, 0);
}




static void ICACHE_FLASH_ATTR
gpio_timer_cb (void *arg)
{
  flash = !flash;

  gpio_dispatch ();
}


void ICACHE_FLASH_ATTR
gpio_write (int a, int b, int c, int d)
{
  uint32_t v = 0;

  gpios[0].state = a;
  gpios[1].state = b;
  gpios[2].state = c;
  gpios[3].state = d;

  gpio_dispatch ();
}


void ICACHE_FLASH_ATTR
gpio_init (void)
{
  int i;

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

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

      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_ENABLE) |
                         GPIO_PIN_SOURCE_SET (GPIO_AS_PIN_SOURCE));

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

      mask |= gpios[i].bit;
    }

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


}