AnsweredAssumed Answered

How to implement usart rx timeout for dma?

Question asked by berger.patrick on Mar 25, 2013
Latest reply on Mar 25, 2013 by Clive One
Hi all
I've a little problem with my usart: sending works well but now I want to modify my code for dma support (tx works also). In the AN3109 I've read, that we can handle the end of a transmission with a timeout. My controller (STM32F051) has a timeout-function with interrupts, so this is no problem. But I my rxBuffer and the Count is always 0.
Could anyone help me?

[c]

void CSerialPort::vSetup (void)

  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  DMA_InitTypeDef  DMA_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
 
 
  RCC_APB2PeriphClockCmd(RS485RccPcUSART, ENABLE);                              /// Enable USART clock 
  RCC_AHBPeriphClockCmd(RS485RccPcRxPort | RS485RccPcTxPort | RS485RccPcDePort, ENABLE);        /// Enable GPIO clock
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);                            /// Enable DMA clock
 
  /// Configure GPIO Pins, AF
  GPIO_PinAFConfig(RS485RxPort, RS485RxPinSource, RS485GPIOAf);
  GPIO_PinAFConfig(RS485TxPort, RS485TxPinSource, RS485GPIOAf);
  GPIO_PinAFConfig(RS485DePort, RS485DePinSource, RS485GPIOAf);
 
  /// Configure GPIO pins:  Rx
  GPIO_InitStructure.GPIO_Pin =  RS485RxPin;                                    /// RX Pin
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                             /// High speed
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                                  /// Alternate function
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                                /// Push-Pull
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;                                  /// Pull-up
  GPIO_Init(RS485RxPort, &GPIO_InitStructure);

  /// Configure GPIO pins:  Tx
  GPIO_InitStructure.GPIO_Pin = RS485TxPin;                                     /// TX Pin
  GPIO_Init(RS485TxPort, &GPIO_InitStructure);

  /// Configure GPIO pins: DE
  GPIO_InitStructure.GPIO_Pin = RS485DePin;
  GPIO_Init(RS485DePort, &GPIO_InitStructure);
 
  /// Configure nested vectored interrupt controller
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
  /// Configure USART
  USART_InitStructure.USART_BaudRate = RS458Baudrate;                           /// Baudrate
  USART_InitStructure.USART_WordLength = RS458WordLength;                       /// Databits
  USART_InitStructure.USART_StopBits = RS485Stopbits;                           /// Stopbits
  USART_InitStructure.USART_Parity = RS485Parity;                               /// Parity
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS;/// <<- DE
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;               /// Mode
  USART_Init(RS485USART, &USART_InitStructure);
 
  /// Modbus RTO settings
  USART_MSBFirstCmd(RS485USART, DISABLE);                                       /// LSB first transmitted/received
  USART_SetReceiverTimeOut(RS485USART, ModbusTimeOut);                          /// Timeout
  USART_ReceiverTimeOutCmd(RS485USART, ENABLE);                                 /// Enables the receiver Time Out feature.
  USART_ITConfig(RS485USART, USART_IT_RTO, ENABLE);                             /// Enable receive time out interrupt
///  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                                /// Enable single Rx interrupts
 
  /// RS485 settings
  USART_DECmd(RS485USART, ENABLE);                                              /// Enable the Driver Enable
  USART_DEPolarityConfig(RS485USART, USART_DEPolarity_High);                    /// Configures the Driver Enable polarity -> high-active
  USART_SetDEAssertionTime(RS485USART, RS485DEtime);                            /// Configures the Driver Enable assertion time (expressed in sample time units: 1/8 or 1/16 bit time,depending on the oversampling rate)
  USART_SetDEDeassertionTime(RS485USART, RS485DEtime);                          /// Configures the Driver Enable deassertion time (expressed in sample time units: 1/8 or 1/16 bit time,depending on the oversampling rate)
 
  /// Configure DMA
  /// USART1_TX Channel 2,4 / USART1_RX Channel 3,5 / USART2_TX Channel 4 / USART2_RX Channel 5
  SYSCFG_DMAChannelRemapConfig(SYSCFG_DMARemap_USART1Rx, DISABLE);              /// Remap USART1Rx to channel 3 (default)
  SYSCFG_DMAChannelRemapConfig(SYSCFG_DMARemap_USART1Tx, DISABLE);              /// Remap USART1Tx to channel 2 (default)
 
  DMA_DeInit(DMA1_Channel2);                                                    /// TX
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;                            /// Read from memory
  DMA_InitStructure.DMA_MemoryBaseAddr = (sf_uint32)m_au8USARTtxBuffer;         /// Ram-Buffer
  DMA_InitStructure.DMA_BufferSize = 0;                                         /// Buffer size
  DMA_InitStructure.DMA_PeripheralBaseAddr = (sf_uint32)&USART1->TDR;           /// Peripheral address
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;              /// Only one peripheral address
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                       /// -> Array
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                                 /// Send array only once
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;                                  /// Memory to memory disable
  DMA_Init(DMA1_Channel2, &DMA_InitStructure);  
  USART_DMACmd(RS485USART, USART_DMAReq_Tx, ENABLE);                            /// Enable the USART Tx DMA request
 
  DMA_DeInit(DMA1_Channel3);                                                    /// RX
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;                            /// Read from peripheral
  DMA_InitStructure.DMA_MemoryBaseAddr = (sf_uint32)m_au8USARTrxBuffer;         /// Ram-Buffer
  DMA_InitStructure.DMA_BufferSize = USARTrxBufferSize;                         /// Buffer size
//DMA_InitStructure.DMA_BufferSize = 12;
//DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_Init(DMA1_Channel3, &DMA_InitStructure);  
  USART_DMACmd(RS485USART, USART_DMAReq_Rx, ENABLE);                            /// Enable the USART Rx DMA request
  
  DMA_Cmd(DMA1_Channel2, ENABLE);                                               /// Enable the DMA Tx Stream 
  DMA_Cmd(DMA1_Channel3, ENABLE);                                               /// Enable the DMA Rx Stream 
 
  USART_Cmd(RS485USART,ENABLE);                                                 /// Enable USART
}

void CSerialPort::UsartRtoCallBack (void)

  sf_uint16 NumRxBytes = 0;
  NumRxBytes = DMA_GetCurrDataCounter(DMA1_Channel3);
  uint8_t data[256]={0}; 
GPIOC->BSRR = GPIO_Pin_9;
  for(sf_uint32 i = 0; i < 0xFF; i ++)
  {
    vSendByte(m_au8USARTrxBuffer[i]);
  }
GPIOC->BRR = GPIO_Pin_9;
}

void USART1_IRQHandler (void)
{
  if(USART_GetITStatus(RS485USART, USART_IT_RTO) != RESET)                      /// Timeout event
  {
    CSerialPort::GetInstance()->UsartRtoCallBack();                             /// Callback function
    USART_ClearITPendingBit(RS485USART, USART_IT_RTO);
  }
}
[/c]

Best regards
P51D

 

 

Outcomes