2 #include <avr/interrupt.h>
8 #ifdef __AVR_ATmega16__
14 #error "Please add chassis power/reset-PIN information for this chip"
17 #define __CPORT(port) PORT##port
18 #define _CPORT(port) __CPORT(port)
19 #define CPORT _CPORT(CHASSISPORT)
21 #define __CDDR(port) DDR##port
22 #define _CDDR(port) __CDDR(port)
23 #define CDDR _CDDR(CHASSISPORT)
25 static volatile int power_cnt
= 0;
26 static volatile int reset_cnt
= 0;
27 static volatile int power_cycle
= 0;
29 static void chassis_set_pins(uint8_t pins
, uint8_t state
);
33 chassis_set_pins((1<<POWER_PIN
) | (1<<RESET_PIN
), 0);
36 OCR0
= ((F_CPU
/64)/1000) - 1;
38 TCCR0
= ((1<<WGM01
) | (1<<CS01
) | (1<<CS00
)); /* CTC, Prescaler 64 */
43 /* gracefully transition between tri-state and active */
44 static void chassis_set_pins(uint8_t pins
, uint8_t state
)
49 /* Pull UPs for a short moment... */
58 CPORT
&= ~pins
; /* NoOp... */
67 static void chassis_power(int msec
)
69 uint8_t old_SREG
= SREG
;
71 chassis_set_pins((1<<POWER_PIN
), 1);
79 static void chassis_reset(int msec
)
81 uint8_t old_SREG
= SREG
;
83 chassis_set_pins((1<<RESET_PIN
), 1);
91 void chassis_control(unsigned char action
)
93 uint8_t old_SREG
= SREG
;
96 printf("Chassis control 0x%02x\n", action
);
100 case CHASSIS_ACTION_POWER_DOWN
:
101 chassis_power(POWERDOWN_TIME_MS
);
104 case CHASSIS_ACTION_POWER_UP
:
105 chassis_power(POWERUP_TIME_MS
);
108 case CHASSIS_ACTION_HARD_RESET
:
109 chassis_reset(RESET_TIME_MS
);
112 case CHASSIS_ACTION_POWER_CYCLE
:
114 power_cycle
= POWER_CYCLE_PAUSE_MS
;
116 chassis_power(POWERDOWN_TIME_MS
);
120 printf("Unimplemented chassis action 0x%02x\n", action
);
126 ISR(TIMER0_COMP_vect
)
132 chassis_set_pins((1<<POWER_PIN
), 0);
133 } else if (power_cycle
) {
137 chassis_set_pins((1<<POWER_PIN
), 1);
138 power_cnt
= POWERUP_TIME_MS
;
145 chassis_set_pins((1<<RESET_PIN
), 0);