-
Notifications
You must be signed in to change notification settings - Fork 1
/
drivers.cpp
executable file
·384 lines (345 loc) · 8.87 KB
/
drivers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Author - Erez Raviv <erezraviv@gmail.com>
*
* Based on th9x -> http://code.google.com/p/th9x/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "er9x.h"
#ifndef SIMU
#include "avr/interrupt.h"
///opt/cross/avr/include/avr/eeprom.h
static inline void __attribute__ ((always_inline))
eeprom_write_byte_cmp (uint8_t dat, uint16_t pointer_eeprom)
{
//see /home/thus/work/avr/avrsdk4/avr-libc-1.4.4/libc/misc/eeprom.S:98 143
#ifdef CPUM2561
while(EECR & (1<<EEPE)) /* make sure EEPROM is ready */
#else
while(EECR & (1<<EEWE)) /* make sure EEPROM is ready */
#endif
{
if (Ee_lock & EE_TRIM_LOCK) // Only if writing trim changes
{
mainSequence() ; // Keep the controls running while waiting
}
} ;
EEAR = pointer_eeprom;
EECR |= 1<<EERE;
if(dat == EEDR) return;
EEDR = dat;
uint8_t flags=SREG;
cli();
#ifdef CPUM2561
EECR |= 1<<EEMPE;
EECR |= 1<<EEPE;
#else
EECR |= 1<<EEMWE;
EECR |= 1<<EEWE;
#endif
SREG = flags;
}
void eeWriteBlockCmp(const void *i_pointer_ram, uint16_t i_pointer_eeprom, size_t size)
{
const char* pointer_ram = (const char*)i_pointer_ram;
uint16_t pointer_eeprom = i_pointer_eeprom;
while(size){
eeprom_write_byte_cmp(*pointer_ram++,pointer_eeprom++);
size--;
}
}
#endif
//inline uint16_t anaIn(uint8_t chan)
//{
// // ana-in: 3 1 2 0 4 5 6 7
// static prog_char APM crossAna[]={4,2,3,1,5,6,7,0}; // wenn schon Tabelle, dann muss sich auch lohnen
// return s_ana[pgm_read_byte(crossAna+chan)] / 4;
//}
uint8_t s_evt;
uint8_t getEvent()
{
uint8_t evt = s_evt;
s_evt=0;
return evt;
}
class Key
{
#define FILTERBITS 4
#define FFVAL ((1<<FILTERBITS)-1)
#define KSTATE_OFF 0
#define KSTATE_RPTDELAY 95 // gruvin: longer dely before key repeating starts
//#define KSTATE_SHORT 96
#define KSTATE_START 97
#define KSTATE_PAUSE 98
#define KSTATE_KILLED 99
uint8_t m_vals:FILTERBITS; // key debounce? 4 = 40ms
uint8_t unused_m_dblcnt:2;
uint8_t m_cnt;
uint8_t m_state;
public:
void input(bool val, EnumKeys enuk);
bool state() { return m_vals==FFVAL; }
void pauseEvents() { m_state = KSTATE_PAUSE; m_cnt = 0;}
void killEvents() { m_state = KSTATE_KILLED; /*m_dblcnt=0;*/ }
// uint8_t getDbl() { return m_dblcnt; }
};
Key keys[NUM_KEYS];
void Key::input(bool val, EnumKeys enuk)
{
// uint8_t old=m_vals;
uint8_t t_vals ;
// m_vals <<= 1; if(val) m_vals |= 1; //portbit einschieben
t_vals = m_vals ;
t_vals <<= 1 ;
if(val) t_vals |= 1; //portbit einschieben
m_vals = t_vals ;
m_cnt++;
if(m_state && m_vals==0){ //gerade eben sprung auf 0
if(m_state!=KSTATE_KILLED) {
putEvent(EVT_KEY_BREAK(enuk));
// if(!( m_state == 16 && m_cnt<16)){
// m_dblcnt=0;
// }
// }
}
m_cnt = 0;
m_state = KSTATE_OFF;
}
switch(m_state){
case KSTATE_OFF:
if(m_vals==FFVAL){ //gerade eben sprung auf ff
m_state = KSTATE_START;
// if(m_cnt>16) m_dblcnt=0; //pause zu lang fuer double
m_cnt = 0;
}
break;
//fallthrough
case KSTATE_START:
putEvent(EVT_KEY_FIRST(enuk));
Inactivity.inacCounter = 0;
// m_dblcnt++;
#ifdef KSTATE_RPTDELAY
m_state = KSTATE_RPTDELAY;
#else
m_state = 16;
#endif
m_cnt = 0;
break;
#ifdef KSTATE_RPTDELAY
case KSTATE_RPTDELAY: // gruvin: longer delay before first key repeat
if(m_cnt == 32) putEvent(EVT_KEY_LONG(enuk)); // need to catch this inside RPTDELAY time
if (m_cnt == 40) {
m_state = 16;
m_cnt = 0;
}
break;
#endif
case 16:
#ifndef KSTATE_RPTDELAY
if(m_cnt == 32) putEvent(EVT_KEY_LONG(enuk));
//fallthrough
#endif
case 8:
case 4:
case 2:
if(m_cnt >= 48) { //3 6 12 24 48 pulses in every 480ms
m_state >>= 1;
m_cnt = 0;
}
//fallthrough
case 1:
if( (m_cnt & (m_state-1)) == 0) putEvent(EVT_KEY_REPT(enuk));
break;
case KSTATE_PAUSE: //pause
if(m_cnt >= 64) {
m_state = 8;
m_cnt = 0;
}
break;
case KSTATE_KILLED: //killed
break;
}
}
bool keyState(EnumKeys enuk)
{
uint8_t xxx = 0 ;
uint8_t ping = PING ;
if(enuk < (int)DIM(keys)) return keys[enuk].state() ? 1 : 0;
switch((uint8_t)enuk){
case SW_ElevDR : xxx = PINE & (1<<INP_E_ElevDR);
break ;
#if defined(CPUM128) || defined(CPUM2561)
case SW_AileDR :
if ( g_eeGeneral.FrskyPins )
{
xxx = PINC & (1<<INP_C_AileDR) ;
}
else
{
xxx = PINE & (1<<INP_E_AileDR) ;
}
#else
//case SW_AileDR : return PINE & (1<<INP_E_AileDR);
#if (!(defined(JETI) || defined(FRSKY) || defined(ARDUPILOT) || defined(NMEA)))
case SW_AileDR : xxx = PINE & (1<<INP_E_AileDR);
#else
case SW_AileDR : xxx = PINC & (1<<INP_C_AileDR); //shad974: rerouted inputs to free up UART0
// Test bit 0 of PINC as well, temp fix for reacher10 broken pin on PINC bit 7
// if ( ( PINC & 0x01 ) == 0 )
// {
// xxx = 0 ;
// }
#endif
#endif
break ;
case SW_RuddDR : xxx = ping & (1<<INP_G_RuddDR);
break ;
// INP_G_ID1 INP_E_ID2
// id0 0 1
// id1 1 1
// id2 1 0
case SW_ID0 : xxx = ~ping & (1<<INP_G_ID1);
break ;
case SW_ID1 : xxx = (ping & (1<<INP_G_ID1)) ; if ( xxx ) xxx = (PINE & (1<<INP_E_ID2));
break ;
case SW_ID2 : xxx = ~PINE & (1<<INP_E_ID2);
break ;
case SW_Gear : xxx = PINE & (1<<INP_E_Gear);
break ;
//case SW_ThrCt : return PINE & (1<<INP_E_ThrCt);
#if defined(CPUM128) || defined(CPUM2561)
case SW_ThrCt :
if ( g_eeGeneral.FrskyPins )
{
xxx = PINC & (1<<INP_C_ThrCt) ;
}
else
{
xxx = PINE & (1<<INP_E_ThrCt) ;
}
#else
#if (!(defined(JETI) || defined(FRSKY) || defined(ARDUPILOT) || defined(NMEA)))
case SW_ThrCt : xxx = PINE & (1<<INP_E_ThrCt);
#else
case SW_ThrCt : xxx = PINC & (1<<INP_C_ThrCt); //shad974: rerouted inputs to free up UART0
#endif
#endif
break ;
case SW_Trainer: xxx = PINE & (1<<INP_E_Trainer);
break ;
default:;
}
if ( xxx )
{
return 1 ;
}
return 0;
}
void pauseEvents(uint8_t event)
{
event=event & EVT_KEY_MASK;
if(event < (int)DIM(keys)) keys[event].pauseEvents();
}
void killEvents(uint8_t event)
{
event=event & EVT_KEY_MASK;
if(event < (int)DIM(keys)) keys[event].killEvents();
}
//uint8_t getEventDbl(uint8_t event)
//{
// event=event & EVT_KEY_MASK;
// if(event < (int)DIM(keys)) return keys[event].getDbl();
// return 0;
//}
//uint16_t g_anaIns[8];
volatile uint16_t g_tmr10ms;
//volatile uint8_t g8_tmr10ms ;
volatile uint8_t g_blinkTmr10ms;
extern uint8_t StickScrollTimer ;
void per10ms()
{
uint16_t tmr ;
// g_tmr10ms++; // 16 bit sized
// g8_tmr10ms += 1 ; // byte sized
// g_blinkTmr10ms++;
tmr = g_tmr10ms + 1 ;
g_tmr10ms = tmr ;
g_blinkTmr10ms = tmr ;
uint8_t enuk = KEY_MENU;
uint8_t in = ~PINB;
static uint8_t current ;
uint8_t dir_keys ;
uint8_t lcurrent ;
dir_keys = in & 0x78 ; // Mask to direction keys
if ( ( lcurrent = current ) )
{ // Something already pressed
if ( ( lcurrent & dir_keys ) == 0 )
{
lcurrent = 0 ; // No longer pressed
}
else
{
in &= lcurrent | 0x06 ; // current or MENU or EXIT allowed
}
}
if ( lcurrent == 0 )
{ // look for a key
if ( dir_keys & 0x20 ) // right
{
lcurrent = 0x60 ; // Allow L and R for 9X
}
else if ( dir_keys & 0x40 ) // left
{
lcurrent = 0x60 ; // Allow L and R for 9X
}
else if ( dir_keys & 0x08 ) // down
{
lcurrent = 0x08 ;
}
else if ( dir_keys & 0x10 ) // up
{
lcurrent = 0x10 ;
}
in &= lcurrent | 0x06 ; // current or MENU or EXIT allowed
}
current = lcurrent ;
for(uint8_t i=1; i<7; i++)
{
//INP_B_KEY_MEN 1 .. INP_B_KEY_LFT 6
keys[enuk].input(in & 2,(EnumKeys)enuk);
++enuk;
in >>= 1 ;
}
const static prog_uchar APM crossTrim[]={
1<<INP_D_TRM_LH_DWN,
1<<INP_D_TRM_LH_UP,
1<<INP_D_TRM_LV_DWN,
1<<INP_D_TRM_LV_UP,
1<<INP_D_TRM_RV_DWN,
1<<INP_D_TRM_RV_UP,
1<<INP_D_TRM_RH_DWN,
1<<INP_D_TRM_RH_UP
};
in = ~PIND;
for(int i=0; i<8; i++)
{
// INP_D_TRM_RH_UP 0 .. INP_D_TRM_LH_UP 7
keys[enuk].input(in & pgm_read_byte(crossTrim+i),(EnumKeys)enuk);
++enuk;
}
uint8_t value = Rotary.RotEncoder & 0x20 ;
keys[enuk].input( value,(EnumKeys)enuk); // Rotary Enc. Switch
value |= ~PINB & 0x7E ;
if ( value )
{
StickScrollTimer = STICK_SCROLL_TIMEOUT ;
}
}