Application of wearable devices with weak performance: Lua for stm32

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

This year, wearable devices suddenly fire ...... the difference between wearable devices and electronic products before the concept breaks out is that they are "Networked" and "supporting apps", so that a closed gadgets can be imagined by many people, diaosi's imagination is very rich ....

Currently, wearable devices are divided into two types: "diaosi" and "Gao fushuai". The so-called "diaosi" configuration means that the device cannot be installed on Android. In the future, watches and wristbands are mainly configured with "diaosi". For example, pebble and Sony watches use arm cortex m3 cores, but "Gao fushuai" geek watch uses android ....

Blow the water, it is the focus of this article, how to make the weak performance equipment to support the app, that is, to port Lua to the terminal ...... This article code can be downloaded here (http://download.csdn.net/detail/hellogv/5721915.

The Board on hand is stm32f103rct6 flash 256 k ram 48 K, and the configuration is barely running Lua. The development environment is IAR for ARM 6.4. It will also facilitate the use of eclipse CDT in the future:


1. Download Lua: http://www.lua.org/download.html. This document uses Lua 5.2.2;


2. Copy all the files in/Lua/src to the IAR for stm32 project;


3. In the IAR for stm32 template project, create a Lua folder under workspace and add all source files:



4. Modify the project configuration





5. Remove Lua. C and luac. C from the project. Otherwise, an error [li006]: duplicate definitions for "Main" will be prompted during compilation ";


6. Modify stm32f10x_flash.icf. Otherwise, if the compilation succeeds, the system prompts insufficient memory:
Define symbol _ icfedit_size_cstack _ = 0x00008000;
Define symbol _ icfedit_size_heap _ = 0x00002000;

In addition to transplantation, this program also compares the computing efficiency of Luc and c Through cyclic addition through computing time consumption. The source code is as follows:

# Include "stm32f10x_conf.h" # include "stm32f10x_lib.h" # include "stm32f10x_javasick.h" # include <math. h> # include "Lua. H "# include" lauxlib. H "# include" lualib. H "Void rcc_init (void) {/* defines the enumerated type variable hsestartupstatus */errorstatus hsestartupstatus;/* resets the system clock setting */rcc_deinit (); /* enable HSE */rcc_hseconfig (rcc_hse_on);/* Wait For HSE to activate and stabilize */hsestartupstatus = rcc_waitforhsestartup ();/* determine whether the HSE has been activated successfully, yes, it enters the IF () Internal */If (hses Tartupstatus = success) {/* select the hclk (AHB) clock source as sysclk 1 division */rcc_hclkconfig (rcc_sysclk_div1);/* select the pclk2 clock source as hclk (AHB) 1 division */rcc_pclk2config (rcc_hclk_div1);/* select pclk1 clock source as hclk (AHB) 2 Division */rcc_pclk1config (rcc_hclk_div2 ); /* set the flash latency to 2 */flash_setlatency (flash_latency_2);/* enable Flash cache prefetch */flash_prefetchbuffercmd (flash_prefetchbuffer_enable);/* select the PLL) if the clock source is HSE 1, and the frequency is 9, the PLL output frequency is 8 MHz * 9. = 72 MHz */rcc_pllconfig (rack, rcc_pllmul_9);/* enable PLL */rcc_pllcmd (enable) = reset);/* select sysclk as the clock source */rcc_sysclkconfig (rcc_sysclksource_pllclk);/* Wait for the PLL to become the sysclk clock source */while (rcc_getsysclksource ()! = 0x08);}/* enable each peripheral clock used */rcc_apb2periphclockcmd (rcc_apb2periph_usart1 | rcc_apb2periph_gpioa, enable);} void gpioinit (void) {/* define the gpio initialization structure */gpio_initstructure */gpio_inittypedef gpio_initstructure;/* set the Tx foot (pa.9) of usart1 to the second function to push the output function */outputs = gpio_pin_9; outputs =; authorization = gpio_speed_50mhz; gpio_init (gpioa, & gpio_initstructure);/* set the RX foot of usart1 (pa.10) to the float input foot */authorization = gpio_pin_10; authorization = gpio_mode_in_flo; gpio_init (gpioa, & gpio_initstructure);} void usartinit (void) {/* defines the usart initialization struct usart_initstructure */usart_inittypedef usart_initstructure;/* defines the usart initialization struct */usart_clockinittypedef struct; listener = listener; listener = usart_cpol_low; listener = listener; usart_clockinit (usart1, & timeout); listener = 9600; listener = listener; listener = usart_stopbits_1; listener = usart_parity_no; parameters = enabled; Parameters = usart_mode_rx | usart_mode_tx; usart_init (usart1, & usart_initstructure);/* enable usart1 */usart_cmd (usart1, enable );} /*************************************** **************************************** * Function Name: fputc * Function Description: relocate the printf function to usatr1 * input parameter: none * output result: none * return value: no *************************************** **************************************** /INT fputc (int ch, file * f) {usart_senddata (usart1, (u8) CH); While (usart_getflagstatus (usart1, usart_flag_tc) = reset); Return ch ;} const char lua_script [] = "function loop_add (a, B, T)" "Local sum = 0" "for I = 1, t do "" sum = sum + A + B "" end "" Return sum "" end "; u32 c_loop_add (int A, int B, int T) {u32 result = 0, I = 0; for (I = 0; I <t; I ++) {result = Result + A + B;} return result ;} int use_lua_add (lua_state * l, const char * func_name, int X, int y, int t) {int sum;/* load script */lua_getglobal (L, func_name ); /* First parameter */lua_pushnumber (L, x);/* second parameter */lua_pushnumber (L, Y);/* third parameter */lua_pushnumber (L, t);/* call the function to notify you that there are three parameters, one return value */lua_call (L, 3, 1);/* get the result */SUM = (INT) lua_tointeger (L,-1); lua_pop (L, 1); Return sum;}/*** start time-consuming computing, not suitable for too-time computing */void start_calculate_time () {export ick_clksourceconfig (optional); // (8*9000) * 1000 = 72mhzsystick_setreload (0 xffffff); // The calibration value is decreasing continuously after the calibration starts. Export ick_countercmd (export ick_counter_enable ); // start the systick timer}/*** to stop the computing time, which is not suitable for ultra-time computing */u32 stop_calculate_time () {systick_countercmd (systick_counter_disable ); // disable the autoick timer u32 result = 0 xffffff-systick_getcounter (); // obtain the time difference between them: systick_countercmd (systick_counter_clear); // clear 0 result = Result/9000; // divide by 9000-> result in the unit of msreturn result;} int main () {rcc_init (); gpioinit (); usartinit (); lua_state * l = lual_newstate (); if (L = NULL) printf ("cannot create State: not enough memory"); luaopen_base (l); // 48 KB memory is insufficient, and all modules are loaded with lual_openlibs, will automatically exit lual_dostring (L, lua_script); int I; for (I = 10000; I <90000; I = I + 30000) {// ---- time consumed for Lua cyclic addition calculation start_calculate_time (); int sum = use_lua_add (L, "loop_add", 1, 1, I); u32 duration = stop_calculate_time (); printf ("Lua % d", I); printf ("Frequency duration (MS): % d \ r \ n", duration ); // ---- start_calculate_time (); sum = c_loop_add (1, 1, I); duration = stop_calculate_time (); printf ("C % d ", i); printf ("Frequency duration (MS): % d \ r \ n", duration);} // disable the Virtual Machine lua_close (l); While (1) {};}

Loop addition operations of Lua and C are time-consuming and output through serial ports. The efficiency ratio of simple Lua programs to C Programs is, whileThe larger the Lua workload, the smaller the efficiency gap with the C program..


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.