Use xmake to build STM32 program

Publisher:幸福自由Latest update time:2019-03-11 Source: eefocusKeywords:xmake Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Host environment: Windows 7 SP1


Compiler version: gcc-arm-none-eabi-7-2017-q4-major-win32


Target board: STM32F103C8T6 board


STM32 library version: STM32Cube_FW_F1_V1.6.0


XMAKE version: 2.1.9


I have been thinking about cross-platform development for some time. I used MDK development environment to develop STM32 before, but the software can only be used on Windows platform. The cross-platform integrated development environments include SW4STM32 (supports windows, macos, linux), TureStudio (supports windows, linux), and Embeded Studio (supports windows, macos, linux). The final choice is gcc. In fact, SW4STM32 and TureStudio are also based on gcc, and the two are basically similar. I have used Embeded Studio several times before, and the version update is also very fast. But now I have to apply for a license to compile, and I am too lazy to do it. It just happens to be a chance to learn gcc. Most of the gcc history on the Internet is a combination of gcc+eclipse+makefile. I applied for an NXP 54608 board before. CMake was mentioned in the SDK, so I went online to learn about it. It is a very good tool. At the same time, I learned about another build tool, which is today's topic - XMake. The URL is as follows: http://xmake.io. If you are interested, you can go and learn about it. The difference between xmake and cmake is that it does not generate project files, but directly compiles. It uses lua language and supports three major platforms. At present, I have only studied it on the official website, and I am still in a state of knowing only a little. Therefore, using it to build STM32 to learn xmake and gcc is killing two birds with one stone.


First, install xmake and gcc toolchain and add them to PATH as follows:



The next step is to edit our source code files. Since we are just learning, we only need very simple functions. Here we take the serial port as an example to perform simple output. Copy the UART_TwoBoards_ComPolling project and name it UART_Xmake. The organization is as follows:



The startup file and link file are copied from the SW4STM32 example. The main program is as follows:


/**

  ******************************************************************************

  * @file    UART/UART_TwoBoards_ComPolling/Src/main.c 

  * @author  MCD Application Team

  * @version V1.5.0

  * @date    14-April-2017

  * @brief   This sample code shows how to use UART HAL API to transmit

  *          and receive a data buffer with a communication process based on

  *          polling transfer. 

  *          The communication is done using 2 Boards.

  ******************************************************************************

  * @attention

  *

  *

© COPYRIGHT(c) 2016 STMicroelectronics

  *

  * Redistribution and use in source and binary forms, with or without modification,

  * are permitted provided that the following conditions are met:

  *   1. Redistributions of source code must retain the above copyright notice,

  *      this list of conditions and the following disclaimer.

  *   2. Redistributions in binary form must reproduce the above copyright notice,

  *      this list of conditions and the following disclaimer in the documentation

  *      and/or other materials provided with the distribution.

  *   3. Neither the name of STMicroelectronics nor the names of its contributors

  *      may be used to endorse or promote products derived from this software

  *      without specific prior written permission.

  *

  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE

  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE

  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL

  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER

  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,

  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  *

  ******************************************************************************

  */

 

/* Includes ------------------------------------------------------------------*/

#include "main.h"

 

/** @addtogroup STM32F1xx_HAL_Examples

  * @{

  */

 

/** @addtogroup UART_TwoBoards_ComPolling

  * @{

  */ 

 

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

//#define TRANSMITTER_BOARD

 

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* UART handler declaration */

UART_HandleTypeDef UartHandle;

 

/* Buffer used for transmission */

uint8_t aTxBuffer[] = " **** UART_Xmake sample ****";

 

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void Error_Handler(void);

 

/* Private functions ---------------------------------------------------------*/

 

/**

  * @brief  Main program

  * @param  None

  * @retval None

  */

int main(void)

{

  /* STM32F103xB HAL library initialization:

       - Configure the Flash prefetch

       - Systick timer is configured by default as source of time base, but user 

         can eventually implement his proper time base source (a general purpose 

         timer for example or other time source), keeping in mind that Time base 

         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 

         handled in milliseconds basis.

       - Set NVIC Group Priority to 4

       - Low Level Initialization

     */

  HAL_Init();

 

  /* Configure the system clock to 64 MHz */

  SystemClock_Config();

 

  /*##-1- Configure the UART peripheral ######################################*/

  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */

  /* UART configured as follows:

      - Word Length = 8 Bits

      - Stop Bit = One Stop bit

      - Parity = None

      - BaudRate = 9600 baud

      - Hardware flow control disabled (RTS and CTS signals) */

  UartHandle.Instance        = USARTx;

 

  UartHandle.Init.BaudRate     = 9600;

  UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;

  UartHandle.Init.StopBits     = UART_STOPBITS_1;

  UartHandle.Init.Parity       = UART_PARITY_NONE;

  UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;

  UartHandle.Init.Mode         = UART_MODE_TX_RX;

  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)

  {

    Error_Handler();

  }  

  if(HAL_UART_Init(&UartHandle) != HAL_OK)

  {

    Error_Handler();

  }

  

  /*##-2- Start the transmission process #####################################*/  

  /* While the UART in reception process, user can transmit data through 

     "aTxBuffer" buffer */

  if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)

  {

    Error_Handler();   

  }

  /* Infinite loop */

  while (1)

  {

  }

}

 

