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 uint8_t ao_pad_ignite;
26 static struct ao_pad_command command;
27 static struct ao_pad_query query;
28 static uint8_t ao_pad_armed;
29 static AO_TICK_TYPE ao_pad_arm_time;
30 static uint8_t ao_pad_box;
31 static uint8_t ao_pad_disabled;
32 static AO_TICK_TYPE ao_pad_packet_time;
34 #ifndef AO_PAD_RSSI_MINIMUM
35 #define AO_PAD_RSSI_MINIMUM -90
41 static 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, v);
56 ao_beep(v ? AO_BEEP_MID : 0);
67 ao_gpio_set(AO_STROBE_PORT, AO_STROBE_PIN, 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 (int16_t) (((int32_t) adc * mul + mul/2) / div);
192 AO_LED_TYPE prev = 0, cur = 0;
194 volatile struct ao_data *packet;
195 uint16_t arm_beep_time = 0;
197 sample = ao_data_head;
198 ao_led_set(LEDS_AVAILABLE);
199 ao_delay(AO_MS_TO_TICKS(1000));
205 while (sample == ao_data_head)
206 ao_sleep((void *) &ao_data_head);
210 packet = &ao_data_ring[sample];
211 sample = ao_data_ring_next(sample);
213 /* Reply battery voltage */
214 query.battery = (uint8_t) ao_pad_decivolt(packet->adc.batt, AO_PAD_R_V_BATT_BATT_SENSE, AO_PAD_R_BATT_SENSE_GND);
216 /* Current pyro voltage */
217 pyro = ao_pad_decivolt(packet->adc.pyro,
218 AO_PAD_R_V_PYRO_PYRO_SENSE,
219 AO_PAD_R_PYRO_SENSE_GND);
222 if (pyro > query.battery * 7 / 8) {
223 query.arm_status = AO_PAD_ARM_STATUS_ARMED;
226 query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
229 if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
231 else if (ao_radio_cmac_rssi < AO_PAD_RSSI_MINIMUM)
236 for (c = 0; c < AO_PAD_NUM; c++) {
237 int16_t sense = ao_pad_decivolt(packet->adc.sense[c],
238 AO_PAD_R_IGNITER_IGNITER_SENSE,
239 AO_PAD_R_IGNITER_SENSE_GND);
240 uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
243 * Here's the resistor stack on each
244 * igniter channel. Note that
245 * AO_PAD_R_V_PYRO_IGNITER is optional
248 * AO_PAD_R_V_PYRO_IGNITER igniter
250 * AO_PAD_R_IGNITER_IGNITER_SENSE \
252 * AO_PAD_R_IGNITER_SENSE_GND /
257 #ifdef AO_PAD_R_V_PYRO_IGNITER
258 if (sense <= pyro / 8) {
259 /* close to zero → relay is closed */
260 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
261 if ((ao_time() % 100) < 50)
262 cur |= AO_LED_CONTINUITY(c);
267 if (sense >= (pyro * 7) / 8) {
269 /* sense close to pyro voltage; igniter is good
271 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
272 cur |= AO_LED_CONTINUITY(c);
275 /* relay not shorted (if we can tell),
276 * and igniter not obviously present
278 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
281 query.igniter_status[c] = status;
284 PRINTD("change leds from %02x to %02x\n",
291 if (ao_pad_armed && (AO_TICK_SIGNED) (ao_time() - ao_pad_arm_time) > (AO_TICK_SIGNED) AO_PAD_ARM_TIME)
298 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
299 if (arm_beep_time == 0) {
300 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
305 } else if (beeping) {
316 if (!ao_pad_disabled) {
318 ao_radio_recv_abort();
326 ao_wakeup (&ao_pad_disabled);
331 ao_pad_read_box(void)
333 uint8_t byte = ao_74hc165_read();
342 #ifdef AO_PAD_SELECTOR_PORT
343 static uint8_t ao_pad_read_box(void) {
344 AO_PORT_TYPE value = ao_gpio_get_all(AO_PAD_SELECTOR_PORT);
348 for (pin = 0; pin < sizeof (AO_PORT_TYPE) * 8; pin++) {
349 if (AO_PAD_SELECTOR_PINS & (1 << pin)) {
350 if ((value & (1 << pin)) == 0)
355 return ao_config.pad_box;
359 #if HAS_FIXED_PAD_BOX
360 #define ao_pad_read_box() ao_config.pad_box
364 #define ao_pad_read_box() PAD_BOX
372 int16_t tick_difference;
378 while (ao_pad_disabled)
379 ao_sleep(&ao_pad_disabled);
380 ret = ao_radio_cmac_recv(&command, sizeof (command), 0);
381 PRINTD ("receive packet status %d rssi %d\n", ret, ao_radio_cmac_rssi);
382 if (ret != AO_RADIO_CMAC_OK)
384 ao_pad_packet_time = ao_time();
386 ao_pad_box = ao_pad_read_box();
388 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
389 command.tick, command.box, ao_pad_box, command.cmd, command.channels);
391 switch (command.cmd) {
393 if (command.box != ao_pad_box) {
394 PRINTD ("box number mismatch\n");
398 if (command.channels & ~(AO_PAD_ALL_CHANNELS))
401 tick_difference = (int16_t) (command.tick - (uint16_t) ao_time());
402 PRINTD ("arm tick %d local tick %d\n", command.tick, (uint16_t) ao_time());
403 if (tick_difference < 0)
404 tick_difference = -tick_difference;
405 if (tick_difference > 10) {
406 PRINTD ("tick difference too large %d\n", tick_difference);
409 if (query.arm_status != AO_PAD_ARM_STATUS_ARMED) {
410 PRINTD ("box not armed locally\n");
414 ao_pad_armed = command.channels;
415 ao_pad_arm_time = ao_time();
419 if (command.box != ao_pad_box) {
420 PRINTD ("box number mismatch\n");
424 query.tick = (uint16_t) ao_time();
425 query.box = ao_pad_box;
426 query.channels = AO_PAD_ALL_CHANNELS;
427 query.armed = ao_pad_armed;
428 PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
429 query.tick, query.box, query.channels, query.armed,
431 query.igniter_status[0],
432 query.igniter_status[1],
433 query.igniter_status[2],
434 query.igniter_status[3]);
435 ao_radio_cmac_send(&query, sizeof (query));
439 PRINTD ("not armed\n");
442 if ((AO_TICK_SIGNED) (ao_time() - ao_pad_arm_time) > (AO_TICK_SIGNED) AO_SEC_TO_TICKS(20)) {
443 PRINTD ("late pad arm_time %ld time %ld\n",
444 (long) ao_pad_arm_time, ao_time());
448 ao_pad_ignite = ao_pad_armed;
449 ao_pad_arm_time = ao_time();
450 ao_wakeup(&ao_pad_ignite);
454 PRINTD ("not armed\n");
458 if (!ao_log_running) ao_log_start();
460 if ((AO_TICK_SIGNED) (ao_time() - ao_pad_arm_time) > (AO_TICK_SIGNED) AO_SEC_TO_TICKS(20)) {
461 PRINTD ("late pad arm_time %ld time %ld\n",
462 (long) ao_pad_arm_time, (long) ao_time());
466 ao_pad_ignite = ao_pad_armed;
467 ao_pad_arm_time = ao_time();
468 ao_wakeup(&ao_pad_ignite);
470 case AO_PAD_ENDSTATIC:
484 printf ("Arm switch: ");
485 switch (query.arm_status) {
486 case AO_PAD_ARM_STATUS_ARMED:
489 case AO_PAD_ARM_STATUS_DISARMED:
490 printf ("Disarmed\n");
492 case AO_PAD_ARM_STATUS_UNKNOWN:
493 printf ("Unknown\n");
497 for (c = 0; c < AO_PAD_NUM; c++) {
498 printf ("Pad %d: ", c);
499 switch (query.igniter_status[c]) {
500 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED: printf ("No igniter. Relay closed\n"); break;
501 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN: printf ("No igniter. Relay open\n"); break;
502 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN: printf ("Good igniter. Relay open\n"); break;
503 case AO_PAD_IGNITER_STATUS_UNKNOWN: printf ("Unknown\n"); break;
514 if (!ao_match_word("DoIt"))
516 ignite = 1 << ao_cmd_decimal();
517 if (ao_cmd_status != ao_cmd_success)
519 repeat = (uint8_t) ao_cmd_decimal();
520 if (ao_cmd_status != ao_cmd_success) {
522 ao_cmd_status = ao_cmd_success;
524 while (repeat-- > 0) {
525 ao_pad_ignite = ignite;
526 ao_wakeup(&ao_pad_ignite);
527 ao_delay(AO_PAD_FIRE_TIME>>1);
531 static struct ao_task ao_pad_task;
532 static struct ao_task ao_pad_ignite_task;
533 static struct ao_task ao_pad_monitor_task;
537 ao_pad_set_debug(void)
539 uint32_t r = ao_cmd_decimal();
540 if (ao_cmd_status == ao_cmd_success)
541 ao_pad_debug = r != 0;
546 ao_pad_alarm_debug(void)
548 uint8_t which, value;
549 which = ao_cmd_decimal() != 0;
550 if (ao_cmd_status != ao_cmd_success)
552 value = ao_cmd_decimal() != 0;
553 if (ao_cmd_status != ao_cmd_success)
555 printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
563 const struct ao_cmds ao_pad_cmds[] = {
564 { ao_pad_test, "t\0Test pad continuity" },
565 { ao_pad_manual, "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
567 { ao_pad_set_debug, "D <0 off, 1 on>\0Debug" },
568 { ao_pad_alarm_debug, "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
573 #ifndef AO_PAD_PORT_1
574 #define AO_PAD_0_PORT AO_PAD_PORT
575 #define AO_PAD_1_PORT AO_PAD_PORT
576 #define AO_PAD_2_PORT AO_PAD_PORT
577 #define AO_PAD_3_PORT AO_PAD_PORT
578 #define AO_PAD_4_PORT AO_PAD_PORT
579 #define AO_PAD_5_PORT AO_PAD_PORT
580 #define AO_PAD_6_PORT AO_PAD_PORT
581 #define AO_PAD_7_PORT AO_PAD_PORT
587 #ifdef AO_PAD_SELECTOR_PORT
590 for (pin = 0; pin < (int) sizeof (AO_PORT_TYPE) * 8; pin++) {
591 if (AO_PAD_SELECTOR_PINS & (1 << pin))
592 ao_enable_input(AO_PAD_SELECTOR_PORT, pin, AO_EXTI_MODE_PULL_UP);
596 ao_enable_output(AO_PAD_0_PORT, AO_PAD_PIN_0, 0);
599 ao_enable_output(AO_PAD_1_PORT, AO_PAD_PIN_1, 0);
602 ao_enable_output(AO_PAD_2_PORT, AO_PAD_PIN_2, 0);
605 ao_enable_output(AO_PAD_3_PORT, AO_PAD_PIN_3, 0);
608 ao_enable_output(AO_PAD_4_PORT, AO_PAD_PIN_4, 0);
611 ao_enable_output(AO_PAD_5_PORT, AO_PAD_PIN_5, 0);
614 ao_enable_output(AO_PAD_6_PORT, AO_PAD_PIN_6, 0);
617 ao_enable_output(AO_PAD_7_PORT, AO_PAD_PIN_7, 0);
620 ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, 0);
623 ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, 0);
625 ao_cmd_register(&ao_pad_cmds[0]);
626 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
627 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
628 ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");