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.
Previous article:STM32F10x usart data transceiver
Next article:STM32 UVC study notes 2
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Please give a comprehensive analysis of the advantages and disadvantages of i.MX8QuadMAX and RK3399PRO
- Review Weekly Report 20211115: 1 day left to apply for Fudan Micro FM33LG0, Pingtou Ge smart voice, Anxinke PB-02 unboxing, etc.
- 【E840-DTU】MQTT sending and receiving test
- Who should I turn to for help when I need help with domestic FPCB manufacturers?
- uasyncio basic tutorial (English)
- Are there any netizens in our forum participating in the 2021 STM32 China Summit Hackathon 24-hour challenge?
- [RT-Thread Reading Notes] Week 3: Entering the World of RT-Thread - Threads, Clocks
- Autonomous Robot Design Using ROS and mmWave Sensors
- Please help me check, after the PCB copper is applied, a lot of errors are reported: Unrouted net constraint.
- FPGA implementation of high-speed dedicated GFP processor.pdf