00001 /* 00002 * Copyright (C) 2000-2005 by ETH Zurich 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the copyright holders nor the names of 00014 * contributors may be used to endorse or promote products derived 00015 * from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS 00018 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00019 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00020 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH 00021 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00022 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00023 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00024 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00025 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00026 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00027 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00028 * SUCH DAMAGE. 00029 * 00030 * For additional information see http://www.btnode.ethz.ch 00031 * 00032 */ 00033 00045 #include <stdio.h> 00046 #include <sys/types.h> 00047 #include <sys/timer.h> 00048 00049 #include <hardware/btn-hardware.h> 00050 #include <led/btn-led.h> 00051 #include <hardware/btn-bat.h> 00052 #include <support/bt_remoteprog.h> 00053 00054 #include <bt/bt_hci_cmds.h> 00055 #include <bt/bt_l2cap.h> 00056 00057 #include <dev/usartavr.h> 00058 00059 #include "program_version.h" 00060 00061 00062 //#define MAX_MTU BT_L2CAP_MTU_DEFAULT 00063 #define MAX_MTU 1024 00064 00065 #define BAUD 57600 00066 00067 #define THRESHOLD_MV 2000 00068 00069 #define BT_L2CAP_HCI_PACKET_TYPE ( BT_HCI_PACKET_TYPE_DM1 | BT_HCI_PACKET_TYPE_DM3 | BT_HCI_PACKET_TYPE_DM5 ) 00070 00071 #define BLUE 0 00072 #define RED 1 00073 #define YELLOW 2 00074 #define GREEN 3 00075 00076 00077 // the bluetooth stack (for HCI and L2CAP) 00078 struct btstack* stack; 00079 struct bt_l2cap_stack* l2cap_stack; 00080 00081 char intstr[10]; 00082 00083 u_char rp_reset_cb(u_char arg) 00084 { 00085 return 1; 00086 } 00087 00088 void check_battery(void){ 00089 00090 u_int volt_mV; 00091 00092 // check supply, else dont boot up bluetooth 00093 if ( !btn_external_power() ) 00094 { 00095 volt_mV = btn_bat_measure(10); 00096 printf_P(PSTR("battery: %d.%02d V\n\n"), volt_mV / 1000, 00097 (volt_mV % 1000)/10 ); 00098 00099 if ( volt_mV < THRESHOLD_MV ) 00100 { 00101 puts_P(PSTR("ERROR: not enough power...\nHALT.\n")); 00102 btn_led_set(RED); 00103 while (1); 00104 } 00105 } 00106 } 00107 00108 /* 00109 * Main 00110 * 00111 */ 00112 int main(void) 00113 { 00114 00115 // set bps for serial port (8N1N) 00116 u_long baud = BAUD; 00117 00118 long ret; 00119 00120 btn_hardware_init(); 00121 00122 btn_led_init(1); 00123 btn_led_set(BLUE); 00124 00125 // Initialize UART 00126 NutRegisterDevice(&APP_UART, 0, 0); 00127 freopen(APP_UART.dev_name, "r+",stdout); 00128 _ioctl(_fileno(stdout), UART_SETSPEED, &baud); 00129 00130 puts_P(PSTR("\n")); 00131 00132 ret = 0; 00133 00134 check_battery(); 00135 00136 puts_P(PSTR("bootup bluetooth...")); 00137 00138 //_bt_hci_debug_uart = 1; 00139 btn_hardware_bt_on(); 00140 00141 puts_P(PSTR("done.\n")); 00142 00143 // TODO Remove Sleep 00144 // sleep a little to prevent crashes 00145 NutSleep(70); 00146 00147 puts_P(PSTR("build up HCI and L2CAP stack.")); 00148 stack = bt_hci_init(&BT_UART); 00149 00150 //ret = bt_hci_set_baudrate(stack,BT_HCI_B115200); 00151 //if (ret) 00152 // puts_P(PSTR("WARNING: Cannot set baudrate.\n")); 00153 00154 // TODO Remove Sleep 00155 // sleep a little to prevent crashes 00156 NutSleep(70); 00157 00158 // set fancy bt name 00159 if (bt_hci_write_local_name(stack,BT_HCI_SYNC,(u_char*) "Remote-Programmable-BTnode")) 00160 puts_P(PSTR("WARNING: Cannot set local name.\n")); 00161 00162 // set bt class of device 00163 if (bt_hci_write_local_cod(stack,BT_HCI_SYNC,100)) 00164 puts_P(PSTR("WARNING: Cannot set local class of device.\n")); 00165 00166 // init l2cap stack 00167 l2cap_stack = bt_l2cap_init(stack,8,1,BT_L2CAP_HCI_PACKET_TYPE); 00168 puts_P(PSTR("done.\n")); 00169 00170 // enabl remote programming on default PSM (see in support/bt_remoteprog.h> 00171 bt_remoteprog_init(l2cap_stack, rp_reset_cb); 00172 00173 // main application, show compile time 00174 puts_P(PSTR(PROGRAM_VERSION "\n")); 00175 00176 btn_led_clear(BLUE); 00177 00178 // something to do -> main cannot finish 00179 for (;;) { 00180 NutSleep(-1); 00181 } 00182 return 1; 00183 00184 }