本帖最后由 anobodykey 于 2018-5-15 14:03 编辑
' Q2 l! w) E2 a1 V
, e6 N2 ^5 z6 T k主机环境:Windows 7 SP1 编译器版本:gcc-arm-none-eabi-7-2017-q4-major-win32 目标板:STM32F103C8T6单板 STM32库版本:STM32Cube_FW_F1_V1.6.0 XMAKE版本:2.1.9 之前一段时间一直在思索跨平台开发的问题,以前开发STM32用的是MDK开发环境,但该软件又只能在Windows平台使用,跨平台的集成开发环境有SW4STM32(支持windows、macos、linux),TureStudio(支持windows、linux),以及Embeded Studio(支持windows、macos、linux),最后的选择就是gcc了。其实SW4STM32和TureStudio也都是基于gcc的,两者基本类似,而Embeded Studio之前用过几次版本更新的也挺快的,就是现在要申请license才能编译,懒得弄了,刚好借此来学习一下gcc,网上大多数gcc历程都是gcc+eclipse+makefile的组合。之前申请了一块NXP的54608板卡,在SDK中有提到CMake,就去网上了解了一下,是一个挺好的工具,同时又知晓了另一个构建工具,即今天的主题-XMake,网址如下:http://xmake.io,有兴趣的可以去了解一下,xmake与cmake的区别是它不是生成工程文件为主,而是直接去编译,采用lua语言,支持三大平台,目前我也只是去官网学习了一下,还是处于一知半解的状态,因此用它来构建一下STM32来学习一下xmake和gcc,可谓是一举两得。
V1 Q6 @8 `* O/ n首先是安装xmake以及gcc工具链,并把它们添加到PATH中,如下:
接下来是编辑好我们的源码文件,由于只是学习,只需要很简单的功能即可,这里就以串口为例,进行简单的输出,拷贝UART_TwoBoards_ComPolling工程,命名为UART_Xmake,组织如下:
其中启动文件以及链接文件是从SW4STM32示例中拷贝出来的,主程序如下: - /**& r( w+ z& j5 X) j1 K& i
- ******************************************************************************
6 P) r+ s: ?5 j7 e, r q" ~) B - * @file UART/UART_TwoBoards_ComPolling/Src/main.c # l- y1 i) q) Y- T1 Q
- * @author MCD Application Team
* A- X, S, A) D% H% C/ v# | - * @version V1.5.0
7 \9 h8 K2 F6 }0 ~1 z- K - * @date 14-April-2017
7 i- U2 S# X# O# O: B - * @brief This sample code shows how to use UART HAL API to transmit& Y2 b" J4 ^$ R
- * and receive a data buffer with a communication process based on
: V4 H5 `4 f8 K7 L: y - * polling transfer. 1 p1 `. N+ U# c$ D
- * The communication is done using 2 Boards.
$ Y, y4 |: G ~3 E - ******************************************************************************. G+ H ~$ \3 M2 y. i4 {( h& p
- * @attention
5 ~7 L& [8 O" ~' Q' `- i - * N' R5 H6 ^8 c+ A% n
- * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
: q7 k& e/ x, P+ ?# G( A2 L; T& k - *0 V: T4 V# q. t$ B, v
- * Redistribution and use in source and binary forms, with or without modification,# F3 U( [/ P/ S
- * are permitted provided that the following conditions are met:8 v5 {+ M0 T. l9 E
- * 1. Redistributions of source code must retain the above copyright notice,5 H, W1 i# `5 n. Q4 K2 \
- * this list of conditions and the following disclaimer.4 t6 y% P5 ^# Y) p/ g
- * 2. Redistributions in binary form must reproduce the above copyright notice,
0 a# {3 N5 i- }# [( e* ` - * this list of conditions and the following disclaimer in the documentation
5 c( J, B' k) |6 I: y - * and/or other materials provided with the distribution.
; l8 L9 C9 C0 G3 {2 \3 @7 s0 b - * 3. Neither the name of STMicroelectronics nor the names of its contributors- } P9 x7 m6 ^7 ?' P3 x
- * may be used to endorse or promote products derived from this software
" Y3 T- D$ d/ T1 V - * without specific prior written permission.
# \: W; v% X6 V1 }# q - ** h/ G, _) w9 o9 w! j7 x1 b5 U
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"; b% M- V$ @& b: X5 m
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE2 }3 L( M; K! b) `6 [( s
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2 W9 h e3 e( G7 o$ D* K* f- b7 \! E, K - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
5 {: ~8 E. j# B+ |) w* |/ |. J - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0 i3 {! @! c5 ^, Q; Z - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
8 B* r9 e# B3 E - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
9 h/ w: P! T7 m+ I6 G( X2 a0 e - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,- c* N8 d' K; B! g2 c3 I
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
D% i) U% J+ P1 U6 [2 P+ k - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- a9 h. @+ ~# h! V4 X5 @ - *9 h+ a* e! K2 Q5 M, i1 o3 t9 O
- ******************************************************************************
, o. B3 q3 m; e+ V - */
. K! D7 v2 N$ l9 L/ W6 x( p
# t W' k: P7 n9 c- /* Includes ------------------------------------------------------------------*/
* n/ S( ?; ^9 v+ ~0 ?' ^/ I - #include "main.h"1 d8 l1 k0 [' q, Z E j" U
3 y/ g8 k8 u! R- /** @addtogroup STM32F1xx_HAL_Examples
& \& O* B I: F - * @{- Y9 ?( H; w8 t# [, i
- */4 @# b( a+ C, J
4 R& Z0 a2 i# f6 g+ p. `0 @- /** @addtogroup UART_TwoBoards_ComPolling
9 l9 k; G e2 @ - * @{
' Y3 ^1 a* c1 {+ }/ [( I0 U0 H - */
# ?! H2 f. q) i$ a( l0 |8 V3 \ - & z/ d, {' _# B2 Q- W
- /* Private typedef -----------------------------------------------------------*/7 b, t$ D. s# J3 u% U- \
- /* Private define ------------------------------------------------------------*/8 ^) ^7 y$ N7 U& r$ s7 x
- //#define TRANSMITTER_BOARD4 V- g* b( p8 V2 p0 \5 K
' n+ i! Y$ N) q( U) f) m% l7 e1 c: o3 z- /* Private macro -------------------------------------------------------------*/) y: {; K/ O9 t/ f2 J. g3 n( A
- /* Private variables ---------------------------------------------------------*/
: D* D+ f7 n# |6 K9 E6 G7 t - /* UART handler declaration */$ ~3 s! j' j! |) M* k7 P
- UART_HandleTypeDef UartHandle;
# Q+ D# b8 Z p/ @) w; V8 Y - ! b B1 d( Q `; n, v7 p& w
- /* Buffer used for transmission */
0 o4 d5 Q5 J5 O3 `/ E) X - uint8_t aTxBuffer[] = " **** UART_Xmake sample ****";( F9 W+ c5 f; b
- 9 n9 {- V: J/ Y, m4 j
- /* Private function prototypes -----------------------------------------------*/
/ f2 ~+ x, F9 f% D, _0 G - void SystemClock_Config(void);6 J% z. J% t6 ~7 U/ @" _
- static void Error_Handler(void);
) l4 E3 F; T* A& |4 N: ?
1 u- }7 c9 _) y2 o- /* Private functions ---------------------------------------------------------*/
: r; D& \! N# p
0 O& \3 @0 d: \& [1 P4 o- /**
8 P7 ^1 B* v9 ]7 g% A - * @brief Main program. j0 F6 [6 W2 k8 E2 w6 p- n( {
- * @param None, z5 |( `3 F6 R0 s
- * @retval None1 z0 q) w1 D( d/ r' d. F
- */: }3 j$ C! L- g4 }, q
- int main(void)
5 ] ]2 ^6 j3 B6 F" M7 O& Y4 j - {
) Z+ _; [9 \( q" Y" ~! W - /* STM32F103xB HAL library initialization:
; m( P! g0 j9 n2 J; F3 H6 g - - Configure the Flash prefetch) l1 d0 b \; M6 h
- - Systick timer is configured by default as source of time base, but user 1 y9 \1 J v. `
- can eventually implement his proper time base source (a general purpose
5 x/ U+ ^1 M( { - timer for example or other time source), keeping in mind that Time base % Y/ x, T9 Y: o# l% W8 n/ n7 d
- duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 9 P9 T0 ?* d/ [
- handled in milliseconds basis.6 S. U8 y4 Q3 u. T! n! O
- - Set NVIC Group Priority to 4
_* L# v& [2 p3 Q - - Low Level Initialization. h% w) e7 B M y; k) ~; R
- */
( v. G4 m$ c0 r0 y1 j( | - HAL_Init();6 Y3 j/ m6 s9 e
- / @1 V& W9 M5 D$ W
- /* Configure the system clock to 64 MHz */
3 H/ E) U9 W. I5 p9 ]5 X% C - SystemClock_Config();0 r \+ s, Q/ B% E! M
3 B' W! p" a: E: @ m( J- /*##-1- Configure the UART peripheral ######################################*/: l' h3 N. o& P) {- S1 H+ m3 ]
- /* Put the USART peripheral in the Asynchronous mode (UART Mode) */) O) P& p( s9 d' B( W
- /* UART configured as follows:" S$ n: K' m4 u9 M* c/ @/ c
- - Word Length = 8 Bits
( `% j$ N$ y* l- p, \ - - Stop Bit = One Stop bit
% B6 h" {. u, }& O* F - - Parity = None) X% x3 c/ d3 o4 S, Z
- - BaudRate = 9600 baud+ b: _" ^0 I4 k& z9 W3 i0 |
- - Hardware flow control disabled (RTS and CTS signals) */
4 q' u$ Y* V; {5 {4 c - UartHandle.Instance = USARTx;
. w# e- n* n! \/ O; v' |5 x. W( l
# p9 q) R7 m4 W0 y5 }& B4 u: D9 V0 ?- UartHandle.Init.BaudRate = 9600;
4 s, G- U9 o* O2 N- ~ - UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
& M, u5 Z: H( w, G* R3 G - UartHandle.Init.StopBits = UART_STOPBITS_1;9 @, O) o2 K* M1 D8 i b
- UartHandle.Init.Parity = UART_PARITY_NONE;5 ]; P; |4 L7 |! q1 Q% E( p
- UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
2 E3 P5 x" Y% S% b R - UartHandle.Init.Mode = UART_MODE_TX_RX;
2 j# ]+ P) h- C: w- p - if(HAL_UART_DeInit(&UartHandle) != HAL_OK)3 ^& v5 K& n" a9 _2 a' W3 L
- {' k/ \! {4 g( a% f! I9 e+ \
- Error_Handler();& s8 O( C8 } D' @" r4 S. ?
- }
# C! N- x0 c6 G- t - if(HAL_UART_Init(&UartHandle) != HAL_OK)+ _, i& ] d6 w( X/ s5 c
- {
: z7 k: b- _. P" u+ t- f - Error_Handler();
; U; G) p$ V# j7 u - }5 S, G) E6 S3 m
- 8 z" V5 j/ a; c2 h- x9 B
- /*##-2- Start the transmission process #####################################*/
5 }1 {& G `- Q; k N0 S! p - /* While the UART in reception process, user can transmit data through
( k2 C/ w/ x) Y& z2 v! O1 D - "aTxBuffer" buffer */
p: y/ N, e4 @! i& Y - if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)' N Q8 r; ~" C
- {6 o1 _' Z% p2 ]
- Error_Handler();
4 \# ^$ |$ Z9 s( ^ - }
$ |7 O) u% [! k$ _% G - /* Infinite loop */
0 {0 {! f. a- a/ [4 {, l0 `& G - while (1): t, x, j+ |2 p% f% J1 W" G2 _" W
- {
/ v5 w; }1 z* @* E# o - }
2 j% O; \( ?* H& y0 N - }( R) F: l8 x, ~2 g. J
1 W# \; G% ^$ ?% k& J* U- /**
5 V. T) T. J1 t - * @brief System Clock Configuration0 U- X7 D# E5 C* E, B
- * The system Clock is configured as follow :
: y. \/ S# J: v/ c/ J - * System Clock source = PLL (HSI)
9 o+ Q0 N+ a! u( t1 K2 U9 w4 ~, K5 c - * SYSCLK(Hz) = 64000000
; S" e) s# J' t5 B+ e# z0 S - * HCLK(Hz) = 64000000
! e+ s- K. ]3 H - * AHB Prescaler = 1
' k. C7 ]* z4 b* ^" C - * APB1 Prescaler = 2
7 h! K9 |4 ^! S; y( e9 Z - * APB2 Prescaler = 1
) H2 t9 T b! W8 w' c6 K' \ - * PLLMUL = 16
\0 N, d8 K) _0 L# Z - * Flash Latency(WS) = 2
* s! w2 L4 t1 n+ @4 _/ N - * @param None9 B, ~5 a/ ]9 E E
- * @retval None" N# y* S8 k/ q
- */
, g! {% G; k0 S - void SystemClock_Config(void)! H7 r* I' @+ g% m, a) N" m
- {0 @1 _+ Q' r) d3 N8 h
- RCC_ClkInitTypeDef clkinitstruct = {0};2 |' t) f/ a6 B
- RCC_OscInitTypeDef oscinitstruct = {0};
4 e5 p# h& w2 E: V5 O -
1 |; C( |) I( @4 @/ j; P - /* Configure PLL ------------------------------------------------------*/
2 z( j: S1 r6 Q) s8 r - /* PLL configuration: PLLCLK = (HSI / 2) * PLLMUL = (8 / 2) * 16 = 64 MHz */& \! y7 b& T; f, a, t
- /* PREDIV1 configuration: PREDIV1CLK = PLLCLK / HSEPredivValue = 64 / 1 = 64 MHz */. S, @; j1 E+ c& f) R
- /* Enable HSI and activate PLL with HSi_DIV2 as source */5 ]7 t& K- r" D9 i- O' _/ T
- oscinitstruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
2 L9 x# b9 _$ [$ Y8 x - oscinitstruct.HSEState = RCC_HSE_OFF;( l+ _3 y5 Z' U
- oscinitstruct.LSEState = RCC_LSE_OFF;/ N* b0 t1 `9 g& n5 K7 u) l. @
- oscinitstruct.HSIState = RCC_HSI_ON;
. l! f: M8 e3 @2 l3 I; l' S1 p: P - oscinitstruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
+ E: M3 P1 V/ [5 | - oscinitstruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;: E( T* S& F0 p ]& J" Z
- oscinitstruct.PLL.PLLState = RCC_PLL_ON;
5 D1 b# n2 X( v9 |" [ - oscinitstruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
' G& z! m9 J9 x1 N* _$ O - oscinitstruct.PLL.PLLMUL = RCC_PLL_MUL16;/ Z' ]# _0 G% S0 t% n
- if (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)4 T' | [8 r1 D
- {, }+ }3 L: ^! |5 i" t
- /* Initialization Error */- n$ p6 l F9 U4 ^
- while(1); 5 u, d& ?9 m" b% M0 K
- }
- p+ |! ]- M, Z8 l r* [
# E! B2 x2 p/ S% n( {) o- /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 " J1 ?3 _2 }! }5 V, R1 d
- clocks dividers */
* J B' `; f! [$ J% |5 h6 Z6 Q$ d - clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);8 u+ A9 o( t; m% N; v+ ^% c( n
- clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
/ ?% a: O9 R, ^( p - clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
* t+ M. d" Q1 Y1 J4 Z - clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;2 E! a) @; D# i1 _: F2 b
- clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2; # x* N5 p) K8 h: m; e) \& z# V
- if (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2)!= HAL_OK)2 t% a) L& X& H
- {
3 n( I: B7 N/ C) |3 b# F' N - /* Initialization Error */* j: X3 O& h' g7 N
- while(1);
# t3 r s1 G9 r5 B: |. ? - }2 C$ d) g, F6 O* s9 Y7 ~
- }
( |" p Z( R N. y, z7 X - , a4 t% ?, f. }) t: h; j* q
- /**
' t# T. {# P$ n3 h* e: n8 z; u2 B% M - * @brief UART error callbacks* V- Q* h4 @6 a1 B. N: K
- * @param UartHandle: UART handle( E) W- v1 L9 D1 ~% j2 @
- * @note This example shows a simple way to report transfer error, and you can
# W: H/ W7 L1 w9 [ - * add your own implementation.: ^: \0 a( ]4 W% d0 U) m1 v* n
- * @retval None
5 F9 X: j0 O' L5 Z& q U# J - */
5 v' k) ^# h% z0 S* | - void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)9 m# [0 {# L. T$ ^
- {; o+ x' C( j) ]; a% D
- Error_Handler();
: ?5 H! }1 m2 H3 z; e' t" u - }8 U- ^- ^( I+ p
g; A n b, i4 ~- /**9 t6 G+ a3 y7 t* w" h
- * @brief This function is executed in case of error occurrence.
: b9 L: C) K2 o8 [, L2 P+ ~+ K, d - * @param None
3 @2 r. \: z4 H/ N/ @ - * @retval None6 ]3 E8 z- S+ B3 j4 _: Q( P
- */
: B0 E n, `8 L+ W* n7 R7 l - static void Error_Handler(void)
8 N1 ~* J7 U: N' h - {9 z5 e3 n+ B/ Y* |" f) q
- while(1)
- G% x$ t6 v; l9 {! e, y o - {
- u" A m2 I9 i8 M1 ?$ ]) v( m' i( } - HAL_Delay(1000);
( l9 ?, M8 d; h, K& r - }' x% b+ i) q Y% s0 ]+ y$ e
- }
9 V8 L+ _ Q$ T" j/ I, P# W - & T) e+ z' s2 g4 K2 f' z/ M4 O9 ~
- #ifdef USE_FULL_ASSERT* C# I5 h$ {& u! K {1 A6 r; C
- ; }8 Q) r. ~, m. D# c
- /**
/ p$ u) d5 r5 o* ~' M* r - * @brief Reports the name of the source file and the source line number
4 A7 r# i1 d# M7 N: L - * where the assert_param error has occurred.: s- x" g, m+ |2 R. g1 D1 k8 m
- * @param file: pointer to the source file name) e1 ?1 D. a1 x. H! Z4 n/ i
- * @param line: assert_param error line source number
0 U% p5 |' H2 j2 h, t4 { _ - * @retval None; X, y; [) s% G" C. `
- */& c8 O/ S- w; d
- void assert_failed(uint8_t* file, uint32_t line)% p) G+ T8 y, Z% ~7 I0 e! z! S/ f
- { . W$ c2 S# I9 B( G! ?
- /* User can add his own implementation to report the file name and line number,
9 g6 Z7 r/ @! J. E - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
) Q! I7 S% p' y0 X0 f" o
. t: E9 Z( e, |- A- /* Infinite loop */
. h3 G/ W0 F4 f. r: w. z0 h% S) O - while (1)
7 w5 Q, x; \ K) s/ O - {
' H f; |/ E: x( t# C - }& O$ J( T" x2 J0 f: J# v5 n/ G
- }
, r: }$ h9 ` Z, B0 |, E) [# m - #endif! G* M3 j5 ~/ }$ c, m
# a6 r6 ?4 [, @! w1 k1 [" \
# n# Q5 C) K. @: D' ]7 V; B- /**
% i% Q& _$ k7 @5 v - * @}
2 ~0 Q. B" l3 G: e5 |* d - */
- u. m ^9 T% r0 ]5 } - 2 g% e# s+ \) ?" H O. M! S
- /**1 _' m& ^5 F% n4 ]: {4 [
- * @}
# n N) J' N! A; Q - */
- P$ }) F* H& [7 d - 4 h V& }/ i0 F, y9 A7 C, D
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
: d7 |, N; I! X) v. c! K* o
复制代码打开Powershell进入到该目录中,如下:
xmake构建程序需要一个名为xmake.lua的文件,该文件可以自动生成,输入xmake后会提示你找不到xmake.lua文件,输入y来生成该文件,或是从xmake安装目录下拷贝一个示例的xmake.lua文件,两种方式都可以。我使用的第一种方式,有了xmake.lua文件后我们就可以在上面添加一下配置信息来构建我们的STM32程序,关于xmake.lua的信息可以去官网上查看手册来了解它. 6 s6 { N! {' H# D* ~# L
此外,我们还需要对xmake做一些配置,xmake是支持多平台的构建,包括windows、linux、macos、cross、android等,这里我们需要配置为cross即跨平台,输入xmake f --menu会弹出配置对话框,如下:
进入第一项Basic Configuration,配置平台、体系架构、以及目标类型,如下:
这里我们需要配置前6项,前5项配置很容易,做一些选择即可,接下来配置第6项,如下:
设置工具链前缀以及目录,后面还需要配置编译器以及连接器,如下:
配置完后保存退出,会发现当前目录下多了一个.xmake文件夹
其中xmake.conf就包含了刚才我们的配置信息:
其他文件的信息,可以多去官网查看,.xmake文件夹下的文件是xmake自动生成的,直接修改配置文件是不行的。 # z. T' q+ s( B' G
接下来就是更改我们的xmake.lua文件,该文件主要是添加源文件,设置头文件搜索路径、添加宏定义、以及设置编译和链接选项,该文件内容如下: - -- the debug mode
# M+ {; j3 {; z' B; g+ S5 C' F - if is_mode("debug") then3 k. c7 L4 M( z. }4 l
- 7 n! ~% F' M$ S4 i" z0 s# e% C
- -- enable the debug symbols( @% y6 P3 C* h$ j
- set_symbols("debug")* T* ?; _4 }- k$ p5 F
- ' ?+ O: y! u: h' a
- -- disable optimization
! D( r/ s. I; V! ?" | - set_optimize("none")
# R5 {, H# p* ?. C. [: Q. k* S5 E - end! E3 m I5 g" \/ T X
- . X) D2 e* H6 x3 F3 P9 N
- -- the release mode2 N# b/ _2 j) v- O4 N
- if is_mode("release") then
& R5 ^( O3 n$ T3 v" D - 6 m/ }- s$ L% K, c5 y" g- B
- -- set the symbols visibility: hidden
6 G4 A P6 {$ N( _/ V" A5 c& I7 b - set_symbols("hidden")
8 L: \- V3 B P. i+ I' c* Y0 x1 ] - + I% Y6 B3 @% q8 M9 f$ ~
- -- enable fastest optimization
$ m7 [7 N; d: T+ N- `! X/ {2 g - set_optimize("fastest")2 }, O: N& Z3 |6 i& g
- # w/ c" u1 @! s
- -- strip all symbols: @$ Z% e* U2 d. E
- set_strip("all")
' z. c# L3 e6 m0 t - end* n5 a) G. f' x9 t- }0 n
- -- define target. |% {. V$ @: V/ d/ k* Q9 x- j
- target("UART_Xmake.elf")
* G" c% N+ F4 J0 e# ?- y
, i4 T" z. I8 e7 b' x% y$ D- -- set kind
, F5 p9 N% b+ n8 z- W9 c - set_kind("binary")
5 m) z# e0 A6 o) V5 H q9 \ - . B" [- i* v) S& J% a' v' |
- -- add files: z( @3 k( e g$ X7 |; S' @) ^
- add_files("startup_stm32f103xb.s")( \% z. s2 i! ]2 s2 |/ O9 ?
- add_files("Src/*.c")
1 p. E, ?- j8 k( j - add_files("../../../../../Drivers/BSP/STM32F1xx_Nucleo/stm32f1xx_nucleo.c") `7 ^# y4 s/ R
- add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c")
/ { Y6 U# p6 x* t. P - add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c")3 [& Y$ A9 Y: U* P# v
- add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c")
1 `$ B' l: M2 w* }0 a - add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c")# E0 X8 f; u/ N3 x( b- J, M$ }
- add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c")
" W( V! U$ Y8 L$ B9 s7 X0 K - add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c")
1 Y# ]8 T0 Z& L L* G - add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c")4 u9 t0 V! X. o) \7 M4 M+ ]/ o
- add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c")0 }# g, H/ `0 T
- * }* i# y+ L- b0 o# {& H& x" ?
- --add include search directories$ `8 ] Z" A6 i' l; z S
- add_includedirs("Inc")
1 K$ e* f* A) O4 s4 A - add_includedirs("../../../../../Drivers/CMSIS/Device/ST/STM32F1xx/Include")# s6 a$ i, ~ m% V
- add_includedirs("../../../../../Drivers/CMSIS/Include")/ o* H6 m. ^7 m n, N$ A! g
- add_includedirs("../../../../../Drivers/STM32F1xx_HAL_Driver/Inc")% v( P2 `7 c& v
- add_includedirs("../../../../../Drivers/BSP/STM32F1xx_Nucleo")/ M( a y4 \9 c5 ~
-
" z+ f+ p) U# D9 ]/ f1 L - --add macro defination
~' ?4 H6 ^- [8 Z3 o - add_defines("USE_HAL_DRIVER","STM32F103xB","USE_STM32F1xx_NUCLEO")" l1 f* h* H: A$ Q% F
- : B- Q- U: ^4 s$ w- Y1 r2 s
- -- set warning all as error/ s% A. n V8 V/ u. R
- set_warnings("all", "error")6 {( Z; u4 u& h3 S& I- W( b6 @9 b% o
- , P% _+ m* X4 R* c2 R
- -- set language: c997 ~' f/ j( C3 @3 h
- set_languages("c99"). Q5 `+ u) T# D/ T
-
7 y2 T5 s: }/ ? A* O* _( W - add_cxflags("-mcpu=cortex-m3 -mthumb -mfloat-abi=soft --specs=nano.specs -ffunction-sections -fdata-sections")7 f6 O# j+ i' l" `5 I
- add_ldflags("-mcpu=cortex-m3 -mthumb -mfloat-abi=soft --specs=nosys.specs --specs=nano.specs -T"STM32F103RBTx_FLASH.ld" -Wl,--gc-sections")/ [6 x5 H1 o, U0 h
- after_build(function(target) N: s" W0 ]0 @) E E) o A: u
- os.exec("arm-none-eabi-objcopy -O binary .\\build\\UART_Xmake.elf .\\build\\UART_Xmake.bin"): p7 ?! g" E1 n: K8 P
- end)
[# `- J, b1 B - -- FAQ+ ^; D% i. I- X5 Z
- --' k$ P9 ]4 E1 g2 b$ }
- -- You can enter the project directory firstly before building project.' a; M) t8 \* E0 b( \$ K& D
- -- ' T7 d) }6 d+ r5 E
- -- $ cd projectdir2 k9 a$ U- j6 R5 Y0 ?
- -- 8 x, p+ W5 D/ P6 i! z
- -- 1. How to build project?" `& D' K1 [ z( x8 r# r7 \- R
- --
$ f) h) w8 I# D" Q* P - -- $ xmake
; }5 G8 T$ ^7 G& n3 o - --* z& H V3 D* o* {
- -- 2. How to configure project?' l! B7 t. i1 U
- --3 g+ p9 n% z/ n- `# e6 v
- -- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]6 Y8 K0 W- |& j8 ^8 I6 R. x
- --1 a( f1 c$ g I1 d' e/ }) H3 L7 s
- -- 3. Where is the build output directory?# ~1 F6 u' ~) a4 M5 Y2 S5 U
- --6 N2 B* {3 y4 d' K7 ^& R
- -- The default output directory is `./build` and you can configure the output directory.
) l2 @! `4 t% O, G/ v8 c) j - --/ Y' P$ g6 |( x! C* W: \+ S+ z
- -- $ xmake f -o outputdir, t' F! @0 E) J3 ~4 O
- -- $ xmake
9 M- Y: h* \9 e6 P3 w# n - --) d" A0 M3 y3 s$ [/ d
- -- 4. How to run and debug target after building project?% r7 U/ c" U- R# E4 T0 Z
- --" M V. Y9 f6 k3 E4 A
- -- $ xmake run [targetname]
6 m' B7 S0 ?+ o; e$ ]; m* q; W - -- $ xmake run -d [targetname]
2 a% X) P' M% {+ S2 T* L - --
: J1 A1 R) X$ R9 ~; h2 W" q8 O - -- 5. How to install target to the system directory or other output directory?4 ?6 k, p% G9 q- Y2 n/ C: K$ {
- --
: e" Q' Z1 @7 T6 u, |# y - -- $ xmake install 1 B' w! k1 U: B* |6 t
- -- $ xmake install -o installdir D8 D3 F7 d$ _% G2 p* v
- --
+ f; ~- M8 ?8 f& g: w9 v - -- 6. Add some frequently-used compilation flags in xmake.lua
# P) L `& d [8 y - --* I! o* g+ I2 g4 w$ Y
- -- @code
& G% [# g( @2 J2 _1 D - -- -- add macro defination8 q2 O8 L; X: Z; v2 v
- -- add_defines("NDEBUG", "_GNU_SOURCE=1")
2 N0 L3 m- a+ `! R: s1 L - --
( b! Y# Q: d7 {7 Q& n- F - -- -- set warning all as error
$ F+ l$ @' m3 P, K9 U$ a5 v1 |4 z* e - -- set_warnings("all", "error")/ D6 a% M3 I6 M8 ]6 O
- --( L3 f4 m) x9 z; x4 h6 ^9 }% J
- -- -- set language: c99, c++11
" {) H) B) w! X! p6 i; h/ ` - -- set_languages("c99", "cxx11")% C, y. W, j. |0 \ ?
- --1 z6 l' v3 }$ U
- -- -- set optimization: none, faster, fastest, smallest
5 g* h2 G; i; E# K. B# u# L3 Y - -- set_optimize("fastest")
3 F' `( u: s. d7 ^: p8 Z2 i% D3 ^& _ c - --
O, i/ s6 v4 u6 [( E - -- -- add include search directories
5 P9 Y8 m6 f5 [" F- P8 B& l - -- add_includedirs("/usr/include", "/usr/local/include")8 U# A5 J. Y& L' K% a9 V* \
- --3 p' M \/ S, ^# t
- -- -- add link libraries and search directories
! \! }# q2 Q2 h- V2 e - -- add_links("tbox", "z", "pthread")
6 _' p$ x/ W: Q' `* H3 o - -- add_linkdirs("/usr/local/lib", "/usr/lib")- u$ ^. e- J% a
- --
# S( m' v; B* p8 O4 g - -- -- add compilation and link flags+ j- ?7 S/ p$ A( [8 H! u4 z
- -- add_cxflags("-stdnolib", "-fno-strict-aliasing"), {9 [. k6 m; m; K4 Q5 h
- -- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})4 \8 |3 m/ S- |/ s# J* y
- --, b3 v0 S( E3 a
- -- @endcode% \& r3 N7 q% t. z* ~' h# I) ?
- --
+ D' L ?% N# \* w& x# T5 ^ I) c( C5 A - -- 7. If you want to known more usage about xmake, please see http://xmake.io/#/home* c8 k D* G8 ~1 c5 U% \, ^! W" |
- --
* f& N4 C& |0 p) s$ l. q -
" J5 c# ^' D; Y
复制代码至此,xmake配置就算完成了,在当前目录下输入xmake会自动编译该工程,最后会在build目录下生成UART_Xmake.bin文件,烧录该文件到目标板中运行,结果如下:
好了,使用xmake来构建STM32程序的示例就完成了。 3 g9 F! V; ^7 }2 _
! q) X# u6 j% X/ D P0 l, D8 {1 Q" C A* t; s
0 n' a( e0 w' X$ U1 O+ q9 J9 U
|
不需要的,除非你要扩展插件之类的
[00%]: compiling.release Src\system_stm32f1xx.c
[20%]: compiling.release Src\stm32f1xx_hal_msp.c
[40%]: compiling.release Src\stm32f1xx_it.c( W. f6 p/ w6 ^
[60%]: compiling.release Src\main.c" `# x6 |- ~3 L; G, V, X6 ^
error: arm-none-eabi-gcc: error: unrecognized command line option '-Qunused-arguments'
看名字是提示你命令行参数无法识别,你把这个参数去掉再重新编译下