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 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
31 /* Pull in the board-specific static HW definitions.
32  * Including .c files is a bit ugly but this allows all of
33  * the HW definitions to be const and static to limit their
34  * scope.
35  *
36  * NOTE: THIS IS THE ONLY PLACE THAT SHOULD EVER INCLUDE THIS FILE
37  */
38 
39 #include "board_hw_defs.c"
40 
41 #include <pios.h>
42 #include <pios_hal.h>
43 #include <openpilot.h>
44 #include <uavobjectsinit.h>
45 #include "hwmatek405.h"
46 #include "manualcontrolsettings.h"
47 #include "modulesettings.h"
48 #include "rgbledsettings.h"
49 
53 
54 #ifdef PIOS_INCLUDE_MAX7456
55 max7456_dev_t pios_max7456_id;
56 #endif
57 
64 #include <pios_board_info.h>
65 
66 void PIOS_Board_Init(void) {
67  const struct pios_board_info * bdinfo = &pios_board_info_blob;
68 
69 #if defined(PIOS_INCLUDE_ANNUNC)
70  const struct pios_annunc_cfg * led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(bdinfo->board_rev);
71  PIOS_Assert(led_cfg);
72  PIOS_ANNUNC_Init(led_cfg);
73 #endif /* PIOS_INCLUDE_ANNUNC */
74 
75 #if defined(PIOS_INCLUDE_SPI)
76  if (PIOS_SPI_Init(&pios_spi_gyro_id, &pios_spi_gyro_cfg)) {
77  PIOS_Assert(0);
78  }
79 #endif
80 
81 #if defined(PIOS_INCLUDE_FLASH)
82  /* Initialize all flash drivers */
83  if (PIOS_Flash_Internal_Init(&pios_internal_flash_id, &flash_internal_cfg) != 0)
85 
86  /* Register the partition table */
87  const struct pios_flash_partition * flash_partition_table;
88  uint32_t num_partitions;
89  flash_partition_table = PIOS_BOARD_HW_DEFS_GetPartitionTable(bdinfo->board_rev, &num_partitions);
90  PIOS_FLASH_register_partition_table(flash_partition_table, num_partitions);
91 
92  /* Mount all filesystems */
95 #endif /* PIOS_INCLUDE_FLASH */
96 
97  HwMatek405Initialize();
98  ModuleSettingsInitialize();
99 
100 #if defined(PIOS_INCLUDE_RTC)
101  /* Initialize the real-time clock and its associated tick */
102  PIOS_RTC_Init(&pios_rtc_main_cfg);
103 #endif
104 
105  /* Initialize watchdog as early as possible to catch faults during init
106  * but do it only if there is no debugger connected
107  */
108  if ((CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == 0) {
109  PIOS_WDG_Init();
110  }
111 
112  HwMatek405FC_Pad_S6Options hw_fcPadS6;
113  HwMatek405FC_Pad_S6Get(&hw_fcPadS6);
114 
115  /* Set up pulse timers */
116 
117  // Timers used for inputs (5)
119 
120  // Timers used for outputs (3, 8, 2, 1, 4)
124 
125  if (hw_fcPadS6 == HWMATEK405_FC_PAD_S6_PWM_6)
127 
129 
130 #ifdef PIOS_INCLUDE_SPI
131  uint8_t hw_osdport;
132 
133  HwMatek405OSDPortGet(&hw_osdport);
134 
135  if (hw_osdport != HWMATEK405_OSDPORT_DISABLED) {
136  if (PIOS_SPI_Init(&pios_spi_osd_id, &pios_spi_osd_cfg)) {
137  PIOS_Assert(0);
138  }
139 
140  switch (hw_osdport) {
141 #ifdef PIOS_INCLUDE_MAX7456
142  case HWMATEK405_OSDPORT_MAX7456OSD:
143  if (!PIOS_MAX7456_init(&pios_max7456_id, pios_spi_osd_id, 0)) {
144  // XXX do something?
145  }
146  break;
147 #endif
148  default:
149  PIOS_Assert(0);
150  break;
151  }
152  }
153 #endif /* PIOS_INCLUDE_SPI */
154 
155  /* IAP System Setup */
156  PIOS_IAP_Init();
157  uint16_t boot_count = PIOS_IAP_ReadBootCount();
158  if (boot_count < 3) {
159  PIOS_IAP_WriteBootCount(++boot_count);
160  AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT);
161  } else {
162  /* Too many failed boot attempts, force hw config to defaults */
163  HwMatek405SetDefaults(HwMatek405Handle(), 0);
164  ModuleSettingsSetDefaults(ModuleSettingsHandle(),0);
165  AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL);
166  }
167 
168 #if defined(PIOS_INCLUDE_USB)
169  /* Initialize board specific USB data */
171 
172  /* Flags to determine if various USB interfaces are advertised */
173  bool usb_hid_present = false;
174  bool usb_cdc_present = false;
175 
176 #if defined(PIOS_INCLUDE_USB_CDC)
178  PIOS_Assert(0);
179  }
180  usb_hid_present = true;
181  usb_cdc_present = true;
182 #else
184  PIOS_Assert(0);
185  }
186  usb_hid_present = true;
187 #endif
188 
189  uintptr_t pios_usb_id;
190  PIOS_USB_Init(&pios_usb_id, PIOS_BOARD_HW_DEFS_GetUsbCfg(bdinfo->board_rev));
191 
192 #if defined(PIOS_INCLUDE_USB_CDC)
193 
194  uint8_t hw_usb_vcpport;
195  /* Configure the USB VCP port */
196  HwMatek405USB_VCPPortGet(&hw_usb_vcpport);
197 
198  if (!usb_cdc_present) {
199  /* Force VCP port function to disabled if we haven't advertised VCP in our USB descriptor */
200  hw_usb_vcpport = HWMATEK405_USB_VCPPORT_DISABLED;
201  }
202 
203  PIOS_HAL_ConfigureCDC(hw_usb_vcpport, pios_usb_id, &pios_usb_cdc_cfg);
204 
205 #endif /* PIOS_INCLUDE_USB_CDC */
206 
207 #if defined(PIOS_INCLUDE_USB_HID)
208  /* Configure the usb HID port */
209  uint8_t hw_usb_hidport;
210  HwMatek405USB_HIDPortGet(&hw_usb_hidport);
211 
212  if (!usb_hid_present) {
213  /* Force HID port function to disabled if we haven't advertised HID in our USB descriptor */
214  hw_usb_hidport = HWMATEK405_USB_HIDPORT_DISABLED;
215  }
216 
217  PIOS_HAL_ConfigureHID(hw_usb_hidport, pios_usb_id, &pios_usb_hid_cfg);
218 
219 #endif /* PIOS_INCLUDE_USB_HID */
220 
221  if (usb_hid_present || usb_cdc_present) {
223  }
224 
225  PIOS_WDG_Clear();
226 #endif /* PIOS_INCLUDE_USB */
227 
228  /* Configure the IO ports */
229 
230 #if defined(PIOS_INCLUDE_I2C)
231  if (PIOS_I2C_Init(&pios_i2c_internal_id, &pios_i2c_internal_cfg))
233 
234  if (PIOS_I2C_CheckClear(pios_i2c_internal_id) != 0)
235  AlarmsSet(SYSTEMALARMS_ALARM_I2C, SYSTEMALARMS_ALARM_WARNING);
236  else
237  if (AlarmsGet(SYSTEMALARMS_ALARM_I2C) == SYSTEMALARMS_ALARM_UNINITIALISED)
238  AlarmsSet(SYSTEMALARMS_ALARM_I2C, SYSTEMALARMS_ALARM_OK);
239 #endif // PIOS_INCLUDE_I2C
240 
241  HwMatek405DSMxModeOptions hw_DSMxMode;
242  HwMatek405DSMxModeGet(&hw_DSMxMode);
243 
244  /* UART1 Port */
245  uint8_t hw_uart1;
246  HwMatek405Uart1Get(&hw_uart1);
247 
248  PIOS_HAL_ConfigurePort(hw_uart1, // port type protocol
249  &pios_usart1_cfg, // usart_port_cfg
250  &pios_usart_com_driver, // com_driver
251  NULL, // i2c_id
252  NULL, // i2c_cfg
253  NULL, // ppm_cfg
254  NULL, // pwm_cfg
255  PIOS_LED_ALARM, // led_id
256  &pios_usart1_dsm_aux_cfg, // dsm_cfg
257  hw_DSMxMode, // dsm_mode
258  NULL); // sbus_cfg
259 
260  uint8_t hw_ppm;
261  HwMatek405PPM_ReceiverGet(&hw_ppm);
262 
263  if (hw_ppm == HWMATEK405_PPM_RECEIVER_ENABLED) {
264  PIOS_HAL_ConfigurePort(HWSHARED_PORTTYPES_PPM, // port type protocol
265  NULL, // usart_port_cfg
266  NULL, // com_driver
267  NULL, // i2c_id
268  NULL, // i2c_cfg
269  &pios_ppm_cfg, // ppm_cfg
270  NULL, // pwm_cfg
271  PIOS_LED_ALARM, // led_id
272  NULL, // dsm_cfg
273  0, // dsm_mode
274  NULL); // sbus_cfg
275  } else {
276  /* UART2 Port */
277  uint8_t hw_uart2;
278  HwMatek405Uart2Get(&hw_uart2);
279 
280  PIOS_HAL_ConfigurePort(hw_uart2, // port type protocol
281  &pios_usart2_cfg, // usart_port_cfg
282  &pios_usart_com_driver, // com_driver
283  NULL, // i2c_id
284  NULL, // i2c_cfg
285  NULL, // ppm_cfg
286  NULL, // pwm_cfg
287  PIOS_LED_ALARM, // led_id
288  &pios_usart2_dsm_aux_cfg, // dsm_cfg
289  hw_DSMxMode, // dsm_mode
290  NULL); // sbus_cfg
291  }
292 
293  /* UART3 Port */
294  uint8_t hw_uart3;
295  HwMatek405Uart3Get(&hw_uart3);
296 
297  PIOS_HAL_ConfigurePort(hw_uart3, // port type protocol
298  &pios_usart3_cfg, // usart_port_cfg
299  &pios_usart_com_driver, // com_driver
300  NULL, // i2c_id
301  NULL, // i2c_cfg
302  NULL, // ppm_cfg
303  NULL, // pwm_cfg
304  PIOS_LED_ALARM, // led_id
305  &pios_usart3_dsm_aux_cfg, // dsm_cfg
306  hw_DSMxMode, // dsm_mode
307  NULL); // sbus_cfg
308 
309  /* UART4 Port */
310  uint8_t hw_uart4;
311  HwMatek405Uart4Get(&hw_uart4);
312 
313  PIOS_HAL_ConfigurePort(hw_uart4, // port type protocol
314  &pios_usart4_cfg, // usart_port_cfg
315  &pios_usart_com_driver, // com_driver
316  NULL, // i2c_id
317  NULL, // i2c_cfg
318  NULL, // ppm_cfg
319  NULL, // pwm_cfg
320  PIOS_LED_ALARM, // led_id
321  &pios_usart4_dsm_aux_cfg, // dsm_cfg
322  hw_DSMxMode, // dsm_mode
323  NULL); // sbus_cfg
324 
325  /* UART5 Port */
326  uint8_t hw_uart5;
327  HwMatek405Uart5Get(&hw_uart5);
328 
329  PIOS_HAL_ConfigurePort(hw_uart5, // port type protocol
330  &pios_usart5_cfg, // usart_port_cfg
331  &pios_usart_com_driver, // com_driver
332  NULL, // i2c_id
333  NULL, // i2c_cfg
334  NULL, // ppm_cfg
335  NULL, // pwm_cfg
336  PIOS_LED_ALARM, // led_id
337  &pios_usart5_dsm_aux_cfg, // dsm_cfg
338  hw_DSMxMode, // dsm_mode
339  NULL); // sbus_cfg
340 
341 #if defined(PIOS_INCLUDE_SERVO) & defined(PIOS_INCLUDE_DMASHOT)
342  if (hw_fcPadS6 == HWMATEK405_FC_PAD_S6_PWM_6)
343  PIOS_DMAShot_Init(&dmashot_config_without_led_string);
344  else
345  PIOS_DMAShot_Init(&dmashot_config_with_led_string);
346 #endif
347 
348 #if defined(PIOS_INCLUDE_TIM) && defined(PIOS_INCLUDE_SERVO)
349 #ifndef PIOS_DEBUG_ENABLE_DEBUG_PINS
350  if (hw_fcPadS6 == HWMATEK405_FC_PAD_S6_PWM_6)
351  PIOS_Servo_Init(&pios_servo_cfg_without_led_string);
352  else
353  PIOS_Servo_Init(&pios_servo_cfg_with_led_string);
354 #else
355  PIOS_DEBUG_Init(&pios_tim_servo_all_channels, NELEMENTS(pios_tim_servo_all_channels));
356 #endif
357 #endif
358 
359 #ifdef PIOS_INCLUDE_WS2811
360  if (hw_fcPadS6 == HWMATEK405_FC_PAD_S6_LEDSTRING) {
361  RGBLEDSettingsInitialize();
362 
363  uint8_t temp;
364 
365  RGBLEDSettingsNumLedsGet(&temp);
366 
367  if (temp > 0) {
369 
370  // Drive default value (off) to strand once at startup
372  }
373  }
374 #endif
375 
376 #ifdef PIOS_INCLUDE_DAC
377  PIOS_DAC_init(&pios_dac, &pios_dac_cfg);
378 
379  PIOS_HAL_ConfigureDAC(pios_dac);
380 #endif /* PIOS_INCLUDE_DAC */
381 
382 /* init sensor queue registration */
384 
385  PIOS_WDG_Clear();
386  PIOS_DELAY_WaitmS(200);
387  PIOS_WDG_Clear();
388 
389 #if defined(PIOS_INCLUDE_MPU)
390  pios_mpu_dev_t mpu_dev = NULL;
391  if (PIOS_MPU_SPI_Init(&mpu_dev, pios_spi_gyro_id, 0, &pios_mpu_cfg) != 0)
393 
395 
397 
398  // Per pios_mpu.c, a bandwidth value of 188 will:
399  // Set the MPU60X0 low pass filter to 188 Hz
400  // Set the ICM206XX gyro low pass filter to 176 Hz
402 
403  // Per pios_mpu.c, a bandwidth value of 99 will:
404  // Do nothing for the MPU60X0
405  // Set the ICM206XX accel low pass filter to 99 Hz
407 #endif
408 
409 #if defined(PIOS_INCLUDE_I2C)
410  PIOS_WDG_Clear();
411 
412  uint8_t hw_ext_mag;
413  HwMatek405MagnetometerGet(&hw_ext_mag);
414 
415  if (hw_ext_mag != HWMATEK405_MAGNETOMETER_NONE) {
416  uint8_t hw_orientation;
417  HwMatek405ExtMagOrientationGet(&hw_orientation);
418 
419  PIOS_HAL_ConfigureExternalMag(hw_ext_mag, hw_orientation,
420  &pios_i2c_internal_id,
421  &pios_i2c_internal_cfg);
422  }
423 
424 #if defined(PIOS_INCLUDE_BMP280)
425  // BMP280 support
426  if (PIOS_BMP280_Init(&pios_bmp280_cfg, pios_i2c_internal_id)) {
428  } else if (PIOS_BMP280_Test()) {
430  }
431 #endif
432 
433  //I2C is slow, sensor init as well, reset watchdog to prevent reset here
434  PIOS_WDG_Clear();
435 
436 #endif /* PIOS_INCLUDE_I2C */
437 
438 #if defined(PIOS_INCLUDE_ADC)
439  /* Configure the adc port(s) */
440 
441  uintptr_t internal_adc_id;
442 
443  if (PIOS_INTERNAL_ADC_Init(&internal_adc_id, &pios_adc_cfg) < 0) {
444  PIOS_Assert(0);
446  }
447 
448  if (PIOS_ADC_Init(&pios_internal_adc_id, &pios_internal_adc_driver, internal_adc_id) < 0)
450 #endif
451 
452  /* Make sure we have at least one telemetry link configured or else fail initialization */
454 }
455 
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)
#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
Configuration structure for the BMP280 driver.
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
struct max7456_dev_s * max7456_dev_t
Definition: pios_max7456.h:65
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
#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
int PIOS_DAC_init(dac_dev_t *dev_out, const struct pios_dac_cfg *cfg)
Allocate and initialise DAC device.
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
static const struct pios_tim_clock_cfg tim_4_cfg
uintptr_t pios_com_telem_serial_id
Definition: pios_hal.c:127
const struct pios_com_driver pios_usart_com_driver
static const struct pios_tim_clock_cfg tim_5_cfg
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
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)
static const struct pios_tim_clock_cfg tim_1_cfg
int32_t PIOS_SENSORS_Init()
Initialize the PIOS_SENSORS interface.
Definition: pios_sensors.c:51
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_MPU_SetAccelBandwidth(uint16_t bandwidth)
Sets the bandwidth desired from the accelerometer. The driver will automatically select the lowest ba...
int32_t PIOS_BMP280_Init(const struct pios_bmp280_cfg *cfg, pios_i2c_t i2c_device)
void PIOS_USBHOOK_Activate(void)
Definition: pios_usbhook.c:80
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_3_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
void PIOS_DMAShot_Init(const struct pios_dmashot_cfg *config)
Initializes the DMAShot driver by loading the configuration.
Definition: pios_dmashot.c:96
#define PIOS_Assert(test)
Definition: pios_debug.h:52
int PIOS_MAX7456_init(max7456_dev_t *dev_out, pios_spi_t spi_handle, uint32_t slave_idx)
Allocate and initialise MAX7456 device.
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_SENSORS_SetMissing(enum pios_sensor_type type)
Assert that an optional (non-accel/gyro), but expected sensor is missing.
Definition: pios_sensors.c:191