The serial port of STM32F407 uses DMA to send and receive data, and stm32f407dma

Source: Internet
Author: User

The serial port of STM32F407 uses DMA to send and receive data, and stm32f407dma

The serial port of STM32F407 uses DMA to send and receive data


For more information, see http://blog.csdn.net/jdh99, :jdh.


Environment:

HOST: WIN8

Development Environment: MDK5.13

Mcu: stm32f407VGT6


Note:

Previously, we used STM32F103 to Implement DMA to send and receive serial data. Now we use STM32F407 in the project, so we port this mechanism to F4.

Using DMA to send and receive serial data on STM32F103:

The serial port of STM32 sends data in DMA mode for testing.

The serial port of STM32 receives data in DMA mode for testing.


Source code:

Serial Port initialization code:

/*************************************** ******* **************************************** * *********************/Static void init_uart (void) {// define the interrupt struct javasnvic_initstructure; // define the IO initialization struct GPIO_InitTypeDef GPIO_InitStructure; // define the serial port struct USART_InitTypeDef USART_InitStructure; // define the DMA struct mongodma_initstructure; // open the peripheral clock RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2, ENABLE) corresponding to the serial port; // configure the serial port to send DMA; // start the DMA clock RCC_AHB1PeriphClockCmd (enabled, ENABLE ); // set the DMA sending interruption to DMA1_Stream6_IRQn; bandwidth = 2; bandwidth = 0; bandwidth = ENABLE; NVIC_Init (& NVIC_InitStructure); // configure the DMA channel // DMA_DeInit (DMA_Channel_4 ); DMA_InitStructure.DMA_Channel = DMA_Channel_4; // peripheral address frequency = (uint32_t) (& USART2-> DR); // memory address frequency = (uint32_t) Tx_Buf_Gsm; // dma transmission direction = DMA_DIR_MemoryToPeripheral; // set the length of the buffer zone during DMA transmission to TX_LEN_GSM; // set the peripheral increment mode of DMA, One peripheral region = DMA_PeripheralInc_Disable; // set the memory increment mode of DMA to memory increment = DMA_MemoryInc_Enable; // The peripheral data wordlength increment = DMA_PeripheralDataSize_Byte; // The Memory Data wordlength increment = DMA_PeripheralDataSize_Byte; // set the DMA transmission mode DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // set the priority level of DMA = DMA_Priority_High; // specify that if the FIFO mode or direct mode is used for the specified stream: do not enable the FIFO mode scheme = DMA_FIFOMode_Disable; // specify the FIFO threshold level scheme = dma_1_othreshold_halffull; // specify the Burst transfer configuration memory transfer scheme = DMA_MemoryBurst_Single; // specify the Burst transfer configuration for peripheral transfer */region = bytes; // configure DMA1 channel DMA_Init (DMA1_Stream6, & DMA_InitStructure); // enable DMA_ITConfig (DMA1_Stream6, DMA_IT_TC, ENABLE); // configure the serial port to receive DMA // start the DMA clock RCC_AHB1PeriphClockCmd (enabled, ENABLE); // configure the DMA channel // DMA_DeInit (DMA2_Channel3); token = DMA_Channel_4; // peripheral address serial = (uint32_t) (& USART2-> DR); // memory address serial = (uint32_t) Rx_Buf_Gsm; // dma Transmission Direction serial = DMA_DIR_PeripheralToMemory; // set the length of the buffer during DMA transmission. DMA_InitStructure.DMA_BufferSize = RX_LEN_GSM; // set the peripheral increment mode of DMA. One peripheral region is DMA_PeripheralInc_Disable; // set the memory increment mode of DMA to memory increment = DMA_MemoryInc_Enable; // The data field length of the peripheral device, namely DMA_PeripheralDataSize_Byte; // The memory data field length, namely DMA_MemoryDataSize_Byte; // set the DMA transmission mode DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // set the priority of the DMA parameter = DMA_Priority_VeryHigh; // specify whether the FIFO mode or direct mode is used for the specified stream: do not enable the FIFO mode scheme = DMA_FIFOMode_Disable; // specify the FIFO threshold level scheme = dma_1_othreshold_halffull; // specify the Burst transfer configuration memory transfer scheme = DMA_MemoryBurst_Single; // specify the Burst transfer configuration for peripheral transfer */region = DMA_PeripheralBurst_Single; // configure DMA1 channel DMA_Init (DMA1_Stream5, & DMA_InitStructure); // enable channel DMA_Cmd (DMA1_Stream5, ENABLE); // initialize the serial port parameter serial = serial; Serial = USART_StopBits_1; Serial = USART_Parity_No; Serial = serial; Serial = USART_Mode_Rx | USART_Mode_Tx; Serial = DEFAULT_BAUD_GSM; // initialize the serial port USART_Init (USART2, & USART_InitStructure); // interrupt the configuration USART_ITConfig (USART2, DISABLE, DISABLE); USART_ITConfig (USART2, DISABLE, DISABLE); USART_ITConfig, ENABLE); // configuration interrupt // NVIC_PriorityGroupConfig (NVIC_PriorityGroup_3); interrupt = USART2_IRQn; // the channel is set to serial port interrupt timeout = 0; // The interrupt severity threshold = 0; // interrupt response priority = ENABLE; // ENABLE interrupt NVIC_Init (& NVIC_InitStructure); // send USART_DMACmd (USART2, USART_DMAReq_Tx, ENABLE) in DMA mode ); // receive USART_DMACmd (USART2, random, ENABLE) in DMA mode; // interrupt configuration USART_ITConfig (USART2, USART_IT_TC, DISABLE); USART_ITConfig (USART2, USART_IT_RXNE, DISABLE ); USART_ITConfig (USART2, frequency, DISABLE); USART_ITConfig (USART2, USART_IT_IDLE, ENABLE); // start the serial port USART_Cmd (USART2, ENABLE); // set the IO port clock frequency (frequency, ENABLE); GPIO_PinAFConfig (GPIOA, GPIO_PinSource2, callback); GPIO_PinAFConfig (GPIOA, gpio_pinsoure3, connector); // pin mode: output port restart = GPIO_Mode_AF; // type: push-pull mode: Export = GPIO_OType_PP; // pull-up and pull-down settings: Export = GPIO_PuPd_UP; // IO port speed: Export = GPIO_Speed_100MHz; // pin-specific export = GPIO_Pin_2; // initialize GPIO_Init, & GPIO_InitStructure); // pin mode: input port serial = GPIO_Mode_AF; // set serial = GPIO_PuPd_NOPULL; // specify the serial = GPIO_Pin_3; // initialize GPIO_Init (GPIOA, & GPIO_InitStructure );}


Send data:

/*************************************** ******************************* Interface functions: send data to the gsm module * parameter: data: storage address of the sent data * size: number of sent data bytes ************************************ * *******************************/void drv_gsm_tx (uint8_t * data, uint16_t size) {// wait for idle while (idle); Flag_Tx_Gsm_Busy = 1; // copy data memcpy (Tx_Buf_Gsm, data, size); // set the transmission data length DMA_SetCurrDataCounter (dmaw.stream6, size); // open the DMA and start sending DMA_Cmd (DMA1_Stream6, ENABLE );}


Interrupt handling function:

/*************************************** ******************************* Interface functions: *********************************** * ********************************/void drv_gsm_deal_irq_dma_tx (void) {if (DMA_GetITStatus (dmaw.stream6, DMA_IT_TCIF6 )! = RESET) {// clear the flag DMA_ClearFlag (DMA1_Stream6, DMA_FLAG_TCIF6); // DISABLE DMADMA_Cmd (DMA1_Stream6, DISABLE); // enable the sending to complete the interruption, send the last two bytes of USART_ITConfig (USART2, USART_IT_TC, ENABLE );}} /*************************************** * *********************************** return after sending completion interruption: 0: not generated, 1: ************************************** * *****************************/uint8_t drv_gsm_deal_irq_tx_end (void) {if (USART_GetITStatus (USART2, USART_IT _ TC )! = RESET) {// DISABLE the USART_ITConfig (USART2, USART_IT_TC, DISABLE); // The Flag_Tx_Gsm_Busy = 0; return 1;} return 0 ;} /*************************************** * handle the interrupted reception: buf: Received data * len: length of received data * returned: 0: not generated, other: generated, this value is the length of the received data ********************************* * **********************************/uint8_t drv_gsm_deal_irq_rx_end (uint8_t * buf) {uint16_t len = 0; // if (USART_GetI TStatus (USART2, USART_IT_IDLE )! = RESET) {USART2-> SR; USART2-> DR; // clear the USART_IT_IDLE flag DMA_Cmd (DMA1_Stream5, DISABLE ); // obtain the length of the received frame len = RX_LEN_GSM-frames (DMA1_Stream5); memcpy (buf, Rx_Buf_Gsm, len); // set the length of the transmitted data DMA_SetCurrDataCounter (DMA1_Stream5, RX_LEN_GSM ); // open DMADMA_Cmd (DMA1_Stream5, ENABLE); return len;} return 0 ;}

Interrupt function:

/*************************************** * ****************************** GSM module: *********************************** * ********************************/void DMA1_Stream6_IRQHandler (void) {gsm_dma_tx_irq_handler ();} /*************************************** * ****************************** GSM module: serial interrupt processing function ************************************ * ********************************/void USART2_IRQHandler (void) {gsm_irq_handler ();}


Interrupt handling function:

/*************************************** ******************************* Interface functions: *********************************** * ********************************/void gsm_dma_tx_irq_handler (void) {inf_gsm_deal_irq_dma_tx ();} /*************************************** ******************************* interface functions: serial Port Interrupt Processing Function * parameter: data: receiving data storage address * return: length of received data ************************************* ********************************* /Void gsm_irq_handler (void) {struct _ Gsm_Rx rx; uint8_t I = 0; // sending completion interrupt handling inf_gsm_deal_irq_tx_end (); // receiving completion interrupt handling rx. len = inf_gsm_deal_irq_rx_end (rx. buf); if (rx. len! = 0) {// notification Observer for (I = 0; I <Len_Observer; I ++) {Observer [I] (rx );}}}


Interface Function Definition:

/*************************************** ******************************* Interface functions: *********************************** * ********************************/void inf_gsm_deal_irq_dma_tx (void) {drv_gsm_deal_irq_dma_tx ();} /*************************************** * *********************************** return after sending completion interruption: 0: not generated, 1: ************************************** * *****************************/uint8_t inf_gsm_deal_irq_tx_end (void) {return drv_gsm_deal_irq_tx_end ();} /*************************************** * handle the interrupted reception: buf: Received data * len: length of received data * returned: 0: not generated, other: generated, this value is the length of the received data ********************************* * **********************************/uint8_t inf_gsm_deal_irq_rx_end (uint8_t * buf) {return drv_gsm_deal_irq_rx_end (buf );}





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.