/**

  * @brief  System Clock Configuration

  *         The system Clock is configured as follow : 

  *            System Clock source            = PLL (HSI)

  *            SYSCLK(Hz)                     = 64000000

  *            HCLK(Hz)                       = 64000000

  *            AHB Prescaler                  = 1

  *            APB1 Prescaler                 = 2

  *            APB2 Prescaler                 = 1

  *            PLLMUL                         = 16

  *            Flash Latency(WS)              = 2

  * @param  None

  * @retval None

  */

void SystemClock_Config(void)

{

  RCC_ClkInitTypeDef clkinitstruct = {0};

  RCC_OscInitTypeDef oscinitstruct = {0};

  

  /* Configure PLL ------------------------------------------------------*/

  /* PLL configuration: PLLCLK = (HSI / 2) * PLLMUL = (8 / 2) * 16 = 64 MHz */

  /* PREDIV1 configuration: PREDIV1CLK = PLLCLK / HSEPredivValue = 64 / 1 = 64 MHz */

  /* Enable HSI and activate PLL with HSi_DIV2 as source */

  oscinitstruct.OscillatorType  = RCC_OSCILLATORTYPE_HSI;

  oscinitstruct.HSEState        = RCC_HSE_OFF;

  oscinitstruct.LSEState        = RCC_LSE_OFF;

  oscinitstruct.HSIState        = RCC_HSI_ON;

  oscinitstruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

  oscinitstruct.HSEPredivValue    = RCC_HSE_PREDIV_DIV1;

  oscinitstruct.PLL.PLLState    = RCC_PLL_ON;

  oscinitstruct.PLL.PLLSource   = RCC_PLLSOURCE_HSI_DIV2;

  oscinitstruct.PLL.PLLMUL      = RCC_PLL_MUL16;

  if (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)

  {

    /* Initialization Error */

    while(1); 

  }

 

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 

     clocks dividers */

  clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

  clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;

  clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2;  

  if (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2)!= HAL_OK)

  {

    /* Initialization Error */

    while(1); 

  }

}

 

/**

  * @brief  UART error callbacks

  * @param  UartHandle: UART handle

  * @note   This example shows a simple way to report transfer error, and you can

  *         add your own implementation.

  * @retval None

  */

void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)

{

    Error_Handler();

}

 

/**

  * @brief  This function is executed in case of error occurrence.

  * @param  None

  * @retval None

  */

static void Error_Handler(void)

{

  while(1)

  {

    HAL_Delay(1000); 

  }

}

 

#ifdef  USE_FULL_ASSERT

 

/**

  * @brief  Reports the name of the source file and the source line number

  *         where the assert_param error has occurred.

  * @param  file: pointer to the source file name

  * @param  line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,

     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

 

  /* Infinite loop */

  while (1)

  {

  }

}

#endif

 

 

/**

  * @}

  */

 

/**

  * @}

  */

 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Open Powershell and enter the directory as follows:

The xmake build program requires a file named xmake.lua, which can be automatically generated. After entering xmake, you will be prompted that the xmake.lua file cannot be found. Enter y to generate the file, or copy a sample xmake.lua file from the xmake installation directory. Both methods are acceptable. I used the first method. After having the xmake.lua file, we can add some configuration information to it to build our STM32 program. For information about xmake.lua, you can go to the official website to check the manual to learn about it.


In addition, we also need to do some configuration for xmake. xmake supports multi-platform building, including windows, linux, macos, cross, android, etc. Here we need to configure it as cross, that is, cross-platform. Enter xmake f --menu and the configuration dialog box will pop up, as follows:

Enter the first item Basic Configuration and configure the platform, architecture, and target type as follows:

Here we need to configure the first 6 items. The first 5 items are easy to configure. Just make some selections. Next, configure the 6th item as follows:

