PWM hardware fade

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
nagylzs
Posts: 40
Joined: Sat Jan 19, 2019 8:01 pm

PWM hardware fade

Post by nagylzs » Thu Dec 05, 2019 7:24 pm

Hello! The ESP32 has hardware support for gradually changing the PWM duty cycle.

Documented here: https://docs.espressif.com/projects/esp ... /ledc.html

I would like to use this. I need to run a web server on the ESP32 (accepting and processing commands), and gradually change the PWM duty cycle of at least 6 channels at the same time.

Can I do this in MicroPython? Is this feature available? I did not find it in the description of the machine.PWM class. The documentation only shows how to do fading from software:

https://docs.micropython.org/en/latest/ ... ing-an-led

but this is inscufficient in some cases. While I do some CPU intensive calculations, the fading stops completely.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: PWM hardware fade

Post by jimmo » Fri Dec 06, 2019 3:03 am

Unfortunately the ledc_*fade functions are not exposed to the Python API. It would be really cool to support this though.

I wonder if rather than implementing this as part of the Python machine.PWM API, it might make sense to implement this as esp32.LEDControl as it does seem like that would be quite esp32-specific.

If you're interested in implementing this, feel free to ask questions here and there are many people who can help.

nagylzs
Posts: 40
Joined: Sat Jan 19, 2019 8:01 pm

Re: PWM hardware fade

Post by nagylzs » Tue Dec 17, 2019 4:12 pm

Can you please point me to the right direction? I think I need to clone the micropython project from GitHub and then try to compile a new firmware. I was trying to follow the instructions in https://github.com/micropython/micropyt ... /README.md.

This is what I could do:

Code: Select all

git clone https://github.com/micropython/micropython.git
cd micropython
cd mpy-cross
make
cd ../ports/unix
make submodules
Then I do `make` and I get this:

Code: Select all

CC modtermios.c
CC modusocket.c
CC modffi.c
CC ../../lib/mp-readline/readline.c
CC ../../lib/timeutils/timeutils.c
LINK micropython
build/modffi.o: In function `char2ffi_type':
modffi.c:(.text.char2ffi_type+0x20): undefined reference to `ffi_type_uint8'
modffi.c:(.text.char2ffi_type+0x28): undefined reference to `ffi_type_sint16'
modffi.c:(.text.char2ffi_type+0x30): undefined reference to `ffi_type_uint16'
modffi.c:(.text.char2ffi_type+0x38): undefined reference to `ffi_type_sint32'
modffi.c:(.text.char2ffi_type+0x40): undefined reference to `ffi_type_uint32'
modffi.c:(.text.char2ffi_type+0x48): undefined reference to `ffi_type_sint64'
modffi.c:(.text.char2ffi_type+0x50): undefined reference to `ffi_type_uint64'
modffi.c:(.text.char2ffi_type+0x58): undefined reference to `ffi_type_float'
modffi.c:(.text.char2ffi_type+0x60): undefined reference to `ffi_type_double'
modffi.c:(.text.char2ffi_type+0x68): undefined reference to `ffi_type_pointer'
modffi.c:(.text.char2ffi_type+0x70): undefined reference to `ffi_type_void'
modffi.c:(.text.char2ffi_type+0x7b): undefined reference to `ffi_type_sint8'
build/modffi.o: In function `make_func':
modffi.c:(.text.make_func+0xc1): undefined reference to `ffi_prep_cif'
build/modffi.o: In function `ffifunc_call':
modffi.c:(.text.ffifunc_call+0x163): undefined reference to `ffi_call'
build/modffi.o: In function `mod_ffi_callback':
modffi.c:(.text.mod_ffi_callback+0xc5): undefined reference to `ffi_prep_cif'
modffi.c:(.text.mod_ffi_callback+0xec): undefined reference to `ffi_prep_closure_loc'
collect2: error: ld returned 1 exit status
../../py/mkrules.mk:145: recipe for target 'micropython' failed
make: *** [micropython] Error 1

nagylzs
Posts: 40
Joined: Sat Jan 19, 2019 8:01 pm

Re: PWM hardware fade

Post by nagylzs » Tue Dec 17, 2019 4:17 pm

Might be related to this:

viewtopic.php?t=517

After doing

sudo apt install libffi-dev

it still fails. After setting `MICROPY_PY_FFI = 0` in mpconfigport.mk I can compile it, but I'm not sure what the implications are.

nagylzs
Posts: 40
Joined: Sat Jan 19, 2019 8:01 pm

Re: PWM hardware fade

Post by nagylzs » Tue Dec 17, 2019 5:44 pm

All right, I could install the toolchain, compile a new firmware and burn it. It seems to be working.

Next step will be to examine the C code for PWM and Timer, make changes and test it.

Thanks

nagylzs
Posts: 40
Joined: Sat Jan 19, 2019 8:01 pm

Re: PWM hardware fade

Post by nagylzs » Tue Dec 17, 2019 6:07 pm

Can you please tell me how to start a new empty module in this project? I was reading this documentation about user modules:

https://github.com/nagylzs/micropython/ ... odules.rst

Should I implement this as a user module, or should I try to put it under micropython/ ?

I have already forked micropython, but I don't know how to create a new module as part of the project (e.g. not a user module).

My first bet would be to add it under extmod

nagylzs
Posts: 40
Joined: Sat Jan 19, 2019 8:01 pm

Re: PWM hardware fade

Post by nagylzs » Tue Dec 17, 2019 7:55 pm

I could create a custom C module that fades a led with the LEDC driver. So maybe I'm going to create it as a custom module, and test it before I try to put it into the core micropython repo.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: PWM hardware fade

Post by jimmo » Wed Dec 18, 2019 2:26 am

Here's an example of a very similar piece of work (adding a ESP32 hardware peripheral, in this case RMT). https://github.com/micropython/micropython/pull/5184

That PR adds a new class "RMT" to the existing esp32 module. I imagine you want to do something similar.

nagylzs
Posts: 40
Joined: Sat Jan 19, 2019 8:01 pm

Re: PWM hardware fade

Post by nagylzs » Wed Dec 18, 2019 6:29 am

That helps, thank you!

Post Reply