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 <Lib/MacroLib.h>
30 #include <scan_loop.h>
42 // ----- Variables -----
44 // Keeps track of the sequence used to reflash the teensy in software
45 static uint8_t Bootloader_ConditionSequence[] = {1,16,6,11};
46 uint8_t Bootloader_ConditionState = 0;
47 uint8_t Bootloader_NextPositionReady = 1;
51 // ----- Functions -----
53 void jumpToBootloader(void)
56 // disable watchdog, if enabled
57 // disable all peripherals
59 USBCON = (1<<FRZCLK); // disable USB
63 #if defined(__AVR_AT90USB162__) // Teensy 1.0
64 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
65 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
66 DDRB = 0; DDRC = 0; DDRD = 0;
67 PORTB = 0; PORTC = 0; PORTD = 0;
68 asm volatile("jmp 0x3E00");
69 #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
70 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
71 TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
72 DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
73 PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
74 asm volatile("jmp 0x7E00");
75 #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
76 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
77 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
78 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
79 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
80 asm volatile("jmp 0xFC00");
81 #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
82 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
83 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
84 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
85 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
86 asm volatile("jmp 0x1FC00");
90 // Given a sampling array, and the current number of detected keypress
91 // Add as many keypresses from the sampling array to the USB key send array as possible.
92 inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
94 uint8_t Bootloader_KeyDetected = 0;
95 uint8_t processed_keys = 0;
97 // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
98 for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
100 if ( keys[key] & (1 << 7) )
104 // Display the detected scancode
106 int8ToStr( key, tmpStr );
107 dPrintStrs( tmpStr, " " );
109 // Is this a bootloader sequence key?
110 if ( !Bootloader_KeyDetected
111 && Bootloader_NextPositionReady
112 && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
114 Bootloader_KeyDetected = 1;
115 Bootloader_NextPositionReady = 0;
116 Bootloader_ConditionState++;
118 else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
120 Bootloader_KeyDetected = 1;
123 // Determine if the key is a modifier
124 uint8_t modFound = 0;
125 for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
127 if ( modifiers[mod] == key ) {
128 USBKeys_Modifiers |= map[key];
134 // Modifier, already done this loop
139 if ( USBKeys_Sent >= USBKeys_MaxSize )
141 info_print("USB Key limit reached");
146 // Allow ignoring keys with 0's
148 USBKeys_Array[USBKeys_Sent++] = map[key];
152 // Boot loader sequence state handler
153 switch ( processed_keys )
155 // The next bootloader key can now be pressed, if there were no keys processed
157 Bootloader_NextPositionReady = 1;
159 // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
160 // This case purposely falls through
162 if ( Bootloader_KeyDetected )
165 Bootloader_ConditionState = 0;
169 // Add debug separator if keys sent via USB
170 if ( USBKeys_Sent > 0 )
171 print("\033[1;32m|\033[0m\n");
174 inline void process_macros(void)
176 // Online process macros once (if some were found), until the next USB send
177 if ( USBKeys_Sent != 0 )
180 // Debounce Sampling Array to USB Data Array
181 keyPressDetection( KeyIndex_Array, KeyIndex_Size, MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
183 // Check for bootloader condition
184 if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )