/******************************************************************************** * FunctionName: STM32_Init * * Description : STM32 外设初始化 * * Parameters : None * * Returns : None *******************************************************************************/ void STM32_Init(void) { MyRCCInit(); MyUsartInit(); MyExtiNvicInit(); DMA_Configuration(); }
void Hardware_Configuration() { //Config RCC(clock PLL flash periph_clock) RCC_Configuration(); //Config GPIO GPIO_Configuration(); //Config EXTI //EXTI_Configuration(); //Config TIM //TIM_Configuration(); //Config USART USART_Configuration(); //Config NVIC NVIC_Configuration(); //Config DMA DMA_Configuration(); //Config I2C I2C_Configuration(); }
/******************************************************************************* * Function Name : main * Description : Main program. * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif /* Configure the system clocks */ RCC_Configuration(); /* Configure GPIOs */ GPIO_Configuration(); /* Configures the EXTI Lines */ EXTI_Configuration(); /* Configures the DMA Channel */ DMA_Configuration(); /* Configures the USART1 */ USART_Configuration(); #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the DMA1 Channel 5 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the EXTI9_5 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_Init(&NVIC_InitStructure); while (1) { if(LowPowerMode == 1) { GPIO_ResetBits(GPIO_LED, GPIO_Pin_7 | GPIO_Pin_8); /* Request to enter WFI mode */ __WFI(); LowPowerMode = 0; } Delay(0xFFFFF); GPIO_WriteBit(GPIO_LED, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIO_LED, GPIO_Pin_6))); } }
void adc_hw_init(void) { GPIO_Configuration(); TIM_Configuration(); DMA_Configuration(); ADC_Configuration(); NVIC_Configuration(); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* System Clocks Configuration */ RCC_Configuration(); /* GPIO Configuration */ GPIO_Configuration(); /* DMA Configuration */ DMA_Configuration(); /* TIM1 DMA Transfer example ------------------------------------------------- TIM1CLK = 72 MHz, Prescaler = 0, TIM1 counter clock = 72 MHz The TIM1 Channel3 is configured to generate a complementary PWM signal with a frequency equal to: TIM1 counter clock / (TIM1_Period + 1) = 17.57 KHz and a variable duty cycle that is changed by the DMA after a specific number of Update DMA request. The number of this repetitive requests is defined by the TIM1 Repetion counter, each 3 Update Requests, the TIM1 Channel 3 Duty Cycle changes to the next new value defined by the SRC_Buffer . -----------------------------------------------------------------------------*/ /* TIM1 Peripheral Configuration --------------------------------------------*/ /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 4095; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 2; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* Channel 3 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = 127; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC3Init(TIM1, &TIM_OCInitStructure); /* TIM1 Update DMA Request enable */ TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE); /* TIM1 counter enable */ TIM_Cmd(TIM1, ENABLE); /* Main Output Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); while (1) {} }
static rt_size_t codec_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) { struct codec_device* device; struct codec_data_node* node; rt_uint32_t level; rt_uint16_t next_index; device = (struct codec_device*) dev; RT_ASSERT(device != RT_NULL); next_index = device->put_index + 1; if (next_index >= DATA_NODE_MAX) next_index = 0; /* check data_list full */ if (next_index == device->read_index) { rt_set_errno(-RT_EFULL); return 0; } level = rt_hw_interrupt_disable(); node = &device->data_list[device->put_index]; device->put_index = next_index; /* set node attribute */ node->data_ptr = (rt_uint16_t*) buffer; node->data_size = size >> 1; /* size is byte unit, convert to half word unit */ next_index = device->read_index + 1; if (next_index >= DATA_NODE_MAX) next_index = 0; /* check data list whether is empty */ if (next_index == device->put_index) { DMA_Configuration((rt_uint32_t) node->data_ptr, node->data_size); #if CODEC_MASTER_MODE if ((r06 & MS) == 0) { CODEC_I2S_PORT->I2SCFGR |= SPI_I2SCFGR_I2SE; r06 |= MS; codec_send(r06); } #endif } rt_hw_interrupt_enable(level); return size; }
//------------------------------------------------------------------------------ // === Initialize Function === //------------------------------------------------------------------------------ void Init_Main(void) { RCC_Configuration(); // RCC_GetClocksFreq(&rcc_clocks); GPIO_Configuration(); NVIC_Configuration(); DMA_Configuration(); ADC_Configuration(); // TIM_Configuration(); EXTI_Configuration(); nRF24_init(); // nRF24_ClearIRQFlags(); USART1_Init(57600); // in 36mhz error 0% }
/** * @brief Main program. * @param None * @retval : None */ int main(void) { /* Configure the system clocks */ RCC_Configuration(); /* Configure GPIOs */ GPIO_Configuration(); /* Configures the EXTI Lines */ EXTI_Configuration(); /* Configures the DMA Channel */ DMA_Configuration(); /* Configures the USART1 */ USART_Configuration(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the DMA1 Channel 5 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the EXTI9_5 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_Init(&NVIC_InitStructure); while (1) { if(LowPowerMode == 1) { GPIO_ResetBits(GPIO_LED, GPIO_Pin_7 | GPIO_Pin_8); /* Request to enter WFI mode */ __WFI(); LowPowerMode = 0; } Delay(0xFFFFF); GPIO_WriteBit(GPIO_LED, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIO_LED, GPIO_Pin_6))); } }
int main(void) { initButton(); RCC_Configuration(); //NVIC_Configuration(); GPIO_Configuration(); UART4_Configuration(); DMA_Configuration(); int buttonPressed = 0; while (1) { if(GPIO_ReadInputDataBit(USER_BUTTON_GPIO_PORT, USER_BUTTON_PIN)) { if(buttonPressed == 0) { /* reinitializes the dma to trigger the sending again */ DMA_DeInit(DMA1_Stream4); DMA_Init(DMA1_Stream4, &DMA_InitStructure); DMA_ITConfig(DMA1_Stream4, DMA_IT_TC, ENABLE); DMA_Cmd(DMA1_Stream4, ENABLE); } buttonPressed = 1; green.on(); } else { green.off(); buttonPressed = 0; } orange.on(); } return(0); }
/** * @brief Main program. * @param None * @retval None */ int main() { RCC_Configuration(); GPIO_Configuration(); GD_EVAL_LEDInit(LED1); SysTick_Configuration(); DMA_Configuration(); TIMER_Configuration(); while(1) { GD_EVAL_LEDOn(LED1); Delay_1ms(1000); GD_EVAL_LEDOff(LED1); Delay_1ms(1000); } }
/******************************************************************************** * sampleAcquisitionInit * * Init the sampling routine. * Blocking function. * * @param Void * @return 0 if working *******************************************************************************/ void sampleAcquisitionInit( void ) { /***************** * SYSTEM CLOCKS * *****************/ RCC_Configuration(); /******** * GPIO * ********/ GPIO_Configuration(); /******* * DMA * *******/ DMA_Configuration(); /******* * ADC * *******/ ADC_Configuration(); /********* * TIMER * *********/ TIMER_Configuration(); /******** * NVIC * ********/ IT_Configuration(); /********* * START * *********/ }
//------------------------------------------------------------------------------ // === Initialize Function === //------------------------------------------------------------------------------ void Init_Main(void) { RCC_Configuration(); //RCC_GetClocksFreq(&rcc_clocks); GPIO_Configuration(); NVIC_Configuration(); DMA_Configuration(); ADC_Configuration(); TIM_Configuration(); EXTI_Configuration(); Tim_Encoder_initial(); USART1_Init(57600); // in 36mhz error 0% USART3_Init(115200);// in 72mhz error 0% //if (SysTick_Config(rcc_clocks.SYSCLK_Frequency / 1000)) //{ /* Capture error */ // while (1); //} }
int main(void) { u8 i = 0; u8 tickCountCpu = 0; u8 tickCountDma = 0; RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); USART_Configuration(); DMA_Configuration(); SysTick_Configuration(); tick = 0 ; for(i = 0; i < BUFFER_SIZE; i++) { dstBuffer[i] = srcConstBuffer[i]; } tickCountCpu = tick; for(i = 0; i < BUFFER_SIZE; i++) { dstBuffer[i] = 0; } tick = 0; DMA_Cmd(DMA1_Channel6, ENABLE); while(currDataCounter!=0); tickCountDma = tick; if(strncmp( (const char*)srcConstBuffer, (const char*)dstBuffer , BUFFER_SIZE ) == 0){ printf("\r\nTransmit Success\r\n"); }else{ printf("\r\nTransmit Fault\r\n"); } printf("\r\nCPU cost %d us\r\n",tickCountCpu); printf("\r\nDMA cost %d us\r\n",tickCountDma); while(1); }
/******************************************************************************* * Function Name : DMA1_Channel5_IRQHandler * Description : This function handles DMA1 Channel 5 interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void DMA1_Channel5_IRQHandler(void) { if(DMA_GetITStatus(DMA1_IT_TC5)) { DMA_ClearITPendingBit(DMA1_IT_TC5); /* Check the received buffer */ TestStatus = Buffercmp16(SRC_Buffer, DST_Buffer, 10); if(TestStatus == 0) { GPIO_WriteBit(GPIO_LED, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIO_LED, GPIO_Pin_7))); } else { GPIO_WriteBit(GPIO_LED, GPIO_Pin_8, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIO_LED, GPIO_Pin_8))); } /* Re-configure DMA1 */ DMA_Configuration(); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* GPIO Configuration */ GPIO_Configuration(); /* DMA Configuration */ DMA_Configuration(); /* TIM1 DMA Transfer example ------------------------------------------------- TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density and Connectivity line devices and to 24 MHz for Low-Density Value line and Medium-Density Value line devices. The objective is to configure TIM1 channel 3 to generate complementary PWM signal with a frequency equal to 17.57 KHz: - TIM1_Period = (SystemCoreClock / 17570) - 1 and a variable duty cycle that is changed by the DMA after a specific number of Update DMA request. The number of this repetitive requests is defined by the TIM1 Repetition counter, each 3 Update Requests, the TIM1 Channel 3 Duty Cycle changes to the next new value defined by the SRC_Buffer . -----------------------------------------------------------------------------*/ /* Compute the value to be set in ARR register to generate signal frequency at 17.57 Khz */ TimerPeriod = (SystemCoreClock / 17570 ) - 1; /* Compute CCR1 value to generate a duty cycle at 50% */ SRC_Buffer[0] = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10); /* Compute CCR1 value to generate a duty cycle at 37.5% */ SRC_Buffer[1] = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000); /* Compute CCR1 value to generate a duty cycle at 25% */ SRC_Buffer[2] = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100); /* TIM1 Peripheral Configuration --------------------------------------------*/ /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = TimerPeriod; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 2; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* Channel 3 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = SRC_Buffer[0]; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC3Init(TIM1, &TIM_OCInitStructure); /* TIM1 Update DMA Request enable */ TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE); /* TIM1 counter enable */ TIM_Cmd(TIM1, ENABLE); /* Main Output Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); while (1) {} }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* Configure the system clocks */ RCC_Configuration(); /* Initialize Leds and Key Button mounted on STM3210X-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); STM_EVAL_PBInit(Button_KEY, Mode_EXTI); /* Configures the DMA Channel */ DMA_Configuration(); /* EVAL_COM1 configuration ---------------------------------------------------*/ /* EVAL_COM1 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ 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; STM_EVAL_COMInit(COM1, &USART_InitStructure); USART_DMACmd(EVAL_COM1, USART_DMAReq_Rx, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the USARTy_DMA1_IRQn Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USARTy_DMA1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the EXTI9_5 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_Init(&NVIC_InitStructure); while (1) { if(LowPowerMode == 1) { STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); /* Request to enter WFI mode */ __WFI(); LowPowerMode = 0; } Delay(0xFFFFF); STM_EVAL_LEDToggle(LED1); } }
/* * Init all related hardware in here * rt_hw_serial_init() will register all supported USART device */ void rt_hw_usart_init() { USART_InitTypeDef USART_InitStructure; RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); DMA_Configuration(); /* uart init */ #ifdef RT_USING_UART1 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(USART1, &USART_InitStructure); /* register uart1 */ rt_hw_serial_register(&uart1_device, "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, &uart1); /* enable interrupt */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); #endif #ifdef RT_USING_UART2 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(USART2, &USART_InitStructure); /* register uart2 */ rt_hw_serial_register(&uart2_device, "uart2", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, &uart2); /* Enable USART2 DMA Rx request */ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); #endif #ifdef RT_USING_UART3 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(USART3, &USART_InitStructure); // uart3_dma_tx.dma_channel= UART3_TX_DMA; /* register uart3 */ rt_hw_serial_register(&uart3_device, "uart3", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX, &uart3); /* Enable USART3 DMA Tx request */ USART_DMACmd(USART3, USART_DMAReq_Tx , ENABLE); /* enable interrupt */ USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); #endif }
void codec_dma_isr(void) { /* switch to next buffer */ rt_uint16_t next_index; void* data_ptr; next_index = codec.read_index + 1; if (next_index >= DATA_NODE_MAX) next_index = 0; /* save current data pointer */ data_ptr = codec.data_list[codec.read_index].data_ptr; #if !CODEC_MASTER_MODE if (codec_sr_new) { I2S_Configuration(codec_sr_new); I2S_Cmd(CODEC_I2S_PORT, ENABLE); codec_sr_new = 0; } #endif codec.read_index = next_index; if (next_index != codec.put_index) { /* enable next dma request */ DMA_Configuration((rt_uint32_t) codec.data_list[codec.read_index].data_ptr, codec.data_list[codec.read_index].data_size); #if CODEC_MASTER_MODE if ((r06 & MS) == 0) { CODEC_I2S_PORT->I2SCFGR |= SPI_I2SCFGR_I2SE; r06 |= MS; codec_send(r06); } #endif } else { #if CODEC_MASTER_MODE if (r06 & MS) { CODEC_I2S_DMA->CCR &= ~DMA_CCR1_EN; while ((CODEC_I2S_PORT->SR & SPI_I2S_FLAG_TXE) == 0); while ((CODEC_I2S_PORT->SR & SPI_I2S_FLAG_BSY) != 0); CODEC_I2S_PORT->I2SCFGR &= ~SPI_I2SCFGR_I2SE; r06 &= ~MS; codec_send(r06); } #endif rt_kprintf("*\n"); } /* notify transmitted complete. */ if (codec.parent.tx_complete != RT_NULL) { codec.parent.tx_complete(&codec.parent, data_ptr); } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* Configure the system clocks */ RCC_Configuration(); /* Initialize Leds and Key Button mounted on STM3210X-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); /* Configures the DMA Channel */ DMA_Configuration(); /* EVAL_COM1 configuration ---------------------------------------------------*/ /* EVAL_COM1 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ 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; STM_EVAL_COMInit(COM1, &USART_InitStructure); USART_DMACmd(EVAL_COM1, USART_DMAReq_Rx, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the USARTy_DMA1_IRQn Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USARTy_DMA1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the EXTI9_5 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_Init(&NVIC_InitStructure); while (1) { if(LowPowerMode == 1) { STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); /* Request to enter WFI mode */ __WFI(); LowPowerMode = 0; } Delay(0xFFFFF); STM_EVAL_LEDToggle(LED1); } }
static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message) { struct stm32_spi_bus * stm32_spi_bus = (struct stm32_spi_bus *)device->bus; struct rt_spi_configuration * config = &device->config; SPI_TypeDef * SPI = stm32_spi_bus->SPI; struct stm32_spi_cs * stm32_spi_cs = device->parent.user_data; rt_uint32_t size = message->length; /* take CS */ if(message->cs_take) { GPIO_ResetBits(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin); } #ifdef SPI_USE_DMA if(message->length > 32) { if(config->data_width <= 8) { DMA_Configuration(stm32_spi_bus, message->send_buf, message->recv_buf, message->length); SPI_I2S_DMACmd(SPI, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, ENABLE); while (DMA_GetFlagStatus(stm32_spi_bus->DMA_Channel_RX_FLAG_TC) == RESET || DMA_GetFlagStatus(stm32_spi_bus->DMA_Channel_TX_FLAG_TC) == RESET); SPI_I2S_DMACmd(SPI, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, DISABLE); } // rt_memcpy(buffer,_spi_flash_buffer,DMA_BUFFER_SIZE); // buffer += DMA_BUFFER_SIZE; } else #endif { if(config->data_width <= 8) { const rt_uint8_t * send_ptr = message->send_buf; rt_uint8_t * recv_ptr = message->recv_buf; while(size--) { rt_uint8_t data = 0xFF; if(send_ptr != RT_NULL) { data = *send_ptr++; } //Wait until the transmit buffer is empty while (SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE) == RESET); // Send the byte SPI_I2S_SendData(SPI, data); //Wait until a data is received while (SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE) == RESET); // Get the received data data = SPI_I2S_ReceiveData(SPI); if(recv_ptr != RT_NULL) { *recv_ptr++ = data; } } } else if(config->data_width <= 16) { const rt_uint16_t * send_ptr = message->send_buf; rt_uint16_t * recv_ptr = message->recv_buf; while(size--) { rt_uint16_t data = 0xFF; if(send_ptr != RT_NULL) { data = *send_ptr++; } //Wait until the transmit buffer is empty while (SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE) == RESET); // Send the byte SPI_I2S_SendData(SPI, data); //Wait until a data is received while (SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE) == RESET); // Get the received data data = SPI_I2S_ReceiveData(SPI); if(recv_ptr != RT_NULL) { *recv_ptr++ = data; } } } } /* release CS */ if(message->cs_release) { GPIO_SetBits(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin); } return message->length; };
/* * Init all related hardware in here * rt_hw_serial_init() will register all supported USART device */ void rt_hw_usart_init() { struct stm32_uart* uart; struct serial_configure config; RCC_Configuration(); GPIO_Configuration(); DMA_Configuration(); /* uart init */ #ifdef RT_USING_UART1 uart = &uart1; config.baud_rate = BAUD_RATE_115200; config.bit_order = BIT_ORDER_LSB; config.data_bits = DATA_BITS_8; config.parity = PARITY_NONE; config.stop_bits = STOP_BITS_1; config.invert = NRZ_NORMAL; serial1.ops = &stm32_uart_ops; serial1.int_rx = &uart1_int_rx; serial1.config = config; NVIC_Configuration(&uart1); /* register UART1 device */ rt_hw_serial_register(&serial1, "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, uart); #endif #ifdef RT_USING_UART2 uart = &uart2; config.baud_rate = BAUD_RATE_115200; config.bit_order = BIT_ORDER_LSB; config.data_bits = DATA_BITS_8; config.parity = PARITY_NONE; config.stop_bits = STOP_BITS_1; config.invert = NRZ_NORMAL; serial2.ops = &stm32_uart_ops; serial2.int_rx = &uart2_int_rx; serial2.config = config; NVIC_Configuration(&uart2); /* register UART2 device */ rt_hw_serial_register(&serial2, "uart2", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, uart); #endif #ifdef RT_USING_UART3 uart = &uart3; config.baud_rate = BAUD_RATE_115200; config.bit_order = BIT_ORDER_LSB; config.data_bits = DATA_BITS_8; config.parity = PARITY_NONE; config.stop_bits = STOP_BITS_1; config.invert = NRZ_NORMAL; serial3.ops = &stm32_uart_ops; serial3.int_rx = &uart3_int_rx; serial3.config = config; NVIC_Configuration(&uart3); /* register UART3 device */ rt_hw_serial_register(&serial3, "uart3", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, uart); #endif }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ uint32_t delay; uint8_t sendFlag_Ai = 0; SOE_Struct soe; uint32_t start = 0; /* System clocks configuration ---------------------------------------------*/ SystemInit(); RCC_ClocksTypeDef clock; RCC_GetClocksFreq(&clock); RCC_Configuration(); GPIO_Configuration(); ADC_Configuration(); COM3_Configuration(9600); DMA_Configuration(); TIM_UART_Config(); //uart3-rx FREQ_MEA_Init(); //freq采样 TIM2_Configuration(); //adc采样 SysTick_Configuration(); //DI采样 RTC_Init(); NVIC_Configuration(); DataBase_Init(); //memset(DiStatus_DI, 0, sizeof(DiStatus_Type)*DI_NUM); //初始化DI内存列表 GetComAddr(); SetCurrent.ChannelCoef[0] =1050;//1#:1029;2#:1028 SetCurrent.ChannelCoef[1] =1029;//1#:1029;2#:1027 SetCurrent.ChannelCoef[2] =1050;//1#:1030;2#:1028 SetCurrent.ChannelCoef[3] =1028;//1#:1033;2#:1028 SetCurrent.ChannelCoef[4] =1029;//1#:1033;2#:1029 SetCurrent.ChannelCoef[5] =1028;//1#:1033;2#:1028 SetCurrent.ChannelCoef[6] =1025;//1#:1029;2#:1028 SetCurrent.ChannelCoef[7] =1026;//1#:1029;2#:1027 SetCurrent.ChannelCoef[8] =1027;//1#:1030;2#:1028 #ifdef WATCHDOG IWDG_Configuration(); #endif DMA_Cmd(DMA1_Channel3, ENABLE); // Emable TIM3, 使能串口接收通道 TIM_Cmd(TIM3, ENABLE); //UART3接收,使能 while (1) { Di_PostWork(); if(63==PeriodCycle_Index) { TOTAL_MEASURE(&meas); SequenceFilter_2(&meas); SequenceFilter_0(&meas); ValueScaling(MeaTab,&meas); //tyh:20150629 添加遥测数据上送 if(Begin_AI_Send) { if(Is_new_soe()) { get_soe(&soe, 1); SoeResponse(soe); } else { if((sendFlag_Ai%3)==0) AiResponse(0); else { if((sendFlag_Ai%5)==0) //tyh:20150803 增加遥信数据上送 DiResponse(0); } sendFlag_Ai++; } } } if(Flag_Uart_Recv) BusCalling_Process(Flag_Uart_Recv); //处理数据 #ifdef WATCHDOG WDGFeeding(); #endif } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); /* Configure the DMA */ DMA_Configuration(); /* USARTy and USARTz configuration -------------------------------------------*/ /* USARTy and USARTz configured as follow: - BaudRate = 230400 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 230400; 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; /* Configure USARTy */ USART_Init(USARTy, &USART_InitStructure); /* Configure USARTz */ USART_Init(USARTz, &USART_InitStructure); /* Enable USARTy DMA TX request */ USART_DMACmd(USARTy, USART_DMAReq_Tx, ENABLE); /* Enable USARTz DMA TX request */ USART_DMACmd(USARTz, USART_DMAReq_Tx, ENABLE); /* Enable the USARTz Receive Interrupt */ USART_ITConfig(USARTz, USART_IT_RXNE, ENABLE); /* Enable USARTy */ USART_Cmd(USARTy, ENABLE); /* Enable USARTz */ USART_Cmd(USARTz, ENABLE); /* Enable USARTy DMA TX Channel */ DMA_Cmd(USARTy_Tx_DMA_Channel, ENABLE); /* Enable USARTz DMA TX Channel */ DMA_Cmd(USARTz_Tx_DMA_Channel, ENABLE); /* Receive the TxBuffer2 */ while(index < TxBufferSize2) { while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET) { } RxBuffer1[index++] = USART_ReceiveData(USARTy); } /* Wait until USARTy TX DMA1 Channel Transfer Complete */ while (DMA_GetFlagStatus(USARTy_Tx_DMA_FLAG) == RESET) { } /* Wait until USARTz TX DMA1 Channel Transfer Complete */ while (DMA_GetFlagStatus(USARTz_Tx_DMA_FLAG) == RESET) { } /* Check the received data with the send ones */ TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2); /* TransferStatus1 = PASSED, if the data transmitted from USARTz and received by USARTy are the same */ /* TransferStatus1 = FAILED, if the data transmitted from USARTz and received by USARTy are different */ TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1); /* TransferStatus2 = PASSED, if the data transmitted from USARTy and received by USARTz are the same */ /* TransferStatus2 = FAILED, if the data transmitted from USARTy and received by USARTz are different */ while (1) { } }