2014-03-12 09:01 AM
hi clive. thanks for information. i completed my code for stm32f4 Discovery with your idea with the link that you put here. i think it should be worked , but it doesn't. what's your idea about my code???
thanks in advance#include ''stm32f4xx.h''
voidRCC_Configuration(
void)
{ /* --------------------------- System Clocks Configuration -----------------*/ /* USART2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* GPIOA clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* DMA1 clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); } voidGPIO_Configuration(
void)
{ GPIO_InitTypeDef GPIO_InitStructure; /*-------------------------- GPIO Configuration ----------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Connect USART pins to AF */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); } voidinit_LED()
{ GPIO_InitTypeDef GPIO_InitStructure; /*-------------------------- GPIO Configuration ----------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); } voidUSART2_Configuration(
void)
{ 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); USART_Cmd(USART2, ENABLE); } uint8_t Rx_Buffer; voidDMA_Rx_Configuration(
void)
{ DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA1_Stream5); DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; // receiveDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&Rx_Buffer;
DMA_InitStructure.DMA_BufferSize = (uint16_t) sizeof(Rx_Buffer);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream5, &DMA_InitStructure); /* Enable the USART Rx DMA request */ USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE); /* Enable DMA Stream Half Transfer and Transfer Complete interrupt */ DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE); // DMA_ITConfig(DMA1_Stream5, DMA_IT_HT, ENABLE); /* Enable the DMA RX Stream */ DMA_Cmd(DMA1_Stream5, ENABLE); } voidNVIC_Rx_Configuration(
void)
{ NVIC_InitTypeDef NVIC_InitStructure; /* Configure the Priority Group to 2 bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Enable the UART4 RX DMA Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } voidDMA1_Stream5_IRQHandler(
void)
{ /* Test on DMA Stream Transfer Complete interrupt */ if(DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5))
{ /* Clear DMA Stream Transfer Complete interrupt pending bit */ DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5); DMA_Rx_Configuration(); } } intmain()
{ RCC_Configuration(); GPIO_Configuration(); USART2_Configuration(); DMA_Rx_Configuration(); NVIC_Rx_Configuration(); init_LED(); uint8_t temp = 0; while(1){
temp = Rx_Buffer ;GPIO_SetBits(GPIOD,GPIO_Pin_12); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2,Rx_Buffer); // Tx_Buffer = Rx_Buffer; if(temp > 0){
GPIO_SetBits(GPIOD,GPIO_Pin_13); } } }