Generating Frequency

Writing software that inputs and/or outputs audio and performs DSP algorithms such as filters, new codecs or audio effects.
Sizzle
User
Posts: 3
Joined: Fri 2019-11-01 12:22

Generating Frequency

Post by Sizzle »

Hello all i got a device named VS1053 Mp3 Shield for arduino.Now i have to generate a sine wave(beep tone)having a frequecny of 1-10khz on of its pins .This device is totally new for me .Does anybody know how generate my desired signal(frequency)on one of its pins.
Thanks
User avatar
pasi
VLSI Staff
Posts: 2006
Joined: Thu 2010-07-15 16:04

Re: Generating Frequency

Post by pasi »

Can it be analog output? There is a built-in sine test, so with a few SCI writes you can get that running on the analog outputs.

Must it be other than analog outputs? Can it be any pin?

Some possibilities:
- I2S - configure I2S to 48kHz, configure GPIO4 (I2S_LROUT) as output, it will produce 24kHz signal which you can divide with an externally.
- UART - set a suitable UART speed, and repeatedly transmit a suitable data value (e.g. 0x55 is transmitted LSb first and with start and stop bits as "0101010101", 0xf0 as "0000011111").
- GPIO pin toggled with a timer interrupt.

The latter two require some VSDSP programming and uploading the code through SCI. The first requires an external divider to get to 12kHz.
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook
Sizzle
User
Posts: 3
Joined: Fri 2019-11-01 12:22

Re: Generating Frequency

Post by Sizzle »

Thanks for the response .Yes the output should be an audio.It could be on any pin.How to get this using arduino?
Sizzle
User
Posts: 3
Joined: Fri 2019-11-01 12:22

Re: Generating Frequency

Post by Sizzle »

So what actually the sine test do?Can we generate the frequency of our own choice?
Hannu
Senior User
Posts: 396
Joined: Mon 2016-05-30 11:54
Location: Finland
Contact:

Re: Generating Frequency

Post by Hannu »

If I tracked down to right Arduino library, https://github.com/madsci1016/Sparkfun- ... no-Library

uint8_t SFEMP3Shield::enableTestSineWave(uint8_t freq)

function is to enable the sine test. It uses the old sine test.
Have a look at chapter 10.12.1 of the VS1053 datasheet http://www.vlsi.fi/fileadmin/datasheets/vs1053.pdf how the single byte is calculated to be the wanted frequency. For more accurate frequency you might want to use the new sine test, but I think the old version is good enough.
Arkantium
User
Posts: 9
Joined: Tue 2022-06-07 16:32

Re: Generating Frequency

Post by Arkantium »

Good afternoon,

I am trying to generate frecuencies using "SINE.dl3" and it seems to me that something in the code must be wrong, because if I try to open it with VSIDE and I just press the "Build Solution" button, without changing nothing, it stops working when uploaded to my mk2 board.

In the log I can see that messages when building the solution:

Build started. Project: Sine, configuration: Emulation-Debug

lcc -g -hhw_desc -O6 -fsmall-code -DDEBUG -Iinclude -IC:\Program\ Files\ (x86)\VSIDE\libvs1005h_vsos3 -o Emulation-Debug\main.o C:\Users\gaspar.jose\Desktop\Sine\main.c

C:\Users\Desktop\Sine\main.c (151) Warning 153: a type cast into a shorter type
C:\Users\Desktop\Sine\main.c (231) Warning 153: a type cast into a shorter type
C:\Users\Desktop\Sine\main.c (232) Warning 153: a type cast into a shorter type
optimizing (1661): 1258 1222 1216 1212 1211.1211.1195.1191.1189.1189.1189
Successfully compiled 10902 lines (284 in source) with 3 warnings.
C 1189 CF 0 X 1073 Y 544 F 0
voplinkg -k -m mem_desc_app03.mem C:\Program\ Files\ (x86)\VSIDE/libvs1005h_vsos3/vsos03.o Emulation-Debug\main.o -o Emulation-Debug\MyProject.coff -L. -Llib -LC:\Program\ Files\ (x86)\VSIDE\libvs1005h_vsos3 -lvsos03 -lc -lgeneral -lrtossmall


Total words: I 2209, X 1074, Y 544.
copy loadable.ap3 Sine.dl3 /y
1 archive(s) copied(s).

I do not know why is this happening so please, I appreciate any help.

My idea is just to generate a beep using SINE, is that possible? (after repairing SINE code)

Many thanks

