X-Git-Url: http://git.kpe.io/?p=avr_bc100.git;a=blobdiff_plain;f=BaseMegaFirmware%2FGCC%2Fmain.c;fp=BaseMegaFirmware%2FGCC%2Fmain.c;h=f467b4b34977a505824f2fa6c1e3a3b04c78e5ce;hp=0000000000000000000000000000000000000000;hb=89173ec9cd6f33843bf27c45be0f2f9be3f5f0ce;hpb=7997f136b12114035265bd082c854bff95fd73dc diff --git a/BaseMegaFirmware/GCC/main.c b/BaseMegaFirmware/GCC/main.c new file mode 100644 index 0000000..f467b4b --- /dev/null +++ b/BaseMegaFirmware/GCC/main.c @@ -0,0 +1,250 @@ +/* Copyright (c) 2008 Martin Thomas + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + * Neither the name of the copyright holders nor the names of + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +#define VERSION_STRING "V0.1 4/2008" + +#include +#include +#include +#include +#include +#include +#include + +#include "uart.h" +#include "mystdio.h" +#include "bc100_slave.h" +#include "port_ext.h" +#include "timebase.h" +#include "key_io.h" + +#ifndef F_CPU +#warning "F_CPU not defined - will do now" +#define F_CPU 7372800UL +#endif + +#define UART_BAUD_RATE 115200 + +static void fun_delay() +{ + uint8_t i; + for (i=0;i<10;i++) _delay_ms(10); +} + +static void fun(void) +{ + uint8_t i, j; + + for ( i=0; i<5; i++) { + port_ext_set( 1, 0x0f ); + port_ext_update(); + fun_delay(); + port_ext_set( 1, 0xf0 ); + port_ext_update(); + fun_delay(); + } + port_ext_set( 1, 0x00 ); + port_ext_update(); + + for ( i=0; i<2; i++) { + for ( j=0; j<4; j++ ) { + port_ext_bit_set( 1, j ); + port_ext_bit_set( 1, j+4 ); + port_ext_update(); + fun_delay(); + } + for ( j=4; j>0; j-- ) { + port_ext_bit_clear( 1, j-1 ); + port_ext_bit_clear( 1, j-1+4 ); + port_ext_update(); + fun_delay(); + } + } + port_ext_set( 1, 0x00 ); + port_ext_update(); +} + +static void slave_run(void) +{ + port_ext_bit_set( 0, 0 ); + port_ext_bit_set( 0, 1 ); + port_ext_update(); +} + +static void slave_stop(void) +{ + port_ext_bit_clear( 0, 0 ); + port_ext_bit_clear( 0, 1 ); + port_ext_update(); +} + +int main(void) +{ + uint16_t ui_input, tick_local, tick_now; + uint8_t c, show_menu; + + // wait for power-button release (paranoia?) + while ( !( PIND & ( 1 << PD5 ) ) ) { ; } + key_io_init(); + + uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); + mystdio_init(); + + timebase_init(); + + sei(); + + myprintf_P("\n\nBC100 Master Firmware, (C) Martin Thomas " VERSION_STRING "\n"); + + + bc100_slave_init(); + + port_ext_init(); + fun(); + slave_run(); + + show_menu = 1; + tick_local = timebase_get_tick(); + + while ( 1 ) { + if ( show_menu ) { + myprintf_P("(1) 0x55 to LEDs\n"); + myprintf_P("(2) 0xAA to LEDs\n"); + myprintf_P("(3) MASTER_INT low\n"); + myprintf_P("(4) MASTER_INT high\n"); + myprintf_P("(f) Power Off\n"); + myprintf_P("(t) Test Slave Comm\n"); + myprintf_P("(r) Reset Slave\n"); + myprintf_P("(h) Show this again\n"); + myprintf_P("Select > "); + show_menu = 0; + } + + ui_input = uart_getc(); + if ( ui_input & UART_NO_DATA ) { + /* not data */ + } + else { + if ( ui_input & UART_FRAME_ERROR ) { + uart_puts_P("UART Frame Error: "); + } + if ( ui_input & UART_OVERRUN_ERROR ) { + uart_puts_P("UART Overrun Error: "); + } + if ( ui_input & UART_BUFFER_OVERFLOW ) { + uart_puts_P("Buffer overflow error: "); + } + + c = (uint8_t)ui_input; + myprintf_P("%c\n", c); + switch ( c ) { +#ifdef BC100_SLAVE_TEST + case 't' : + if ( ( PORTD & (1< "); + } + } /* key detected */ + + tick_now = timebase_get_tick(); + if ( (uint16_t)( tick_now - tick_local ) > 200 ) { + // myprintf_P("Keys: %02x", key_io_state_hardware() ); + tick_local = tick_now; + } + + if ( key_io_get_press( KEY_IO_KEY_SW0 ) ) { + myprintf_P("SW0 press\n"); + } + if( key_io_get_longpress( KEY_IO_KEY_SW1 ) ) { + myprintf_P("SW1 long\n"); + } + if( key_io_get_was_short( KEY_IO_KEY_SW2 ) ) { + myprintf_P("SW2 was short\n"); + } + if( key_io_get_rpt( KEY_IO_KEY_SW2 ) ) { + myprintf_P("SW2 repeat/long\n"); + } + if ( key_io_get_press( KEY_IO_KEY_SW3 ) ) { + myprintf_P("SW3 pres\n"); + } + + if( key_io_get_press( KEY_IO_KEY_POWER ) ) { + myprintf_P("POWER Key hit -> Power off\n"); + while ( !( PIND & (1 << PD5) ) ) { ; } + slave_stop(); + port_ext_bit_clear( 0, 2 ); + port_ext_update(); + } + + + } /* main-loop */ +}