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 "hwlux.h"
45 #include "hwshared.h"
46 #include "manualcontrolsettings.h"
47 #include "modulesettings.h"
48 
49 #if defined(PIOS_INCLUDE_WS2811)
50 #include "rgbledsettings.h"
51 #endif
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;
59 uintptr_t pios_com_can_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 #if defined(PIOS_INCLUDE_SPI)
83  pios_spi_t pios_spi_gyro_id;
84 
85  /* Set up the SPI interface to the gyro/acelerometer */
86  if (PIOS_SPI_Init(&pios_spi_gyro_id, &pios_spi_gyro_cfg)) {
88  }
89 #endif /* PIOS_INCLUDE_SPI */
90 
91 #if defined(PIOS_INCLUDE_FLASH)
92  /* Inititialize all flash drivers */
93  if (PIOS_Flash_Internal_Init(&pios_internal_flash_id, &flash_internal_cfg) != 0)
95 
96  /* Register the partition table */
97  const struct pios_flash_partition *flash_partition_table;
98  uint32_t num_partitions;
99  flash_partition_table = PIOS_BOARD_HW_DEFS_GetPartitionTable(bdinfo->board_rev, &num_partitions);
100  PIOS_FLASH_register_partition_table(flash_partition_table, num_partitions);
101 
102  /* Mount all filesystems */
103  if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_internal_settings_cfg, FLASH_PARTITION_LABEL_SETTINGS) != 0)
105 #endif /* PIOS_INCLUDE_FLASH */
106 
107  PIOS_RESET_Clear(); // Clear the RCC reset flags after use.
108 
109  /* Initialize the hardware UAVOs */
110  HwLuxInitialize();
111  ModuleSettingsInitialize();
112 
113 #if defined(PIOS_INCLUDE_RTC)
114  /* Initialize the real-time clock and its associated tick */
115  PIOS_RTC_Init(&pios_rtc_main_cfg);
116 #endif
117 
118  /* Initialize watchdog as early as possible to catch faults during init
119  * but do it only if there is no debugger connected
120  */
121  if ((CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == 0) {
122  PIOS_WDG_Init();
123  }
124 
125  /* Set up pulse timers */
126  //inputs
127 
128  //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  HwLuxSetDefaults(HwLuxHandle(), 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  HwLuxUSB_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 = HWLUX_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  HwLuxUSB_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 = HWLUX_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  /* Configure the IO ports */
203  HwLuxDSMxModeOptions hw_DSMxMode;
204  HwLuxDSMxModeGet(&hw_DSMxMode);
205 
206  uint8_t hw_uart3;
207  HwLuxUart3Get(&hw_uart3);
208  PIOS_HAL_ConfigurePort(hw_uart3, // port_type
209  &pios_uart3_cfg, // usart_port_cfg
210  &pios_usart_com_driver, // com_driver
211  NULL, // i2c_id
212  NULL, // i2c_cfg
213  NULL, // ppm_cfg
214  NULL, // pwm_cfg
215  PIOS_LED_ALARM, // led_id
216  &pios_uart3_dsm_aux_cfg, // dsm_cfg
217  hw_DSMxMode, // dsm_mode
218  NULL); // sbus_cfg
219 
220  uint8_t hw_uart2;
221  HwLuxUart2Get(&hw_uart2);
222  PIOS_HAL_ConfigurePort(hw_uart2, // port_type
223  &pios_uart2_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_aux_cfg, // dsm_cfg
231  hw_DSMxMode, // dsm_mode
232  NULL); // sbus_cfg
233 
234  /* Configure the rx port */
235  uint8_t hw_rxport;
236  HwLuxRxPortGet(&hw_rxport);
237  PIOS_HAL_ConfigurePort(hw_rxport, // port_type
238  &pios_rxport_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_rxport_dsm_aux_cfg,// dsm_cfg
246  hw_DSMxMode, // dsm_mode
247  NULL); // sbus_cfg
248 
249  /* Always configure PPM */
250  const uint8_t hw_rxppm = HWSHARED_PORTTYPES_PPM;
251  PIOS_HAL_ConfigurePort(hw_rxppm, // port_type
252  NULL, // usart_port_cfg
253  NULL, // com_driver
254  NULL, // i2c_id
255  NULL, // i2c_cfg
256  &pios_ppm_cfg, // ppm_cfg
257  NULL, // pwm_cfg
258  PIOS_LED_ALARM, // led_id
259  NULL, // dsm_cfg
260  0, // dsm_mode
261  NULL); // sbus_cfg
262 
263 #ifdef PIOS_INCLUDE_WS2811
264  uint8_t num_leds;
265 
266  RGBLEDSettingsInitialize();
267 
268  RGBLEDSettingsNumLedsGet(&num_leds);
269 
272 #endif
273 
274 #ifndef PIOS_DEBUG_ENABLE_DEBUG_PINS
275 #ifdef PIOS_INCLUDE_SERVO
277 #endif
278 #else
279  PIOS_DEBUG_Init(&pios_tim_servo_all_channels, NELEMENTS(pios_tim_servo_all_channels));
280 #endif
281 
282 #if defined(PIOS_INCLUDE_ADC)
283  uintptr_t unused_adc;
284  if (PIOS_INTERNAL_ADC_Init(&pios_internal_adc_id, &internal_adc_cfg) < 0)
285  PIOS_Assert(0);
287 #endif /* PIOS_INCLUDE_ADC */
288 
289  PIOS_WDG_Clear();
290  PIOS_DELAY_WaitmS(200);
291  PIOS_WDG_Clear();
292 
293 #if defined(PIOS_INCLUDE_MPU)
294  pios_mpu_dev_t mpu_dev = NULL;
295 
296  if (PIOS_MPU_SPI_Init(&mpu_dev, pios_spi_gyro_id, 0, &pios_mpu_cfg) != 0)
298 
299  // To be safe map from UAVO enum to driver enum
300  uint8_t hw_gyro_range;
301  HwLuxGyroRangeGet(&hw_gyro_range);
302  switch(hw_gyro_range) {
303  case HWLUX_GYRORANGE_250:
305  break;
306  case HWLUX_GYRORANGE_500:
308  break;
309  case HWLUX_GYRORANGE_1000:
311  break;
312  case HWLUX_GYRORANGE_2000:
314  break;
315  }
316 
317  uint8_t hw_accel_range;
318  HwLuxAccelRangeGet(&hw_accel_range);
319  switch(hw_accel_range) {
320  case HWLUX_ACCELRANGE_2G:
322  break;
323  case HWLUX_ACCELRANGE_4G:
325  break;
326  case HWLUX_ACCELRANGE_8G:
328  break;
329  case HWLUX_ACCELRANGE_16G:
331  break;
332  }
333 
334  // the filter has to be set before rate else divisor calculation will fail
335  uint8_t hw_mpu9250_dlpf;
336  HwLuxMPU9250GyroLPFGet(&hw_mpu9250_dlpf);
337 
338  uint16_t bandwidth = \
339  (hw_mpu9250_dlpf == HWLUX_MPU9250GYROLPF_184) ? 184 : \
340  (hw_mpu9250_dlpf == HWLUX_MPU9250GYROLPF_92) ? 92 : \
341  (hw_mpu9250_dlpf == HWLUX_MPU9250GYROLPF_41) ? 41 : \
342  (hw_mpu9250_dlpf == HWLUX_MPU9250GYROLPF_20) ? 20 : \
343  (hw_mpu9250_dlpf == HWLUX_MPU9250GYROLPF_10) ? 10 : \
344  (hw_mpu9250_dlpf == HWLUX_MPU9250GYROLPF_5) ? 5 : \
345  188;
346  PIOS_MPU_SetGyroBandwidth(bandwidth);
347 
348 #endif /* PIOS_INCLUDE_MPU */
349 
350  //I2C is slow, sensor init as well, reset watchdog to prevent reset here
351  PIOS_WDG_Clear();
352 
353  //I2C is slow, sensor init as well, reset watchdog to prevent reset here
354  PIOS_WDG_Clear();
355 
356  /* Make sure we have at least one telemetry link configured or else fail initialization */
358 }
359 
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
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
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
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)
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_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_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
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