AnsweredAssumed Answered

USART Pin Remapping

Question asked by asl.sean on Dec 6, 2013
Latest reply on Dec 16, 2013 by asl.sean
Hi , 

I am working on USART DMA for STM32F1 . Using one of Clive1 example i successfully got it working . However the program fails when i try to remap USART1 pin from PA9/PA10 to PB6/PB7. Is there something else that i miss out?

void USART_RCC_Configuration(void)
{
/* Enable DMA clock */
RCC_AHBPeriphClockCmd(COMM_USART_DMA_CLK, ENABLE);
      
  /* Enable GPIO clock */
  RCC_APB2PeriphClockCmd(COMM_USART_GPIO_CLK, ENABLE);
  
  /* Enable UART clock */
  RCC_APB2PeriphClockCmd(COMM_USART_CLK, ENABLE);
}
  
/**************************************************************************************/
  
void USART_GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
     
  /* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = COMM_USART_TxPin; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 
  GPIO_Init(COMM_USART_GPIO, &GPIO_InitStructure);
  
  /* Configure USART Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = COMM_USART_RxPin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(COMM_USART_GPIO, &GPIO_InitStructure);
     
 GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
}
  
/**************************************************************************************/
  
void USART_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
  
  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_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  
  /* USART configuration */
  USART_Init(COMM_USART, &USART_InitStructure);
  
  /* Enable the USART1 */
  USART_Cmd(COMM_USART, ENABLE);
}
  
/**************************************************************************************/
  
 char TxBuffer[] = "The quick brown fox jumps over the lazy dog\r\n";
 char RxBuffer[6] = "      ";
 
 
void USART_DMA_Tx_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;
  
  DMA_DeInit(COMM_USART_Tx_DMA_Channel);
  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&COMM_USART->DR;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_BufferSize = sizeof(TxBuffer) - 1;
  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(COMM_USART_Tx_DMA_Channel, &DMA_InitStructure);
  
  USART_DMACmd(COMM_USART, USART_DMAReq_Tx, ENABLE);
  
  /* Enable DMA Stream Transfer Complete interrupt */
  DMA_ITConfig(COMM_USART_Tx_DMA_Channel, DMA_IT_TC, ENABLE);
  
  DMA_Cmd(COMM_USART_Tx_DMA_Channel, ENABLE);
 
}
 
void USART_DMA_Rx_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;
  
  DMA_DeInit(COMM_USART_Rx_DMA_Channel);
  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&COMM_USART->DR;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = sizeof(RxBuffer) - 1;
  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(COMM_USART_Rx_DMA_Channel, &DMA_InitStructure);
  
  USART_DMACmd(COMM_USART, USART_DMAReq_Rx, ENABLE);
  
  /* Enable DMA Stream Transfer Complete interrupt */
  DMA_ITConfig(COMM_USART_Rx_DMA_Channel, DMA_IT_TC, ENABLE);
  
  DMA_Cmd(COMM_USART_Rx_DMA_Channel, ENABLE);
}
  
/**************************************************************************************/
  
void COMM_USART_DMA_TX_IRQHandler(void) // USART1_TX
{
  /* Test on DMA Transfer Complete interrupt */
  if (DMA_GetITStatus(COMM_USART_Tx_DMA_FLAG))
  {
    /* Clear DMA Transfer Complete interrupt pending bit */
    DMA_ClearITPendingBit(COMM_USART_Tx_DMA_FLAG);
  
    /* ... */
  }
}
  
/**************************************************************************************/
  
void COMM_USART_DMA_RX_IRQHandler(void) // USART1_RX
{
  /* Test on DMA Transfer Complete interrupt */
  if (DMA_GetITStatus(COMM_USART_Rx_DMA_FLAG))     
  {
         
        if (strcmp("Hello",RxBuffer) == 0){
            strcpy(TxBuffer, "I See Hello");           
        }else{
            strcpy(TxBuffer, "Confused");
        }
        USART_DMA_Tx_Configuration();
  
        while (DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET);
        USART_DMA_Rx_Configuration();
        memset(RxBuffer,'\0',sizeof(RxBuffer));    
 
    /* Clear DMA Transfer Complete interrupt pending bit */
    DMA_ClearITPendingBit(COMM_USART_Rx_DMA_FLAG);
  
    /* ... */
  }
}
  
/**************************************************************************************/
  
void USART_NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* Configure the Priority Group to 2 bits */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  
 
        /* Enable the USART1 TX DMA Interrupt */
        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);
      
        /* Enable the USART1 RX DMA Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
 
}
#define COMM_USART                   USART1
#define COMM_USART_GPIO              GPIOB
#define COMM_USART_CLK               RCC_APB2Periph_USART1
#define COMM_USART_GPIO_CLK          RCC_APB2Periph_GPIOB
#define COMM_USART_RxPin             GPIO_Pin_7
#define COMM_USART_TxPin             GPIO_Pin_6
#define COMM_USARTz_DR_Base          0x40013804
#define COMM_USART_IRQn              USART1_IRQn
 
#define COMM_USART_DMA_CLK           RCC_AHBPeriph_DMA1
#define COMM_USART_Tx_DMA_Channel    DMA1_Channel4
#define COMM_USART_Tx_DMA_FLAG       DMA1_IT_TC4
#define COMM_USART_Rx_DMA_Channel    DMA1_Channel5
#define COMM_USART_Rx_DMA_FLAG       DMA1_IT_TC5
 
#define COMM_USART_DMA_TX_IRQHandler    DMA1_Channel4_IRQHandler
#define COMM_USART_DMA_RX_IRQHandler    DMA1_Channel5_IRQHandler

Outcomes