Access to DSP instructions

Posted on
  • Sorry this is probably a bit niche, but I was wondering if anyone has looked at using the DSP instructions?
    I didn't even know the cortex-M4's had these instructions until I recently came across this audio library for the Teensy boards that uses them.

    I'd love to be able to do some experimenting with these for audio projects from within Espruino too but I'm guessing it would be quite a lot of work to expose the DSP instructions from with Espruino?

  • It is quite easy to expose such stuff as inline C code, anything that gcc compiler allows to compile including inline assembly can be used. You can also setup the compiler service on your computer to enable preprocessor or other compiler flags if needed

    see e.g. this https://gist.github.com/fanoush/9227640a869d78d69a799276dff0fb71 or a bit outdated (some patches no longer needed) guide here https://github.com/fanoush/ds-d6/wiki/Espruino-Inline-C

  • As @fanoush says really - Inline C (http://www.espruino.com/InlineC) should 'just work' for you (with the existing, online compiler if you're using official boards). In fact I believe you could do inline assembler inside the Inline C, which should actually work pretty nicely.

  • Brillant, thanks fanoush! Thats very helpful info on your wiki page and thank you for your work on the dsd6 watches too - I've got a fw of the basic models and experiment a bit with them with espruino too when I get the odd chance.

  • you could do inline assembler inside the Inline C, which should actually work pretty nicely

    yes it works, used it in floating point code to get/clear FP status register
    https://gist.github.com/fanoush/9227640a869d78d69a799276dff0fb71#file-espruino-fpu-softfp-inlinec-js-L78
    I also linked there some SDK code with more assembly examples

  • Thanks Gordan! Yes I still have trusty my Pico's wayback from the kickstarter which are M4's so I think
    should have the DSP instructions, I didn't realise I could just use inline C or even assembler to experiment with this and thought I need to get setup for building Espruino.
    Very exciting - I'll have to read upon the DSP instructions and look at the CMSIS library code and see if I can get something going - so much easier being able to do it within Espruino env itself!

  • Sorry one more question before I get too far along with this, on this page it warns that outputting at higher freq could max out the mcu, but Im not clear on what causes that limitation? If I wanted to generate say a sine wave with some modulation applied (say using DSP instructions to be as efficient as possible and using 16bit data which I think is what they operate on) could I still output 16bit at 44.1khz sample rate?

  • it warns that outputting at higher freq could max out the mcu, but Im not clear on what causes that limitation?

    The timers used for the Waveform are a mix of a single hardware timer, plus software to handle the scheduling of multiple tasks (eg multiple waveforms, digitalPulse, software PWM, etc) on that one timer. If you do too much stuff then the processor just spends all its time servicing the interrupt and no time executing anything else!

    If you're doing a sine wave then you could just write that to a buffer and use Waveform on that buffer without need for DSP instructions though?

    If you're looking at doing realtime audio output and you're doing Inline C, then I would say you should actually just make your C code access the PWM output hardware directly - an example of doing that from JS is at https://www.espruino.com/STM32+Peripherals.

    Actually executing your code at a fixed interval could be a little more problematic, but probably the easiest method right now is:

    • Use analogWrite(...,{freq:44100}) on one pin
    • Physically connect that pin to another pin
    • Use setWatch(your_inline_c_fn, other_pin, { irq:true, repeat:1})

    And that should then be able to execute your function 441000 times a second without JS ever getting involved

  • Brill, thanks Gordan!
    Sorry I should have explained more about what I wanted to do.
    What I had in mind was making a synthesiser-sampler-sequencer, along the lines of this and this
    , though on a much more modest scale.

    Both those projects use the Teensy and its audio library and as the Teensy mcu is a M4 as well I had the idea of doing something along the same lines using Espruino as well.
    So thats why I was mentioning starting with a sine wav (but also square, tooth, etc) and then applying modulations to them to create sounds.

    Thanks again for all the info that you and fanoush replied with, I think I have a good idea now of how to get started experimenting with this.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Access to DSP instructions

Posted by Avatar for mks @mks

Actions