cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX 4.7.1 Make USART2(REMAP_USART2) Code Run Error!

wjandsq
Associate III
Posted on May 22, 2015 at 05:56

//--------------------------------------------------------------------------------------------------------------

  /* USER CODE BEGIN USART2_MspInit 0 */

  /* USER CODE END USART2_MspInit 0 */

    /* Peripheral clock enable */

    __USART2_CLK_ENABLE();

  

    /**USART2 GPIO Configuration    

    PD5     ------> USART2_TX

    PD6     ------> USART2_RX 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_5;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_6;

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    __HAL_AFIO_REMAP_USART2_ENABLE(); // here STM32F103ZE run error!

  /* System interrupt init*/

    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(USART2_IRQn);

  /* USER CODE BEGIN USART2_MspInit 1 */

  /* USER CODE END USART2_MspInit 1 */

//--------------------------------------------------------------------------------------------------------------

USART2 code run error! but old code is normal! use USE_STDPERIPH_DRIVER lib

//--------------------------------------------------------------------------------------------------------------

   GPIO_InitTypeDef GPIO_InitStructure;

   USART_InitTypeDef USART_InitStructure;

&sharpifdef  USART2_Remap_PD5_PD6

 /* Configure USART2 GPIO PD5 PD6 */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO,ENABLE);

&sharpelse

 /* Configure USART2 GPIO PA2 PA3 */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);

&sharpendif

 /* Configure and enable the USART2 */

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

&sharpifdef  USART2_Remap_PD5_PD6

 /* USART2 TX PD.05 */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_Init(GPIOD, &GPIO_InitStructure);

 /* USART2 RX PD.06 */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

 GPIO_Init(GPIOD, &GPIO_InitStructure);

 /*Configure peripheral I/O remapping */

 GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);

&sharpelse

 /* USART2 TX PA.02 */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* USART2 RX PA.03 */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

&sharpendif

 USART_InitStructure.USART_BaudRate = linecoding2.bitrate;

 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);

 USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);

#stm32cubemx-4.7.1-remap_usart2
3 REPLIES 3
wjandsq
Associate III
Posted on May 22, 2015 at 07:34

USART2 REMAP have no error, but Receive data number is not correct?

why the first char of receive is always 0xF8 ? 

/* USER CODE BEGIN 0 */

#define USART1_RXD_SBUF_MAX 4096

#define USART2_TXD_SBUF_MAX 4096

uint8_t USART1_RXD_Buffer[USART1_RXD_SBUF_MAX];

uint8_t USART2_TXD_Buffer[USART2_TXD_SBUF_MAX];

#define USART1_RX_LEN 11

#define USART2_TX_LEN 10

/* USER CODE END 0 */

  /* USER CODE BEGIN 1 */

  uint16_t i;

  for(i = 0; i < USART2_TX_LEN; ++i)  {

    USART2_TXD_Buffer[i] = i%128;

  }

  /* USER CODE END 1 */

  /* Initialize all configured peripherals */

  MX_USART1_UART_Init();

  MX_USART2_UART_Init(); /* F103/429 USARTx_TX Pin have a char,why ?*/

  /* USER CODE BEGIN 2 */

  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13,GPIO_PIN_SET); // Error

  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_SET); // Normal

  HAL_UART_Receive_IT(&huart1, &USART1_RXD_Buffer[0],USART1_RX_LEN);

  HAL_UART_Transmit_IT(&huart2,&USART2_TXD_Buffer[0],USART2_TX_LEN);

  /* USER CODE END 2 */

0690X00000603OuQAI.jpg

wjandsq
Associate III
Posted on May 22, 2015 at 08:20

but baud ratechange to 19200 bps, all receive data and number is

correct

!

when baud rate up to 1000000 bps , into this code and not exit!

Of course, baud rate is 230400bps,hardware have noproblem 。

if(huart->ErrorCode != HAL_UART_ERROR_NONE)

{

/* Set the UART state ready to be able to start again the process */

huart->State = HAL_UART_STATE_READY;

HAL_UART_ErrorCallback(huart);

}

USE_STDPERIPH_DRIVER, USB_Dual_CDC, two MAX3232, hardware test is ok:

0690X0000060MojQAE.gif

wjandsq
Associate III
Posted on May 22, 2015 at 13:40

/* Initialize all configured peripherals */

MX_USART1_UART_Init();

MX_USART2_UART_Init(); /* USART2 init, but not send */

/* USER CODE BEGIN 2 */

HAL_UART_Receive_IT(&huart1, &USART1_RXD_Buffer[0],USART1_RX_LEN);

//HAL_UART_Transmit_IT(&huart2,&USART2_TXD_Buffer[0],USART2_TX_LEN); /* not run ,but still usart1 receive a 0xF8 */

/* USER CODE END 2 */

0690X0000060MokQAE.gif

/* Initialize all configured peripherals */

MX_USART1_UART_Init();

//MX_USART2_UART_Init(); /* off this code, normal */

/* USER CODE BEGIN 2 */

HAL_UART_Receive_IT(&huart1, &USART1_RXD_Buffer[0],USART1_RX_LEN);

//HAL_UART_Transmit_IT(&huart2,&USART2_TXD_Buffer[0],USART2_TX_LEN); /* not run */

/* USER CODE END 2 */

0690X0000060MolQAE.gif