Cosmetics
[opentx.git] / radio / src / targets / taranis / board.h
bloba5d8ca82561621ca434234968d7299d58865f107
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(PCBX7ACCESS)
342 SW_SI0,
343 SW_SI1,
344 SW_SI2,
345 #elif defined(PCBX7)
346 SW_SI0,
347 SW_SI1,
348 SW_SI2,
349 SW_SJ0,
350 SW_SJ1,
351 SW_SJ2,
352 #endif
353 #if defined(PCBX9E)
354 SW_SI0,
355 SW_SI1,
356 SW_SI2,
357 SW_SJ0,
358 SW_SJ1,
359 SW_SJ2,
360 SW_SK0,
361 SW_SK1,
362 SW_SK2,
363 SW_SL0,
364 SW_SL1,
365 SW_SL2,
366 SW_SM0,
367 SW_SM1,
368 SW_SM2,
369 SW_SN0,
370 SW_SN1,
371 SW_SN2,
372 SW_SO0,
373 SW_SO1,
374 SW_SO2,
375 SW_SP0,
376 SW_SP1,
377 SW_SP2,
378 SW_SQ0,
379 SW_SQ1,
380 SW_SQ2,
381 SW_SR0,
382 SW_SR1,
383 SW_SR2,
384 #endif
385 NUM_SWITCHES_POSITIONS
388 #if defined(PCBXLITES)
389 #define NUM_SWITCHES 6
390 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
391 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 10) + (SWITCH_TOGGLE << 8) + (SWITCH_2POS << 6) + (SWITCH_2POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
392 #define DEFAULT_POTS_CONFIG (POT_WITHOUT_DETENT << 2) + (POT_WITHOUT_DETENT << 0)
393 #elif defined(PCBXLITE)
394 #define NUM_SWITCHES 4
395 #define STORAGE_NUM_SWITCHES 6
396 #define DEFAULT_SWITCH_CONFIG (SWITCH_2POS << 6) + (SWITCH_2POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0);
397 #define DEFAULT_POTS_CONFIG (POT_WITHOUT_DETENT << 2) + (POT_WITHOUT_DETENT << 0)
398 #elif defined(RADIO_T12)
399 #define NUM_SWITCHES 8
400 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
401 #define DEFAULT_SWITCH_CONFIG (SWITCH_2POS << 10) + (SWITCH_2POS << 8) + (SWITCH_3POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
402 #define DEFAULT_POTS_CONFIG (POT_WITHOUT_DETENT << 0) + (POT_WITHOUT_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
403 #elif defined(PCBX7ACCESS)
404 #define NUM_SWITCHES 7
405 #define STORAGE_NUM_SWITCHES 8
406 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 10) + (SWITCH_2POS << 8) + (SWITCH_3POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
407 #define DEFAULT_POTS_CONFIG (POT_WITHOUT_DETENT << 0) + (POT_WITH_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
408 #elif defined(PCBX7)
409 #define NUM_SWITCHES 8
410 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
411 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 10) + (SWITCH_2POS << 8) + (SWITCH_3POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
412 #define DEFAULT_POTS_CONFIG (POT_WITHOUT_DETENT << 0) + (POT_WITH_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
413 #elif defined(PCBX9LITES)
414 #define NUM_SWITCHES 7
415 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
416 #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);
417 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0); // S1 = pot with detent
418 #elif defined(PCBX9LITE)
419 #define NUM_SWITCHES 5
420 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
421 #define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 8) + (SWITCH_2POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0);
422 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0); // S1 = pot with detent
423 #elif defined(PCBX9E)
424 #define NUM_SWITCHES 18 // yes, it's perfect like that !
425 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
426 #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)
427 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0) + (POT_WITH_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
428 #define DEFAULT_SLIDERS_CONFIG (SLIDER_WITH_DETENT << 3) + (SLIDER_WITH_DETENT << 2) + (SLIDER_WITH_DETENT << 1) + (SLIDER_WITH_DETENT << 0)
429 #elif defined(PCBX9DP) && PCBREV >= 2019
430 #define NUM_SWITCHES 9
431 #define STORAGE_NUM_SWITCHES NUM_SWITCHES
432 #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);
433 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0) + (POT_WITH_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
434 #define DEFAULT_SLIDERS_CONFIG (SLIDER_WITH_DETENT << 1) + (SLIDER_WITH_DETENT << 0)
435 #elif defined(PCBX9D) || defined(PCBX9DP)
436 #define NUM_SWITCHES 8
437 #define STORAGE_NUM_SWITCHES 9
438 #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)
439 #define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 0) + (POT_WITH_DETENT << 2); // S1 = pot without detent, S2 = pot with detent
440 #define DEFAULT_SLIDERS_CONFIG (SLIDER_WITH_DETENT << 1) + (SLIDER_WITH_DETENT << 0)
441 #endif
443 #define STORAGE_NUM_SWITCHES_POSITIONS (STORAGE_NUM_SWITCHES * 3)
445 void keysInit();
446 uint32_t switchState(uint8_t index);
447 uint32_t readKeys();
448 uint32_t readTrims();
449 #define TRIMS_PRESSED() (readTrims())
450 #define KEYS_PRESSED() (readKeys())
452 // WDT driver
453 #define WDG_DURATION 500 /*ms*/
454 #if !defined(WATCHDOG) || defined(SIMU)
455 #define WDG_ENABLE(x)
456 #define WDG_RESET()
457 #else
458 #define WDG_ENABLE(x) watchdogInit(x)
459 #define WDG_RESET() IWDG->KR = 0xAAAA
460 #endif
461 void watchdogInit(unsigned int duration);
462 #define WAS_RESET_BY_SOFTWARE() (RCC->CSR & RCC_CSR_SFTRSTF)
463 #define WAS_RESET_BY_WATCHDOG() (RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF))
464 #define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() (RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_SFTRSTF))
466 // ADC driver
467 enum Analogs {
468 STICK1,
469 STICK2,
470 STICK3,
471 STICK4,
472 POT_FIRST,
473 POT1 = POT_FIRST,
474 #if defined(PCBX9LITE)
475 POT_LAST = POT1,
476 #elif defined(PCBXLITE) || defined(PCBX7)
477 POT2,
478 POT_LAST = POT2,
479 #elif defined(PCBX9E)
480 POT2,
481 POT3,
482 POT4,
483 POT_LAST = POT4,
484 SLIDER1,
485 SLIDER2,
486 SLIDER3,
487 SLIDER4,
488 #else
489 POT2,
490 POT3,
491 POT_LAST = POT3,
492 SLIDER1,
493 SLIDER2,
494 #endif
495 TX_VOLTAGE,
496 TX_RTC_VOLTAGE,
497 NUM_ANALOGS
500 #if defined(PCBX9LITE)
501 #define NUM_POTS 1
502 #define NUM_SLIDERS 0
503 #define STORAGE_NUM_POTS 1
504 #define STORAGE_NUM_SLIDERS 0
505 #elif defined(PCBXLITE) || defined(PCBX7)
506 #define NUM_POTS 2
507 #define NUM_SLIDERS 0
508 #define STORAGE_NUM_POTS 2
509 #define STORAGE_NUM_SLIDERS 0
510 #elif defined(PCBX9E)
511 #define NUM_POTS 4
512 #define NUM_SLIDERS 4
513 #define STORAGE_NUM_POTS 4
514 #define STORAGE_NUM_SLIDERS 4
515 #elif defined(PCBX9DP)
516 #define NUM_POTS 3
517 #define NUM_SLIDERS 2
518 #define STORAGE_NUM_POTS 3
519 #define STORAGE_NUM_SLIDERS 2
520 #else
521 #define NUM_POTS 3 // TODO X9D has only 2 pots
522 #define NUM_SLIDERS 2
523 #define STORAGE_NUM_POTS 3
524 #define STORAGE_NUM_SLIDERS 2
525 #endif
527 #define NUM_XPOTS STORAGE_NUM_POTS
528 #define NUM_TRIMS 4
529 #define NUM_MOUSE_ANALOGS 0
530 #define STORAGE_NUM_MOUSE_ANALOGS 0
532 #if defined(STICKS_PWM)
533 #define NUM_PWMSTICKS 4
534 #define STICKS_PWM_ENABLED() (!hardwareOptions.sticksPwmDisabled)
535 void sticksPwmInit();
536 void sticksPwmRead(uint16_t * values);
537 extern volatile uint32_t pwm_interrupt_count; // TODO => reusable buffer (boot section)
538 #define NUM_TRIMS_KEYS 4
539 #else
540 #define NUM_TRIMS_KEYS 8
541 #define STICKS_PWM_ENABLED() false
542 #endif
544 #if NUM_PWMSTICKS > 0
545 PACK(typedef struct {
546 uint8_t sticksPwmDisabled:1;
547 uint8_t pxx2Enabled:1;
548 }) HardwareOptions;
549 #else
550 PACK(typedef struct {
551 uint8_t pxx2Enabled:1;
552 }) HardwareOptions;
553 #endif
555 extern HardwareOptions hardwareOptions;
557 #if !defined(PXX2)
558 #define IS_PXX2_INTERNAL_ENABLED() (false)
559 #define IS_PXX1_INTERNAL_ENABLED() (true)
560 #elif !defined(PXX1) || defined(PCBXLITES) || defined(PCBX9LITE)
561 #define IS_PXX2_INTERNAL_ENABLED() (true)
562 #define IS_PXX1_INTERNAL_ENABLED() (false)
563 #elif defined(INTERNAL_MODULE_PXX1)
564 #define IS_PXX2_INTERNAL_ENABLED() (false)
565 #define IS_PXX1_INTERNAL_ENABLED() (true)
566 #else
567 // TODO #define PXX2_PROBE
568 // TODO #define IS_PXX2_INTERNAL_ENABLED() (hardwareOptions.pxx2Enabled)
569 #define IS_PXX2_INTERNAL_ENABLED() (true)
570 #define IS_PXX1_INTERNAL_ENABLED() (true)
571 #endif
573 enum CalibratedAnalogs {
574 CALIBRATED_STICK1,
575 CALIBRATED_STICK2,
576 CALIBRATED_STICK3,
577 CALIBRATED_STICK4,
578 CALIBRATED_POT_FIRST,
579 CALIBRATED_POT_LAST = CALIBRATED_POT_FIRST + NUM_POTS - 1,
580 CALIBRATED_SLIDER_FIRST,
581 CALIBRATED_SLIDER_LAST = CALIBRATED_SLIDER_FIRST + NUM_SLIDERS - 1,
582 NUM_CALIBRATED_ANALOGS
585 #if defined(PCBX9D)
586 #define IS_POT(x) ((x)>=POT_FIRST && (x)<=POT2) // POT3 is only defined in software
587 #else
588 #define IS_POT(x) ((x)>=POT_FIRST && (x)<=POT_LAST)
589 #endif
591 #define IS_SLIDER(x) ((x)>POT_LAST && (x)<TX_VOLTAGE)
593 extern uint16_t adcValues[NUM_ANALOGS];
595 // Battery driver
596 #if defined(PCBX9E)
597 // NI-MH 9.6V
598 #define BATTERY_WARN 87 // 8.7V
599 #define BATTERY_MIN 85 // 8.5V
600 #define BATTERY_MAX 115 // 11.5V
601 #elif defined(PCBXLITE)
602 // 2 x Li-Ion
603 #define BATTERY_WARN 66 // 6.6V
604 #define BATTERY_MIN 67 // 6.7V
605 #define BATTERY_MAX 83 // 8.3V
606 #else
607 // NI-MH 7.2V
608 #define BATTERY_WARN 65 // 6.5V
609 #define BATTERY_MIN 60 // 6.0V
610 #define BATTERY_MAX 80 // 8.0V
611 #endif
613 #if defined(PCBXLITE)
614 #define BATT_SCALE 131
615 #elif defined(PCBX7)
616 #define BATT_SCALE 123
617 #elif defined(PCBX9LITE)
618 #define BATT_SCALE 117
619 #elif defined(PCBX9DP) && PCBREV >= 2019
620 #define BATT_SCALE 117
621 #else
622 #define BATT_SCALE 150
623 #endif
625 #if defined(__cplusplus) && !defined(SIMU)
626 extern "C" {
627 #endif
629 // Power driver
630 #define SOFT_PWR_CTRL
631 void pwrInit();
632 uint32_t pwrCheck();
633 void pwrOn();
634 void pwrOff();
635 bool pwrPressed();
636 #if defined(PWR_BUTTON_PRESS)
637 #define STARTUP_ANIMATION
638 uint32_t pwrPressedDuration();
639 #endif
640 void pwrResetHandler();
642 #if defined(SIMU)
643 #define UNEXPECTED_SHUTDOWN() false
644 #else
645 #define UNEXPECTED_SHUTDOWN() (WAS_RESET_BY_WATCHDOG() || g_eeGeneral.unexpectedShutdown)
646 #endif
648 // Backlight driver
649 void backlightInit();
650 void backlightDisable();
651 #define BACKLIGHT_DISABLE() backlightDisable()
652 uint8_t isBacklightEnabled();
653 #if !defined(__cplusplus)
654 #define backlightEnable(...)
655 #elif defined(PCBX9E) || defined(PCBX9DP)
656 void backlightEnable(uint8_t level = 0, uint8_t color = 0);
657 #define BACKLIGHT_ENABLE() backlightEnable(g_eeGeneral.backlightBright, g_eeGeneral.backlightColor)
658 #else
659 void backlightEnable(uint8_t level = 0);
660 #define BACKLIGHT_ENABLE() backlightEnable(g_eeGeneral.backlightBright)
661 #endif
663 #if !defined(SIMU)
664 void usbJoystickUpdate();
665 #endif
666 #define USB_NAME "FrSky Taranis"
667 #define USB_MANUFACTURER 'F', 'r', 'S', 'k', 'y', ' ', ' ', ' ' /* 8 bytes */
668 #define USB_PRODUCT 'T', 'a', 'r', 'a', 'n', 'i', 's', ' ' /* 8 Bytes */
670 #if defined(__cplusplus) && !defined(SIMU)
672 #endif
674 // I2C driver: EEPROM + Audio Volume
675 #define EEPROM_SIZE (32*1024)
677 void i2cInit();
678 void eepromReadBlock(uint8_t * buffer, size_t address, size_t size);
679 void eepromStartWrite(uint8_t * buffer, size_t address, size_t size);
680 uint8_t eepromIsTransferComplete();
682 // Debug driver
683 void debugPutc(const char c);
685 // Telemetry driver
686 void telemetryPortInit(uint32_t baudrate, uint8_t mode);
687 void telemetryPortSetDirectionInput();
688 void telemetryPortSetDirectionOutput();
689 void sportSendByte(uint8_t byte);
690 void sportSendByteLoop(uint8_t byte);
691 void sportStopSendByteLoop();
692 void sportSendBuffer(const uint8_t * buffer, uint32_t count);
693 bool telemetryGetByte(uint8_t * byte);
694 void telemetryClearFifo();
695 extern uint32_t telemetryErrors;
697 // soft-serial
698 void telemetryPortInvertedInit(uint32_t baudrate);
700 // PCBREV driver
701 #if defined(PCBX7ACCESS)
702 #define HAS_SPORT_UPDATE_CONNECTOR() true
703 #elif defined(PCBX7)
704 #define IS_PCBREV_40() (GPIO_ReadInputDataBit(PCBREV_GPIO, PCBREV_GPIO_PIN) == Bit_SET)
705 #define HAS_SPORT_UPDATE_CONNECTOR() IS_PCBREV_40()
706 #elif defined(SPORT_UPDATE_PWR_GPIO)
707 #define HAS_SPORT_UPDATE_CONNECTOR() true
708 #else
709 #define HAS_SPORT_UPDATE_CONNECTOR() false
710 #endif
712 // Sport update driver
713 #if defined(SPORT_UPDATE_PWR_GPIO)
714 void sportUpdateInit();
715 void sportUpdatePowerOn();
716 void sportUpdatePowerOff();
717 #define SPORT_UPDATE_POWER_ON() sportUpdatePowerOn()
718 #define SPORT_UPDATE_POWER_OFF() sportUpdatePowerOff()
719 #else
720 #define sportUpdateInit()
721 #define SPORT_UPDATE_POWER_ON()
722 #define SPORT_UPDATE_POWER_OFF()
723 #endif
725 // Audio driver
726 void audioInit() ;
727 void audioEnd() ;
728 void dacStart();
729 void dacStop();
730 void setSampleRate(uint32_t frequency);
731 #define VOLUME_LEVEL_MAX 23
732 #define VOLUME_LEVEL_DEF 12
733 #if !defined(SOFTWARE_VOLUME)
734 void setScaledVolume(uint8_t volume);
735 void setVolume(uint8_t volume);
736 int32_t getVolume();
737 #endif
738 #if defined(AUDIO_SPEAKER_ENABLE_GPIO)
739 void initSpeakerEnable();
740 void enableSpeaker();
741 void disableSpeaker();
742 #else
743 static inline void initSpeakerEnable() { }
744 static inline void enableSpeaker() { }
745 static inline void disableSpeaker() { }
746 #endif
747 #if defined(HEADPHONE_TRAINER_SWITCH_GPIO)
748 void initHeadphoneTrainerSwitch();
749 void enableHeadphone();
750 void enableTrainer();
751 #else
752 static inline void initHeadphoneTrainerSwitch() { }
753 static inline void enableHeadphone() { }
754 static inline void enableTrainer() { }
755 #endif
756 #if defined(JACK_DETECT_GPIO)
757 void initJackDetect();
758 bool isJackPlugged();
759 #endif
760 void audioConsumeCurrentBuffer();
761 #define audioDisableIrq() __disable_irq()
762 #define audioEnableIrq() __enable_irq()
764 // Haptic driver
765 void hapticInit();
766 void hapticOff();
767 #if defined(HAPTIC_PWM)
768 void hapticOn(uint32_t pwmPercent);
769 #else
770 void hapticOn();
771 #endif
773 // Second serial port driver
774 #if defined(AUX_SERIAL_GPIO)
775 #define DEBUG_BAUDRATE 115200
776 #define AUX_SERIAL
777 extern uint8_t auxSerialMode;
778 void auxSerialInit(unsigned int mode, unsigned int protocol);
779 void auxSerialPutc(char c);
780 #define auxSerialTelemetryInit(protocol) auxSerialInit(UART_MODE_TELEMETRY, protocol)
781 void auxSerialSbusInit();
782 void auxSerialStop();
783 #endif
785 // BT driver
786 #define BLUETOOTH_BOOTLOADER_BAUDRATE 230400
787 #define BLUETOOTH_DEFAULT_BAUDRATE 115200
788 #if defined(PCBX9E)
789 #define BLUETOOTH_FACTORY_BAUDRATE 9600
790 #else
791 #define BLUETOOTH_FACTORY_BAUDRATE 57600
792 #endif
793 #define BT_TX_FIFO_SIZE 64
794 #define BT_RX_FIFO_SIZE 128
795 void bluetoothInit(uint32_t baudrate, bool enable);
796 void bluetoothWriteWakeup();
797 uint8_t bluetoothIsWriting();
798 void bluetoothDisable();
799 #if defined(PCBX9LITES) || defined(PCBX7ACCESS)
800 #define IS_BLUETOOTH_CHIP_PRESENT() (true)
801 #elif defined(PCBX9LITE)
802 #define IS_BLUETOOTH_CHIP_PRESENT() (false)
803 #elif (defined(PCBX7) || defined(PCBXLITE)) && !defined(SIMU)
804 extern volatile uint8_t btChipPresent;
805 #define IS_BLUETOOTH_CHIP_PRESENT() (btChipPresent)
806 #else
807 #define IS_BLUETOOTH_CHIP_PRESENT() (true)
808 #endif
810 // LED driver
811 void ledInit();
812 void ledOff();
813 void ledRed();
814 void ledGreen();
815 void ledBlue();
817 // LCD driver
818 #if defined(PCBX9D) || defined(PCBX9DP) || defined(PCBX9E)
819 #define LCD_W 212
820 #define LCD_H 64
821 #define LCD_DEPTH 4
822 #define LCD_CONTRAST_MIN 0
823 #define LCD_CONTRAST_MAX 45
824 #define LCD_CONTRAST_DEFAULT 25
825 #else
826 #define LCD_W 128
827 #define LCD_H 64
828 #define LCD_DEPTH 1
829 #define IS_LCD_RESET_NEEDED() true
830 #define LCD_CONTRAST_MIN 10
831 #define LCD_CONTRAST_MAX 30
832 #define LCD_CONTRAST_DEFAULT 20
833 #endif
835 #if defined(PCBX9D) || defined(PCBX9E) || (defined(PCBX9DP) && PCBREV < 2019)
836 #define IS_LCD_RESET_NEEDED() (!WAS_RESET_BY_WATCHDOG_OR_SOFTWARE())
837 #else
838 #define IS_LCD_RESET_NEEDED() true
839 #endif
841 void lcdInit();
842 void lcdInitFinish();
843 void lcdOff();
845 // TODO lcdRefreshWait() stub in simpgmspace and remove LCD_DUAL_BUFFER
846 #if defined(LCD_DMA) && !defined(LCD_DUAL_BUFFER) && !defined(SIMU)
847 void lcdRefreshWait();
848 #else
849 #define lcdRefreshWait()
850 #endif
851 #if defined(PCBX9D) || defined(SIMU) || !defined(__cplusplus)
852 void lcdRefresh();
853 #else
854 void lcdRefresh(bool wait=true); // TODO uint8_t wait to simplify this
855 #endif
856 void lcdSetRefVolt(unsigned char val);
857 void lcdSetContrast();
859 // Top LCD driver
860 #if defined(TOPLCD_GPIO)
861 void toplcdInit();
862 void toplcdOff();
863 void toplcdRefreshStart();
864 void toplcdRefreshEnd();
865 void setTopFirstTimer(int32_t value);
866 void setTopSecondTimer(uint32_t value);
867 void setTopRssi(uint32_t rssi);
868 void setTopBatteryState(int state, uint8_t blinking);
869 void setTopBatteryValue(uint32_t volts);
870 #endif
872 #define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)
874 #if defined(__cplusplus)
875 #include "fifo.h"
876 #include "dmafifo.h"
878 #if defined(CROSSFIRE)
879 #define TELEMETRY_FIFO_SIZE 128
880 #else
881 #define TELEMETRY_FIFO_SIZE 64
882 #endif
884 extern Fifo<uint8_t, TELEMETRY_FIFO_SIZE> telemetryFifo;
885 typedef DMAFifo<32> AuxSerialRxFifo;
886 extern AuxSerialRxFifo auxSerialRxFifo;
887 #endif
889 // Gyro driver
890 #define GYRO_VALUES_COUNT 6
891 #define GYRO_BUFFER_LENGTH (GYRO_VALUES_COUNT * sizeof(int16_t))
892 int gyroInit();
893 int gyroRead(uint8_t buffer[GYRO_BUFFER_LENGTH]);
894 #define GYRO_MAX_DEFAULT 30
895 #define GYRO_MAX_RANGE 60
896 #define GYRO_OFFSET_MIN -30
897 #define GYRO_OFFSET_MAX 10
899 #endif // _BOARD_H_