cancel
Showing results for 
Search instead for 
Did you mean: 

interrupt problem for dma in usart

mohamad_armoon
Associate III
Posted on March 09, 2014 at 16:58

hi guys, i wanna use interrupt for dma that i set for USART2 Rx/Tx  but i don't know how can i implement  intrrupt  functions , here is my code please tell me how can i use interrupt for Rx and Tx dma in my code:

#include ''stm32f4xx.h''

void RCC_Configuration(void)

{

  /* --------------------------- System Clocks Configuration -----------------*/

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);

}

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

 

  /*-------------------------- GPIO Configuration ----------------------------*/

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

 

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);

}

void USART2_Configuration(void)

    USART_IniTypeDef USART_InitStructure;

  USART_InitStructure.USART_BaudRate = 9600;

  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);

  USART_Cmd(USART2, ENABLE);

}

uint8_t Rx_Buffer;

void DMA_Rx_Configuration(void)

{

  DMA_InitTypeDef  DMA_InitStructure;

 

  DMA_DeInit(DMA1_Stream5);

 

  DMA_InitStructure.DMA_Channel = DMA_Channel_4;

  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; // receive

 

  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&Rx_Buffer;

  DMA_InitStructure.DMA_BufferSize = (uint16_t)sizeof(Rx_Buffer);

  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR;

  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

  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_Full;

  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;

  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

 

  DMA_Init(DMA1_Stream5, &DMA_InitStructure);

  USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);

  DMA_Cmd(DMA1_Stream5, ENABLE);

}

void init_LED()

{

  GPIO_InitTypeDef GPIO_InitStructure;

  /*-------------------------- GPIO Configuration ----------------------------*/

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  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_50MHz;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

}

uint8_t Tx_Buffer;

void DMA_Tx_Configuration()

{

  DMA_InitTypeDef  DMA_InitStructure;

  DMA_DeInit(DMA1_Stream6);

 

  DMA_InitStructure.DMA_Channel = DMA_Channel_4;

  DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; // send

 

  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&Tx_Buffer;

  DMA_InitStructure.DMA_BufferSize = (uint16_t)sizeof(Tx_Buffer);

  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR;

  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

  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_Full;

  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;

  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

 

  DMA_Init(DMA1_Stream6, &DMA_InitStructure);

 

  USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);

  DMA_Cmd(DMA1_Stream6, ENABLE);

}

int main()

{RCC_Configuration();

 

    GPIO_Configuration();

 

    USART2_Configuration();

 

    DMA_Rx_Configuration();

    DMA_Tx_Configuration();

    

    init_LED();

    

  uint8_t temp = 0;

  while(1){

   temp = Rx_Buffer  ;

  while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); 

    // USART_SendData(USART2, temp);

  Tx_Buffer = Rx_Buffer;

     if(temp > 0){

       GPIO_SetBits(GPIOD,GPIO_Pin_12);

       GPIO_SetBits(GPIOD,GPIO_Pin_13);

     }

  }

}

1 REPLY 1