1 /* Copyright (C) 2011 by Jacob Alexander
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // ----- Includes -----
25 #include <util/delay.h>
26 #include <avr/interrupt.h>
31 #include <scan_loop.h>
43 // ----- Variables -----
45 // Keeps track of the sequence used to reflash the teensy in software
46 static uint8_t Bootloader_ConditionSequence[] = {1,16,6,11};
47 uint8_t Bootloader_ConditionState = 0;
48 uint8_t Bootloader_NextPositionReady = 1;
52 // ----- Functions -----
54 void jumpToBootloader(void)
57 // disable watchdog, if enabled
58 // disable all peripherals
60 USBCON = (1<<FRZCLK); // disable USB
64 #if defined(__AVR_AT90USB162__) // Teensy 1.0
65 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
66 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
67 DDRB = 0; DDRC = 0; DDRD = 0;
68 PORTB = 0; PORTC = 0; PORTD = 0;
69 asm volatile("jmp 0x3E00");
70 #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
71 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
72 TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
73 DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
74 PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
75 asm volatile("jmp 0x7E00");
76 #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
77 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
78 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
79 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
80 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
81 asm volatile("jmp 0xFC00");
82 #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
83 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
84 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
85 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
86 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
87 asm volatile("jmp 0x1FC00");
91 // Given a sampling array, and the current number of detected keypress
92 // Add as many keypresses from the sampling array to the USB key send array as possible.
93 inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
95 uint8_t Bootloader_KeyDetected = 0;
96 uint8_t processed_keys = 0;
98 // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
99 for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
101 if ( keys[key] & (1 << 7) )
105 // Display the detected scancode
107 int8ToStr( key, tmpStr );
108 dPrintStrs( tmpStr, " " );
110 // Is this a bootloader sequence key?
111 if ( !Bootloader_KeyDetected
112 && Bootloader_NextPositionReady
113 && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
115 Bootloader_KeyDetected = 1;
116 Bootloader_NextPositionReady = 0;
117 Bootloader_ConditionState++;
119 else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
121 Bootloader_KeyDetected = 1;
124 // Determine if the key is a modifier
125 uint8_t modFound = 0;
126 for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
128 if ( modifiers[mod] == key ) {
129 USBKeys_Modifiers |= map[key];
135 // Modifier, already done this loop
140 if ( USBKeys_Sent >= USBKeys_MaxSize )
142 info_print("USB Key limit reached");
147 // Allow ignoring keys with 0's
149 USBKeys_Array[USBKeys_Sent++] = map[key];
153 // Boot loader sequence state handler
154 switch ( processed_keys )
156 // The next bootloader key can now be pressed, if there were no keys processed
158 Bootloader_NextPositionReady = 1;
160 // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
161 // This case purposely falls through
163 if ( Bootloader_KeyDetected )
166 Bootloader_ConditionState = 0;
170 // Add debug separator if keys sent via USB
171 if ( USBKeys_Sent > 0 )
172 print("\033[1;32m|\033[0m\n");
175 inline void process_macros(void)
177 // Online process macros once (if some were found), until the next USB send
178 if ( USBKeys_Sent != 0 )
181 // Debounce Sampling Array to USB Data Array
182 keyPressDetection( KeyIndex_Array, KeyIndex_Size, MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
184 // Check for bootloader condition
185 if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )