Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / targets / taranis / board.h
blob07b7f3dbad1df50a8f7359d04048d0f1a7704042
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _BOARD_H_
22 #define _BOARD_H_
24 #include <inttypes.h>
25 #include "../definitions.h"
26 #include "../opentx_constants.h"
27 #include "board_common.h"
28 #include "hal.h"
31 #if defined(ROTARY_ENCODER_NAVIGATION)
32 // Rotary Encoder driver
33 void rotaryEncoderInit();
34 void rotaryEncoderCheck();
35 #endif
37 #define FLASHSIZE 0x80000
38 #define BOOTLOADER_SIZE 0x8000
39 #define FIRMWARE_ADDRESS 0x08000000
41 #define LUA_MEM_MAX (0) // max allowed memory usage for complete Lua (in bytes), 0 means unlimited
43 #if defined(STM32F4)
44 #define PERI1_FREQUENCY 42000000
45 #define PERI2_FREQUENCY 84000000
46 #else
47 #define PERI1_FREQUENCY 30000000
48 #define PERI2_FREQUENCY 60000000
49 #endif
51 #define TIMER_MULT_APB1 2
52 #define TIMER_MULT_APB2 2
54 extern uint16_t sessionTimer;
56 // Board driver
57 void boardInit();
58 void boardOff();
60 // Timers driver
61 void init2MhzTimer();
62 void init5msTimer();
64 // SD driver
65 #define BLOCK_SIZE 512 /* Block Size in Bytes */
66 #if !defined(SIMU) || defined(SIMU_DISKIO)
67 uint32_t sdIsHC();
68 uint32_t sdGetSpeed();
69 #define SD_IS_HC() (sdIsHC())
70 #define SD_GET_SPEED() (sdGetSpeed())
71 #define SD_GET_FREE_BLOCKNR() (sdGetFreeSectors())
72 #else
73 #define SD_IS_HC() (0)
74 #define SD_GET_SPEED() (0)
75 #endif
76 #define __disk_read disk_read
77 #define __disk_write disk_write
78 #if defined(SIMU)
79 #if !defined(SIMU_DISKIO)
80 #define sdInit()
81 #define sdDone()
82 #endif
83 #define sdMount()
84 #define SD_CARD_PRESENT() true
85 #else
86 void sdInit();
87 void sdMount();
88 void sdDone();
89 void sdPoll10ms();
90 uint32_t sdMounted();
91 #define SD_CARD_PRESENT() ((SD_GPIO_PRESENT_GPIO->IDR & SD_GPIO_PRESENT_GPIO_PIN) == 0)
92 #endif
94 // Flash Write driver
95 #define FLASH_PAGESIZE 256
96 void unlockFlash();
97 void lockFlash();
98 void flashWrite(uint32_t * address, const uint32_t * buffer);
99 uint32_t isFirmwareStart(const uint8_t * buffer);
100 uint32_t isBootloaderStart(const uint8_t * buffer);
102 // Pulses driver
103 #define INTERNAL_MODULE_ON() GPIO_SetBits(INTMODULE_PWR_GPIO, INTMODULE_PWR_GPIO_PIN)
104 #if defined(INTMODULE_USART)
105 #define INTERNAL_MODULE_OFF() intmoduleStop()
106 #else
107 #define INTERNAL_MODULE_OFF() GPIO_ResetBits(INTMODULE_PWR_GPIO, INTMODULE_PWR_GPIO_PIN)
108 #endif
110 #define EXTERNAL_MODULE_ON() EXTERNAL_MODULE_PWR_ON()
112 #if defined(EXTMODULE_USART)
113 #define EXTERNAL_MODULE_OFF() extmoduleStop()
114 #else
115 #define EXTERNAL_MODULE_OFF() EXTERNAL_MODULE_PWR_OFF()
116 #endif
118 #if defined(RADIO_T12)
119 #define IS_INTERNAL_MODULE_ON() false
120 #else
121 #define IS_INTERNAL_MODULE_ON() (GPIO_ReadInputDataBit(INTMODULE_PWR_GPIO, INTMODULE_PWR_GPIO_PIN) == Bit_SET)
122 #endif
124 void intmoduleSerialStart(uint32_t baudrate, uint8_t rxEnable, uint16_t parity, uint16_t stopBits, uint16_t wordLength);
125 #if defined(INTERNAL_MODULE_MULTI)
126 void intmoduleTimerStart(uint32_t periodMs);
127 #endif
128 void intmoduleSendByte(uint8_t byte);
129 void intmoduleSendBuffer(const uint8_t * data, uint8_t size);
130 void intmoduleSendNextFrame();
132 void extmoduleSerialStart(uint32_t baudrate, uint32_t period_half_us, bool inverted);
133 void extmoduleInvertedSerialStart(uint32_t baudrate);
134 void extmoduleSendBuffer(const uint8_t * data, uint8_t size);
135 void extmoduleSendNextFrame();
136 void extmoduleSendInvertedByte(uint8_t byte);
138 // Trainer driver
139 #define SLAVE_MODE() (g_model.trainerData.mode == TRAINER_MODE_SLAVE)
141 #if defined(PCBX9D) || (defined(PCBX9DP) && PCBREV < 2019)
142 // Trainer detect is a switch on the jack
143 #define TRAINER_CONNECTED() (GPIO_ReadInputDataBit(TRAINER_DETECT_GPIO, TRAINER_DETECT_GPIO_PIN) == Bit_RESET)
144 #elif defined(PCBXLITES)
145 // Trainer is on the same connector than Headphones
146 enum JackState
148 SPEAKER_ACTIVE,
149 HEADPHONE_ACTIVE,
150 TRAINER_ACTIVE,
152 extern uint8_t jackState;
153 #define TRAINER_CONNECTED() (jackState == TRAINER_ACTIVE)
154 #elif defined(PCBXLITE)
155 // No Tainer jack on Taranis X-Lite
156 #define TRAINER_CONNECTED() false
157 #else
158 // Trainer detect catches PPM, detection would use more CPU
159 #define TRAINER_CONNECTED() true
160 #endif
162 #if defined(TRAINER_GPIO)
163 void init_trainer_ppm();
164 void stop_trainer_ppm();
165 void init_trainer_capture();
166 void stop_trainer_capture();
167 #else
168 #define init_trainer_ppm()
169 #define stop_trainer_ppm()
170 #define init_trainer_capture()
171 #define stop_trainer_capture()
172 #endif
173 #if defined(TRAINER_MODULE_CPPM)
174 void init_trainer_module_cppm();
175 void stop_trainer_module_cppm();
176 #else
177 #define init_trainer_module_cppm()
178 #define stop_trainer_module_cppm()
179 #endif
180 #if defined(TRAINER_MODULE_SBUS)
181 void init_trainer_module_sbus();
182 void stop_trainer_module_sbus();
183 #else
184 #define init_trainer_module_sbus()
185 #define stop_trainer_module_sbus()
186 #endif
188 #if defined(INTMODULE_HEARTBEAT_GPIO)
189 void init_intmodule_heartbeat();
190 void stop_intmodule_heartbeat();
191 void check_intmodule_heartbeat();
192 #else
193 #define init_intmodule_heartbeat()
194 #define stop_intmodule_heartbeat()
195 #define check_intmodule_heartbeat()
196 #endif
198 void check_telemetry_exti();
200 // SBUS
201 int sbusGetByte(uint8_t * byte);
203 // Keys driver
204 enum EnumKeys
206 #if defined(KEYS_GPIO_REG_SHIFT)
207 KEY_SHIFT,
208 #endif
210 #if defined(KEYS_GPIO_REG_MENU)
211 KEY_MENU,
212 #endif
214 KEY_EXIT,
215 KEY_ENTER,
217 #if defined(KEYS_GPIO_REG_DOWN)
218 KEY_DOWN,
219 KEY_UP,
220 #endif
222 #if defined(KEYS_GPIO_REG_RIGHT)
223 KEY_RIGHT,
224 KEY_LEFT,
225 #endif
227 #if defined(KEYS_GPIO_REG_PAGE)
228 KEY_PAGE,
229 #endif
231 #if defined(KEYS_GPIO_REG_PLUS)
232 KEY_PLUS,
233 KEY_MINUS,
234 #endif
236 KEY_COUNT,
237 KEY_MAX = KEY_COUNT - 1,
239 #if defined(ROTARY_ENCODER_NAVIGATION)
240 KEY_PLUS,
241 KEY_MINUS,
242 #endif
244 TRM_BASE,
245 TRM_LH_DWN = TRM_BASE,
246 TRM_LH_UP,
247 TRM_LV_DWN,
248 TRM_LV_UP,
249 TRM_RV_DWN,
250 TRM_RV_UP,
251 TRM_RH_DWN,
252 TRM_RH_UP,
253 TRM_LAST = TRM_RH_UP,
255 NUM_KEYS
258 #if defined(PCBX9E) && !defined(SIMU)
259 #define KEY_UP KEY_MINUS
260 #define KEY_DOWN KEY_PLUS
261 #define KEY_RIGHT KEY_PLUS
262 #define KEY_LEFT KEY_MINUS
263 #elif defined(NAVIGATION_XLITE)
264 #define KEY_PLUS KEY_RIGHT
265 #define KEY_MINUS KEY_LEFT
266 #elif defined(NAVIGATION_9X)
267 #define KEY_MENU KEY_ENTER
268 #define KEY_MINUS KEY_DOWN
269 #define KEY_PLUS KEY_UP
270 #else
271 #define KEY_UP KEY_PLUS
272 #define KEY_DOWN KEY_MINUS
273 #define KEY_RIGHT KEY_MINUS
274 #define KEY_LEFT KEY_PLUS
275 #endif
277 #if defined(KEYS_GPIO_PIN_SHIFT)
278 #define IS_SHIFT_KEY(index) (index == KEY_SHIFT)
279 #if defined(SIMU)
280 #define IS_SHIFT_PRESSED() (readKeys() & (1 << KEY_SHIFT))
281 #else
282 #define IS_SHIFT_PRESSED() (~KEYS_GPIO_REG_SHIFT & KEYS_GPIO_PIN_SHIFT)
283 #endif
284 #else
285 #define IS_SHIFT_KEY(index) (false)
286 #define IS_SHIFT_PRESSED() (false)
287 #endif
289 enum EnumSwitches
291 SW_SA,
292 SW_SB,
293 SW_SC,
294 SW_SD,
295 SW_SE,
296 SW_SF,
297 SW_SG,
298 SW_SH
300 #define IS_3POS(x) ((x) != SW_SF && (x) != SW_SH)
302 enum EnumSwitchesPositions
304 SW_SA0,
305 SW_SA1,
306 SW_SA2,
307 SW_SB0,
308 SW_SB1,
309 SW_SB2,
310 SW_SC0,
311 SW_SC1,
312 SW_SC2,
313 SW_SD0,
314 SW_SD1,
315 SW_SD2,
316 #if defined(PCBX9) || defined(PCBXLITES) || defined(PCBX9LITES)
317 SW_SE0,
318 SW_SE1,
319 SW_SE2,
320 #endif
321 #if defined(PCBX9D) || defined(PCBX9DP) || defined(PCBX9E) || defined(PCBX7) || defined(PCBXLITES) || defined(PCBX9LITES)
322 SW_SF0,
323 SW_SF1,
324 SW_SF2,
325 #endif
326 #if defined(PCBX9D) || defined(PCBX9DP) || defined(PCBX9E) || defined(PCBX9LITES)
327 SW_SG0,
328 SW_SG1,
329 SW_SG2,
330 #endif
331 #if defined(PCBX9D) || defined(PCBX9DP) || defined(PCBX9E) || defined(PCBX7)
332 SW_SH0,
333 SW_SH1,
334 SW_SH2,
335 #endif
336 #if defined(PCBX9DP) && PCBREV >= 2019
337 SW_SI0,
338 SW_SI1,
339 SW_SI2,
340 #endif
341 #if defined(PCBX7)
342 SW_SI0,
343 SW_SI1,
344 SW_SI2,
345 SW_SJ0,
346 SW_SJ1,
347 SW_SJ2,
348 #endif
349 #if defined(PCBX9E)
350 SW_SI0,
351 SW_SI1,
352 SW_SI2,
353 SW_SJ0,
354 SW_SJ1,
355 SW_SJ2,
356 SW_SK0,
357 SW_SK1,
358 SW_SK2,
359 SW_SL0,
360 SW_SL1,
361 SW_SL2,
362 SW_SM0,
363 SW_SM1,
364 SW_SM2,
365 SW_SN0,
366 SW_SN1,
367 SW_SN2,
368 SW_SO0,
369 SW_SO1,
370 SW_SO2,
371 SW_SP0,
372 SW_SP1,
373 SW_SP2,
374 SW_SQ0,
375 SW_SQ1,
376 SW_SQ2,
377 SW_SR0,
378 SW_SR1,
379 SW_SR2,
380 #endif
381 NUM_SWITCHES_POSITIONS
384 #if defined(PCBXLITES)
385 #define NUM_SWITCHES 6
386 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
387 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 10) + (SWITCH_TOGGLE << 8) + (SWITCH_2POS << 6) + (SWITCH_2POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
388 #define DEFAULT_POTS_CONFIG (POT_WITHOUT_DETENT << 2) + (POT_WITHOUT_DETENT << 0)
389 #elif defined(PCBXLITE)
390 #define NUM_SWITCHES 4
391 #define STORAGE_NUM_SWITCHES 6
392 #define DEFAULT_SWITCH_CONFIG (SWITCH_2POS << 6) + (SWITCH_2POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0);
393 #define DEFAULT_POTS_CONFIG (POT_WITHOUT_DETENT << 2) + (POT_WITHOUT_DETENT << 0)
394 #elif defined(RADIO_T12)
395 #define NUM_SWITCHES 8
396 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
397 #define DEFAULT_SWITCH_CONFIG (SWITCH_2POS << 10) + (SWITCH_2POS << 8) + (SWITCH_3POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
398 #define DEFAULT_POTS_CONFIG (POT_WITHOUT_DETENT << 0) + (POT_WITHOUT_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
399 #elif defined(PCBX7)
400 #define NUM_SWITCHES 8
401 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
402 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 10) + (SWITCH_2POS << 8) + (SWITCH_3POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
403 #define DEFAULT_POTS_CONFIG (POT_WITHOUT_DETENT << 0) + (POT_WITH_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
404 #elif defined(PCBX9LITES)
405 #define NUM_SWITCHES 7
406 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
407 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 12) + (SWITCH_TOGGLE << 10) + (SWITCH_TOGGLE << 8) + (SWITCH_2POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0);
408 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0); // S1 = pot with detent
409 #elif defined(PCBX9LITE)
410 #define NUM_SWITCHES 5
411 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
412 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 8) + (SWITCH_2POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0);
413 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0); // S1 = pot with detent
414 #elif defined(PCBX9E)
415 #define NUM_SWITCHES 18 // yes, it's perfect like that !
416 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
417 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 14) + (SWITCH_3POS << 12) + (SWITCH_2POS << 10) + (SWITCH_3POS << 8) + (SWITCH_3POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
418 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0) + (POT_WITH_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
419 #define DEFAULT_SLIDERS_CONFIG (SLIDER_WITH_DETENT << 3) + (SLIDER_WITH_DETENT << 2) + (SLIDER_WITH_DETENT << 1) + (SLIDER_WITH_DETENT << 0)
420 #elif defined(PCBX9DP) && PCBREV >= 2019
421 #define NUM_SWITCHES 9
422 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
423 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 16) + (SWITCH_TOGGLE << 14) + (SWITCH_3POS << 12) + (SWITCH_2POS << 10) + (SWITCH_3POS << 8) + (SWITCH_3POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0);
424 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0) + (POT_WITH_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
425 #define DEFAULT_SLIDERS_CONFIG (SLIDER_WITH_DETENT << 1) + (SLIDER_WITH_DETENT << 0)
426 #elif defined(PCBX9D) || defined(PCBX9DP)
427 #define NUM_SWITCHES 8
428 #define STORAGE_NUM_SWITCHES 9
429 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 14) + (SWITCH_3POS << 12) + (SWITCH_2POS << 10) + (SWITCH_3POS << 8) + (SWITCH_3POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
430 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0) + (POT_WITH_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
431 #define DEFAULT_SLIDERS_CONFIG (SLIDER_WITH_DETENT << 1) + (SLIDER_WITH_DETENT << 0)
432 #endif
434 #define STORAGE_NUM_SWITCHES_POSITIONS (STORAGE_NUM_SWITCHES * 3)
436 void keysInit();
437 uint32_t switchState(uint8_t index);
438 uint32_t readKeys();
439 uint32_t readTrims();
440 #define TRIMS_PRESSED() (readTrims())
441 #define KEYS_PRESSED() (readKeys())
443 // WDT driver
444 #define WDG_DURATION 500 /*ms*/
445 #if !defined(WATCHDOG) || defined(SIMU)
446 #define WDG_ENABLE(x)
447 #define WDG_RESET()
448 #else
449 #define WDG_ENABLE(x) watchdogInit(x)
450 #define WDG_RESET() IWDG->KR = 0xAAAA
451 #endif
452 void watchdogInit(unsigned int duration);
453 #define WAS_RESET_BY_SOFTWARE() (RCC->CSR & RCC_CSR_SFTRSTF)
454 #define WAS_RESET_BY_WATCHDOG() (RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF))
455 #define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() (RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_SFTRSTF))
457 // ADC driver
458 enum Analogs {
459 STICK1,
460 STICK2,
461 STICK3,
462 STICK4,
463 POT_FIRST,
464 POT1 = POT_FIRST,
465 #if defined(PCBX9LITE)
466 POT_LAST = POT1,
467 #elif defined(PCBXLITE) || defined(PCBX7)
468 POT2,
469 POT_LAST = POT2,
470 #elif defined(PCBX9E)
471 POT2,
472 POT3,
473 POT4,
474 POT_LAST = POT4,
475 SLIDER1,
476 SLIDER2,
477 SLIDER3,
478 SLIDER4,
479 #else
480 POT2,
481 POT3,
482 POT_LAST = POT3,
483 SLIDER1,
484 SLIDER2,
485 #endif
486 TX_VOLTAGE,
487 TX_RTC_VOLTAGE,
488 NUM_ANALOGS
491 #if defined(PCBX9LITE)
492 #define NUM_POTS 1
493 #define NUM_SLIDERS 0
494 #define STORAGE_NUM_POTS 1
495 #define STORAGE_NUM_SLIDERS 0
496 #elif defined(PCBXLITE) || defined(PCBX7)
497 #define NUM_POTS 2
498 #define NUM_SLIDERS 0
499 #define STORAGE_NUM_POTS 2
500 #define STORAGE_NUM_SLIDERS 0
501 #elif defined(PCBX9E)
502 #define NUM_POTS 4
503 #define NUM_SLIDERS 4
504 #define STORAGE_NUM_POTS 4
505 #define STORAGE_NUM_SLIDERS 4
506 #elif defined(PCBX9DP)
507 #define NUM_POTS 3
508 #define NUM_SLIDERS 2
509 #define STORAGE_NUM_POTS 3
510 #define STORAGE_NUM_SLIDERS 2
511 #else
512 #define NUM_POTS 3 // TODO X9D has only 2 pots
513 #define NUM_SLIDERS 2
514 #define STORAGE_NUM_POTS 3
515 #define STORAGE_NUM_SLIDERS 2
516 #endif
518 #define NUM_XPOTS STORAGE_NUM_POTS
519 #define NUM_TRIMS 4
520 #define NUM_MOUSE_ANALOGS 0
521 #define STORAGE_NUM_MOUSE_ANALOGS 0
523 #if defined(STICKS_PWM)
524 #define NUM_PWMSTICKS 4
525 #define STICKS_PWM_ENABLED() (!hardwareOptions.sticksPwmDisabled)
526 void sticksPwmInit();
527 void sticksPwmRead(uint16_t * values);
528 extern volatile uint32_t pwm_interrupt_count; // TODO => reusable buffer (boot section)
529 #define NUM_TRIMS_KEYS 4
530 #else
531 #define NUM_TRIMS_KEYS 8
532 #define STICKS_PWM_ENABLED() false
533 #endif
535 #if NUM_PWMSTICKS > 0
536 PACK(typedef struct {
537 uint8_t sticksPwmDisabled:1;
538 uint8_t pxx2Enabled:1;
539 }) HardwareOptions;
540 #else
541 PACK(typedef struct {
542 uint8_t pxx2Enabled:1;
543 }) HardwareOptions;
544 #endif
546 extern HardwareOptions hardwareOptions;
548 #if !defined(PXX2)
549 #define IS_PXX2_INTERNAL_ENABLED() (false)
550 #define IS_PXX1_INTERNAL_ENABLED() (true)
551 #elif !defined(PXX1) || defined(PCBXLITES) || defined(PCBX9LITE)
552 #define IS_PXX2_INTERNAL_ENABLED() (true)
553 #define IS_PXX1_INTERNAL_ENABLED() (false)
554 #elif defined(INTERNAL_MODULE_PXX1)
555 #define IS_PXX2_INTERNAL_ENABLED() (false)
556 #define IS_PXX1_INTERNAL_ENABLED() (true)
557 #else
558 // TODO #define PXX2_PROBE
559 // TODO #define IS_PXX2_INTERNAL_ENABLED() (hardwareOptions.pxx2Enabled)
560 #define IS_PXX2_INTERNAL_ENABLED() (true)
561 #define IS_PXX1_INTERNAL_ENABLED() (true)
562 #endif
564 enum CalibratedAnalogs {
565 CALIBRATED_STICK1,
566 CALIBRATED_STICK2,
567 CALIBRATED_STICK3,
568 CALIBRATED_STICK4,
569 CALIBRATED_POT_FIRST,
570 CALIBRATED_POT_LAST = CALIBRATED_POT_FIRST + NUM_POTS - 1,
571 CALIBRATED_SLIDER_FIRST,
572 CALIBRATED_SLIDER_LAST = CALIBRATED_SLIDER_FIRST + NUM_SLIDERS - 1,
573 NUM_CALIBRATED_ANALOGS
576 #if defined(PCBX9D)
577 #define IS_POT(x) ((x)>=POT_FIRST && (x)<=POT2) // POT3 is only defined in software
578 #else
579 #define IS_POT(x) ((x)>=POT_FIRST && (x)<=POT_LAST)
580 #endif
582 #define IS_SLIDER(x) ((x)>POT_LAST && (x)<TX_VOLTAGE)
584 extern uint16_t adcValues[NUM_ANALOGS];
586 // Battery driver
587 #if defined(PCBX9E)
588 // NI-MH 9.6V
589 #define BATTERY_WARN 87 // 8.7V
590 #define BATTERY_MIN 85 // 8.5V
591 #define BATTERY_MAX 115 // 11.5V
592 #elif defined(PCBXLITE)
593 // 2 x Li-Ion
594 #define BATTERY_WARN 66 // 6.6V
595 #define BATTERY_MIN 67 // 6.7V
596 #define BATTERY_MAX 83 // 8.3V
597 #else
598 // NI-MH 7.2V
599 #define BATTERY_WARN 65 // 6.5V
600 #define BATTERY_MIN 60 // 6.0V
601 #define BATTERY_MAX 80 // 8.0V
602 #endif
604 #if defined(PCBXLITE)
605 #define BATT_SCALE 131
606 #elif defined(PCBX7)
607 #define BATT_SCALE 123
608 #elif defined(PCBX9LITE)
609 #define BATT_SCALE 117
610 #elif defined(PCBX9DP) && PCBREV >= 2019
611 #define BATT_SCALE 117
612 #else
613 #define BATT_SCALE 150
614 #endif
616 #if defined(__cplusplus) && !defined(SIMU)
617 extern "C" {
618 #endif
620 // Power driver
621 #define SOFT_PWR_CTRL
622 void pwrInit();
623 uint32_t pwrCheck();
624 void pwrOn();
625 void pwrOff();
626 bool pwrPressed();
627 #if defined(PWR_BUTTON_PRESS)
628 #define STARTUP_ANIMATION
629 uint32_t pwrPressedDuration();
630 #endif
631 void pwrResetHandler();
633 #if defined(SIMU)
634 #define UNEXPECTED_SHUTDOWN() false
635 #else
636 #define UNEXPECTED_SHUTDOWN() (WAS_RESET_BY_WATCHDOG() || g_eeGeneral.unexpectedShutdown)
637 #endif
639 // Backlight driver
640 void backlightInit();
641 void backlightDisable();
642 #define BACKLIGHT_DISABLE() backlightDisable()
643 uint8_t isBacklightEnabled();
644 #if !defined(__cplusplus)
645 #define backlightEnable(...)
646 #elif defined(PCBX9E) || defined(PCBX9DP)
647 void backlightEnable(uint8_t level = 0, uint8_t color = 0);
648 #define BACKLIGHT_ENABLE() backlightEnable(g_eeGeneral.backlightBright, g_eeGeneral.backlightColor)
649 #else
650 void backlightEnable(uint8_t level = 0);
651 #define BACKLIGHT_ENABLE() backlightEnable(g_eeGeneral.backlightBright)
652 #endif
654 #if !defined(SIMU)
655 void usbJoystickUpdate();
656 #endif
657 #define USB_NAME "FrSky Taranis"
658 #define USB_MANUFACTURER 'F', 'r', 'S', 'k', 'y', ' ', ' ', ' ' /* 8 bytes */
659 #define USB_PRODUCT 'T', 'a', 'r', 'a', 'n', 'i', 's', ' ' /* 8 Bytes */
661 #if defined(__cplusplus) && !defined(SIMU)
663 #endif
665 // I2C driver: EEPROM + Audio Volume
666 #define EEPROM_SIZE (32*1024)
668 void i2cInit();
669 void eepromReadBlock(uint8_t * buffer, size_t address, size_t size);
670 void eepromStartWrite(uint8_t * buffer, size_t address, size_t size);
671 uint8_t eepromIsTransferComplete();
673 // Debug driver
674 void debugPutc(const char c);
676 // Telemetry driver
677 void telemetryPortInit(uint32_t baudrate, uint8_t mode);
678 void telemetryPortSetDirectionInput();
679 void telemetryPortSetDirectionOutput();
680 void sportSendByte(uint8_t byte);
681 void sportSendByteLoop(uint8_t byte);
682 void sportStopSendByteLoop();
683 void sportSendBuffer(const uint8_t * buffer, uint32_t count);
684 bool telemetryGetByte(uint8_t * byte);
685 void telemetryClearFifo();
686 extern uint32_t telemetryErrors;
688 // soft-serial
689 void telemetryPortInvertedInit(uint32_t baudrate);
691 // PCBREV driver
692 #if defined(PCBX7)
693 #define IS_PCBREV_40() (GPIO_ReadInputDataBit(PCBREV_GPIO, PCBREV_GPIO_PIN) == Bit_SET)
694 #define HAS_SPORT_UPDATE_CONNECTOR() IS_PCBREV_40()
695 #elif defined(SPORT_UPDATE_PWR_GPIO)
696 #define HAS_SPORT_UPDATE_CONNECTOR() true
697 #else
698 #define HAS_SPORT_UPDATE_CONNECTOR() false
699 #endif
701 // Sport update driver
702 #if defined(SPORT_UPDATE_PWR_GPIO)
703 void sportUpdateInit();
704 void sportUpdatePowerOn();
705 void sportUpdatePowerOff();
706 #define SPORT_UPDATE_POWER_ON() sportUpdatePowerOn()
707 #define SPORT_UPDATE_POWER_OFF() sportUpdatePowerOff()
708 #else
709 #define sportUpdateInit()
710 #define SPORT_UPDATE_POWER_ON()
711 #define SPORT_UPDATE_POWER_OFF()
712 #endif
714 // Audio driver
715 void audioInit() ;
716 void audioEnd() ;
717 void dacStart();
718 void dacStop();
719 void setSampleRate(uint32_t frequency);
720 #define VOLUME_LEVEL_MAX 23
721 #define VOLUME_LEVEL_DEF 12
722 #if !defined(SOFTWARE_VOLUME)
723 void setScaledVolume(uint8_t volume);
724 void setVolume(uint8_t volume);
725 int32_t getVolume();
726 #endif
727 #if defined(AUDIO_SPEAKER_ENABLE_GPIO)
728 void initSpeakerEnable();
729 void enableSpeaker();
730 void disableSpeaker();
731 #else
732 static inline void initSpeakerEnable() { }
733 static inline void enableSpeaker() { }
734 static inline void disableSpeaker() { }
735 #endif
736 #if defined(HEADPHONE_TRAINER_SWITCH_GPIO)
737 void initHeadphoneTrainerSwitch();
738 void enableHeadphone();
739 void enableTrainer();
740 #else
741 static inline void initHeadphoneTrainerSwitch() { }
742 static inline void enableHeadphone() { }
743 static inline void enableTrainer() { }
744 #endif
745 #if defined(JACK_DETECT_GPIO)
746 void initJackDetect();
747 bool isJackPlugged();
748 #endif
749 void audioConsumeCurrentBuffer();
750 #define audioDisableIrq() __disable_irq()
751 #define audioEnableIrq() __enable_irq()
753 // Haptic driver
754 void hapticInit();
755 void hapticOff();
756 #if defined(HAPTIC_PWM)
757 void hapticOn(uint32_t pwmPercent);
758 #else
759 void hapticOn();
760 #endif
762 // Second serial port driver
763 #if defined(AUX_SERIAL_GPIO)
764 #define DEBUG_BAUDRATE 115200
765 #define AUX_SERIAL
766 extern uint8_t auxSerialMode;
767 void auxSerialInit(unsigned int mode, unsigned int protocol);
768 void auxSerialPutc(char c);
769 #define auxSerialTelemetryInit(protocol) auxSerialInit(UART_MODE_TELEMETRY, protocol)
770 void auxSerialSbusInit();
771 void auxSerialStop();
772 #endif
774 // BT driver
775 #define BLUETOOTH_BOOTLOADER_BAUDRATE 230400
776 #define BLUETOOTH_DEFAULT_BAUDRATE 115200
777 #if defined(PCBX9E)
778 #define BLUETOOTH_FACTORY_BAUDRATE 9600
779 #else
780 #define BLUETOOTH_FACTORY_BAUDRATE 57600
781 #endif
782 #define BT_TX_FIFO_SIZE 64
783 #define BT_RX_FIFO_SIZE 128
784 void bluetoothInit(uint32_t baudrate, bool enable);
785 void bluetoothWriteWakeup();
786 uint8_t bluetoothIsWriting();
787 void bluetoothDisable();
788 #if defined(PCBX9LITES)
789 #define IS_BLUETOOTH_CHIP_PRESENT() (true)
790 #elif defined(PCBX9LITE)
791 #define IS_BLUETOOTH_CHIP_PRESENT() (false)
792 #elif (defined(PCBX7) || defined(PCBXLITE)) && !defined(SIMU)
793 extern volatile uint8_t btChipPresent;
794 #define IS_BLUETOOTH_CHIP_PRESENT() (btChipPresent)
795 #else
796 #define IS_BLUETOOTH_CHIP_PRESENT() (true)
797 #endif
799 // LED driver
800 void ledInit();
801 void ledOff();
802 void ledRed();
803 void ledGreen();
804 void ledBlue();
806 // LCD driver
807 #if defined(PCBX9D) || defined(PCBX9DP) || defined(PCBX9E)
808 #define LCD_W 212
809 #define LCD_H 64
810 #define LCD_DEPTH 4
811 #define LCD_CONTRAST_MIN 0
812 #define LCD_CONTRAST_MAX 45
813 #define LCD_CONTRAST_DEFAULT 25
814 #else
815 #define LCD_W 128
816 #define LCD_H 64
817 #define LCD_DEPTH 1
818 #define IS_LCD_RESET_NEEDED() true
819 #define LCD_CONTRAST_MIN 10
820 #define LCD_CONTRAST_MAX 30
821 #define LCD_CONTRAST_DEFAULT 20
822 #endif
824 #if defined(PCBX9D) || defined(PCBX9E) || (defined(PCBX9DP) && PCBREV < 2019)
825 #define IS_LCD_RESET_NEEDED() (!WAS_RESET_BY_WATCHDOG_OR_SOFTWARE())
826 #else
827 #define IS_LCD_RESET_NEEDED() true
828 #endif
830 void lcdInit();
831 void lcdInitFinish();
832 void lcdOff();
834 // TODO lcdRefreshWait() stub in simpgmspace and remove LCD_DUAL_BUFFER
835 #if defined(LCD_DMA) && !defined(LCD_DUAL_BUFFER) && !defined(SIMU)
836 void lcdRefreshWait();
837 #else
838 #define lcdRefreshWait()
839 #endif
840 #if defined(PCBX9D) || defined(SIMU) || !defined(__cplusplus)
841 void lcdRefresh();
842 #else
843 void lcdRefresh(bool wait=true); // TODO uint8_t wait to simplify this
844 #endif
845 void lcdSetRefVolt(unsigned char val);
846 void lcdSetContrast();
848 // Top LCD driver
849 #if defined(TOPLCD_GPIO)
850 void toplcdInit();
851 void toplcdOff();
852 void toplcdRefreshStart();
853 void toplcdRefreshEnd();
854 void setTopFirstTimer(int32_t value);
855 void setTopSecondTimer(uint32_t value);
856 void setTopRssi(uint32_t rssi);
857 void setTopBatteryState(int state, uint8_t blinking);
858 void setTopBatteryValue(uint32_t volts);
859 #endif
861 #define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)
863 #if defined(__cplusplus)
864 #include "fifo.h"
865 #include "dmafifo.h"
867 #if defined(CROSSFIRE)
868 #define TELEMETRY_FIFO_SIZE 128
869 #else
870 #define TELEMETRY_FIFO_SIZE 64
871 #endif
873 extern Fifo<uint8_t, TELEMETRY_FIFO_SIZE> telemetryFifo;
874 typedef DMAFifo<32> AuxSerialRxFifo;
875 extern AuxSerialRxFifo auxSerialRxFifo;
876 #endif
878 // Gyro driver
879 #define GYRO_VALUES_COUNT 6
880 #define GYRO_BUFFER_LENGTH (GYRO_VALUES_COUNT * sizeof(int16_t))
881 int gyroInit();
882 int gyroRead(uint8_t buffer[GYRO_BUFFER_LENGTH]);
883 #define GYRO_MAX_DEFAULT 30
884 #define GYRO_MAX_RANGE 60
885 #define GYRO_OFFSET_MIN -30
886 #define GYRO_OFFSET_MAX 10
888 #endif // _BOARD_H_