Set the toolchain prefix and directory, and then configure the compiler and connector as follows:



After configuration, save and exit, you will find that there is an additional .xmake folder in the current directory

Among them, xmake.conf contains our configuration information just now:

For information about other files, you can check the official website. The files in the .xmake folder are automatically generated by xmake, and it is not possible to modify the configuration file directly.


The next step is to change our xmake.lua file, which mainly adds source files, sets header file search paths, adds macro definitions, and sets compile and link options. The contents of the file are as follows:


-- the debug mode

if is_mode("debug") then

    

    -- enable the debug symbols

    set_symbols("debug")

    -- disable optimization

    set_optimize("none")

end

-- the release mode

if is_mode("release") then

    -- set the symbols visibility: hidden

    set_symbols("hidden")

    -- enable fastest optimization

    set_optimize("fastest")

    -- strip all symbols

    set_strip("all")

end

-- define target

target("UART_Xmake.elf")

    -- set kind

    set_kind("binary")

    -- add files

    add_files("startup_stm32f103xb.s")

    add_files("Src/*.c")

    add_files("../../../../../Drivers/BSP/STM32F1xx_Nucleo/stm32f1xx_nucleo.c")

    add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c")

    add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c")

    add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c")

    add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c")

    add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c")

    add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c")

    add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c")

    add_files("../../../../../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c")


    --add include search directories

    add_includedirs("Inc")

    add_includedirs("../../../../../Drivers/CMSIS/Device/ST/STM32F1xx/Include")

    add_includedirs("../../../../../Drivers/CMSIS/Include")

    add_includedirs("../../../../../Drivers/STM32F1xx_HAL_Driver/Inc")

    add_includedirs("../../../../../Drivers/BSP/STM32F1xx_Nucleo")

    

    --add macro defination

    add_defines("USE_HAL_DRIVER","STM32F103xB","USE_STM32F1xx_NUCLEO")

    

    -- set warning all as error

    set_warnings("all", "error")

    

    -- set language: c99

    set_languages("c99")


    add_cxflags("-mcpu=cortex-m3 -mthumb -mfloat-abi=soft --specs=nano.specs  -ffunction-sections -fdata-sections")

    add_ldflags("-mcpu=cortex-m3 -mthumb -mfloat-abi=soft --specs=nosys.specs --specs=nano.specs -T\"STM32F103RBTx_FLASH.ld\" -Wl,--gc-sections")

    after_build(function(target)

        os.exec("arm-none-eabi-objcopy -O binary .\\build\\UART_Xmake.elf .\\build\\UART_Xmake.bin")

    end)

-- FAQ

--

-- You can enter the project directory firstly before building project.

--   

--   $ cd projectdir

-- 

-- 1. How to build project?

--   

--   $ xmake

--

-- 2. How to configure project?

--

--   $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]

--

-- 3. Where is the build output directory?

--

--   The default output directory is `./build` and you can configure the output directory.

--

--   $ xmake f -o outputdir

--   $ xmake

--

-- 4. How to run and debug target after building project?

--

--   $ xmake run [targetname]

--   $ xmake run -d [targetname]

--

-- 5. How to install target to the system directory or other output directory?

--

--   $ xmake install 

--   $ xmake install -o installdir

--

-- 6. Add some frequently-used compilation flags in xmake.lua

--

-- @code 

--    -- add macro defination

--    add_defines("NDEBUG", "_GNU_SOURCE=1")

--

--    -- set warning all as error

--    set_warnings("all", "error")

--

--    -- set language: c99, c++11

--    set_languages("c99", "cxx11")

--

--    -- set optimization: none, faster, fastest, smallest 

--    set_optimize("fastest")

--    

--    -- add include search directories

--    add_includedirs("/usr/include", "/usr/local/include")

--

--    -- add link libraries and search directories

--    add_links("tbox", "z", "pthread")

--    add_linkdirs("/usr/local/lib", "/usr/lib")

--

--    -- add compilation and link flags

--    add_cxflags("-stdnolib", "-fno-strict-aliasing")

--    add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})

--

-- @endcode

--

-- 7. If you want to known more usage about xmake, please see http://xmake.io/#/home

--

    

At this point, the xmake configuration is complete. Entering xmake in the current directory will automatically compile the project, and finally generate the UART_Xmake.bin file in the build directory. Burn the file to the target board and run it. The results are as follows:

OK, the example of using xmake to build the STM32 program is complete.


Keywords:xmake Reference address:Use xmake to build STM32 program

Previous article:STM32F10x usart data transceiver
Next article:STM32 UVC study notes 2

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号