STM32 アプリケーション ノート 2606 でこれについて説明していますが、簡単なコード例はありません。
2 に答える
この回答は、IAR EWARM を使用して STM32F072 Nucleo ボードでテストされています。この回答では、「STM32 Standard Peripheral Library」のみを使用しています。
正常にブートローダー モード (DFU モード) になっていることを確認する最善/最も簡単な方法は、USB-2-UART コンバーター (Sparkfun から入手: http://sfe.io/p9873で 15 ドル) を回線に接続することです。 PA_9 (USART1_TX) と PA_10 (USART1_RX) (グランドも接続することを忘れないでください)。Nucleo USART2 のデフォルト接続 (/dev/ttyACM0) を使用できなかったため、外部 USB-2-USART 接続を使用しました。次に、USART 接続で 0x7F を書き込む簡単な C プログラムを作成します。DFU モードの場合は、1 バイトの 0x79 で応答します。私は Ubuntu を使用しているため、テスト プログラムは Linux 上でコンパイルおよび実行されます。
また、ブートローダ モード (別名 DFU モード) をテストする最も簡単な方法は、BOOT0 ラインを +3.3V にジャンパすることです。これらは、Nucleo 上で互いに隣接しています。
main.c の main() ルーチンに追加します。
// Our STM32 F072 has:
// 16k SRAM in address 0x2000 0000 - 0x2000 3FFF
*((unsigned long *)0x20003FF0) = 0xDEADBEEF;
// Reset the processor
NVIC_SystemReset();
SystemInit() 関数の先頭で Libraries/sysconfig/system_stm32f0xx.c にいくつかのコードを追加します。
// Define our function pointer
void (*SysMemBootJump)(void);
void SystemInit (void)
{
// Check if we should go into bootloader mode.
//
// Set the main stack pointer __set_MSP() to its default value. The default
// value of the main stack pointer is found by looking at the default value
// in the System Memory start address. Do this in IAR View -> Memory. I
// tried this and it showed address: 0x200014A8 which I then tried here.
// The IAR compiler complained that it was out of range. After some
// research, I found the following from "The STM32 Cortex-M0 Programming
// Manual":
// Main Stack Pointer (MSP)(reset value). On reset, the processor
// loads the MSP with the value from address 0x00000000.
//
// So I then looked at the default value at address 0x0 and it was 0x20002250
//
// Note that 0x1fffC800 is "System Memory" start address for STM32 F0xx
//
if ( *((unsigned long *)0x20003FF0) == 0xDEADBEEF ) {
*((unsigned long *)0x20003FF0) = 0xCAFEFEED; // Reset our trigger
__set_MSP(0x20002250);
// 0x1fffC800 is "System Memory" start address for STM32 F0xx
SysMemBootJump = (void (*)(void)) (*((uint32_t *) 0x1fffC804)); // Point the PC to the System Memory reset vector (+4)
SysMemBootJump();
while (1);
}
... // The rest of the vanilla SystemInit() function
ブートローダー モード (別名 DFU モード) にあるかどうかを確認するための簡単なユーティリティを作成します。これは Linux 上でコンパイルおよび実行されます。シリアルポートが正しいことを確認してください。以下に示すように、/dev/ttyUSB0 になる可能性があります。
//
// A bare-bones utility: Test if the STM32 is in DFU mode
// (aka bootloader mode, aka firmware update mode).
//
// If it is in DFU mode, you can send it 0x7F over a UART port and it
// will send 0x79 back.
//
// For details, see the STM32 DFU USART spec.
//
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h> // errno
#define DEFAULT_SERDEVICE "/dev/ttyUSB0"
//#define DEFAULT_SERDEVICE "/dev/ttyACM0"
int main(int argc, char **argv)
{
int fd, cooked_baud = B9600;
char *sername = DEFAULT_SERDEVICE;
struct termios oldsertio, newsertio;
unsigned char mydata[2] = {0};
mydata[0] = 0x7F;
mydata[1] = 0;
/* Not a controlling tty: CTRL-C shouldn't kill us. */
fd = open(sername, O_RDWR | O_NOCTTY);
if ( fd < 0 )
{
perror(sername);
exit(-1);
}
tcgetattr(fd, &oldsertio); /* save current modem settings */
/*
* 8 data, EVEN PARITY, 1 stop bit. Ignore modem control lines. Enable
* receive. Set appropriate baud rate. NO HARDWARE FLOW CONTROL!
*/
newsertio.c_cflag = cooked_baud | CS8 | CLOCAL | CREAD | PARENB;
/* Raw input. Ignore errors and breaks. */
newsertio.c_iflag = IGNBRK | IGNPAR;
/* Raw output. */
newsertio.c_oflag = OPOST;
/* No echo and no signals. */
newsertio.c_lflag = 0;
/* blocking read until 1 char arrives */
newsertio.c_cc[VMIN]=1;
newsertio.c_cc[VTIME]=0;
/* now clean the modem line and activate the settings for modem */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newsertio);
// Here is where the magic happens
write(fd,&mydata[0],1);
int red = read(fd,&mydata[1],1);
if (red < 0) {
fprintf(stderr, "Error: read() failed, errno [%d], strerrer [%s]\n",
errno, strerror(errno));
}
tcsetattr(fd,TCSANOW,&oldsertio);
close(fd);
printf("Read [%d] bytes: [0x%x]\n", red, mydata[1]);
return 0;
}
私のプロジェクトでは、基本的に Brad と同じことをしていますが、SystemInit() 関数を変更していません。
CubeMX HAL は次のように定義します。
void __attribute__((weak)) __initialize_hardware_early(void);
これは - 私の場合 - SystemInit(); を呼び出すだけです。
したがって、この関数を上書きするだけです:
#include <stdint.h>
#include "stm32f0xx_hal.h"
#define SYSMEM_RESET_VECTOR 0x1fffC804
#define RESET_TO_BOOTLOADER_MAGIC_CODE 0xDEADBEEF
#define BOOTLOADER_STACK_POINTER 0x20002250
uint32_t dfu_reset_to_bootloader_magic;
void __initialize_hardware_early(void)
{
if (dfu_reset_to_bootloader_magic == RESET_TO_BOOTLOADER_MAGIC_CODE) {
void (*bootloader)(void) = (void (*)(void)) (*((uint32_t *) SYSMEM_RESET_VECTOR));
dfu_reset_to_bootloader_magic = 0;
__set_MSP(BOOTLOADER_STACK_POINTER);
bootloader();
while (42);
} else {
SystemInit();
}
}
void dfu_run_bootloader()
{
dfu_reset_to_bootloader_magic = RESET_TO_BOOTLOADER_MAGIC_CODE;
NVIC_SystemReset();
}