cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103RB DMA UART problem

darekfilip
Associate II
Posted on January 10, 2014 at 10:46

Hi,

I use STM32 vl-discovery in my project. I've connected and configured USART1 + DMA1 with informations and code from this forum. Application almost works, but all data in DMA memory receive buffer are garbage (when i disconnect the buffer fills with zeros). External device connected to PC works correctly. Device communication parameters are 9600 8n1

// power RCC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// interrupts configuration
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
// USART 1 TX interrupt configure
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// USART 1 RX interrupt configure
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// TX
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART
USART_DeInit(USART1);
USART_InitTypeDef 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(USART1, &USART_InitStructure);
// DMA rx
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USART1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) DMA_GPS_IN_BUFFER;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = GPS_IN_BUFFER_SIZE;
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(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TC | DMA_IT_HT, ENABLE);
DMA_Cmd(DMA1_Channel5, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
USART_Cmd(USART1, ENABLE);
Interrupt handler:
void
DMA1_Channel5_IRQHandler(
void
) {
if
(DMA_GetITStatus(DMA1_IT_TC5) == SET) {
DMA_ClearITPendingBit(DMA1_IT_TC5);
} 
else
if
(DMA_GetITStatus(DMA1_IT_HT5) == SET) {
DMA_ClearITPendingBit(DMA1_IT_HT5);
}
}

What can be wrong? Why DMA fills buffer with 0 when device is disconnected? Connected device sends information 1/second. Does the DMA fill buffer with 0 if there will be no data to receive?
6 REPLIES 6
darekfilip
Associate II
Posted on January 10, 2014 at 11:32

One more information: first time i connected pins rx tx incorrectly (pins were swaped).

Where is edit option?
Posted on January 10, 2014 at 12:18

Missing the defines and definitions here... probably salient

If you are still logged in you should see an Edit button under the post.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 10, 2014 at 12:21

0690X000006053KQAQ.png
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
darekfilip
Associate II
Posted on January 10, 2014 at 13:56

What exacly is missing?

Applications works exactly in the same way when i use UART2 without DMA:

int i = 0;
while (1) {
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
DMA_GPS_IN_BUFFER[i++] = USART_ReceiveData(USART2) & 0xFF;
if (i >= GPS_IN_BUFFER_SIZE) {
i = 0;
}
}
// Init:
// power RCC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInit(USART2, &USART_ClockInitStructure);
// GPIO Configuration TX = PA.09 , RX = PA.10
// TX
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART
USART_DeInit(USART2);
USART_InitTypeDef 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);

Debugging shown that very oftenUSART_FLAG_FE is set. What can cause this?

Posted on January 10, 2014 at 16:14

What exacly is missing?

Really? How long have you been doing C programming? ''defines and definitions''

ie The variables and other crap you didn't bother to cut-n-paste, and would allow someone other than you to analyze and compile your exemplar.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
darekfilip
Associate II
Posted on January 10, 2014 at 16:27

I don't think this other crap matther.

/**
* Initializes GPS hardware:
* - power: DMA1, GPIOA, USART1
* - configure DMA interrupts
* - configure GPIO
* - configure USART
* - configura DMA receive
*/
void
initGPSHW() {
// power RCC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInit(USART2, &USART_ClockInitStructure);
// interrupts configuration
// NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
// USART 1 TX interrupt configure
// NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
// USART 1 RX interrupt configure
// NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
// GPIO Configuration TX = PA.09 , RX = PA.10
// TX
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART
USART_DeInit(USART2);
USART_InitTypeDef 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);
// DMA rx
/* DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USART1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) DMA_GPS_IN_BUFFER;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = GPS_IN_BUFFER_SIZE;
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(DMA1_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5, DMA_IT_TC | DMA_IT_HT, ENABLE);
DMA_Cmd(DMA1_Channel5, ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); */
USART_Cmd(USART2, ENABLE);
}
long
gpsSend(
char
* buffer, 
int
len) {
int
i = 0;
for
(i = 0; i < len; ++i) {
DMA_GPS_OUT_BUFFER[i] = buffer[i];
}
// configure DMA and send
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) DMA_GPS_OUT_BUFFER;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = GPS_OUT_BUFFER_SIZE;
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_Low;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel4, ENABLE);
return
pdTRUE;
}
#define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)
void
initGPS() {
/**
* Nothing received.
*/
gpsReceivedStructure.received_frames = 0;
/**
* Initialize Hardware.
*/
initGPSHW();
int
i = 0;
volatile
char
received[10];
while
(1) {
while
(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) {
if
(USART_GetFlagStatus(USART2, USART_FLAG_ORE) == SET) {
USART_ReceiveData(USART2);
}
if
(USART_GetFlagStatus(USART2, USART_FLAG_NE) == SET) {
USART_ReceiveData(USART2);
}
if
(USART_GetFlagStatus(USART2, USART_FLAG_FE) == SET) {
USART_ReceiveData(USART2);
}
if
(USART_GetFlagStatus(USART2, USART_FLAG_PE) == SET) {
USART_ReceiveData(USART2);
}
}
received[i++] = USART_ReceiveData(USART2) & 0xFF;
if
(i >= 10) {
i = 0;
}
}
}

That all i think is needed. Board is STM32 VL-Discovery. After all have 2 ideas: - clock problems, - transmission voltage????