Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ringbuf_pop always returns false #4

Open
biemster opened this issue Dec 1, 2022 · 5 comments
Open

ringbuf_pop always returns false #4

biemster opened this issue Dec 1, 2022 · 5 comments

Comments

@biemster
Copy link

biemster commented Dec 1, 2022

This one's a doozy.
I use your (great!) machine_i2s.c with the C-SDK like this:

#include "pico/stdlib.h"
#include "machine_i2s.c"

#define SCK 3
#define WS 4 // needs to be SCK +1
#define SD 29
#define BPS 32 // 24 is not valid in this implementation, but INMP441 outputs 24 bits samples
#define RATE 16000

int main() {
	stdio_init_all();
	machine_i2s_obj_t* i2s0 = machine_i2s_make_new(0, SCK, WS, SD, RX, BPS, STEREO, /*ringbuf_len*/SIZEOF_DMA_BUFFER_IN_BYTES, RATE);
	int32_t buffer[I2S_RX_FRAME_SIZE_IN_BYTES /4];
	while (true) {
		machine_i2s_stream_read(i2s0, (void*)&buffer[0], I2S_RX_FRAME_SIZE_IN_BYTES);
		printf("%.6x %.6x\n", buffer[0], buffer[1]);			
	}
}

but I don't get any samples out without the most peculiar change:
if I add on https://github.com/sfera-labs/arduino-pico-i2s-audio/blob/master/src/machine_i2s.c#L232 the following:

printf("%d %d\n", rbuf->head,rbuf->tail);

it suddenly starts spewing out correct frames! So it seems to me that the printf statement somehow materializes the head and tail values, but my knowledge of the pico and or C is not nearly enough to start to understand this.

@biemster
Copy link
Author

biemster commented Dec 1, 2022

Maybe I hit something like this raspberrypi/pico-sdk#971?

@biemster
Copy link
Author

biemster commented Dec 1, 2022

Printing some random stuff already fixes the issue, it does not have to involve head or tail.

@biemster
Copy link
Author

biemster commented Dec 1, 2022

I hacked my way around it by adding a stdio_flush() on line 971, but I'd love to find a better solution here..

@giampiero7
Copy link
Member

Hi @biemster ,
Things like this are usually caused by some timing issue. E.g. your call to printf() slows things down just enough to make the right thing happen at the right moment. Maybe a sleep call will produce the same effect.

This library is no longer updated, the efforts have been moved to the I2S library of the arduino-pico project:
https://github.com/earlephilhower/arduino-pico/tree/master/libraries/I2S
which starts from the same foundations and includes fixes and improvements.

So I'd strongly recommend to use that as your starting point.

Alternatively, you may want to have a look at the original MicroPython code to see if there's any update there:
https://github.com/micropython/micropython/blob/master/ports/rp2/machine_i2s.c

Good luck!

@biemster
Copy link
Author

biemster commented Dec 1, 2022

Thanks @giampiero7 for your answer. I started of from the arduino-pico I2s library, but got stuck: https://github.com/biemster/pico-I2S, probably at the ping-pong DMA stuff (not my forte). I looked at the original MicroPython code as well indeed, but I don't see any relevant changes in that area, so that might have the bug as well..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants