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 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  *
26  * Additional note on redistribution: The copyright and license notices above
27  * must be maintained in each individual source file that is a derivative work
28  * of this source file; otherwise redistribution is prohibited.
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 <pios_max7456.h>
46 #include "hwplayuavosd.h"
47 #include "modulesettings.h"
48 #include "onscreendisplaysettings.h"
49 
50 /* TODO: something nicer about these */
51 #include "attitudeactual.h"
52 #include "accels.h"
53 #include "baroaltitude.h"
54 #include "flightstatus.h"
55 #include "manualcontrolcommand.h"
56 #include "positionactual.h"
57 #include "stabilizationsettings.h"
58 #include "tabletinfo.h"
59 
61 
66 static void PIOS_BOARD_Init_Uavos(void);
67 
72 #if defined(PIOS_INCLUDE_VIDEO)
73 void OSD_configure_bw_levels(void)
74 {
75  GPIO_InitTypeDef GPIO_InitStructure;
76  TIM_OCInitTypeDef TIM_OCInitStructure;
77  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
78 
79  /* --------------------------- System Clocks Configuration -----------------*/
80  /* TIM1 clock enable */
81  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
82 
83  /* GPIOA clock enable */
84  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
85 
86  /* Connect TIM1 pins to AF */
87  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
88  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);
89 
90  /*-------------------------- GPIO Configuration ----------------------------*/
91  GPIO_StructInit(&GPIO_InitStructure); // Reset init structure
92  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_10;
93  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
94  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
95  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
96  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
97  GPIO_Init(GPIOA, &GPIO_InitStructure);
98 
99 
100  /* Time base configuration */
101  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
102  TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_SYSCLK / 25500000) - 1; // Get clock to 25 MHz on STM32F2/F4
103  TIM_TimeBaseStructure.TIM_Period = 255;
104  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
105  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
106  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
107 
108  /* Enable TIM1 Preload register on ARR */
109  TIM_ARRPreloadConfig(TIM1, ENABLE);
110 
111  /* TIM PWM1 Mode configuration */
112  TIM_OCStructInit(&TIM_OCInitStructure);
113  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
114  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
115  TIM_OCInitStructure.TIM_Pulse = 90;
116  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
117 
118  /* Output Compare PWM1 Mode configuration: Channel1 PA.08 */
119  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
120  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
121  TIM_OC3Init(TIM1, &TIM_OCInitStructure);
122  TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
123 
124  /* TIM1 Main Output Enable */
125  TIM_CtrlPWMOutputs(TIM1, ENABLE);
126 
127  /* TIM1 enable counter */
128  TIM_Cmd(TIM1, ENABLE);
129  TIM1->CCR1 = 30;
130  TIM1->CCR3 = 110;
131 }
132 #endif /* PIOS_INCLUDE_VIDEO */
133 
140 #include <pios_board_info.h>
141 
142 void PIOS_Board_Init(void) {
143  // Drive USB re-enumerate pin low (so it doesn't pull DP down!)
144  GPIO_InitTypeDef GPIO_InitStructure;
145  GPIO_StructInit(&GPIO_InitStructure);
146  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
147  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
148  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
149  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
150  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
151  GPIO_Init(GPIOB, &GPIO_InitStructure);
152  GPIO_ResetBits(GPIOB, GPIO_Pin_12);
153 
154 #if defined(PIOS_INCLUDE_ANNUNC)
156 #endif /* PIOS_INCLUDE_ANNUNC */
157 
158 #if defined(PIOS_INCLUDE_FLASH)
159  /* Inititialize all flash drivers */
160  if (PIOS_Flash_Internal_Init(&pios_internal_flash_id, &flash_internal_cfg) != 0)
162 
163  /* Register the partition table */
164  PIOS_FLASH_register_partition_table(pios_flash_partition_table_internal, NELEMENTS(pios_flash_partition_table_internal));
165 
166  /* Mount all filesystems */
167  if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_settings_internal_cfg, FLASH_PARTITION_LABEL_SETTINGS) != 0)
169 #endif /* PIOS_INCLUDE_FLASH */
170 
171  HwPlayUavOsdInitialize();
172  ModuleSettingsInitialize();
173 
174 #if defined(PIOS_INCLUDE_RTC)
175  /* Initialize the real-time clock and its associated tick */
176  PIOS_RTC_Init(&pios_rtc_main_cfg);
177 #endif
178 
179  /* Initialize watchdog as early as possible to catch faults during init
180  * but do it only if there is no debugger connected
181  */
182  if ((CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == 0) {
183  //PIOS_WDG_Init();
184  }
185 
187 
188  /* Set up pulse timers */
189  //inputs
192  //outputs
194 
195  /* IAP System Setup */
196  PIOS_IAP_Init();
197  uint16_t boot_count = PIOS_IAP_ReadBootCount();
198  if (boot_count < 3) {
199  PIOS_IAP_WriteBootCount(++boot_count);
200  AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT);
201  } else {
202  /* Too many failed boot attempts, force hw config to defaults */
203  HwPlayUavOsdSetDefaults(HwPlayUavOsdHandle(), 0);
204  ModuleSettingsSetDefaults(ModuleSettingsHandle(),0);
205  AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL);
206  }
207 
208 #if defined(PIOS_INCLUDE_USB)
209  /* Initialize board specific USB data */
211 
212  /* Flags to determine if various USB interfaces are advertised */
213  bool usb_hid_present = false;
214  bool usb_cdc_present = false;
215 
216 #if defined(PIOS_INCLUDE_USB_CDC)
218  PIOS_Assert(0);
219  }
220  usb_hid_present = true;
221  usb_cdc_present = true;
222 #else
224  PIOS_Assert(0);
225  }
226  usb_hid_present = true;
227 #endif
228 
229  uintptr_t pios_usb_id;
230  PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg);
231 
232 #if defined(PIOS_INCLUDE_USB_CDC)
233 
234  uint8_t hw_usb_vcpport;
235  /* Configure the USB VCP port */
236  HwPlayUavOsdUSB_VCPPortGet(&hw_usb_vcpport);
237 
238  if (!usb_cdc_present) {
239  /* Force VCP port function to disabled if we haven't advertised VCP in our USB descriptor */
240  hw_usb_vcpport = HWPLAYUAVOSD_USB_VCPPORT_DISABLED;
241  }
242 
243  PIOS_HAL_ConfigureCDC(hw_usb_vcpport, pios_usb_id, &pios_usb_cdc_cfg);
244 
245 #endif /* PIOS_INCLUDE_USB_CDC */
246 
247 #if defined(PIOS_INCLUDE_USB_HID)
248  /* Configure the usb HID port */
249  uint8_t hw_usb_hidport;
250  HwPlayUavOsdUSB_HIDPortGet(&hw_usb_hidport);
251 
252  if (!usb_hid_present) {
253  /* Force HID port function to disabled if we haven't advertised HID in our USB descriptor */
254  hw_usb_hidport = HWPLAYUAVOSD_USB_HIDPORT_DISABLED;
255  }
256 
257  PIOS_HAL_ConfigureHID(hw_usb_hidport, pios_usb_id, &pios_usb_hid_cfg);
258 
259 #endif /* PIOS_INCLUDE_USB_HID */
260 
261  if (usb_hid_present || usb_cdc_present) {
263  }
264 #endif /* PIOS_INCLUDE_USB */
265 
266  /* Main Port */
267  uint8_t hw_mainport;
268  HwPlayUavOsdMainPortGet(&hw_mainport);
269 
270  PIOS_HAL_ConfigurePort(hw_mainport, // port type protocol
271  &pios_usart_main_cfg, // usart_port_cfg
272  &pios_usart_com_driver, // com_driver
273  NULL, // i2c_id
274  NULL, // i2c_cfg
275  NULL, // ppm_cfg
276  NULL, // pwm_cfg
277  PIOS_LED_ALARM, // led_id
278  NULL, // dsm_cfg
279  0, // dsm_mode
280  NULL); // sbus_cfg
281 
282 
283  //I2C is slow, sensor init as well, reset watchdog to prevent reset here
284  PIOS_WDG_Clear();
285 
286 #if defined(PIOS_INCLUDE_SPI)
287  if (PIOS_SPI_Init(&pios_spi_max7456_id, &pios_spi_max7456_cfg))
289 
290  GPIO_StructInit(&GPIO_InitStructure);
291  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
292  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
293  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
294  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
295  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
296  GPIO_Init(GPIOC, &GPIO_InitStructure);
297  GPIO_SetBits(GPIOC, GPIO_Pin_0);
298  GPIO_Init(GPIOA, &GPIO_InitStructure);
299  GPIO_ResetBits(GPIOA, GPIO_Pin_0);
300 
301 #if defined (PIOS_INCLUDE_MAX7456)
302  max7456_dev_t max7456;
303  if (PIOS_MAX7456_init(&max7456, pios_spi_max7456_id, 0))
305 #endif // defined (PIOS_INCLUDE_MAX7456)
306 
307 #endif // defined(PIOS_INCLUDE_SPI)
308 
309 #if defined(PIOS_INCLUDE_VIDEO)
310  // make sure the mask pin is low
311  GPIO_Init(pios_video_cfg.mask.miso.gpio, (GPIO_InitTypeDef*)&pios_video_cfg.mask.miso.init);
312  GPIO_ResetBits(pios_video_cfg.mask.miso.gpio, pios_video_cfg.mask.miso.init.GPIO_Pin);
313 
314  // Initialize settings
315  OnScreenDisplaySettingsInitialize();
316 
317  uint8_t osd_state;
318  OnScreenDisplaySettingsOSDEnabledGet(&osd_state);
319  if (osd_state == ONSCREENDISPLAYSETTINGS_OSDENABLED_ENABLED) {
320  OSD_configure_bw_levels();
321  }
322 #endif // defined(PIOS_INCLUDE_VIDEO)
323 
324  /* Make sure we have at least one telemetry link configured or else fail initialization */
326 
328 }
329 
330 static void PIOS_BOARD_Init_Uavos(void)
331 {
332  AttitudeActualInitialize();
333  AccelsInitialize();
334  BaroAltitudeInitialize();
335  FlightStatusInitialize();
336  ManualControlCommandInitialize();
337  PositionActualInitialize();
338  StabilizationSettingsInitialize();
339  TabletInfoInitialize();
340 }
341 
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.
GPIO_InitTypeDef init
Definition: pios_stm32.h:61
#define NELEMENTS(x)
Definition: pios.h:192
GPIO_TypeDef * gpio
Definition: pios_stm32.h:60
#define PIOS_DEBUG_Assert(test)
Definition: pios_debug.h:51
static const struct pios_usart_cfg pios_usart_main_cfg
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
static void PIOS_BOARD_Init_Uavos(void)
Definition: pios_board.c:330
struct max7456_dev_s * max7456_dev_t
Definition: pios_max7456.h:65
#define PIOS_LED_ALARM
Definition: pios_board.h:86
void PIOS_WDG_Clear(void)
Clear the watchdog timer.
Definition: pios_wdg.c:147
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_TIM_InitClock(const struct pios_tim_clock_cfg *cfg)
Definition: pios_tim.c:62
const struct pios_spi_cfg mask
Definition: pios_video.h:97
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)
uintptr_t pios_com_telem_usb_id
Definition: pios_board.c:42
static const struct pios_tim_clock_cfg tim_12_cfg
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
int32_t PIOS_USB_Init(uintptr_t *usb_id, const struct pios_usb_cfg *cfg)
#define PIOS_SYSCLK
Definition: pios_board.h:143
void PIOS_IAP_WriteBootCount(uint16_t)
Definition: pios_iap.c:98
struct stm32_gpio miso
Definition: pios_spi_priv.h:48
void PIOS_HAL_CriticalError(uint32_t led_id, enum pios_hal_panic code)
Flash a blink code.
Definition: pios_hal.c:291
#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_USB_BOARD_DATA_Init(void)
void PIOS_RESET_Clear(void)
PIOS_RESET_Clear Does nothing on POSIX systems.
Definition: pios_reset.c:38