STM32 serial port reception uses DMA double buffering

Publisher:WiseSage123Latest update time:2018-06-08 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#define IMAGE_BUFFER_SIZE 100 //In words After testing, increasing this value did not improve the speed

u32 Image_Buffer1[IMAGE_BUFFER_SIZE]={0};
u32 Image_Buffer2[IMAGE_BUFFER_SIZE]={0};

//OV2640 JPEG mode interface configuration
void OV2640_JpegDcmiInit(void)
{
DCMI_InitTypeDef DCMI_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure; //NVIC
/*** Configures the DCMI to interface with the OV2640 camera module ***/
/* Enable DCMI clock */
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);

/* DCMI configuration */ 
DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_Continuous; // Single DCMI_CaptureMode_Continuous;
DCMI_InitStructure.DCMI_SynchroMode = DCMI_SynchroMode_Hardware;
DCMI_InitStructure.DCMI_PCKPolarity = DCMI_PCKPolarity_Rising; //
DCMI_InitStructure.DCMI_VSPolarity = DCMI _VSPolarity_Low;
DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_Low;
DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_All_Frame;
DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b;
//Initialize DCMI
DCMI_Init(&DCMI_InitStructure);
//Enable J PG
DCMI_JPEGCmd(ENABLE);

/* Configures the DMA2 to transfer Data from DCMI */
/* Enable DMA2 clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);

/* DMA2 Stream1 Configuration */  
DMA_DeInit(DMA2_Stream1);

DMA_InitStructure.DMA_Channel = DMA_Channel_1; InitStructure.DMA_PeripheralBaseAddr  
= DCMI_DR_ADDRESS; 
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&Image_Buffer1;//Transfer to internal array
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = IMAGE_BUFFER_SIZE; //以字为单位
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_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
/*DMA double buffer mode*///Double buffer mode
DMA_DoubleBufferModeConfig(DMA2_Stream1,(uint32_t)&Image_Buffer2,DMA_Memory_0);//DMA_Memory_0 is transferred first
DMA_DoubleBufferModeCmd(DMA2_Stream1,ENABLE);
DMA_Init(DMA2_Stream1, &DMA_InitStructure); //Initialize DMA

Dma_FreeBuf_Ok = 0;//No data is ready at this time
// DMA_Cmd(DMA2_Stream1,ENABLE);//Start DMA2_Stream1

//Interrupt enable
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = DCMI_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure); 
DCMI_ITConfig(DCMI_IT_FRAME,ENABLE); //Interrupt enable

NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ITConfig(DMA2_Stream1,DMA_IT_TC,ENABLE);
}

//DMA transfer completed interrupt
void DMA2_Stream1_IRQHandler(void)
{
DMA_InitTypeDef DMA_InitStructure;
if(DMA_GetITStatus(DMA2_Stream1,DMA_IT_TCIF1) != RESET)
{
DMA_ClearITPendingBit(DMA2_Stream1,DMA_IT_TCIF1);
Dma_FreeBuf_Ok = 1;//There is data ready
}
}


//Main loop
while(1)
{
//uip_polling();
if(Dma_FreeBuf_Ok == 1)//DMA has a free buffer
{
usart_sendJPEGdata();
Dma_FreeBuf_Ok = 0;
}


//Serial port transmission
void usart_sendJPEGdata(void)
{
u32 i;
if(DMA_GetCurrentMemoryTarget(DMA2_Stream1) == 1)
{
for(i=0;i{
USART_SendData(USART2,Image_Buffer1);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
USART_SendData(USART2,Image_Buffer1>>8);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
USART_SendData(USART2,I mage_Buffer1>>16);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
USART_SendData(USART2,Image_Buffer1>>24);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
}
}
else
{
for(i=0;i{
USART_SendData(USART2, Image_Buffer2);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
USART_SendData(USART2,Image_Buffer2>>8);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
USART_SendData(USART2,Image_Buffer2>>16);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
USART_SendData(USART2,Image_Buffer2>>24);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET); 
}
}
}

Keywords:STM32 Reference address:STM32 serial port reception uses DMA double buffering

Previous article:STM32 uses two ways to receive variable-length data using serial port IDLE interrupt
Next article:Problem of missing bytes when using STM32 serial port to send data in DMA mode in RS485 communication

Recommended ReadingLatest update time:2024-11-16 11:51

Understanding of USB buffer after using STM32 USB module
I recently developed a project using the STM32 USB module. I thought it was quite simple, but it took me almost two days to get the USB packet buffer accessed. Here is a brief summary.   The STM32 USB module packet buffer has 512B, but the memory image in the STM32 reference manual shows 0x40006000-0x400063ff, which i
[Microcontroller]
STM32 RCC configuration
Firmware Library V3.5 IAR /* ---------------------Function entity--------------------------------*/ /**************************************************************************** * Function name: RCC_Configuration * Function: Set the clocks of various parts of the system * Parameter variables: NONE * Global variab
[Microcontroller]
Strawberry picking robot control system based on Gizwits Cloud platform + STM32 + Raspberry Pi
1. Introduction This strawberry picking robot control system was designed and developed by Cheng Pengsheng, a mechanical engineering major at Jiangxi University of Science and Technology. Aiming at the problem of time-consuming and costly manual picking of elevated strawberries planted in a certain strawberry p
[Microcontroller]
Strawberry picking robot control system based on Gizwits Cloud platform + STM32 + Raspberry Pi
STM32 collects signals from DHT11 temperature and humidity sensor
First, let me introduce the DHT11 temperature and humidity sensor. The DHT11 digital temperature and humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. The sensor includes a resistive humidity sensing element and an NTC temperature measuring element, and is connected
[Microcontroller]
STM32 collects signals from DHT11 temperature and humidity sensor
Detailed explanation of STM32 DMA usage
The DMA part I used is relatively simple. Of course, maybe this is a new thing, and I can't use its complex functions for the time being. The following is a question and answer format to express my thoughts. What is DMA used for?        Direct memory access is used to provide high-speed data transfer between periphera
[Microcontroller]
Detailed explanation of STM32 DMA usage
STM32 interrupts and exceptions
Exceptions are mainly divided into interrupts (such as IO ports, UART, etc.) and system exceptions (such as NMI, SYCTICK, etc.). Pay attention to the difference between interrupts and system exceptions. Exceptions are collectively referred to below. In CMSIS, IRQn_Type is an enumeration of exception types. System ex
[Microcontroller]
STM32 interrupts and exceptions
The principle and use of STM32 PWM
1.What is PWM?     It is pulse width modulation, or PWM for short. A very effective technique for controlling analog circuits using the digital output of a microprocessor is the control of pulse width.     The pulse mentioned here is the square wave we generate. A square wave is a continuous generation of N such c
[Microcontroller]
The principle and use of STM32 PWM
STM32 study notes DMA transmission
1. Introduction to DMA 1. Introduction to DMA DMA (Direct Memory Access) is a data transfer method that can greatly reduce the CPU workload. The CPU has many functions such as transferring data, computing, and controlling program transfer, but in fact, data transfer (especially large amounts of data transfer) does
[Microcontroller]
STM32 study notes DMA transmission
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号