2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #include <ao_74hc165.h>
23 #include <ao_radio_cmac.h>
25 static __xdata uint8_t ao_pad_ignite;
26 static __xdata struct ao_pad_command command;
27 static __xdata struct ao_pad_query query;
28 static __pdata uint8_t ao_pad_armed;
29 static __pdata uint16_t ao_pad_arm_time;
30 static __pdata uint8_t ao_pad_box;
31 static __xdata uint8_t ao_pad_disabled;
32 static __pdata uint16_t ao_pad_packet_time;
34 #ifndef AO_PAD_RSSI_MINIMUM
35 #define AO_PAD_RSSI_MINIMUM -90
41 static __pdata uint8_t ao_pad_debug;
42 #define PRINTD(...) (ao_pad_debug ? (printf(__VA_ARGS__), 0) : 0)
43 #define FLUSHD() (ao_pad_debug ? (flush(), 0) : 0)
53 ao_gpio_set(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, v);
56 ao_beep(v ? AO_BEEP_MID : 0);
67 ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, v);
74 #define pins_pad(pad) (*((AO_PAD_ ## pad ## _PORT) == AO_PAD_PORT_0 ? (&pins0) : (&pins1)))
76 #define pins_pad(pad) pins0
77 #define AO_PAD_PORT_0 AO_PAD_PORT
89 while (!ao_pad_ignite)
90 ao_sleep(&ao_pad_ignite);
92 * Actually set the pad bits
99 if (ao_pad_ignite & (1 << 0))
100 pins_pad(0) |= (1 << AO_PAD_PIN_0);
103 if (ao_pad_ignite & (1 << 1))
104 pins_pad(1) |= (1 << AO_PAD_PIN_1);
107 if (ao_pad_ignite & (1 << 2))
108 pins_pad(2) |= (1 << AO_PAD_PIN_2);
111 if (ao_pad_ignite & (1 << 3))
112 pins_pad(3) |= (1 << AO_PAD_PIN_3);
115 if (ao_pad_ignite & (1 << 4))
116 pins_pad(4) |= (1 << AO_PAD_PIN_4);
119 if (ao_pad_ignite & (1 << 5))
120 pins_pad(5) |= (1 << AO_PAD_PIN_5);
123 if (ao_pad_ignite & (1 << 6))
124 pins_pad(6) |= (1 << AO_PAD_PIN_6);
127 if (ao_pad_ignite & (1 << 7))
128 pins_pad(7) |= (1 << AO_PAD_PIN_7);
131 PRINTD("ignite pins 0x%x 0x%x\n", pins0, pins1);
132 ao_gpio_set_bits(AO_PAD_PORT_0, pins0);
133 ao_gpio_set_bits(AO_PAD_PORT_1, pins1);
135 PRINTD("ignite pins 0x%x\n", pins0);
136 ao_gpio_set_bits(AO_PAD_PORT_0, pins0);
138 while (ao_pad_ignite) {
141 ao_delay(AO_PAD_FIRE_TIME);
144 ao_gpio_clr_bits(AO_PAD_PORT_0, pins0);
145 ao_gpio_clr_bits(AO_PAD_PORT_1, pins1);
146 PRINTD("turn off pins 0x%x 0x%x\n", pins0, pins1);
148 ao_gpio_set_bits(AO_PAD_PORT_0, pins0);
149 PRINTD("turn off pins 0x%x\n", pins0);
154 #define AO_PAD_ARM_SIREN_INTERVAL 200
156 /* Resistor values needed for various voltage test ratios:
158 * Net names involved:
160 * V_BATT Battery power, after the initial power switch
161 * V_PYRO Pyro power, after the pyro power switch (and initial power switch)
162 * PYRO_SENSE ADC input to sense V_PYRO voltage
163 * BATT_SENSE ADC input to sense V_BATT voltage
164 * IGNITER FET output to pad (the other pad lead hooks to V_PYRO)
165 * IGNITER_SENSE ADC input to sense igniter voltage
167 * AO_PAD_R_V_BATT_BATT_SENSE Resistor from battery rail to battery sense input
168 * AO_PAD_R_BATT_SENSE_GND Resistor from battery sense input to ground
170 * AO_PAD_R_V_BATT_V_PYRO Resistor from battery rail to pyro rail
171 * AO_PAD_R_V_PYRO_PYRO_SENSE Resistor from pyro rail to pyro sense input
172 * AO_PAD_R_PYRO_SENSE_GND Resistor from pyro sense input to ground
174 * AO_PAD_R_V_PYRO_IGNITER Optional resistors from pyro rail to FET igniter output
175 * AO_PAD_R_IGNITER_IGNITER_SENSE Resistors from FET igniter output to igniter sense ADC inputs
176 * AO_PAD_R_IGNITER_SENSE_GND Resistors from igniter sense ADC inputs to ground
180 ao_pad_decivolt(int16_t adc, int16_t r_plus, int16_t r_minus)
182 int32_t mul = (int32_t) AO_ADC_REFERENCE_DV * (r_plus + r_minus);
183 int32_t div = (int32_t) AO_ADC_MAX * r_minus;
184 return ((int32_t) adc * mul + mul/2) / div;
192 __pdata uint8_t prev = 0, cur = 0;
193 __pdata uint8_t beeping = 0;
194 __xdata volatile struct ao_data *packet;
195 __pdata uint16_t arm_beep_time = 0;
197 sample = ao_data_head;
199 __pdata int16_t pyro;
202 while (sample == ao_data_head)
203 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
207 packet = &ao_data_ring[sample];
208 sample = ao_data_ring_next(sample);
210 /* Reply battery voltage */
211 query.battery = ao_pad_decivolt(packet->adc.batt, AO_PAD_R_V_BATT_BATT_SENSE, AO_PAD_R_BATT_SENSE_GND);
213 /* Current pyro voltage */
214 pyro = ao_pad_decivolt(packet->adc.pyro,
215 AO_PAD_R_V_PYRO_PYRO_SENSE,
216 AO_PAD_R_PYRO_SENSE_GND);
219 if (pyro > query.battery * 7 / 8) {
220 query.arm_status = AO_PAD_ARM_STATUS_ARMED;
223 query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
226 if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
228 else if (ao_radio_cmac_rssi < AO_PAD_RSSI_MINIMUM)
233 for (c = 0; c < AO_PAD_NUM; c++) {
234 int16_t sense = ao_pad_decivolt(packet->adc.sense[c],
235 AO_PAD_R_IGNITER_IGNITER_SENSE,
236 AO_PAD_R_IGNITER_SENSE_GND);
237 uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
240 * Here's the resistor stack on each
241 * igniter channel. Note that
242 * AO_PAD_R_V_PYRO_IGNITER is optional
245 * AO_PAD_R_V_PYRO_IGNITER igniter
247 * AO_PAD_R_IGNITER_IGNITER_SENSE \
249 * AO_PAD_R_IGNITER_SENSE_GND /
254 #ifdef AO_PAD_R_V_PYRO_IGNITER
255 if (sense <= pyro / 8) {
256 /* close to zero → relay is closed */
257 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
258 if ((ao_time() % 100) < 50)
259 cur |= AO_LED_CONTINUITY(c);
264 if (sense >= (pyro * 7) / 8) {
266 /* sense close to pyro voltage; igniter is good
268 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
269 cur |= AO_LED_CONTINUITY(c);
272 /* relay not shorted (if we can tell),
273 * and igniter not obviously present
275 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
278 query.igniter_status[c] = status;
281 PRINTD("change leds from %02x to %02x\n",
288 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
295 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
296 if (arm_beep_time == 0) {
297 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
302 } else if (beeping) {
313 if (!ao_pad_disabled) {
315 ao_radio_recv_abort();
323 ao_wakeup (&ao_pad_disabled);
328 ao_pad_read_box(void)
330 uint8_t byte = ao_74hc165_read();
339 #ifdef AO_PAD_SELECTOR_PORT
340 static int ao_pad_read_box(void) {
341 AO_PORT_TYPE value = ao_gpio_get_all(AO_PAD_SELECTOR_PORT);
345 for (pin = 0; pin < sizeof (AO_PORT_TYPE) * 8; pin++) {
346 if (AO_PAD_SELECTOR_PINS & (1 << pin)) {
347 if ((value & (1 << pin)) == 0)
352 return ao_config.pad_box;
356 #if HAS_FIXED_PAD_BOX
357 #define ao_pad_read_box() ao_config.pad_box
361 #define ao_pad_read_box() PAD_BOX
369 int16_t time_difference;
376 while (ao_pad_disabled)
377 ao_sleep(&ao_pad_disabled);
378 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
379 PRINTD ("cmac_recv %d %d\n", ret, ao_radio_cmac_rssi);
380 if (ret != AO_RADIO_CMAC_OK)
382 ao_pad_packet_time = ao_time();
384 ao_pad_box = ao_pad_read_box();
386 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
387 command.tick, command.box, ao_pad_box, command.cmd, command.channels);
389 switch (command.cmd) {
391 if (command.box != ao_pad_box) {
392 PRINTD ("box number mismatch\n");
396 if (command.channels & ~(AO_PAD_ALL_CHANNELS))
399 time_difference = command.tick - ao_time();
400 PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
401 if (time_difference < 0)
402 time_difference = -time_difference;
403 if (time_difference > 10) {
404 PRINTD ("time difference too large %d\n", time_difference);
408 ao_pad_armed = command.channels;
409 ao_pad_arm_time = ao_time();
413 if (command.box != ao_pad_box) {
414 PRINTD ("box number mismatch\n");
418 query.tick = ao_time();
419 query.box = ao_pad_box;
420 query.channels = AO_PAD_ALL_CHANNELS;
421 query.armed = ao_pad_armed;
422 PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
423 query.tick, query.box, query.channels, query.armed,
425 query.igniter_status[0],
426 query.igniter_status[1],
427 query.igniter_status[2],
428 query.igniter_status[3]);
429 ao_radio_cmac_send(&query, sizeof (query));
433 PRINTD ("not armed\n");
436 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
437 PRINTD ("late pad arm_time %d time %d\n",
438 ao_pad_arm_time, ao_time());
442 ao_pad_ignite = ao_pad_armed;
443 ao_pad_arm_time = ao_time();
444 ao_wakeup(&ao_pad_ignite);
448 PRINTD ("not armed\n");
452 if (!ao_log_running) ao_log_start();
454 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
455 PRINTD ("late pad arm_time %d time %d\n",
456 ao_pad_arm_time, ao_time());
460 ao_pad_ignite = ao_pad_armed;
461 ao_pad_arm_time = ao_time();
462 ao_wakeup(&ao_pad_ignite);
464 case AO_PAD_ENDSTATIC:
478 printf ("Arm switch: ");
479 switch (query.arm_status) {
480 case AO_PAD_ARM_STATUS_ARMED:
483 case AO_PAD_ARM_STATUS_DISARMED:
484 printf ("Disarmed\n");
486 case AO_PAD_ARM_STATUS_UNKNOWN:
487 printf ("Unknown\n");
491 for (c = 0; c < AO_PAD_NUM; c++) {
492 printf ("Pad %d: ", c);
493 switch (query.igniter_status[c]) {
494 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED: printf ("No igniter. Relay closed\n"); break;
495 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN: printf ("No igniter. Relay open\n"); break;
496 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN: printf ("Good igniter. Relay open\n"); break;
497 case AO_PAD_IGNITER_STATUS_UNKNOWN: printf ("Unknown\n"); break;
508 if (!ao_match_word("DoIt"))
511 if (ao_cmd_status != ao_cmd_success)
513 ignite = 1 << ao_cmd_lex_i;
515 if (ao_cmd_status != ao_cmd_success) {
517 ao_cmd_status = ao_cmd_success;
519 repeat = ao_cmd_lex_i;
520 while (repeat-- > 0) {
521 ao_pad_ignite = ignite;
522 ao_wakeup(&ao_pad_ignite);
523 ao_delay(AO_PAD_FIRE_TIME>>1);
527 static __xdata struct ao_task ao_pad_task;
528 static __xdata struct ao_task ao_pad_ignite_task;
529 static __xdata struct ao_task ao_pad_monitor_task;
533 ao_pad_set_debug(void)
536 if (ao_cmd_status == ao_cmd_success)
537 ao_pad_debug = ao_cmd_lex_i != 0;
542 ao_pad_alarm_debug(void)
544 uint8_t which, value;
546 if (ao_cmd_status != ao_cmd_success)
548 which = ao_cmd_lex_i;
550 if (ao_cmd_status != ao_cmd_success)
552 value = ao_cmd_lex_i;
553 printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
561 __code struct ao_cmds ao_pad_cmds[] = {
562 { ao_pad_test, "t\0Test pad continuity" },
563 { ao_pad_manual, "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
565 { ao_pad_set_debug, "D <0 off, 1 on>\0Debug" },
566 { ao_pad_alarm_debug, "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
571 #ifndef AO_PAD_PORT_1
572 #define AO_PAD_0_PORT AO_PAD_PORT
573 #define AO_PAD_1_PORT AO_PAD_PORT
574 #define AO_PAD_2_PORT AO_PAD_PORT
575 #define AO_PAD_3_PORT AO_PAD_PORT
576 #define AO_PAD_4_PORT AO_PAD_PORT
577 #define AO_PAD_5_PORT AO_PAD_PORT
578 #define AO_PAD_6_PORT AO_PAD_PORT
579 #define AO_PAD_7_PORT AO_PAD_PORT
585 #ifdef AO_PAD_SELECTOR_PORT
588 for (pin = 0; pin < sizeof (AO_PORT_TYPE) * 8; pin++) {
589 if (AO_PAD_SELECTOR_PINS & (1 << pin))
590 ao_enable_input(AO_PAD_SELECTOR_PORT, pin, AO_EXTI_MODE_PULL_UP);
594 ao_enable_output(AO_PAD_0_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
597 ao_enable_output(AO_PAD_1_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
600 ao_enable_output(AO_PAD_2_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
603 ao_enable_output(AO_PAD_3_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
606 ao_enable_output(AO_PAD_4_PORT, AO_PAD_PIN_4, AO_PAD_4, 0);
609 ao_enable_output(AO_PAD_5_PORT, AO_PAD_PIN_5, AO_PAD_5, 0);
612 ao_enable_output(AO_PAD_6_PORT, AO_PAD_PIN_6, AO_PAD_6, 0);
615 ao_enable_output(AO_PAD_7_PORT, AO_PAD_PIN_7, AO_PAD_7, 0);
618 ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
621 ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
623 ao_cmd_register(&ao_pad_cmds[0]);
624 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
625 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
626 ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");