# CircuitPython for STM32

## วิธีการติดตั้ง CircuitPython สำหรับ STM32

บอร์ดไมโครคอนโทรลเลอร์ที่ได้เลือกนำมาใช้งานได้แก่ **STM32F411CE** (**ARM Cortex-M4F**) **Black Pil**l สามารถติดตั้งเฟิร์มแวร์ **CircuitPython** โดยใช้วิธี **DFU** ผ่านทางพอร์ต **USB (USB Type-C)** และเป็นวิธีที่ทำได้ง่าย ไม่ต้องใช้อุปกรณ์เสริม เช่น **ST-Link Programmer** เป็นต้น

ถัดไปให้เตรียมอุปกรณ์ฮาร์ดแวร์และซอฟต์แวร์ดังนี้

* เชื่อมต่อขา **A10 (PA10**) ด้วยสาย **Jumper Wire** กับตัวต้านทาน เช่น **10k** แบบ **Pullup** ไปยัง **3.3V**
* เสียบสาย **USB-C** เชื่อมต่อบอร์ดไมโครคอนโทรลเลอร์กับคอมพิวเตอร์
* กดปุ่ม **BOOT0** กดค้างไว้ กดปุ่ม **RESET** แล้วจึงปล่อยปุ่ม **RESET** และ **BOOT0** ตามลำดับ เพื่อเข้าสู่ใหมด **DFU-USB**&#x20;
* ดาวน์โหลดและติดตั้งโปรแกรม [**STM32CubeProgrammer**](https://www.st.com/en/development-tools/stm32cubeprog.html) สำหรับ **Windows 10** หรือ **Linux** เป็นต้น
* ดาวน์โหลดไฟล์เฟิร์มแวร์ **CircuitPython (.bin)** สำหรับ **STM32F411CE:** [**https://circuitpython.org/board/stm32f411ce\_blackpill/**](https://circuitpython.org/board/stm32f411ce_blackpill/)

![รูปภาพ: บอร์ด STM32F411CE Black Pill](https://969412697-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIHfYo9IV3uTFm2tkDn%2F-MQ118ahnKo0Pn56IWXW%2F-MQ1Aqalgo-1NtkXJMnx%2Fstm32f411ceu_black_pill_dfu_usb.png?alt=media\&token=1cc1e50a-ce54-4901-bf23-af0a8fef2b52)

![รูปภาพ: STM32CubeProgrammer Download Page](https://969412697-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIHfYo9IV3uTFm2tkDn%2F-MQ0rnr4KcrW7E02dVeg%2F-MQ0xB3RSin3pmQu71aw%2Fstm32cubeprogrammer_download.png?alt=media\&token=0ccbe6c4-ec7c-437e-b601-c309edffd09d)

![รูปภาพ: แสดงสถานะเชื่อมต่อกับบอร์ดในโหมด DFU-USB ได้แล้ว](https://969412697-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIHfYo9IV3uTFm2tkDn%2F-MQ0rnr4KcrW7E02dVeg%2F-MQ0xMwv6yetBoD6Dxip%2Fstm32prog_dfu_connect.png?alt=media\&token=b8f39f8b-8d3e-494a-a7bf-fc22b1acff27)

![รูปภาพ: แสดงขั้นตอนการโปรแกรมไฟล์เฟิร์มแวร์ไปยังบอร์ด STM32F411CE](https://969412697-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIHfYo9IV3uTFm2tkDn%2F-MQ0rnr4KcrW7E02dVeg%2F-MQ0xZJNKLVr-DFMy83V%2Fstm32prog_dfu_download.png?alt=media\&token=8bb7dcf9-e3ee-4f7a-aa92-d362b8e0c535)

เมื่อทำขั้นตอนทั้งหมดแล้ว และรีเซตบอร์ด จะมองเห็นไดรฟ์ชื่อ `CIRCUITPY` สามารถใช้ **Thonny IDE** หรือ **Mu Editor** เชื่อมต่อผ่าน **REPL** ได้

บอร์ด **STM32F411CE Black Pill** ใช้ชิป **stm32F411ceu6** เป็นตัวประมวลผล มีหน่วยความจำ **SRAM 128KB** และ **Flash 512KB** และใช้ความเร็วได้ถึง **100MHz**&#x20;

ถ้าเปรียบเทียบกับ **SAMD21** จะเห็นว่า **STM32F411CE** มีความจุหน่วยความจำมากกว่า และทำงานด้วยความถี่ที่สูงกว่า

ให้ทดลองรันโค้ดต่อไปนี้ เพื่อดูข้อมูลเกี่ยวกับขนาดของหน่วยความจำที่เหลืออยู่ (**Free Memory**) และที่ถูกใช้ไป (**Allocated Memory**)&#x20;

```python
import gc

text = 'Memory Info\n- Free: {} bytes\n- Allocated: {} bytes'
values = (gc.mem_free(), gc.mem_alloc())
print( text.format(*values) )
```

ตัวอย่างข้อความเอาต์พุต (เมื่อรันสคริปต์โดยใช้ซอฟต์แวร์ **Thonny IDE**)

```python
Memory Info
- Free: 71520 bytes
- Allocated: 2208 bytes
```

หรือลองโค้ดตัวอย่างที่ทำให้ **LED** บนบอร์ดกระพริบ ซึ่งตรงกับขา **PC13**

```python
import time
import digitalio
import board
from microcontroller import pin


## use onboard LED (blue) / PC13
## 1) use board LED  pin or
#led = digitalio.DigitalInOut(board.LED)
## 2) use microcontroller pin
led = digitalio.DigitalInOut(pin.PC13)
## output direction for the LED pin
led.direction = digitalio.Direction.OUTPUT

try:
    while True:
        led.value = not led.value # toggle output
        time.sleep(0.1)
except KeyboardInterrupt:
    pass
led.deinit() # release the LEd pin
print('Done')
```

{% hint style="info" %}
**เผยแพร่ภายใต้ลิขสิทธิ์**\
**Attribution-ShareAlike 4.0 International (**[**CC BY-SA 4.0**](https://creativecommons.org/licenses/by-sa/4.0/)**)**
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://think-embedded.gitbook.io/micropython/circuitpython/circuitpython-for-stm32.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
