2

How can I use ESP8266 library pwm.h? If I just include the library in my arduino project, I get an error: undefined reference to pwm_init

I need a 40khz sine wave for ultrasonic sensor and analogWrite works bad at high frequencies.

Sorry, if the answer is obvious, I have been googling the problem for a few hours and I can't find anything useful.

EDIT

I think this is the correct pwm.c file. Where do I have to put it? I tried adding just this file to my project but it doesn't work. I guess it has dependencies to other files in that library. But how do I even add C library to arduino project? And why it is not already included in ESP8266 core library if there is a header pwm.h?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
  • 1
    you most probably have to add some lib (like maybe "libpwm.lib") to your project as well. – tofro Sep 22 '16 at 11:50
  • 1
    In C (and C++, but to a lesser extent) headers don't contain the actual code, just the *interface* that tells you how a particular module or library works, what functions it has and what data types and so on. So you must have the code, either as a source file or a pre-compiled library, too. – unwind Sep 22 '16 at 11:52
  • possible duplicate https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Galik Sep 22 '16 at 11:55
  • I understand that header doesn't contain code, but I am asking more specifically to pwm library. Where do I have to put pwm.c file? I think this is the correct [pwm.c file](https://github.com/mattcallow/esp8266-sdk/tree/master/apps/07switch/driver) I think this is the correct . I tried adding just this file to my project but it doesn't work. I guess it has dependencies to other files in that library. But how do I even add C library to arduino project? And why it is not already included in ESP8266 core library if there is a header pwm.h? –  Sep 22 '16 at 12:10
  • You want to generate a 40kHz sine through PWM? What PWM frequency do you need then? – JimmyB Sep 25 '16 at 12:14

1 Answers1

0

The libpwm.a is part of ESP8266 SDK, if you installed the ESP8266 Addon With the Arduino Boards Manager, this library should be in :

.arduino15/packages/esp8266/hardware/esp8266/<SDK Version>/tools/sdk/lib/libpwm.a

In order to use PWM, you should include in your sketch pwm.h forcing C mangling :

extern "C" {
  #include "pwm.h" 
}

Then in order to link with libpwm.a you should customize the link command creating a custom plateform configuration file .arduino15/packages/esp8266/hardware/esp8266/<SDK Version>/platform.local.txt, adding

compiler.c.elf.extra_flags=-lpwm

You can also add this line in the plateform configuration file .arduino15/packages/esp8266/hardware/esp8266/<SDK Version>/platform.txt

mpromonet
  • 11,326
  • 43
  • 62
  • 91
  • I don't know how to tell compiler to use custom config file. So I just added this line to file platform.txt and i still get the same error. I have found file libpwm.a in the folder you mentioned. –  Oct 12 '16 at 15:19