#include <avr/io.h>
+#include <avr/interrupt.h>
#include <stdio.h>
#include "chassis.h"
+#ifdef __AVR_ATmega16__
+#define CHASSISPORT B
+#define POWER_PIN 0
+#define RESET_PIN 1
+#define ACTIVE_LOW
+#else
+#error "Please add chassis power/reset-PIN information for this chip"
+#endif
+
+#define __CPORT(port) PORT##port
+#define _CPORT(port) __CPORT(port)
+#define CPORT _CPORT(CHASSISPORT)
+
+#define __CDDR(port) DDR##port
+#define _CDDR(port) __CDDR(port)
+#define CDDR _CDDR(CHASSISPORT)
+
+static volatile int power_cnt = 0;
+static volatile int reset_cnt = 0;
+
+static void chassis_set_pins(uint8_t pins, uint8_t state);
+
void chassis_init()
{
- DDRB = 0xff;
- PORTB = 0xff;
+ chassis_set_pins((1<<POWER_PIN) | (1<<RESET_PIN), 0);
+ CDDR |= ((1<<POWER_PIN) | (1<<RESET_PIN));
+
+ /* About 1ms */
+ OCR0 = ((F_CPU/256)/1000)-1;
+
+ TCCR0 = ((1<<WGM01) | (1<<CS02)); /* CTC, Prescaler 256 */
+ TIMSK |= (1<<OCIE0);
+
+}
+
+static void chassis_set_pins(uint8_t pins, uint8_t state)
+{
+#ifdef ACTIVE_LOW
+ state = !state;
+#endif
+
+ if(state) {
+ CPORT |= pins;
+ } else {
+ CPORT &= ~pins;
+ }
+}
+
+static void chassis_power(int msec)
+{
+ uint8_t old_SREG = SREG;
+
+ chassis_set_pins((1<<POWER_PIN), 1);
+
+ cli();
+ if (!power_cnt)
+ power_cnt = msec;
+ SREG = old_SREG;
+}
+
+static void chassis_reset(int msec)
+{
+ uint8_t old_SREG = SREG;
+
+ chassis_set_pins((1<<RESET_PIN), 1);
+
+ cli();
+ if (!reset_cnt)
+ reset_cnt = msec;
+ SREG = old_SREG;
}
void chassis_control(unsigned char action)
switch(action) {
case CHASSIS_ACTION_POWER_DOWN:
- PORTB=0xff;
+ chassis_power(5000);
break;
case CHASSIS_ACTION_POWER_UP:
- PORTB=0x00;
+ chassis_power(500);
break;
case CHASSIS_ACTION_HARD_RESET:
- PORTB=0x55;
+ chassis_reset(500);
break;
default:
break;
}
}
+
+ISR(TIMER0_COMP_vect)
+{
+ if (power_cnt) {
+ power_cnt--;
+
+ if (!power_cnt)
+ chassis_set_pins((1<<POWER_PIN), 0);
+ }
+ if (reset_cnt) {
+ reset_cnt--;
+
+ if (!reset_cnt)
+ chassis_set_pins((1<<RESET_PIN), 0);
+ }
+}