AnsweredAssumed Answered

stm32f10x DMA RX Interupt ....

Question asked by redkofka on Jun 18, 2012
Latest reply on Jun 26, 2012 by redkofka
HI I want to use DMA to Recieving data via uart (rs485)
 I want to use 20byte buffer but for siplicity in this exaple is shown only 1byte buffer...



 int main(void){
 RCC_Configuration();
 GPIO_Configuration();
 USART_Configuration();
 DMA_Configuration();
 NVIC_Configuration_DMA_RX();
 while(1){
 };
 }
 /******************************************************************************/

 void RCC_Configuration(void)
 {
 /* Enable DMA clock */
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

 /* Enable GPIO bank clock */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

 /* Enable UART clock */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 }
 /******************************************************************************/
 void GPIO_Configuration(void)
 {
 GPIO_InitTypeDef GPIO_InitStructure;

 /* Configure USART Tx as alternate function push-pull */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA.09 USART1.TX
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* Configure USART Rx as input floating */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA.10 USART1.RX
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 }

 /******************************************************************************/

 void USART_Configuration(void)
 {
 USART_InitTypeDef USART_InitStructure;

 /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
 /* USART configured as follow:
 - BaudRate = 115200 baud
 - Word Length = 8 Bits
 - One Stop Bit
 - No parity
 - Hardware flow control disabled (RTS and CTS signals)
 - Receive and transmit enabled
 */
 USART_InitStructure.USART_BaudRate = 115200;
 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_RTS;
 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

 /* USART configuration */
 USART_Init(USART1, &USART_InitStructure);

 /* Enable the USART1 */
 USART_Cmd(USART1, ENABLE);

 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

 /* Enable DMA Requests*/
 USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
 USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
 }

 /******************************************************************************/
 /**
 * @Brief
 * @Parameter None
 *
 * @Return None
 */
 void DMA_Configuration(void)
 {
 // USART1 Channel 4, USART2 Channel 7, USART3 Channel 2
 /***TX********************/
 DMA_DeInit(DMA1_Channel4);
 DMA_Cmd(DMA1_Channel4, DISABLE);

 /***RX********************/
 DMA_DeInit(DMA1_Channel5);
 DMA_Cmd(DMA1_Channel5, DISABLE);
 }

 /******************************************************************************/

 void DMA_USART_Rx (void){
 DMA_InitTypeDef DMA_InitStructure;

 DMA_DeInit(DMA1_Channel5);

 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)RXpacket;
 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
 DMA_InitStructure.DMA_BufferSize = 1; //20 in the end
 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_High;
 DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,ENABLE);
 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
 DMA_ClearFlag(DMA1_FLAG_TC5);
 DMA_Init(DMA1_Channel5, &DMA_InitStructure);
 DMA_Cmd(DMA1_Channel5, ENABLE);

 /* Wait of DMA completion */
 USART_Cmd(USART2, ENABLE);

 DMA_ITConfig(DMA1_Channel5, DMA_IT_TC , ENABLE);

 while(DMA_GetFlagStatus(DMA1_FLAG_TC5) == RESET);

 /* Loop until the end of transmit */
 while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);

 }
 /******************************************************************************/
 void NVIC_Configuration_DMA_RX(void)
 {
 NVIC_InitTypeDef NVIC_InitStructure;

 //Enable the USARRx Interrupt
 NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn ;

 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//ENABLE
 NVIC_Init(&NVIC_InitStructure);
 /******************************************************************************/
 void DMA1_Channel5_IRQHandler(void)
 {
 /*This part never start*/

 }
 /******************************************************************************/

 BUT WHEN I USED :
 /******************************************************************************/
 void NVIC_Configuration_Irq(void)
 {
 NVIC_InitTypeDef NVIC_InitStructure;

 //Enable the USART_Rx Interrupt
 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//ENABLE
 NVIC_Init(&NVIC_InitStructure);

 }
 /******************************************************************************/
 void USART1_IRQHandler(void) //Reading data
 {
 GPIO_WriteBit(LED_PORT, GPIO_Pin_13, Bit_SET);
 if ((USART1->SR & USART_FLAG_RXNE) != (u16)RESET);
 DMA_USART_Rx();

 }
 /******************************************************************************/
  iTS FIRST TIME GO IN USART1 INTERUPT AND THEN IN DMA1_Channel5....

  sorry about your time but i cant find some DMA Rx example .....

 second problem is that i want to use 20byte buffer - but in debugger always i recieve only firt
 i use -DMA_InitStructure.DMA_BufferSize = 20; and in debugger i see that first is recieved bud only firt
 isnt problem that dma cant read whole buffer -its read one by one ?

 somewhere i read that the DMA_IT_TC is possible to use only in M2M mode ....
 would you be so kind and take a look at code
 thanks

Outcomes