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 "hwsparky.h"
45 #include "manualcontrolsettings.h"
46 #include "modulesettings.h"
47 
48 
49 #define PIOS_COM_CAN_RX_BUF_LEN 256
50 #define PIOS_COM_CAN_TX_BUF_LEN 256
51 
52 uintptr_t pios_com_aux_id;
53 uintptr_t pios_com_can_id;
55 uintptr_t pios_can_id;
57 
64 #include <pios_board_info.h>
65 
66 void PIOS_Board_Init(void)
67 {
68  const struct pios_board_info *bdinfo = &pios_board_info_blob;
69 
70 #if defined(PIOS_INCLUDE_ANNUNC)
71  const struct pios_annunc_cfg *led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(bdinfo->board_rev);
72  PIOS_Assert(led_cfg);
73  PIOS_ANNUNC_Init(led_cfg);
74 #endif /* PIOS_INCLUDE_ANNUNC */
75 
76 #if defined(PIOS_INCLUDE_CAN)
79 
84 
86 #endif
87 
88 #if defined(PIOS_INCLUDE_FLASH)
89  /* Inititialize all flash drivers */
90  if (PIOS_Flash_Internal_Init(&pios_internal_flash_id, &flash_internal_cfg) != 0)
92 
93  /* Register the partition table */
94  const struct pios_flash_partition *flash_partition_table;
95  uint32_t num_partitions;
96  flash_partition_table = PIOS_BOARD_HW_DEFS_GetPartitionTable(bdinfo->board_rev, &num_partitions);
97  PIOS_FLASH_register_partition_table(flash_partition_table, num_partitions);
98 
99  /* Mount all filesystems */
100  if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_internal_settings_cfg, FLASH_PARTITION_LABEL_SETTINGS) != 0)
102 #if defined(ERASE_FLASH)
104 #endif
105 
106 #endif /* PIOS_INCLUDE_FLASH */
107 
108  PIOS_RESET_Clear(); // Clear the RCC reset flags after use.
109 
110  /* Initialize the hardware UAVOs */
111  HwSparkyInitialize();
112  ModuleSettingsInitialize();
113 
114 #if defined(PIOS_INCLUDE_RTC)
115  /* Initialize the real-time clock and its associated tick */
116  PIOS_RTC_Init(&pios_rtc_main_cfg);
117 #endif
118 
119  /* Initialize watchdog as early as possible to catch faults during init
120  * but do it only if there is no debugger connected
121  */
122  if ((CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == 0) {
123  PIOS_WDG_Init();
124  }
125 
126  /* Set up pulse timers */
127  //inputs
128 
129  //outputs
136 
137  /* IAP System Setup */
138  PIOS_IAP_Init();
139  uint16_t boot_count = PIOS_IAP_ReadBootCount();
140  if (boot_count < 3) {
141  PIOS_IAP_WriteBootCount(++boot_count);
142  AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT);
143  } else {
144  /* Too many failed boot attempts, force hw config to defaults */
145  HwSparkySetDefaults(HwSparkyHandle(), 0);
146  ModuleSettingsSetDefaults(ModuleSettingsHandle(), 0);
147  AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL);
148  }
149 
150 #if defined(PIOS_INCLUDE_USB)
151  /* Initialize board specific USB data */
153 
154  /* Flags to determine if various USB interfaces are advertised */
155  bool usb_hid_present = false;
156 
157 #if defined(PIOS_INCLUDE_USB_CDC)
158  bool usb_cdc_present = false;
160  PIOS_Assert(0);
161  }
162  usb_hid_present = true;
163  usb_cdc_present = true;
164 #else
166  PIOS_Assert(0);
167  }
168  usb_hid_present = true;
169 #endif
170 
171  uintptr_t pios_usb_id;
172  PIOS_USB_Init(&pios_usb_id, PIOS_BOARD_HW_DEFS_GetUsbCfg(bdinfo->board_rev));
173 
174 #if defined(PIOS_INCLUDE_USB_CDC)
175 
176  uint8_t hw_usb_vcpport;
177  /* Configure the USB VCP port */
178  HwSparkyUSB_VCPPortGet(&hw_usb_vcpport);
179 
180  if (!usb_cdc_present) {
181  /* Force VCP port function to disabled if we haven't advertised VCP in our USB descriptor */
182  hw_usb_vcpport = HWSPARKY_USB_VCPPORT_DISABLED;
183  }
184 
185  PIOS_HAL_ConfigureCDC(hw_usb_vcpport, pios_usb_id, &pios_usb_cdc_cfg);
186 #endif /* PIOS_INCLUDE_USB_CDC */
187 
188 #if defined(PIOS_INCLUDE_USB_HID)
189  /* Configure the usb HID port */
190  uint8_t hw_usb_hidport;
191  HwSparkyUSB_HIDPortGet(&hw_usb_hidport);
192 
193  if (!usb_hid_present) {
194  /* Force HID port function to disabled if we haven't advertised HID in our USB descriptor */
195  hw_usb_hidport = HWSPARKY_USB_HIDPORT_DISABLED;
196  }
197 
198  PIOS_HAL_ConfigureHID(hw_usb_hidport, pios_usb_id, &pios_usb_hid_cfg);
199 
200 #endif /* PIOS_INCLUDE_USB_HID */
201 #endif /* PIOS_INCLUDE_USB */
202 
203  /* Configure the IO ports */
204 
205 #if defined(PIOS_INCLUDE_I2C)
206  if (PIOS_I2C_Init(&pios_i2c_internal_id, &pios_i2c_internal_cfg))
208 
209  if (PIOS_I2C_CheckClear(pios_i2c_internal_id) != 0)
211  else
212  if (AlarmsGet(SYSTEMALARMS_ALARM_I2C) == SYSTEMALARMS_ALARM_UNINITIALISED)
213  AlarmsSet(SYSTEMALARMS_ALARM_I2C, SYSTEMALARMS_ALARM_OK);
214 #endif // PIOS_INCLUDE_I2C
215 
216  HwSparkyDSMxModeOptions hw_DSMxMode;
217  HwSparkyDSMxModeGet(&hw_DSMxMode);
218 
219  /* Configure main USART port */
220  uint8_t hw_mainport;
221  HwSparkyMainPortGet(&hw_mainport);
222 
223  PIOS_HAL_ConfigurePort(hw_mainport, // port type protocol
224  &pios_main_usart_cfg, // usart_port_cfg
225  &pios_usart_com_driver, // com_driver
226  NULL, // i2c_id
227  NULL, // i2c_cfg
228  NULL, // ppm_cfg
229  NULL, // pwm_cfg
230  PIOS_LED_ALARM, // led_id
231  &pios_main_dsm_aux_cfg, // dsm_cfg
232  hw_DSMxMode, // dsm_mode
233  NULL); // sbus_cfg
234 
235  /* Configure FlexiPort */
236  uint8_t hw_flexiport;
237  HwSparkyFlexiPortGet(&hw_flexiport);
238 
239  PIOS_HAL_ConfigurePort(hw_flexiport, // port type protocol
240  &pios_flexi_usart_cfg, // usart_port_cfg
241  &pios_usart_com_driver, // com_driver
242  &pios_i2c_flexi_id, // i2c_id
243  &pios_i2c_flexi_cfg, // i2c_cfg
244  NULL, // ppm_cfg
245  NULL, // pwm_cfg
246  PIOS_LED_ALARM, // led_id
247  &pios_flexi_dsm_aux_cfg, // dsm_cfg
248  hw_DSMxMode, // dsm_mode
249  NULL); // sbus_cfg
250 
251  /* Configure the rcvr port */
252  uint8_t hw_rcvrport;
253  HwSparkyRcvrPortGet(&hw_rcvrport);
254 
255  PIOS_HAL_ConfigurePort(hw_rcvrport, // port type protocol
256  &pios_rcvr_usart_cfg, // usart_port_cfg
257  &pios_usart_com_driver, // com_driver
258  NULL, // i2c_id
259  NULL, // i2c_cfg
260  &pios_ppm_cfg, // ppm_cfg
261  NULL, // pwm_cfg
262  PIOS_LED_ALARM, // led_id
263  &pios_rcvr_dsm_aux_cfg, // dsm_cfg
264  hw_DSMxMode, // dsm_mode
265  NULL); // sbus_cfg
266 
267  uint8_t hw_outport;
268  uint8_t number_of_pwm_outputs;
269  uint8_t number_of_adc_ports;
270  bool use_pwm_in;
271  HwSparkyOutPortGet(&hw_outport);
272  switch (hw_outport) {
273  case HWSPARKY_OUTPORT_PWM10:
274  number_of_pwm_outputs = 10;
275  number_of_adc_ports = 0;
276  use_pwm_in = false;
277  break;
278  case HWSPARKY_OUTPORT_PWM82ADC:
279  number_of_pwm_outputs = 8;
280  number_of_adc_ports = 2;
281  use_pwm_in = false;
282  break;
283  case HWSPARKY_OUTPORT_PWM73ADC:
284  number_of_pwm_outputs = 7;
285  number_of_adc_ports = 3;
286  use_pwm_in = false;
287  break;
288  case HWSPARKY_OUTPORT_PWM9PWM_IN:
289  number_of_pwm_outputs = 9;
290  use_pwm_in = true;
291  number_of_adc_ports = 0;
292  break;
293  case HWSPARKY_OUTPORT_PWM72ADCPWM_IN:
294  number_of_pwm_outputs = 7;
295  use_pwm_in = true;
296  number_of_adc_ports = 2;
297  break;
298  default:
299  PIOS_Assert(0);
300  break;
301  }
302 
303 #ifndef PIOS_DEBUG_ENABLE_DEBUG_PINS
304 #ifdef PIOS_INCLUDE_SERVO
305  pios_servo_cfg.num_channels = number_of_pwm_outputs;
306 
307  if (hw_rcvrport != HWSHARED_PORTTYPES_PPM) {
309  } else {
310  PIOS_Servo_Init(&pios_servo_slow_cfg);
311  }
312 #endif
313 #else
314  PIOS_DEBUG_Init(&pios_tim_servo_all_channels, NELEMENTS(pios_tim_servo_all_channels));
315 #endif
316 
317 #if defined(PIOS_INCLUDE_ADC)
318  if (number_of_adc_ports > 0) {
319  internal_adc_cfg.adc_pin_count = number_of_adc_ports;
320  uintptr_t unused_adc;
321  if (PIOS_INTERNAL_ADC_Init(&pios_internal_adc_id, &internal_adc_cfg) < 0)
322  PIOS_Assert(0);
324  }
325 #endif /* PIOS_INCLUDE_ADC */
326 
327 #if defined(PIOS_INCLUDE_PWM)
328  if (use_pwm_in) {
329  if (number_of_adc_ports > 0)
330  pios_pwm_cfg.channels = &pios_tim_rcvrport_pwm[1];
331  uintptr_t pios_pwm_id;
332  PIOS_PWM_Init(&pios_pwm_id, &pios_pwm_cfg);
333 
334  uintptr_t pios_pwm_rcvr_id;
335  if (PIOS_RCVR_Init(&pios_pwm_rcvr_id, &pios_pwm_rcvr_driver, pios_pwm_id)) {
336  PIOS_Assert(0);
337  }
338  pios_rcvr_group_map[MANUALCONTROLSETTINGS_CHANNELGROUPS_PWM] = pios_pwm_rcvr_id;
339  }
340 #endif /* PIOS_INCLUDE_PWM */
341  PIOS_WDG_Clear();
342  PIOS_DELAY_WaitmS(200);
343  PIOS_WDG_Clear();
344 
345  uint8_t hw_magnetometer;
346  HwSparkyMagnetometerGet(&hw_magnetometer);
347 
348 #if defined(PIOS_INCLUDE_MPU)
349  if (hw_magnetometer != HWSPARKY_MAGNETOMETER_INTERNAL)
350  pios_mpu_cfg.use_internal_mag = false;
351 
352  pios_mpu_dev_t mpu_dev = NULL;
353 
354  if (PIOS_MPU_I2C_Init(&mpu_dev, pios_i2c_internal_id, &pios_mpu_cfg) != 0)
356 
357  HwSparkyGyroRangeOptions hw_gyro_range;
358  HwSparkyGyroRangeGet(&hw_gyro_range);
359  switch(hw_gyro_range) {
360  case HWSPARKY_GYRORANGE_250:
362  break;
363  case HWSPARKY_GYRORANGE_500:
365  break;
366  case HWSPARKY_GYRORANGE_1000:
368  break;
369  case HWSPARKY_GYRORANGE_2000:
371  break;
372  }
373 
374  HwSparkyAccelRangeOptions hw_accel_range;
375  HwSparkyAccelRangeGet(&hw_accel_range);
376  switch(hw_accel_range) {
377  case HWSPARKY_ACCELRANGE_2G:
379  break;
380  case HWSPARKY_ACCELRANGE_4G:
382  break;
383  case HWSPARKY_ACCELRANGE_8G:
385  break;
386  case HWSPARKY_ACCELRANGE_16G:
388  break;
389  }
390 
391  uint8_t hw_mpu_dlpf;
392  HwSparkyMPU9150DLPFGet(&hw_mpu_dlpf);
393 
394  uint16_t bandwidth = \
395  (hw_mpu_dlpf == HWSPARKY_MPU9150DLPF_188) ? 188 : \
396  (hw_mpu_dlpf == HWSPARKY_MPU9150DLPF_98) ? 98 : \
397  (hw_mpu_dlpf == HWSPARKY_MPU9150DLPF_42) ? 42 : \
398  (hw_mpu_dlpf == HWSPARKY_MPU9150DLPF_20) ? 20 : \
399  (hw_mpu_dlpf == HWSPARKY_MPU9150DLPF_10) ? 10 : \
400  (hw_mpu_dlpf == HWSPARKY_MPU9150DLPF_5) ? 5 : \
401  188;
402  PIOS_MPU_SetGyroBandwidth(bandwidth);
403 #endif /* PIOS_INCLUDE_MPU */
404 
405  /* I2C is slow, sensor init as well, reset watchdog to prevent reset here */
406  PIOS_WDG_Clear();
407 
408  switch (hw_magnetometer) {
409  case HWSPARKY_MAGNETOMETER_NONE:
410  case HWSPARKY_MAGNETOMETER_INTERNAL:
411  /* internal handled by MPU code above */
412  break;
413 
414  /* default external mags and handle them in PiOS HAL rather than maintaining list here */
415  default:
416  if (hw_flexiport == HWSHARED_PORTTYPES_I2C) {
417  uint8_t hw_orientation;
418  HwSparkyExtMagOrientationGet(&hw_orientation);
419 
420  PIOS_HAL_ConfigureExternalMag(hw_magnetometer, hw_orientation,
421  &pios_i2c_flexi_id, &pios_i2c_flexi_cfg);
422  } else {
424  }
425  break;
426  }
427 
428  /* I2C is slow, sensor init as well, reset watchdog to prevent reset here */
429  PIOS_WDG_Clear();
430 
431 #if defined(PIOS_INCLUDE_MS5611)
432  if ((PIOS_MS5611_Init(&pios_ms5611_cfg, pios_i2c_internal_id) != 0)
433  || (PIOS_MS5611_Test() != 0))
435 #endif
436 
437  /* Make sure we have at least one telemetry link configured or else fail initialization */
439 }
440 
int32_t PIOS_MPU_I2C_Init(pios_mpu_dev_t *dev, pios_i2c_t i2c_id, const struct pios_mpu_cfg *cfg)
Initialize the MPU-xxxx 6/9-axis sensor on I2C.
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)
int32_t PIOS_PWM_Init(uintptr_t *pwm_id, const struct pios_pwm_cfg *cfg)
const struct pios_rcvr_driver pios_pwm_rcvr_driver
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)
uintptr_t pios_rcvr_group_map[]
Definition: pios_hal.c:73
int32_t PIOS_I2C_CheckClear(pios_i2c_t i2c_id)
#define NELEMENTS(x)
Definition: pios.h:192
#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.
const struct pios_board_info pios_board_info_blob
struct pios_mpu_dev * pios_mpu_dev_t
Definition: pios_mpu.h:150
uintptr_t pios_com_aux_id
Definition: pios_board.c:58
uintptr_t pios_can_id
Definition: pios_board.c:61
#define PIOS_LED_ALARM
Definition: pios_board.h:86
Configuration structure for the MS5611 driver.
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
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
int32_t PIOS_RCVR_Init(uintptr_t *rcvr_id, const struct pios_rcvr_driver *driver, const uintptr_t lower_id)
uintptr_t pios_internal_adc_id
Definition: pios_board.c:51
static const struct pios_tim_clock_cfg tim_15_cfg
const struct pios_tim_channel * channels
Definition: pios_pwm_priv.h:40
void PIOS_Board_Init()
Definition: pios_board.c:44
int32_t PIOS_ANNUNC_Init(const struct pios_annunc_cfg *cfg)
static const struct pios_tim_clock_cfg tim_1_cfg
void PIOS_RTC_Init(const struct pios_rtc_cfg *cfg)
int32_t PIOS_USB_DESC_HID_ONLY_Init(void)
int32_t PIOS_CAN_Init(uintptr_t *id, const struct pios_can_cfg *cfg)
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...
const struct pios_com_driver pios_can_com_driver
uintptr_t pios_com_openlog_logging_id
Definition: pios_board.c:49
uintptr_t pios_com_telem_usb_id
Definition: pios_board.c:42
int32_t PIOS_COM_Init(uintptr_t *com_id, const struct pios_com_driver *driver, uintptr_t lower_id, uint16_t rx_buffer_len, uint16_t tx_buffer_len)
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
int32_t PIOS_USB_Init(uintptr_t *usb_id, const struct pios_usb_cfg *cfg)
uint8_t num_channels
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)
#define PIOS_COM_CAN_TX_BUF_LEN
Definition: pios_board.c:50
void PIOS_IAP_WriteBootCount(uint16_t)
Definition: pios_iap.c:98
static const struct pios_tim_clock_cfg tim_17_cfg
int32_t PIOS_MS5611_Init(const struct pios_ms5611_cfg *cfg, pios_i2c_t i2c_device)
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
int32_t PIOS_FLASHFS_Format(uintptr_t fs_id)
Erases all filesystem arenas and activate the first arena.
uintptr_t pios_com_bridge_id
Definition: pios_hal.c:81
#define PIOS_COM_CAN_RX_BUF_LEN
Definition: pios_board.c:49
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
uintptr_t pios_com_can_id
Definition: pios_board.c:59