Port libfixmath to WICED Smart SDK

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

1. Use of Pre-built libfixmath Lib


Overview

Aims at offering useful math functions to WICED Smart developers, this library is implemented based on the open source fix-point math lib called ‘libfixmath’. In general, this lib supports at most 37 fix-point math functions. For a complete list of the math functions supported, please refer to the table in section 3.

Goal of this section

This section describes necessary steps to use the pre-built math functions lib in WICED Smart SDK. A test application is also included to demonstrate the use of this lib.

Files required in this section

libfixmath lib:

    Header files: libfixmath.h  fix16.h fract32.h int64.h uint32.h (5 in total)

    Static lib file: libfixmath.a

libfixmath_test application:

    libfixmath_test.c

    libfixmath_test.h

    makefile.in

Notes

Following description assumes that you are using WICED Smart SDK 1.x.x. It will work for WICED Smart SDK 2.x.x after some minor changes.

Step1: Copy files to specific directory.

Copy the 5 header files to the directory below:

<WICED-Smart-SDK>/include

Copy the static lib file “lib_fixmath.a” to the directory below:

< WICED-Smart-SDK > /Wiced-Smart/spar

Step2: Edit the makefile.

Open the file “makefile” under the directory of < WICED-Smart-SDK > /Wiced-Smart/spar.

At around line 150, there is a block of commands which made the elf file. You should add the name of the static lib file at the end of the line which starts with $(XLD).  An example is shown as below.

  makefile.png

Figure 1  Edit the makefile

Step3: Verify your porting.

Copy the directory of “libfixmath_test” to the path below:

        <WICED-Smart-SDK>/Apps/RAM

At the Make Target panel, add a new target. Edit its name to the one below:

RAM.libfixmath_test-BCM920732TAG_Q32 download

Double click on the new-added target. After the application is running on the WICED Smart platform, by pressing the user button on the BCM20732S EVB, we could verify the output of the test application via a serial terminal. If the application runs safely and the calculation results proved to be right, then you have verified your porting.

An example of the output of the “libfixmath_test” application is as below:

test.png

Figure 2 Output of test program

From the test application we could found that, once we include the header file “fixmath.h” in our application, we are able to use the corresponding math functions freely as if they were originally part of the SDK.

2. Build libfixmath for WICED Smart SDK


Goals of this section

In this section, you will learn about the way to build this math functions lib, and you may customize your own math function lib in the same way.

Files required in this section:

libfixmath source directory.

Header files: libfixmath.h  fix16.h fract32.h int64.h uint32.h (5 in total)

      Source files: fix16.c fix16_exp.c fix16_sqrt.c fix16_str.c fix16_trig.c fract32.c uint32.c (totally 7)

      Makefile

Step1: Source code customization

      Source code modification:

The source code of the “libfixmath” lib is in the files of the “libfixmath source” directory mentioned above. However, sometimes we may need change the source code according our own requirement.

For example, in “fix16_str.c”, the libc function “isspace” and “isdigit” is used in the function “fix16_from_str” but the WICED Smart platform does not include these functions. Then you have to implement them in your own method.

Macros definition:

The libfixmath lib uses some macros as interfaces for the developers to customize the implementation of the lib files. The 7 macros are listed below:

#define FIXMATH_NO_CACHE

#define FIXMATH_SIN_LUT

#define FIXMATH_FAST_SIN

#define FIXMATH_NO_OVERFLOW

#define FIXMATH_NO_ROUNDING

#define FIXMATH_NO_64BIT

#define FIXMATH_OPTIMIZE_8BIT

Just as their name indicates, the macros are defined to enable or disable some properties of the math lib functions. For example, the definition of FIXMATH_NO_CACHE will lead to no cache used in the implementation of some complex math functions such as exp(x) or sin(x).

The definition of the macros should be done in fixmath.h or fix16.h.

Step2: Generate the math function lib file.

Generate the .o object files:

  • Copy all the 5 header files and the 7 source files into an arbitrary application, “RAM.hello_sensor” for example. The path should be <WICED-Smart-SDK>/Apps/RAM/hello_sensor.
  • Open the “makefile.inc” in the application directory above and add the names of the 7 source files at the end of line 11 starting with “APP_SRC”. That means add the 7 .c files as source files of the application too. The modification should be similar to the shortcut below:

makefile2.png

Figure 3 Makefile to compile

  • Double click on the make target panel and make the hello_sensor application. You will find 7 .o files named after the 7 .c source files in the directory below:

