2024年9月28日 星期六

MicroPython 學習筆記 : ESP32-S3 開發板燒錄 v1.23 韌體

前幾天到貨的兩片 ESP32-S3 今天下午終於有時間拆開來測試, 此板規格參見賣家商品頁面 :


模組名稱為 ESP32-S3-WROOM-1-N16R8, 其中 N16 表示 Nand Flash 容量為 16MB, 而 R8 則表示 PSRAM 記憶體容量是 8MB, PSRAM 是 ESP32-S3 模組內的周邊擴充 SRAM 晶片, 當所執行的程式需要的記憶體超過內建的 512K SRAM 時就會自動透過 Octal SPI 介面調用 PSRAM 來擴充, 但這這需要支援 PSRAM 的韌體才能啟用. 

首先來燒錄不使用 PSRAM 的韌體. 請注意此板有兩個 Type C 插槽, 左邊那個是 OTG USB 插槽, 右邊那個才是連接 PC COM 埠用來燒錄韌體傳送檔案的, 不要插錯了 :


Source : 露天



板上有兩個按鈕, 上方是 RST 重置鈕用來重開機, 下方的是 BOOT 按鈕用來燒錄韌體用, 但實測發現現在的板子用 esptool 燒錄或某除韌體時都不需要再按下 BOOT 鈕 (可能是新版 esptool 會自動去控制). 另外板上還有一顆全彩 RGB LED (GPIO48). 





ESP32-S3 的韌體的下載頁面如下 :  


最上面的韌體是不支援 PSRAM 的, 按其中的 .bin 連結下載 :




或直接按下列網址 : 


將開發板 COM 埠 USB 連接 PC USB 槽, 從裝置管理員找出連接埠編號, 此處為 COM4, 先檢查 Flash 資訊 : 

D:\ESP32>esptool --port COM4 flash_id    
esptool.py v4.6.2
Serial port COM4
Connecting....
Detecting chip type... ESP32-S3
Chip is ESP32-S3 (revision v0.2)
Features: WiFi, BLE
Crystal is 40MHz
MAC: 3c:84:27:c9:67:1c
Uploading stub...
Running stub...
Stub running...
Manufacturer: 85
Device: 2018
Detected flash size: 16MB
Flash type set in eFuse: quad (4 data lines)
Hard resetting via RTS pin...

可見此開發板確實有 16MB Flash 記憶體 (硬碟).

然後將 Flash 裡面的韌體抹除, 注意, ESP32-S3 的指令與 ESP32 的不同, --chip 參數要改用 esp32s3 或 esp32-s3 : 

D:\ESP32>esptool --chip esp32s3 --port COM4 erase_flash   
esptool.py v4.6.2
Serial port COM4
Connecting...
Chip is ESP32-S3 (revision v0.2)
Features: WiFi, BLE
Crystal is 40MHz
MAC: 3c:84:27:c9:67:1c
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 29.8s
Hard resetting via RTS pin...

然後燒錄韌體 : 

