/** * Initialise a single USART device */ int32_t PIOS_USART_Init(uint32_t * usart_id, const struct pios_usart_cfg * cfg) { PIOS_DEBUG_Assert(usart_id); PIOS_DEBUG_Assert(cfg); struct pios_usart_dev * usart_dev; usart_dev = (struct pios_usart_dev *) PIOS_USART_alloc(); if (!usart_dev) goto out_fail; /* Bind the configuration to the device instance */ usart_dev->cfg = cfg; /* Clear buffer counters */ fifoBuf_init(&usart_dev->rx, usart_dev->rx_buffer, sizeof(usart_dev->rx_buffer)); fifoBuf_init(&usart_dev->tx, usart_dev->tx_buffer, sizeof(usart_dev->tx_buffer)); /* Enable the USART Pins Software Remapping */ if (usart_dev->cfg->remap) { GPIO_PinRemapConfig(usart_dev->cfg->remap, ENABLE); } /* Initialize the USART Rx and Tx pins */ GPIO_Init(usart_dev->cfg->rx.gpio, &usart_dev->cfg->rx.init); GPIO_Init(usart_dev->cfg->tx.gpio, &usart_dev->cfg->tx.init); /* Enable USART clock */ switch ((uint32_t)usart_dev->cfg->regs) { case (uint32_t)USART1: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); break; case (uint32_t)USART2: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); break; case (uint32_t)USART3: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); break; } /* Configure the USART */ USART_Init(usart_dev->cfg->regs, &usart_dev->cfg->init); *usart_id = (uint32_t)usart_dev; /* Configure USART Interrupts */ NVIC_Init(&usart_dev->cfg->irq.init); USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE); USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE); /* Enable USART */ USART_Cmd(usart_dev->cfg->regs, ENABLE); return(0); out_fail: return(-1); }
/** * Initialises COM layer * \param[out] handle * \param[in] driver * \param[in] id * \return < 0 if initialisation failed */ int32_t PIOS_COM_Init(uint32_t *com_id, const struct pios_com_driver *driver, uint32_t lower_id, uint8_t *rx_buffer, uint16_t rx_buffer_len, uint8_t *tx_buffer, uint16_t tx_buffer_len) { PIOS_Assert(com_id); PIOS_Assert(driver); bool has_rx = (rx_buffer && rx_buffer_len > 0); bool has_tx = (tx_buffer && tx_buffer_len > 0); PIOS_Assert(has_rx || has_tx); PIOS_Assert(driver->bind_tx_cb || !has_tx); PIOS_Assert(driver->bind_rx_cb || !has_rx); struct pios_com_dev *com_dev; com_dev = (struct pios_com_dev *)PIOS_COM_alloc(); if (!com_dev) { goto out_fail; } com_dev->driver = driver; com_dev->lower_id = lower_id; com_dev->has_rx = has_rx; com_dev->has_tx = has_tx; if (has_rx) { fifoBuf_init(&com_dev->rx, rx_buffer, rx_buffer_len); #if defined(PIOS_INCLUDE_FREERTOS) vSemaphoreCreateBinary(com_dev->rx_sem); #endif /* PIOS_INCLUDE_FREERTOS */ (com_dev->driver->bind_rx_cb)(lower_id, PIOS_COM_RxInCallback, (uint32_t)com_dev); if (com_dev->driver->rx_start) { /* Start the receiver */ (com_dev->driver->rx_start)(com_dev->lower_id, fifoBuf_getFree(&com_dev->rx)); } } if (has_tx) { fifoBuf_init(&com_dev->tx, tx_buffer, tx_buffer_len); #if defined(PIOS_INCLUDE_FREERTOS) vSemaphoreCreateBinary(com_dev->tx_sem); #endif /* PIOS_INCLUDE_FREERTOS */ (com_dev->driver->bind_tx_cb)(lower_id, PIOS_COM_TxOutCallback, (uint32_t)com_dev); } #if defined(PIOS_INCLUDE_FREERTOS) com_dev->sendbuffer_sem = xSemaphoreCreateMutex(); #endif /* PIOS_INCLUDE_FREERTOS */ *com_id = (uint32_t)com_dev; return 0; out_fail: return -1; }
/** * PIOS_Board_Init() * initializes all the core subsystems on this specific hardware * called from System/openpilot.c */ void PIOS_Board_Init(void) { /* Brings up System using CMSIS functions, enables the LEDs. */ PIOS_SYS_Init(); PIOS_LED_On(LED1); /* Delay system */ PIOS_DELAY_Init(); /* Communication system */ #if !defined(PIOS_ENABLE_DEBUG_PINS) #if defined(PIOS_INCLUDE_COM) if (PIOS_USART_Init(&pios_usart_aux_id, &pios_usart_aux_cfg)) { PIOS_DEBUG_Assert(0); } if (PIOS_COM_Init(&pios_com_aux_id, &pios_usart_com_driver, pios_usart_aux_id)) { PIOS_DEBUG_Assert(0); } #endif /* PIOS_INCLUDE_COM */ #endif /* IAP System Setup */ PIOS_IAP_Init(); /* ADC system */ PIOS_ADC_Init(); extern uint8_t adc_oversampling; PIOS_ADC_Config(adc_oversampling); extern void adc_callback(float *); PIOS_ADC_SetCallback(adc_callback); /* ADC buffer */ extern t_fifo_buffer adc_fifo_buffer; fifoBuf_init(&adc_fifo_buffer, adc_fifo_buf, sizeof(adc_fifo_buf)); /* Setup the Accelerometer FS (Full-Scale) GPIO */ PIOS_GPIO_Enable(0); SET_ACCEL_6G; #if defined(PIOS_INCLUDE_HMC5843) && defined(PIOS_INCLUDE_I2C) /* Magnetic sensor system */ if (PIOS_I2C_Init(&pios_i2c_main_adapter_id, &pios_i2c_main_adapter_cfg)) { PIOS_DEBUG_Assert(0); } PIOS_HMC5843_Init(); #endif #if defined(PIOS_INCLUDE_SPI) #include "ahrs_spi_comm.h" AhrsInitComms(); /* Set up the SPI interface to the OP board */ if (PIOS_SPI_Init(&pios_spi_op_id, &pios_spi_op_cfg)) { PIOS_DEBUG_Assert(0); } AhrsConnect(pios_spi_op_id); #endif }
/** * @brief Allocate a new device */ static struct bma180_dev * PIOS_BMA180_alloc(void) { struct bma180_dev * bma180_dev; bma180_dev = (struct bma180_dev *)pvPortMalloc(sizeof(*bma180_dev)); if (!bma180_dev) return (NULL); fifoBuf_init(&bma180_dev->fifo, (uint8_t *) bma180_dev->buffer, sizeof(bma180_dev->buffer)); bma180_dev->magic = PIOS_BMA180_DEV_MAGIC; return(bma180_dev); }
int main() { /* NOTE: Do NOT modify the following start-up sequence */ /* Any new initialization functions should be added in OpenPilotInit() */ /* Brings up System using CMSIS functions, enables the LEDs. */ PIOS_SYS_Init(); if (BSL_HOLD_STATE == 0) USB_connected = TRUE; PIOS_IAP_Init(); if (PIOS_IAP_CheckRequest() == TRUE) { PIOS_Board_Init(); PIOS_DELAY_WaitmS(1000); User_DFU_request = TRUE; PIOS_IAP_ClearRequest(); } GO_dfu = (USB_connected == TRUE) || (User_DFU_request == TRUE); if (GO_dfu == TRUE) { if (USB_connected) ProgPort = Usb; else ProgPort = Serial; PIOS_Board_Init(); if(User_DFU_request == TRUE) DeviceState = DFUidle; else DeviceState = BLidle; STOPWATCH_Init(100,LED_PWM_TIMER); if (ProgPort == Serial) { fifoBuf_init(&ssp_buffer,rx_buffer,UART_BUFFER_SIZE); STOPWATCH_Init(100,SSP_TIMER);//nao devia ser 1000? STOPWATCH_Reset(SSP_TIMER); ssp_Init(&ssp_port, &SSP_PortConfig); } PIOS_OPAHRS_ForceSlaveSelected(true); } else JumpToApp = TRUE; STOPWATCH_Reset(LED_PWM_TIMER); while (TRUE) { if (ProgPort == Serial) { ssp_ReceiveProcess(&ssp_port); status=ssp_SendProcess(&ssp_port); while((status!=SSP_TX_IDLE) && (status!=SSP_TX_ACKED)){ ssp_ReceiveProcess(&ssp_port); status=ssp_SendProcess(&ssp_port); } } if (JumpToApp == TRUE) jump_to_app(); //pwm_period = 50; // *100 uS -> 5 mS //pwm_sweep_steps =100; // * 5 mS -> 500 mS switch (DeviceState) { case Last_operation_Success: case uploadingStarting: case DFUidle: period1 = 50; sweep_steps1 = 100; PIOS_LED_Off(RED); period2 = 0; break; case uploading: period1 = 50; sweep_steps1 = 100; period2 = 25; sweep_steps2 = 50; break; case downloading: period1 = 25; sweep_steps1 = 50; PIOS_LED_Off(RED); period2 = 0; break; case BLidle: period1 = 0; PIOS_LED_On(BLUE); period2 = 0; break; default://error period1 = 50; sweep_steps1 = 100; period2 = 50; sweep_steps2 = 100; } if (period1 != 0) { if (LedPWM(period1, sweep_steps1, STOPWATCH_ValueGet(LED_PWM_TIMER))) PIOS_LED_On(BLUE); else PIOS_LED_Off(BLUE); } else PIOS_LED_On(BLUE); if (period2 != 0) { if (LedPWM(period2, sweep_steps2, STOPWATCH_ValueGet(LED_PWM_TIMER))) PIOS_LED_On(RED); else PIOS_LED_Off(RED); } else PIOS_LED_Off(RED); if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100) STOPWATCH_Reset(LED_PWM_TIMER); if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState == BLidle)) JumpToApp = TRUE; processRX(); DataDownload(start); } }