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_HORUS_H_
22 #define _BOARD_HORUS_H_
27 #if defined(__cplusplus) && !defined(SIMU)
32 // clang is very picky about the use of "register"
33 // Tell clang to ignore the warnings for the following files
34 #pragma clang diagnostic push
35 #pragma clang diagnostic ignored "-Wdeprecated-register"
38 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h"
39 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_rcc.h"
40 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_gpio.h"
41 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_spi.h"
42 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_i2c.h"
43 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_rtc.h"
44 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_pwr.h"
45 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_dma.h"
46 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_usart.h"
47 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_flash.h"
48 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_sdio.h"
49 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_dbgmcu.h"
50 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_ltdc.h"
51 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_fmc.h"
52 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_tim.h"
53 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_dma2d.h"
56 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_adc.h"
59 #include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/misc.h"
62 // Restore warnings about registers
63 #pragma clang diagnostic pop
66 #include "usb_driver.h"
69 #include "usbd_cdc_core.h"
70 #include "usbd_msc_core.h"
71 #include "usbd_hid_core.h"
73 #include "usbd_desc.h"
75 #include "usbd_conf.h"
80 #if defined(__cplusplus) && !defined(SIMU)
84 #define FLASHSIZE 0x80000
85 #define BOOTLOADER_SIZE 0x8000
86 #define FIRMWARE_ADDRESS 0x08000000
89 #define LUA_MEM_EXTRA_MAX (2 MB) // max allowed memory usage for Lua bitmaps (in bytes)
90 #define LUA_MEM_MAX (6 MB) // max allowed memory usage for complete Lua (in bytes), 0 means unlimited
92 // HSI is at 168Mhz (over-drive is not enabled!)
93 #define PERI1_FREQUENCY 42000000
94 #define PERI2_FREQUENCY 84000000
95 #define TIMER_MULT_APB1 2
96 #define TIMER_MULT_APB2 2
98 #define strcpy_P strcpy
99 #define strcat_P strcat
101 extern uint16_t sessionTimer
;
103 #define SLAVE_MODE() (g_model.trainerMode == TRAINER_MODE_SLAVE)
104 #define TRAINER_CONNECTED() (GPIO_ReadInputDataBit(TRAINER_DETECT_GPIO, TRAINER_DETECT_GPIO_PIN) == Bit_RESET)
107 void boardInit(void);
114 void delaysInit(void);
115 void delay_01us(uint16_t nb
);
116 void delay_us(uint16_t nb
);
117 void delay_ms(uint32_t ms
);
123 #define IS_HORUS_PROD() GPIO_ReadInputDataBit(PCBREV_GPIO, PCBREV_GPIO_PIN)
124 #if defined(SIMU) || defined(PCBX10)
125 #define IS_FIRMWARE_COMPATIBLE_WITH_BOARD() true
127 #define IS_FIRMWARE_COMPATIBLE_WITH_BOARD() IS_HORUS_PROD()
129 #define IS_FIRMWARE_COMPATIBLE_WITH_BOARD() (!IS_HORUS_PROD())
133 #define LEN_CPU_UID (3*8+2)
134 void getCPUUniqueID(char * s
);
137 #define BLOCK_SIZE 512 /* Block Size in Bytes */
138 #if !defined(SIMU) || defined(SIMU_DISKIO)
139 uint32_t sdIsHC(void);
140 uint32_t sdGetSpeed(void);
141 #define SD_IS_HC() (sdIsHC())
142 #define SD_GET_SPEED() (sdGetSpeed())
143 #define SD_GET_FREE_BLOCKNR() (sdGetFreeSectors())
144 #define SD_CARD_PRESENT() (~SD_PRESENT_GPIO->IDR & SD_PRESENT_GPIO_PIN)
149 uint32_t sdMounted(void);
151 #define SD_IS_HC() (0)
152 #define SD_GET_SPEED() (0)
156 #define SD_CARD_PRESENT() true
158 #if defined(DISK_CACHE)
160 DRESULT
__disk_read(BYTE drv
, BYTE
* buff
, DWORD sector
, UINT count
);
161 DRESULT
__disk_write(BYTE drv
, const BYTE
* buff
, DWORD sector
, UINT count
);
163 #define __disk_read disk_read
164 #define __disk_write disk_write
167 // Flash Write driver
168 #define FLASH_PAGESIZE 256
169 void unlockFlash(void);
170 void lockFlash(void);
171 void flashWrite(uint32_t * address
, uint32_t * buffer
);
172 uint32_t isFirmwareStart(const uint8_t * buffer
);
173 uint32_t isBootloaderStart(const uint8_t * buffer
);
176 void SDRAM_Init(void);
179 #define INTERNAL_MODULE_ON() GPIO_SetBits(INTMODULE_PWR_GPIO, INTMODULE_PWR_GPIO_PIN)
180 #define INTERNAL_MODULE_OFF() GPIO_ResetBits(INTMODULE_PWR_GPIO, INTMODULE_PWR_GPIO_PIN)
181 #define EXTERNAL_MODULE_ON() GPIO_SetBits(EXTMODULE_PWR_GPIO, EXTMODULE_PWR_GPIO_PIN)
182 #define EXTERNAL_MODULE_OFF() GPIO_ResetBits(EXTMODULE_PWR_GPIO, EXTMODULE_PWR_GPIO_PIN)
183 #define IS_INTERNAL_MODULE_ON() (GPIO_ReadInputDataBit(INTMODULE_PWR_GPIO, INTMODULE_PWR_GPIO_PIN) == Bit_SET)
184 #define IS_EXTERNAL_MODULE_ON() (GPIO_ReadInputDataBit(EXTMODULE_PWR_GPIO, EXTMODULE_PWR_GPIO_PIN) == Bit_SET)
185 #define IS_UART_MODULE(port) (port == INTERNAL_MODULE)
187 void init_no_pulses(uint32_t port
);
188 void disable_no_pulses(uint32_t port
);
189 void init_ppm(uint32_t module_index
);
190 void disable_ppm(uint32_t module_index
);
191 void init_pxx(uint32_t module_index
);
192 void disable_pxx(uint32_t module_index
);
193 void init_dsm2(uint32_t module_index
);
194 void disable_dsm2(uint32_t module_index
);
195 void init_crossfire(uint32_t module_index
);
196 void disable_crossfire(uint32_t module_index
);
198 #if defined(MULTIMODULE)
199 void init_multimodule(uint32_t module_index
);
200 void disable_multimodule(uint32_t module_index
);
204 void init_trainer_ppm(void);
205 void stop_trainer_ppm(void);
206 void init_trainer_capture(void);
207 void stop_trainer_capture(void);
220 KEY_RIGHT
= KEY_TELEM
,
222 KEY_LEFT
= KEY_RADIO
,
225 TRM_LH_DWN
= TRM_BASE
,
237 TRM_LAST
= TRM_RS_UP
,
254 #define IS_3POS(x) ((x) != SW_SF && (x) != SW_SH)
256 enum EnumSwitchesPositions
284 uint8_t keyState(uint8_t index
);
285 uint32_t switchState(uint8_t index
);
286 uint32_t readKeys(void);
287 uint32_t readTrims(void);
288 #define TRIMS_PRESSED() (readTrims())
289 #define KEYS_PRESSED() (readKeys())
290 #define DBLKEYS_PRESSED_RGT_LFT(in) ((in & ((1<<KEY_RIGHT) + (1<<KEY_LEFT))) == ((1<<KEY_RIGHT) + (1<<KEY_LEFT)))
291 #define DBLKEYS_PRESSED_UP_DWN(in) ((in & ((1<<KEY_UP) + (1<<KEY_DOWN))) == ((1<<KEY_UP) + (1<<KEY_DOWN)))
292 #define DBLKEYS_PRESSED_RGT_UP(in) ((in & ((1<<KEY_RIGHT) + (1<<KEY_UP))) == ((1<<KEY_RIGHT) + (1<<KEY_UP)))
293 #define DBLKEYS_PRESSED_LFT_DWN(in) ((in & ((1<<KEY_LEFT) + (1<<KEY_DOWN))) == ((1<<KEY_LEFT) + (1<<KEY_DOWN)))
295 // Rotary encoder driver
296 #define ROTARY_ENCODER_NAVIGATION
297 void checkRotaryEncoder(void);
300 #define WDTO_500MS 500
301 extern uint32_t powerupReason
;
303 #define SHUTDOWN_REQUEST 0xDEADBEEF
304 #define NO_SHUTDOWN_REQUEST ~SHUTDOWN_REQUEST
305 #define DIRTY_SHUTDOWN 0xCAFEDEAD
306 #define NORMAL_POWER_OFF ~DIRTY_SHUTDOWN
308 #define wdt_disable()
309 void watchdogInit(unsigned int duration
);
311 #define WAS_RESET_BY_WATCHDOG() (false)
312 #define WAS_RESET_BY_SOFTWARE() (false)
313 #define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() (false)
314 #define wdt_enable(x)
317 #if defined(WATCHDOG_DISABLED)
318 #define wdt_enable(x)
321 #define wdt_enable(x) watchdogInit(x)
322 #define wdt_reset() IWDG->KR = 0xAAAA
324 #define WAS_RESET_BY_WATCHDOG() (RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF))
325 #define WAS_RESET_BY_SOFTWARE() (RCC->CSR & RCC_CSR_SFTRSTF)
326 #define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() (RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_SFTRSTF))
331 #define NUM_XPOTS NUM_POTS
333 #define NUM_SLIDERS 2
335 #define NUM_SLIDERS 4
348 SLIDER1
= SLIDER_FIRST
,
351 SLIDER_FRONT_LEFT
= SLIDER_FIRST
,
359 SLIDER_LAST
= SLIDER_FIRST
+ NUM_SLIDERS
- 1,
366 enum CalibratedAnalogs
{
375 CALIBRATED_SLIDER_FRONT_LEFT
,
376 CALIBRATED_SLIDER_FRONT_RIGHT
,
377 CALIBRATED_SLIDER_REAR_LEFT
,
378 CALIBRATED_SLIDER_REAR_RIGHT
,
380 CALIBRATED_SLIDER_REAR_LEFT
,
381 CALIBRATED_SLIDER_REAR_RIGHT
,
385 NUM_CALIBRATED_ANALOGS
388 #define IS_POT(x) ((x)>=POT_FIRST && (x)<=POT_LAST)
389 #define IS_SLIDER(x) ((x)>=SLIDER_FIRST && (x)<=SLIDER_LAST)
390 extern uint16_t adcValues
[NUM_ANALOGS
];
393 uint16_t getAnalogValue(uint8_t index
);
394 uint16_t getBatteryVoltage(); // returns current battery voltage in 10mV steps
396 #if defined(__cplusplus) && !defined(SIMU)
401 #define SOFT_PWR_CTRL
403 uint32_t pwrCheck(void);
406 void pwrResetHandler(void);
407 uint32_t pwrPressed(void);
408 uint32_t pwrPressedDuration(void);
409 #if defined(SIMU) || defined(NO_UNEXPECTED_SHUTDOWN)
410 #define UNEXPECTED_SHUTDOWN() (false)
412 #define UNEXPECTED_SHUTDOWN() (powerupReason == DIRTY_SHUTDOWN)
429 void lcdRefresh(void);
430 void DMAFillRect(uint16_t * dest
, uint16_t destw
, uint16_t desth
, uint16_t x
, uint16_t y
, uint16_t w
, uint16_t h
, uint16_t color
);
431 void DMACopyBitmap(uint16_t * dest
, uint16_t destw
, uint16_t desth
, uint16_t x
, uint16_t y
, const uint16_t * src
, uint16_t srcw
, uint16_t srch
, uint16_t srcx
, uint16_t srcy
, uint16_t w
, uint16_t h
);
432 void DMACopyAlphaBitmap(uint16_t * dest
, uint16_t destw
, uint16_t desth
, uint16_t x
, uint16_t y
, const uint16_t * src
, uint16_t srcw
, uint16_t srch
, uint16_t srcx
, uint16_t srcy
, uint16_t w
, uint16_t h
);
433 void DMABitmapConvert(uint16_t * dest
, const uint8_t * src
, uint16_t w
, uint16_t h
, uint32_t format
);
434 void lcdStoreBackupBuffer(void);
435 int lcdRestoreBackupBuffer(void);
436 void lcdSetContrast();
438 #define lcdSetRefVolt(...)
439 #define lcdRefreshWait(...)
442 void backlightInit(void);
444 #define backlightEnable(...)
446 void backlightEnable(uint8_t dutyCycle
);
448 #define BACKLIGHT_LEVEL_MAX 100
450 #define BACKLIGHT_LEVEL_MIN 5
452 #define BACKLIGHT_LEVEL_MIN 42
454 #define BACKLIGHT_ENABLE() backlightEnable(unexpectedShutdown ? BACKLIGHT_LEVEL_MAX : BACKLIGHT_LEVEL_MAX-g_eeGeneral.backlightBright)
455 #define BACKLIGHT_DISABLE() backlightEnable(unexpectedShutdown ? BACKLIGHT_LEVEL_MAX : g_eeGeneral.blOffBright)
456 #define isBacklightEnabled() true
459 void usbJoystickUpdate();
462 #define USB_NAME "FrSky Horus"
463 #define USB_MANUFACTURER 'F', 'r', 'S', 'k', 'y', ' ', ' ', ' ' /* 8 bytes */
464 #define USB_PRODUCT 'H', 'o', 'r', 'u', 's', ' ', ' ', ' ' /* 8 Bytes */
465 #elif defined(PCBX10)
466 #define USB_NAME "FrSky X10"
467 #define USB_MANUFACTURER 'F', 'r', 'S', 'k', 'y', ' ', ' ', ' ' /* 8 bytes */
468 #define USB_PRODUCT 'X', '1', '0', ' ', ' ', ' ', ' ', ' ' /* 8 Bytes */
471 #if defined(__cplusplus) && !defined(SIMU)
476 void audioInit(void);
477 void audioConsumeCurrentBuffer(void);
478 #define audioDisableIrq() // interrupts must stay enabled on Horus
479 #define audioEnableIrq() // interrupts must stay enabled on Horus
481 #define setSampleRate(freq)
483 void setSampleRate(uint32_t frequency
);
485 void setScaledVolume(uint8_t volume
);
486 void setVolume(uint8_t volume
);
487 int32_t getVolume(void);
488 #define VOLUME_LEVEL_MAX 23
489 #define VOLUME_LEVEL_DEF 12
492 #define TELEMETRY_FIFO_SIZE 512
493 void telemetryPortInit(uint32_t baudrate
, uint8_t mode
);
494 void telemetryPortSetDirectionOutput(void);
495 void sportSendBuffer(uint8_t * buffer
, uint32_t count
);
496 uint8_t telemetryGetByte(uint8_t * byte
);
498 // Sport update driver
499 #define SPORT_UPDATE_POWER_ON() EXTERNAL_MODULE_ON()
500 #define SPORT_UPDATE_POWER_OFF() EXTERNAL_MODULE_OFF()
503 void hapticInit(void);
504 void hapticDone(void);
505 void hapticOff(void);
506 #define HAPTIC_OFF() hapticOff()
507 void hapticOn(uint32_t pwmPercent
);
510 void gpsInit(uint32_t baudrate
);
511 uint8_t gpsGetByte(uint8_t * byte
);
513 extern uint8_t gpsTraceEnabled
;
515 void gpsSendByte(uint8_t byte
);
517 // Second serial port driver
519 #define DEBUG_BAUDRATE 115200
520 extern uint8_t serial2Mode
;
521 void serial2Init(unsigned int mode
, unsigned int protocol
);
522 void serial2Putc(char c
);
523 #define serial2TelemetryInit(protocol) serial2Init(UART_MODE_TELEMETRY, protocol)
524 void serial2SbusInit(void);
525 void serial2Stop(void);
526 #define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)
527 int sbusGetByte(uint8_t * byte
);
530 #define BLUETOOTH_FACTORY_BAUDRATE 57600
531 #define BLUETOOTH_DEFAULT_BAUDRATE 115200
532 void bluetoothInit(uint32_t baudrate
);
533 void bluetoothWriteWakeup(void);
534 uint8_t bluetoothIsWriting(void);
535 void bluetoothDone(void);
537 extern uint8_t currentTrainerMode
;
538 void checkTrainerSettings(void);
540 #if defined(__cplusplus)
543 extern DMAFifo
<512> telemetryFifo
;
544 extern DMAFifo
<32> serial2RxFifo
;
548 #endif // _BOARD_HORUS_H_