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(...) do { if (ao_pad_debug) { printf(__VA_ARGS__); flush(); } } while(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);
142 PRINTD("ao_pad_ignite now %d\n", ao_pad_ignite);
145 ao_gpio_clr_bits(AO_PAD_PORT_0, pins0);
146 ao_gpio_clr_bits(AO_PAD_PORT_1, pins1);
147 PRINTD("turn off pins 0x%x 0x%x\n", pins0, pins1);
149 ao_gpio_clr_bits(AO_PAD_PORT_0, pins0);
150 PRINTD("turn off pins 0x%x\n", pins0);
155 #define AO_PAD_ARM_SIREN_INTERVAL 200
157 /* Resistor values needed for various voltage test ratios:
159 * Net names involved:
161 * V_BATT Battery power, after the initial power switch
162 * V_PYRO Pyro power, after the pyro power switch (and initial power switch)
163 * PYRO_SENSE ADC input to sense V_PYRO voltage
164 * BATT_SENSE ADC input to sense V_BATT voltage
165 * IGNITER FET output to pad (the other pad lead hooks to V_PYRO)
166 * IGNITER_SENSE ADC input to sense igniter voltage
168 * AO_PAD_R_V_BATT_BATT_SENSE Resistor from battery rail to battery sense input
169 * AO_PAD_R_BATT_SENSE_GND Resistor from battery sense input to ground
171 * AO_PAD_R_V_BATT_V_PYRO Resistor from battery rail to pyro rail
172 * AO_PAD_R_V_PYRO_PYRO_SENSE Resistor from pyro rail to pyro sense input
173 * AO_PAD_R_PYRO_SENSE_GND Resistor from pyro sense input to ground
175 * AO_PAD_R_V_PYRO_IGNITER Optional resistors from pyro rail to FET igniter output
176 * AO_PAD_R_IGNITER_IGNITER_SENSE Resistors from FET igniter output to igniter sense ADC inputs
177 * AO_PAD_R_IGNITER_SENSE_GND Resistors from igniter sense ADC inputs to ground
181 ao_pad_decivolt(int16_t adc, int16_t r_plus, int16_t r_minus)
183 int32_t mul = (int32_t) AO_ADC_REFERENCE_DV * (r_plus + r_minus);
184 int32_t div = (int32_t) AO_ADC_MAX * r_minus;
185 return ((int32_t) adc * mul + mul/2) / div;
193 __pdata AO_LED_TYPE prev = 0, cur = 0;
194 __pdata uint8_t beeping = 0;
195 __xdata volatile struct ao_data *packet;
196 __pdata uint16_t arm_beep_time = 0;
198 sample = ao_data_head;
199 ao_led_set(LEDS_AVAILABLE);
200 ao_delay(AO_MS_TO_TICKS(1000));
203 __pdata int16_t pyro;
206 while (sample == ao_data_head)
207 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
211 packet = &ao_data_ring[sample];
212 sample = ao_data_ring_next(sample);
214 /* Reply battery voltage */
215 query.battery = ao_pad_decivolt(packet->adc.batt, AO_PAD_R_V_BATT_BATT_SENSE, AO_PAD_R_BATT_SENSE_GND);
217 /* Current pyro voltage */
218 pyro = ao_pad_decivolt(packet->adc.pyro,
219 AO_PAD_R_V_PYRO_PYRO_SENSE,
220 AO_PAD_R_PYRO_SENSE_GND);
223 if (pyro > query.battery * 7 / 8) {
224 query.arm_status = AO_PAD_ARM_STATUS_ARMED;
227 query.arm_status = AO_PAD_ARM_STATUS_DISARMED;
230 if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
232 else if (ao_radio_cmac_rssi < AO_PAD_RSSI_MINIMUM)
237 for (c = 0; c < AO_PAD_NUM; c++) {
238 int16_t sense = ao_pad_decivolt(packet->adc.sense[c],
239 AO_PAD_R_IGNITER_IGNITER_SENSE,
240 AO_PAD_R_IGNITER_SENSE_GND);
241 uint8_t status = AO_PAD_IGNITER_STATUS_UNKNOWN;
244 * Here's the resistor stack on each
245 * igniter channel. Note that
246 * AO_PAD_R_V_PYRO_IGNITER is optional
249 * AO_PAD_R_V_PYRO_IGNITER igniter
251 * AO_PAD_R_IGNITER_IGNITER_SENSE \
253 * AO_PAD_R_IGNITER_SENSE_GND /
258 #ifdef AO_PAD_R_V_PYRO_IGNITER
259 if (sense <= pyro / 8) {
260 /* close to zero → relay is closed */
261 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED;
262 if ((ao_time() % 100) < 50)
263 cur |= AO_LED_CONTINUITY(c);
268 if (sense >= (pyro * 7) / 8) {
270 /* sense close to pyro voltage; igniter is good
272 status = AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN;
273 cur |= AO_LED_CONTINUITY(c);
276 /* relay not shorted (if we can tell),
277 * and igniter not obviously present
279 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
282 query.igniter_status[c] = status;
285 // PRINTD("change leds from %02x to %02x\n",
292 if (ao_pad_armed && (int16_t) (ao_time() - ao_pad_arm_time) > AO_PAD_ARM_TIME)
299 } else if (query.arm_status == AO_PAD_ARM_STATUS_ARMED && !beeping) {
300 if (arm_beep_time == 0) {
301 arm_beep_time = AO_PAD_ARM_SIREN_INTERVAL;
306 } else if (beeping) {
317 if (!ao_pad_disabled) {
319 ao_radio_recv_abort();
327 ao_wakeup (&ao_pad_disabled);
332 ao_pad_read_box(void)
334 uint8_t byte = ao_74hc165_read();
337 PRINTD("box %02x\n", byte);
344 #ifdef AO_PAD_SELECTOR_PORT
345 static int ao_pad_read_box(void) {
346 AO_PORT_TYPE value = ao_gpio_get_all(AO_PAD_SELECTOR_PORT);
350 for (pin = 0; pin < sizeof (AO_PORT_TYPE) * 8; pin++) {
351 if (AO_PAD_SELECTOR_PINS & (1 << pin)) {
352 if ((value & (1 << pin)) == 0)
357 return ao_config.pad_box;
362 #define ao_pad_read_box() PAD_BOX
370 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_config.pad_box;
385 #ifndef HAS_FIXED_PAD_BOX
386 if (ao_pad_box == 0 || ao_pad_box == 0xff)
387 ao_pad_box = ao_pad_read_box();
390 PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
391 command.tick, command.box, ao_pad_box, command.cmd, command.channels);
393 switch (command.cmd) {
395 if (command.box != ao_pad_box) {
396 PRINTD ("box number mismatch\n");
400 if (command.channels & ~(AO_PAD_ALL_CHANNELS))
403 time_difference = command.tick - ao_time();
404 PRINTD ("arm tick %d local tick %d\n", command.tick, ao_time());
405 if (time_difference < 0)
406 time_difference = -time_difference;
407 if (time_difference > 10) {
408 PRINTD ("time difference too large %d\n", time_difference);
412 ao_pad_armed = command.channels;
413 ao_pad_arm_time = ao_time();
417 if (command.box != ao_pad_box) {
418 PRINTD ("box number mismatch\n");
422 query.tick = ao_time();
423 query.box = ao_pad_box;
424 query.channels = AO_PAD_ALL_CHANNELS;
425 query.armed = ao_pad_armed;
426 PRINTD ("query tick %d box %d channels %02x arm %d arm_status %d igniter %d,%d,%d,%d\n",
427 query.tick, query.box, query.channels, query.armed,
429 query.igniter_status[0],
430 query.igniter_status[1],
431 query.igniter_status[2],
432 query.igniter_status[3]);
433 ao_radio_cmac_send(&query, sizeof (query));
437 PRINTD ("not armed\n");
440 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
441 PRINTD ("late pad arm_time %d time %d\n",
442 ao_pad_arm_time, ao_time());
446 ao_pad_ignite = ao_pad_armed;
447 ao_pad_arm_time = ao_time();
448 ao_wakeup(&ao_pad_ignite);
452 PRINTD ("not armed\n");
456 if (!ao_log_running) ao_log_start();
458 if ((uint16_t) (ao_time() - ao_pad_arm_time) > AO_SEC_TO_TICKS(20)) {
459 PRINTD ("late pad arm_time %d time %d\n",
460 ao_pad_arm_time, ao_time());
464 ao_pad_ignite = ao_pad_armed;
465 ao_pad_arm_time = ao_time();
466 ao_wakeup(&ao_pad_ignite);
468 case AO_PAD_ENDSTATIC:
482 printf ("Arm switch: ");
483 switch (query.arm_status) {
484 case AO_PAD_ARM_STATUS_ARMED:
487 case AO_PAD_ARM_STATUS_DISARMED:
488 printf ("Disarmed\n");
490 case AO_PAD_ARM_STATUS_UNKNOWN:
491 printf ("Unknown\n");
495 for (c = 0; c < AO_PAD_NUM; c++) {
496 printf ("Pad %d: ", c);
497 switch (query.igniter_status[c]) {
498 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_CLOSED: printf ("No igniter. Relay closed\n"); break;
499 case AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN: printf ("No igniter. Relay open\n"); break;
500 case AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN: printf ("Good igniter. Relay open\n"); break;
501 case AO_PAD_IGNITER_STATUS_UNKNOWN: printf ("Unknown\n"); break;
512 if (!ao_match_word("DoIt"))
515 if (ao_cmd_status != ao_cmd_success)
517 ignite = 1 << ao_cmd_lex_i;
519 if (ao_cmd_status != ao_cmd_success) {
521 ao_cmd_status = ao_cmd_success;
523 repeat = ao_cmd_lex_i;
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 __xdata struct ao_task ao_pad_task;
532 static __xdata struct ao_task ao_pad_ignite_task;
533 static __xdata struct ao_task ao_pad_monitor_task;
537 ao_pad_set_debug(void)
540 if (ao_cmd_status == ao_cmd_success)
541 ao_pad_debug = ao_cmd_lex_i != 0;
546 ao_pad_alarm_debug(void)
548 uint8_t which, value;
550 if (ao_cmd_status != ao_cmd_success)
552 which = ao_cmd_lex_i;
554 if (ao_cmd_status != ao_cmd_success)
556 value = ao_cmd_lex_i;
557 printf ("Set %s to %d\n", which ? "siren" : "strobe", value);
565 __code struct ao_cmds ao_pad_cmds[] = {
566 { ao_pad_test, "t\0Test pad continuity" },
567 { ao_pad_manual, "i <key> <n>\0Fire igniter. <key> is doit with D&I" },
569 { ao_pad_set_debug, "D <0 off, 1 on>\0Debug" },
570 { ao_pad_alarm_debug, "S <0 strobe, 1 siren> <0 off, 1 on>\0Set alarm output" },
575 #ifndef AO_PAD_PORT_1
576 #define AO_PAD_0_PORT AO_PAD_PORT
577 #define AO_PAD_1_PORT AO_PAD_PORT
578 #define AO_PAD_2_PORT AO_PAD_PORT
579 #define AO_PAD_3_PORT AO_PAD_PORT
580 #define AO_PAD_4_PORT AO_PAD_PORT
581 #define AO_PAD_5_PORT AO_PAD_PORT
582 #define AO_PAD_6_PORT AO_PAD_PORT
583 #define AO_PAD_7_PORT AO_PAD_PORT
589 #ifdef AO_PAD_SELECTOR_PORT
592 for (pin = 0; pin < sizeof (AO_PORT_TYPE) * 8; pin++) {
593 if (AO_PAD_SELECTOR_PINS & (1 << pin))
594 ao_enable_input(AO_PAD_SELECTOR_PORT, pin, AO_EXTI_MODE_PULL_UP);
598 ao_enable_output(AO_PAD_0_PORT, AO_PAD_PIN_0, AO_PAD_0, 0);
601 ao_enable_output(AO_PAD_1_PORT, AO_PAD_PIN_1, AO_PAD_1, 0);
604 ao_enable_output(AO_PAD_2_PORT, AO_PAD_PIN_2, AO_PAD_2, 0);
607 ao_enable_output(AO_PAD_3_PORT, AO_PAD_PIN_3, AO_PAD_3, 0);
610 ao_enable_output(AO_PAD_4_PORT, AO_PAD_PIN_4, AO_PAD_4, 0);
613 ao_enable_output(AO_PAD_5_PORT, AO_PAD_PIN_5, AO_PAD_5, 0);
616 ao_enable_output(AO_PAD_6_PORT, AO_PAD_PIN_6, AO_PAD_6, 0);
619 ao_enable_output(AO_PAD_7_PORT, AO_PAD_PIN_7, AO_PAD_7, 0);
622 ao_enable_output(AO_STROBE_PORT, AO_STROBE_PIN, AO_STROBE, 0);
625 ao_enable_output(AO_SIREN_PORT, AO_SIREN_PIN, AO_SIREN, 0);
627 ao_cmd_register(&ao_pad_cmds[0]);
628 ao_add_task(&ao_pad_task, ao_pad, "pad listener");
629 ao_add_task(&ao_pad_ignite_task, ao_pad_run, "pad igniter");
630 ao_add_task(&ao_pad_monitor_task, ao_pad_monitor, "pad monitor");