cancel
Showing results for 
Search instead for 
Did you mean: 

USART and DMA

senmeis
Associate
Posted on March 14, 2012 at 09:50

/* Private typedef -----------------------------------------------------------*/ 
/* Private define ------------------------------------------------------------*/ 
#ifdef USE_STM8L1526_EVAL 
/* define the DMA parameters related to USART1 since USE_STM8L1526_EVAL is used */ 
#define USART_DMA_CHANNEL_RX DMA1_Channel2 
#define USART_DMA_CHANNEL_TX DMA1_Channel1 
#define USART_DMA_FLAG_TCRX (uint16_t)DMA1_FLAG_TC2 
#define USART_DMA_FLAG_TCTX (uint16_t)DMA1_FLAG_TC1 
#define USART_DR_ADDRESS (uint16_t)0x5231 /* USART1 Data register Address */ 
#else /* USE_STM8L1528_EVAL */ 
/* define the DMA parameters related to USART2 since USE_STM8L1528_EVAL is used */ 
#define USART_DMA_CHANNEL_RX DMA1_Channel3 
#define USART_DMA_CHANNEL_TX DMA1_Channel0 
#define USART_DMA_FLAG_TCRX (uint16_t)DMA1_FLAG_TC3 
#define USART_DMA_FLAG_TCTX (uint16_t)DMA1_FLAG_TC0 
#define USART_DR_ADDRESS (uint16_t)0x53E1 /* USART2 Data register Address */ 
#endif /* USE_STM8L1526_EVAL */ 
#define USART_BAUDRATE (uint32_t)9600 
//#define DATA_TO_TRANSFER (countof(TxBuffer) - 1) 
#define DATA_TO_TRANSFER (uint8_t)5 
//#define DATA_TO_RECEIVE (uint8_t) 0x20 
#define DATA_TO_RECEIVE (uint8_t)5 
/* Private macro -------------------------------------------------------------*/ 
#define countof(a) (sizeof(a) / sizeof(*(a))) 
/* Private variables ---------------------------------------------------------*/ 
//uint8_t TxBuffer[] = ''\n\rUSART Example:USART-Hyperterminal communication using DMA.\nEnter your Text\n\r''; 
uint8_t TxBuffer[DATA_TO_TRANSFER] = {0}; 
uint8_t RxBuffer[DATA_TO_RECEIVE] = {0}; 
/* Private function prototypes -----------------------------------------------*/ 
static void CLK_Config(void); 
static void DMA1_Config(void); 
static void DMA2_Config(void); 
static void USART_Config(void); 
void Delay (uint32_t nCount); 
/* Private functions ---------------------------------------------------------*/ 
/** 
* @brief Main program. 
* @param None 
* @retval None 
*/ 
void main(void) 
{ 
/* CLK configuration -------------------------------------------*/ 
CLK_Config(); 
/* USART configuration -------------------------------------------*/ 
USART_Config(); 
/* DMA configuration -------------------------------------------*/ 
//DMA_Config(); 
while (1) 
{ 
/* DMA2 configuration -------------------------------------------*/ 
DMA2_Config(); 
/* USART Enable */ 
USART_Cmd(EVAL_COM1, ENABLE); 
/* Wait the USART DMA Rx transfer complete */ 
while (DMA_GetFlagStatus((DMA_FLAG_TypeDef)USART_DMA_FLAG_TCRX) == RESET); 
strcpy(TxBuffer, RxBuffer); 
/* DMA1 configuration -------------------------------------------*/ 
DMA1_Config(); 
/* USART Enable */ 
USART_Cmd(EVAL_COM1, ENABLE); 
/* Wait the USART DMA Tx transfer complete */ 
while (DMA_GetFlagStatus((DMA_FLAG_TypeDef)USART_DMA_FLAG_TCTX) == RESET); 
} 
} 
/** 
* @brief Configure peripherals Clock 
* @param None 
* @retval None 
*/ 
static void CLK_Config(void) 
{ 
CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); 
/*High speed external clock prescaler: 1*/ 
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); 
/*Enable DMA clock */ 
CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE); 
} 
/** 
* @brief Configure DMA peripheral 
* @param None 
* @retval None 
*/ 
static void DMA1_Config(void) 
{ 
/* Deinitialize DMA channels */ 
DMA_GlobalDeInit(); 
DMA_DeInit(DMA1_Channel1); 
/* DMA channel Tx of USART Configuration */ 
DMA_Init(USART_DMA_CHANNEL_TX, (uint16_t)TxBuffer, (uint16_t)USART_DR_ADDRESS, 
DATA_TO_TRANSFER, DMA_DIR_MemoryToPeripheral, DMA_Mode_Normal, 
DMA_MemoryIncMode_Inc, DMA_Priority_High, DMA_MemoryDataSize_Byte); 
/* Enable the USART Tx/Rx DMA requests */ 
USART_DMACmd(EVAL_COM1, USART_DMAReq_TX, ENABLE); 
/* Global DMA Enable */ 
DMA_GlobalCmd(ENABLE); 
/* Enable the USART Tx DMA channel */ 
DMA_Cmd(USART_DMA_CHANNEL_TX, ENABLE); 
} 
static void DMA2_Config(void) 
{ 
/* Deinitialize DMA channels */ 
DMA_GlobalDeInit(); 
DMA_DeInit(DMA1_Channel2); 
/* DMA channel Rx of USART Configuration */ 
DMA_Init(USART_DMA_CHANNEL_RX, (uint16_t)RxBuffer, (uint16_t)USART_DR_ADDRESS, 
DATA_TO_RECEIVE, DMA_DIR_PeripheralToMemory, DMA_Mode_Normal, 
DMA_MemoryIncMode_Inc, DMA_Priority_Low, DMA_MemoryDataSize_Byte); 
/* Enable the USART Tx/Rx DMA requests */ 
USART_DMACmd(EVAL_COM1, USART_DMAReq_RX, ENABLE); 
/* Global DMA Enable */ 
DMA_GlobalCmd(ENABLE); 
/* Enable the USART Rx DMA channel */ 
DMA_Cmd(USART_DMA_CHANNEL_RX, ENABLE); 
} 
/** 
* @brief Configure USART peripheral 
* @param None 
* @retval None 
*/ 
static void USART_Config(void) 
{ 
/* EVAL COM (USARTx) configuration -----------------------------------------*/ 
/* USART configured as follow: 
- BaudRate = USART_BAUDRATE baud 
- Word Length = 8 Bits 
- One Stop Bit 
- No parity 
- Receive and transmit enabled 
- USART Clock disabled 
*/ 
STM_EVAL_COMInit(COM1, (uint32_t)USART_BAUDRATE, USART_WordLength_8b, USART_StopBits_1, 
USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx)); 
/* USART Disable */ 
USART_Cmd(EVAL_COM1, DISABLE); 
} 
/** 
* @brief Delay. 
* @param nCount 
* @retval None 
*/ 
void Delay(uint32_t nCount) 
{ 
/* Decrement nCount value */ 
while (nCount != 0) 
{ 
nCount--; 
} 
} 

Hello,

I want to use DMA to transmit and receive data of USART, based on the example code: USART_HyperTerminal_DMA.

With this code I want to create the following: The USART will send back the same string if this string is received.

The idea is, the original DMA_Config() is split into two functions: DMA1_Config() (USART Tx) and DMA2_Config() (USART Rx), but it doesn’t work as expected.

I think the error is because I don’t understand DMA fully. I will thank you for every advice.

Thanks

Owen

0 REPLIES 0