Contiki 2.5
contiki-raven-default-init-lowlevel.c
1 /*
2  * Copyright (c) 2006, Technical University of Munich
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Simon Barner <barner@in.tum.de>
32  *
33  * @(#)$$
34  */
35 #include "contiki-raven.h"
36 
37 #include "contiki.h"
38 #include "usb_drv.h"
39 #include "usb_descriptors.h"
40 #include "usb_specific_request.h"
41 #include <util/delay.h>
42 #include "bootloader.h"
43 
44 uint8_t checkForFinger(void);
45 
46 uint8_t fingerPresent = 0;
47 
48 /* Defining this allows you to mount the USB Stick as a mass storage device by shorting the two pins. See docs. */
49 //#define WINXPSP2
50 
51 void
52 init_lowlevel(void)
53 {
54  Leds_init();
55  Leds_off();
56 
57  if (checkForFinger()) {
58  if(bootloader_is_present())
59  Jump_To_Bootloader();
60 #ifdef WINXPSP2
61  usb_mode = mass_storage;
62 #else
63  fingerPresent = 1;
64 #endif
65  }
66 
67  return;
68 
69 }
70 
71 
72 uint8_t checkForFinger(void)
73 {
74  uint8_t tests;
75  uint8_t matches;
76 
77  /*
78  Three pads on RZUSBSTICK go: GND PD3 PD2
79 
80  We pulse PD3, and check for that pattern on PD2.
81 
82  A (moist) finger across those three pads should be enough
83  to bridge these
84  */
85 
86  //Output
87  DDRD |= 1<<PD3;
88 
89  //Input
90  DDRD &= ~(1<<PD2);
91 
92 
93 
94  tests = 100;
95  matches = 0;
96  while(tests) {
97 
98  //Set bit PD3 to value of LSB of 'tests'
99  PORTD = (PORTD & ~(1<<PD3)) | ( (tests & 0x01) << PD3);
100 
101  //Allow changes to propogate
102  _delay_us(1);
103 
104  //Check if PD2 matches what we set PD3 to
105  if ((PIND & (1<<PD2)) == ((tests & 0x01) << PD2)) {
106  matches++;
107  }
108 
109  tests--;
110  }
111 
112  if (matches > 70) {
113  return 1;
114  }
115 
116  return 0;
117 }