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