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.
94 inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
96 uint8_t Bootloader_KeyDetected = 0;
97 uint8_t processed_keys = 0;
99 // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
100 for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
102 if ( keys[key] & (1 << 7) )
106 // Display the detected scancode
108 int8ToStr( key, tmpStr );
109 dPrintStrs( tmpStr, " " );
111 // Is this a bootloader sequence key?
112 if ( !Bootloader_KeyDetected
113 && Bootloader_NextPositionReady
114 && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
116 Bootloader_KeyDetected = 1;
117 Bootloader_NextPositionReady = 0;
118 Bootloader_ConditionState++;
120 else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
122 Bootloader_KeyDetected = 1;
125 // Determine if the key is a modifier
126 uint8_t modFound = 0;
127 for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
129 if ( modifiers[mod] == key ) {
130 USBKeys_Modifiers |= map[key];
136 // Modifier, already done this loop
141 if ( USBKeys_Sent >= USBKeys_MaxSize )
143 info_print("USB Key limit reached");
148 // Allow ignoring keys with 0's
150 USBKeys_Array[USBKeys_Sent++] = map[key];
154 // Boot loader sequence state handler
155 switch ( processed_keys )
157 // The next bootloader key can now be pressed, if there were no keys processed
159 Bootloader_NextPositionReady = 1;
161 // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
162 // This case purposely falls through
164 if ( Bootloader_KeyDetected )
167 Bootloader_ConditionState = 0;
171 // Add debug separator if keys sent via USB
172 if ( USBKeys_Sent > 0 )
173 print("\033[1;32m|\033[0m\n");
177 // Given a list of keypresses, translate into the USB key codes
178 // The buffer is cleared after running
179 // If the buffer doesn't fit into the USB send array, the extra keys are dropped
180 void keyPressBufferRead( uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
182 // Loop through input buffer
183 for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
185 // Get the keycode from the buffer
186 uint8_t key = KeyIndex_Buffer[index];
188 // Determine if the key is a modifier
189 uint8_t modFound = 0;
190 for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
192 if ( modifiers[mod] == key ) {
193 USBKeys_Modifiers |= map[key];
199 // Modifier, already done this loop
204 if ( USBKeys_Sent >= USBKeys_MaxSize )
206 info_print("USB Key limit reached");
211 // Allow ignoring keys with 0's
214 USBKeys_Array[USBKeys_Sent++] = map[key];
218 // Key was not mapped
219 // TODO Add dead key map
221 hexToStr( key, tmpStr );
222 erro_dPrint( "Key not mapped... - ", tmpStr );
227 // Signal buffer that we've used it
228 scan_finishedWithBuffer();
231 inline void process_macros(void)
233 // Online process macros once (if some were found), until the next USB send
234 if ( USBKeys_Sent != 0 )
237 // Query the input buffer for keypresses
238 keyPressBufferRead( MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
240 // Check for bootloader condition
241 //if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
242 // jumpToBootloader();