/* * See the serial2.h header file. */ xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength ) { xComPortHandle xReturn; UART_InitTypeDef xUART1_Init; GPIO_InitTypeDef GPIO_InitStructure; /* Create the queues used to hold Rx characters. */ xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) ); /* Create the semaphore used to wake a task waiting for space to become available in the FIFO. */ vSemaphoreCreateBinary( xTxFIFOSemaphore ); /* If the queue/semaphore was created correctly then setup the serial port hardware. */ if( ( xRxedChars != serINVALID_QUEUE ) && ( xTxFIFOSemaphore != serINVALID_QUEUE ) ) { /* Pre take the semaphore so a task will block if it tries to access it. */ xSemaphoreTake( xTxFIFOSemaphore, 0 ); /* Configure the UART. */ xUART1_Init.UART_WordLength = UART_WordLength_8D; xUART1_Init.UART_StopBits = UART_StopBits_1; xUART1_Init.UART_Parity = UART_Parity_No; xUART1_Init.UART_BaudRate = ulWantedBaud; xUART1_Init.UART_HardwareFlowControl = UART_HardwareFlowControl_None; xUART1_Init.UART_Mode = UART_Mode_Tx_Rx; xUART1_Init.UART_FIFO = UART_FIFO_Enable; /* Enable the UART1 Clock */ SCU_APBPeriphClockConfig( __UART1, ENABLE ); /* Enable the GPIO3 Clock */ SCU_APBPeriphClockConfig( __GPIO3, ENABLE ); /* Configure UART1_Rx pin GPIO3.2 */ GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ; GPIO_Init( GPIO3, &GPIO_InitStructure ); /* Configure UART1_Tx pin GPIO3.3 */ GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ; GPIO_Init( GPIO3, &GPIO_InitStructure ); portENTER_CRITICAL(); { /* Configure the UART itself. */ UART_DeInit( UART1 ); UART_Init( UART1, &xUART1_Init ); UART_ITConfig( UART1, UART_IT_Receive | UART_IT_Transmit, ENABLE ); UART1->ICR = serCLEAR_ALL_INTERRUPTS; UART_LoopBackConfig( UART1, DISABLE ); UART_IrDACmd( IrDA1, DISABLE ); /* Configure the VIC for the UART interrupts. */ VIC_Config( UART1_ITLine, VIC_IRQ, 9 ); VIC_ITCmd( UART1_ITLine, ENABLE ); UART_Cmd( UART1, ENABLE ); lTaskWaiting = pdFALSE; } portEXIT_CRITICAL(); } else { xReturn = ( xComPortHandle ) 0; } /* This demo file only supports a single port but we have to return something to comply with the standard demo header file. */ return xReturn; }
void f3d_gyro_interface_init() { GPIO_InitTypeDef GPIO_InitStructure; /////////////////////////////////////////////////////// RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //SCK Pin PA5 GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_5); //MOSI Pin PA6 GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_5); //MISO Pin PA7 GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_5); ////////////////////////////////////////////////////// RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOE,&GPIO_InitStructure); GPIO_SetBits(GPIOE, GPIO_Pin_3); //////////////////////////////////////////////////// RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPI1, &SPI_InitStructure); SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF); SPI_Cmd(SPI1, ENABLE); }
/** * @brief Configures LED GPIO. * @param xLed Specifies the Led to be configured. * This parameter can be one of following parameters: * @arg LED1 * @arg LED2 * @retval None. */ void SdkEvalLedInit(SdkEvalLed xLed) { /* Configure the GPIO_LED pin */ GPIO_Init(vectpxGpioPort[xLed], s_vectnGpioPin[xLed], GPIO_Mode_Out_PP_High_Fast); }
int main(void) { uint8_t ch; /* Enable GPIO clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* Enable USART3 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_RESET); GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_RESET); /* Configure USART3 Rx & Tx as alternate function */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOC, &GPIO_InitStructure); /* GPIOC --> USART3 Rx & Tx */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_9b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* USART configuration */ USART_Init(USART3, &USART_InitStructure); /* Enable USART */ USART_Cmd(USART3, ENABLE); do { USART_ReceiveData(USART3); } while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET); while(1) { /* wait for character to arrive */ while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET) ; GPIO_TOGGLE(GPIOB,GPIO_Pin_6); /* read the available data */ ch = USART_ReceiveData(USART3); ch++; /* Wait while TX is full */ while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET) ; /*transmit data only when TX is empty */ USART_SendData(USART3, ch); GPIO_TOGGLE(GPIOB, GPIO_Pin_7); } }
OSStatus internal_uart_init( mico_uart_t uart, const mico_uart_config_t* config, ring_buffer_t* optional_rx_buffer ) { GPIO_InitTypeDef gpio_init_structure; USART_InitTypeDef usart_init_structure; NVIC_InitTypeDef nvic_init_structure; DMA_InitTypeDef dma_init_structure; #ifndef NO_MICO_RTOS mico_rtos_init_semaphore(&uart_interfaces[uart].tx_complete, 1); mico_rtos_init_semaphore(&uart_interfaces[uart].rx_complete, 1); #else uart_interfaces[uart].tx_complete = false; uart_interfaces[uart].rx_complete = false; #endif MicoMcuPowerSaveConfig(false); /* Enable GPIO peripheral clocks for TX and RX pins */ RCC_AHB1PeriphClockCmd( uart_mapping[uart].pin_rx->peripheral_clock | uart_mapping[uart].pin_tx->peripheral_clock, ENABLE ); /* Configure USART TX Pin */ gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << uart_mapping[uart].pin_tx->number ); gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_PP; gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( uart_mapping[uart].pin_tx->bank, &gpio_init_structure ); GPIO_PinAFConfig( uart_mapping[uart].pin_tx->bank, uart_mapping[uart].pin_tx->number, uart_mapping[uart].gpio_af ); /* Configure USART RX Pin */ gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << uart_mapping[uart].pin_rx->number ); gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_OD; gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init( uart_mapping[uart].pin_rx->bank, &gpio_init_structure ); GPIO_PinAFConfig( uart_mapping[uart].pin_rx->bank, uart_mapping[uart].pin_rx->number, uart_mapping[uart].gpio_af ); #ifndef NO_MICO_RTOS if(config->flags & UART_WAKEUP_ENABLE){ current_uart = uart; mico_rtos_init_semaphore( &uart_interfaces[uart].sem_wakeup, 1 ); mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "UART_WAKEUP", thread_wakeup, 0x100, ¤t_uart); } #endif /* Check if any of the flow control is enabled */ if ( uart_mapping[uart].pin_cts && (config->flow_control == FLOW_CONTROL_CTS || config->flow_control == FLOW_CONTROL_CTS_RTS) ) { /* Enable peripheral clock */ RCC_AHB1PeriphClockCmd( uart_mapping[uart].pin_cts->peripheral_clock, ENABLE ); /* Configure CTS Pin */ gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << uart_mapping[uart].pin_cts->number ); gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_OD; gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init( uart_mapping[uart].pin_cts->bank, &gpio_init_structure ); GPIO_PinAFConfig( uart_mapping[uart].pin_cts->bank, uart_mapping[uart].pin_cts->number, uart_mapping[uart].gpio_af ); } if ( uart_mapping[uart].pin_cts && (config->flow_control == FLOW_CONTROL_RTS || config->flow_control == FLOW_CONTROL_CTS_RTS) ) { /* Enable peripheral clock */ RCC_AHB1PeriphClockCmd( uart_mapping[uart].pin_rts->peripheral_clock, ENABLE ); /* Configure RTS Pin */ gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << uart_mapping[uart].pin_rts->number ); gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_OD; gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init( uart_mapping[uart].pin_rts->bank, &gpio_init_structure ); GPIO_PinAFConfig( uart_mapping[uart].pin_rts->bank, uart_mapping[uart].pin_rts->number, uart_mapping[uart].gpio_af ); } /* Enable UART peripheral clock */ uart_mapping[uart].usart_peripheral_clock_func( uart_mapping[uart].usart_peripheral_clock, ENABLE ); /************************************************************************** * Initialise STM32 USART registers * NOTE: * - Both transmitter and receiver are disabled until usart_enable_transmitter/receiver is called. * - Only 1 and 2 stop bits are implemented at the moment. **************************************************************************/ usart_init_structure.USART_Mode = 0; usart_init_structure.USART_BaudRate = config->baud_rate; usart_init_structure.USART_WordLength = ( ( config->data_width == DATA_WIDTH_9BIT ) || ( ( config->data_width == DATA_WIDTH_8BIT ) && ( config->parity != NO_PARITY ) ) ) ? USART_WordLength_9b : USART_WordLength_8b; usart_init_structure.USART_StopBits = ( config->stop_bits == STOP_BITS_1 ) ? USART_StopBits_1 : USART_StopBits_2; switch ( config->parity ) { case NO_PARITY: usart_init_structure.USART_Parity = USART_Parity_No; break; case EVEN_PARITY: usart_init_structure.USART_Parity = USART_Parity_Even; break; case ODD_PARITY: usart_init_structure.USART_Parity = USART_Parity_Odd; break; default: return kParamErr; } switch ( config->flow_control ) { case FLOW_CONTROL_DISABLED: usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; break; case FLOW_CONTROL_CTS: usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS; break; case FLOW_CONTROL_RTS: usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS; break; case FLOW_CONTROL_CTS_RTS: usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; break; default: return kParamErr; } /* Initialise USART peripheral */ USART_Init( uart_mapping[uart].usart, &usart_init_structure ); /************************************************************************** * Initialise STM32 DMA registers * Note: If DMA is used, USART interrupt isn't enabled. **************************************************************************/ /* Enable DMA peripheral clock */ uart_mapping[uart].tx_dma_peripheral_clock_func( uart_mapping[uart].tx_dma_peripheral_clock, ENABLE ); uart_mapping[uart].rx_dma_peripheral_clock_func( uart_mapping[uart].rx_dma_peripheral_clock, ENABLE ); /* Fill init structure with common DMA settings */ dma_init_structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Enable; dma_init_structure.DMA_Priority = DMA_Priority_VeryHigh; dma_init_structure.DMA_FIFOMode = DMA_FIFOMode_Disable; dma_init_structure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; dma_init_structure.DMA_MemoryBurst = DMA_MemoryBurst_Single; dma_init_structure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; if ( config->data_width == DATA_WIDTH_9BIT ) { dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; } else { dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; } /* Initialise TX DMA */ DMA_DeInit( uart_mapping[uart].tx_dma_stream ); dma_init_structure.DMA_Channel = uart_mapping[uart].tx_dma_channel; dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &uart_mapping[uart].usart->DR; dma_init_structure.DMA_Memory0BaseAddr = (uint32_t) 0; dma_init_structure.DMA_DIR = DMA_DIR_MemoryToPeripheral; dma_init_structure.DMA_BufferSize = 0; dma_init_structure.DMA_Mode = DMA_Mode_Normal; DMA_Init( uart_mapping[uart].tx_dma_stream, &dma_init_structure ); /* Initialise RX DMA */ DMA_DeInit( uart_mapping[uart].rx_dma_stream ); dma_init_structure.DMA_Channel = uart_mapping[uart].rx_dma_channel; dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &uart_mapping[uart].usart->DR; dma_init_structure.DMA_Memory0BaseAddr = 0; dma_init_structure.DMA_DIR = DMA_DIR_PeripheralToMemory; dma_init_structure.DMA_BufferSize = 0; dma_init_structure.DMA_Mode = DMA_Mode_Normal; DMA_Init( uart_mapping[uart].rx_dma_stream, &dma_init_structure ); /************************************************************************** * Initialise STM32 DMA interrupts * Note: Only TX DMA interrupt is enabled. **************************************************************************/ /* Configure TX DMA interrupt on Cortex-M3 */ nvic_init_structure.NVIC_IRQChannel = uart_mapping[uart].tx_dma_irq; nvic_init_structure.NVIC_IRQChannelPreemptionPriority = (uint8_t) 0x5; nvic_init_structure.NVIC_IRQChannelSubPriority = 0x8; nvic_init_structure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &nvic_init_structure ); /* Enable TC (transfer complete) and TE (transfer error) interrupts on source */ DMA_ITConfig( uart_mapping[uart].tx_dma_stream, DMA_IT_TC | DMA_IT_TE | DMA_IT_DME | DMA_IT_FE, ENABLE ); /* Enable USART's RX DMA interfaces */ USART_DMACmd( uart_mapping[uart].usart, USART_DMAReq_Rx, ENABLE ); /************************************************************************** * Initialise STM32 USART interrupt **************************************************************************/ nvic_init_structure.NVIC_IRQChannel = uart_mapping[uart].usart_irq; nvic_init_structure.NVIC_IRQChannelPreemptionPriority = (uint8_t) 0x6; nvic_init_structure.NVIC_IRQChannelSubPriority = 0x7; nvic_init_structure.NVIC_IRQChannelCmd = ENABLE; /* Enable USART interrupt vector in Cortex-M3 */ NVIC_Init( &nvic_init_structure ); /* Enable USART */ USART_Cmd( uart_mapping[uart].usart, ENABLE ); /* Enable both transmit and receive */ uart_mapping[uart].usart->CR1 |= USART_CR1_TE; uart_mapping[uart].usart->CR1 |= USART_CR1_RE; /* Setup ring buffer */ if (optional_rx_buffer != NULL) { /* Note that the ring_buffer should've been initialised first */ uart_interfaces[uart].rx_buffer = optional_rx_buffer; uart_interfaces[uart].rx_size = 0; platform_uart_receive_bytes( uart, optional_rx_buffer->buffer, optional_rx_buffer->size, 0 ); } else { /* Not using ring buffer. Configure RX DMA interrupt on Cortex-M3 */ nvic_init_structure.NVIC_IRQChannel = uart_mapping[uart].rx_dma_irq; nvic_init_structure.NVIC_IRQChannelPreemptionPriority = (uint8_t) 0x5; nvic_init_structure.NVIC_IRQChannelSubPriority = 0x8; nvic_init_structure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &nvic_init_structure ); /* Enable TC (transfer complete) and TE (transfer error) interrupts on source */ DMA_ITConfig( uart_mapping[uart].rx_dma_stream, DMA_IT_TC | DMA_IT_TE | DMA_IT_DME | DMA_IT_FE, ENABLE ); } MicoMcuPowerSaveConfig(true); return kNoErr; }
/** * @brief ADC configuration * @note This function Configure the ADC peripheral 1) Enable peripheral clocks 2) Configure ADC Channel 12 pin as analog input 3) DMA2_Stream0 channel2 configuration 4) Configure ADC1 Channel 12 5) Configure ADC2 Channel 12 6) Configure ADC3 Channel 12 * @param None * @retval None */ static void ADC_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; /* Enable peripheral clocks *************************************************/ RCC_AHB1PeriphClockCmd( ADC1_2_CHANNEL_GPIO_CLK , ENABLE); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_DMA2 , ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 , ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC2 , ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC3 , ENABLE); /* Configure ADC Channel 12 pin as analog input *****************************/ GPIO_InitStructure.GPIO_Pin = GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIO_PORT, &GPIO_InitStructure); /* DMA2 Stream0 channel0 configuration **************************************/ DMA_InitStructure.DMA_Channel = DMA_CHANNELx; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC_CDR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&aADCTripleConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 3; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA_STREAMx, &DMA_InitStructure); /* DMA2_Stream0 enable */ DMA_Cmd(DMA_STREAMx, ENABLE); /* ADC Common configuration *************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_TripleMode_Interl; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 regular channel 12 configuration ************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_CHANNEL, 1, ADC_SampleTime_3Cycles); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* ADC2 regular channel 12 configuration ************************************/ ADC_Init(ADC2, &ADC_InitStructure); /* ADC2 regular channel12 configuration */ ADC_RegularChannelConfig(ADC2, ADC_CHANNEL, 1, ADC_SampleTime_3Cycles); /* ADC3 regular channel 12 configuration ************************************/ ADC_Init(ADC3, &ADC_InitStructure); /* ADC3 regular channel12 configuration */ ADC_RegularChannelConfig(ADC3, ADC_CHANNEL, 1, ADC_SampleTime_3Cycles); /* Enable DMA request after last transfer (multi-ADC mode) ******************/ ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE); /* Enable ADC1 **************************************************************/ ADC_Cmd(ADC1, ENABLE); /* Enable ADC2 **************************************************************/ ADC_Cmd(ADC2, ENABLE); /* Enable ADC3 **************************************************************/ ADC_Cmd(ADC3, ENABLE); }
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) { #ifdef USE_STM3210C_EVAL RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE) ; #else // USE_STM322xG_EVAL GPIO_InitTypeDef GPIO_InitStructure; #ifdef USE_USB_OTG_FS RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE); /* Configure SOF ID DM DP Pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ; /* Configure VBUS Pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure ID pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; #else // USE_USB_OTG_HS #ifdef USE_ULPI_PHY // ULPI RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH | RCC_AHB1Periph_GPIOI, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // D0 GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // CLK GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // D1 GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_OTG2_HS) ; // D2 GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // D7 GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_HS) ; // D3 GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // D4 GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG2_HS) ; // D5 GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_HS) ; // D6 GPIO_PinAFConfig(GPIOH,GPIO_PinSource4, GPIO_AF_OTG2_HS) ; // NXT GPIO_PinAFConfig(GPIOI,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // DIR GPIO_PinAFConfig(GPIOC,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // STP // CLK GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &GPIO_InitStructure); // D0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); // D1 D2 D3 D4 D5 D6 D7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_10 | GPIO_Pin_11| GPIO_Pin_12 | GPIO_Pin_13 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); // STP GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOC, &GPIO_InitStructure); //NXT GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOH, &GPIO_InitStructure); //DIR GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOI, &GPIO_InitStructure); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS | RCC_AHB1Periph_OTG_HS_ULPI, ENABLE) ; #else RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_Init(GPIOB, &GPIO_InitStructure); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; #endif #endif //USB_OTG_HS #endif //USE_STM322xG_EVAL }
void LCD_SC_DeInit(void) { GPIO_Init(GPIO_PORT_COM0, GPIO_PIN_COM0, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_COM1, GPIO_PIN_COM1, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_COM2, GPIO_PIN_COM2, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_COM3, GPIO_PIN_COM3, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S0, GPIO_PIN_S0, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S1, GPIO_PIN_S1, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S2, GPIO_PIN_S2, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S3, GPIO_PIN_S3, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S4, GPIO_PIN_S4, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S5, GPIO_PIN_S5, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S6, GPIO_PIN_S6, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S7, GPIO_PIN_S7, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S8, GPIO_PIN_S8, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S9, GPIO_PIN_S9, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S10, GPIO_PIN_S10, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S11, GPIO_PIN_S11, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S12, GPIO_PIN_S12, GPIO_Mode_Out_PP_Low_Fast); GPIO_Init(GPIO_PORT_S13, GPIO_PIN_S13, GPIO_Mode_Out_PP_Low_Fast); }
static void I2C_Initialize1() { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(I2C1_DMA_CLK, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_AHB1PeriphClockCmd(I2C1_GPIO_CLK, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2C1_OWN_ADDRESS; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 100000; I2C_Cmd(I2C1, ENABLE); I2C_Init(I2C1, &I2C_InitStructure); /* Initialize I2C1 on PB8 and PB9 */ GPIO_PinAFConfig(I2C1_SCL_PORT, I2C1_SCL_PIN_SOURCE, GPIO_AF_I2C1); GPIO_PinAFConfig(I2C1_SDA_PORT, I2C1_SDA_PIN_SOURCE, GPIO_AF_I2C1); GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN; GPIO_Init(GPIOB, &GPIO_InitStructure); DMA_ClearFlag(I2C1_DMA_STREAM_RX, I2C1_DMA_RX_FLAG_FEIF | I2C1_DMA_RX_FLAG_DMEIF | I2C1_DMA_RX_FLAG_TEIF | I2C1_DMA_RX_FLAG_HTIF | I2C1_DMA_RX_FLAG_TCIF); DMA_Cmd(I2C1_DMA_STREAM_RX, DISABLE); DMA_DeInit(I2C1_DMA_STREAM_RX); DMA_StructInit(&I2C1_DMA_InitStructure); I2C1_DMA_InitStructure.DMA_Channel = I2C1_DMA_CHANNEL_RX; I2C1_DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(I2C1->DR); I2C1_DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0; /* This parameter will be configured durig communication */; I2C1_DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /* This parameter will be configured durig communication */ I2C1_DMA_InitStructure.DMA_BufferSize = 0xFFFF; /* This parameter will be configured durig communication */ I2C1_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; I2C1_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; I2C1_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; I2C1_DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; I2C1_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; I2C1_DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; I2C1_DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; I2C1_DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; I2C1_DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; I2C1_DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(I2C1_DMA_STREAM_RX, &I2C1_DMA_InitStructure); DMA_ClearFlag(I2C1_DMA_STREAM_TX, I2C1_DMA_TX_FLAG_FEIF | I2C1_DMA_TX_FLAG_DMEIF | I2C1_DMA_TX_FLAG_TEIF | I2C1_DMA_TX_FLAG_HTIF | I2C1_DMA_TX_FLAG_TCIF); DMA_Cmd(I2C1_DMA_STREAM_TX, DISABLE); DMA_DeInit(I2C1_DMA_STREAM_TX); I2C1_DMA_InitStructure.DMA_Channel = I2C1_DMA_CHANNEL_TX; DMA_Init(I2C1_DMA_STREAM_TX, &I2C1_DMA_InitStructure); /* Setup interrupts */ NVIC_InitStructure.NVIC_IRQChannel = I2C1_DMA_RX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2C1_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2C1_DMA_SUBPRIO; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = I2C1_DMA_TX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2C1_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2C1_DMA_SUBPRIO; NVIC_Init(&NVIC_InitStructure); /* Enable TC Interrupts */ DMA_ITConfig(I2C1_DMA_STREAM_TX, DMA_IT_TC, ENABLE); DMA_ITConfig(I2C1_DMA_STREAM_RX, DMA_IT_TC, ENABLE); I2C1_DMA_RX_Semaphore = 0; I2C1_DMA_TX_Semaphore = 0; }
void CONF_PWMIN(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* GPIOB clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* TIM2 chennel2 configuration : PA.01 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Connect TIM pin to AF1 */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_2); TIM_TimeBaseStructure.TIM_Prescaler = 360; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* Enable the TIM2 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); ////// TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM3, &TIM_ICInitStructure); /* Select the TIM2 Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); ////TIM4 /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); /* GPIOB clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE); /* TIM2 chennel2 configuration : PA.01 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Connect TIM pin to AF1 */ GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_2); TIM_TimeBaseStructure.TIM_Prescaler = 360; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /* Enable the TIM2 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); ////// TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM4, &TIM_ICInitStructure); /* Select the TIM2 Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset); TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM4, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE); ////TIM8 /* TIM2 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); /* GPIOB clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* TIM2 chennel2 configuration : PA.01 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Connect TIM pin to AF1 */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_4); TIM_TimeBaseStructure.TIM_Prescaler = 360; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); /* Enable the TIM2 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM8_BRK_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); ////// TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM8, &TIM_ICInitStructure); /* Select the TIM2 Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(TIM8, TIM_TS_TI1FP1); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Reset); TIM_SelectMasterSlaveMode(TIM8,TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM8, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM8, TIM_IT_CC2, ENABLE); }
static void LCD_FSMCConfig(void) { FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; FSMC_NORSRAMTimingInitTypeDef Timing_read,Timing_write; /* FSMC GPIO configure */ { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* FSMC_D0 ~ FSMC_D3 PD14 FSMC_D0 PD15 FSMC_D1 PD0 FSMC_D2 PD1 FSMC_D3 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOD,&GPIO_InitStructure); /* FSMC_D4 ~ FSMC_D12 PE7 ~ PE15 FSMC_D4 ~ FSMC_D12 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOE,&GPIO_InitStructure); /* FSMC_D13 ~ FSMC_D15 PD8 ~ PD10 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; GPIO_Init(GPIOD,&GPIO_InitStructure); /* FSMC_A0 ~ FSMC_A5 FSMC_A6 ~ FSMC_A9 PF0 ~ PF5 PF12 ~ PF15 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOF,&GPIO_InitStructure); /* FSMC_A10 ~ FSMC_A15 PG0 ~ PG5 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5; GPIO_Init(GPIOG,&GPIO_InitStructure); /* FSMC_A16 ~ FSMC_A18 PD11 ~ PD13 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13; GPIO_Init(GPIOD,&GPIO_InitStructure); /* RD-PD4 WR-PD5 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_Init(GPIOD,&GPIO_InitStructure); /* NBL0-PE0 NBL1-PE1 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_Init(GPIOE,&GPIO_InitStructure); /* NE1/NCE2 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOD,&GPIO_InitStructure); /* NE2 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOG,&GPIO_InitStructure); /* NE3 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOG,&GPIO_InitStructure); /* NE4 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_Init(GPIOG,&GPIO_InitStructure); } /* FSMC GPIO configure */ /*-- FSMC Configuration -------------------------------------------------*/ FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &Timing_read; FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &Timing_write; FSMC_NORSRAMStructInit(&FSMC_NORSRAMInitStructure); Timing_read.FSMC_AddressSetupTime = 8; /* 地址建立时间 */ Timing_read.FSMC_AddressHoldTime = 8; /* 地址保持时间 */ Timing_read.FSMC_DataSetupTime = 8; /* 数据建立时间 */ Timing_read.FSMC_AccessMode = FSMC_AccessMode_A; /* FSMC 访问模式 */ Timing_write.FSMC_AddressSetupTime = 8; /* 地址建立时间 */ Timing_write.FSMC_AddressHoldTime = 8; /* 地址保持时间 */ Timing_write.FSMC_DataSetupTime = 8; /* 数据建立时间 */ Timing_write.FSMC_AccessMode = FSMC_AccessMode_A; /* FSMC 访问模式 */ /* Color LCD configuration ------------------------------------ LCD configured as follow: - Data/Address MUX = Disable - Memory Type = SRAM - Data Width = 16bit - Write Operation = Enable - Extended Mode = Enable - Asynchronous Wait = Disable */ FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2; FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM; FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE); }
void CONF_TIMERS(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB| RCC_AHBPeriph_GPIOE, ENABLE); /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_2); GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_2); GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_2); GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_2); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE); TIM_TimeBaseStructure.TIM_Prescaler = 10; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1; TIM_TimeBaseStructure.TIM_Period = 60000; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; TIM_OCInitStructure.TIM_Pulse = 3400; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = 3400; TIM_OC2Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = 3400; TIM_OC3Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = 3400; TIM_OC4Init(TIM1, &TIM_OCInitStructure); TIM_Cmd(TIM1, ENABLE); TIM_CtrlPWMOutputs(TIM1, ENABLE); //////////////////////////////////////////////////////////////////////////////////// /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* GPIOB clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* TIM2 chennel2 configuration : PA.01 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Connect TIM pin to AF1 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_1); TIM_TimeBaseStructure.TIM_Prescaler = 360; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Enable the TIM2 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); ////// TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM2, &TIM_ICInitStructure); /* Select the TIM2 Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM2, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); }
int main(void) { int scope1dt,scope1t,scope1s,scope1d,scope1j; int scope2dt,scope2t,scope2s,scope2d,scope2j; char znakr; char znak; GPIO_InitTypeDef GPIO_str; uint8_t ii; uint8_t i = 0; /* SysTick end of count event each 0,01ms 0,00001*/// 0,01ms -100000 RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100000); /* Accelerometer Configuration */ Acc_Config(); Demo_GyroConfig(); ii = SystemCoreClock; /* This is a way to read the System core clock */ CONF_TIMERS(); CONF_PWMIN(); confI2C(); while(1) I2C_start(); /////////// Przyciski RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); GPIO_str.GPIO_Pin= GPIO_Pin_4 | GPIO_Pin_5; GPIO_str.GPIO_Mode=GPIO_Mode_IN; GPIO_str.GPIO_PuPd=GPIO_PuPd_UP; GPIO_str.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_str); ii = 0; USART2_Init(115200); GPIO_SetBits(GPIOD, GPIO_Pin_0); LEFT =3400; RIGHT=3400; FRONT=3400; REAR=3400; //delay_ms(4000); while (1) { esf=SystemCoreClock/360/(TIM2->CCR2); esd = (TIM2->CCR1*100);///(TIM2->CCR2); throttle=(int)(esd*32.0/156.0-1236.0); esf3=SystemCoreClock/360/(TIM3->CCR2); if(throttle>6600) throttle=3400; esd3 = (TIM3->CCR1*100);///(TIM3->CCR2); pitch_zadany=(float)(-6.0*esd3/1800.0+100.0)+0.0; if(pitch_zadany>40 || pitch_zadany<-40) pitch_zadany=0; esd4 = (TIM4->CCR1*100); roll_zadany=(float)(-6.0*esd4/1800.0+100.0); if(roll_zadany>40 || roll_zadany<-40) roll_zadany=0; //esd5 =(TIM8->CCR2*100)/TIM8->CCR1; //yaw_zadany=(float)(-6.0*esd5/1800.0+100.0); //if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5)==0) //flaga=0; //IMU/////////////////////////////////////////////////////// Acc_ReadData(AccBuffer); for(i=0;i<3;i++) AccBuffer[i] /= 100.0f; acc_x=AccBuffer[0]; acc_y=AccBuffer[1]; acc_z=AccBuffer[2]; Demo_GyroReadAngRate(Buffer); gyr_x=Buffer[0]; gyr_y=Buffer[1]; gyr_z=Buffer[2]; MadgwickAHRSupdateIMU( -gyr_y*0.01745, gyr_x*0.01745, gyr_z*0.01745, acc_x, acc_y, acc_z); roll=180/PI*atan2(2*(q2*q3+q0*q1),1-2*(q1*q1+q2*q2));//(q0*q0-q1*q1-q2*q2+q3*q3)); pitch=180/PI*asin(-2*(q1*q3-q0*q2)); yaw=180/PI*atan2(2*(q1*q2+q0*q3),(1-2*(q3*q3+q2*q2))); scope1=PIDY*1000;//niebieski scope2=yaw*1000;//czerwony if(scope1>0) znak='+'; else znak='-'; if(scope2>0) znakr='+'; else znakr='-'; scope1=abs(scope1); scope2=abs(scope2); CZAS_S=(float)CZAS/100000.0; CZAS=0; scope1dt=scope1/10000; scope1t=scope1/1000-scope1dt*10; scope1s=scope1/100-scope1dt*100-scope1t*10; scope1d=scope1/10-scope1dt*1000-scope1t*100-scope1s*10; scope1j=scope1-scope1dt*10000-scope1t*1000-scope1s*100-scope1d*10; scope2dt=scope2/10000; scope2t=scope2/1000-scope2dt*10; scope2s=scope2/100-scope2dt*100-scope2t*10; scope2d=scope2/10-scope2dt*1000-scope2t*100-scope2s*10; scope2j=scope2-scope2dt*10000-scope2t*1000-scope2s*100-scope2d*10; printf("%c%d%d%d%d%d %c%d%d%d%d%d\r\n",znak,scope1dt,scope1t,scope1s,scope1d,scope1j,znakr,scope2dt,scope2t,scope2s,scope2d,scope2j); /*PIDY=-PIDyawrate(yaw_zadany, yaw); rollratezadane=PIDroll(roll_zadany, roll); PIDR=PIDrollrate(rollratezadane, -gyr_y); speedLeft=(int)((float)throttle+PIDR-PIDY);//throttle+PIDR; speedRight=(int)((float)throttle-PIDR-PIDY); pitchratezadane=PIDpitch(pitch_zadany, pitch); PIDP=PIDpitchrate(pitchratezadane, gyr_x);//float PIDpitchrollrate(float zadana, float aktualna) speedFront=(int)((float)throttle-PIDP+PIDY); speedRear=(int)((float)throttle+PIDP+PIDY); */ if(speedFront>speedmax) speedFront=speedmax; else if(speedFront<speedmin) speedFront=speedmin; else ; if(speedRear>speedmax) speedRear=speedmax; else if(speedRear<speedmin) speedRear=speedmin; else ; if(speedLeft>speedmax) speedLeft=speedmax; else if(speedLeft<speedmin) speedLeft=speedmin; else ; if(speedRight>speedmax) speedRight=speedmax; else if(speedRight<speedmin) speedRight=speedmin; else ; if(throttle>3450) { LEFT =(int)speedLeft; RIGHT=(int)speedRight; FRONT=3400;//(int)speedFront; REAR=3400;//(int)speedRear; } else { LEFT =3400; RIGHT=3400; FRONT=3400; REAR=3400; } } return 0; }
void main(void) { //CFG->GCR |= 0x01; //disable swim pin /* Configure the Fcpu to DIV1*/ CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1); /* select Clock = 16 MHz */ CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1); /* Configure the system clock to use HSI clock source and to run at 16Mhz */ CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE); CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); CLK_HSICmd(ENABLE); FLASH_Config(); EXTI_DeInit(); //You should add: // Define FLASH programming time //FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD); //optbyte1 = FLASH_ReadOptionByte(0x4803); //255 by def //optbyte2 = FLASH_ReadOptionByte(0x4804); //255 by def /* if (optbyte != 765) { FLASH_Unlock(FLASH_MEMTYPE_DATA); // unlock data memory by passing the RASS key in the proper order FLASH_ProgramOptionByte(0x4803, 0xFD); // byte OPT2 resides at address x4803, write a 1 to bit 7. This will also write to the NOPT2 complement byte FLASH_Lock(FLASH_MEMTYPE_DATA); // re-lock data memory } */ //General purpose timer TIM4_Config(); #ifdef DFS_90 HotAir_Config(); #endif #ifndef DFS_90 GPIO_Init(CONTROL_GPIO_PORT, CONTROL_GPIO_PIN, GPIO_MODE_OUT_PP_LOW_FAST); #endif Temperature_ADC_Config(); STM_EVAL_SEGInit(SEG1); STM_EVAL_SEGInit(SEG2); STM_EVAL_SEGInit(SEG3); STM_EVAL_LEDInit(LEDA); STM_EVAL_LEDInit(LEDB); STM_EVAL_LEDInit(LEDC); STM_EVAL_LEDInit(LEDD); STM_EVAL_LEDInit(LEDE); STM_EVAL_LEDInit(LEDF); STM_EVAL_LEDInit(LEDG); STM_EVAL_LEDInit(LEDP); #ifndef SOLDERING_VAR2 //STM_EVAL_LEDInit(LEDP); #endif // STM_EVAL_SEGOn(SEG1); // STM_EVAL_SEGOn(SEG2); // STM_EVAL_SEGOn(SEG3); // // STM_EVAL_LEDOn(LEDA); // STM_EVAL_LEDOn(LEDB); // STM_EVAL_LEDOn(LEDC); // STM_EVAL_LEDOn(LEDD); // STM_EVAL_LEDOn(LEDE); // STM_EVAL_LEDOn(LEDF); // STM_EVAL_LEDOn(LEDG); // // STM_EVAL_LEDOff(LEDA); // STM_EVAL_LEDOff(LEDB); // STM_EVAL_LEDOff(LEDC); // STM_EVAL_LEDOff(LEDD); // STM_EVAL_LEDOff(LEDE); // STM_EVAL_LEDOff(LEDF); // STM_EVAL_LEDOff(LEDG); // STM_EVAL_LEDOff(LEDP); // // STM_EVAL_SEGOff(SEG1); // STM_EVAL_SEGOff(SEG2); // STM_EVAL_SEGOff(SEG3); //STM_EVAL_LEDOn(LEDP); //GPIO_Init(ENC_DN_BUTTON_PORT, ENC_DN_BUTTON_PIN, GPIO_MODE_IN_PU_NO_IT); //GPIO_Init(ENC_UP_BUTTON_PORT, ENC_UP_BUTTON_PIN, GPIO_MODE_IN_PU_NO_IT); STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_REED, BUTTON_MODE_GPIO); ssegInit(); ssegWriteStr("1.0.1", 5, SEG1); enableInterrupts(); #ifdef DFS_90 HotAir_Main(); #endif #ifdef SOLDERING_HAKKO_A1321 Soldering_Main(); #endif while(1) { /* #if 0 for (level = 0; level < calUpperLimit; level +=5) { Delayms(1); } for (level = calUpperLimit; level > 0; level -=5) { Delayms(1); } #else RawData = GetAdcValue(ADC_SOLDER_TEMP_CHANNEL); SmoothData = SmoothData - (LPF_Beta * (SmoothData - RawData)); level = ((int)SmoothData)*26; #endif */ } }
void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /*************************************/ /*!< SD_SPI_CS_GPIO, SD_SPI_MOSI_GPIO, SD_SPI_MISO_GPIO, SD_SPI_DETECT_GPIO and SD_SPI_SCK_GPIO Periph clock enable */ RCC_AHBPeriphClockCmd(SD_CS_GPIO_CLK | SD_SPI_MOSI_GPIO_CLK | SD_SPI_MISO_GPIO_CLK | SD_SPI_SCK_GPIO_CLK | SD_DETECT_GPIO_CLK, ENABLE); /*!< SD_SPI Periph clock enable */ RCC_APB2PeriphClockCmd(SD_SPI_CLK, ENABLE); /*!< Configure SD_SPI pins: SCK */ GPIO_InitStructure.GPIO_Pin = SD_SPI_SCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI pins: MISO */ GPIO_InitStructure.GPIO_Pin = SD_SPI_MISO_PIN; GPIO_Init(SD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI pins: MOSI */ GPIO_InitStructure.GPIO_Pin = SD_SPI_MOSI_PIN; GPIO_Init(SD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI_CS_PIN pin: SD Card CS pin */ GPIO_InitStructure.GPIO_Pin = SD_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */ GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure); /* Connect PXx to SD_SPI_SCK */ GPIO_PinAFConfig(SD_SPI_SCK_GPIO_PORT, SD_SPI_SCK_SOURCE, SD_SPI_SCK_AF); /* Connect PXx to SD_SPI_MISO */ GPIO_PinAFConfig(SD_SPI_MISO_GPIO_PORT, SD_SPI_MISO_SOURCE, SD_SPI_MISO_AF); /* Connect PXx to SD_SPI_MOSI */ GPIO_PinAFConfig(SD_SPI_MOSI_GPIO_PORT, SD_SPI_MOSI_SOURCE, SD_SPI_MOSI_AF); /**************************************/ /* MCO config */ //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Output clock on MCO pin ---------------------------------------------*/ /* GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); // pick one of the clocks to spew //RCC_MCOConfig(RCC_MCOSource_SYSCLK); // Put on MCO pin the: System clock selected RCC_MCOConfig(RCC_MCOSource_PLLCLK,RCC_MCODiv_1); */ }
/*..........................................................................*/ uint8_t QS_onStartup(void const *arg) { static uint8_t qsBuf[QS_BUF_SIZE]; /* buffer for Quantum Spy */ QS_initBuf(qsBuf, sizeof(qsBuf)); /* enable USART2 and GPIOA/AFIO clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); /* configure GPIOD.5 as push-pull... */ GPIO_InitTypeDef gpio_init; gpio_init.GPIO_Pin = GPIO_Pin_5; gpio_init.GPIO_Speed = GPIO_Speed_50MHz; gpio_init.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &gpio_init); /* configure GPIOD.6 as input floating... */ gpio_init.GPIO_Pin = GPIO_Pin_6; gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &gpio_init); USART_InitTypeDef usart_init; usart_init.USART_BaudRate = QS_BAUD_RATE; usart_init.USART_WordLength = USART_WordLength_8b; usart_init.USART_StopBits = USART_StopBits_1; usart_init.USART_Parity = USART_Parity_No ; usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usart_init.USART_Mode = USART_Mode_Tx; USART_Init(USART2, &usart_init); USART_ClockInitTypeDef usart_clk_init; usart_clk_init.USART_Clock = USART_Clock_Disable; usart_clk_init.USART_CPOL = USART_CPOL_Low; usart_clk_init.USART_CPHA = USART_CPHA_2Edge; usart_clk_init.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART2, &usart_clk_init); USART_Cmd(USART2, ENABLE); /* enable USART2 */ QS_tickPeriod_ = (QSTimeCtr)(SystemFrequency_SysClk / BSP_TICKS_PER_SEC); QS_tickTime_ = QS_tickPeriod_; /* to start the timestamp at zero */ /* setup the QS filters... */ QS_FILTER_ON(QS_ALL_RECORDS); // QS_FILTER_OFF(QS_QEP_STATE_EMPTY); // QS_FILTER_OFF(QS_QEP_STATE_ENTRY); // QS_FILTER_OFF(QS_QEP_STATE_EXIT); // QS_FILTER_OFF(QS_QEP_STATE_INIT); // QS_FILTER_OFF(QS_QEP_INIT_TRAN); // QS_FILTER_OFF(QS_QEP_INTERN_TRAN); // QS_FILTER_OFF(QS_QEP_TRAN); // QS_FILTER_OFF(QS_QEP_IGNORED); QS_FILTER_OFF(QS_QF_ACTIVE_ADD); QS_FILTER_OFF(QS_QF_ACTIVE_REMOVE); QS_FILTER_OFF(QS_QF_ACTIVE_SUBSCRIBE); QS_FILTER_OFF(QS_QF_ACTIVE_UNSUBSCRIBE); QS_FILTER_OFF(QS_QF_ACTIVE_POST_FIFO); QS_FILTER_OFF(QS_QF_ACTIVE_POST_LIFO); QS_FILTER_OFF(QS_QF_ACTIVE_GET); QS_FILTER_OFF(QS_QF_ACTIVE_GET_LAST); QS_FILTER_OFF(QS_QF_EQUEUE_INIT); QS_FILTER_OFF(QS_QF_EQUEUE_POST_FIFO); QS_FILTER_OFF(QS_QF_EQUEUE_POST_LIFO); QS_FILTER_OFF(QS_QF_EQUEUE_GET); QS_FILTER_OFF(QS_QF_EQUEUE_GET_LAST); QS_FILTER_OFF(QS_QF_MPOOL_INIT); QS_FILTER_OFF(QS_QF_MPOOL_GET); QS_FILTER_OFF(QS_QF_MPOOL_PUT); QS_FILTER_OFF(QS_QF_PUBLISH); QS_FILTER_OFF(QS_QF_NEW); QS_FILTER_OFF(QS_QF_GC_ATTEMPT); QS_FILTER_OFF(QS_QF_GC); // QS_FILTER_OFF(QS_QF_TICK); QS_FILTER_OFF(QS_QF_TIMEEVT_ARM); QS_FILTER_OFF(QS_QF_TIMEEVT_AUTO_DISARM); QS_FILTER_OFF(QS_QF_TIMEEVT_DISARM_ATTEMPT); QS_FILTER_OFF(QS_QF_TIMEEVT_DISARM); QS_FILTER_OFF(QS_QF_TIMEEVT_REARM); QS_FILTER_OFF(QS_QF_TIMEEVT_POST); QS_FILTER_OFF(QS_QF_CRIT_ENTRY); QS_FILTER_OFF(QS_QF_CRIT_EXIT); QS_FILTER_OFF(QS_QF_ISR_ENTRY); QS_FILTER_OFF(QS_QF_ISR_EXIT); // QS_FILTER_OFF(QS_QK_MUTEX_LOCK); // QS_FILTER_OFF(QS_QK_MUTEX_UNLOCK); QS_FILTER_OFF(QS_QK_SCHEDULE); return (uint8_t)1; /* return success */ }
/** * @brief Configures the FSMC and GPIOs to interface with the NAND memory. * This function must be called before any write/read operation on the * NAND. * @param None * @retval None */ void NAND_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; FSMC_NAND_PCCARDTimingInitTypeDef p; FSMC_NANDInitTypeDef FSMC_NANDInitStructure; /*FSMC总线使用的GPIO组时钟使能*/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF, ENABLE); //RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); /*FSMC CLE, ALE, D0->D3, NOE, NWE and NCE2初始化,推挽复用输出*/ /* GPIOD configuration */ GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FSMC); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOD, &GPIO_InitStructure); /* GPIOE configuration */ GPIO_PinAFConfig(GPIOE, GPIO_PinSource0 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource1 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource2 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource3 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource4 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource5 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource6 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource7 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource8 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource9 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource10 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource11 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource12 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource13 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource14 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource15 , GPIO_AF_FSMC); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11| GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOE, &GPIO_InitStructure); /* GPIOF configuration */ GPIO_PinAFConfig(GPIOF, GPIO_PinSource0 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource1 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource2 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource3 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource4 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource5 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource12 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource13 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource14 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource15 , GPIO_AF_FSMC); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOF, &GPIO_InitStructure); /* GPIOG configuration */ GPIO_PinAFConfig(GPIOG, GPIO_PinSource0 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource1 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource2 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource3 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource4 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource5 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource9 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource10 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource12 , GPIO_AF_FSMC); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_12 ; GPIO_Init(GPIOG, &GPIO_InitStructure); /*--------------FSMC 总线 存储器参数配置------------------------------*/ p.FSMC_SetupTime = 0x0; //建立时间 p.FSMC_WaitSetupTime = 0x4; //等待时间 p.FSMC_HoldSetupTime = 0x2; //保持时间 p.FSMC_HiZSetupTime = 0x0; //高阻建立时间 FSMC_NANDInitStructure.FSMC_Bank = FSMC_Bank2_NAND; //使用FSMC BANK2 FSMC_NANDInitStructure.FSMC_Waitfeature = FSMC_Waitfeature_Enable; //使能FSMC的等待功能 FSMC_NANDInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b; //NAND Flash的数据宽度为8位 FSMC_NANDInitStructure.FSMC_ECC = FSMC_ECC_Enable; //使能ECC特性 FSMC_NANDInitStructure.FSMC_ECCPageSize = FSMC_ECCPageSize_2048Bytes; //ECC页大小2048 FSMC_NANDInitStructure.FSMC_TCLRSetupTime = 0x00; FSMC_NANDInitStructure.FSMC_TARSetupTime = 0x00; FSMC_NANDInitStructure.FSMC_CommonSpaceTimingStruct = &p; FSMC_NANDInitStructure.FSMC_AttributeSpaceTimingStruct = &p; FSMC_NANDInit(&FSMC_NANDInitStructure); /*!使能FSMC BANK2 */ FSMC_NANDCmd(FSMC_Bank2_NAND, ENABLE); }
void PIOS_Board_Init(void) { /* Delay system */ PIOS_DELAY_Init(); const struct pios_board_info *bdinfo = &pios_board_info_blob; #if defined(PIOS_INCLUDE_LED) const struct pios_led_cfg *led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(bdinfo->board_rev); PIOS_Assert(led_cfg); PIOS_LED_Init(led_cfg); #endif /* PIOS_INCLUDE_LED */ /* Set up the SPI interface to the gyro/acelerometer */ if (PIOS_SPI_Init(&pios_spi_gyro_id, &pios_spi_gyro_cfg)) { PIOS_DEBUG_Assert(0); } /* Set up the SPI interface to the flash and rfm22b */ if (PIOS_SPI_Init(&pios_spi_telem_flash_id, &pios_spi_telem_flash_cfg)) { PIOS_DEBUG_Assert(0); } #if defined(PIOS_INCLUDE_FLASH) /* Connect flash to the appropriate interface and configure it */ uintptr_t flash_id; // Initialize the external USER flash if (PIOS_Flash_Jedec_Init(&flash_id, pios_spi_telem_flash_id, 1)) { PIOS_DEBUG_Assert(0); } if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_external_system_cfg, &pios_jedec_flash_driver, flash_id)) { PIOS_DEBUG_Assert(0); } if (PIOS_FLASHFS_Logfs_Init(&pios_user_fs_id, &flashfs_external_user_cfg, &pios_jedec_flash_driver, flash_id)) { PIOS_DEBUG_Assert(0); } #endif /* if defined(PIOS_INCLUDE_FLASH) */ #if defined(PIOS_INCLUDE_RTC) PIOS_RTC_Init(&pios_rtc_main_cfg); #endif /* IAP System Setup */ PIOS_IAP_Init(); // check for safe mode commands from gcs if (PIOS_IAP_ReadBootCmd(0) == PIOS_IAP_CLEAR_FLASH_CMD_0 && PIOS_IAP_ReadBootCmd(1) == PIOS_IAP_CLEAR_FLASH_CMD_1 && PIOS_IAP_ReadBootCmd(2) == PIOS_IAP_CLEAR_FLASH_CMD_2) { PIOS_FLASHFS_Format(pios_uavo_settings_fs_id); PIOS_IAP_WriteBootCmd(0, 0); PIOS_IAP_WriteBootCmd(1, 0); PIOS_IAP_WriteBootCmd(2, 0); } #ifdef PIOS_INCLUDE_WDG PIOS_WDG_Init(); #endif /* Initialize the task monitor */ if (PIOS_TASK_MONITOR_Initialize(TASKINFO_RUNNING_NUMELEM)) { PIOS_Assert(0); } /* Initialize UAVObject libraries */ EventDispatcherInitialize(); UAVObjInitialize(); HwSettingsInitialize(); #if defined(PIOS_INCLUDE_RFM22B) OPLinkSettingsInitialize(); OPLinkStatusInitialize(); #endif /* PIOS_INCLUDE_RFM22B */ /* Initialize the alarms library */ AlarmsInitialize(); /* Initialize the delayed callback library */ CallbackSchedulerInitialize(); /* Set up pulse timers */ PIOS_TIM_InitClock(&tim_1_cfg); PIOS_TIM_InitClock(&tim_3_cfg); PIOS_TIM_InitClock(&tim_4_cfg); PIOS_TIM_InitClock(&tim_5_cfg); PIOS_TIM_InitClock(&tim_8_cfg); PIOS_TIM_InitClock(&tim_9_cfg); PIOS_TIM_InitClock(&tim_10_cfg); PIOS_TIM_InitClock(&tim_11_cfg); PIOS_TIM_InitClock(&tim_12_cfg); uint16_t boot_count = PIOS_IAP_ReadBootCount(); if (boot_count < 3) { PIOS_IAP_WriteBootCount(++boot_count); AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT); } else { /* Too many failed boot attempts, force hwsettings to defaults */ HwSettingsSetDefaults(HwSettingsHandle(), 0); AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL); } // PIOS_IAP_Init(); #if defined(PIOS_INCLUDE_USB) /* Initialize board specific USB data */ PIOS_USB_BOARD_DATA_Init(); /* Flags to determine if various USB interfaces are advertised */ bool usb_hid_present = false; bool usb_cdc_present = false; #if defined(PIOS_INCLUDE_USB_CDC) if (PIOS_USB_DESC_HID_CDC_Init()) { PIOS_Assert(0); } usb_hid_present = true; usb_cdc_present = true; #else if (PIOS_USB_DESC_HID_ONLY_Init()) { PIOS_Assert(0); } usb_hid_present = true; #endif uint32_t pios_usb_id; PIOS_USB_Init(&pios_usb_id, PIOS_BOARD_HW_DEFS_GetUsbCfg(bdinfo->board_rev)); #if defined(PIOS_INCLUDE_USB_CDC) uint8_t hwsettings_usb_vcpport; /* Configure the USB VCP port */ HwSettingsUSB_VCPPortGet(&hwsettings_usb_vcpport); if (!usb_cdc_present) { /* Force VCP port function to disabled if we haven't advertised VCP in our USB descriptor */ hwsettings_usb_vcpport = HWSETTINGS_USB_VCPPORT_DISABLED; } uint32_t pios_usb_cdc_id; if (PIOS_USB_CDC_Init(&pios_usb_cdc_id, &pios_usb_cdc_cfg, pios_usb_id)) { PIOS_Assert(0); } uint32_t pios_usb_hid_id; if (PIOS_USB_HID_Init(&pios_usb_hid_id, &pios_usb_hid_cfg, pios_usb_id)) { PIOS_Assert(0); } switch (hwsettings_usb_vcpport) { case HWSETTINGS_USB_VCPPORT_DISABLED: break; case HWSETTINGS_USB_VCPPORT_USBTELEMETRY: #if defined(PIOS_INCLUDE_COM) { uint8_t *rx_buffer = (uint8_t *)pvPortMalloc(PIOS_COM_TELEM_USB_RX_BUF_LEN); uint8_t *tx_buffer = (uint8_t *)pvPortMalloc(PIOS_COM_TELEM_USB_TX_BUF_LEN); PIOS_Assert(rx_buffer); PIOS_Assert(tx_buffer); if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_cdc_com_driver, pios_usb_cdc_id, rx_buffer, PIOS_COM_TELEM_USB_RX_BUF_LEN, tx_buffer, PIOS_COM_TELEM_USB_TX_BUF_LEN)) { PIOS_Assert(0); } } #endif /* PIOS_INCLUDE_COM */ break; case HWSETTINGS_USB_VCPPORT_COMBRIDGE: #if defined(PIOS_INCLUDE_COM) { uint8_t *rx_buffer = (uint8_t *)pvPortMalloc(PIOS_COM_BRIDGE_RX_BUF_LEN); uint8_t *tx_buffer = (uint8_t *)pvPortMalloc(PIOS_COM_BRIDGE_TX_BUF_LEN); PIOS_Assert(rx_buffer); PIOS_Assert(tx_buffer); if (PIOS_COM_Init(&pios_com_vcp_id, &pios_usb_cdc_com_driver, pios_usb_cdc_id, rx_buffer, PIOS_COM_BRIDGE_RX_BUF_LEN, tx_buffer, PIOS_COM_BRIDGE_TX_BUF_LEN)) { PIOS_Assert(0); } } #endif /* PIOS_INCLUDE_COM */ break; case HWSETTINGS_USB_VCPPORT_DEBUGCONSOLE: #if defined(PIOS_INCLUDE_COM) #if defined(PIOS_INCLUDE_DEBUG_CONSOLE) { uint8_t *tx_buffer = (uint8_t *)pvPortMalloc(PIOS_COM_DEBUGCONSOLE_TX_BUF_LEN); PIOS_Assert(tx_buffer); if (PIOS_COM_Init(&pios_com_debug_id, &pios_usb_cdc_com_driver, pios_usb_cdc_id, NULL, 0, tx_buffer, PIOS_COM_DEBUGCONSOLE_TX_BUF_LEN)) { PIOS_Assert(0); } } #endif /* PIOS_INCLUDE_DEBUG_CONSOLE */ #endif /* PIOS_INCLUDE_COM */ break; } #endif /* PIOS_INCLUDE_USB_CDC */ #if defined(PIOS_INCLUDE_USB_HID) /* Configure the usb HID port */ uint8_t hwsettings_usb_hidport; HwSettingsUSB_HIDPortGet(&hwsettings_usb_hidport); if (!usb_hid_present) { /* Force HID port function to disabled if we haven't advertised HID in our USB descriptor */ hwsettings_usb_hidport = HWSETTINGS_USB_HIDPORT_DISABLED; } switch (hwsettings_usb_hidport) { case HWSETTINGS_USB_HIDPORT_DISABLED: break; case HWSETTINGS_USB_HIDPORT_USBTELEMETRY: #if defined(PIOS_INCLUDE_COM) { uint8_t *rx_buffer = (uint8_t *)pvPortMalloc(PIOS_COM_TELEM_USB_RX_BUF_LEN); uint8_t *tx_buffer = (uint8_t *)pvPortMalloc(PIOS_COM_TELEM_USB_TX_BUF_LEN); PIOS_Assert(rx_buffer); PIOS_Assert(tx_buffer); if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_hid_id, rx_buffer, PIOS_COM_TELEM_USB_RX_BUF_LEN, tx_buffer, PIOS_COM_TELEM_USB_TX_BUF_LEN)) { PIOS_Assert(0); } } #endif /* PIOS_INCLUDE_COM */ break; } #endif /* PIOS_INCLUDE_USB_HID */ if (usb_hid_present || usb_cdc_present) { PIOS_USBHOOK_Activate(); } #endif /* PIOS_INCLUDE_USB */ /* Configure IO ports */ uint8_t hwsettings_DSMxBind; HwSettingsDSMxBindGet(&hwsettings_DSMxBind); /* Configure main USART port */ uint8_t hwsettings_mainport; HwSettingsRM_MainPortGet(&hwsettings_mainport); switch (hwsettings_mainport) { case HWSETTINGS_RM_MAINPORT_DISABLED: break; case HWSETTINGS_RM_MAINPORT_TELEMETRY: PIOS_Board_configure_com(&pios_usart_main_cfg, PIOS_COM_TELEM_RF_RX_BUF_LEN, PIOS_COM_TELEM_RF_TX_BUF_LEN, &pios_usart_com_driver, &pios_com_telem_rf_id); break; case HWSETTINGS_RM_MAINPORT_GPS: PIOS_Board_configure_com(&pios_usart_main_cfg, PIOS_COM_GPS_RX_BUF_LEN, -1, &pios_usart_com_driver, &pios_com_gps_id); break; case HWSETTINGS_RM_MAINPORT_SBUS: #if defined(PIOS_INCLUDE_SBUS) { uint32_t pios_usart_sbus_id; if (PIOS_USART_Init(&pios_usart_sbus_id, &pios_usart_sbus_main_cfg)) { PIOS_Assert(0); } uint32_t pios_sbus_id; if (PIOS_SBus_Init(&pios_sbus_id, &pios_sbus_cfg, &pios_usart_com_driver, pios_usart_sbus_id)) { PIOS_Assert(0); } uint32_t pios_sbus_rcvr_id; if (PIOS_RCVR_Init(&pios_sbus_rcvr_id, &pios_sbus_rcvr_driver, pios_sbus_id)) { PIOS_Assert(0); } pios_rcvr_group_map[MANUALCONTROLSETTINGS_CHANNELGROUPS_SBUS] = pios_sbus_rcvr_id; } #endif break; case HWSETTINGS_RM_MAINPORT_DSM2: case HWSETTINGS_RM_MAINPORT_DSMX10BIT: case HWSETTINGS_RM_MAINPORT_DSMX11BIT: { enum pios_dsm_proto proto; switch (hwsettings_mainport) { case HWSETTINGS_RM_MAINPORT_DSM2: proto = PIOS_DSM_PROTO_DSM2; break; case HWSETTINGS_RM_MAINPORT_DSMX10BIT: proto = PIOS_DSM_PROTO_DSMX10BIT; break; case HWSETTINGS_RM_MAINPORT_DSMX11BIT: proto = PIOS_DSM_PROTO_DSMX11BIT; break; default: PIOS_Assert(0); break; } // Force binding to zero on the main port hwsettings_DSMxBind = 0; // TODO: Define the various Channelgroup for Revo dsm inputs and handle here PIOS_Board_configure_dsm(&pios_usart_dsm_main_cfg, &pios_dsm_main_cfg, &pios_usart_com_driver, &proto, MANUALCONTROLSETTINGS_CHANNELGROUPS_DSMMAINPORT, &hwsettings_DSMxBind); } break; case HWSETTINGS_RM_MAINPORT_DEBUGCONSOLE: #if defined(PIOS_INCLUDE_DEBUG_CONSOLE) { PIOS_Board_configure_com(&pios_usart_main_cfg, 0, PIOS_COM_DEBUGCONSOLE_TX_BUF_LEN, &pios_usart_com_driver, &pios_com_debug_id); } #endif /* PIOS_INCLUDE_DEBUG_CONSOLE */ break; case HWSETTINGS_RM_MAINPORT_COMBRIDGE: PIOS_Board_configure_com(&pios_usart_main_cfg, PIOS_COM_BRIDGE_RX_BUF_LEN, PIOS_COM_BRIDGE_TX_BUF_LEN, &pios_usart_com_driver, &pios_com_bridge_id); break; case HWSETTINGS_RM_MAINPORT_OSDHK: PIOS_Board_configure_com(&pios_usart_hkosd_main_cfg, PIOS_COM_HKOSD_RX_BUF_LEN, PIOS_COM_HKOSD_TX_BUF_LEN, &pios_usart_com_driver, &pios_com_hkosd_id); break; } /* hwsettings_rm_mainport */ if (hwsettings_mainport != HWSETTINGS_RM_MAINPORT_SBUS) { GPIO_Init(pios_sbus_cfg.inv.gpio, &pios_sbus_cfg.inv.init); GPIO_WriteBit(pios_sbus_cfg.inv.gpio, pios_sbus_cfg.inv.init.GPIO_Pin, pios_sbus_cfg.gpio_inv_disable); } /* Configure FlexiPort */ uint8_t hwsettings_flexiport; HwSettingsRM_FlexiPortGet(&hwsettings_flexiport); switch (hwsettings_flexiport) { case HWSETTINGS_RM_FLEXIPORT_DISABLED: break; case HWSETTINGS_RM_FLEXIPORT_TELEMETRY: PIOS_Board_configure_com(&pios_usart_flexi_cfg, PIOS_COM_TELEM_RF_RX_BUF_LEN, PIOS_COM_TELEM_RF_TX_BUF_LEN, &pios_usart_com_driver, &pios_com_telem_rf_id); break; case HWSETTINGS_RM_FLEXIPORT_I2C: #if defined(PIOS_INCLUDE_I2C) { if (PIOS_I2C_Init(&pios_i2c_flexiport_adapter_id, &pios_i2c_flexiport_adapter_cfg)) { PIOS_Assert(0); } } #endif /* PIOS_INCLUDE_I2C */ break; case HWSETTINGS_RM_FLEXIPORT_GPS: PIOS_Board_configure_com(&pios_usart_flexi_cfg, PIOS_COM_GPS_RX_BUF_LEN, -1, &pios_usart_com_driver, &pios_com_gps_id); break; case HWSETTINGS_RM_FLEXIPORT_DSM2: case HWSETTINGS_RM_FLEXIPORT_DSMX10BIT: case HWSETTINGS_RM_FLEXIPORT_DSMX11BIT: { enum pios_dsm_proto proto; switch (hwsettings_flexiport) { case HWSETTINGS_RM_FLEXIPORT_DSM2: proto = PIOS_DSM_PROTO_DSM2; break; case HWSETTINGS_RM_FLEXIPORT_DSMX10BIT: proto = PIOS_DSM_PROTO_DSMX10BIT; break; case HWSETTINGS_RM_FLEXIPORT_DSMX11BIT: proto = PIOS_DSM_PROTO_DSMX11BIT; break; default: PIOS_Assert(0); break; } // TODO: Define the various Channelgroup for Revo dsm inputs and handle here PIOS_Board_configure_dsm(&pios_usart_dsm_flexi_cfg, &pios_dsm_flexi_cfg, &pios_usart_com_driver, &proto, MANUALCONTROLSETTINGS_CHANNELGROUPS_DSMFLEXIPORT, &hwsettings_DSMxBind); } break; case HWSETTINGS_RM_FLEXIPORT_DEBUGCONSOLE: #if defined(PIOS_INCLUDE_DEBUG_CONSOLE) { PIOS_Board_configure_com(&pios_usart_main_cfg, 0, PIOS_COM_DEBUGCONSOLE_TX_BUF_LEN, &pios_usart_com_driver, &pios_com_debug_id); } #endif /* PIOS_INCLUDE_DEBUG_CONSOLE */ break; case HWSETTINGS_RM_FLEXIPORT_COMBRIDGE: PIOS_Board_configure_com(&pios_usart_flexi_cfg, PIOS_COM_BRIDGE_RX_BUF_LEN, PIOS_COM_BRIDGE_TX_BUF_LEN, &pios_usart_com_driver, &pios_com_bridge_id); break; case HWSETTINGS_RM_FLEXIPORT_OSDHK: PIOS_Board_configure_com(&pios_usart_hkosd_flexi_cfg, PIOS_COM_HKOSD_RX_BUF_LEN, PIOS_COM_HKOSD_TX_BUF_LEN, &pios_usart_com_driver, &pios_com_hkosd_id); break; } /* hwsettings_rm_flexiport */ /* Initalize the RFM22B radio COM device. */ #if defined(PIOS_INCLUDE_RFM22B) /* Fetch the OPinkSettings object. */ OPLinkSettingsData oplinkSettings; OPLinkSettingsGet(&oplinkSettings); // Initialize out status object. OPLinkStatusData oplinkStatus; OPLinkStatusGet(&oplinkStatus); oplinkStatus.BoardType = bdinfo->board_type; PIOS_BL_HELPER_FLASH_Read_Description(oplinkStatus.Description, OPLINKSTATUS_DESCRIPTION_NUMELEM); PIOS_SYS_SerialNumberGetBinary(oplinkStatus.CPUSerial); oplinkStatus.BoardRevision = bdinfo->board_rev; /* Is the radio turned on? */ bool is_coordinator = (oplinkSettings.Coordinator == OPLINKSETTINGS_COORDINATOR_TRUE); bool is_oneway = (oplinkSettings.OneWay == OPLINKSETTINGS_ONEWAY_TRUE); bool ppm_mode = (oplinkSettings.PPM == OPLINKSETTINGS_PPM_TRUE); bool ppm_only = (oplinkSettings.PPMOnly == OPLINKSETTINGS_PPMONLY_TRUE); if (oplinkSettings.MaxRFPower != OPLINKSETTINGS_MAXRFPOWER_0) { /* Configure the RFM22B device. */ const struct pios_rfm22b_cfg *rfm22b_cfg = PIOS_BOARD_HW_DEFS_GetRfm22Cfg(bdinfo->board_rev); if (PIOS_RFM22B_Init(&pios_rfm22b_id, PIOS_RFM22_SPI_PORT, rfm22b_cfg->slave_num, rfm22b_cfg)) { PIOS_Assert(0); } /* Configure the radio com interface */ uint8_t *rx_buffer = (uint8_t *)pvPortMalloc(PIOS_COM_RFM22B_RF_RX_BUF_LEN); uint8_t *tx_buffer = (uint8_t *)pvPortMalloc(PIOS_COM_RFM22B_RF_TX_BUF_LEN); PIOS_Assert(rx_buffer); PIOS_Assert(tx_buffer); if (PIOS_COM_Init(&pios_com_telem_rf_id, &pios_rfm22b_com_driver, pios_rfm22b_id, rx_buffer, PIOS_COM_RFM22B_RF_RX_BUF_LEN, tx_buffer, PIOS_COM_RFM22B_RF_TX_BUF_LEN)) { PIOS_Assert(0); } oplinkStatus.LinkState = OPLINKSTATUS_LINKSTATE_ENABLED; // Set the RF data rate on the modem to ~2X the selected buad rate because the modem is half duplex. enum rfm22b_datarate datarate = RFM22_datarate_64000; switch (oplinkSettings.ComSpeed) { case OPLINKSETTINGS_COMSPEED_4800: datarate = RFM22_datarate_9600; break; case OPLINKSETTINGS_COMSPEED_9600: datarate = RFM22_datarate_19200; break; case OPLINKSETTINGS_COMSPEED_19200: datarate = RFM22_datarate_32000; break; case OPLINKSETTINGS_COMSPEED_38400: datarate = RFM22_datarate_64000; break; case OPLINKSETTINGS_COMSPEED_57600: datarate = RFM22_datarate_100000; break; case OPLINKSETTINGS_COMSPEED_115200: datarate = RFM22_datarate_192000; break; } /* Set the radio configuration parameters. */ PIOS_RFM22B_SetChannelConfig(pios_rfm22b_id, datarate, oplinkSettings.MinChannel, oplinkSettings.MaxChannel, oplinkSettings.ChannelSet, is_coordinator, is_oneway, ppm_mode, ppm_only); PIOS_RFM22B_SetCoordinatorID(pios_rfm22b_id, oplinkSettings.CoordID); /* Set the PPM callback if we should be receiving PPM. */ if (ppm_mode) { PIOS_RFM22B_SetPPMCallback(pios_rfm22b_id, PIOS_Board_PPM_callback); } /* Set the modem Tx poer level */ switch (oplinkSettings.MaxRFPower) { case OPLINKSETTINGS_MAXRFPOWER_125: PIOS_RFM22B_SetTxPower(pios_rfm22b_id, RFM22_tx_pwr_txpow_0); break; case OPLINKSETTINGS_MAXRFPOWER_16: PIOS_RFM22B_SetTxPower(pios_rfm22b_id, RFM22_tx_pwr_txpow_1); break; case OPLINKSETTINGS_MAXRFPOWER_316: PIOS_RFM22B_SetTxPower(pios_rfm22b_id, RFM22_tx_pwr_txpow_2); break; case OPLINKSETTINGS_MAXRFPOWER_63: PIOS_RFM22B_SetTxPower(pios_rfm22b_id, RFM22_tx_pwr_txpow_3); break; case OPLINKSETTINGS_MAXRFPOWER_126: PIOS_RFM22B_SetTxPower(pios_rfm22b_id, RFM22_tx_pwr_txpow_4); break; case OPLINKSETTINGS_MAXRFPOWER_25: PIOS_RFM22B_SetTxPower(pios_rfm22b_id, RFM22_tx_pwr_txpow_5); break; case OPLINKSETTINGS_MAXRFPOWER_50: PIOS_RFM22B_SetTxPower(pios_rfm22b_id, RFM22_tx_pwr_txpow_6); break; case OPLINKSETTINGS_MAXRFPOWER_100: PIOS_RFM22B_SetTxPower(pios_rfm22b_id, RFM22_tx_pwr_txpow_7); break; default: // do nothing break; } /* Reinitialize the modem. */ PIOS_RFM22B_Reinit(pios_rfm22b_id); } else { oplinkStatus.LinkState = OPLINKSTATUS_LINKSTATE_DISABLED; } OPLinkStatusSet(&oplinkStatus); #endif /* PIOS_INCLUDE_RFM22B */ #if defined(PIOS_INCLUDE_PWM) || defined(PIOS_INCLUDE_PWM) const struct pios_servo_cfg *pios_servo_cfg; // default to servo outputs only pios_servo_cfg = &pios_servo_cfg_out; #endif /* Configure the receiver port*/ uint8_t hwsettings_rcvrport; HwSettingsRM_RcvrPortGet(&hwsettings_rcvrport); // switch (hwsettings_rcvrport) { case HWSETTINGS_RM_RCVRPORT_DISABLED: break; case HWSETTINGS_RM_RCVRPORT_PWM: #if defined(PIOS_INCLUDE_PWM) /* Set up the receiver port. Later this should be optional */ PIOS_Board_configure_pwm(&pios_pwm_cfg); #endif /* PIOS_INCLUDE_PWM */ break; case HWSETTINGS_RM_RCVRPORT_PPM: case HWSETTINGS_RM_RCVRPORT_PPMOUTPUTS: case HWSETTINGS_RM_RCVRPORT_PPMPWM: #if defined(PIOS_INCLUDE_PPM) if (hwsettings_rcvrport == HWSETTINGS_RM_RCVRPORT_PPMOUTPUTS) { // configure servo outputs and the remaining 5 inputs as outputs pios_servo_cfg = &pios_servo_cfg_out_in_ppm; } PIOS_Board_configure_ppm(&pios_ppm_cfg); // enable pwm on the remaining channels if (hwsettings_rcvrport == HWSETTINGS_RM_RCVRPORT_PPMPWM) { PIOS_Board_configure_pwm(&pios_pwm_ppm_cfg); } break; #endif /* PIOS_INCLUDE_PPM */ case HWSETTINGS_RM_RCVRPORT_OUTPUTS: // configure only the servo outputs pios_servo_cfg = &pios_servo_cfg_out_in; break; } #if defined(PIOS_INCLUDE_GCSRCVR) GCSReceiverInitialize(); uint32_t pios_gcsrcvr_id; PIOS_GCSRCVR_Init(&pios_gcsrcvr_id); uint32_t pios_gcsrcvr_rcvr_id; if (PIOS_RCVR_Init(&pios_gcsrcvr_rcvr_id, &pios_gcsrcvr_rcvr_driver, pios_gcsrcvr_id)) { PIOS_Assert(0); } pios_rcvr_group_map[MANUALCONTROLSETTINGS_CHANNELGROUPS_GCS] = pios_gcsrcvr_rcvr_id; #endif /* PIOS_INCLUDE_GCSRCVR */ #if defined(PIOS_INCLUDE_OPLINKRCVR) { OPLinkReceiverInitialize(); uint32_t pios_oplinkrcvr_id; PIOS_OPLinkRCVR_Init(&pios_oplinkrcvr_id); uint32_t pios_oplinkrcvr_rcvr_id; if (PIOS_RCVR_Init(&pios_oplinkrcvr_rcvr_id, &pios_oplinkrcvr_rcvr_driver, pios_oplinkrcvr_id)) { PIOS_Assert(0); } pios_rcvr_group_map[MANUALCONTROLSETTINGS_CHANNELGROUPS_OPLINK] = pios_oplinkrcvr_rcvr_id; } #endif /* PIOS_INCLUDE_OPLINKRCVR */ #ifndef PIOS_ENABLE_DEBUG_PINS // pios_servo_cfg points to the correct configuration based on input port settings PIOS_Servo_Init(pios_servo_cfg); #else PIOS_DEBUG_Init(pios_tim_servoport_all_pins, NELEMENTS(pios_tim_servoport_all_pins)); #endif if (PIOS_I2C_Init(&pios_i2c_mag_pressure_adapter_id, &pios_i2c_mag_pressure_adapter_cfg)) { PIOS_DEBUG_Assert(0); } PIOS_DELAY_WaitmS(50); #if defined(PIOS_INCLUDE_ADC) PIOS_ADC_Init(&pios_adc_cfg); #endif #if defined(PIOS_INCLUDE_HMC5883) PIOS_HMC5883_Init(&pios_hmc5883_cfg); #endif #if defined(PIOS_INCLUDE_MS5611) PIOS_MS5611_Init(&pios_ms5611_cfg, pios_i2c_mag_pressure_adapter_id); #endif #if defined(PIOS_INCLUDE_MPU6000) PIOS_MPU6000_Init(pios_spi_gyro_id, 0, &pios_mpu6000_cfg); PIOS_MPU6000_CONFIG_Configure(); #endif }
/** * @brief Configures the CAN. * @param None * @retval None */ static void CAN_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; CAN_InitTypeDef CAN_InitStructure; /* CAN GPIOs configuration **************************************************/ /* Enable GPIO clock */ RCC_AHBPeriphClockCmd(CAN_GPIO_CLK, ENABLE); /* Connect CAN pins to AF7 */ GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT); GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT); /* Configure CAN RX and TX pins */ GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure); /* CAN NVIC configuration **************************************************/ NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* CAN configuration ********************************************************/ /* Enable CAN clock */ RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE); /* CAN register init */ CAN_DeInit(CANx); CAN_StructInit(&CAN_InitStructure); /* CAN cell init */ CAN_InitStructure.CAN_TTCM = DISABLE; CAN_InitStructure.CAN_ABOM = DISABLE; CAN_InitStructure.CAN_AWUM = DISABLE; CAN_InitStructure.CAN_NART = DISABLE; CAN_InitStructure.CAN_RFLM = ENABLE; CAN_InitStructure.CAN_TXFP = ENABLE; CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; /* CAN Baudrate = 1MBps (CAN clocked at 36 MHz) */ CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; CAN_InitStructure.CAN_Prescaler = 2; CAN_Init(CANx, &CAN_InitStructure); /* CAN filter init "FIFO0" */ CAN_FilterInitStructure.CAN_FilterNumber = 0; CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; CAN_FilterInitStructure.CAN_FilterIdHigh = 0x6420; CAN_FilterInitStructure.CAN_FilterIdLow = 0x2461; CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0; CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); /* Transmit Structure preparation */ TxMessage.StdId = 0x321; TxMessage.ExtId = 0x00; TxMessage.RTR = CAN_RTR_DATA; TxMessage.IDE = CAN_ID_STD; TxMessage.DLC = 1; /* Enable FIFO 0 full Interrupt */ CAN_ITConfig(CANx, CAN_IT_FF0, ENABLE); /* Enable FIFO 1 full Interrupt */ CAN_ITConfig(CANx, CAN_IT_FF1, ENABLE); }
int main(){ SysTick_Config(SystemCoreClock / 2000); // 0.5ms // NEED OF KEY AND LED RCC_ClockSecuritySystemCmd(ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOB, &GPIO_InitStructure); // Init LCD LCD_Cmd_InitFSMC(); LCD_Cmd_Init(); LCD_Cmd_InitBacklight(); // Put some background // LCD_FillRectangle_RGB565(0, 0, 240, 320, C_RGB565(0x80, 0x80, 0x80)); Painter_PutImage(res_img_res_lp_jpg,0,0,0xff); // Painter_SetupContextBitmask(240, 320, 0); // benchmark(2); // A circle to overlay long shadow LCD_FillCircle_RGB4444(120, 120, 100+18, 0x88fa); // now you can see it long_shadow(); // an alternative shadow // LCD_FillCircle(120+1, 120+1, 100+2, 0x8886, 0); // LCD_FillCircle(120, 120, 100+2, 0x8886, 0); // LCD_FillCircle(120, 120, 100+1, 0x8886, 0); // Put my clock panel on screen LCD_FillCircle_RGB565(120, 120, 100, C_RGB565(0, 0, 0)); LCD_FillCircle_RGB565(120, 120, 90, C_RGB565(0xff, 0xff, 0xff)); // Clock frame need some shadow! Painter_SetupContextBitmask(210, 210, 0); u16 x, y, i; u32 t; for (x=0;x<200;x++) for (y=0;y<200;y++){ t = (x-100)*(x-100)+(y-100)*(y-100); if ((t>=91*91)&&(t<100*100)) LCD_SetBitMask(_d_dl_ctx.bm, x, y, 210); } // Draw ticks and numbers const s8 TICK_XI[] = { 0, 42, 73, 85, 73, 42, 0, -42, -73, -85, -73, -42}; const s8 TICK_YI[] = {-85, -73, -42, 0, 42, 73, 85, 73, 42, 0, -42, -73}; const s8 TICK_XO[] = { 0, 44, 77, 90, 77, 45, 0, -44, -77, -90, -77, -45}; const s8 TICK_YO[] = {-90, -77, -45, 0, 44, 77, 90, 77, 45, 0, -44, -77}; const u16 *text[] = {res_string_0/*_FSTR_0*/, res_string_1/*_FSTR_1*/, res_string_2/*_FSTR_2*/, res_string_3/*_FSTR_3*/, res_string_4/*_FSTR_4*/, res_string_5/*_FSTR_5*/, res_string_6/*_FSTR_6*/, res_string_7/*_FSTR_7*/, res_string_8/*_FSTR_8*/, res_string_9/*_FSTR_9*/, res_string_11/*_FSTR_10*/, res_string_12/*_FSTR_11*/, res_string_13/*_FSTR_12*/ }; const u16 *col = res_string_14/*_FSTR_:*/; const u16 *space = res_string_15/*_FSTR_ */; u16 time[20]; for (i=0;i<12;i++){ Painter_LocateContextBitmask(100+TICK_XO[i], 100+TICK_YO[i]); Painter_DrawLine(120+TICK_XO[i], 120+TICK_YO[i], 120+TICK_XI[i], 120+TICK_YI[i], 0x000f, 5, PAINTER_DRAW_BM_HOLD); Painter_PutString(text[i?i:12], 14, 0x000f, 0 , 120+TICK_XI[i]-(TICK_XO[i]-TICK_XI[i])*2-5, 120+TICK_YI[i]-(TICK_YO[i]-TICK_YI[i])*2-8 , 20, 20, PAINTER_STR_SHADOW*2); } // Bravo! Painter_Fill_BitMaskShadow(30, 30, 230, 230, 0L, 10, 10, 210, 0x0008, 4, 4, 15); // LET IT RUN! u8 m, s; // so-called second and minute bitmask bm; _UNUSED(bm); s = 0; m = 0; Painter_SetupContextBitmask(200, 200, 0); // new context for new shadow Painter_LocateContextBitmask(100, 100); // start from center u8 shadow_on; u16 bg[200*60]; // grab screen region, saving for later repaint LCD_SetWindow(20, 240, 200, 60); LCD_GetImage_RGB565(bg, 200*60); while (1){ // PUSH BUTTON TO RUN REALLY FAST, WITHOUT SHADOW! shadow_on = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)?PAINTER_STR_SHADOW*2:0; LCD_FillCircle_RGB565(120, 120, 65, 0xffff); // Clear clock hands, sucks aha~ // Dont forget bitmasks for (x=30;x<170;x++) for (y=30;y<170;y++){ LCD_ResetBitMask(_d_dl_ctx.bm, x, y, 200); } // second hand u16 sh_x[] = {120, 120+(TICK_XI[_mod(s+11,12)]+3)/5, 120+TICK_XI[s]-(TICK_XO[s]-TICK_XI[s])*6, 120+(TICK_XI[_mod(s+1,12)]+3)/5 }; u16 sh_y[] = {120, 120+(TICK_YI[_mod(s+11,12)]+3)/5, 120+TICK_YI[s]-(TICK_YO[s]-TICK_YI[s])*6, 120+(TICK_YI[_mod(s+1,12)]+3)/5 }; // Center circle makes it cute~ Painter_DrawCircle(120, 120, 4, 0x6a2f, 12, PAINTER_DRAW_BM_HOLD); Painter_DrawPoly(sh_x, sh_y, 4, 0x6a2f, 3, PAINTER_DRAW_BM_HOLD | PAINTER_DRAW_POLY_CLOSE); // Painter_DrawLine(120, 120 // , 120+TICK_XI[s]-(TICK_XO[s]-TICK_XI[s])*6 // , 120+TICK_YI[s]-(TICK_YO[s]-TICK_YI[s])*6, // 0x6a2f, 5, PAINTER_DRAW_BM_HOLD); // Painter_DrawLine(120, 120 // , 120-(TICK_XI[s]+4)/8 // , 120-(TICK_YI[s]+4)/8, // 0x6a2f, 5, PAINTER_DRAW_BM_HOLD); // GET MY SHADOW! if (shadow_on) Painter_Fill_BitMaskShadow(50, 50, 190, 190, 0L, 30, 30, 200, 0x0008, 4, 4, 12); for (x=30;x<170;x++) for (y=30;y<170;y++){ LCD_ResetBitMask(_d_dl_ctx.bm, x, y, 200); } u32 mm = m % 12; // minute hand Painter_DrawLine(120, 120 , 120+TICK_XI[mm]-(TICK_XO[mm]-TICK_XI[mm])*10 , 120+TICK_YI[mm]-(TICK_YO[mm]-TICK_YI[mm])*10, 0xa22f, 5, PAINTER_DRAW_BM_HOLD); Painter_DrawLine(120, 120 , 120-(TICK_XI[mm]+4)/8 , 120-(TICK_YI[mm]+4)/8, 0xa22f, 5, PAINTER_DRAW_BM_HOLD); // Center circle makes it cute~ Painter_DrawCircle(120, 120, 3, 0xa22f, 5, PAINTER_DRAW_BM_HOLD); // GET MY SHADOW! if (shadow_on) Painter_Fill_BitMaskShadow(50, 50, 190, 190, 0L, 30, 30, 200, 0x0008, 4, 4, 12); // show time in text u16 *p = time, *q; // need some space *p++ = space[0]; mm = m?m:1; while (mm<10000) { *p++ = space[0]; mm *= 10; } //put minute digits p = itoa(m, p); *p++ = col[0]; //put second text directly q = (u16 *)(text)[s]; while (*q) *p++ = *q++; *p++ = 0; // repaint the background LCD_SetWindow(20, 240, 200, 60); LCD_PutImage_RGB565(bg, 200*60); // GET MY TIME! Painter_PutString((const u16*)time, 48, 0xffff, 0xf5b9 , 20, 240, 200, 60, shadow_on | PAINTER_STR_SFLUSH); put_fps(1); // PUSH A TO DASH! if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)) DUMMY; // TICK TOCK~ s++; if (s == 12) { m++; s = 0; } } return 0; }
// PWM // TIM2 and TIM5 have CH1, CH2, CH3, CH4 on PA0-PA3 respectively // they are both 32-bit counters // 16-bit prescaler // TIM2_CH3 also on PB10 (used below) void servo_init(void) { // TIM2 clock enable RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // for PB10 /* // GPIOB Configuration: TIM2_CH3 (PB10) GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); // Connect TIM2 pins to AF1 GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_TIM2); */ // for PA0, PA1, PA2, PA3 { // GPIOA Configuration: TIM2_CH0, TIM2_CH1 (PA0, PA1) GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); // Connect TIM2 pins to AF1 GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM2); } // Compute the prescaler value so TIM2 runs at 100kHz uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 100000) - 1; // Time base configuration TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 2000; // timer cycles at 50Hz TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // PWM Mode configuration TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 150; // units of 10us TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM2, &TIM_OCInitStructure); // channel 1 TIM_OC2Init(TIM2, &TIM_OCInitStructure); // channel 2 TIM_OC3Init(TIM2, &TIM_OCInitStructure); // channel 3 TIM_OC4Init(TIM2, &TIM_OCInitStructure); // channel 4 // ? TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); // channel 1 TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); // channel 2 TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); // channel 3 TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); // channel 4 // ? TIM_ARRPreloadConfig(TIM2, ENABLE); // TIM2 enable counter TIM_Cmd(TIM2, ENABLE); }
/* * 函数名:LCD_GPIO_Config * 描述 :根据FSMC配置LCD的I/O * 输入 :无 * 输出 :无 * 调用 :内部调用 */ void LCD_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable the FSMC Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); /* config lcd gpio clock base on FSMC */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* config tft back_light gpio base on the PT4101 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_Init(GPIOD, &GPIO_InitStructure); /* config tft rst gpio */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_13 ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* config tft data lines base on FSMC * data lines,FSMC-D0~D15: PD 14 15 0 1,PE 7 8 9 10 11 12 13 14 15,PD 8 9 10 */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOE, &GPIO_InitStructure); /* config tft control lines base on FSMC * PD4-FSMC_NOE :LCD-RD * PD5-FSMC_NWE :LCD-WR * PD7-FSMC_NE1 :LCD-CS * PD11-FSMC_A16 :LCD-DC */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; GPIO_Init(GPIOD, &GPIO_InitStructure); /* tft control gpio init */ GPIO_SetBits(GPIOD, GPIO_Pin_13); // RST = 1 GPIO_SetBits(GPIOC, GPIO_Pin_5); // LIGHT GPIO_SetBits(GPIOD, GPIO_Pin_4); // RD = 1 GPIO_SetBits(GPIOD, GPIO_Pin_5); // WR = 1 GPIO_SetBits(GPIOD, GPIO_Pin_7); // CS = 1 // BL RS CS GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_ResetBits(GPIOA, GPIO_Pin_4); GPIO_SetBits(GPIOC, GPIO_Pin_13); // CS2 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_5|GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_ResetBits(GPIOD, GPIO_Pin_7); }
/* GPIOx: where x can be (A..H) to select the GPIO peripheral. pinNumber: GPIO_Pin_0 ... GPIO_Pin_15, GPIO_Pin_All GPIO_AF: stm32l1xx_gpio.c, search GPIO_AFSelection. pinType: GPIO_OType_PP, GPIO_OType_OD - sets the pin type to push / pull or open drain pinPull: GPIO_PuPd_NOPULL, GPIO_PuPd_UP, GPIO_PuPd_DOWN pinSpeed: GPIO_Speed_400KHz, GPIO_Speed_2MHz, GPIO_Speed_10MHz, GPIO_Speed_40MHz gpio_pinSetup_AF(GPIOA, GPIO_Pin_2, GPIO_AF_TIM9, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_Speed_2MHz); */ void gpio_pinSetup_AF(GPIO_TypeDef* GPIOx, uint32_t pinNumber, uint8_t GPIO_AF, GPIOOType_TypeDef pinType, GPIOPuPd_TypeDef pinPull, GPIOSpeed_TypeDef pinSpeed) { GPIO_InitTypeDef GPIO_setup; uint32_t pinSource = 0; /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); if(GPIOx == GPIOA) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); } else if(GPIOx == GPIOB) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); } else if(GPIOx == GPIOC) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); } else if(GPIOx == GPIOD) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE); } else if(GPIOx == GPIOE) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE); } else if(GPIOx == GPIOF) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE); } else if(GPIOx == GPIOG) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOG, ENABLE); } else { if(GPIOx == GPIOH) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOH, ENABLE); } } switch(pinNumber) { case GPIO_Pin_0: pinSource = GPIO_PinSource0; break; case GPIO_Pin_1: pinSource = GPIO_PinSource1; break; case GPIO_Pin_2: pinSource = GPIO_PinSource2; break; case GPIO_Pin_3: pinSource = GPIO_PinSource3; break; case GPIO_Pin_4: pinSource = GPIO_PinSource4; break; case GPIO_Pin_5: pinSource = GPIO_PinSource5; break; case GPIO_Pin_6: pinSource = GPIO_PinSource6; break; case GPIO_Pin_7: pinSource = GPIO_PinSource7; break; case GPIO_Pin_8: pinSource = GPIO_PinSource8; break; case GPIO_Pin_9: pinSource = GPIO_PinSource9; break; case GPIO_Pin_10: pinSource = GPIO_PinSource10; break; case GPIO_Pin_11: pinSource = GPIO_PinSource11; break; case GPIO_Pin_12: pinSource = GPIO_PinSource12; break; case GPIO_Pin_13: pinSource = GPIO_PinSource13; break; case GPIO_Pin_14: pinSource = GPIO_PinSource14; break; case GPIO_Pin_15: pinSource = GPIO_PinSource15; break; } GPIO_PinAFConfig(GPIOx, pinSource, GPIO_AF); GPIO_setup.GPIO_Pin = pinNumber; GPIO_setup.GPIO_Mode = GPIO_Mode_AF; GPIO_setup.GPIO_OType = pinType; GPIO_setup.GPIO_PuPd = pinPull; GPIO_setup.GPIO_Speed = pinSpeed; GPIO_Init(GPIOx, &GPIO_setup); }
void OSView_InitTarget (INT32U baud_rate) { GPIO_InitTypeDef gpio_init; USART_InitTypeDef usart_init; USART_ClockInitTypeDef usart_clk_init; OSView_TmrInit(); /* Initialize the free running timer */ /* ----------------- INIT USART STRUCT ---------------- */ usart_init.USART_BaudRate = baud_rate; usart_init.USART_WordLength = USART_WordLength_8b; usart_init.USART_StopBits = USART_StopBits_1; usart_init.USART_Parity = USART_Parity_No ; usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usart_clk_init.USART_Clock = USART_Clock_Disable; usart_clk_init.USART_CPOL = USART_CPOL_Low; usart_clk_init.USART_CPHA = USART_CPHA_2Edge; usart_clk_init.USART_LastBit = USART_LastBit_Disable; #if (OS_VIEW_COMM_SEL == OS_VIEW_UART_1) BSP_PeriphEn(BSP_PERIPH_ID_USART1); /* ----------------- SETUP USART1 GPIO ---------------- */ BSP_PeriphEn(BSP_PERIPH_ID_IOPA); /* Configure GPIOA.9 as push-pull */ gpio_init.GPIO_Pin = GPIO_Pin_9; gpio_init.GPIO_Speed = GPIO_Speed_50MHz; gpio_init.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &gpio_init); /* Configure GPIOA.10 as input floating */ gpio_init.GPIO_Pin = GPIO_Pin_10; gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &gpio_init); /* ------------------ SETUP USART1 -------------------- */ USART_Init(USART1, &usart_init); USART_ClockInit(USART1, &usart_clk_init); USART_Cmd(USART1, ENABLE); BSP_IntVectSet(BSP_INT_ID_USART1, OSView_RxTxISRHandler); BSP_IntEn(BSP_INT_ID_USART1); #endif #if (OS_VIEW_COMM_SEL == OS_VIEW_UART_2) BSP_PeriphEn(BSP_PERIPH_ID_USART2); /* ----------------- SETUP USART2 GPIO ---------------- */ BSP_PeriphEn(BSP_PERIPH_ID_IOPA); /* Configure GPIOA.2 as push-pull */ gpio_init.GPIO_Pin = GPIO_Pin_2; gpio_init.GPIO_Speed = GPIO_Speed_50MHz; gpio_init.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &gpio_init); /* Configure GPIOA.3 as input floating */ gpio_init.GPIO_Pin = GPIO_Pin_3; gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &gpio_init); /* ------------------ SETUP USART2 -------------------- */ USART_Init(USART2, &usart_init); USART_ClockInit(USART2, &usart_clk_init); USART_Cmd(USART2, ENABLE); BSP_IntVectSet(BSP_INT_ID_USART2, OSView_RxTxISRHandler); BSP_IntEn(BSP_INT_ID_USART2); #endif #if (OS_VIEW_COMM_SEL == OS_VIEW_UART_3) BSP_PeriphEn(BSP_PERIPH_ID_USART3); /* ----------------- SETUP USART3 GPIO ---------------- */ BSP_PeriphEn(BSP_PERIPH_ID_IOPB); /* Configure GPIOB.10 as push-pull */ gpio_init.GPIO_Pin = GPIO_Pin_10; gpio_init.GPIO_Speed = GPIO_Speed_50MHz; gpio_init.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &gpio_init); /* Configure GPIOB.11 as input floating */ gpio_init.GPIO_Pin = GPIO_Pin_11; gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &gpio_init); /* ------------------ SETUP USART3 -------------------- */ USART_Init(USART3, &usart_init); USART_ClockInit(USART3, &usart_clk_init); USART_Cmd(USART3, ENABLE); BSP_IntVectSet(BSP_INT_ID_USART3, OSView_RxTxISRHandler); BSP_IntEn(BSP_INT_ID_USART3); #endif }
/** * @brief Configure the TIM2 Pins. * @param None * @retval None */ static void TIM_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* TIM4 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* GPIOA clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* TIM2_CH1 pin (PA.05) and TIM2_CH2 pin (PA.01) configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Connect TIM pins to AF2 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_2); /* -------------------------------------------------------------------------- TIM2 configuration: One Pulse mode The external signal is connected to TIM2_CH2 pin (PA.01), The Rising edge is used as active edge, The One Pulse signal is output on TIM2_CH1 pin (PA.05) The TIM_Pulse defines the delay value The (TIM_Period - TIM_Pulse) defines the One Pulse value. TIM2 input clock (TIM2CLK) is set to APB1 clock (PCLK1) TIM2CLK = PCLK1 PCLK1 = HCLK => TIM2CLK = HCLK = SystemCoreClock TIM2CLK = SystemCoreClock, we want to get TIM2 counter clock at 24 MHz: Prescaler = (TIM2CLK / TIM2 counter clock) - 1 Prescaler = (SystemCoreClock /24 MHz) - 1 The Autoreload value is 65535 (TIM2->ARR), so the maximum frequency value to trigger the TIM2 input is 24000000/65535 = 366.2 Hz. The TIM_Pulse defines the delay value, this value is fixed to: delay = CCR1/TIM2 counter clock = 682.6 us. The (TIM_Period - TIM_Pulse) defines the One Pulse value, this value is fixed to: One Pulse value = (TIM_Period - TIM_Pulse) / TIM2 counter clock = 2.04 ms. Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file. Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() function to update SystemCoreClock variable value. Otherwise, any configuration based on this variable will be incorrect. --------------------------------------------------------------------------- */ /* Compute the prescaler value */ PrescalerValue = (uint16_t) ((SystemCoreClock ) / 24000000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 PWM2 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 16383; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM2, &TIM_OCInitStructure); /* TIM4 configuration in Input Capture Mode */ TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM2, &TIM_ICInitStructure); /* One Pulse Mode selection */ TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single); /* Input Trigger selection */ TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); /* Slave Mode selection: Trigger Mode */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger); }
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) { GPIO_InitTypeDef GPIO_InitStructure; #ifndef USE_ULPI_PHY #ifdef USB_OTG_FS_LOW_PWR_MGMT_SUPPORT EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; #endif #endif #ifdef USE_USB_OTG_FS RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE); /* Configure SOF VBUS ID DM DP Pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ; /* this for ID line debug */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; #else // USE_USB_OTG_HS #ifdef USE_ULPI_PHY // ULPI RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH | RCC_AHB1Periph_GPIOI, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // D0 GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // CLK GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // D1 GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_OTG2_HS) ; // D2 GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // D7 GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_HS) ; // D3 GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // D4 GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG2_HS) ; // D5 GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_HS) ; // D6 GPIO_PinAFConfig(GPIOH,GPIO_PinSource4, GPIO_AF_OTG2_HS) ; // NXT GPIO_PinAFConfig(GPIOI,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // DIR GPIO_PinAFConfig(GPIOC,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // STP // CLK GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &GPIO_InitStructure); // D0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); // D1 D2 D3 D4 D5 D6 D7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_10 | GPIO_Pin_11| GPIO_Pin_12 | GPIO_Pin_13 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); // STP GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOC, &GPIO_InitStructure); //NXT GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOH, &GPIO_InitStructure); //DIR GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOI, &GPIO_InitStructure); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS | RCC_AHB1Periph_OTG_HS_ULPI, ENABLE) ; #else #ifdef USE_I2C_PHY RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB , ENABLE); /* Configure RESET INTN SCL SDA (Phy/I2C) Pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_FS); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; #else RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; #endif #endif // USE_ULPI_PHY #endif //USB_OTG_HS /* enable the PWR clock */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE); /* Configure the Key button in EXTI mode */ STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); #ifdef USB_OTG_FS_LOW_PWR_MGMT_SUPPORT EXTI_ClearITPendingBit(EXTI_Line18); EXTI_InitStructure.EXTI_Line = EXTI_Line18; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); EXTI_ClearITPendingBit(EXTI_Line18); NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); EXTI_ClearITPendingBit(EXTI_Line18); #endif #ifdef USB_OTG_HS_LOW_PWR_MGMT_SUPPORT EXTI_ClearITPendingBit(EXTI_Line20); EXTI_InitStructure.EXTI_Line = EXTI_Line20; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); EXTI_ClearITPendingBit(EXTI_Line20); NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); EXTI_ClearITPendingBit(EXTI_Line20); #endif EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE); }
void i2cInit() { NVIC_InitTypeDef nvic; I2C_InitTypeDef i2c; GPIO_InitTypeDef GPIO_InitStructure; //Enable the i2c RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); //Reset the Peripheral RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); //Enable the GPIOs for the SCL/SDA Pins RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //Configure and initialize the GPIOs GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOB, &GPIO_InitStructure); //Connect GPIO pins to peripheral GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); // clock out stuff to make sure slaves arent stuck // This will also configure GPIO as AF_OD at the end //i2cUnstick(); // Init I2C peripheral I2C_DeInit(I2C1); I2C_StructInit(&i2c); I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_ERR, DISABLE); // Enable EVT and ERR interrupts - they are enabled by the first request i2c.I2C_Mode = I2C_Mode_I2C; i2c.I2C_DutyCycle = I2C_DutyCycle_2; i2c.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; i2c.I2C_Ack = I2C_Ack_Enable; i2c.I2C_OwnAddress1 = 0; i2c.I2C_ClockSpeed = 100000; I2C_Init(I2C1, &i2c); I2C_Cmd(I2C1, ENABLE); // I2C ER Interrupt nvic.NVIC_IRQChannel = I2C1_ER_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0; nvic.NVIC_IRQChannelSubPriority = 2; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); // I2C EV Interrupt nvic.NVIC_IRQChannel = I2C1_EV_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0; nvic.NVIC_IRQChannelSubPriority = 2; NVIC_Init(&nvic); }
void sens9DInit(void){ //PB10/I2C2_SCL, PB11/I2C2_SDA // XM Read Address 8bit 0x3B, Gyro 8bit Read 0xD7 GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; u8 statOk=!0; int i; I2C_DeInit(I2C2); I2C_Cmd(I2C2, DISABLE); I2C_SoftwareResetCmd(I2C2, ENABLE); I2C_SoftwareResetCmd(I2C2, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIOB->BSRRL=GPIO_Pin_10|GPIO_Pin_11; i=8; while( (i--) && ((GPIOB->IDR&GPIO_Pin_11)==0)){ delay_us(200); GPIOB->BSRRH=GPIO_Pin_10; delay_us(200); GPIOB->BSRRL=GPIO_Pin_10; } GPIO_PinAFConfig(GPIOB,GPIO_PinSource10, GPIO_AF_I2C2); GPIO_PinAFConfig(GPIOB,GPIO_PinSource11, GPIO_AF_I2C2); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOB, &GPIO_InitStructure); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_OwnAddress1 = 0x01; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed=100e3; I2C_InitStructure.I2C_DutyCycle=I2C_DutyCycle_16_9; I2C_Init(I2C2, &I2C_InitStructure); I2C_StretchClockCmd(I2C2,ENABLE); //I2C_GeneralCallCmd(I2C1,ENABLE); //I2C_AnalogFilterCmd(I2C2,ENABLE); //I2C_DigitalFilterConfig(I2C2,0x02); Not available on 407.. :( I2C_Cmd(I2C2, ENABLE); delay_us(1e3); //CTRL_REG1_G to CTRL_REG5_G 0x20..0x24 //190Hz, 25Hz cut //0.45Hz HPass //Full scale 2000dps //HPass ΝΟΤ selected // 20h 21h 22h 23h 24h u8 GyroCTRreg[5]={0b01111111, 0b00100101, 0b00000000, 0b10100000, 0b00000000 };//24h ->0b00000000 disables LPF2 //Enable 0b00000010 I2C_write(Gy_addr,0x20|0x80,GyroCTRreg,5); // HP filter bypassed // 1Fh 20h 21h 22h 23h 24h 25h 26h u8 XM_CTRreg[8]={0b00000000, 0b01111111, 0b11001000, 0b00000000, 0b00000000, 0b11110100, 0b00000000, 0b00000000 };//200Hz, 50HzBW, 4G, Magn:100Hz,2Gauss I2C_write(XM_addr,0x1F|0x80,XM_CTRreg,8); }
bool spi_init() { // --- clocks ---- GPIO_InitTypeDef GPIO_InitStructure; /*!< Enable the SPI clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); /*!< Enable GPIO clocks */ RCC_AHB1PeriphClockCmd(SPI_SCK_GPIO_CLK | SPI_MISO_GPIO_CLK | SPI_MOSI_GPIO_CLK | SPI_CS_GPIO_CLK, ENABLE); // --- GPIOs ---- /*!< Connect SPI pins to AF */ GPIO_PinAFConfig(SPI_SCK_GPIO_PORT, SPI_SCK_GPIO_PINSOURCE, GPIO_AF_SPI2); GPIO_PinAFConfig(SPI_MISO_GPIO_PORT, SPI_MISO_GPIO_PINSOURCE, GPIO_AF_SPI2); GPIO_PinAFConfig(SPI_MOSI_GPIO_PORT, SPI_MOSI_GPIO_PINSOURCE, GPIO_AF_SPI2); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; /*!< SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = SPI_SCK_GPIO_PIN; GPIO_Init(SPI_SCK_GPIO_PORT, &GPIO_InitStructure); /*!< SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = SPI_MOSI_GPIO_PIN; GPIO_Init(SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /*!< SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = SPI_MISO_GPIO_PIN; GPIO_Init(SPI_MISO_GPIO_PORT, &GPIO_InitStructure); /*!< Configure CS pin in output pushpull mode ********************/ GPIO_InitStructure.GPIO_Pin = SPI_CS_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(SPI_CS_GPIO_PORT, &GPIO_InitStructure); // ---- SPI ---- SPI_InitTypeDef SPI_InitStructure; /*!< Deselect bus: Chip Select high */ spi_CS_high(); /*!< SPI configuration */ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // lets go slow first SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); /*!< Enable the sFLASH_SPI */ SPI_Cmd(SPI2, ENABLE); return true; }
/*************************************************************************************************** * @fn uarts_regist * * @brief 串口资源注册 * @param NULL * @return null ***************************************************************************************************/ void uarts_regist(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_InitTypeDef USART_InitStructure; DMA_InitTypeDef DMA_InitStructure; uint32_t temp; //USART_S_PORT_TxPin RCC_AHB1PeriphClockCmd(USART_S_PORT_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = USART_S_PORT_TxPin ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(USART_S_PORT_GPIO, &GPIO_InitStructure); GPIO_PinAFConfig(USART_S_PORT_GPIO, USART_S_PORT_Tx_Pinsource, USART_S_PORT_GPIO_AF); //USART_S_PORT_RxPin GPIO_InitStructure.GPIO_Pin = USART_S_PORT_RxPin ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(USART_S_PORT_GPIO, &GPIO_InitStructure); GPIO_PinAFConfig(USART_S_PORT_GPIO, USART_S_PORT_Rx_Pinsource, USART_S_PORT_GPIO_AF); //USARTdbg_TxPin RCC_AHB1PeriphClockCmd(USARTdbg_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = USARTdbg_TxPin ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(USARTdbg_GPIO, &GPIO_InitStructure); GPIO_PinAFConfig(USARTdbg_GPIO, USARTdbg_Tx_Pinsource, USARTdbg_GPIO_AF); //USARTdbg_RxPin GPIO_InitStructure.GPIO_Pin = USARTdbg_RxPin ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(USARTdbg_GPIO, &GPIO_InitStructure); GPIO_PinAFConfig(USARTdbg_GPIO, USARTdbg_Rx_Pinsource, USARTdbg_GPIO_AF); //---------------------USART_S_PORT串口功能配置--------------------- //打开串口对应的外设时钟 RCC_APB1PeriphClockCmd(USART_S_PORT_CLK, ENABLE); //串口发DMA配置 //启动DMA时钟 RCC_AHB1PeriphClockCmd(USART_S_PORT_DMA_CLK, ENABLE); //DMA1通道配置 //关闭通道 DMA_Cmd(USART_S_PORT_Tx_DMA_STREAM,DISABLE); DMA_DeInit(USART_S_PORT_Tx_DMA_STREAM); //通道地址 DMA_InitStructure.DMA_Channel = USART_S_PORT_Tx_DMA_Channel; //外设地址 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART_S_PORT->DR); //内存地址 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Sport_TxBuffer; //dma传输方向单向 DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //设置DMA在传输时缓冲区的长度 DMA_InitStructure.DMA_BufferSize = S_PORT_TX_BUFF_LEN; //设置DMA的外设递增模式,一个外设 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的内存递增模式 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //外设数据字长 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //内存数据字长 DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; //设置DMA的传输模式 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的优先级别 DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(USART_S_PORT_Tx_DMA_STREAM,&DMA_InitStructure); //清中断 DMA_ClearITPendingBit(USART_S_PORT_Tx_DMA_STREAM, USART_S_PORT_Tx_DMA_FLAG); //使能中断 DMA_ITConfig(USART_S_PORT_Tx_DMA_STREAM,DMA_IT_TC,ENABLE); //串口收DMA配置 //关闭通道 DMA_Cmd(USART_S_PORT_Rx_DMA_STREAM,DISABLE); DMA_DeInit(USART_S_PORT_Rx_DMA_STREAM); //通道地址 DMA_InitStructure.DMA_Channel = USART_S_PORT_Rx_DMA_Channel; //外设地址 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART_S_PORT->DR); //内存地址 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Sport_RxBuffer; //dma传输方向单向 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //设置DMA在传输时缓冲区的长度 DMA_InitStructure.DMA_BufferSize = S_PORT_RX_BUFF_LEN; //设置DMA的外设递增模式,一个外设 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的内存递增模式 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //外设数据字长 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //内存数据字长 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //设置DMA的传输模式 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的优先级别 DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(USART_S_PORT_Rx_DMA_STREAM,&DMA_InitStructure); DMA_Cmd(USART_S_PORT_Rx_DMA_STREAM,ENABLE); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //初始化串口 USART_Init(USART_S_PORT,&USART_InitStructure); //中断配置 USART_ITConfig(USART_S_PORT,USART_IT_TC,DISABLE); USART_ITConfig(USART_S_PORT,USART_IT_RXNE,DISABLE); USART_ITConfig(USART_S_PORT,USART_IT_IDLE,ENABLE); //配置UART中断 NVIC_InitStructure.NVIC_IRQChannel = USART_S_PORT_IRQn; //通道设置为串口1中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USART_S_PORT_Rx_NVIC_PRIORITY; //中断占先等级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断 NVIC_Init(&NVIC_InitStructure); //DMA发送中断 NVIC_InitStructure.NVIC_IRQChannel = USART_S_PORT_Tx_DMA_IRQn; //通道设置为串口1中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USART_S_PORT_Tx_DMA_NVIC_PRIORITY; //中断占先等级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断 NVIC_Init(&NVIC_InitStructure); //清中断 temp = USART_S_PORT->SR; temp = USART_S_PORT->DR; //清USART_IT_IDLE标志 //采用DMA方式发送 // USART_DMACmd(USART_S_PORT,USART_DMAReq_Tx,ENABLE); // //采用DMA方式接收 USART_DMACmd(USART_S_PORT,USART_DMAReq_Rx,ENABLE); //启动串口 USART_Cmd(USART_S_PORT, ENABLE); //---------------------USARTdbg串口功能配置--------------------- //打开串口对应的外设时钟 RCC_APB1PeriphClockCmd(USARTdbg_CLK, ENABLE); //串口发DMA配置 //关闭通道 DMA_Cmd(USARTdbg_Tx_DMA_STREAM,DISABLE); DMA_DeInit(USARTdbg_Tx_DMA_STREAM); //通道地址 DMA_InitStructure.DMA_Channel = USARTdbg_Tx_DMA_Channel; //外设地址 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USARTdbg->DR); //内存地址 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)dbg_TxBuffer; //dma传输方向单向 DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //设置DMA在传输时缓冲区的长度 DMA_InitStructure.DMA_BufferSize = DBG_TX_BUF_LEN; //设置DMA的外设递增模式,一个外设 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的内存递增模式 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //外设数据字长 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //内存数据字长 DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; //设置DMA的传输模式 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的优先级别 DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(USARTdbg_Tx_DMA_STREAM,&DMA_InitStructure); //清中断 DMA_ClearITPendingBit(USARTdbg_Tx_DMA_STREAM, USARTdbg_Tx_DMA_FLAG); //使能中断 DMA_ITConfig(USARTdbg_Tx_DMA_STREAM,DMA_IT_TC,ENABLE); //串口收DMA配置 //关闭通道 DMA_Cmd(USARTdbg_Rx_DMA_STREAM,DISABLE); DMA_DeInit(USARTdbg_Rx_DMA_STREAM); //通道地址 DMA_InitStructure.DMA_Channel = USARTdbg_Rx_DMA_Channel; //外设地址 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USARTdbg->DR); //内存地址 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)dbg_RxBuffer; //dma传输方向单向 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //设置DMA在传输时缓冲区的长度 DMA_InitStructure.DMA_BufferSize = DBG_RX_BUF_LEN; //设置DMA的外设递增模式,一个外设 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //设置DMA的内存递增模式 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //外设数据字长 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //内存数据字长 DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; //设置DMA的传输模式 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //设置DMA的优先级别 DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(USARTdbg_Rx_DMA_STREAM,&DMA_InitStructure); DMA_Cmd(USARTdbg_Rx_DMA_STREAM,ENABLE); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //初始化串口 USART_Init(USARTdbg,&USART_InitStructure); //中断配置 USART_ITConfig(USARTdbg,USART_IT_TC,DISABLE); USART_ITConfig(USARTdbg,USART_IT_RXNE,DISABLE); USART_ITConfig(USARTdbg,USART_IT_IDLE,ENABLE); USART_ClearITPendingBit(USARTdbg, USART_IT_RXNE); //清中断 temp = USARTdbg->SR; temp = USARTdbg->DR; //清USART_IT_IDLE标志 //配置UART中断 NVIC_InitStructure.NVIC_IRQChannel = USARTdbg_IRQn; //通道设置为串口1中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USARTdbg_Rx_NVIC_PRIORITY; //中断占先等级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断 NVIC_Init(&NVIC_InitStructure); //DMA发送中断 NVIC_InitStructure.NVIC_IRQChannel = USARTdbg_Tx_DMA_IRQn; //通道设置为串口1中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USARTdbg_Tx_DMA_NVIC_PRIORITY; //中断占先等级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断 NVIC_Init(&NVIC_InitStructure); //采用DMA方式发送 // USART_DMACmd(USARTdbg,USART_DMAReq_Tx,ENABLE); //采用DMA方式接收 USART_DMACmd(USARTdbg,USART_DMAReq_Rx,ENABLE); //启动串口 USART_Cmd(USARTdbg, ENABLE); }