一、移植環境說明
(1)、win10系統
(2)、IAR(EWARM)7.7
(3)、STM32標准固件庫3.5.0 http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries/stsw-stm32054.html
二、移植步驟
(1)、首先建立一個C語言的main工程(或空工程),如下圖所示:
(2)、復制STM32固件庫到工程目錄下,並將固件庫文件添加到工程中,添加完文件后的工程目錄如下所示:
其中startup文件夾中的匯編文件要根據所使用的器件來選擇,Flash<=32K為小容量,64K<=Flash<=128K為中容量,Flash>=256為大容量:
startup_stm32f10x_cl.s 互聯型的器件,STM32F105xx,STM32F107xx
startup_stm32f10x_hd.s 大容量的STM32F101xx,STM32F102xx,STM32F103xx
startup_stm32f10x_hd_vl.s 大容量的STM32F100xx
startup_stm32f10x_ld.s 小容量的STM32F101xx,STM32F102xx,STM32F103xx
startup_stm32f10x_ld_vl.s 小容量的STM32F100xx
startup_stm32f10x_md.s 中容量的STM32F101xx,STM32F102xx,STM32F103xx
startup_stm32f10x_md_vl.s 中容量的STM32F100xx
startup_stm32f10x_xl.s FLASH在512K到1024K字節的STM32F101xx,STM32F102xx,STM32F103xx
(3)、添加頭文件的包含:
這個時候編譯會報錯:Fatal Error[Pe035]: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)" E:\project\IARProject\BaseVersion\Libraries\CMSIS\DeviceSupport\stm32f10x.h 96
在stm32f10x.h中把#define STM32F10X_MD和#define USE_STDPERIPH_DRIVER兩個宏打開再次編譯。
這個時候報錯:Error[Pe147]: declaration is incompatible with "__nounwind __interwork __softfp unsigned long __get_PSP(void)" (declared at line 58 of "D:\Program Files (x86)\IAR Systems\Embedded E:\project\IARProject\BaseVersion\Libraries\CMSIS\CoreSupport\core_cm3.h 1084 等一系列錯誤,這是由於core_cm3.h頭文件導致的,需要把固件庫中的core_cm3.h去掉(刪除或改名),用IAR自帶的core_cm3.h文件。並在工程選項的庫配置處選擇使用CMSIS,如下圖:
(4)、從標准模板工程中拷貝stm32f10x_conf.h到工程根目錄下,並把根目錄添加到頭文件搜索路徑中,再次編譯即可通過;但是會出現警告信息:
Warning[25]: Label 'Reset_Handler' is defined pubweak in a section implicitly declared root E:\project\IARProject\BaseVersion\Libraries\CMSIS\DeviceSupport\startup\startup_stm32f10x_md.s 124 等一系列警告。
需要將startup_stm32f10x_md.s文件中的SECTION.text:CODE:REORDER(1)改為SECTION .text:CODE:REORDER:NOROOT(1)。
詳情如下:
7.1版本之前,section默認是noroot,但現在是root。所以可以將SECTION .text:CODE:REORDER(1)改成SECTION .text:CODE:REORDER:NOROOT(1)或SECTION .text:CODE:REORDER:ROOT(1)。注意 NOROOT和 ROOT 的區別,NOROOT表示如果符號沒有被關聯的時候是被優化掉的,如果想不被優化則使用ROOT。
至此STM32固件庫在IAR上的移植已經完成。