D:\ESP32>esptool --chip esp32s3 --port COM4 write_flash -z 0 ESP32_GENERIC_S3-20240602-v1.23.0.bin   
esptool.py v4.6.2
Serial port COM4
Connecting....
Chip is ESP32-S3 (revision v0.2)
Features: WiFi, BLE
Crystal is 40MHz
MAC: 3c:84:27:c9:67:1c
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash will be erased from 0x00000000 to 0x0018dfff...
Compressed 1628144 bytes to 1067824...
Wrote 1628144 bytes (1067824 compressed) at 0x00000000 in 94.1 seconds (effective 138.4 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

重開機後上傳 xtools 函式庫測試傳 Line Notify 與查詢 ChatGPT 均正常 : 

MicroPython v1.23.0 on 2024-06-02; Generic ESP32S3 module with ESP32S3
Type "help()" for more information.

>>> import xtools    
import config
ip=xtools.connect_wifi(config.SSID, config.PASSWORD)
token=config.LINE_NOTIFY_TOKEN
openai_api_key=config.OPENAI_API_KEY
message='test'
prompt='Who are you'
image_url='https://cdn.pixabay.com/photo/2024/03/15/17/50/dogs-8635461_1280.jpg'   

network config: ('192.168.50.124', '255.255.255.0', '192.168.50.1', '192.168.50.1')
>>> xtools.line_msg(token, message)    
Message has been sent.
>>> xtools.line_sticker(token, message, 1, 4)     
'The sticker has been sent.'
>>> xtools.line_image_url(token, message, image_url)    
'The image URL has been sent.'
>>> xtools.ask_gpt(prompt, openai_api_key)    
'I am an AI language model created by OpenAI, designed to assist with a wide range of questions and tasks by providing information and generating text. How can I help you today?'
>>> prompt='你是誰?'    
>>> print(xtools.ask_gpt(prompt, openai_api_key))       
我是一个人工智能助手,旨在回答问题、提供信息和帮助您解决问题。有任何需要了解的内容或者想要讨论的话题吗?

此韌體是不支援 SPI PSRAM 的, 所以查詢記憶體顯示的是 SRAM 的大小 :

MicroPython v1.23.0 on 2024-06-02; Generic ESP32S3 module with ESP32S3
Type "help()" for more information.

>>> import gc   
>>> print(gc.mem_free())      
242576  

512 KB 的 SRAM 只剩下一半不到. 用 micropython.mem_info() 會顯示動態記憶體配置情形 :

>>> import micropython  
>>> micropython.mem_info()      
stack: 736 out of 15360
GC: total: 64000, used: 13008, free: 50992, max new split: 188416
 No. of 1-blocks: 299, 2-blocks: 39, max blk sz: 32, max free sz: 3175

此開發板若要用在大量計算 (例如 AI/ML 方面) 就需要用到 PSRAM, 因此接下來改燒錄支援 PSRAM 的韌體, 將下載頁面向下捲動到 Support for Octal-SPIRAM 項目下點選 .bin 即可下載支援 PSRAM 的韌體 : 



或按下列網址 : 


先將上面燒錄的原韌體抹除 : 

D:\ESP32>esptool --chip esp32s3 --port COM4 erase_flash   
esptool.py v4.6.2
Serial port COM4
Connecting....
Chip is ESP32-S3 (revision v0.2)
Features: WiFi, BLE
Crystal is 40MHz
MAC: 3c:84:27:c9:67:1c
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 29.3s
Hard resetting via RTS pin...

然後燒錄這個有支援 PSRAM 的韌體 : 

D:\ESP32>esptool --chip esp32s3 --port COM4 write_flash -z 0 ESP32_GENERIC_S3-SPIRAM_OCT-20240602-v1.23.0.bin   
esptool.py v4.6.2
Serial port COM4
Connecting...
Chip is ESP32-S3 (revision v0.2)
Features: WiFi, BLE
Crystal is 40MHz
MAC: 3c:84:27:c9:67:1c
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash will be erased from 0x00000000 to 0x0018efff...
Compressed 1631424 bytes to 1069649...
Wrote 1631424 bytes (1069649 compressed) at 0x00000000 in 94.2 seconds (effective 138.5 kbit/s)...
Hash of data verified.

檢查動態記憶體 : 

MicroPython v1.23.0 on 2024-06-02; Generic ESP32S3 module with Octal-SPIRAM with ESP32S3
Type "help()" for more information.
>>> import gc     
>>> print(gc.mem_free())       
8302160

總共有 8.3 MB, 其中 8MB 為 PSRAM. 用 micropython.mem_info() 顯示較詳細配置 : 

>>> import micropython    
>>> micropython.mem_info()      
stack: 736 out of 15360
GC: total: 64000, used: 23520, free: 40480, max new split: 8257536  
 No. of 1-blocks: 548, 2-blocks: 57, max blk sz: 32, max free sz: 2518

當然, 此支援 PSRAM 的韌體執行 Line Notify 傳訊息與詢問 OpenAI API 均可正常執行. 總之, 此 ESP32-S3 開發板既然內建 PSRAM, 當然要充分利用, 雖然 PSRAM 效能比不上 SRAM, 但不用也是浪費. 

沒有留言 :