Kind Regards.
Hannu
Senior User
Posts: 396
Joined: Mon 2016-05-30 11:54
Location: Finland
Contact:

Re: Generating Frequency

Post by Hannu »

Maybe you don't have paramspl installed on your board? The Sine was built just fine.

If I remember correctly I wrote a sine generator (good for beeps) in getting started VS1005 application note in 2016. After all Sine.dl3 is huge when compared to simple sine generator.

Another some test program which loops forever and copies from stdaudioin to stdaudioout or beeps with sine. If exit is required, remember remove the cyclic node. The strcpy() is needed, because paramspl ruins the params string.

Code: Select all

#include <vo_stdio.h>
#include <volink.h>     // Linker directives like DLLENTRY
#include <apploader.h>  // RunLibraryFunction etc
#include <kernel.h>             // Kernel symbols
#include <consolestate.h> // appFlags etc
#include <vo_gpio.h>
#include <cyclic.h>
#include <string.h>

#define BUTTON 0x00
s_int32 sampleBuf[32];
void ButtonCyclic(register struct CyclicNode *cyclicNode) {
        if (GpioReadPin(BUTTON)) {
                appFlags |= APP_FLAG_QUIT;
        }
}
struct CyclicNode buttonNode = {{0}, ButtonCyclic};
ioresult main(char *parameters) {
        AddCyclic(&buttonNode, TICKS_PER_SEC/50, TICKS_PER_SEC/50);
        while(1) {
                if (GpioReadPin(BUTTON) == 0) {
                        strcpy((char *)sampleBuf, "1001.2345 -0.1 -0.1");
                        RunProgram("sine", (char *) sampleBuf);
                        printf("Line in\n");
                }
                appFlags &= ~APP_FLAG_QUIT;
                fread(sampleBuf, sizeof(sampleBuf[0]), sizeof(sampleBuf)/sizeof(sampleBuf[0]), stdaudioin);
                fwrite(sampleBuf, sizeof(sampleBuf[0]), sizeof(sampleBuf)/sizeof(sampleBuf[0]), stdaudioout);
        }
        return S_OK;
}
Arkantium
User
Posts: 9
Joined: Tue 2022-06-07 16:32

Re: Generating Frequency

Post by Arkantium »

Good afternoon Hannu and thanks a lot for your quick response.

I checked my SYS folder and "paramspl.dl3" is there, so I do not know if something is wrong with the code.
What is happening is:

If I just copy your Sine.dl3 and caall it from config.txt is working fine, but, if I open it with VSIDE and just build the solution, without touching nothing, the warnings I told you before appears and it never works again. I realized of that because I was trying to add code to Sine solution and it was not working but After several tests I can confirm that this is stopping working even writing nothing, with any modification of code, just building it in VSIDE.

Unfortunately I am not a software expert but I am trying my best, and if I am write, analysing your code I see that is made to generate beeps using pins, am I wright? I would like to buid a generator without using anything, I mean, just start beeping when power on and stop when power off.
I would like to use this function, we could say, as a beacon, getting a beep of a certain frecuency every x seconds.

Following your example code, I tried to substitute button and even tried to avoid cyclic node to stop it but I can't. The code is as follows:

#include <vo_stdio.h>
#include <volink.h> // Linker directives like DLLENTRY
#include <apploader.h> // RunLibraryFunction etc
#include <kernel.h> // Kernel symbols
#include <consolestate.h> // appFlags etc
#include <vo_gpio.h>
#include <cyclic.h>
#include <string.h>

//#define BUTTON 0x00
s_int32 sampleBuf[32];
int i;
//void ButtonCyclic(register struct CyclicNode *cyclicNode) {
// if (GpioReadPin(BUTTON)) {
// appFlags |= APP_FLAG_QUIT;
// }
//}
//struct CyclicNode buttonNode = {{0}, ButtonCyclic};
ioresult main(char *parameters) {
// AddCyclic(&buttonNode, TICKS_PER_SEC/50, TICKS_PER_SEC/50);
// while(1) {
if (i<5)
{
strcpy((char *)sampleBuf, "1001.2345 -6 -6");
RunProgram("sine", (char *)sampleBuf);
printf("Line in\n");
Delay(2000);
strcpy((char *)sampleBuf, "1001.2345 1 1");
RunProgram("sine", (char *)sampleBuf);
printf("Line in\n");
Delay(2000);
i=i++;
printf("i is: %d\n",i);
}
appFlags &= ~APP_FLAG_QUIT;
fread(sampleBuf, sizeof(sampleBuf[0]), sizeof(sampleBuf)/sizeof(sampleBuf[0]), stdaudioin);
fwrite(sampleBuf, sizeof(sampleBuf[0]), sizeof(sampleBuf)/sizeof(sampleBuf[0]), stdaudioout);
// }
return S_OK;
}


