summaryrefslogtreecommitdiffstats
path: root/app/gpio.c
blob: e597d169cc7caee97f7756a8a6fe918fccb421c5 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "project.h"
#define DEBOUNCE	10
#define DIAL_BLACKOUT	90 /*	(10pps)*/

static int hs_poll;
int hook;
static int dial_poll;
static int dial_pulse_blackout;
static int pulse_count;



static int fake_hook;

static void exti5_isr(void) 
{
exti_reset_request(EXTI5);
dial_poll=DEBOUNCE;
}

static void exti7_isr(void) 
{
exti_reset_request(EXTI7);
if (dial_pulse_blackout)  return;
dial_pulse_blackout=DIAL_BLACKOUT;
pulse_count++;
}


static void exti14_isr(void) 
{
exti_reset_request(EXTI14);
hs_poll=DEBOUNCE;
}



void exti9_5_isr(void)
{
if (exti_get_flag_status(EXTI5)) exti5_isr();
if (exti_get_flag_status(EXTI7)) exti7_isr();
}
void exti15_10_isr(void)
{
if (exti_get_flag_status(EXTI14)) exti14_isr();
}



static void hs_tick(void)
{
int h;


if (!hs_poll) return;
hs_poll--;


if (hs_poll) return;

h=!gpio_get(GPIOC,GPIO14);

h^=fake_hook;

if (hook==h) return;

hook=h;

printf("Hook is now %d\r\n",hook);

if (!hook) {
	if (ringing) {
	ring_off();
	answer_call();
	}
}

if (hook) {
	ring_off();
	terminate_call();
}



}
static void dial_tick(void) {
int m;

if (dial_pulse_blackout) dial_pulse_blackout--;
if (!dial_poll) return;
dial_poll--;
if (dial_poll) return;

m=!!gpio_get(GPIOA,GPIO5);


if (m) return;
if (!pulse_count) return;

dialstr_digit((pulse_count == 10) ? 0:pulse_count);
pulse_count=0;

}



void toggle_fake_hook(void)
{
fake_hook^=1;
hs_poll=DEBOUNCE;
}

void gpio_tick(void)
{
hs_tick();
dial_tick();

}





void gpio_init(void)
{
  /*GSM module reset pin*/
  gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
                 GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
  gpio_set(GPIOA,GPIO1);


  /* Dial mute */
  gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO4);
  gpio_set(GPIOA,GPIO4);

  gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO5);
  gpio_clear(GPIOA,GPIO5);


  exti_select_source(EXTI5, GPIOA);
  exti_set_trigger(EXTI5, EXTI_TRIGGER_BOTH);
  exti_enable_request(EXTI5);

  /*Dial contacts */

  gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO6);
  gpio_set(GPIOA,GPIO6);

  gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO7);
  gpio_clear(GPIOA,GPIO7);


  exti_select_source(EXTI7, GPIOA);
  exti_set_trigger(EXTI7, EXTI_TRIGGER_RISING);
  exti_enable_request(EXTI7);


  nvic_enable_irq(NVIC_EXTI9_5_IRQ);


  /*hookswitch*/
  gpio_set_mode (GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
  gpio_set(GPIOC,GPIO15);

  gpio_set_mode (GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO14);
  gpio_clear(GPIOC,GPIO14);


  exti_select_source(EXTI14, GPIOC);
  exti_set_trigger(EXTI14, EXTI_TRIGGER_BOTH);
  exti_enable_request(EXTI14);

  nvic_enable_irq(NVIC_EXTI15_10_IRQ);

}