Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Last active May 26, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/084d5f066c49a61495dcb4b6d4c4e547 to your computer and use it in GitHub Desktop.
Save mamemomonga/084d5f066c49a61495dcb4b6d4c4e547 to your computer and use it in GitHub Desktop.
STM32 Nucleo F401RE の USARTでHello World

STM32 Nucleo F401RE の UARTでHello World

  • STM32 Nucleo F401RE macOSでビルドとアップロードしてLチカ
  • STM32 NucleoのUSART2はST-LinkのUARTと繋がっている。
  • Linuxマシン(Raspberry Piなど)に接続すると、/dev/ttyACM0として認識される
  • STM32CubeMXのF401REボードのデフォルト設定、Makefile用で初期コードを生成して開始する。
  • 要screen、終了はCTRL+A, k → y

コード

Makefile

追加

# C sources
C_SOURCES =  \
App/app.c \ ←追加
Src/main.c \
Src/stm32f4xx_it.c \
Src/stm32f4xx_hal_msp.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c \
Src/system_stm32f4xx.c  

# C includes
C_INCLUDES =  \
-IApp \ ←追加 
-IInc \
-IDrivers/STM32F4xx_HAL_Driver/Inc \
-IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32F4xx/Include \
-IDrivers/CMSIS/Include \
-IDrivers/CMSIS/Include

末尾に追加

include user.make

Src/main.c

追記、それぞれのUSER CODEのBEGINとENDの間にコードを入れる

/* USER CODE BEGIN Includes */
#include "app.h"
/* USER CODE END Includes */

/* USER CODE BEGIN 2 */
App_Init(huart2);
/* USER CODE END 2 */

/* USER CODE BEGIN 3 */
	App_MainLoop();
}
/* USER CODE END 3 */

user.make

新規作成

flash:
	st-flash --reset --format ihex write $(BUILD_DIR)/$(TARGET).hex

serial:
	screen /dev/ttyACM0 115200

.PHONY: flash serial

App/app.h

#ifndef __APP_H
#define __APP_H

#include "main.h"

void App_Init(UART_HandleTypeDef huart);
void App_MainLoop();

#endif

App/app.c

#include "app.h"
#include <string.h>

UART_HandleTypeDef huart;

void App_Init(UART_HandleTypeDef h) {
	huart = h;
}

void say(char *buf) {
	HAL_UART_Transmit(&huart,(uint8_t *)buf,strlen(buf)+1,0xFFFF);
}

void App_MainLoop() {
	HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);

	say("A"); HAL_Delay(100); say("B"); HAL_Delay(100); say("C"); HAL_Delay(500);
	say(" ");
	say("Hello "); HAL_Delay(50); say("World!\r\n"); HAL_Delay(50);

	HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
	HAL_Delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment