AnsweredAssumed Answered

RX and reTX from USART1 with dma (Stm32f100B)

Question asked by dariogi88 on Feb 7, 2013
Latest reply on Feb 10, 2013 by dariogi88
Hi,
excuse me for my bad english,I'm new in the microcontrollers field, so I've some uncertainty on the use of the dma to read and write from USART. At the moment I'm interfacing the stm32f100B with a medical module that trasmittes continuosly datas, so I need reading these datas and to retransmit it by USART1.
I have written a code that reads the datas from USART and retransmittes it, this code works but it is without dma.
But now I need to use the dma to read and write data from USART, so I have written another code with dma but I don't understand where is the error and if I use dma in correct mode, because I don't receive nothing. 

My code is this:

#include "stm32f10x_conf.h" 
#include "stm32f10x.h"
#include "stm32f10x_dma.h"
#include "stm32f10x_dma.c"
#include "stm32f10x_usart.h"
#include "stm32f10x_usart.c"
#include "stm32f10x_rcc.c"
#include "platform_config.h"


uint8_t buffer;
int k=0;


void board_init(void) {


    RCC->APB2ENR = 0
                   // Turn on USART1
                   | RCC_APB2ENR_USART1EN
                   // Turn on IO Port A
                   | RCC_APB2ENR_IOPAEN;




    // Put PA9  (TX) to alternate function output push-pull at 50 MHz
    // Put PA10 (RX) to floating input
    GPIOA->CRH = 0x000004B0;


    // Configure BRR by deviding the bus clock with the baud rate
    USART1->BRR = 8000000/9600;


    // Enable the USART, TX, and RX circuit
    USART1->CR1 = USART_CR1_UE | USART_CR1_TE | USART_CR1_RE;
}


// DMA_configuration
void DMA_Configuration(void)
{


  DMA_InitTypeDef DMA_InitStructure;


  /* USARTy TX DMA1 Channel (triggered by USARTy Tx event) Config */
  DMA_DeInit(USARTy_Tx_DMA_Channel);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
  DMA_InitStructure.DMA_MemoryBaseAddr =buffer;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  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_VeryHigh;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(USARTy_Tx_DMA_Channel, &DMA_InitStructure);


  /* USARTy RX DMA1 Channel (triggered by USARTy Rx event) Config */
  DMA_DeInit(USARTy_Rx_DMA_Channel);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
  DMA_InitStructure.DMA_MemoryBaseAddr = buffer;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_Init(USARTy_Rx_DMA_Channel, &DMA_InitStructure);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);


   /* Enable USARTy TX DMA1 Channel */
  DMA_Cmd(USARTy_Tx_DMA_Channel, ENABLE);
  /* Enable USARTy RX DMA1 Channel */
  DMA_Cmd(USARTy_Rx_DMA_Channel, ENABLE);
    /* Enable USARTy DMA Rx and TX request */
  /* Enable USARTy DMA Rx and TX request */
  USART_DMACmd(USARTy, USART_DMAReq_Rx | USART_DMAReq_Tx, ENABLE);


}




//


/**
 * Receive a single byte from USART1.
 */
int usartdma_rec(void) {
/* Wait until USARTy RX DMA1 Channel Transfer Complete */
  while (DMA_GetFlagStatus(USARTy_Rx_DMA_FLAG) == RESET);


    // read RX data, combine with DR mask (we only accept a max of 9 Bits)
    return USART1->DR & 0x1FF;
}






//Send a single byte by USART1. 
void usartdma_snd(int data) {
    USART1->DR = data;


/* Wait until USARTy TX DMA1 Channel Transfer Complete */
  while (DMA_GetFlagStatus(USARTy_Tx_DMA_FLAG) == RESET);
}
  






int main(void) {
    board_init();
DMA_Configuration();


    for(;;) {
      int rec=usartdma_rec();
      buffer=rec;
  
usartdma_snd(buffer);
  
    }


}







Outcomes