你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

使用xmake构建STM32程序

[复制链接]
anobodykey 发布时间:2018-5-6 15:24
本帖最后由 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中,如下:

1.JPG

接下来是编辑好我们的源码文件,由于只是学习,只需要很简单的功能即可,这里就以串口为例,进行简单的输出,拷贝UART_TwoBoards_ComPolling工程,命名为UART_Xmake,组织如下:

2.JPG

其中启动文件以及链接文件是从SW4STM32示例中拷贝出来的,主程序如下:

  1. /**& r( w+ z& j5 X) j1 K& i
  2.   ******************************************************************************
    6 P) r+ s: ?5 j7 e, r  q" ~) B
  3.   * @file    UART/UART_TwoBoards_ComPolling/Src/main.c # l- y1 i) q) Y- T1 Q
  4.   * @author  MCD Application Team
    * A- X, S, A) D% H% C/ v# |
  5.   * @version V1.5.0
    7 \9 h8 K2 F6 }0 ~1 z- K
  6.   * @date    14-April-2017
    7 i- U2 S# X# O# O: B
  7.   * @brief   This sample code shows how to use UART HAL API to transmit& Y2 b" J4 ^$ R
  8.   *          and receive a data buffer with a communication process based on
    : V4 H5 `4 f8 K7 L: y
  9.   *          polling transfer. 1 p1 `. N+ U# c$ D
  10.   *          The communication is done using 2 Boards.
    $ Y, y4 |: G  ~3 E
  11.   ******************************************************************************. G+ H  ~$ \3 M2 y. i4 {( h& p
  12.   * @attention
    5 ~7 L& [8 O" ~' Q' `- i
  13.   *  N' R5 H6 ^8 c+ A% n
  14.   * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
    : q7 k& e/ x, P+ ?# G( A2 L; T& k
  15.   *0 V: T4 V# q. t$ B, v
  16.   * Redistribution and use in source and binary forms, with or without modification,# F3 U( [/ P/ S
  17.   * are permitted provided that the following conditions are met:8 v5 {+ M0 T. l9 E
  18.   *   1. Redistributions of source code must retain the above copyright notice,5 H, W1 i# `5 n. Q4 K2 \
  19.   *      this list of conditions and the following disclaimer.4 t6 y% P5 ^# Y) p/ g
  20.   *   2. Redistributions in binary form must reproduce the above copyright notice,
    0 a# {3 N5 i- }# [( e* `
  21.   *      this list of conditions and the following disclaimer in the documentation
    5 c( J, B' k) |6 I: y
  22.   *      and/or other materials provided with the distribution.
    ; l8 L9 C9 C0 G3 {2 \3 @7 s0 b
  23.   *   3. Neither the name of STMicroelectronics nor the names of its contributors- }  P9 x7 m6 ^7 ?' P3 x
  24.   *      may be used to endorse or promote products derived from this software
    " Y3 T- D$ d/ T1 V
  25.   *      without specific prior written permission.
    # \: W; v% X6 V1 }# q
  26.   ** h/ G, _) w9 o9 w! j7 x1 b5 U
  27.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"; b% M- V$ @& b: X5 m
  28.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE2 }3 L( M; K! b) `6 [( s
  29.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    2 W9 h  e3 e( G7 o$ D* K* f- b7 \! E, K
  30.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    5 {: ~8 E. j# B+ |) w* |/ |. J
  31.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    0 i3 {! @! c5 ^, Q; Z
  32.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    8 B* r9 e# B3 E
  33.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    9 h/ w: P! T7 m+ I6 G( X2 a0 e
  34.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,- c* N8 d' K; B! g2 c3 I
  35.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      D% i) U% J+ P1 U6 [2 P+ k
  36.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    - a9 h. @+ ~# h! V4 X5 @
  37.   *9 h+ a* e! K2 Q5 M, i1 o3 t9 O
  38.   ******************************************************************************
    , o. B3 q3 m; e+ V
  39.   */
    . K! D7 v2 N$ l9 L/ W6 x( p

  40. # t  W' k: P7 n9 c
  41. /* Includes ------------------------------------------------------------------*/
    * n/ S( ?; ^9 v+ ~0 ?' ^/ I
  42. #include "main.h"1 d8 l1 k0 [' q, Z  E  j" U

  43. 3 y/ g8 k8 u! R
  44. /** @addtogroup STM32F1xx_HAL_Examples
    & \& O* B  I: F
  45.   * @{- Y9 ?( H; w8 t# [, i
  46.   */4 @# b( a+ C, J

  47. 4 R& Z0 a2 i# f6 g+ p. `0 @
  48. /** @addtogroup UART_TwoBoards_ComPolling
    9 l9 k; G  e2 @
  49.   * @{
    ' Y3 ^1 a* c1 {+ }/ [( I0 U0 H
  50.   */
    # ?! H2 f. q) i$ a( l0 |8 V3 \
  51. & z/ d, {' _# B2 Q- W
  52. /* Private typedef -----------------------------------------------------------*/7 b, t$ D. s# J3 u% U- \
  53. /* Private define ------------------------------------------------------------*/8 ^) ^7 y$ N7 U& r$ s7 x
  54. //#define TRANSMITTER_BOARD4 V- g* b( p8 V2 p0 \5 K

  55. ' n+ i! Y$ N) q( U) f) m% l7 e1 c: o3 z
  56. /* Private macro -------------------------------------------------------------*/) y: {; K/ O9 t/ f2 J. g3 n( A
  57. /* Private variables ---------------------------------------------------------*/
    : D* D+ f7 n# |6 K9 E6 G7 t
  58. /* UART handler declaration */$ ~3 s! j' j! |) M* k7 P
  59. UART_HandleTypeDef UartHandle;
    # Q+ D# b8 Z  p/ @) w; V8 Y
  60. ! b  B1 d( Q  `; n, v7 p& w
  61. /* Buffer used for transmission */
    0 o4 d5 Q5 J5 O3 `/ E) X
  62. uint8_t aTxBuffer[] = " **** UART_Xmake sample ****";( F9 W+ c5 f; b
  63. 9 n9 {- V: J/ Y, m4 j
  64. /* Private function prototypes -----------------------------------------------*/
    / f2 ~+ x, F9 f% D, _0 G
  65. void SystemClock_Config(void);6 J% z. J% t6 ~7 U/ @" _
  66. static void Error_Handler(void);
    ) l4 E3 F; T* A& |4 N: ?

  67. 1 u- }7 c9 _) y2 o
  68. /* Private functions ---------------------------------------------------------*/
    : r; D& \! N# p

  69. 0 O& \3 @0 d: \& [1 P4 o
  70. /**
    8 P7 ^1 B* v9 ]7 g% A
  71.   * @brief  Main program. j0 F6 [6 W2 k8 E2 w6 p- n( {
  72.   * @param  None, z5 |( `3 F6 R0 s
  73.   * @retval None1 z0 q) w1 D( d/ r' d. F
  74.   */: }3 j$ C! L- g4 }, q
  75. int main(void)
    5 ]  ]2 ^6 j3 B6 F" M7 O& Y4 j
  76. {
    ) Z+ _; [9 \( q" Y" ~! W
  77.   /* STM32F103xB HAL library initialization:
    ; m( P! g0 j9 n2 J; F3 H6 g
  78.        - Configure the Flash prefetch) l1 d0 b  \; M6 h
  79.        - Systick timer is configured by default as source of time base, but user 1 y9 \1 J  v. `
  80.          can eventually implement his proper time base source (a general purpose
    5 x/ U+ ^1 M( {
  81.          timer for example or other time source), keeping in mind that Time base % Y/ x, T9 Y: o# l% W8 n/ n7 d
  82.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 9 P9 T0 ?* d/ [
  83.          handled in milliseconds basis.6 S. U8 y4 Q3 u. T! n! O
  84.        - Set NVIC Group Priority to 4
      _* L# v& [2 p3 Q
  85.        - Low Level Initialization. h% w) e7 B  M  y; k) ~; R
  86.      */
    ( v. G4 m$ c0 r0 y1 j( |
  87.   HAL_Init();6 Y3 j/ m6 s9 e
  88. / @1 V& W9 M5 D$ W
  89.   /* Configure the system clock to 64 MHz */
    3 H/ E) U9 W. I5 p9 ]5 X% C
  90.   SystemClock_Config();0 r  \+ s, Q/ B% E! M

  91. 3 B' W! p" a: E: @  m( J
  92.   /*##-1- Configure the UART peripheral ######################################*/: l' h3 N. o& P) {- S1 H+ m3 ]
  93.   /* Put the USART peripheral in the Asynchronous mode (UART Mode) */) O) P& p( s9 d' B( W
  94.   /* UART configured as follows:" S$ n: K' m4 u9 M* c/ @/ c
  95.       - Word Length = 8 Bits
    ( `% j$ N$ y* l- p, \
  96.       - Stop Bit = One Stop bit
    % B6 h" {. u, }& O* F
  97.       - Parity = None) X% x3 c/ d3 o4 S, Z
  98.       - BaudRate = 9600 baud+ b: _" ^0 I4 k& z9 W3 i0 |
  99.       - Hardware flow control disabled (RTS and CTS signals) */
    4 q' u$ Y* V; {5 {4 c
  100.   UartHandle.Instance        = USARTx;
    . w# e- n* n! \/ O; v' |5 x. W( l

  101. # p9 q) R7 m4 W0 y5 }& B4 u: D9 V0 ?
  102.   UartHandle.Init.BaudRate     = 9600;
    4 s, G- U9 o* O2 N- ~
  103.   UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
    & M, u5 Z: H( w, G* R3 G
  104.   UartHandle.Init.StopBits     = UART_STOPBITS_1;9 @, O) o2 K* M1 D8 i  b
  105.   UartHandle.Init.Parity       = UART_PARITY_NONE;5 ]; P; |4 L7 |! q1 Q% E( p
  106.   UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
    2 E3 P5 x" Y% S% b  R
  107.   UartHandle.Init.Mode         = UART_MODE_TX_RX;
    2 j# ]+ P) h- C: w- p
  108.   if(HAL_UART_DeInit(&UartHandle) != HAL_OK)3 ^& v5 K& n" a9 _2 a' W3 L
  109.   {' k/ \! {4 g( a% f! I9 e+ \
  110.     Error_Handler();& s8 O( C8 }  D' @" r4 S. ?
  111.   }  
    # C! N- x0 c6 G- t
  112.   if(HAL_UART_Init(&UartHandle) != HAL_OK)+ _, i& ]  d6 w( X/ s5 c
  113.   {
    : z7 k: b- _. P" u+ t- f
  114.     Error_Handler();
    ; U; G) p$ V# j7 u
  115.   }5 S, G) E6 S3 m
  116.   8 z" V5 j/ a; c2 h- x9 B
  117.   /*##-2- Start the transmission process #####################################*/  
    5 }1 {& G  `- Q; k  N0 S! p
  118.   /* While the UART in reception process, user can transmit data through
    ( k2 C/ w/ x) Y& z2 v! O1 D
  119.      "aTxBuffer" buffer */
      p: y/ N, e4 @! i& Y
  120.   if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)' N  Q8 r; ~" C
  121.   {6 o1 _' Z% p2 ]
  122.     Error_Handler();   
    4 \# ^$ |$ Z9 s( ^
  123.   }
    $ |7 O) u% [! k$ _% G
  124.   /* Infinite loop */
    0 {0 {! f. a- a/ [4 {, l0 `& G
  125.   while (1): t, x, j+ |2 p% f% J1 W" G2 _" W
  126.   {
    / v5 w; }1 z* @* E# o
  127.   }
    2 j% O; \( ?* H& y0 N
  128. }( R) F: l8 x, ~2 g. J

  129. 1 W# \; G% ^$ ?% k& J* U
  130. /**
    5 V. T) T. J1 t
  131.   * @brief  System Clock Configuration0 U- X7 D# E5 C* E, B
  132.   *         The system Clock is configured as follow :
    : y. \/ S# J: v/ c/ J
  133.   *            System Clock source            = PLL (HSI)
    9 o+ Q0 N+ a! u( t1 K2 U9 w4 ~, K5 c
  134.   *            SYSCLK(Hz)                     = 64000000
    ; S" e) s# J' t5 B+ e# z0 S
  135.   *            HCLK(Hz)                       = 64000000
    ! e+ s- K. ]3 H
  136.   *            AHB Prescaler                  = 1
    ' k. C7 ]* z4 b* ^" C
  137.   *            APB1 Prescaler                 = 2
    7 h! K9 |4 ^! S; y( e9 Z
  138.   *            APB2 Prescaler                 = 1
    ) H2 t9 T  b! W8 w' c6 K' \
  139.   *            PLLMUL                         = 16
      \0 N, d8 K) _0 L# Z
  140.   *            Flash Latency(WS)              = 2
    * s! w2 L4 t1 n+ @4 _/ N
  141.   * @param  None9 B, ~5 a/ ]9 E  E
  142.   * @retval None" N# y* S8 k/ q
  143.   */
    , g! {% G; k0 S
  144. void SystemClock_Config(void)! H7 r* I' @+ g% m, a) N" m
  145. {0 @1 _+ Q' r) d3 N8 h
  146.   RCC_ClkInitTypeDef clkinitstruct = {0};2 |' t) f/ a6 B
  147.   RCC_OscInitTypeDef oscinitstruct = {0};
    4 e5 p# h& w2 E: V5 O
  148.   
    1 |; C( |) I( @4 @/ j; P
  149.   /* Configure PLL ------------------------------------------------------*/
    2 z( j: S1 r6 Q) s8 r
  150.   /* PLL configuration: PLLCLK = (HSI / 2) * PLLMUL = (8 / 2) * 16 = 64 MHz */& \! y7 b& T; f, a, t
  151.   /* PREDIV1 configuration: PREDIV1CLK = PLLCLK / HSEPredivValue = 64 / 1 = 64 MHz */. S, @; j1 E+ c& f) R
  152.   /* Enable HSI and activate PLL with HSi_DIV2 as source */5 ]7 t& K- r" D9 i- O' _/ T
  153.   oscinitstruct.OscillatorType  = RCC_OSCILLATORTYPE_HSI;
    2 L9 x# b9 _$ [$ Y8 x
  154.   oscinitstruct.HSEState        = RCC_HSE_OFF;( l+ _3 y5 Z' U
  155.   oscinitstruct.LSEState        = RCC_LSE_OFF;/ N* b0 t1 `9 g& n5 K7 u) l. @
  156.   oscinitstruct.HSIState        = RCC_HSI_ON;
    . l! f: M8 e3 @2 l3 I; l' S1 p: P
  157.   oscinitstruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    + E: M3 P1 V/ [5 |
  158.   oscinitstruct.HSEPredivValue    = RCC_HSE_PREDIV_DIV1;: E( T* S& F0 p  ]& J" Z
  159.   oscinitstruct.PLL.PLLState    = RCC_PLL_ON;
    5 D1 b# n2 X( v9 |" [
  160.   oscinitstruct.PLL.PLLSource   = RCC_PLLSOURCE_HSI_DIV2;
    ' G& z! m9 J9 x1 N* _$ O
  161.   oscinitstruct.PLL.PLLMUL      = RCC_PLL_MUL16;/ Z' ]# _0 G% S0 t% n
  162.   if (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)4 T' |  [8 r1 D
  163.   {, }+ }3 L: ^! |5 i" t
  164.     /* Initialization Error */- n$ p6 l  F9 U4 ^
  165.     while(1); 5 u, d& ?9 m" b% M0 K
  166.   }
    - p+ |! ]- M, Z8 l  r* [

  167. # E! B2 x2 p/ S% n( {) o
  168.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 " J1 ?3 _2 }! }5 V, R1 d
  169.      clocks dividers */
    * J  B' `; f! [$ J% |5 h6 Z6 Q$ d
  170.   clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);8 u+ A9 o( t; m% N; v+ ^% c( n
  171.   clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    / ?% a: O9 R, ^( p
  172.   clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    * t+ M. d" Q1 Y1 J4 Z
  173.   clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;2 E! a) @; D# i1 _: F2 b
  174.   clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2;  # x* N5 p) K8 h: m; e) \& z# V
  175.   if (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2)!= HAL_OK)2 t% a) L& X& H
  176.   {
    3 n( I: B7 N/ C) |3 b# F' N
  177.     /* Initialization Error */* j: X3 O& h' g7 N
  178.     while(1);
    # t3 r  s1 G9 r5 B: |. ?
  179.   }2 C$ d) g, F6 O* s9 Y7 ~
  180. }
    ( |" p  Z( R  N. y, z7 X
  181. , a4 t% ?, f. }) t: h; j* q
  182. /**
    ' t# T. {# P$ n3 h* e: n8 z; u2 B% M
  183.   * @brief  UART error callbacks* V- Q* h4 @6 a1 B. N: K
  184.   * @param  UartHandle: UART handle( E) W- v1 L9 D1 ~% j2 @
  185.   * @note   This example shows a simple way to report transfer error, and you can
    # W: H/ W7 L1 w9 [
  186.   *         add your own implementation.: ^: \0 a( ]4 W% d0 U) m1 v* n
  187.   * @retval None
    5 F9 X: j0 O' L5 Z& q  U# J
  188.   */
    5 v' k) ^# h% z0 S* |
  189. void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)9 m# [0 {# L. T$ ^
  190. {; o+ x' C( j) ]; a% D
  191.     Error_Handler();
    : ?5 H! }1 m2 H3 z; e' t" u
  192. }8 U- ^- ^( I+ p

  193.   g; A  n  b, i4 ~
  194. /**9 t6 G+ a3 y7 t* w" h
  195.   * @brief  This function is executed in case of error occurrence.
    : b9 L: C) K2 o8 [, L2 P+ ~+ K, d
  196.   * @param  None
    3 @2 r. \: z4 H/ N/ @
  197.   * @retval None6 ]3 E8 z- S+ B3 j4 _: Q( P
  198.   */
    : B0 E  n, `8 L+ W* n7 R7 l
  199. static void Error_Handler(void)
    8 N1 ~* J7 U: N' h
  200. {9 z5 e3 n+ B/ Y* |" f) q
  201.   while(1)
    - G% x$ t6 v; l9 {! e, y  o
  202.   {
    - u" A  m2 I9 i8 M1 ?$ ]) v( m' i( }
  203.     HAL_Delay(1000);
    ( l9 ?, M8 d; h, K& r
  204.   }' x% b+ i) q  Y% s0 ]+ y$ e
  205. }
    9 V8 L+ _  Q$ T" j/ I, P# W
  206. & T) e+ z' s2 g4 K2 f' z/ M4 O9 ~
  207. #ifdef  USE_FULL_ASSERT* C# I5 h$ {& u! K  {1 A6 r; C
  208. ; }8 Q) r. ~, m. D# c
  209. /**
    / p$ u) d5 r5 o* ~' M* r
  210.   * @brief  Reports the name of the source file and the source line number
    4 A7 r# i1 d# M7 N: L
  211.   *         where the assert_param error has occurred.: s- x" g, m+ |2 R. g1 D1 k8 m
  212.   * @param  file: pointer to the source file name) e1 ?1 D. a1 x. H! Z4 n/ i
  213.   * @param  line: assert_param error line source number
    0 U% p5 |' H2 j2 h, t4 {  _
  214.   * @retval None; X, y; [) s% G" C. `
  215.   */& c8 O/ S- w; d
  216. void assert_failed(uint8_t* file, uint32_t line)% p) G+ T8 y, Z% ~7 I0 e! z! S/ f
  217. { . W$ c2 S# I9 B( G! ?
  218.   /* User can add his own implementation to report the file name and line number,
    9 g6 Z7 r/ @! J. E
  219.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    ) Q! I7 S% p' y0 X0 f" o

  220. . t: E9 Z( e, |- A
  221.   /* Infinite loop */
    . h3 G/ W0 F4 f. r: w. z0 h% S) O
  222.   while (1)
    7 w5 Q, x; \  K) s/ O
  223.   {
    ' H  f; |/ E: x( t# C
  224.   }& O$ J( T" x2 J0 f: J# v5 n/ G
  225. }
    , r: }$ h9 `  Z, B0 |, E) [# m
  226. #endif! G* M3 j5 ~/ }$ c, m

  227. # a6 r6 ?4 [, @! w1 k1 [" \

  228. # n# Q5 C) K. @: D' ]7 V; B
  229. /**
    % i% Q& _$ k7 @5 v
  230.   * @}
    2 ~0 Q. B" l3 G: e5 |* d
  231.   */
    - u. m  ^9 T% r0 ]5 }
  232. 2 g% e# s+ \) ?" H  O. M! S
  233. /**1 _' m& ^5 F% n4 ]: {4 [
  234.   * @}
    # n  N) J' N! A; Q
  235.   */
    - P$ }) F* H& [7 d
  236. 4 h  V& }/ i0 F, y9 A7 C, D
  237. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    : d7 |, N; I! X) v. c! K* o
复制代码

打开Powershell进入到该目录中,如下:

3.JPG

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会弹出配置对话框,如下:

4.JPG

进入第一项Basic Configuration,配置平台、体系架构、以及目标类型,如下:

5.JPG

这里我们需要配置前6项,前5项配置很容易,做一些选择即可,接下来配置第6项,如下:

6.JPG

设置工具链前缀以及目录,后面还需要配置编译器以及连接器,如下:

8.JPG

7.JPG

配置完后保存退出,会发现当前目录下多了一个.xmake文件夹

9.JPG

其中xmake.conf就包含了刚才我们的配置信息:

捕获.JPG

其他文件的信息,可以多去官网查看,.xmake文件夹下的文件是xmake自动生成的,直接修改配置文件是不行的。

# z. T' q+ s( B' G

接下来就是更改我们的xmake.lua文件,该文件主要是添加源文件,设置头文件搜索路径、添加宏定义、以及设置编译和链接选项,该文件内容如下:

  1. -- the debug mode
    # M+ {; j3 {; z' B; g+ S5 C' F
  2. if is_mode("debug") then3 k. c7 L4 M( z. }4 l
  3.     7 n! ~% F' M$ S4 i" z0 s# e% C
  4.     -- enable the debug symbols( @% y6 P3 C* h$ j
  5.     set_symbols("debug")* T* ?; _4 }- k$ p5 F
  6. ' ?+ O: y! u: h' a
  7.     -- disable optimization
    ! D( r/ s. I; V! ?" |
  8.     set_optimize("none")
    # R5 {, H# p* ?. C. [: Q. k* S5 E
  9. end! E3 m  I5 g" \/ T  X
  10. . X) D2 e* H6 x3 F3 P9 N
  11. -- the release mode2 N# b/ _2 j) v- O4 N
  12. if is_mode("release") then
    & R5 ^( O3 n$ T3 v" D
  13. 6 m/ }- s$ L% K, c5 y" g- B
  14.     -- set the symbols visibility: hidden
    6 G4 A  P6 {$ N( _/ V" A5 c& I7 b
  15.     set_symbols("hidden")
    8 L: \- V3 B  P. i+ I' c* Y0 x1 ]
  16. + I% Y6 B3 @% q8 M9 f$ ~
  17.     -- enable fastest optimization
    $ m7 [7 N; d: T+ N- `! X/ {2 g
  18.     set_optimize("fastest")2 }, O: N& Z3 |6 i& g
  19. # w/ c" u1 @! s
  20.     -- strip all symbols: @$ Z% e* U2 d. E
  21.     set_strip("all")
    ' z. c# L3 e6 m0 t
  22. end* n5 a) G. f' x9 t- }0 n
  23. -- define target. |% {. V$ @: V/ d/ k* Q9 x- j
  24. target("UART_Xmake.elf")
    * G" c% N+ F4 J0 e# ?- y

  25. , i4 T" z. I8 e7 b' x% y$ D
  26.     -- set kind
    , F5 p9 N% b+ n8 z- W9 c
  27.     set_kind("binary")
    5 m) z# e0 A6 o) V5 H  q9 \
  28. . B" [- i* v) S& J% a' v' |
  29.     -- add files: z( @3 k( e  g$ X7 |; S' @) ^
  30.     add_files("startup_stm32f103xb.s")( \% z. s2 i! ]2 s2 |/ O9 ?
  31.     add_files("Src/*.c")
    1 p. E, ?- j8 k( j
  32.     add_files("../../../../../Drivers/BSP/STM32F1xx_Nucleo/stm32f1xx_nucleo.c")  `7 ^# y4 s/ R
  33.     add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c")
    / {  Y6 U# p6 x* t. P
  34.     add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c")3 [& Y$ A9 Y: U* P# v
  35.     add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c")
    1 `$ B' l: M2 w* }0 a
  36.     add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c")# E0 X8 f; u/ N3 x( b- J, M$ }
  37.     add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c")
    " W( V! U$ Y8 L$ B9 s7 X0 K
  38.     add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c")
    1 Y# ]8 T0 Z& L  L* G
  39.     add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c")4 u9 t0 V! X. o) \7 M4 M+ ]/ o
  40.     add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c")0 }# g, H/ `0 T
  41.         * }* i# y+ L- b0 o# {& H& x" ?
  42.     --add include search directories$ `8 ]  Z" A6 i' l; z  S
  43.     add_includedirs("Inc")
    1 K$ e* f* A) O4 s4 A
  44.     add_includedirs("../../../../../Drivers/CMSIS/Device/ST/STM32F1xx/Include")# s6 a$ i, ~  m% V
  45.     add_includedirs("../../../../../Drivers/CMSIS/Include")/ o* H6 m. ^7 m  n, N$ A! g
  46.     add_includedirs("../../../../../Drivers/STM32F1xx_HAL_Driver/Inc")% v( P2 `7 c& v
  47.     add_includedirs("../../../../../Drivers/BSP/STM32F1xx_Nucleo")/ M( a  y4 \9 c5 ~
  48.    
    " z+ f+ p) U# D9 ]/ f1 L
  49.     --add macro defination
      ~' ?4 H6 ^- [8 Z3 o
  50.     add_defines("USE_HAL_DRIVER","STM32F103xB","USE_STM32F1xx_NUCLEO")" l1 f* h* H: A$ Q% F
  51.     : B- Q- U: ^4 s$ w- Y1 r2 s
  52.     -- set warning all as error/ s% A. n  V8 V/ u. R
  53.     set_warnings("all", "error")6 {( Z; u4 u& h3 S& I- W( b6 @9 b% o
  54.     , P% _+ m* X4 R* c2 R
  55.     -- set language: c997 ~' f/ j( C3 @3 h
  56.     set_languages("c99"). Q5 `+ u) T# D/ T
  57.         
    7 y2 T5 s: }/ ?  A* O* _( W
  58.     add_cxflags("-mcpu=cortex-m3 -mthumb -mfloat-abi=soft --specs=nano.specs  -ffunction-sections -fdata-sections")7 f6 O# j+ i' l" `5 I
  59.     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
  60.     after_build(function(target)  N: s" W0 ]0 @) E  E) o  A: u
  61.         os.exec("arm-none-eabi-objcopy -O binary .\\build\\UART_Xmake.elf .\\build\\UART_Xmake.bin"): p7 ?! g" E1 n: K8 P
  62.     end)
      [# `- J, b1 B
  63. -- FAQ+ ^; D% i. I- X5 Z
  64. --' k$ P9 ]4 E1 g2 b$ }
  65. -- You can enter the project directory firstly before building project.' a; M) t8 \* E0 b( \$ K& D
  66. --   ' T7 d) }6 d+ r5 E
  67. --   $ cd projectdir2 k9 a$ U- j6 R5 Y0 ?
  68. -- 8 x, p+ W5 D/ P6 i! z
  69. -- 1. How to build project?" `& D' K1 [  z( x8 r# r7 \- R
  70. --   
    $ f) h) w8 I# D" Q* P
  71. --   $ xmake
    ; }5 G8 T$ ^7 G& n3 o
  72. --* z& H  V3 D* o* {
  73. -- 2. How to configure project?' l! B7 t. i1 U
  74. --3 g+ p9 n% z/ n- `# e6 v
  75. --   $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]6 Y8 K0 W- |& j8 ^8 I6 R. x
  76. --1 a( f1 c$ g  I1 d' e/ }) H3 L7 s
  77. -- 3. Where is the build output directory?# ~1 F6 u' ~) a4 M5 Y2 S5 U
  78. --6 N2 B* {3 y4 d' K7 ^& R
  79. --   The default output directory is `./build` and you can configure the output directory.
    ) l2 @! `4 t% O, G/ v8 c) j
  80. --/ Y' P$ g6 |( x! C* W: \+ S+ z
  81. --   $ xmake f -o outputdir, t' F! @0 E) J3 ~4 O
  82. --   $ xmake
    9 M- Y: h* \9 e6 P3 w# n
  83. --) d" A0 M3 y3 s$ [/ d
  84. -- 4. How to run and debug target after building project?% r7 U/ c" U- R# E4 T0 Z
  85. --" M  V. Y9 f6 k3 E4 A
  86. --   $ xmake run [targetname]
    6 m' B7 S0 ?+ o; e$ ]; m* q; W
  87. --   $ xmake run -d [targetname]
    2 a% X) P' M% {+ S2 T* L
  88. --
    : J1 A1 R) X$ R9 ~; h2 W" q8 O
  89. -- 5. How to install target to the system directory or other output directory?4 ?6 k, p% G9 q- Y2 n/ C: K$ {
  90. --
    : e" Q' Z1 @7 T6 u, |# y
  91. --   $ xmake install 1 B' w! k1 U: B* |6 t
  92. --   $ xmake install -o installdir  D8 D3 F7 d$ _% G2 p* v
  93. --
    + f; ~- M8 ?8 f& g: w9 v
  94. -- 6. Add some frequently-used compilation flags in xmake.lua
    # P) L  `& d  [8 y
  95. --* I! o* g+ I2 g4 w$ Y
  96. -- @code
    & G% [# g( @2 J2 _1 D
  97. --    -- add macro defination8 q2 O8 L; X: Z; v2 v
  98. --    add_defines("NDEBUG", "_GNU_SOURCE=1")
    2 N0 L3 m- a+ `! R: s1 L
  99. --
    ( b! Y# Q: d7 {7 Q& n- F
  100. --    -- set warning all as error
    $ F+ l$ @' m3 P, K9 U$ a5 v1 |4 z* e
  101. --    set_warnings("all", "error")/ D6 a% M3 I6 M8 ]6 O
  102. --( L3 f4 m) x9 z; x4 h6 ^9 }% J
  103. --    -- set language: c99, c++11
    " {) H) B) w! X! p6 i; h/ `
  104. --    set_languages("c99", "cxx11")% C, y. W, j. |0 \  ?
  105. --1 z6 l' v3 }$ U
  106. --    -- set optimization: none, faster, fastest, smallest
    5 g* h2 G; i; E# K. B# u# L3 Y
  107. --    set_optimize("fastest")
    3 F' `( u: s. d7 ^: p8 Z2 i% D3 ^& _  c
  108. --   
      O, i/ s6 v4 u6 [( E
  109. --    -- add include search directories
    5 P9 Y8 m6 f5 [" F- P8 B& l
  110. --    add_includedirs("/usr/include", "/usr/local/include")8 U# A5 J. Y& L' K% a9 V* \
  111. --3 p' M  \/ S, ^# t
  112. --    -- add link libraries and search directories
    ! \! }# q2 Q2 h- V2 e
  113. --    add_links("tbox", "z", "pthread")
    6 _' p$ x/ W: Q' `* H3 o
  114. --    add_linkdirs("/usr/local/lib", "/usr/lib")- u$ ^. e- J% a
  115. --
    # S( m' v; B* p8 O4 g
  116. --    -- add compilation and link flags+ j- ?7 S/ p$ A( [8 H! u4 z
  117. --    add_cxflags("-stdnolib", "-fno-strict-aliasing"), {9 [. k6 m; m; K4 Q5 h
  118. --    add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})4 \8 |3 m/ S- |/ s# J* y
  119. --, b3 v0 S( E3 a
  120. -- @endcode% \& r3 N7 q% t. z* ~' h# I) ?
  121. --
    + D' L  ?% N# \* w& x# T5 ^  I) c( C5 A
  122. -- 7. If you want to known more usage about xmake, please see http://xmake.io/#/home* c8 k  D* G8 ~1 c5 U% \, ^! W" |
  123. --
    * f& N4 C& |0 p) s$ l. q
  124.    
    " J5 c# ^' D; Y
复制代码

至此,xmake配置就算完成了,在当前目录下输入xmake会自动编译该工程,最后会在build目录下生成UART_Xmake.bin文件,烧录该文件到目标板中运行,结果如下:

11.JPG

好了,使用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

评分

参与人数 1 ST金币 +2 收起 理由
MrJiu + 2

查看全部评分

赞 收藏 2 评论11 发布时间:2018-5-6 15:24

举报

11个回答
anobodykey 回答时间:2018-5-6 15:26:13
自己顶一顶
pythonworld 回答时间:2018-5-6 16:35:08
谢谢分享,是不是还得会lua语言?
anobodykey 回答时间:2018-5-6 16:47:56
pythonworld 发表于 2018-5-6 16:357 h; l/ z: S" {7 x7 ~9 @* i
谢谢分享,是不是还得会lua语言?
& x7 z: J- F4 P$ D$ x& P
不需要的,除非你要扩展插件之类的
黑皮男 回答时间:2018-5-7 08:12:37
赞一个
MrJiu 回答时间:2018-5-7 09:34:48
确实不错!!!
butterflyspring 回答时间:2018-5-7 10:45:03
不错,这是个扫盲帖...赞!
andey 回答时间:2018-5-7 11:08:19
提示: 作者被禁止或删除 内容自动屏蔽
xiajintaord 回答时间:2018-5-7 11:47:01
感谢楼主的分享!在公司每天忙业务,解 bug ,没得机会沉下心思来研究这些细致的东西;
zjp1013 回答时间:2018-5-14 20:07:35
你好,按照你的方法我试了一下,有如下错误,请指点一下谢谢
' L1 I) R( x% M+ {* D, n3 z[00%]: compiling.release Src\system_stm32f1xx.c
4 t( L! R  F+ h[20%]: compiling.release Src\stm32f1xx_hal_msp.c
/ o6 c# I0 h9 e, D- ~- _* J# H0 \[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'
anobodykey 回答时间:2018-5-15 09:20:18
zjp1013 发表于 2018-5-14 20:07
) J+ ~2 P  E/ S  y! N7 [你好,按照你的方法我试了一下,有如下错误,请指点一下谢谢
$ f8 b6 L+ Y. f& u5 P' Q& F[00%]: compiling.release Src\system_stm32f1 ...

  d0 B4 w/ \" o( V& y看名字是提示你命令行参数无法识别,你把这个参数去掉再重新编译下
jcx0324 回答时间:2018-5-15 09:34:47
又一种方案                  
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版