Author Topic: Using libraries on the MCU  (Read 1355 times)

0 Members and 1 Guest are viewing this topic.

Offline nForceTopic starter

  • Frequent Contributor
  • **
  • Posts: 393
  • Country: ee
Using libraries on the MCU
« on: November 03, 2018, 04:08:43 pm »
If I am writing a program in C for a microcontroller. Can I use libraries which are a standard for a personal computer, or there are some limitations?

Let's suppose I want to use the tan function, can I use math.h?

Thanks.
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26891
  • Country: nl
    • NCT Developments
Re: Using libraries on the MCU
« Reply #1 on: November 03, 2018, 04:13:26 pm »
If I am writing a program in C for a microcontroller. Can I use libraries which are a standard for a personal computer, or there are some limitations?

Let's suppose I want to use the tan function, can I use math.h?

Thanks.
You can but be aware that math operations are likely to performed in software without a dedicated floating point processor. This means that your controller will need enough flash to store the libraries and that the math operations will be considerably slower. All in all you can use math.h and you'll need to link the software floating point library.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 
The following users thanked this post: nForce

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: Using libraries on the MCU
« Reply #2 on: November 03, 2018, 06:09:09 pm »
Also things like malloc()/free()  may not be supported due to memory constrains.
Alex
 
The following users thanked this post: nForce

Offline hussamaldean

  • Supporter
  • ****
  • Posts: 266
  • Country: iq
Re: Using libraries on the MCU
« Reply #3 on: November 03, 2018, 07:06:04 pm »
or you could use piece of the library in your code to improve the performance and reduce the size needed since the whole library has to be compiled like what I do in 90% of my projects
 
The following users thanked this post: nForce

Offline richardman

  • Frequent Contributor
  • **
  • Posts: 427
  • Country: us
Re: Using libraries on the MCU
« Reply #4 on: November 03, 2018, 07:19:26 pm »
The short answer is "it depends on the compiler and the target MCU".

The long answer:

There are certain functions, e.g. tan in math.h that are part of the standard C library. So all conforming C compilers should support it. However, not all compilers are conforming, and sometimes even if it is, it may not support all functions, e.g. ones that depend on a file system (stdio.h) or heap (malloc/free) due to lack of SRAM.

Then there's a speed and size consideration for your MCU. An 8 bit AVR may not have enough space to support a floating point function in addition to your code.

The short short answer is, "maybe".
// richard http://imagecraft.com/
JumpStart C++ for Cortex (compiler/IDE/debugger): the fastest easiest way to get productive on Cortex-M.
Smart.IO: phone App for embedded systems with no app or wireless coding
 
The following users thanked this post: nForce

Online hans

  • Super Contributor
  • ***
  • Posts: 1637
  • Country: nl
Re: Using libraries on the MCU
« Reply #5 on: November 04, 2018, 07:01:24 am »
In C++ you could even consider using the STL library, but these will often use memory allocation. Memory allocation is often undesirable, so there are static STL libraries (3rd party) that are more tailored towards embedded.
Then again you may still want to consider not using C++ at all.

In C, you can use printf() to print serial data. But where will it appear? Some manufacturers will include, often part of the boilerplate code for a chip or compiler, a char redirection function that for example always spits out the data on the first UART of the chip. You could redirect this to another peripheral.. doesn't even have to be UART.

But does it make sense to use getch or sscanf? They are polling functions. Have never tried actually, but conceptually it doesn't make much sense to program that way on embedded. :-/O
In particular, I think many embedded compilers won't support the stdin/stdout/stderr file streams.
 
The following users thanked this post: nForce

Offline hli

  • Frequent Contributor
  • **
  • Posts: 255
  • Country: de
Re: Using libraries on the MCU
« Reply #6 on: November 04, 2018, 11:11:21 pm »
When using libraries, especially when they are thought more for Linux or other operation system, be wary of their non-stated requirements. Stdin / stdout have been mentioned, but anything with file access needs some porting. Also, most libraries working with time or date will probably rely on some operation system support - just for getting the timezone (or current date / time).
 

Offline TK

  • Super Contributor
  • ***
  • Posts: 1722
  • Country: us
  • I am a Systems Analyst who plays with Electronics
Re: Using libraries on the MCU
« Reply #7 on: November 04, 2018, 11:25:19 pm »
When you are programming in C or C++ for a microcontroller, you are using a compiler from the toolkit for a specific target (Cross compiler: runs on a host OS, generates code for a TARGET micro that is different from the host) microcontroller.  i.e. avrgcc when developing for AVR microcontrollers.  You just cannot use any library that is available on the host system because it contains code compiled for intel X86 architecture (mac os x, windows, linux on X86), not AVR.  If you check the documentation for avrgcc and tan() is available, then you can certainly use it because it is supported on the target architecture.
« Last Edit: November 06, 2018, 03:21:44 pm by TK »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf