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  * Additional note on redistribution: The copyright and license notices above
30  * must be maintained in each individual source file that is a derivative work
31  * of this source file; otherwise redistribution is prohibited.
32  */
33 
34 /* Pull in the board-specific static HW definitions.
35  * Including .c files is a bit ugly but this allows all of
36  * the HW definitions to be const and static to limit their
37  * scope.
38  *
39  * NOTE: THIS IS THE ONLY PLACE THAT SHOULD EVER INCLUDE THIS FILE
40  */
41 
42 #include "board_hw_defs.c"
43 
44 #include <pios.h>
45 #include <pios_hal.h>
46 #include <openpilot.h>
47 #include <uavobjectsinit.h>
48 #include "hwdtfc.h"
49 #include "manualcontrolsettings.h"
50 #include "modulesettings.h"
51 #include "flightbatterysettings.h"
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 uintptr_t pios_can_id;
63 
70 #include <pios_board_info.h>
71 
72 void PIOS_Board_Init(void)
73 {
74  const struct pios_board_info *bdinfo = &pios_board_info_blob;
75 
76 #if defined(PIOS_INCLUDE_ANNUNC)
77  const struct pios_annunc_cfg *led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(bdinfo->board_rev);
78  PIOS_Assert(led_cfg);
79  PIOS_ANNUNC_Init(led_cfg);
80 #endif /* PIOS_INCLUDE_ANNUNC */
81 
82  pios_spi_t pios_spi_gyro_id;
83 
84  /* Set up the SPI interface to the gyro/acelerometer */
85  if (PIOS_SPI_Init(&pios_spi_gyro_id, &pios_spi_gyro_cfg)) {
87  }
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 
95  /* Register the partition table */
96  const struct pios_flash_partition *flash_partition_table;
97  uint32_t num_partitions;
98  flash_partition_table = PIOS_BOARD_HW_DEFS_GetPartitionTable(bdinfo->board_rev, &num_partitions);
99  PIOS_FLASH_register_partition_table(flash_partition_table, num_partitions);
100 
101  /* Mount all filesystems */
102  if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_internal_settings_cfg, FLASH_PARTITION_LABEL_SETTINGS) != 0)
104 #endif /* PIOS_INCLUDE_FLASH */
105 
106  PIOS_RESET_Clear(); // Clear the RCC reset flags after use.
107 
108  /* Initialize the hardware UAVOs */
109  HwDtfcInitialize();
110  ModuleSettingsInitialize();
111 
112 #if defined(PIOS_INCLUDE_RTC)
113  /* Initialize the real-time clock and its associated tick */
114  PIOS_RTC_Init(&pios_rtc_main_cfg);
115 #endif
116 
117  /* Initialize watchdog as early as possible to catch faults during init
118  * but do it only if there is no debugger connected
119  */
120  if ((CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == 0) {
121  PIOS_WDG_Init();
122  }
123 
124  /* Set up pulse timers */
125  //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  HwDtfcSetDefaults(HwDtfcHandle(), 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  HwDtfcUSB_VCPPortOptions hw_usb_vcpport;
174  /* Configure the USB VCP port */
175  HwDtfcUSB_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 = HWDTFC_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  HwDtfcUSB_HIDPortOptions hw_usb_hidport;
188  HwDtfcUSB_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 = HWDTFC_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  /* Configure the IO ports */
201  HwSharedDSMxModeOptions hw_DSMxMode;
202  HwDtfcDSMxModeGet(&hw_DSMxMode);
203 
204  /* Configure the rcvr port */
205  HwSharedPortTypesOptions hw_rcvrport;
206  HwDtfcRcvrPortGet(&hw_rcvrport);
207  PIOS_HAL_ConfigurePort(hw_rcvrport, // port_type
208  &pios_uart3_usart_cfg, // usart_port_cfg
209  &pios_usart_com_driver, // com_driver
210  NULL, // i2c_id
211  NULL, // i2c_cfg
212  &pios_ppm_cfg, // ppm_cfg
213  NULL, // pwm_cfg
214  PIOS_LED_ALARM, // led_id
215  &pios_uart3_dsm_cfg, // dsm_cfg
216  hw_DSMxMode, // dsm_mode
217  NULL); // sbus_cfg
218 
219  /* Configure Uart1 */
220  HwSharedPortTypesOptions hw_uart1;
221  HwDtfcUart1Get(&hw_uart1);
222  PIOS_HAL_ConfigurePort(hw_uart1, // port_type
223  &pios_uart1_usart_cfg, // usart_port_cfg
224  &pios_usart_com_driver, // com_driver
225  NULL, // i2c_id
226  NULL, // i2c_cfg
227  NULL, // ppm_cfg
228  NULL, // pwm_cfg
229  PIOS_LED_ALARM, // led_id
230  &pios_uart1_dsm_cfg, // dsm_cfg
231  hw_DSMxMode, // dsm_mode
232  NULL); // sbus_cfg
233 
234  /* Configure Uart2 */
235  HwSharedPortTypesOptions hw_uart2;
236  HwDtfcUart2Get(&hw_uart2);
237  PIOS_HAL_ConfigurePort(hw_uart2, // port_type
238  &pios_uart2_usart_cfg, // usart_port_cfg
239  &pios_usart_com_driver, // com_driver
240  NULL, // i2c_id
241  NULL, // i2c_cfg
242  NULL, // ppm_cfg
243  NULL, // pwm_cfg
244  PIOS_LED_ALARM, // led_id
245  &pios_uart2_dsm_cfg, // dsm_cfg
246  hw_DSMxMode, // dsm_mode
247  NULL); // sbus_cfg
248 
249 #ifndef PIOS_DEBUG_ENABLE_DEBUG_PINS
250 #ifdef PIOS_INCLUDE_SERVO
252 #endif
253 #else
254  PIOS_DEBUG_Init(&pios_tim_servo_all_channels, NELEMENTS(pios_tim_servo_all_channels));
255 #endif
256 
257 #if defined(PIOS_INCLUDE_ADC)
258  uintptr_t unused_adc;
259  if (PIOS_INTERNAL_ADC_Init(&pios_internal_adc_id, &internal_adc_cfg) < 0)
260  PIOS_Assert(0);
262 
263  // Set voltage/current calibration values, if the current settings are UAVO defaults.
264  FlightBatterySettingsInitialize();
265  FlightBatterySettingsData batterysettings;
266  FlightBatterySettingsGet(&batterysettings);
267 
268  if(batterysettings.SensorCalibrationFactor[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONFACTOR_CURRENT] == (float)36.6)
269  batterysettings.SensorCalibrationFactor[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONFACTOR_CURRENT] = (float)DTFC_CURRENT_CALIBRATION_VALUE;
270  if(batterysettings.SensorCalibrationFactor[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONFACTOR_VOLTAGE] == (float)63.69)
271  batterysettings.SensorCalibrationFactor[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONFACTOR_VOLTAGE] = (float)DTFC_VOLTAGE_CALIBRATION_VALUE;
272  FlightBatterySettingsSet(&batterysettings);
273 #endif /* PIOS_INCLUDE_ADC */
274 
275  PIOS_WDG_Clear();
276  PIOS_DELAY_WaitmS(200);
277  PIOS_WDG_Clear();
278 
279 #if defined(PIOS_INCLUDE_MPU)
280  pios_mpu_dev_t mpu_dev = NULL;
281  if (PIOS_MPU_SPI_Init(&mpu_dev, pios_spi_gyro_id, 0, &pios_mpu_cfg) != 0)
283 
284  HwDtfcGyroRangeOptions hw_gyro_range;
285  HwDtfcGyroRangeGet(&hw_gyro_range);
286  switch(hw_gyro_range) {
287  case HWDTFC_GYRORANGE_250:
289  break;
290  case HWDTFC_GYRORANGE_500:
292  break;
293  case HWDTFC_GYRORANGE_1000:
295  break;
296  case HWDTFC_GYRORANGE_2000:
298  break;
299  }
300 
301  HwDtfcAccelRangeOptions hw_accel_range;
302  HwDtfcAccelRangeGet(&hw_accel_range);
303  switch(hw_accel_range) {
304  case HWDTFC_ACCELRANGE_2G:
306  break;
307  case HWDTFC_ACCELRANGE_4G:
309  break;
310  case HWDTFC_ACCELRANGE_8G:
312  break;
313  case HWDTFC_ACCELRANGE_16G:
315  break;
316  }
317 
318  // the filter has to be set before rate else divisor calculation will fail
319  HwDtfcICM20608G_GyroLPFOptions hw_mpu_gyro_dlpf;
320  HwDtfcICM20608G_GyroLPFGet(&hw_mpu_gyro_dlpf);
321  uint16_t gyro_bandwidth =
322  (hw_mpu_gyro_dlpf == HWDTFC_ICM20608G_GYROLPF_176) ? 176 :
323  (hw_mpu_gyro_dlpf == HWDTFC_ICM20608G_GYROLPF_92) ? 92 :
324  (hw_mpu_gyro_dlpf == HWDTFC_ICM20608G_GYROLPF_41) ? 41 :
325  (hw_mpu_gyro_dlpf == HWDTFC_ICM20608G_GYROLPF_20) ? 20 :
326  (hw_mpu_gyro_dlpf == HWDTFC_ICM20608G_GYROLPF_10) ? 10 :
327  (hw_mpu_gyro_dlpf == HWDTFC_ICM20608G_GYROLPF_5) ? 5 :
328  176;
329  PIOS_MPU_SetGyroBandwidth(gyro_bandwidth);
330 
331  HwDtfcICM20608G_AccelLPFOptions hw_mpu_accel_dlpf;
332  HwDtfcICM20608G_AccelLPFGet(&hw_mpu_accel_dlpf);
333  uint16_t acc_bandwidth =
334  (hw_mpu_accel_dlpf = HWDTFC_ICM20608G_ACCELLPF_218) ? 218 :
335  (hw_mpu_accel_dlpf = HWDTFC_ICM20608G_ACCELLPF_99) ? 99 :
336  (hw_mpu_accel_dlpf = HWDTFC_ICM20608G_ACCELLPF_45) ? 45 :
337  (hw_mpu_accel_dlpf = HWDTFC_ICM20608G_ACCELLPF_21) ? 21 :
338  (hw_mpu_accel_dlpf = HWDTFC_ICM20608G_ACCELLPF_10) ? 10 :
339  (hw_mpu_accel_dlpf = HWDTFC_ICM20608G_ACCELLPF_5) ? 5 :
340  218;
341  PIOS_MPU_SetAccelBandwidth(acc_bandwidth);
342 #endif /* PIOS_INCLUDE_MPU */
343 
344  //I2C is slow, sensor init as well, reset watchdog to prevent reset here
345  PIOS_WDG_Clear();
346 
347  //I2C is slow, sensor init as well, reset watchdog to prevent reset here
348  PIOS_WDG_Clear();
349 
350  /* Make sure we have at least one telemetry link configured or else fail initialization */
352 }
353 
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)
#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.
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
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
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
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)
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)
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...
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)
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_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)
void PIOS_RESET_Clear(void)
PIOS_RESET_Clear Does nothing on POSIX systems.
Definition: pios_reset.c:38