update credits
[librepilot.git] / flight / targets / boards / tinyfish / firmware / pios_board.c
blob5535c0f05d57dd316c98d335c47e4fa0a0e424dc
1 /**
2 *****************************************************************************
3 * @file pios_board.c
4 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
5 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * PhoenixPilot, http://github.com/PhoenixPilot, Copyright (C) 2012
7 * @addtogroup LibrePilotSystem LibrePilot System
8 * @{
9 * @addtogroup LibrePilotCore LibrePilot Core
10 * @{
11 * @brief Defines board specific static initializers for hardware for the tinyFISH board.
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "inc/openpilot.h"
30 #include <pios_board_info.h>
31 #include <uavobjectsinit.h>
32 #include <hwsettings.h>
33 #include <hwtinyfishsettings.h>
34 #include <manualcontrolsettings.h>
35 #include <gcsreceiver.h>
36 #include <taskinfo.h>
37 #include <sanitycheck.h>
38 #include <actuatorsettings.h>
39 #include <auxmagsettings.h>
40 #include <flightbatterysettings.h>
41 #include <revosettings.h>
42 #ifdef PIOS_INCLUDE_INSTRUMENTATION
43 #include <pios_instrumentation.h>
44 #endif
46 #include <pios_board_io.h>
47 #include <pios_board_sensors.h>
50 * Pull in the board-specific static HW definitions.
51 * Including .c files is a bit ugly but this allows all of
52 * the HW definitions to be const and static to limit their
53 * scope.
55 * NOTE: THIS IS THE ONLY PLACE THAT SHOULD EVER INCLUDE THIS FILE
57 #include "../board_hw_defs.c"
59 uintptr_t pios_uavo_settings_fs_id;
60 uintptr_t pios_user_fs_id = 0;
62 #ifdef PIOS_INCLUDE_WS2811
63 uint32_t pios_ws2811_id;
64 #endif
66 void FlightBatterySettingsDataOverrideDefaults(FlightBatterySettingsData *data)
68 data->SensorCalibrations.VoltageFactor = 8.8f;
69 data->SensorCalibrations.CurrentFactor = 0.07f;
72 void RevoSettingsDataOverrideDefaults(RevoSettingsData *data)
74 /* This board has no barometer, so adjust default fusion algorithm to one that does not depend on working baro */
75 data->FusionAlgorithm = REVOSETTINGS_FUSIONALGORITHM_ACRONOSENSORS;
78 static HwTinyFISHSettingsData boardHwSettings;
80 static void hwTinyFISHSettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
82 HwTinyFISHSettingsData currentBoardHwSettings;
84 HwTinyFISHSettingsGet(&currentBoardHwSettings);
86 if (memcmp(&currentBoardHwSettings, &boardHwSettings, sizeof(HwTinyFISHSettingsData)) != 0) {
87 ExtendedAlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL, SYSTEMALARMS_EXTENDEDALARMSTATUS_REBOOTREQUIRED, 0);
91 /**
92 * PIOS_Board_Init()
93 * initializes all the core subsystems on this specific hardware
94 * called from System/openpilot.c
97 void PIOS_Board_Init(void)
99 #if defined(PIOS_INCLUDE_LED)
100 const struct pios_gpio_cfg *led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(pios_board_info_blob.board_rev);
101 PIOS_DEBUG_Assert(led_cfg);
102 PIOS_LED_Init(led_cfg);
103 #endif /* PIOS_INCLUDE_LED */
105 #ifdef PIOS_INCLUDE_INSTRUMENTATION
106 PIOS_Instrumentation_Init(PIOS_INSTRUMENTATION_MAX_COUNTERS);
107 #endif
109 #if defined(PIOS_INCLUDE_SPI)
110 /* Set up the SPI interface to the mpu6000 */
112 if (PIOS_SPI_Init(&pios_spi1_id, &pios_spi1_cfg)) {
113 PIOS_DEBUG_Assert(0);
116 if (PIOS_SPI_Init(&pios_spi2_id, &pios_spi2_cfg)) {
117 PIOS_DEBUG_Assert(0);
119 #endif
121 #if defined(PIOS_INCLUDE_FLASH)
122 /* Connect flash to the appropriate interface and configure it */
123 uintptr_t flash_id;
125 // initialize the internal settings storage flash
126 if (PIOS_Flash_Internal_Init(&flash_id, &flash_internal_system_cfg)) {
127 PIOS_DEBUG_Assert(0);
130 if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_internal_cfg, &pios_internal_flash_driver, flash_id)) {
131 PIOS_DEBUG_Assert(0);
134 /* init SPI flash here */
136 #endif /* if defined(PIOS_INCLUDE_FLASH) */
138 /* Initialize the task monitor */
139 if (PIOS_TASK_MONITOR_Initialize(TASKINFO_RUNNING_NUMELEM)) {
140 PIOS_Assert(0);
143 /* Initialize the delayed callback library */
144 PIOS_CALLBACKSCHEDULER_Initialize();
146 /* Initialize UAVObject libraries */
147 EventDispatcherInitialize();
148 UAVObjInitialize();
149 SETTINGS_INITIALISE_ALL;
151 #if defined(PIOS_INCLUDE_RTC)
152 /* Initialize the real-time clock and its associated tick */
153 PIOS_RTC_Init(&pios_rtc_main_cfg);
154 #endif
155 PIOS_IAP_Init();
156 // check for safe mode commands from gcs
157 if (PIOS_IAP_ReadBootCmd(0) == PIOS_IAP_CLEAR_FLASH_CMD_0 &&
158 PIOS_IAP_ReadBootCmd(1) == PIOS_IAP_CLEAR_FLASH_CMD_1 &&
159 PIOS_IAP_ReadBootCmd(2) == PIOS_IAP_CLEAR_FLASH_CMD_2) {
160 PIOS_FLASHFS_Format(pios_uavo_settings_fs_id);
161 PIOS_IAP_WriteBootCmd(0, 0);
162 PIOS_IAP_WriteBootCmd(1, 0);
163 PIOS_IAP_WriteBootCmd(2, 0);
166 #ifndef ERASE_FLASH
167 #ifdef PIOS_INCLUDE_WDG
168 /* Initialize watchdog as early as possible to catch faults during init */
169 PIOS_WDG_Init();
170 #endif
171 #endif
173 /* Initialize the alarms library */
174 AlarmsInitialize();
176 /* Check for repeated boot failures */
177 uint16_t boot_count = PIOS_IAP_ReadBootCount();
178 if (boot_count < 3) {
179 PIOS_IAP_WriteBootCount(++boot_count);
180 AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT);
181 } else {
182 /* Too many failed boot attempts, force hwsettings to defaults */
183 HwSettingsSetDefaults(HwSettingsHandle(), 0);
184 HwTinyFISHSettingsSetDefaults(HwTinyFISHSettingsHandle(), 0);
186 AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL);
190 PIOS_TIM_InitClock(&tim_1_cfg);
191 PIOS_TIM_InitClock(&tim_2_cfg);
192 // PIOS_TIM_InitClock(&tim_3_cfg);
193 PIOS_TIM_InitClock(&tim_4_cfg);
194 PIOS_TIM_InitClock(&tim_8_cfg);
195 PIOS_TIM_InitClock(&tim_15_cfg);
196 // PIOS_TIM_InitClock(&tim_16_cfg);
197 // PIOS_TIM_InitClock(&tim_17_cfg);
199 #if defined(PIOS_INCLUDE_USB)
200 PIOS_BOARD_IO_Configure_USB();
201 #endif
203 HwTinyFISHSettingsConnectCallback(hwTinyFISHSettingsUpdatedCb);
205 HwTinyFISHSettingsGet(&boardHwSettings);
207 static const PIOS_BOARD_IO_UART_Function uart_function_map[] = {
208 [HWTINYFISHSETTINGS_UART3PORT_TELEMETRY] = PIOS_BOARD_IO_UART_TELEMETRY,
209 [HWTINYFISHSETTINGS_UART3PORT_GPS] = PIOS_BOARD_IO_UART_GPS,
210 [HWTINYFISHSETTINGS_UART3PORT_SBUS] = PIOS_BOARD_IO_UART_SBUS,
211 [HWTINYFISHSETTINGS_UART3PORT_DSM] = PIOS_BOARD_IO_UART_DSM_MAIN, // single DSM instance? ok.
212 [HWTINYFISHSETTINGS_UART3PORT_EXBUS] = PIOS_BOARD_IO_UART_EXBUS,
213 [HWTINYFISHSETTINGS_UART3PORT_HOTTSUMD] = PIOS_BOARD_IO_UART_HOTT_SUMD,
214 [HWTINYFISHSETTINGS_UART3PORT_HOTTSUMH] = PIOS_BOARD_IO_UART_HOTT_SUMH,
215 [HWTINYFISHSETTINGS_UART3PORT_SRXL] = PIOS_BOARD_IO_UART_SRXL,
216 [HWTINYFISHSETTINGS_UART3PORT_IBUS] = PIOS_BOARD_IO_UART_IBUS,
217 [HWTINYFISHSETTINGS_UART3PORT_DEBUGCONSOLE] = PIOS_BOARD_IO_UART_DEBUGCONSOLE,
218 [HWTINYFISHSETTINGS_UART3PORT_COMBRIDGE] = PIOS_BOARD_IO_UART_COMBRIDGE,
219 [HWTINYFISHSETTINGS_UART3PORT_MSP] = PIOS_BOARD_IO_UART_MSP,
220 [HWTINYFISHSETTINGS_UART3PORT_MAVLINK] = PIOS_BOARD_IO_UART_MAVLINK,
221 [HWTINYFISHSETTINGS_UART3PORT_HOTTTELEMETRY] = PIOS_BOARD_IO_UART_HOTT_BRIDGE,
222 [HWTINYFISHSETTINGS_UART3PORT_FRSKYSENSORHUB] = PIOS_BOARD_IO_UART_FRSKY_SENSORHUB,
225 if (boardHwSettings.UART3Port < NELEMENTS(uart_function_map)) {
226 PIOS_BOARD_IO_Configure_UART(&pios_usart_cfg[2], uart_function_map[boardHwSettings.UART3Port]);
229 #ifdef PIOS_INCLUDE_PPM
230 if (boardHwSettings.UART3Port == HWTINYFISHSETTINGS_UART3PORT_PPM) {
231 PIOS_BOARD_IO_Configure_PPM_RCVR(&pios_ppm_cfg);
233 #endif
234 if (PIOS_COM_DEBUG) {
235 PIOS_COM_ChangeBaud(PIOS_COM_DEBUG, 57600);
236 DEBUG_PRINTF(0, "\r\n\r\ntinyFISH FC booting\r\n");
239 /* Configure SBus as primary usart client.
240 * PIOS_BOARD_IO_Configure_UART() will also lock COM port config so that secondary client cannot mess with settings */
241 if (boardHwSettings.UART3Port != HWTINYFISHSETTINGS_UART3PORT_SBUS) {
242 PIOS_BOARD_IO_Configure_UART(&pios_usart_cfg[1], PIOS_BOARD_IO_UART_SBUS_NOT_INVERTED);
245 /* Configure Frsky SensorHub as secondary client. */
246 if (boardHwSettings.UART3Port != HWTINYFISHSETTINGS_UART3PORT_FRSKYSENSORHUB) {
247 PIOS_BOARD_IO_Configure_UART(&pios_usart_cfg[1], PIOS_BOARD_IO_UART_FRSKY_SENSORHUB);
250 /* and what to do with UART1? There is RX_DEBUG only connected. Maybe make it into configurable ComBridge? */
251 if (boardHwSettings.UART1Port == HWTINYFISHSETTINGS_UART1PORT_COMBRIDGE) {
252 PIOS_BOARD_IO_Configure_UART(&pios_usart_cfg[0], PIOS_BOARD_IO_UART_COMBRIDGE);
255 #ifdef PIOS_INCLUDE_GCSRCVR
256 PIOS_BOARD_IO_Configure_GCS_RCVR();
257 #endif
259 #ifdef PIOS_INCLUDE_OPLINKRCVR
260 PIOS_BOARD_IO_Configure_OPLink_RCVR();
261 #endif
263 #ifdef PIOS_ENABLE_DEBUG_PINS
264 PIOS_DEBUG_Init(&pios_servo_cfg.channels, pios_servo_cfg.num_channels);
265 #else
266 if (boardHwSettings.UART3Port == HWTINYFISHSETTINGS_UART3PORT_OUTPUTS) {
267 PIOS_Servo_Init(&pios_servo_uart3_cfg);
268 } else {
269 PIOS_Servo_Init(&pios_servo_cfg);
271 #endif /* PIOS_ENABLE_DEBUG_PINS */
273 switch (boardHwSettings.LEDPort) {
274 case HWTINYFISHSETTINGS_LEDPORT_WS281X:
275 #if defined(PIOS_INCLUDE_WS2811)
276 PIOS_WS2811_Init(&pios_ws2811_id, &pios_ws2811_cfg);
277 #endif
278 break;
279 default:
280 break;
283 PIOS_BOARD_Sensors_Configure();
285 PIOS_LED_On(PIOS_LED_HEARTBEAT);
290 * @}