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 "hwomnibusf3.h"
45 #include "hwshared.h"
46 #include "manualcontrolsettings.h"
47 #include "modulesettings.h"
48 #include <pios_max7456.h>
49 
50 #if defined(PIOS_INCLUDE_WS2811)
51 #include "rgbledsettings.h"
52 #endif
53 
54 #if defined(PIOS_INCLUDE_DEBUG_CONSOLE)
55 #define PIOS_COM_DEBUGCONSOLE_TX_BUF_LEN 40
56 uintptr_t pios_com_debug_id;
57 #endif /* PIOS_INCLUDE_DEBUG_CONSOLE */
58 
59 uintptr_t pios_com_aux_id;
62 #ifdef PIOS_INCLUDE_MAX7456
63 max7456_dev_t pios_max7456_id;
64 #endif
65 
72 #include <pios_board_info.h>
73 
74 void PIOS_Board_Init(void)
75 {
76  const struct pios_board_info *bdinfo = &pios_board_info_blob;
77 
78 #if defined(PIOS_INCLUDE_ANNUNC)
79  const struct pios_annunc_cfg *led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(bdinfo->board_rev);
80  PIOS_Assert(led_cfg);
81  PIOS_ANNUNC_Init(led_cfg);
82 #endif /* PIOS_INCLUDE_ANNUNC */
83 
84 #if defined(PIOS_INCLUDE_SPI)
85  pios_spi_t pios_spi_gyro_id;
86 
87  /* Set up the SPI interface to the gyro/acelerometer */
88  if (PIOS_SPI_Init(&pios_spi_gyro_id, &pios_spi_gyro_cfg)) {
90  }
91 #endif /* PIOS_INCLUDE_SPI */
92 
93 #if defined(PIOS_INCLUDE_FLASH)
94  /* Inititialize all flash drivers */
95  if (PIOS_Flash_Internal_Init(&pios_internal_flash_id, &flash_internal_cfg) != 0)
97 
98  /* Register the partition table */
99  const struct pios_flash_partition *flash_partition_table;
100  uint32_t num_partitions;
101  flash_partition_table = PIOS_BOARD_HW_DEFS_GetPartitionTable(bdinfo->board_rev, &num_partitions);
102  PIOS_FLASH_register_partition_table(flash_partition_table, num_partitions);
103 
104  /* Mount all filesystems */
105  if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_internal_settings_cfg, FLASH_PARTITION_LABEL_SETTINGS) != 0)
107 #endif /* PIOS_INCLUDE_FLASH */
108 
109  PIOS_RESET_Clear(); // Clear the RCC reset flags after use.
110 
111  /* Initialize the hardware UAVOs */
112  HwOmnibusF3Initialize();
113  ModuleSettingsInitialize();
114 
115 #if defined(PIOS_INCLUDE_RTC)
116  /* Initialize the real-time clock and its associated tick */
117  PIOS_RTC_Init(&pios_rtc_main_cfg);
118 #endif
119 
120  /* Initialize watchdog as early as possible to catch faults during init
121  * but do it only if there is no debugger connected
122  */
123  if ((CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == 0) {
124  PIOS_WDG_Init();
125  }
126 
127  /* Set up pulse timers */
128  //inputs
130 
131  //outputs
135 
136  /* IAP System Setup */
137  PIOS_IAP_Init();
138  uint16_t boot_count = PIOS_IAP_ReadBootCount();
139  if (boot_count < 3) {
140  PIOS_IAP_WriteBootCount(++boot_count);
141  AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT);
142  } else {
143  /* Too many failed boot attempts, force hw config to defaults */
144  HwOmnibusF3SetDefaults(HwOmnibusF3Handle(), 0);
145  ModuleSettingsSetDefaults(ModuleSettingsHandle(), 0);
146  AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL);
147  }
148 
149 #if defined(PIOS_INCLUDE_USB)
150  /* Initialize board specific USB data */
152 
153  /* Flags to determine if various USB interfaces are advertised */
154  bool usb_hid_present = false;
155 
156 #if defined(PIOS_INCLUDE_USB_CDC)
157  bool usb_cdc_present = false;
159  PIOS_Assert(0);
160  }
161  usb_hid_present = true;
162  usb_cdc_present = true;
163 #else
165  PIOS_Assert(0);
166  }
167  usb_hid_present = true;
168 #endif
169 
170  uintptr_t pios_usb_id;
171  PIOS_USB_Init(&pios_usb_id, PIOS_BOARD_HW_DEFS_GetUsbCfg(bdinfo->board_rev));
172 
173 #if defined(PIOS_INCLUDE_USB_CDC)
174 
175  uint8_t hw_usb_vcpport;
176  /* Configure the USB VCP port */
177  HwOmnibusF3USB_VCPPortGet(&hw_usb_vcpport);
178 
179  if (!usb_cdc_present) {
180  /* Force VCP port function to disabled if we haven't advertised VCP in our USB descriptor */
181  hw_usb_vcpport = HWOMNIBUSF3_USB_VCPPORT_DISABLED;
182  }
183 
184  PIOS_HAL_ConfigureCDC(hw_usb_vcpport, pios_usb_id, &pios_usb_cdc_cfg);
185 #endif /* PIOS_INCLUDE_USB_CDC */
186 
187 #if defined(PIOS_INCLUDE_USB_HID)
188  /* Configure the usb HID port */
189  uint8_t hw_usb_hidport;
190  HwOmnibusF3USB_HIDPortGet(&hw_usb_hidport);
191 
192  if (!usb_hid_present) {
193  /* Force HID port function to disabled if we haven't advertised HID in our USB descriptor */
194  hw_usb_hidport = HWOMNIBUSF3_USB_HIDPORT_DISABLED;
195  }
196 
197  PIOS_HAL_ConfigureHID(hw_usb_hidport, pios_usb_id, &pios_usb_hid_cfg);
198 
199 #endif /* PIOS_INCLUDE_USB_HID */
200 #endif /* PIOS_INCLUDE_USB */
201 
202 #if defined(PIOS_INCLUDE_I2C)
203  if (PIOS_I2C_Init(&pios_i2c_internal_id, &pios_i2c_internal_cfg))
205 
206  if (PIOS_I2C_CheckClear(pios_i2c_internal_id) != 0)
208  else
209  if (AlarmsGet(SYSTEMALARMS_ALARM_I2C) == SYSTEMALARMS_ALARM_UNINITIALISED)
210  AlarmsSet(SYSTEMALARMS_ALARM_I2C, SYSTEMALARMS_ALARM_OK);
211 #endif // PIOS_INCLUDE_I2C
212 
213  /* Configure the IO ports */
214  HwOmnibusF3DSMxModeOptions hw_DSMxMode;
215  HwOmnibusF3DSMxModeGet(&hw_DSMxMode);
216 
217  uint8_t hw_uart3;
218  HwOmnibusF3Uart3Get(&hw_uart3);
219  PIOS_HAL_ConfigurePort(hw_uart3, // port_type
220  &pios_uart3_cfg, // usart_port_cfg
221  &pios_usart_com_driver, // com_driver
222  NULL, // i2c_id
223  NULL, // i2c_cfg
224  &pios_ppm_cfg, // ppm_cfg
225  NULL, // pwm_cfg
226  PIOS_LED_ALARM, // led_id
227  &pios_uart3_dsm_aux_cfg, // dsm_cfg
228  hw_DSMxMode, // dsm_mode
229  NULL); // sbus_cfg
230 
231  uint8_t hw_uart2;
232  HwOmnibusF3Uart2Get(&hw_uart2);
233  PIOS_HAL_ConfigurePort(hw_uart2, // port_type
234  &pios_uart2_cfg, // usart_port_cfg
235  &pios_usart_com_driver, // com_driver
236  NULL, // i2c_id
237  NULL, // i2c_cfg
238  NULL, // ppm_cfg
239  NULL, // pwm_cfg
240  PIOS_LED_ALARM, // led_id
241  &pios_uart2_dsm_aux_cfg, // dsm_cfg
242  hw_DSMxMode, // dsm_mode
243  NULL); // sbus_cfg
244 
245  uint8_t hw_uart1;
246  HwOmnibusF3Uart1Get(&hw_uart1);
247  PIOS_HAL_ConfigurePort(hw_uart1, // port_type
248  &pios_uart1_cfg, // usart_port_cfg
249  &pios_usart_com_driver, // com_driver
250  NULL, // i2c_id
251  NULL, // i2c_cfg
252  NULL, // ppm_cfg
253  NULL, // pwm_cfg
254  PIOS_LED_ALARM, // led_id
255  &pios_uart1_dsm_aux_cfg, // dsm_cfg
256  hw_DSMxMode, // dsm_mode
257  NULL); // sbus_cfg
258 
259 #ifdef PIOS_INCLUDE_WS2811
260  uint8_t num_leds;
261 
262  RGBLEDSettingsInitialize();
263 
264  RGBLEDSettingsNumLedsGet(&num_leds);
265 
268 #endif
269 
270 #ifndef PIOS_DEBUG_ENABLE_DEBUG_PINS
271 #ifdef PIOS_INCLUDE_SERVO
273 #endif
274 #else
275  PIOS_DEBUG_Init(&pios_tim_servo_all_channels, NELEMENTS(pios_tim_servo_all_channels));
276 #endif
277 
278 #if defined(PIOS_INCLUDE_ADC)
279  uintptr_t unused_adc;
280  if (PIOS_INTERNAL_ADC_Init(&pios_internal_adc_id, &internal_adc1_cfg) < 0)
281  PIOS_Assert(0);
283 #endif /* PIOS_INCLUDE_ADC */
284 
285  PIOS_WDG_Clear();
286  PIOS_DELAY_WaitmS(200);
287  PIOS_WDG_Clear();
288 
289  pios_mpu_cfg.use_internal_mag=true;
290  pios_mpu_dev_t mpu_dev = NULL;
291  if (PIOS_MPU_SPI_Init(&mpu_dev, pios_spi_gyro_id, 0, &pios_mpu_cfg) != 0)
293 
294  HwOmnibusF3GyroRangeOptions hw_gyro_range;
295  HwOmnibusF3GyroRangeGet(&hw_gyro_range);
296 
297  switch(hw_gyro_range) {
298  case HWOMNIBUSF3_GYRORANGE_250:
300  break;
301  case HWOMNIBUSF3_GYRORANGE_500:
303  break;
304  case HWOMNIBUSF3_GYRORANGE_1000:
306  break;
307  case HWOMNIBUSF3_GYRORANGE_2000:
309  break;
310  }
311 
312  HwOmnibusF3AccelRangeOptions hw_accel_range;
313  HwOmnibusF3AccelRangeGet(&hw_accel_range);
314  switch(hw_accel_range) {
315  case HWOMNIBUSF3_ACCELRANGE_2G:
317  break;
318  case HWOMNIBUSF3_ACCELRANGE_4G:
320  break;
321  case HWOMNIBUSF3_ACCELRANGE_8G:
323  break;
324  case HWOMNIBUSF3_ACCELRANGE_16G:
326  break;
327  }
328 
329  HwOmnibusF3MPU9250GyroLPFOptions hw_mpu_dlpf;
330  HwOmnibusF3MPU9250GyroLPFGet(&hw_mpu_dlpf);
331  uint16_t bandwidth = \
332  (hw_mpu_dlpf == HWOMNIBUSF3_MPU9250GYROLPF_184) ? 184 : \
333  (hw_mpu_dlpf == HWOMNIBUSF3_MPU9250GYROLPF_92) ? 92 : \
334  (hw_mpu_dlpf == HWOMNIBUSF3_MPU9250GYROLPF_41) ? 41 : \
335  (hw_mpu_dlpf == HWOMNIBUSF3_MPU9250GYROLPF_20) ? 20 : \
336  (hw_mpu_dlpf == HWOMNIBUSF3_MPU9250GYROLPF_10) ? 10 : \
337  (hw_mpu_dlpf == HWOMNIBUSF3_MPU9250GYROLPF_5) ? 5 : \
338  188;
339  PIOS_MPU_SetGyroBandwidth(bandwidth);
340 
341  //I2C is slow, sensor init as well, reset watchdog to prevent reset here
342  PIOS_WDG_Clear();
343 
344 #ifdef PIOS_INCLUDE_MAX7456
345  if (!PIOS_MAX7456_init(&pios_max7456_id, pios_spi_gyro_id,
346  1)) {
347  // XXX do something?
348  }
349 #endif
350 
351 #if defined(PIOS_INCLUDE_GPIO)
352  PIOS_GPIO_Init();
353 #endif
354 
355  /* Make sure we have at least one telemetry link configured or else fail initialization */
357 }
358 
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
uintptr_t pios_com_debug_id
Definition: pios_board.c:76
#define PIOS_DEBUG_Assert(test)
Definition: pios_debug.h:51
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
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
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_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
#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_RESET_Clear(void)
PIOS_RESET_Clear Does nothing on POSIX systems.
Definition: pios_reset.c:38