<WICED-Smart-SDK>/build/ libfixmath_test-BCM920732TAG_Q32-rom-ram-Wiced-release

              Now you have the object files of your customized source code.

Generate the .a static lib file:

The process to generate the .a static lib file is actually to pack the 7 object files into a special one. Please follow the next steps to finish the lib file generation.

  • Backup the Makefile in the directory <WICED-Smart-SDK>. You should recover it later.
  • Copy the 7 object files and the “libfixmath source /Makefile” to the <WICED-Smart-SDK> and cover the original Makefile.
  • Open a command window and change the path to <WICED-Smart-SDK>. By enter the command “make”, “lib_fixmath.a” static lib file will be created at the same directory.  By enter the command “make lib_review”, you could see the content of “lib_fixmath.a”.

test2.pngFigure 4 Compile output

  • After the static lib file is successfully created, the original Makefile and the “hello_sensor” application should be recovered. You could also modify the Makefile as you like.

3. Functions Supported and Performance

The table below lists all the math functions that are supported in the pre-built math lib. For some time-consuming math functions (trigonometric functions, for example), time performance is tested. The performance data in the last column shows how much resource it costs to run the corresponding math functions in WICED Smart BCM920732 platform.

Function Name

Comment

Time cost(us)

fix16_to_int(fix16_t a)

Format conversion

fix16_from_int(int a)

fix16_to_str(fix16_t value, char *buf, int decimals);

13.5841

fix16_from_str(const char *buf);

14.1913

fix16_abs(fix16_t x)

Value adjust

fix16_floor(fix16_t x)

fix16_ceil(fix16_t x)

fix16_min(fix16_t x, fix16_t y)

fix16_max(fix16_t x, fix16_t y)

fix16_clamp(fix16_t x, fix16_t lo, fix16_t hi)

fix16_add(fix16_t a, fix16_t b)

Basic calculation

0.8148

fix16_sadd(fix16_t a, fix16_t b)

fix16_sub(fix16_t a, fix16_t b)

fix16_ssub(fix16_t a, fix16_t b)

fix16_mul(fix16_t inArg0, fix16_t inArg1)

2.0200

fix16_smul(fix16_t inArg0, fix16_t inArg1)

3.1018

fix16_div(fix16_t inArg0, fix16_t inArg1)

6.0079

fix16_sdiv(fix16_t inArg0, fix16_t inArg1)

7.1820

fix16_mod(fix16_t x, fix16_t y)

fix16_lerp8(fix16_t inArg0, fix16_t inArg1, uint8_t inFract)

Interpolation

fix16_lerp16(fix16_t inArg0, fix16_t inArg1, uint16_t inFract)

fix16_sin_parabola(fix16_t inAngle)

trigonometric

9.6740

fix16_sin(fix16_t inAngle)

14.0997

fix16_cos(fix16_t inAngle)

14.8114

fix16_tan(fix16_t inAngle)

34.6207

fix16_asin(fix16_t inValue)

Anti-trigonometric

33.8250

fix16_acos(fix16_t inValue)

35.0975

fix16_atan(fix16_t inValue)

17.3929

fix16_atan2(fix16_t inY, fix16_t inX)

16.3435

fix16_rad_to_deg(fix16_t radians)

R/d conversion

2.0632

fix16_deg_to_rad(fix16_t degrees)

2.0030

fix16_sqrt(fix16_t inValue)

Sqrt & sq

11.4745

fix16_sq(fix16_t x)

fix16_exp(fix16_t inValue)

Exp & log

20.9141

fix16_log(fix16_t inValue)

458.4487

fix16_log2(fix16_t x)

40.1307

fix16_slog2(fix16_t x)

39.8963

4 Replies
Anonymous
Not applicable

This is great! Excited to test this out.

0 Likes
ThYo_2228536
Level 3
Level 3
First like received First like given

Very nice! I found myself in needs for such library when integrating Linear arduino generated C code for SPI which requires float to cast value read from LTC2983 board with 24 bit digital temperature system.

Without fixed floating point the BLE needs to send data to the Phone to do the final conversion. For back and forth communciation between BLE and a PHONE i stronly recommend Protobuf from Google. There is a lirbary called nano proto buf which is very easy to transport typed structure over the air and make it very easy for the receiver to decode.

0 Likes
Anonymous
Not applicable

What are the "minor changes" needed to build this library for WICED Smart SDK 2.x.x ?

0 Likes
Anonymous
Not applicable

Makefile and directory structure may be different. for example:

there is  no <WICED-Smart-SDK>/Apps/RAM in SDK2.x.x.

No  code change in file .c or .h needed.

0 Likes