Current Lua downloadable firmware will be posted here

User avatar
By AdrianM
#72857 I'm making calls to the PCM firmware module to synthesise audio and drive a loudspeaker. It works great but the PCM operates at 4MHz which is a bit too fast for my particular output driver (H-Bridge driver). The Sigma Delta firmware module on the other hand generates PWM at a fixed 312 kHz frequency which would be ideal but it's not possible to "feed" it at audio frequencies from Lua.

I've been staring at the C sources trying to figure out how the PCM firmware module sets up the Sigma Delta hardware but just can't see it. If a new PCM object is created with the pcm.SD argument, it seems to check for the sigma_delta module:

Code: Select allif (driver == PCM_DRIVER_SD) {
    cfg->pin = luaL_checkinteger( L, 2 );
    MOD_CHECK_ID(sigma_delta, cfg->pin);


but I'm left with many questions - some simple such as what dependencies there are when using the two firmware modules. I can build the firmware without the Sigma Delta module yet the PCM module works OK. I hope someone is able to clarify and possibly give some tips on how it might be possible to change the base frequency of the PCM
User avatar
By devsaurus
#72941
AdrianM wrote:I'm making calls to the PCM firmware module to synthesise audio and drive a loudspeaker. It works great but the PCM operates at 4MHz which is a bit too fast for my particular output driver (H-Bridge driver).

Do you intend to feed the raw output from the esp pin into your driver? That's not how the PCM output is supposed to be used. You need to filter the high-frequency parts to get a suitable audio spectrum. This is the job of an external circuit as described in http://nodemcu.readthedocs.io/en/dev/en ... a-hardware.

AdrianM wrote:The Sigma Delta firmware module on the other hand generates PWM at a fixed 312 kHz frequency which would be ideal but it's not possible to "feed" it at audio frequencies from Lua.

Correct, that's why the pcm module exists. It feeds the sigma-delta hardware in sufficient real-time.

AdrianM wrote:but I'm left with many questions - some simple such as what dependencies there are when using the two firmware modules. I can build the firmware without the Sigma Delta module yet the PCM module works OK.

The PCM module doesn't depend on the sigma-delta module. It will configure and operate the sigma-delta hardware on it's own (and I never tried to interfere with the sigma-delta module).
The sigma-delta module on the other hand provides direct access to the hardware. Think of it as a hardware device driver while the PCM module is an application built on top of the hardware.
User avatar
By AdrianM
#73663
devsaurus wrote:Do you intend to feed the raw output from the esp pin into your driver? That's not how the PCM output is supposed to be used. You need to filter the high-frequency parts to get a suitable audio spectrum. This is the job of an external circuit as described in http://nodemcu.readthedocs.io/en/dev/en ... -hardware..

It's actually perfectly OK (indeed beneficial) to maintain a digital signal path all the way to the speaker! The inductance of the speaker coil does a fine job of rejecting the HF carrier and reacting only to audible frequencies in the carrier. The only problem I had with the PCM module used in this way was that the Mosfet H-Bridge driver chip I used to drive the speaker tops-out at 1MHz.

Fortunately it was easy to track down the fixed prescale value of 9 and change to for something that would keep me under 1MHz. 52 got me down to 750KHz which works just fine.

drv_sigma_delta.c
Code: Select all  platform_sigma_delta_set_prescale( 52 );   //WAS 9(4MHz) 52 = 750K (127=312.5 kHz but "birdies" present)


You will notice that this is potentially problematic at extreme PCM values as the carrier frequency falls towards the audio range - however this coincides with rail-rail excursions of the intended audio which in practise does not reach the extremes. I considered issuing a PR for an enhancement to the module to make the prescale adjustable but I'm finding git a bit intimidating.