在STM32F1片上 Flash 保存第一人稱 3D 射擊游戲 文章來源于RTThread物聯(lián)網(wǎng)操作系統(tǒng) ,作者wuhanstudio 每當(dāng)提到 3D 第一人稱射擊游戲,大家很自然就會(huì)聯(lián)想到高性能獨(dú)顯,以及幾個(gè)G甚至幾十G的硬盤存儲(chǔ)空間,那么有沒有可能在 STM32F103RC 這樣一塊只有 256KB Flash 的小板子上保存一個(gè)大型第一人稱射擊游戲呢? file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps29.png 當(dāng)我們用 USB 線把這塊開發(fā)板連接到電腦上,就可以看到出現(xiàn)了一個(gè)只有100多KB的U盤,這就是 STM32 的片上 Flash 了。 file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps30.png 然后可以看到U盤里有一個(gè) 90KB 的游戲。 file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps31.png 如果我們雙擊它就啟動(dòng)了一個(gè) STM32 上的第一人稱射擊游戲。 file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps32.png 這是完整的演示視頻: 如果你也有興趣把這個(gè)游戲保存在 STM32 上,可以跟著后面的教程,或者也可以在這個(gè)項(xiàng)目里直接找到編譯好的固件、游戲和項(xiàng)目源碼。 鏈接地址:https://github.com/wuhanstudio/stm32-ufun-3d-game RT-Thread 在 Github 上是開源的,源碼可以在這里(鏈接:https://github.com/rt-Thread/rt-thread/)找到,官網(wǎng)文檔也很豐富這里就不重復(fù)了,總結(jié)一下需要的工具: · Git Bash · Keil 開發(fā)環(huán)境 · STM32 開發(fā)板 · RT-Thread 源碼 · env 工具 如果覺得從 github 上面下載 RT-Thread 源碼比較慢,可以先從 gitee 上面下載,然后修改項(xiàng)目地址。 向 1git clone https://gitee.com/rtthread/rt-thread 2cd rt-thread 3git remote rm origin 4git remote add origin https://github.com/RT-Thread/rt-thread 5git pull origin master 準(zhǔn)備好第一步里面的工具后,首先我們需要把 STM32 的片上 flash 掛載為文件系統(tǒng),首先需要在 CubeMX 里使能 USB。 file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps33.png 打開 USB 后在時(shí)鐘配置里確認(rèn)外部晶振和板子上的晶振是一致的,并且 USB 的時(shí)鐘是 48MHz,就可以點(diǎn)擊生成代碼了。由于我們修改了時(shí)鐘,需要把 board/CubeMX_Config/Src/main.c 里面的 void SystemClock_Config(void) 函數(shù)復(fù)制到 board/board.c 里面替換。 接下來修改 bsp 下 board 目錄里的 Kconfig 文件。向 1config BSP_USING_ON_CHIP_FLASH 2 bool "Enable on-chip FLASH" 3 default n 4 5config BSP_USING_USBD_FS 6 bool "Enable USBD as USB device" 7 select RT_USING_USB_DEVICE 8 default n 這樣再在 bsp 目錄下右鍵 ConEmu Here 就可以看到配置界面 (Env 教程:https://www.rt-thread.org/document/site/tutorial/env-video/),依次選中下面幾個(gè)選項(xiàng): 1. Hardware Drivers Config --> Enable on-chip Flash file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps34.png 1. RT-thread Components --> Device Drivers --> Using MTD Nor Flash device drivers 2. file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps35.png 1. RT-Thread online packages --> system packages 選中 fal 和 Littlefs file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps36.png 1. RT-Thread Components --> Device virtual file system file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps37.png 保存配置退出,在 env 里面輸入命令就可以自動(dòng)生成 Keil 項(xiàng)目了: 1pkgs --update 2scons --target=mdk5 -s 我們需要先對(duì) flash 進(jìn)行規(guī)劃,STM32F103RC 一共有 256KB,可以把最后的 128KB 作為文件系統(tǒng)使用,于是新建一個(gè) fal_cfg.h:向 1extern const struct fal_flash_dev stm32_onchip_flash; 2 3#define FAL_FLASH_DEV_TABLE \ 4{ \ 5 &stm32_onchip_flash, \ 6} 7 8#define FAL_PART_TABLE \ 9{ \ 10 {FAL_PART_MAGIC_WROD, "app", "onchip_flash", 0, 128*1024, 0}, \ 11 {FAL_PART_MAGIC_WROD, "flash0", "onchip_flash", 128*1024, 128*1024, 0}, \ 12} 再修改 main.c 掛載文件系統(tǒng): 1#include 2#include 3#include 4#include 5#include 6 7/* defined the LED0 pin: PC13 */ 8#define LED0_PIN GET_PIN(C, 13) 9 10#define FS_PARTITION_NAME "flash0" 11 12int main(void) 13{ 14 int count = 1; 15 /* set LED0 pin mode to output */ 16 rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); 17 18 fal_init(); 19 20#ifdef RT_USB_DEVICE_MSTORAGE 21 struct rt_device *flash_dev = fal_blk_device_create(FS_PARTITION_NAME); 22#else 23 struct rt_device *flash_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME); 24#endif 25 26 if (flash_dev == NULL) 27 { 28 rt_kprintf("Can't create a block device on '%s' partition.\n", FS_PARTITION_NAME); 29 } 30 else 31 { 32 rt_kprintf("Create a block device on the %s partition of flash successful.\n", FS_PARTITION_NAME); 33 } 34 35 while (count++) 36 { 37#ifndef RT_USB_DEVICE_MSTORAGE 38 if(rt_device_find(FS_PARTITION_NAME) != RT_NULL) 39 { 40 dfs_mkfs("lfs", FS_PARTITION_NAME); 41 42 if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK) 43 { 44 rt_kprintf("sd card mount to '/'\n"); 45 break; 46 } 47 else 48 { 49 rt_kprintf("sd card mount to '/' failed!\n"); 50 } 51 } 52#endif 53 rt_pin_write(LED0_PIN, PIN_HIGH); 54 rt_thread_mdelay(500); 55 rt_pin_write(LED0_PIN, PIN_LOW); 56 rt_thread_mdelay(500); 57 } 58 59 return RT_EOK; 60} 項(xiàng)目編譯下載到板子上,串口連接上去在 msh 里輸入 fal probe 應(yīng)當(dāng)就可以看到分區(qū)表了: file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps38.png Linux 下常用的命令 ls ,cat,mkdir,cd 也都是支持的,這樣我們就把 stm32 的片上 flash 掛載為文件系統(tǒng)了, 下一步就是讓它識(shí)別為 U盤。 同樣的,我們?cè)?/font> env 的配置目錄里面依次選中: 1. Hardware Drivers Config --> On-chip Peripheral Drivers file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps39.png 1. RT-Thread Components --> Device Drivers --> Using USB file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps40.png file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps41.png 這樣保存配置,在 env 下生成項(xiàng)目文件: 1scons --target=mdk5 -s 編譯上傳到板子上,接上 USB 到電腦,例如 STM32F103 的 USB 引腳是 PA11 和 PA12,就可以看到電腦識(shí)別到一個(gè)新的 U盤了,第一次使用可能需要先格式化。 file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml6616\wps42.png 最后其實(shí)就只是把高壓的游戲復(fù)制進(jìn)去,就把它裝進(jìn) STM32 的片上 flash 了,其實(shí)還是挺簡(jiǎn)單的。完整的項(xiàng)目地址在這里:https://github.com/wuhanstudio/stm32-ufun-3d-game |