2016-09-08 02:49 AM
Hello
Here is my code as shown below#include <
stm32f10x_gpio.h
>
#include <
stm32f10x_rcc.h
>
#include <
stm32f10x_usart.h
>
#include <
stdio.h
>
#include <
stm32f10x_dma.h
>
struct __FILE { int handle;} ;
FILE __stdout;
FILE __stdin;
FILE __stderr;
int buffer[10];
DMA_InitTypeDef DMAStruc;
int fputc(int ch, FILE *f)
{
while(!USART_GetFlagStatus(USART1,USART_FLAG_TXE));
USART_SendData(USART1,ch);
return ch;
}
void delay()
{
int i,j;
for(i=0;i<
10000
;i++)
for(
j
=
0
;j<250;j++);
}
void config(void){
GPIO_InitTypeDef GPIOStruc;
USART_InitTypeDef USAR;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//PD5
GPIOStruc.GPIO_Mode
=
GPIO_Mode_AF_PP
;
GPIOStruc.GPIO_Speed
=
GPIO_Speed_50MHz
;
GPIOStruc.GPIO_Pin
=
GPIO_Pin_6
;
GPIO_Init(GPIOB,&GPIOStruc);
//PD6
GPIOStruc.GPIO_Mode
=
GPIO_Mode_IN_FLOATING
;
//
GPIOStruc.GPIO_Speed
=
GPIO_Speed_50MHz
;
GPIOStruc.GPIO_Pin
=
GPIO_Pin_7
;
GPIO_Init(GPIOB,&GPIOStruc);
USAR.USART_BaudRate
=
9600
;
USAR.USART_StopBits
=
USART_StopBits_1
;
USAR.USART_WordLength
=
USART_WordLength_8b
;
//
USAR.USART_Parity
=
USART_Parity_No
;
USAR.USART_Parity
=
USART_Parity_Even
;
USAR.USART_HardwareFlowControl
=
USART_HardwareFlowControl_None
;
USAR.USART_Mode
=
USART_Mode_Rx
| USART_Mode_Tx;
USART_Init(USART1,&USAR);
USART_Cmd(USART1,ENABLE);
GPIOC->CRL=(2<<
8
);
GPIOC->BSRR=(1<<
2
);
}
void DMA1_Channel4_IRQnHandler(void)
{
/* Test on DMA Stream Transfer Complete interrupt */
// if (DMA_GetITStatus(DMA1_IT_TE4))
//{
/* Clear DMA Stream Transfer Complete interrupt pending bit */
DMA_ClearITPendingBit(DMA1_IT_TE4);
USART_SendData(USART1, 'C');
// }
}
/**************************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel
=
DMA1_Channel4_IRQn
;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority
=
0
;
NVIC_InitStructure.NVIC_IRQChannelSubPriority
=
0
;
NVIC_InitStructure.NVIC_IRQChannelCmd
=
ENABLE
;
NVIC_Init(&NVIC_InitStructure);
}
int main(){
config();
NVIC_Configuration();
DMA_DeInit(DMA1_Channel4);
DMAStruc.DMA_PeripheralBaseAddr=(uint32_t) &USART1->DR;
DMAStruc.DMA_BufferSize=10;
DMAStruc.DMA_MemoryBaseAddr=(uint32_t) buffer;
DMAStruc.DMA_DIR=DMA_DIR_PeripheralSRC;
DMAStruc.DMA_M2M = DMA_M2M_Disable;
DMAStruc.DMA_Priority=DMA_Priority_High;
DMAStruc.DMA_Mode=DMA_Mode_Circular;
DMAStruc.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMAStruc.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_Init(DMA1_Channel4,&DMAStruc);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel4, ENABLE);
while(1)
{
// GPIOC->BSRR=(1<<2);
//printf(''Hello the world\n\r'');
//USART_SendData(USART1, 'T');
delay();
//
//delay();
}
}
I configure USART1 and enable DMA interrupts but it doesn't do any reaction to receiving data. what is my mistake?
2016-09-08 06:08 AM
Some basic debugging:
Does it work if you don't use DMA? How do you know your receiver is actually working? Do you know if data actually arrives at the USART?
Look at status registers for USART and DMA. Do they show errors?
Embedded is like any other type of programming. Check to make sure your assumptions are correct. Then look for obvious errors. Never ignore the status registers; they are there for a reason.
Jack Peacock
2016-09-08 06:58 AM
Hi mohamadi.parisa,
You are only configuring the USAR1 Tx DMA channel (DMA1_Channel4). You should configure the RX channel for receiving data (DMA1_Channel5). As a help, See code in this user [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/UART%20with%20DMA%20mode&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=21897]thread -Hannibal-2016-09-08 11:44 AM
Thanks dear peacock.jackI correct my DMA channel to right states.After I send 10(size of buffer) chars it hangs in above line and do nothing.before sending the 10 chars it shows ''Hello the(0) world'' correctly
2016-09-09 12:28 AM
DMA1_Channel5_IRQHandler()
i.e. remove ''n''
JW
2016-09-09 10:36 AM
Thank you waclawek.jan
It works fine. How can change the address pointer to the first address of input buffer variable?before it causes an interrupt or getting full