CCWO Embedded Space

CCWOの日々の開発を発信するブログ

STM32 F401RE UART その1

開発環境
・評価ボード NUCLEO-F401RE
・IDE MDK-ARM V5 and STM32CubeMX
・OS windows 10 pro
・本

通信で□が送られる原因はUSART2の設定をSynchronousにしていからでした。
SynchronousではなくAsyncronousにしたところ望み通りの動作しました。
CubeMXの設定はこんな感じで。
f:id:CCWO:20160915221823p:plain
左側ツリーがAsynchronousにしてみた。
それを反映さえたプログラムがこちら。

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"

/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
char sio_getc(void);		// UARTから1文字取得
void sio_putc(char);		// UARTに1文字を出力
void sio_puts(char *);	// UARTに文字列を出力

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */
char sio_getc()
{
	char ch;
	while(HAL_UART_Receive(&huart2, (uint8_t *)&ch, 1, 1000)==HAL_TIMEOUT) ;
	return ch;
}

void sio_putc(char ch)
{
	HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 1000);
}

void sio_puts(char *str)
{
	HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen(str), 1000);
}
/* USER CODE END 0 */

int main(void)
{

  /* USER CODE BEGIN 1 */
	int ch;

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();

  /* USER CODE BEGIN 2 */
	sio_puts("F401RE-USART2\n");

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  ch = sio_getc();
	  sio_putc(ch);
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}

これでエコーバックが正しく行えました。