Cosmetics
[opentx.git] / radio / src / targets / horus / board.cpp
blob048817c69745b1c69c7b912e239e3806216db099
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 #include "opentx.h"
23 #if defined(__cplusplus)
24 extern "C" {
25 #endif
26 #include "usb_dcd_int.h"
27 #include "usb_bsp.h"
28 #if defined(__cplusplus)
30 #endif
32 HardwareOptions hardwareOptions;
34 void watchdogInit(unsigned int duration)
36 IWDG->KR = 0x5555; // Unlock registers
37 IWDG->PR = 3; // Divide by 32 => 1kHz clock
38 IWDG->KR = 0x5555; // Unlock registers
39 IWDG->RLR = duration;
40 IWDG->KR = 0xAAAA; // reload
41 IWDG->KR = 0xCCCC; // start
44 #if defined(SEMIHOSTING)
45 extern "C" void initialise_monitor_handles();
46 #endif
48 #if defined(PCBX10) && !defined(RADIO_T16)
49 void sportUpdateInit()
51 GPIO_InitTypeDef GPIO_InitStructure;
52 GPIO_InitStructure.GPIO_Pin = SPORT_UPDATE_PWR_GPIO_PIN;
53 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
54 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
55 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
56 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
57 GPIO_Init(SPORT_UPDATE_PWR_GPIO, &GPIO_InitStructure);
60 void sportUpdatePowerOn()
62 GPIO_SetBits(SPORT_UPDATE_PWR_GPIO, SPORT_UPDATE_PWR_GPIO_PIN);
65 void sportUpdatePowerOff()
67 GPIO_ResetBits(SPORT_UPDATE_PWR_GPIO, SPORT_UPDATE_PWR_GPIO_PIN);
69 #endif
71 void boardInit()
73 #if defined(SEMIHOSTING)
74 initialise_monitor_handles();
75 #endif
77 RCC_AHB1PeriphClockCmd(PWR_RCC_AHB1Periph |
78 PCBREV_RCC_AHB1Periph |
79 LED_RCC_AHB1Periph |
80 LCD_RCC_AHB1Periph |
81 BACKLIGHT_RCC_AHB1Periph |
82 SD_RCC_AHB1Periph |
83 AUDIO_RCC_AHB1Periph |
84 KEYS_RCC_AHB1Periph |
85 ADC_RCC_AHB1Periph |
86 AUX_SERIAL_RCC_AHB1Periph |
87 TELEMETRY_RCC_AHB1Periph |
88 TRAINER_RCC_AHB1Periph |
89 BT_RCC_AHB1Periph |
90 AUDIO_RCC_AHB1Periph |
91 HAPTIC_RCC_AHB1Periph |
92 INTMODULE_RCC_AHB1Periph |
93 EXTMODULE_RCC_AHB1Periph |
94 GPS_RCC_AHB1Periph |
95 SPORT_UPDATE_RCC_AHB1Periph,
96 ENABLE);
98 RCC_APB1PeriphClockCmd(ROTARY_ENCODER_RCC_APB1Periph |
99 INTERRUPT_xMS_RCC_APB1Periph |
100 ADC_RCC_APB1Periph |
101 TIMER_2MHz_RCC_APB1Periph |
102 AUDIO_RCC_APB1Periph |
103 AUX_SERIAL_RCC_APB1Periph |
104 TELEMETRY_RCC_APB1Periph |
105 TRAINER_RCC_APB1Periph |
106 AUDIO_RCC_APB1Periph |
107 INTMODULE_RCC_APB1Periph |
108 EXTMODULE_RCC_APB1Periph |
109 GPS_RCC_APB1Periph |
110 BACKLIGHT_RCC_APB1Periph,
111 ENABLE);
113 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG |
114 LCD_RCC_APB2Periph |
115 ADC_RCC_APB2Periph |
116 HAPTIC_RCC_APB2Periph |
117 INTMODULE_RCC_APB2Periph |
118 EXTMODULE_RCC_APB2Periph |
119 TELEMETRY_RCC_APB2Periph |
120 BT_RCC_APB2Periph |
121 BACKLIGHT_RCC_APB2Periph,
122 ENABLE);
124 pwrInit();
125 pwrOn();
126 delaysInit();
128 __enable_irq();
130 TRACE("\nHorus board started :)");
131 TRACE("RCC->CSR = %08x", RCC->CSR);
133 audioInit();
135 // we need to initialize g_FATFS_Obj here, because it is in .ram section (because of DMA access)
136 // and this section is un-initialized
137 memset(&g_FATFS_Obj, 0, sizeof(g_FATFS_Obj));
139 keysInit();
140 rotaryEncoderInit();
142 #if NUM_PWMSTICKS > 0
143 sticksPwmInit();
144 delay_ms(20);
145 if (pwm_interrupt_count < 32) {
146 hardwareOptions.sticksPwmDisabled = true;
148 #endif
150 adcInit();
151 lcdInit();
152 backlightInit();
154 init2MhzTimer();
155 init5msTimer();
156 usbInit();
157 hapticInit();
159 #if defined(BLUETOOTH)
160 bluetoothInit(BLUETOOTH_DEFAULT_BAUDRATE, true);
161 #endif
163 #if defined(INTERNAL_GPS)
164 gpsInit(GPS_USART_BAUDRATE);
165 #endif
167 #if defined(DEBUG)
168 DBGMCU_APB1PeriphConfig(DBGMCU_IWDG_STOP|DBGMCU_TIM1_STOP|DBGMCU_TIM2_STOP|DBGMCU_TIM3_STOP|DBGMCU_TIM4_STOP|DBGMCU_TIM5_STOP|DBGMCU_TIM6_STOP|DBGMCU_TIM7_STOP|DBGMCU_TIM8_STOP|DBGMCU_TIM9_STOP|DBGMCU_TIM10_STOP|DBGMCU_TIM11_STOP|DBGMCU_TIM12_STOP|DBGMCU_TIM13_STOP|DBGMCU_TIM14_STOP, ENABLE);
169 #endif
171 ledInit();
173 #if defined(PCBX10) && !defined(RADIO_T16)
174 sportUpdateInit();
175 #endif
177 ledBlue();
179 #if defined(RTCLOCK) && !defined(COPROCESSOR)
180 rtcInit(); // RTC must be initialized before rambackupRestore() is called
181 #endif
184 void boardOff()
186 backlightEnable(0);
188 while (pwrPressed()) {
189 WDG_RESET();
192 SysTick->CTRL = 0; // turn off systick
194 #if defined(PCBX12S)
195 // Shutdown the Audio amp
196 GPIO_InitTypeDef GPIO_InitStructure;
197 GPIO_InitStructure.GPIO_Pin = AUDIO_SHUTDOWN_GPIO_PIN;
198 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
199 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
200 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
201 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
202 GPIO_Init(AUDIO_SHUTDOWN_GPIO, &GPIO_InitStructure);
203 GPIO_ResetBits(AUDIO_SHUTDOWN_GPIO, AUDIO_SHUTDOWN_GPIO_PIN);
204 #endif
206 // Shutdown the Haptic
207 hapticDone();
209 #if defined(RTC_BACKUP_RAM)
210 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, DISABLE);
211 PWR_BackupRegulatorCmd(DISABLE);
212 #endif
214 RTC->BKP0R = SHUTDOWN_REQUEST;
216 pwrOff();
219 uint16_t getBatteryVoltage()
221 int32_t instant_vbat = anaIn(TX_VOLTAGE); // using filtered ADC value on purpose
222 return (uint16_t)((instant_vbat * (1000 + g_eeGeneral.txVoltageCalibration)) / 1629);