วิธีการ Flash Firmware MicroPython ลงใน Black Pill
ย่อโดยคัดลองจาก https://medium.com/@rawat.s/micropython-for-stm32f411ceu-black-pill-embedded-programming-style-5363456e94b3
เหตุที่ต้องย่อเนื่องจาก
1.ผมถนัดใช้ Tool ง่ายๆ เช่น การ Flash ผ่าน USB Port ซึ่งมีสาย USB Type C อยู่แล้ว
2.เลือกใช้โปรแกรมที่ติดตั้งง่ายหรือติดตั้งอยู่แล้วในเครื่องผม
3.ตัวโปรแกรมเล็กๆ ไม่กินทรัพยากร PC มากและเป็นโปรแกรมใช้งานง่ายๆ ทำงานเสร็จโดยคลิกไม่กี่คลิก
ขอขอบคุณผู้เขียนต้นฉบับด้วยครับ
Black Pill เป็นบอร์ดราคาถูก (ผลิตในประเทศจีน โดย WeAct Studio) ใช้ไมโครคอนโทรลเลอร์รุ่น STM32F411CEU6 (Datasheet) ภายในมีตัวประมวลผลหรือซีพียู ARM Cortex-M4F หน่วยความจำ Flash 512 KB และ หน่วยความจำ SRAM 128 KB สามารถใช้ความถี่ในการทำงานของซีพียูได้สูงถึง 100MHz เนื่องจากบอร์ดนี้มีสีดำ จึงมีการตั้งชื่อหรือเรียกกันว่า STM32 Black Pill
ข้อสังเกต: ยังมีบอร์ด Black Pill STM32F401CCU6 ที่มีลักษณะเหมือนกันให้เลือกใช้งานได้ แต่มีขนาดของหน่วยความจำ Flash น้อยกว่า และใช้ความถี่ได้น้อยกว่า (84 MHz, 256 KB Flash, 64KB SRAM) ดังนั้นจึงมีราคาถูกว่า STM32F411CEU เล็กน้อย
Note. 1.STM32F401CCU6 board 64KB RAM 256KB ROM
2.STM32F411CEU6 board 128KB RAM 512KB ROM
เมื่อเปรียบเทียบกับบอร์ด ST NUCLEO F411RE และบอร์ด STM32F411 Discovery Kit แม้ว่าจะใช้ชิป STM32F411RET6 หรือ STM32F411VET6 ที่มี ARM Cortex-M4F CPU เหมือนกัน มีขนาดหน่วยความจำเท่ากัน (512 KB Flash และ 128 KB SRAM) มีจำนวนขา I/O มากกว่า (มี 64 ขา) และมีวงจร ST-Link V2 รวมไว้บนบอร์ดแล้ว แต่ก็มีราคาสูงกว่าและมีขนาดใหญ่กว่า ไม่เหมาะสำหรับการนำมาต่อใช้งานบนเบรดบอร์ด
รูปบอร์ด WeAct STM32F411CEU Black Pill มุมมองด้านบน และด้านล่าง (Top / Bottom View)
แผนผังแสดงตำแหน่งขาต่าง ๆ ของบอร์ด STM32F411 Black Pill (Pinout PDF File)
ดาวน์โหลดและติดตั้งไฟล์ MicroPython Firmware (.hex) จาก Github สำหรับบอร์ด WeAct STM32F411 https://github.com/WeActTC/WeAct_F411CE-MicroPython/releases เช่น
Hex File: firmware_internal_rom_stm32f411_v1.12–35.hex
ถัดไปให้เตรียมอุปกรณ์ฮาร์ดแวร์ดังนี้
- เชื่อมต่อขา A10 (PA10/USB_FS_ID) ด้วยสาย Jumper Wire กับตัวต้านทาน 10k แบบ Pullup ไปยัง 3.3V (ผมไม่มีตัวต้านทานเลยต่อตรง)
- เสียบสาย USB-C เชื่อมต่อบอร์ดไมโครคอนโทรลเลอร์กับคอมพิวเตอร์
- กดปุ่ม BOOT0 กดค้างไว้ กดปุ่ม RESET แล้วจึงปล่อยปุ่ม RESET และ BOOT0 ตามลำดับ
การแปลงไฟล์ .hex เป็น .dfu เราจะใช้โปรแกรมชื่อ DFU File Manager ของ DfuSe กดปุ่ม S19 or Hex แล้วเลือกไฟล์ .hex แล้วกดปุ่ม Generate
ถัดไปให้เปิดโปรแกรม DfuSe Demo ถ้าเชื่อมต่อบอร์ด STM32 แล้วอยู่ในโหมด DFU จะมองเห็น Vendor ID: 0483, Product ID: DF11 และ Version: 2200
เมื่อมองเห็นอุปกรณ์ในโหมด DFU
กดปุ่ม Choose… ในส่วนของ Upgrade or Verify Action เลือกไฟล์ .dfu แล้วกดปุ่ม Upgrade เพื่อทำขั้นตอนสุดท้าย
เมื่เปิดไฟล์ .dfu เพื่อโหลดข้อมูล
ขณะทำขั้นตอน Upgrade (เขียนข้อมูลไปยังหน่วยความจำของ MCU)
เวอร์ชั่นที่ได้
MPY: sync filesystems
MPY: soft reboot
MicroPython v1.12–35-g10709846f on 2020–01–02; WeAct_Core with STM32F411CE
Type “help()” for more information.
การเชื่อมต่อกับ มอดูล LoRaWAN Maxiiot DL7612-AS923-TH
ให้เชื่อม 3.3V GND เช้ากับ 3.3 และ GND ของ Black Pill
TX เข้า ขา A3
RX เข้าขา A2
ลอง Run Code ต่อไปนี้
from machine import UART
import time
uart = UART(2, 115200,timeout=20)def sendATcommand(ATcommand):
print(“{0}\r\n”.format(ATcommand))
uart.write(“{0}\r\n”.format(ATcommand))
print(uart.read().decode(‘utf-8’))sendATcommand (‘AT+NCONFIG’)
จะเห็นผลลัพท์ ดังต่อไปนี้แสดงออกมาบนหน้าจอ
AT+NCONFIG+NCONFIG:CLASS,C
+NCONFIG:ISMBAND,2
+NCONFIG:CHMASK,00ff,0000,0000,0000,0000,0000
+NCONFIG:RXWIN2,923200000,2
+NCONFIG:ACTIVATE,1
+NCONFIG:POWER,0
+NCONFIG:PORT,2
+NCONFIG:ADR,1
+NCONFIG:DR,2,10,0,0
+NCONFIG:CFM,1
+NCONFIG:NNMI Enable,0
+NCONFIG:PNMI Enable,1
OK
เช็คพื้นที่เก็บโปรแกรมใช้งานจะเหลือเพียง 47 KB ซึ่งก็เพียงพอกับการใช้งานระดับหนึ่ง
Note! เพิ่มเติม การสร้าง MicroPython สำหรับ Blackpill
จาก https://github.com/SuperThunde
Dependencies (for ubuntu 18.04):
- git
- gcc
- make
- gcc-arm-none-eabi
- libnewlib-arm-none-eabi on Ubuntu 18.04
Build process (from WeAct)
1) Make MicroPython cross compiler
git clone https://github.com/micropython/micropython.git
cd micropython
git submodule update --init
cd mpy-cross
make -j4
2) Make MicroPython firmware image for the F411 board
cd ../ports/stm32/boards
git clone https://github.com/mcauser/WEACT_F411CEU6
cd ..make BOARD=WEACT_F411CEU6 -j4
- I used the mcauser repository, you can also use the WeAct repository
3) Retrieve build artifacts
cd build-WeAct_F411CE/
Where you can find
firmware.dfu
firmware.elf
firmware.hex
firmware.map
firmware0.bin
firmware1.bin
Firmware 0 and 1 must be flashed contiguously.
Optional: Enable external flash
If you have soldered an SPI flash chip to the underside of the board, or connected one to the pins, you can have MicroPython use that as storage instead of the integrated flash. In both cases, the pins A4 (CS), A5 (SCK), A6 (MISO), and A7 (MOSI) are now in use for SPI.
External SPI flash enabled:
#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0)
in mpconfigboard.h. See here for details on setting the defined flash chip size (eg 32Mbit, 64Mbit).
Flashing Process
You have a few options in how to upload the firmware. In all cases, you will want to put the board into programming mode by
- Hold NRST button
- Hold BOOT0 button
- Release NRST
- Release BOOT0
Then you can flash the board:
1) By DFU (with STM32CubeProgrammer)
- You can also use dfu-util as described in the mcauser repo
- Oddly, STM32CubeProgrammer does not accept .dfu files, but you can use the .hex or .elf
2) By ST-LINK
- Connect the ST-Link to the board. Remember not to plug the 3v3 pin in if you are powering the board by USB, or will be plugging USB in later.
3) By Serial
- Connect the board to a serial adapter and use STM32CubeProgrammer or st-flash to upload the firmware
Serial REPL and ‘USB drive’ of Python files
After flashing, press the NRST button to reboot the board.
It should now show up as a ~40KB mass storage device and a COM serial port device when plugged into your PC by USB. The serial port can be connected to at 115200 bps.
ใส่ SPI Memory เพิ่มใต้ Blackpill อ่าน
https://fightpc.blogspot.com/2021/02/extra-memory-for-your-f411-blackpill.html
หมายเหตุ ขา I2C
- I2C(1) is on the X position: (SCL, SDA) = (X9, X10) = (PB6, PB7)
- I2C(2) is on the Y position: (SCL, SDA) = (Y9, Y10) = (PB10, PB11)
Note!
ถ้าจะ Flash MicroPython ลงใน BluePill ทำได้รึเปล่า
อ่าน BluePill ได้ที่ https://windfis.ch/gd32vf103/gd32vf103.html
และ https://github.com/mcauser/BLUE_PILL_F103C8
BluePill คือ STM32F103 by ST หรือ GD32F103 by Gigadevice
Devboard เช่น STM32F103C8T6 หรือ Sipeed Longan Nano — RISC-V GD32VF103CBT6
Specifications:
STM32F103C8T6 ARM Cortex M3
72MHz, 90 DMIPS / 1.25 DMIPS / MHz
2.0V - 3.6V operating voltage
8MHz system crystal
32.768KHz RTC crystal
2.54mm pitch pins
JTAG/SWD header
64 KByte Flash, 20 KByte SRAM
2x SPI, 3x USART, 2x I2C, 1x CAN
38 I/Os almost all 5V-tolerant
1x USB 2.0 FS
2x ADC (12-bit / 16-channel)
3x general timers, 1x advanced timer
RT9193 3.3V LDO voltage regulator, max current 300mA
Micro USB for power and data
Blue user LED (PC13)
2x jumpers for bootloader selection
Reset button
2x20 side pins + 1x4 SWD pins
Dimensions: 53.0mm x 22.5mm
ปรกติ C8 microcontrollers บางตัวจะมี 128KB ไม่ใช่ 64KB ให้คิดว่าเป็น CB ตอนจะ Flash MicroPython (CB=128K, C8=64K)
วิธี Flash ทำได้หลายวิธี
- เข้าขา PA9/PA10 ใช้โปรแกรม stm32flash tool
- เข้าขา PA9/PA10 ใช้โปรแกรม STM32CubeProgrammer ***
- เข้าทาง USB ด้วย dfu-util
- เข้าทาง USB ด้วย DfuSe Demo ***
ปัญหาคือหา Bin Firmware MicroPython ให้สำหรับ BluePill ไม่ค่อยเจอซึ่งจะต้อง Compile เอง
อ่านวิธี Flash ด้วย ST-Link ได้ที่ https://www.artronshop.co.th/article/43/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89%E0%B8%87%E0%B8%B2%E0%B8%99-stm32-%E0%B8%A3%E0%B9%88%E0%B8%A7%E0%B8%A1%E0%B8%81%E0%B8%B1%E0%B8%9A-arduino-%E0%B9%80%E0%B8%9A%E0%B8%B7%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B8%95%E0%B9%89%E0%B8%99