contiki-raven-default-init-lowlevel.c

00001 /*
00002  * Copyright (c) 2006, Technical University of Munich
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
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 Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * Author: Simon Barner <barner@in.tum.de>
00032  *
00033  * @(#)$$
00034  */
00035 #include "contiki-raven.h"
00036 
00037 #include "contiki.h"
00038 #include "usb_drv.h"
00039 #include "usb_descriptors.h"
00040 #include "usb_specific_request.h"
00041 #include <util/delay.h>
00042 #include "bootloader.h"
00043 
00044 uint8_t checkForFinger(void);
00045 
00046 uint8_t fingerPresent = 0;
00047 
00048 /* Defining this allows you to mount the USB Stick as a mass storage device by shorting the two pins. See docs. */
00049 //#define WINXPSP2
00050 
00051 void
00052 init_lowlevel(void)
00053 {
00054     Leds_init();
00055     Leds_off();
00056 
00057         if (checkForFinger()) {
00058                         if(bootloader_is_present())
00059                                 Jump_To_Bootloader();
00060 #ifdef WINXPSP2
00061                                 usb_mode = mass_storage;
00062 #else
00063                 fingerPresent = 1;
00064 #endif
00065         }
00066 
00067  return;
00068 
00069 }
00070 
00071 
00072 uint8_t checkForFinger(void)
00073 {
00074   uint8_t tests;
00075   uint8_t matches;
00076  
00077  /*    
00078          Three pads on RZUSBSTICK go: GND PD3 PD2
00079      
00080          We pulse PD3, and check for that pattern on PD2.
00081 
00082           A (moist) finger across those three pads should be enough
00083           to bridge these
00084   */
00085 
00086   //Output
00087   DDRD |= 1<<PD3;
00088  
00089   //Input
00090   DDRD &= ~(1<<PD2);
00091 
00092 
00093 
00094   tests = 100;
00095   matches = 0;
00096   while(tests) {
00097 
00098       //Set bit PD3 to value of LSB of 'tests'
00099           PORTD = (PORTD & ~(1<<PD3)) | ( (tests & 0x01) << PD3);
00100 
00101           //Allow changes to propogate
00102           _delay_us(1);
00103 
00104           //Check if PD2 matches what we set PD3 to
00105           if ((PIND & (1<<PD2)) == ((tests & 0x01) << PD2)) {
00106                         matches++;      
00107           }
00108 
00109           tests--;
00110    }
00111 
00112    if (matches > 70) {
00113       return 1;
00114    }
00115 
00116    return 0;
00117 }

Generated on Mon Apr 11 14:23:42 2011 for Contiki 2.5 by  doxygen 1.6.1