dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_board.c
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 /* Pull in the board-specific static HW definitions.
31  * Including .c files is a bit ugly but this allows all of
32  * the HW definitions to be const and static to limit their
33  * scope.
34  *
35  * NOTE: THIS IS THE ONLY PLACE THAT SHOULD EVER INCLUDE THIS FILE
36  */
37 
38 #include "board_hw_defs.c"
39 
40 #include <pios.h>
41 #include <pios_hal.h>
42 #include <openpilot.h>
43 #include <uavobjectsinit.h>
44 #include "hwsprf3e.h"
45 #include "hwshared.h"
46 #include "manualcontrolsettings.h"
47 #include "modulesettings.h"
48 
49 #if defined(PIOS_INCLUDE_WS2811)
50 #include "rgbledsettings.h"
51 #endif
52 
53 #if defined(PIOS_INCLUDE_DEBUG_CONSOLE)
54 #define PIOS_COM_DEBUGCONSOLE_TX_BUF_LEN 40
55 uintptr_t pios_com_debug_id;
56 #endif /* PIOS_INCLUDE_DEBUG_CONSOLE */
57 
58 uintptr_t pios_com_aux_id;
61 
68 #include <pios_board_info.h>
69 
70 void PIOS_Board_Init(void)
71 {
72  const struct pios_board_info *bdinfo = &pios_board_info_blob;
73 
74 #if defined(PIOS_INCLUDE_ANNUNC)
75  const struct pios_annunc_cfg *led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(bdinfo->board_rev);
76  PIOS_Assert(led_cfg);
77  PIOS_ANNUNC_Init(led_cfg);
78 #endif /* PIOS_INCLUDE_ANNUNC */
79 
80 #if defined(PIOS_INCLUDE_SPI)
81  pios_spi_t pios_spi_gyro_id;
82 
83  /* Set up the SPI interface to the gyro/acelerometer */
84  if (PIOS_SPI_Init(&pios_spi_gyro_id, &pios_spi_gyro_cfg)) {
86  }
87 #endif /* PIOS_INCLUDE_SPI */
88 
89 #if defined(PIOS_INCLUDE_FLASH)
90  /* Inititialize all flash drivers */
91  if (PIOS_Flash_Internal_Init(&pios_internal_flash_id, &flash_internal_cfg) != 0)
93 
94  /* Register the partition table */
95  const struct pios_flash_partition *flash_partition_table;
96  uint32_t num_partitions;
97  flash_partition_table = PIOS_BOARD_HW_DEFS_GetPartitionTable(bdinfo->board_rev, &num_partitions);
98  PIOS_FLASH_register_partition_table(flash_partition_table, num_partitions);
99 
100  /* Mount all filesystems */
101  if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_internal_settings_cfg, FLASH_PARTITION_LABEL_SETTINGS) != 0)
103 #endif /* PIOS_INCLUDE_FLASH */
104 
105  PIOS_RESET_Clear(); // Clear the RCC reset flags after use.
106 
107  /* Initialize the hardware UAVOs */
108  HwSprf3eInitialize();
109  ModuleSettingsInitialize();
110 
111 #if defined(PIOS_INCLUDE_RTC)
112  /* Initialize the real-time clock and its associated tick */
113  PIOS_RTC_Init(&pios_rtc_main_cfg);
114 #endif
115 
116  /* Initialize watchdog as early as possible to catch faults during init
117  * but do it only if there is no debugger connected
118  */
119  if ((CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == 0) {
120  PIOS_WDG_Init();
121  }
122 
123  /* Set up pulse timers */
124  //inputs
126 
127  //outputs
133 
134  /* IAP System Setup */
135  PIOS_IAP_Init();
136  uint16_t boot_count = PIOS_IAP_ReadBootCount();
137  if (boot_count < 3) {
138  PIOS_IAP_WriteBootCount(++boot_count);
139  AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT);
140  } else {
141  /* Too many failed boot attempts, force hw config to defaults */
142  HwSprf3eSetDefaults(HwSprf3eHandle(), 0);
143  ModuleSettingsSetDefaults(ModuleSettingsHandle(), 0);
144  AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL);
145  }
146 
147 #if defined(PIOS_INCLUDE_USB)
148  /* Initialize board specific USB data */
150 
151  /* Flags to determine if various USB interfaces are advertised */
152  bool usb_hid_present = false;
153 
154 #if defined(PIOS_INCLUDE_USB_CDC)
155  bool usb_cdc_present = false;
157  PIOS_Assert(0);
158  }
159  usb_hid_present = true;
160  usb_cdc_present = true;
161 #else
163  PIOS_Assert(0);
164  }
165  usb_hid_present = true;
166 #endif
167 
168  uintptr_t pios_usb_id;
169  PIOS_USB_Init(&pios_usb_id, PIOS_BOARD_HW_DEFS_GetUsbCfg(bdinfo->board_rev));
170 
171 #if defined(PIOS_INCLUDE_USB_CDC)
172 
173  uint8_t hw_usb_vcpport;
174  /* Configure the USB VCP port */
175  HwSprf3eUSB_VCPPortGet(&hw_usb_vcpport);
176 
177  if (!usb_cdc_present) {
178  /* Force VCP port function to disabled if we haven't advertised VCP in our USB descriptor */
179  hw_usb_vcpport = HWSPRF3E_USB_VCPPORT_DISABLED;
180  }
181 
182  PIOS_HAL_ConfigureCDC(hw_usb_vcpport, pios_usb_id, &pios_usb_cdc_cfg);
183 #endif /* PIOS_INCLUDE_USB_CDC */
184 
185 #if defined(PIOS_INCLUDE_USB_HID)
186  /* Configure the usb HID port */
187  uint8_t hw_usb_hidport;
188  HwSprf3eUSB_HIDPortGet(&hw_usb_hidport);
189 
190  if (!usb_hid_present) {
191  /* Force HID port function to disabled if we haven't advertised HID in our USB descriptor */
192  hw_usb_hidport = HWSPRF3E_USB_HIDPORT_DISABLED;
193  }
194 
195  PIOS_HAL_ConfigureHID(hw_usb_hidport, pios_usb_id, &pios_usb_hid_cfg);
196 
197 #endif /* PIOS_INCLUDE_USB_HID */
198 #endif /* PIOS_INCLUDE_USB */
199 
200 #if defined(PIOS_INCLUDE_I2C)
201  if (PIOS_I2C_Init(&pios_i2c_internal_id, &pios_i2c_internal_cfg))
203 
204  if (PIOS_I2C_CheckClear(pios_i2c_internal_id) != 0)
206  else
207  if (AlarmsGet(SYSTEMALARMS_ALARM_I2C) == SYSTEMALARMS_ALARM_UNINITIALISED)
208  AlarmsSet(SYSTEMALARMS_ALARM_I2C, SYSTEMALARMS_ALARM_OK);
209 #endif // PIOS_INCLUDE_I2C
210 
211  /* Configure the IO ports */
212  HwSprf3eDSMxModeOptions hw_DSMxMode;
213  HwSprf3eDSMxModeGet(&hw_DSMxMode);
214 
215  uint8_t hw_uart3;
216  HwSprf3eUart3Get(&hw_uart3);
217  PIOS_HAL_ConfigurePort(hw_uart3, // port_type
218  &pios_uart3_cfg, // usart_port_cfg
219  &pios_usart_com_driver, // com_driver
220  NULL, // i2c_id
221  NULL, // i2c_cfg
222  NULL, // ppm_cfg
223  NULL, // pwm_cfg
224  PIOS_LED_ALARM, // led_id
225  &pios_uart3_dsm_aux_cfg, // dsm_cfg
226  hw_DSMxMode, // dsm_mode
227  NULL); // sbus_cfg
228 
229  uint8_t hw_uart2;
230  HwSprf3eUart2Get(&hw_uart2);
231  PIOS_HAL_ConfigurePort(hw_uart2, // port_type
232  &pios_uart2_cfg, // usart_port_cfg
233  &pios_usart_com_driver, // com_driver
234  NULL, // i2c_id
235  NULL, // i2c_cfg
236  &pios_ppm_cfg, // ppm_cfg
237  NULL, // pwm_cfg
238  PIOS_LED_ALARM, // led_id
239  &pios_uart2_dsm_aux_cfg, // dsm_cfg
240  hw_DSMxMode, // dsm_mode
241  NULL); // sbus_cfg
242 
243  uint8_t hw_uart1;
244  HwSprf3eUart1Get(&hw_uart1);
245  PIOS_HAL_ConfigurePort(hw_uart1, // port_type
246  &pios_uart1_cfg, // usart_port_cfg
247  &pios_usart_com_driver, // com_driver
248  NULL, // i2c_id
249  NULL, // i2c_cfg
250  NULL, // ppm_cfg
251  NULL, // pwm_cfg
252  PIOS_LED_ALARM, // led_id
253  &pios_uart1_dsm_aux_cfg, // dsm_cfg
254  hw_DSMxMode, // dsm_mode
255  NULL); // sbus_cfg
256 
257 #ifdef PIOS_INCLUDE_WS2811
258  uint8_t num_leds;
259 
260  RGBLEDSettingsInitialize();
261 
262  RGBLEDSettingsNumLedsGet(&num_leds);
263 
266 #endif
267 
268 #ifndef PIOS_DEBUG_ENABLE_DEBUG_PINS
269 #ifdef PIOS_INCLUDE_SERVO
271 #endif
272 #else
273  PIOS_DEBUG_Init(&pios_tim_servo_all_channels, NELEMENTS(pios_tim_servo_all_channels));
274 #endif
275 
276 #if defined(PIOS_INCLUDE_ADC)
277  if (PIOS_INTERNAL_ADC_Init(&pios_internal_adc_id, &internal_adc_cfg) < 0)
278  PIOS_Assert(0);
279 
280  uintptr_t unused_adc;
281 
283 #endif /* PIOS_INCLUDE_ADC */
284 
285  PIOS_WDG_Clear();
286  PIOS_DELAY_WaitmS(200);
287  PIOS_WDG_Clear();
288 
289 #if defined(PIOS_INCLUDE_BMP280)
290 #include "pios_bmp280.h"
291 #include "pios_bmp280_priv.h"
292  uint8_t hw_baro;
293  HwSprf3eBarometerGet(&hw_baro);
294  switch(hw_baro) {
295  case HWSPRF3E_BAROMETER_BMP280:
296  if (PIOS_HAL_ConfigureExternalBaro(hw_baro,
297  &pios_i2c_internal_id, &pios_i2c_internal_cfg))
299  break;
300  }
301  PIOS_WDG_Clear();
302 #endif
303 
304 #if defined(PIOS_INCLUDE_MPU)
305  uint8_t hw_mag;
306  uint8_t hw_orientation;
307  HwSprf3eMagnetometerGet(&hw_mag);
308  HwSprf3eExtMagOrientationGet(&hw_orientation);
309 
310  // XXX TODO MIXING ENUMS HERE NOT SAFE/CORRECT
311  switch(hw_mag) {
312  case HWSPRF3E_MAGNETOMETER_INTERNAL:
313  pios_mpu_cfg.use_internal_mag=true;
314  break;
315  case HWSPRF3E_MAGNETOMETER_NONE:
316  pios_mpu_cfg.use_internal_mag=false;
317  break;
318  case HWSPRF3E_MAGNETOMETER_EXTERNALHMC5883:
319  pios_mpu_cfg.use_internal_mag=false;
320 #if defined(PIOS_INCLUDE_I2C)
321  PIOS_WDG_Clear();
322  if (PIOS_HAL_ConfigureExternalMag(hw_mag, hw_orientation,
323  &pios_i2c_internal_id, &pios_i2c_internal_cfg))
325  PIOS_WDG_Clear();
326 #else
328 #endif // PIOS_INCLUDE_I2C
329  break;
330  case HWSPRF3E_MAGNETOMETER_EXTERNALHMC5983:
331  pios_mpu_cfg.use_internal_mag=false;
332 #ifdef PIOS_INCLUDE_HMC5983_I2C
333  PIOS_WDG_Clear();
334  if (PIOS_HAL_ConfigureExternalMag(hw_mag, hw_orientation,
335  &pios_i2c_internal_id, &pios_i2c_internal_cfg))
337  PIOS_WDG_Clear();
338 #else
340 #endif /* PIOS_INCLUDE_HMC5983_I2C */
341  break;
342  }
343 
344  pios_mpu_dev_t mpu_dev = NULL;
345  if (PIOS_MPU_SPI_Init(&mpu_dev, pios_spi_gyro_id, 0, &pios_mpu_cfg) != 0)
347 
348  HwSprf3eGyroRangeOptions hw_gyro_range;
349  HwSprf3eGyroRangeGet(&hw_gyro_range);
350 
351  switch(hw_gyro_range) {
352  case HWSPRF3E_GYRORANGE_250:
354  break;
355  case HWSPRF3E_GYRORANGE_500:
357  break;
358  case HWSPRF3E_GYRORANGE_1000:
360  break;
361  case HWSPRF3E_GYRORANGE_2000:
363  break;
364  }
365 
366  HwSprf3eAccelRangeOptions hw_accel_range;
367  HwSprf3eAccelRangeGet(&hw_accel_range);
368  switch(hw_accel_range) {
369  case HWSPRF3E_ACCELRANGE_2G:
371  break;
372  case HWSPRF3E_ACCELRANGE_4G:
374  break;
375  case HWSPRF3E_ACCELRANGE_8G:
377  break;
378  case HWSPRF3E_ACCELRANGE_16G:
380  break;
381  }
382 
383  HwSprf3eMPU9250GyroLPFOptions hw_mpu_dlpf;
384  HwSprf3eMPU9250GyroLPFGet(&hw_mpu_dlpf);
385  uint16_t bandwidth = \
386  (hw_mpu_dlpf == HWSPRF3E_MPU9250GYROLPF_184) ? 184 : \
387  (hw_mpu_dlpf == HWSPRF3E_MPU9250GYROLPF_92) ? 92 : \
388  (hw_mpu_dlpf == HWSPRF3E_MPU9250GYROLPF_41) ? 41 : \
389  (hw_mpu_dlpf == HWSPRF3E_MPU9250GYROLPF_20) ? 20 : \
390  (hw_mpu_dlpf == HWSPRF3E_MPU9250GYROLPF_10) ? 10 : \
391  (hw_mpu_dlpf == HWSPRF3E_MPU9250GYROLPF_5) ? 5 : \
392  188;
393  PIOS_MPU_SetGyroBandwidth(bandwidth);
394 #endif
395 
396  //I2C is slow, sensor init as well, reset watchdog to prevent reset here
397  PIOS_WDG_Clear();
398 
399 #if defined(PIOS_INCLUDE_GPIO)
400  PIOS_GPIO_Init();
401 #endif
402 
403  /* Make sure we have at least one telemetry link configured or else fail initialization */
405 }
406 
int32_t PIOS_Flash_Internal_Init(uintptr_t *flash_id, const struct pios_flash_internal_cfg *cfg)
int32_t PIOS_USB_DESC_HID_CDC_Init(void)
void PIOS_HAL_ConfigureCDC(HwSharedUSB_VCPPortOptions port_type, uintptr_t usb_id, const struct pios_usb_cdc_cfg *cdc_cfg)
Main PiOS header to include all the compiled in PiOS options.
int32_t PIOS_INTERNAL_ADC_Init(uintptr_t *internal_adc_id, const struct pios_internal_adc_cfg *cfg)
int32_t PIOS_I2C_CheckClear(pios_i2c_t i2c_id)
int PIOS_HAL_ConfigureExternalBaro(HwSharedExtBaroOptions baro, pios_i2c_t *i2c_id, const struct pios_i2c_adapter_cfg *i2c_cfg)
Definition: pios_hal.c:1242
#define NELEMENTS(x)
Definition: pios.h:192
uintptr_t pios_com_debug_id
Definition: pios_board.c:76
#define PIOS_DEBUG_Assert(test)
Definition: pios_debug.h:51
int PIOS_HAL_ConfigureExternalMag(HwSharedMagOptions mag, HwSharedMagOrientationOptions orientation, pios_i2c_t *i2c_id, const struct pios_i2c_adapter_cfg *i2c_cfg)
Definition: pios_hal.c:1298
int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity)
Definition: alarms.c:97
uintptr_t pios_uavo_settings_fs_id
Definition: pios_board.c:50
uint16_t PIOS_IAP_ReadBootCount(void)
Definition: pios_iap.c:93
int32_t PIOS_FLASHFS_Logfs_Init(uintptr_t *fs_id, const struct flashfs_logfs_cfg *cfg, enum pios_flash_partition_labels partition_label)
Initialize the flash object setting FS.
static const struct pios_tim_clock_cfg tim_8_cfg
const struct pios_board_info pios_board_info_blob
struct pios_mpu_dev * pios_mpu_dev_t
Definition: pios_mpu.h:150
int PIOS_WS2811_init(ws2811_dev_t *dev_out, const struct pios_ws2811_cfg *cfg, int max_leds)
Allocate and initialise WS2811 device.
Definition: pios_ws2811.c:64
uintptr_t pios_com_aux_id
Definition: pios_board.c:58
#define PIOS_LED_ALARM
Definition: pios_board.h:86
int32_t PIOS_I2C_Init(pios_i2c_t *i2c_id, const char *path)
void PIOS_WDG_Clear(void)
Clear the watchdog timer.
Definition: pios_wdg.c:147
int32_t PIOS_MPU_SPI_Init(pios_mpu_dev_t *dev, pios_spi_t spi_id, uint32_t slave_num, const struct pios_mpu_cfg *cfg)
Initialize the MPU-xxxx 6/9-axis sensor on SPI.
void PIOS_DEBUG_Init(const struct pios_tim_channel *channels, uint8_t num_channels)
Definition: pios_debug.c:44
uintptr_t pios_com_telem_serial_id
Definition: pios_hal.c:127
const struct pios_com_driver pios_usart_com_driver
void PIOS_IAP_Init(void)
PIOS_IAP_Init - performs required initializations for iap module.
Definition: pios_iap.c:44
int32_t PIOS_MPU_SetAccelRange(enum pios_mpu_accel_range range)
const struct pios_adc_driver pios_internal_adc_driver
int32_t PIOS_TIM_InitClock(const struct pios_tim_clock_cfg *cfg)
Definition: pios_tim.c:62
uintptr_t pios_internal_adc_id
Definition: pios_board.c:51
ws2811_dev_t pios_ws2811
Definition: pios_board.c:66
static const struct pios_tim_clock_cfg tim_15_cfg
int32_t PIOS_SPI_Init(pios_spi_t *spi_dev, const struct pios_spi_cfg *cfg)
void PIOS_Board_Init()
Definition: pios_board.c:44
int32_t PIOS_ANNUNC_Init(const struct pios_annunc_cfg *cfg)
void PIOS_RTC_Init(const struct pios_rtc_cfg *cfg)
int32_t PIOS_USB_DESC_HID_ONLY_Init(void)
void PIOS_HAL_ConfigureHID(HwSharedUSB_HIDPortOptions port_type, uintptr_t usb_id, const struct pios_usb_hid_cfg *hid_cfg)
int32_t PIOS_Servo_Init(const struct pios_servo_cfg *cfg)
Definition: pios_servo.c:109
void PIOS_MPU_SetGyroBandwidth(uint16_t bandwidth)
Sets the bandwidth desired from the gyro. The driver will automatically select the lowest bandwidth l...
uintptr_t pios_com_openlog_logging_id
Definition: pios_board.c:49
uintptr_t pios_com_telem_usb_id
Definition: pios_board.c:42
void PIOS_FLASH_register_partition_table(const struct pios_flash_partition partition_table[], uint8_t num_partitions)
Includes PiOS and core architecture components.
int32_t AlarmsClear(SystemAlarmsAlarmElem alarm)
Definition: alarms.c:171
void PIOS_WS2811_trigger_update(ws2811_dev_t dev)
Trigger an update of the LED strand.
Definition: pios_ws2811.c:197
int32_t PIOS_USB_Init(uintptr_t *usb_id, const struct pios_usb_cfg *cfg)
uint16_t PIOS_WDG_Init()
Initialize the watchdog timer for a specified timeout.
Definition: pios_wdg.c:63
int32_t PIOS_ADC_Init(uintptr_t *adc_id, const struct pios_adc_driver *driver, uintptr_t lower_id)
void PIOS_IAP_WriteBootCount(uint16_t)
Definition: pios_iap.c:98
static const struct pios_tim_clock_cfg tim_17_cfg
static const struct pios_tim_clock_cfg tim_3_cfg
static const struct pios_tim_clock_cfg tim_16_cfg
void PIOS_HAL_CriticalError(uint32_t led_id, enum pios_hal_panic code)
Flash a blink code.
Definition: pios_hal.c:291
static const struct pios_tim_clock_cfg tim_2_cfg
#define PIOS_Assert(test)
Definition: pios_debug.h:52
int32_t PIOS_DELAY_WaitmS(uint32_t mS)
Definition: pios_delay.c:140
int32_t PIOS_MPU_SetGyroRange(enum pios_mpu_gyro_range range)
int32_t PIOS_USB_BOARD_DATA_Init(void)
SystemAlarmsAlarmOptions AlarmsGet(SystemAlarmsAlarmElem alarm)
Definition: alarms.c:129
void PIOS_RESET_Clear(void)
PIOS_RESET_Clear Does nothing on POSIX systems.
Definition: pios_reset.c:38
void PIOS_SENSORS_SetMissing(enum pios_sensor_type type)
Assert that an optional (non-accel/gyro), but expected sensor is missing.
Definition: pios_sensors.c:191