TrueSTUDIOとSTM32CubeでもUART通信をやってみました。TureSTUDIOはDebuggerが統合されているのでデバッグにはそっちを使えばいいという話もありますが、ロギングやらなんやかやでUARTも必要になりそうです。
環境は
- Nucleo-F446RE
- STM32CubeMX Version 5.0.1
- STM32Cube FW_F4 V1.23.0
- TrueSTDUIO Version: 9.2.0
- Windows10 + Putty0.7.0
参考にしたドキュメントは
ST公式の「UM1718 User manual STM32CubeMX for STM32 configuration and initialization C code generation」の「11 Tutorial 4 - Example of UART communications with an STM32L053xx Nucleo board」です。Nucleo-F446RE用に一部読み替えています。
STM32CubeMXでCコードの初期化
STM32CubeMXで「New Project」 - 「Board Selector」を選択。「NUCLEO-F446RE」を選択し、「Start Project」
デフォルトモードで初期化。「Initialize all peripherals with their default Mode?」で「Yes」を選択
「SYS」 - 「Debug」 - 「Serial Wire」が選択されているのを確認
「Timers」 - 「TIM2」 - 「Clock Source」 「Internal Clock」を選択
※TIM2は周期的にTimer割り込みを発生するものでUART通信に必須ではありません。
「Connectivity」 - 「USART2」 - 「Mode」 - 「Asynchronouse」が選択されているのを確認
「Pinout」 - 「Pinout view」で下記のピンがアサインされているのを確認
- PA13 TMS (SWD:SWDIO)
- PA14 TCK (SWD:SWCLK)
- PA2 USART_TX
- PA3 USART_RX
「Clock Configuration」 デフォルトのまま
※ドキュメント(UM1718)ではHCLKが2.097MHzになっていますが、F446のデフォルトの84MHzでテストしました。
「Pinout & Configuration」タブの「USART2」 - 「Parameter Setting」で
- 「Baud Rate」を「9600 Bits/s」に設定
- 「Data Direction」で「Receive & Transmit」が選択されているのを確認
「TIM2」 - 「Parameter Setting」で
- 「Prescaler」を「16000」に設定
- 「Counter Period」を「1000」に設定
「TIM2」 - 「NVIC Settings」で
- 「TIM2 global interrupt」の「Enabled」をチェック
「Project Manager」 - 「Project」 - 「Toolchain / IDE」で「TrueSTUDIO」を選択
「Code Gengerator」 「HAL Settings Set all free pins as analog」をチェック
「Generate Code」して、「Open Project」を選択
TrueSTUDIOでコードの追加・実行
TrueSTUDIOが立ち上がるので、C/C++パースペクティブでソースに以下のコードを追加。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* USER CODE BEGIN 0 */ | |
#include "stdio.h" | |
#include "string.h" | |
/* Buffer used for transmission and number of transmissions */ | |
char aTxBuffer[1024]; | |
int nbtime=1; | |
/* USER CODE END 0 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* USER CODE BEGIN 2 */ | |
/* Start Timer event generation */ | |
HAL_TIM_Base_Start_IT(&htim2); | |
/* USER CODE END 2 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* USER CODE BEGIN 4 */ | |
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |
{ | |
sprintf(aTxBuffer,"STM32CubeMX rocks %d times \t", ++nbtime); | |
HAL_UART_Transmit(&huart2,(uint8_t *) aTxBuffer, strlen(aTxBuffer), 5000); | |
} | |
/* USER CODE END 4 */ |
Puttyの設定
Puttyに以下のような文字列が表示されました。
TIM2の「Counter Period」を「10000」に増やすとPutty上でゆっくりと表示されるようになりました。
HCLKが84MHz、プリスケーラが16000なので
84MHz / 16000 = 5.25kHzCounterが1000の場合5.25Hzなので割り込み周期は約190ms
Counterが10000の場合0.525Hzなので割り込み周期は約1.9s
メモ:
STM32CubeのHALの使い方の習得がそこそこ大変そうですが、飼い馴らせば強力そうです。
SPI、I2C、I2S、FPUあたりの使い方から探っていくつもりです。音絡みがメインなのでCMSIS DSPも使ってみたいですが1年ぐらいかかるかな。
0 件のコメント:
コメントを投稿