Skip to main content
leventeyigel52
Associate III
April 4, 2014
Question

UART4 DMA Rx Problem

  • April 4, 2014
  • 10 replies
  • 2550 views
Posted on April 04, 2014 at 13:21

I cannot get into DMA mode, the code  I wrote did not get into DMA  interrupt. The code I wrote is as follows. Any advice?( my processor is STM32F103ZG)

 uint8_t RxBuffer1[100]; 

 /*UART - Pin Config*/

  GPIO_InitTypeDef GPIO_InitStructure;

  USART_InitTypeDef USART_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  DMA_InitTypeDef DMA_InitStructure;; 

  /* Clock configuration -------------------------------------------------------*/

  RCC_APB2PeriphClockCmd( UART_GPIO_CLK_TX | UART_GPIO_CLK_RX | RCC_APB2Periph_AFIO, ENABLE);

  RCC_APB1PeriphClockCmd(UART_CLK , ENABLE);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2,ENABLE);

 

 /* Configure the GPIO ports( UART4 Transmit and Receive Lines) */ 

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Pin =  UART_TxPin;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(UART_GPIO_TX, &GPIO_InitStructure); 

 

/* Configure the UART4_Rx as input floating */

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_InitStructure.GPIO_Pin = UART_RxPin ;

  GPIO_Init(UART_GPIO_RX, &GPIO_InitStructure);

  

  USART_InitStructure.USART_BaudRate = 1200;

  USART_InitStructure.USART_WordLength = USART_WordLength_9b;

  USART_InitStructure.USART_StopBits = USART_StopBits_1;

  USART_InitStructure.USART_Parity = USART_Parity_Even;

  USART_InitStructure.USART_HardwareFlowControl =     USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  

    /* UART Guard Time set to 16 Bit */

  USART_SetGuardTime(UART4, 8);

 

  USART_Init(UART4, &USART_InitStructure);

   /* Enable the UART4 */

  USART_Cmd(UART4, ENABLE);

  

  

  /************************* DMA *****************************/

  DMA_DeInit(DMA2_Channel3);

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

  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer1; 

  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // Receive 

  DMA_InitStructure.DMA_BufferSize = 1; 

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

  DMA_InitStructure.DMA_Priority = DMA_Priority_High; 

  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

 

  DMA_Init(DMA2_Channel3, &DMA_InitStructure); 

  /* Enable the USART Rx DMA request */

  USART_DMACmd(UART4, USART_DMAReq_Rx, ENABLE);

 

  /* Enable DMA Transfer Complete interrupt */

  DMA_ITConfig(DMA2_Channel3, DMA_IT_TC, ENABLE); 

 

  /* Enable the DMA RX Stream */

  DMA_Cmd(DMA2_Channel3, ENABLE);

  /* Enable the UART4 RX DMA Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel3_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);  

/*******************************************************************/ 

void DMA2_Channel3_IRQHandler()

{

  if(DMA_GetITStatus(DMA2_IT_TC3)){

    

    Uart_Interrupt_Handler();

    

    DMA_ClearITPendingBit(DMA2_IT_TC3);

  }

    This topic has been closed for replies.

    10 replies

    waclawek.jan
    Super User
    April 4, 2014
    Posted on April 04, 2014 at 13:40

    >

      DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // Receive

    ?

    JW

    leventeyigel52
    Associate III
    April 4, 2014
    Posted on April 04, 2014 at 13:47

    Don't we choose ''DMA_DIR_PeripheralDST'' when interrupt comes Rx Pin ?

    Tesla DeLorean
    Guru
    April 4, 2014
    Posted on April 04, 2014 at 13:52

    Don't we choose ''DMA_DIR_PeripheralDST'' when interrupt comes Rx Pin ?

    NO, you're configuring a DMA transaction, which you want to READ the Peripheral register, and WRITE to Memory, this would mean the Peripheral is the SOURCE (SRC), right?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    leventeyigel52
    Associate III
    April 4, 2014
    Posted on April 04, 2014 at 14:00

    Is my BufferSize correct ? 

    I want to get into IRQ for each byte ? 

    leventeyigel52
    Associate III
    April 4, 2014
    Posted on April 04, 2014 at 14:08

    I changed the DMA_DIR_PeripheralDST to DMA_DIR_PeripheralSRC .

     

    But i cannot still get into the interrupt 

    Tesla DeLorean
    Guru
    April 4, 2014
    Posted on April 04, 2014 at 14:37

    If you use Normal mode you'll need to reinitialize the DMA on each interrupt

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    leventeyigel52
    Associate III
    April 4, 2014
    Posted on April 04, 2014 at 14:54

    I changed the DMA_InitStructure.DMA_Mode to  DMA_Mode_Circular but the problem has not solved. 

    Is there an error on IRQ Configuring ?

    leventeyigel52
    Associate III
    April 4, 2014
    Posted on April 04, 2014 at 16:20

    to be migrated, sourceId: 40316:697285D7-A9CA-445D-B16C-F23BF0E3B1A3

    Tesla DeLorean
    Guru
    April 4, 2014
    Posted on April 04, 2014 at 17:30

    The last version is following below. I cannot understand where is the wrong ?

     

    A cursory review suggests the code is not unreasonable, although I honestly can't see the point of doing a one byte DMA, and how that's materially more useful than just doing an RXNE interrupt.

    Are you actually receiving any data? If you make a simple polled mode version, or echo back, does that actually work?
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    April 4, 2014
    Posted on April 04, 2014 at 18:13

    I'd perhaps demo it as follows, quick blind port

    // STM32F103-XL UART4 DMA TX/RX (Tx PC.10, Rx PC.11) - sourcer32@gmail.com
    #include ''stm32F10x.h''
    /**************************************************************************************/
    void RCC_Configuration(void)
    {
    /* Enable DMA clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
    /* Enable GPIO clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    /* Enable UART clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
    }
    /**************************************************************************************/
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /* Configure USART Tx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PC.10 UART4.TX
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    /* Configure USART Rx as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // PC.11 UART4.RX
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    }
    /**************************************************************************************/
    void USART_Configuration(void)
    {
    USART_InitTypeDef USART_InitStructure;
    /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
    /* USART configured as follow:
    - BaudRate = 1200 baud
    - Word Length = 8 Bits
    - One Stop Bit
    - Even parity
    - Hardware flow control disabled (RTS and CTS signals)
    - Receive and transmit enabled
    */
    USART_InitStructure.USART_BaudRate = 1200;
    USART_InitStructure.USART_WordLength = USART_WordLength_9b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_Even;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    /* USART configuration */
    USART_Init(UART4, &USART_InitStructure);
    /* Enable the UART4 */
    USART_Cmd(UART4, ENABLE);
    }
    /**************************************************************************************/
    volatile char Buffer[] = ''The quick brown fox jumps over the lazy dog
    
    '';
    void DMA_Configuration(void)
    {
    DMA_InitTypeDef DMA_InitStructure;
    /* UART4_TX DMA2 Channel 5 (See RM0008) */
    DMA_DeInit(DMA2_Channel5);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART4->DR;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Buffer;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = sizeof(Buffer) - 1;
    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_Low;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA2_Channel5, &DMA_InitStructure);
    USART_DMACmd(UART4, USART_DMAReq_Tx, ENABLE);
    /* Enable DMA Stream Transfer Complete interrupt */
    DMA_ITConfig(DMA2_Channel5, DMA_IT_TC, ENABLE);
    /* UART4_RX DMA2 Channel 3 (See RM0008) */
    DMA_DeInit(DMA2_Channel3);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART4->DR;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Buffer;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = sizeof(Buffer) - 1;
    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_Low;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA2_Channel3, &DMA_InitStructure);
    USART_DMACmd(UART4, USART_DMAReq_Rx, ENABLE);
    /* Enable DMA Stream Transfer Complete interrupt */
    DMA_ITConfig(DMA2_Channel3, DMA_IT_TC, ENABLE);
    DMA_Cmd(DMA2_Channel5, ENABLE);
    DMA_Cmd(DMA2_Channel3, ENABLE);
    }
    /**************************************************************************************/
    void DMA2_Channel4_5_IRQHandler(void) // UART4_TX
    {
    /* Test on DMA Transfer Complete interrupt */
    if (DMA_GetITStatus(DMA2_IT_TC5))
    {
    /* Clear DMA Transfer Complete interrupt pending bit */
    DMA_ClearITPendingBit(DMA2_IT_TC5);
    /* ... */
    }
    }
    /**************************************************************************************/
    void DMA2_Channel3_IRQHandler(void) // UART4_RX
    {
    /* Test on DMA Transfer Complete interrupt */
    if (DMA_GetITStatus(DMA2_IT_TC3))
    {
    /* Clear DMA Transfer Complete interrupt pending bit */
    DMA_ClearITPendingBit(DMA2_IT_TC3);
    /* ... */
    }
    }
    /**************************************************************************************/
    void NVIC_Configuration(void)
    {
    NVIC_InitTypeDef NVIC_InitStructure;
    /* Configure the Priority Group to 2 bits */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    /* Enable the UART4 TX DMA Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel4_5_IRQn; // Combined on XL, CL has unique
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    /* Enable the UART4 RX DMA Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    }
    /**************************************************************************************/
    int main(void)
    {
    RCC_Configuration();
    NVIC_Configuration();
    GPIO_Configuration();
    USART_Configuration();
    DMA_Configuration();
    while(1);
    }
    /**************************************************************************************/
    #ifdef USE_FULL_ASSERT
    /**
    * @brief Reports the name of the source file and the source line number
    * where the assert_param error has occurred.
    * @param file: pointer to the source file name
    * @param line: assert_param error line source number
    * @retval None
    */
    void assert_failed(uint8_t* file, uint32_t line)
    {
    /* User can add his own implementation to report the file name and line number,
    ex: printf(''Wrong parameters value: file %s on line %d
    
    '', file, line) */
    /* Infinite loop */
    while (1)
    {
    }
    }
    #endif
    /**************************************************************************************/

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..