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 "hwpikoblx.h"
45 #include "manualcontrolsettings.h"
46 #include "modulesettings.h"
47 
48 #define PIOS_COM_CAN_RX_BUF_LEN 256
49 #define PIOS_COM_CAN_TX_BUF_LEN 256
50 
51 uintptr_t pios_com_aux_id;
52 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_SPI)
77  pios_spi_t pios_spi_gyro_id;
78 
79  /* Set up the SPI interface to the gyro/accelerometer */
80  if (PIOS_SPI_Init(&pios_spi_gyro_id, &pios_spi_gyro_cfg)) {
82  }
83 #endif
84 
85 #if defined(PIOS_INCLUDE_FLASH)
86  /* Inititialize all flash drivers */
87  if (PIOS_Flash_Internal_Init(&pios_internal_flash_id, &flash_internal_cfg) != 0)
89 
90  /* Register the partition table */
91  const struct pios_flash_partition *flash_partition_table;
92  uint32_t num_partitions;
93  flash_partition_table = PIOS_BOARD_HW_DEFS_GetPartitionTable(bdinfo->board_rev, &num_partitions);
94  PIOS_FLASH_register_partition_table(flash_partition_table, num_partitions);
95 
96  /* Mount all filesystems */
97  if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_internal_settings_cfg, FLASH_PARTITION_LABEL_SETTINGS) != 0)
99 #if defined(ERASE_FLASH)
101 #endif
102 
103 #endif /* PIOS_INCLUDE_FLASH */
104 
105  PIOS_RESET_Clear(); // Clear the RCC reset flags after use.
106 
107  /* Initialize the hardware UAVOs */
108  HwPikoBLXInitialize();
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
125 
126  //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  HwPikoBLXSetDefaults(HwPikoBLXHandle(), 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  HwPikoBLXUSB_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 = HWPIKOBLX_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  HwPikoBLXUSB_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 = HWPIKOBLX_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 
202  HwPikoBLXDSMxModeOptions hw_DSMxMode;
203  HwPikoBLXDSMxModeGet(&hw_DSMxMode);
204 
205  HwSharedPortTypesOptions hw_uart1;
206  HwPikoBLXUart1Get(&hw_uart1);
207  PIOS_HAL_ConfigurePort(hw_uart1, // port_type
208  &pios_uart1_usart_cfg, // usart_port_cfg
209  &pios_usart_com_driver, // com_driver
210  NULL, // i2c_id
211  NULL, // i2c_cfg
212  NULL, // ppm_cfg
213  NULL, // pwm_cfg
214  PIOS_LED_ALARM, // led_id
215  &pios_uart1_dsm_cfg, // dsm_cfg
216  hw_DSMxMode, // dsm_mode
217  NULL); // sbus_cfg
218 
219  /* Configure Uart2 */
220  HwSharedPortTypesOptions hw_uart2;
221  HwPikoBLXUart2Get(&hw_uart2);
222  PIOS_HAL_ConfigurePort(hw_uart2, // port_type
223  &pios_uart2_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_uart2_dsm_cfg, // dsm_cfg
231  hw_DSMxMode, // dsm_mode
232  NULL); // sbus_cfg
233 
234  /* Configure Uart3 */
235  HwSharedPortTypesOptions hw_rxp;
236  HwPikoBLXRxPortGet(&hw_rxp);
237  PIOS_HAL_ConfigurePort(hw_rxp, // port_type
238  &pios_uart3_usart_cfg, // usart_port_cfg
239  &pios_usart_com_driver, // com_driver
240  NULL, // i2c_id
241  NULL, // i2c_cfg
242  &pios_ppm_cfg, // ppm_cfg
243  NULL, // pwm_cfg
244  PIOS_LED_ALARM, // led_id
245  &pios_uart3_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 #endif /* PIOS_INCLUDE_ADC */
263 
264  PIOS_WDG_Clear();
265  PIOS_DELAY_WaitmS(200);
266  PIOS_WDG_Clear();
267 
268 #if defined(PIOS_INCLUDE_MPU)
269  pios_mpu_dev_t mpu_dev = NULL;
270  if (PIOS_MPU_SPI_Init(&mpu_dev, pios_spi_gyro_id, 0, &pios_mpu_cfg) != 0)
272 
273  HwPikoBLXGyroRangeOptions hw_gyro_range;
274  HwPikoBLXGyroRangeGet(&hw_gyro_range);
275  switch(hw_gyro_range) {
276  case HWPIKOBLX_GYRORANGE_250:
278  break;
279  case HWPIKOBLX_GYRORANGE_500:
281  break;
282  case HWPIKOBLX_GYRORANGE_1000:
284  break;
285  case HWPIKOBLX_GYRORANGE_2000:
287  break;
288  }
289 
290  HwPikoBLXAccelRangeOptions hw_accel_range;
291  HwPikoBLXAccelRangeGet(&hw_accel_range);
292  switch(hw_accel_range) {
293  case HWPIKOBLX_ACCELRANGE_2G:
295  break;
296  case HWPIKOBLX_ACCELRANGE_4G:
298  break;
299  case HWPIKOBLX_ACCELRANGE_8G:
301  break;
302  case HWPIKOBLX_ACCELRANGE_16G:
304  break;
305  }
306 
307  // the filter has to be set before rate else divisor calculation will fail
308  HwPikoBLXGyroLPFOptions hw_mpu_gyro_dlpf;
309  HwPikoBLXGyroLPFGet(&hw_mpu_gyro_dlpf);
310  uint16_t gyro_bandwidth =
311  (hw_mpu_gyro_dlpf == HWPIKOBLX_GYROLPF_188) ? 188 :
312  (hw_mpu_gyro_dlpf == HWPIKOBLX_GYROLPF_98) ? 98 :
313  (hw_mpu_gyro_dlpf == HWPIKOBLX_GYROLPF_42) ? 42 :
314  (hw_mpu_gyro_dlpf == HWPIKOBLX_GYROLPF_20) ? 20 :
315  (hw_mpu_gyro_dlpf == HWPIKOBLX_GYROLPF_10) ? 10 :
316  (hw_mpu_gyro_dlpf == HWPIKOBLX_GYROLPF_5) ? 5 :
317  184;
318  PIOS_MPU_SetGyroBandwidth(gyro_bandwidth);
319 
320  HwPikoBLXAccelLPFOptions hw_mpu_accel_dlpf;
321  HwPikoBLXAccelLPFGet(&hw_mpu_accel_dlpf);
322  uint16_t acc_bandwidth =
323  (hw_mpu_accel_dlpf = HWPIKOBLX_ACCELLPF_218) ? 218 :
324  (hw_mpu_accel_dlpf = HWPIKOBLX_ACCELLPF_99) ? 99 :
325  (hw_mpu_accel_dlpf = HWPIKOBLX_ACCELLPF_45) ? 45 :
326  (hw_mpu_accel_dlpf = HWPIKOBLX_ACCELLPF_21) ? 21 :
327  (hw_mpu_accel_dlpf = HWPIKOBLX_ACCELLPF_10) ? 10 :
328  (hw_mpu_accel_dlpf = HWPIKOBLX_ACCELLPF_5) ? 5 :
329  184;
330  PIOS_MPU_SetAccelBandwidth(acc_bandwidth);
331 #endif /* PIOS_INCLUDE_MPU */
332 
333  /* I2C is slow, sensor init as well, reset watchdog to prevent reset here */
334  PIOS_WDG_Clear();
335 
336  /* Make sure we have at least one telemetry link configured or else fail initialization */
338 }
339 
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
#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
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
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_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.
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
uintptr_t pios_com_can_id
Definition: pios_board.c:59