initial commit - controller1 working

Mon, 14 Nov 2011 20:31:32 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Mon, 14 Nov 2011 20:31:32 +0100
changeset 0
9b7de464f0ea
child 1
91246416e201

initial commit - controller1 working

blackbox/Makefile file | annotate | diff | comparison | revisions
blackbox/driver/adc.c file | annotate | diff | comparison | revisions
blackbox/driver/adc.h file | annotate | diff | comparison | revisions
blackbox/driver/rs232.c file | annotate | diff | comparison | revisions
blackbox/driver/rs232.h file | annotate | diff | comparison | revisions
blackbox/lowlevel.c file | annotate | diff | comparison | revisions
blackbox/lowlevel.h file | annotate | diff | comparison | revisions
blackbox/main.c file | annotate | diff | comparison | revisions
blackbox/main.h file | annotate | diff | comparison | revisions
blackbox/reverse.engineer file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/Makefile	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,93 @@
+PRG=main
+
+MCU=atmega16
+FUSES=-U lfuse:w:0x2f:m -U hfuse:w:0xc8:m
+BOOTFUSES=-U lfuse:w:0xff:m -U hfuse:w:0xce:m
+
+#F_CPU=4185600
+#F_CPU = 14318000
+
+F_CPU = 8000000
+
+BAUD=38400
+ISP_BAUD = 115200
+
+#SRC = main.c seriald.c driver/ADC.c driver/clock.c driver/timer.c
+#SRC = main.c driver/rs232.c driver/manchester.c
+SRC = main.c driver/rs232.c driver/adc.c
+SRC += lowlevel.c
+
+###################################################################
+# You possibly do not need to change settings below this marker
+###################################################################
+
+# Binaries to be used
+# You may add the path to them if they are not in the PATH variable.
+CC      = avr-gcc
+OBJCOPY = avr-objcopy
+OBJDUMP = avr-objdump
+AVRDUDE = avrdude
+PERL    = perl
+
+# Optional library search path
+LIBS =
+
+# Compiler options for all c source files
+CFLAGS += -g -Wall -mmcu=$(MCU) -DBAUD=$(BAUD) -DF_CPU=$(F_CPU)UL -std=gnu99 
+CFLAGS += -funsigned-char
+CFLAGS += -funsigned-bitfields
+CFLAGS += -fpack-struct
+CFLAGS += -fshort-enums
+CFLAGS += -Wstrict-prototypes
+CFLAGS += -Wundef
+#CFLAGS += -save-temps
+
+# optimize for size
+CFLAGS += -Os
+# dont optimize
+#CFLAGS += -O0
+
+# Linker options
+LDFLAGS = -Wl,-Map,$(PRG).map
+
+# Enable floating-point support in printf
+#LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm
+
+###################################################################
+# TARGET DEFINITIONS:
+
+
+all: code
+
+code: $(PRG).hex
+
+$(PRG).elf: $(SRC:.c=.o)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
+
+%.lst: %.elf
+	$(OBJDUMP) -h -S $< > $@
+
+%.hex: %.elf
+	$(OBJCOPY) -j .text -j .data -O ihex $< $@
+	rm $(PRG).elf
+	rm $(PRG).map
+
+program: code
+	$(AVRDUDE) -c stk500v2 -b $(ISP_BAUD) -i 1 -p $(MCU) -V -U flash:w:$(PRG).hex:i
+
+fuse:
+	$(AVRDUDE) -c stk500 -p $(MCU) -V $(FUSES)
+
+clean:
+	rm -rf *.o *.elf *.elf.src *.s *.i
+	rm -rf driver/*.o
+
+upgrade: code
+	$(RESETCOMMAND)
+	./bootloader -d $(NETDEV) -b $(UPGRADE_BAUD) -p $(PRG).hex
+
+bootloader: bootload.hex
+	$(AVRDUDE) -p $(MCU) -c stk500 -V -U flash:w:bootload.hex:i
+
+bootfuses:
+	$(AVRDUDE) -p $(MCU) -c stk500 $(BOOTFUSES)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/driver/adc.c	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,79 @@
+#include "adc.h"
+#include <avr/io.h> 
+#include <string.h>
+#include <stdint.h>
+
+
+// Functions to access the internal ADC
+
+// internal ADC reference configuration
+static uint8_t adcReference=(1<<REFS0); // = VCC 5V
+
+// get a sample from ADC chip.
+// chanell must be in range 0-7.
+uint16_t getADC(int channel) {
+    if (channel == 0)
+        adcReference = ((1<<REFS1) | (1<<REFS0)); // switch to 2.56V (~2x
+        //adcReference = (1<<REFS1); // switch to 1.1v (~4.5x) - not on ATMEGA16
+    else
+        adcReference = (1<<REFS0); // switch to 5v (1x)
+    ADMUX = adcReference + channel;
+    // start one sample
+    ADCSRA |= (1<<ADSC);
+    // wait for the result
+    loop_until_bit_is_clear(ADCSRA, ADSC);
+    return ADC;
+}
+
+// initialize the ADC 
+void initADC() {
+    ADCSRA |= (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
+}
+
+// select reference
+// value can be "VCC", "1.1V or "2.56V"
+// returns 1 on success
+uint8_t setAREF(char* name) {
+    if (strcmp(name,"VCC")==0)
+        adcReference=(1<<REFS0);
+    else if (strcmp(name,"1.1V")==0)
+        adcReference=(1<<REFS1);
+    else if (strcmp(name,"2.56V")==0)
+        adcReference=((1<<REFS1) | (1<<REFS0));
+    else if (strcmp(name,"EXT")==0)     
+        adcReference=0;                        
+    else 
+        return 0;
+    // Perform one sample to initialize the ADC on the new reference
+    ADMUX = adcReference;
+    ADCSRA |= (1<<ADSC);
+    loop_until_bit_is_clear(ADCSRA, ADSC);
+    (ADC);
+    return 1;
+}
+
+#ifdef EXT_AREF
+// get current current voltage
+// This assumes that VCC is 5.0 Volt.
+float getAREF(void) {
+    uint8_t aref=ADMUX & ((1<<REFS1) | (1<<REFS0));
+    if (aref == 0)
+        return EXT_AREF/1024;        
+    else if (aref == (1<<REFS0))
+        return 5.00/1024;        
+    else if (aref == (1<<REFS1))
+        return 1.10/1024;
+    else
+        return 2.56/1024;
+}
+#endif
+
+
+uint16_t getADC_smooth(uint8_t channel, uint8_t samples) {
+    uint16_t result = 0;
+    unsigned char i;
+    for (i=0; i<samples; i++) {
+        result += getADC(channel);
+    }
+    return (result / samples);
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/driver/adc.h	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,30 @@
+#ifndef _ADC_H__
+#define _ADC_H__
+#include <stdint.h>
+
+// Functions to access the ADC
+
+// We have two implementations:
+// MCP3204.c and internal_ADC.c
+
+
+// get a sample from ADC chip.
+uint16_t getADC(int channel);
+
+// initialize the ADC chip
+void initADC(void);
+
+// select reference
+// returns 1 on success
+uint8_t setAREF(char* name);
+
+#ifdef EXT_AREF
+    // get current reference voltage
+    float getAREF(void);
+#endif
+
+// get smoothed adc readings
+uint16_t getADC_smooth(uint8_t channel, uint8_t samples);
+
+
+#endif // _ADC_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/driver/rs232.c	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,64 @@
+#include <util/setbaud.h>
+#include <util/delay.h>
+#include <avr/interrupt.h>
+#include <avr/pgmspace.h>
+#include <stdlib.h>
+#include "../main.h"
+#include "rs232.h"
+
+void RS232_init(void) {
+    // first init UART
+    // set baud rate
+    UBRRH = UBRRH_VALUE;
+    UBRRL = UBRRL_VALUE;
+    #if USE_2X
+        UCSRA |= (1 << U2X);
+    #else
+        UCSRA &= ~(1 << U2X);
+    #endif
+    // enable receiver and transmitter and Rx interrupts
+    UCSRB = (1<<RXEN) | (1<<TXEN)  | (1 << RXCIE);
+    // framing format 8N1
+    UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0);
+    // enable interrupts
+    sei();
+}
+
+static void RS232_txc(void) {
+    while (!(UCSRA & (1<<TXC))); // wait for TXC
+    UCSRA &= ~(1<<TXC); // clear TXC
+}
+
+void RS232_putc(char c) {
+    // send char
+    loop_until_bit_is_set(UCSRA, UDRE);
+    UDR = c;
+    //loop_until_bit_is_set(UCSRA, UDRE);
+    //RS232_txc();
+}
+
+void RS232_puts(char* s) {
+    while (*s != 0) {
+        // send char
+        loop_until_bit_is_set(UCSRA, UDRE);
+        UDR = *s;
+        s++;
+    }
+    //loop_until_bit_is_set(UCSRA, UDRE);
+    //RS232_txc();
+}
+
+void RS232_puts_p(char* s) {
+    char c=pgm_read_byte(s);
+    while (c != 0) {
+        // send char
+        loop_until_bit_is_set(UCSRA, UDRE);
+        UDR = c;
+        c=pgm_read_byte(++s);
+    }
+
+    //loop_until_bit_is_set(UCSRA, UDRE);
+    //RS232_txc();
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/driver/rs232.h	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,12 @@
+#ifndef __RS232_H__
+#define __RS232_H__
+
+#define RS232_BUFSIZE   15
+
+extern void RS232_init(void);
+extern void RS232_putc(char c);
+extern void RS232_puts(char* s);
+extern void RS232_puts_p(char* s);
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/lowlevel.c	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,36 @@
+#include "stdint.h"
+#include "main.h"
+
+void LED(uint8_t num, uint8_t state) {
+    switch (num) {
+        case 1: switch (state) {
+            case 0: LED1_PORT &= ~_BV(LED1); break;
+            case 1: LED1_PORT |= _BV(LED1); break;
+            case 2: LED1_PORT ^= _BV(LED1); break;
+            } break;
+
+        case 2: switch (state) {
+            case 0: LED2_PORT &= ~_BV(LED2); break;
+            case 1: LED2_PORT |= _BV(LED2); break;
+            case 2: LED2_PORT ^= _BV(LED2); break;
+            } break;
+
+        case 3: switch (state) {
+            case 0: LED3_PORT &= ~_BV(LED3); break;
+            case 1: LED3_PORT |= _BV(LED3); break;
+            case 2: LED3_PORT ^= _BV(LED3); break;
+            } break;
+
+        case 4: switch (state) {
+            case 0: LED4_PORT &= ~_BV(LED4); break;
+            case 1: LED4_PORT |= _BV(LED4); break;
+            case 2: LED4_PORT ^= _BV(LED4); break;
+            } break;
+
+        case 5: switch (state) {
+            case 0: LED5_PORT &= ~_BV(LED5); break;
+            case 1: LED5_PORT |= _BV(LED5); break;
+            case 2: LED5_PORT ^= _BV(LED5); break;
+            } break;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/lowlevel.h	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,8 @@
+#ifndef LOWLEVEL_H
+#define LOWLEVEL_H
+#include "main.h"
+
+void LED(uint8_t num, uint8_t state);
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/main.c	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,362 @@
+#include <avr/interrupt.h>
+#include <avr/io.h>
+#include <avr/wdt.h>
+#include <avr/eeprom.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <avr/pgmspace.h>
+#include <util/delay.h>
+
+#include "main.h"
+
+#include "driver/rs232.h"
+#include "driver/adc.h"
+
+#include "lowlevel.h"
+
+//#include "driver/manchester.h"
+
+/*
+// Hardware config
+#define LAP_COUNTER_PORT        PORTB
+#define LAP_COUNTER             PB2
+
+#define MODUL_PORT              PORTD
+#define MODUL_ST4               PD5
+#define MODUL_ST6               PD6
+
+#define I2C_PORT                PORTC
+#define I2C_SDA                 PC0
+#define I2C_SCL                 PC1
+
+
+#define SW_PACECAR_PORT         PORTC
+#define SW_START_PORT           PORTB
+#define SW_TANK_PORT            PORTB
+#define SW_PACECAR              PC6
+#define SW_TANK                 PB0
+#define SW_START                PB1
+
+#define SPEAKER_PORT            PORTD
+#define SPEAKER                 PD7
+
+*/
+
+
+void init_hardware(void) {
+    RS232_init(); // initialize RS485 interface
+    RS232_puts_p(PSTR("CARRERA beta loading\n"));
+
+    initADC();
+
+    // set LED output
+    DDR(LED1_PORT) |= _BV(LED1);
+    DDR(LED2_PORT) |= _BV(LED2);
+    DDR(LED3_PORT) |= _BV(LED3);
+    DDR(LED4_PORT) |= _BV(LED4);
+    DDR(LED5_PORT) |= _BV(LED5);
+
+    // set Controller Input Pull-UPs
+    CONTROLLER_PORT |= (_BV(CONTROLLER1_SW) | _BV(CONTROLLER2_SW) | _BV(CONTROLLER3_SW) | _BV(CONTROLLER4_SW));
+
+    // switch pull-ups
+    SW_FUEL_PORT |= _BV(SW_FUEL);
+
+    //RAIL_DETECT_PORT |= _BV(RAIL_DETECT); // enable internal pull-up
+    DDR(RAIL_POWER_PORT) |= _BV(RAIL_POWER);
+
+
+    LED(1, 1); _delay_ms(50);
+    LED(2, 1); _delay_ms(50);
+    LED(3, 1); _delay_ms(50);
+    LED(4, 1); _delay_ms(50);
+    LED(5, 1); _delay_ms(50);
+    LED(1, 0); _delay_ms(50);
+    LED(2, 0); _delay_ms(50);
+    LED(3, 0); _delay_ms(50);
+    LED(4, 0); _delay_ms(50);
+    LED(5, 0); _delay_ms(50);
+
+    //TCCR0 = (1<<CS01); //divide by 8
+
+
+    // setup data bit timer
+    TCCR2 = (1<<CS21); //divide by 8
+    TCCR2 |= (1<<WGM21); // set compare match
+    OCR2 = TIMER2_50US;
+    TIMSK |= 1<<OCIE2; //enable timer2 interrupt
+
+    // setup data packet timer
+    //TCCR1A = (1<<COM1A1);
+    TCCR1B = (1<<CS11); //divide by 8
+    TCCR1B |= (1<<WGM12); // set compare match
+    //TCCR1B = (1<<CS11) | (1<<CS10); //divide by 64
+    //TCNT1 = TIMER_7500NS;
+    OCR1A = TIMER1_7500NS;
+    TIMSK |= 1<<OCIE1A; //enable timer1 interrupt
+
+    RS232_puts_p(PSTR("INIT OK\n"));
+
+}
+
+
+
+volatile uint8_t datalen = 0;
+char data[10]; // 8 bytes data buffer + string termination
+
+static char buffer[RS232_BUFSIZE+1];
+static uint8_t buffer_len;
+
+
+// USART0 RX interrupt
+ISR ( USART_RXC_vect ) {
+    char c = UDR;
+
+    // check for buffer overflow
+    if (buffer_len==sizeof(buffer)) {
+        buffer_len=0;
+        if (c == 27) {
+            // escape sequence, store to empty buffer
+            buffer[buffer_len++] = c;
+        }
+    }  else {
+        // collect characters until end of line
+        if (c == 27) {
+            // escape sequence, clear buffer
+            buffer_len = 0;
+            buffer[buffer_len++] = c;
+        } else if ( (c==0xff) && (buffer_len > 3) ) {
+            buffer[buffer_len]=0;
+
+            // packet end received, parse the received packet
+
+            // wait for the next packet
+            buffer_len=0;
+        } else {
+            buffer[buffer_len++]=c;
+        }
+    }
+}
+
+
+volatile uint16_t transmit_buffer;
+volatile uint16_t transmit_buffer_queue;
+volatile uint8_t  transmit_len;
+volatile uint8_t  transmit_len_next;
+volatile uint8_t  transmit_len_queue;
+ISR ( TIMER2_COMP_vect ) {
+    //OCR2 = TIMER2_50US; // make sure that timer2 is 50µs !!!
+    // data packet timer 100µs pro bit...
+    if (transmit_len >= 0xFE) {
+        if (transmit_len != 0xFF) {
+            RAIL_POWER_PORT |= _BV(RAIL_POWER); // end of transmission
+            transmit_len = 0xFF;
+            transmit_buffer = transmit_buffer_queue;
+            transmit_buffer_queue = 0;
+            transmit_len_next = transmit_len_queue;
+        }
+    } else {
+        uint16_t bit = (1<<(transmit_len & 0b01111111));
+        uint16_t clock;
+        if ((transmit_len & 0b10000000) == 0) clock = 0; else clock = 0xffff;
+        if ( ((transmit_buffer ^ clock) & bit) != 0 )
+            RAIL_POWER_PORT |= _BV(RAIL_POWER); else
+            RAIL_POWER_PORT &= ~_BV(RAIL_POWER);
+        if ( (transmit_len & 0b10000000) == 0 ) {
+            // block 0
+            //if (transmit_len == 0) transmit_len = 0xFF; else transmit_len |= 0b10000000; // set clock
+            transmit_len |= 0b10000000; // set clock
+        } else {
+            // block 1, output the current bit
+            transmit_len &= 0b01111111; // reset clock
+            //if (transmit_len != 0) transmit_len--; // next bit
+            if (transmit_len == 0) transmit_len = 0xFE; else transmit_len--; // next bit
+        }
+    }
+}
+
+int insert_queue(uint16_t tmp, uint8_t len) {
+    if (transmit_buffer_queue == 0) {
+        transmit_buffer_queue = tmp;
+        transmit_len_queue = len;
+        return 1;
+    }
+    return 0;
+}
+
+
+int do_controller(uint8_t controller) {
+    // read controller X speed & encode controller data packet
+    uint16_t tmp;
+    switch (controller) {
+        case 0: tmp = getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR; break;
+        case 1: tmp = getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR; break;
+        case 2: tmp = getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR; break;
+        case 3: tmp = getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR; break;
+        case 4: tmp = 0; break; // todo regler 5
+        case 5: tmp = 0; break; // todo regler 6
+    }
+    tmp =  0b1000000000 | (controller << 6) | ((tmp & 0xF) << 1);
+    if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter
+    if ( (PIN(CONTROLLER_PORT) & _BV(CONTROLLER1_SW)) != 0) {
+        tmp |= (1<<5);
+        LED(controller+1, 0);
+    } else LED(controller+1, 1);
+
+    return insert_queue(tmp, 9);
+}
+
+unsigned char mirror( unsigned char n ) {
+    // mirror all 8 bits
+    n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
+    n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
+    n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
+    return n;
+}
+
+
+int do_program(uint8_t controller, uint8_t command, uint8_t parameter) {
+    // send program data packet
+    uint16_t tmp;
+    tmp = 0b1000000000000 | (mirror(parameter) << 4) | mirror(command)| (mirror(controller) >> 5);
+
+    return insert_queue(tmp, 12);
+}
+
+int do_active(void) {
+    // send controller active data packet
+    uint16_t tmp = 0b10000000;
+    if ((getADC(CONTROLLER1_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b11000001;
+    if ((getADC(CONTROLLER2_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10100001;
+    if ((getADC(CONTROLLER3_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10010001;
+    if ((getADC(CONTROLLER4_SPEED) / CONTROLLER_DIVISOR) > 0) tmp |= 0b10001001;
+    // todo: regler 5 und 6
+
+    return insert_queue(tmp, 7);
+}
+
+int do_pace_ghost(void) {
+    // send ghost and pacecar data packet
+    // todo: at the moment, both disabled!
+    uint16_t tmp = 0b1111100000;
+    if ( (PIN(SW_FUEL_PORT) & _BV(SW_FUEL)) != 0) tmp |= 1; // benzinstand aktiv - tankmodusschalter
+    // todo: PC, NH, TK, (KFR, FR)
+
+    return insert_queue(tmp, 9);
+}
+
+/*
+TODO: DIE TIMER1 ISR DARF DIE ADC NICHT ABFRAGEN, NICHTMAL PAKETE BAUEN, NUR TRIGGERN
+DIE PAKETE MÜSSEN IN DER MAIN UNIT GEBAUT WERDEN UND IN EINE QUEUE GESCHOBEN WERDEN
+EINES VORBAUEN IN Q+1, DIE TIMER2 ISR MUSS DANN Q = Q+1 und Q+1 = 0 SETZEN, ERST DANN DARF
+NEUES PAKET IN Q+1 EINGEFÜGT WERDEN
+*/
+
+
+ISR ( TIMER1_COMPA_vect ) {
+    //OCR2 = 0xFF; // make sure that timer2 is synced to timer1, give enough cycles to prepare
+    LED(4,2);
+    // trigger packet transfer:
+    transmit_len = transmit_len_next;
+    
+    // here is some more time to do something else...
+}
+
+
+int main(void)
+{
+
+    unsigned char s[30];
+    uint16_t tmp;
+    uint8_t  packet_index = 1;
+
+
+    init_hardware();
+
+    itoa(TIMER2_50US, s, 10);
+    RS232_puts(s);
+
+    // switch on rails power
+    RAIL_POWER_PORT |= _BV(RAIL_POWER);
+
+    while (1) {
+        //RS232_putc(PIN(RAIL_DETECT_PORT));
+
+        // check for short circuit on the rails
+        if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) {
+            _delay_ms(1);
+            if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) {
+                cli(); // disable ALL Interrupts
+                RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // disable rails power
+                RS232_puts_p(PSTR("!!! SHORT CIRCUIT ON RAILS, POWERED OFF !!!\n"));
+                while (1) {
+                    LED(1, 0);
+                    _delay_ms(100);
+                    LED(1, 1);
+                    _delay_ms(100);
+                }
+            }
+        }
+
+
+
+
+    switch (packet_index) {
+        case 1:
+            if (do_program(7, 19, 0)) packet_index++; // reset
+            //do_program(7, 16, 3); // led an startampel
+            //do_program(0, 4, 0); //
+            break;
+        case 2:
+            if (do_pace_ghost()) packet_index++;
+            break;
+        case 3:
+            if (do_active()) packet_index++;
+            break;
+        case 4:
+            if (do_controller(0)) packet_index++;
+            break;
+        case 5:
+            if (do_controller(4)) packet_index++;
+            break;
+        case 6:
+            if (do_controller(1)) packet_index++;
+            break;
+        case 7:
+            if (do_controller(5)) packet_index++;
+            break;
+        case 8:
+            if (do_controller(2)) packet_index++;
+            break;
+        case 9:
+            if (do_active()) packet_index++;
+            break;
+        case 10:
+            if (do_controller(3)) packet_index = 1;
+            // last packet, so reset packet index
+            break;
+    }
+
+
+
+
+/*
+        do_controller(0);
+        itoa (transmit_len_queue, s, 10);
+        RS232_puts(s);
+        RS232_putc(' ');
+        itoa (transmit_buffer_queue, s, 2);
+        RS232_puts(s);
+        
+        RS232_putc('\n');
+*/
+/*
+        itoa (tmp, s, 2);
+        RS232_puts(s);
+        RS232_putc('\n');
+*/
+
+
+    } // main loop end
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/main.h	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,96 @@
+#ifndef MAIN_H
+#define MAIN_H
+
+#include <avr/wdt.h>
+#include <stdint.h>
+
+// Hardware config
+#define LAP_COUNTER_PORT        PORTB
+#define LAP_COUNTER             PB2
+
+#define MODUL_PORT              PORTD
+#define MODUL_ST4               PD5
+#define MODUL_ST6               PD6
+
+#define I2C_PORT                PORTC
+#define I2C_SDA                 PC0
+#define I2C_SCL                 PC1
+
+#define LED1_PORT               PORTA
+#define LED2_PORT               PORTA
+#define LED3_PORT               PORTC
+#define LED4_PORT               PORTC
+#define LED5_PORT               PORTC
+#define LED1                    PA4
+#define LED2                    PA5
+#define LED3                    PC7
+#define LED4                    PC2
+#define LED5                    PC3
+
+#define SW_PACECAR_PORT         PORTC
+#define SW_START_PORT           PORTB
+#define SW_FUEL_PORT            PORTB
+#define SW_PACECAR              PC6
+#define SW_FUEL                 PB0
+#define SW_START                PB1
+
+#define SPEAKER_PORT            PORTD
+#define SPEAKER                 PD7
+
+#define RAIL_POWER_PORT         PORTD
+#define RAIL_POWER              PD4 // high = rails powered
+#define RAIL_DETECT_PORT        PORTA
+#define RAIL_DETECT             PA7 // low = short circuit detection
+
+#define CONTROLLER_PORT         PORTB
+#define CONTROLLER_MAX          360 // full throttle ADC value
+#define CONTROLLER_DIVISOR      (uint8_t)(CONTROLLER_MAX/15)
+#define CONTROLLER1_SW          4
+#define CONTROLLER2_SW          5
+#define CONTROLLER3_SW          6
+#define CONTROLLER4_SW          7
+#define CONTROLLER1_SPEED       0 // ADC channel #
+#define CONTROLLER2_SPEED       1 // ADC channel #
+#define CONTROLLER3_SPEED       2 // ADC channel #
+#define CONTROLLER4_SPEED       3 // ADC channel #
+
+// TIMING STUFF
+/*
+#define TIMER0_DIVISOR   8
+#define VALUE_50US       0.45e-4
+#define TIMER_50US       0xff - (uint8_t)(VALUE_50US * F_CPU/TIMER0_DIVISOR)
+*/
+/*
+#define VALUE_7500NS     0.85e-2
+#define TIMER_7500NS     0xffff - (uint16_t)(VALUE_7500NS * F_CPU/TIMER1_DIVISOR)
+*/
+//#define TIMER_7500NS     (uint16_t)(VALUE_7500NS * F_CPU/TIMER1_DIVISOR)
+#define TIMER2_DIVISOR   8
+#define VALUE2_50US      0.50e-4
+#define TIMER2_50US      (uint8_t)(VALUE2_50US * F_CPU/TIMER2_DIVISOR)
+
+#define TIMER1_DIVISOR   8
+#define VALUE1_7500NS     0.75e-2
+#define TIMER1_7500NS     (uint16_t)(VALUE1_7500NS * F_CPU/TIMER1_DIVISOR)
+
+
+#define DDR(x) (*(&x - 1))      // address of data direction register of port x
+#define PIN(x) (*(&x - 2))      // address of input register of port x
+
+
+
+// MAYBE USELESS STUFF:
+
+#define uchar unsigned char
+#define uint unsigned int
+
+// Macro used to write to a single I/O pin
+#define writeBit(port,bit,value) { if ((value)>0) (port) |= (1<<bit); else (port) &= ~(1<<bit); } 
+
+// Macro used to read from a single I/O pin
+#define readBit(port,bit) (((port) >> (bit)) & 1)
+
+// we have internal AREF...
+// #define EXT_AREF 1
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blackbox/reverse.engineer	Mon Nov 14 20:31:32 2011 +0100
@@ -0,0 +1,15 @@
+avrdude: safemode: lfuse reads as 2F
+avrdude: safemode: hfuse reads as C8
+
+Paketintervall = 7.5 ms
+
+1 Startbit
+MSB vs LSB-Format???
+8 MHz Systemtakt
+
+
+Datentakt (bits) Bus = 50 µs == 20 KHz
+TIMER0 (8bit mit prescaler 2): alle 400/200 Taktzyklen Manchester Code ausgeben
+
+TIMER1 (16bit) Pakettimer = 7,5 ms == 133 Hz: alle 60000 Taktzyklen ein Paket ausgeben
+

mercurial