Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / targets / boards / nucleof303re / firmware / pios_board.c
blob64eae9c537a7f4f54a456b4acd80b44fa6a947c5
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 NucleoF303RE 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 <hwspracingf3evosettings.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>
41 #ifdef PIOS_INCLUDE_INSTRUMENTATION
42 #include <pios_instrumentation.h>
43 #endif
45 #include <pios_board_io.h>
46 #include <pios_board_sensors.h>
49 * Pull in the board-specific static HW definitions.
50 * Including .c files is a bit ugly but this allows all of
51 * the HW definitions to be const and static to limit their
52 * scope.
54 * NOTE: THIS IS THE ONLY PLACE THAT SHOULD EVER INCLUDE THIS FILE
56 #include "../board_hw_defs.c"
58 uintptr_t pios_uavo_settings_fs_id;
59 uintptr_t pios_user_fs_id = 0;
61 #ifdef PIOS_INCLUDE_WS2811
62 uint32_t pios_ws2811_id;
63 #endif
66 static HwSPRacingF3EVOSettingsData boardHwSettings;
68 static void hwSPRacingF3EVOSettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
70 HwSPRacingF3EVOSettingsData currentBoardHwSettings;
72 HwSPRacingF3EVOSettingsGet(&currentBoardHwSettings);
74 if (memcmp(&currentBoardHwSettings, &boardHwSettings, sizeof(HwSPRacingF3EVOSettingsData)) != 0) {
75 ExtendedAlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL, SYSTEMALARMS_EXTENDEDALARMSTATUS_REBOOTREQUIRED, 0);
79 /**
80 * PIOS_Board_Init()
81 * initializes all the core subsystems on this specific hardware
82 * called from System/openpilot.c
85 void PIOS_Board_Init(void)
87 #if defined(PIOS_INCLUDE_LED)
88 PIOS_LED_Init(&pios_led_cfg);
89 #endif /* PIOS_INCLUDE_LED */
91 #ifdef PIOS_INCLUDE_INSTRUMENTATION
92 PIOS_Instrumentation_Init(PIOS_INSTRUMENTATION_MAX_COUNTERS);
93 #endif
95 #if defined(PIOS_INCLUDE_SPI)
96 /* Set up the SPI interface to the mpu9250 */
98 if (PIOS_SPI_Init(&pios_spi_mpu9250_id, &pios_spi_mpu9250_cfg)) {
99 PIOS_DEBUG_Assert(0);
102 /* Set up the SPI interface to the sdcard */
103 if (PIOS_SPI_Init(&pios_spi_sdcard_id, &pios_spi_sdcard_cfg)) {
104 PIOS_DEBUG_Assert(0);
106 #endif
108 #if defined(PIOS_INCLUDE_FLASH)
109 /* Connect flash to the appropriate interface and configure it */
110 uintptr_t flash_id;
112 // initialize the internal settings storage flash
113 if (PIOS_Flash_Internal_Init(&flash_id, &flash_internal_system_cfg)) {
114 PIOS_DEBUG_Assert(0);
117 if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_internal_cfg, &pios_internal_flash_driver, flash_id)) {
118 PIOS_DEBUG_Assert(0);
121 // if (PIOS_FLASHFS_Logfs_Init(&pios_user_fs_id, &flashfs_internal_user_cfg, &pios_internal_flash_driver, flash_id)) {
122 // PIOS_DEBUG_Assert(0);
123 // }
125 #endif /* if defined(PIOS_INCLUDE_FLASH) */
127 /* Initialize the task monitor */
128 if (PIOS_TASK_MONITOR_Initialize(TASKINFO_RUNNING_NUMELEM)) {
129 PIOS_Assert(0);
132 /* Initialize the delayed callback library */
133 PIOS_CALLBACKSCHEDULER_Initialize();
135 /* Initialize UAVObject libraries */
136 EventDispatcherInitialize();
137 UAVObjInitialize();
138 SETTINGS_INITIALISE_ALL;
140 #if defined(PIOS_INCLUDE_RTC)
141 /* Initialize the real-time clock and its associated tick */
142 PIOS_RTC_Init(&pios_rtc_main_cfg);
143 #endif
144 PIOS_IAP_Init();
145 // check for safe mode commands from gcs
146 if (PIOS_IAP_ReadBootCmd(0) == PIOS_IAP_CLEAR_FLASH_CMD_0 &&
147 PIOS_IAP_ReadBootCmd(1) == PIOS_IAP_CLEAR_FLASH_CMD_1 &&
148 PIOS_IAP_ReadBootCmd(2) == PIOS_IAP_CLEAR_FLASH_CMD_2) {
149 PIOS_FLASHFS_Format(pios_uavo_settings_fs_id);
150 PIOS_IAP_WriteBootCmd(0, 0);
151 PIOS_IAP_WriteBootCmd(1, 0);
152 PIOS_IAP_WriteBootCmd(2, 0);
155 #ifndef ERASE_FLASH
156 #ifdef PIOS_INCLUDE_WDG
157 /* Initialize watchdog as early as possible to catch faults during init */
158 PIOS_WDG_Init();
159 #endif
160 #endif
162 /* Initialize the alarms library */
163 AlarmsInitialize();
165 /* Check for repeated boot failures */
166 uint16_t boot_count = PIOS_IAP_ReadBootCount();
167 if (boot_count < 3) {
168 PIOS_IAP_WriteBootCount(++boot_count);
169 AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT);
170 } else {
171 /* Too many failed boot attempts, force hwsettings to defaults */
172 HwSettingsSetDefaults(HwSettingsHandle(), 0);
173 HwSPRacingF3EVOSettingsSetDefaults(HwSPRacingF3EVOSettingsHandle(), 0);
175 AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL);
179 /* Set up pulse timers: 2, 3, 8, 15 */
180 // PIOS_TIM_InitClock(&tim_1_cfg);
181 PIOS_TIM_InitClock(&tim_2_cfg);
182 PIOS_TIM_InitClock(&tim_3_cfg);
183 // PIOS_TIM_InitClock(&tim_4_cfg);
184 PIOS_TIM_InitClock(&tim_8_cfg);
185 PIOS_TIM_InitClock(&tim_15_cfg);
186 // PIOS_TIM_InitClock(&tim_16_cfg);
187 // PIOS_TIM_InitClock(&tim_17_cfg);
190 #if defined(PIOS_INCLUDE_USB)
191 PIOS_BOARD_IO_Configure_USB();
192 #endif
195 HwSPRacingF3EVOSettingsConnectCallback(hwSPRacingF3EVOSettingsUpdatedCb);
197 HwSPRacingF3EVOSettingsGet(&boardHwSettings);
199 static const PIOS_BOARD_IO_UART_Function uart_function_map[] = {
200 [HWSPRACINGF3EVOSETTINGS_UARTPORT_TELEMETRY] = PIOS_BOARD_IO_UART_TELEMETRY,
201 [HWSPRACINGF3EVOSETTINGS_UARTPORT_GPS] = PIOS_BOARD_IO_UART_GPS,
202 [HWSPRACINGF3EVOSETTINGS_UARTPORT_SBUS] = PIOS_BOARD_IO_UART_SBUS,
203 [HWSPRACINGF3EVOSETTINGS_UARTPORT_DSM] = PIOS_BOARD_IO_UART_DSM_MAIN, // single DSM instance? ok.
204 [HWSPRACINGF3EVOSETTINGS_UARTPORT_EXBUS] = PIOS_BOARD_IO_UART_EXBUS,
205 [HWSPRACINGF3EVOSETTINGS_UARTPORT_HOTTSUMD] = PIOS_BOARD_IO_UART_HOTT_SUMD,
206 [HWSPRACINGF3EVOSETTINGS_UARTPORT_HOTTSUMH] = PIOS_BOARD_IO_UART_HOTT_SUMH,
207 [HWSPRACINGF3EVOSETTINGS_UARTPORT_SRXL] = PIOS_BOARD_IO_UART_SRXL,
208 [HWSPRACINGF3EVOSETTINGS_UARTPORT_IBUS] = PIOS_BOARD_IO_UART_IBUS,
209 [HWSPRACINGF3EVOSETTINGS_UARTPORT_DEBUGCONSOLE] = PIOS_BOARD_IO_UART_DEBUGCONSOLE,
210 [HWSPRACINGF3EVOSETTINGS_UARTPORT_COMBRIDGE] = PIOS_BOARD_IO_UART_COMBRIDGE,
211 [HWSPRACINGF3EVOSETTINGS_UARTPORT_MSP] = PIOS_BOARD_IO_UART_MSP,
212 [HWSPRACINGF3EVOSETTINGS_UARTPORT_MAVLINK] = PIOS_BOARD_IO_UART_MAVLINK,
215 for (unsigned int i = 0; i < HWSPRACINGF3EVOSETTINGS_UARTPORT_NUMELEM; ++i) {
216 if (boardHwSettings.UARTPort[i] < NELEMENTS(uart_function_map)) {
217 PIOS_BOARD_IO_Configure_UART(&pios_usart_cfg[i], uart_function_map[boardHwSettings.UARTPort[i]]);
221 #ifndef PIOS_ENABLE_DEBUG_PINS
222 PIOS_Servo_Init(&pios_servo_cfg);
223 #else
224 PIOS_DEBUG_Init(&pios_servo_cfg.channels, pios_servo_cfg.num_channels);
225 #endif
227 switch (boardHwSettings.LEDPort) {
228 case HWSPRACINGF3EVOSETTINGS_LEDPORT_WS281X:
229 #if defined(PIOS_INCLUDE_WS2811)
230 PIOS_WS2811_Init(&pios_ws2811_id, &pios_ws2811_cfg);
231 #endif
232 break;
233 default:
234 break;
237 #if defined(PIOS_INCLUDE_I2C)
238 // init I2C1 for use with the internal baro
239 if (PIOS_I2C_Init(&pios_i2c_id, &pios_i2c_cfg)) {
240 PIOS_DEBUG_Assert(0);
242 PIOS_DELAY_WaitmS(50);
243 #endif
245 PIOS_BOARD_Sensors_Configure();
247 PIOS_LED_On(PIOS_LED_HEARTBEAT);
249 /* Make sure we have at least one telemetry link configured or else fail initialization */
250 PIOS_Assert(pios_com_telem_rf_id || pios_com_telem_usb_id);
255 * @}