My idea was to Run Sine with a volume and then, wait 2 seconds to generate a mute, trying to get a beep every 2 seconds, but I can never stop the first sine.

Again, any help would be really appreciated.

Many thanks.

Kind regards.
Hannu
Senior User
Posts: 396
Joined: Mon 2016-05-30 11:54
Location: Finland
Contact:

Re: Generating Frequency

Post by Hannu »

I wrote something for you. Hope this helps. It has one kind of fade out (a cheap one) so it doesn't hurt to ears. It can be disabled by commenting out the USE_FADE define.

The beeper works with with 16 and 32-bit and probably all sample rates. I didn't tested thoroughly.

This program however keeps your control while beeping, but it returns eventually. And only 50/50 tone/silence is available.

Code: Select all

S:>beeper -h
Usage: Beeper [-h] freq duration attenuation repeat
	freq	Set frequency to freq Hz
	duration	Beep duration in seconds
	attenuation	How many decibels to attenuate signal
	repeat	How many times beep repeats
S:>beeper 2553.45 1.2 -12 2
'The reason why your program didn't work is simple. The Sine program continues execution until appflags has APP_FLAG_QUIT bit high. And there is nothing going to do that. To return from sine, you would need to send CTRL-C to console.
Attachments
beeper.zip
Beeping beeper with floating point setup
(73.2 KiB) Downloaded 18 times
Arkantium
User
Posts: 9
Joined: Tue 2022-06-07 16:32

Re: Generating Frequency

Post by Arkantium »

Good morning Hannu,

Thanks a lot for your beeper code. I have been testing it and definetely I need to setup different duration for beeps and silences, and have selectable frequencies for them so thats why I am using this code from app notes:

#include <vo_stdio.h>
#include <stdlib.h>
#include <apploader.h> // Contains LoadLibrary() and DropLibrary()
#include <kernel.h> // Contains functions exported from kernel, like CoarseSine()

#define BUFSIZE 128 // Output buffer size
#define SIN_ATT 1 // Attenuation by shifting this many bits right
// Sample rate / buffer size = iteration count of while loop / second
#define WHILE_ITERATION 375 // one second

int main(void) {
// Remember to never allocate buffers from stack space. So, if you
// allocate the space inside your function, never forget " static "!
static s_int16 myBuf[2*BUFSIZE];
s_int16 *p;
u_int16 ph1 = 0, ph2 = 0, i , duration = WHILE_ITERATION;


printf("\nTone On\n");

while (duration--) {
// By default both input and output are 16-bit stereo at 48 kHz.
p = myBuf;
for( i = 0; i < BUFSIZE; i++){
// Left channel
// First Sine wave
// CoarseSine() isn’t HI-FI Sine wave. But for beeps
// it is good enough and fast.
*p = CoarseSine(ph1) >> (SIN_ATT + 1);
// 65536 / 410 = 159 number of samples for one period.
// Output frequency 48000/159 = 301.88Hz
ph1+= 2730;
// Second sine wave
// 65536 / 2730 = 24 number of samples for one period.
// Output frequency 48000/24 = 2kHz
p[0] += CoarseSine(ph2) >> (SIN_ATT + 1);
ph2 += 2730;
// Copy left channel to right channel.
p[1] = p [0];
// Advance p to next sample.
p += 2;
}
// Write stereo samples from myBuf into stdaudioout.
// By default, stdaudioout goes to line out.
fwrite (myBuf, sizeof(s_int16), 2*BUFSIZE, stdaudioout);
} /* while (duration--) */

Delay(200);
printf("\nTone Off\n");


//return EXIT_SUCCESS;
}


But I am facing the last problem, which is that I need to use both functions at the same time, I mean "Rec" and this beep generator.
Up to this moment I have been testing this piece of code I shared inside "Rec" code (as part of the main loop at first and out of it after that), but the only things I get are:
- One beep and then stacked.
or
- A cycle of to codes one after the other: Beep, Rec, Beep, Rec... but never at the same time.

Do you think it is possible to mix them to make them work at the same time?

This would be very important for us, and as always, any help would be really appreciated.

Many thanks,

Kind regards.
Post Reply