multicore working barely

This commit is contained in:
Sebastian
2025-06-22 14:30:09 +02:00
parent b4f504b21d
commit b9130dc0ae
2 changed files with 337 additions and 288 deletions

View File

@ -53,5 +53,5 @@ class RingBuffer{
int counter = 0; int counter = 0;
int write = 0; int write = 0;
int read = 0; int read = 0;
T *buffer; volatile T *buffer;
}; };

View File

@ -14,6 +14,7 @@
#include "wavestream.h" #include "wavestream.h"
bool core1_separate_stack = true; bool core1_separate_stack = true;
bool core1_disable_systick = true;
#define HAPTIC 1 #define HAPTIC 1
#define AURAL 1 #define AURAL 1
@ -62,10 +63,14 @@ int32_t position = 0;
CRGB ui_leds[74]; CRGB ui_leds[74];
CRGB edge_leds[11]; CRGB edge_leds[11];
volatile bool buttonChanged = false; volatile bool setup0_finished = false;
volatile bool setup1_finished = false;
bool ui_click = false; volatile bool buttonChanged = false;
bool ui_beep = false; volatile bool sdInitialized = false;
volatile bool ui_click = false;
volatile bool ui_beep = false;
bool amp = false; bool amp = false;
volatile bool streams_loaded = false; volatile bool streams_loaded = false;
bool speakerToggle = false; bool speakerToggle = false;
@ -318,30 +323,41 @@ struct Vibration{
}; };
Vibration vibration; Vibration vibration;
// -------------------------------------------- SETUP 0
void setup() { void setup() {
Serial.begin(); Serial.begin();
delay(1000); delay(500);
i2s.setSysClk(48000);
i2s.onTransmit(codec_transmit);
i2s.onReceive(codec_receive);
i2s.setDOUT(15);
i2s.setDIN(14);
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
i2s.setMCLK(18);
i2s.setMCLKmult(512); // 256 = 12.288.000Hz 512 = 25Mhz
i2s.swapClocks();
i2s.setFrequency(48000); i2s.setFrequency(48000);
i2s.setBitsPerSample(16);
pinMode(19, OUTPUT); // MCLK enable i2s.setBuffers(6, BUFFERSIZE * sizeof(int16_t) / sizeof(uint32_t));
digitalWrite(19, HIGH); // enable MCLK
pinMode(20, OUTPUT); // CODEC reset (enable)
digitalWrite(20, HIGH);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT); // speaker enable l
pinMode(13, OUTPUT); // speaker enable r
speaker(false); speaker(false);
pinMode(21, INPUT_PULLUP); pinMode(21, INPUT_PULLUP);
sd_card_detected = !digitalRead(21); sd_card_detected = !digitalRead(21);
delay(500); delay(500);
bool sdInitialized = SD.begin(22, 23, 24); while(!sdInitialized){
delay(100); sdInitialized = SD.begin(22, 23, 24);
if(!sdInitialized) sdInitialized = SD.begin(22, 23, 24); // hack to prevent SD card from not initializing after soft reset delay(250);
Serial.println("0: Initializing SD Card");
}
if(sdInitialized) loadConfiguration(config); if(sdInitialized) loadConfiguration(config);
@ -349,34 +365,9 @@ void setup() {
load_ui_sounds("/ui/click.wav", ui_click_snd, click_length); load_ui_sounds("/ui/click.wav", ui_click_snd, click_length);
load_ui_sounds("/ui/beep.wav", ui_beep_snd, beep_length); load_ui_sounds("/ui/beep.wav", ui_beep_snd, beep_length);
} }
config_loaded = true; config_loaded = true;
pinMode(6, OUTPUT); // Vibration Motor
pinMode(7, OUTPUT); // UI Amp Enable
ui_snd.onTransmit(pwm_audio_callback);
ui_snd.begin(UI_SAMPLERATE);
digitalWrite(7, LOW); // UI amp off
while(!codec_ready){
delay(5);
}
//i2s.onTransmit(codec_transmit);
//i2s.onReceive(codec_receive);
i2s.setDOUT(15);
i2s.setDIN(14);
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
i2s.setMCLK(18);
i2s.setMCLKmult(512); // 256 = 12.288.000Hz 512 = 25Mhz
i2s.swapClocks();
i2s.setBitsPerSample(16);
//i2s.setBuffers(6, BUFFERSIZE * sizeof(int16_t) / sizeof(uint32_t));
if(sdInitialized) { if(sdInitialized) {
streams_loaded = load_samples(); streams_loaded = load_samples();
} }
@ -387,11 +378,19 @@ void setup() {
} }
} }
while(!codec_ready){
delay(250);
Serial.println("0: Waiting for codec");
}
if(!i2s.begin(48000)){ if(!i2s.begin(48000)){
Serial.println("I2S error!"); Serial.println("0: I2S error!");
while(100); while(100);
} }
Serial.println("I2S OK"); Serial.println("0: I2S OK");
Serial.print("0: STARTUP COMPLETE");
digitalWrite(6, HIGH); digitalWrite(6, HIGH);
delay(25); delay(25);
@ -400,47 +399,84 @@ void setup() {
digitalWrite(6, HIGH); digitalWrite(6, HIGH);
delay(25); delay(25);
digitalWrite(6, LOW); digitalWrite(6, LOW);
setup0_finished = true;
} }
void setup1(){ void setup1(){
while(!config_loaded){ Serial.print("1: STARTUP");
delay(5);
}
Serial.print("1: INIT LEDS: ");
FastLED.addLeds<NEOPIXEL, 4>(edge_leds, 11);
FastLED.addLeds<NEOPIXEL, 5>(ui_leds, 74);
Serial.println("SUCCESS");
Serial.print("1: INIT SPI: ");
SPI1.setSCK(10); SPI1.setSCK(10);
SPI1.setTX(11); SPI1.setTX(11);
SPI1.begin(); SPI1.begin();
Serial.println("SUCCESS");
Serial.print("1: INIT DAC: ");
dac.begin(); dac.begin();
Serial.println("SUCCESS");
FastLED.addLeds<NEOPIXEL, 4>(edge_leds, 11); Serial.print("1: INIT WIRE: ");
FastLED.addLeds<NEOPIXEL, 5>(ui_leds, 74);
Serial.println("INIT WIRE");
Wire1.setSDA(2); Wire1.setSDA(2);
Wire1.setSCL(3); Wire1.setSCL(3);
Wire1.begin(); Wire1.begin();
Serial.println("SUCCESS"); Serial.println("SUCCESS");
delay(100);
Serial.println("INIT TCA"); Serial.print("1: INIT TCA: ");
TCA.begin(); TCA.begin();
TCA.pinMode16(0xFFFF); TCA.pinMode16(0xFFFF);
TCA.setPolarity16(0x0000); TCA.setPolarity16(0x0000);
Serial.println("SUCCESS"); Serial.println(" SUCCESS");
Serial.println("INIT INTERRUPT"); Serial.print("1: INIT TCA INTERRUPT: ");
pinMode(1, INPUT_PULLUP); pinMode(1, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(1), tca_irq, FALLING); attachInterrupt(digitalPinToInterrupt(1), tca_irq, FALLING);
Serial.println("SUCCESS"); Serial.println("SUCCESS");
// Rotary Encoder Serial.print("1: INIT ROTARY ENCODER: ");
ENC.begin(); // set direction pin. ENC.begin(); // set direction pin.
ENC.setDirection(AS5600_CLOCK_WISE); ENC.setDirection(AS5600_CLOCK_WISE);
ENC.resetCumulativePosition(); ENC.resetCumulativePosition();
Serial.println("SUCCESS");
while(!config_loaded){
delay(250);
Serial.println("1: Waiting for config");
}
Serial.print("1: ENABLE I2S MCLK: ");
pinMode(19, OUTPUT); // MCLK enable
digitalWrite(19, HIGH); // enable MCLK
Serial.println("SUCCESS");
delay(10);
Serial.print("1: ENABLE CODEC: ");
pinMode(20, OUTPUT); // CODEC reset (enable)
digitalWrite(20, HIGH);
Serial.println("SUCCESS");
delay(10);
Serial.print("1: INIT CODEC: ");
codec.begin(&Wire1); codec.begin(&Wire1);
Serial.println("SUCCESS");
delay(100); delay(100);
pinMode(6, OUTPUT); // Vibration Motor
pinMode(7, OUTPUT); // UI Amp Enable
ui_snd.onTransmit(pwm_audio_callback);
ui_snd.begin(UI_SAMPLERATE);
digitalWrite(7, LOW); // UI amp off
Serial.print("1: STARTUP COMPLETE");
codec_ready = true; codec_ready = true;
setup1_finished = true;
} }
uint32_t last = 0; uint32_t last = 0;
@ -454,6 +490,7 @@ int16_t encdelta_raw = 0;
int16_t encdeltadiv = 512; int16_t encdeltadiv = 512;
void loop1() { void loop1() {
if(setup0_finished && setup0_finished){
position = ENC.getCumulativePosition(); position = ENC.getCumulativePosition();
angle = ENC.readAngle(); angle = ENC.readAngle();
encdelta_raw += (position - position_last); encdelta_raw += (position - position_last);
@ -620,7 +657,7 @@ void loop1() {
buttonChanged = false; buttonChanged = false;
} }
// if(position < 0) position += 4096; // if(position < 0) position += 4096;
switch(state){ switch(state){
case BANK: case BANK:
@ -680,7 +717,6 @@ void loop1() {
} }
} }
// // set active LED ring LED // // set active LED ring LED
// for(int i = 0; i < active_led_ring; i++){ // for(int i = 0; i < active_led_ring; i++){
// ui_leds[lut_ring_ccw[i]] = CRGB(config.ring_color.r, config.ring_color.g, config.ring_color.b); // ui_leds[lut_ring_ccw[i]] = CRGB(config.ring_color.r, config.ring_color.g, config.ring_color.b);
@ -698,22 +734,31 @@ void loop1() {
bar_old = bar; bar_old = bar;
vibration.update(); vibration.update();
position_last = position; position_last = position;
delay(1);
}
} }
//int sc = 0;
void loop(){ void loop(){
int16_t l, r; if(setup1_finished && setup0_finished){
i2s.read16(&l, &r); // int16_t l, r;
// i2s.read16(&l, &r);
// for(int k = 0; k < NSTREAMS; k++){
// int16_t sample_l = 0;
// int16_t sample_r = 0;
// sample_l = stream[k].get();
// sample_r = stream[k].get();
// l += sample_l >> 2;
// r += sample_r >> 2;
// }
// i2s.write16(l, r);
// i2s.flush();
for(int k = 0; k < NSTREAMS; k++){ // //Serial.println(sc);
int16_t sample_l = 0; // // Serial.print(" ");
int16_t sample_r = 0; // // Serial.print(l);
sample_l = stream[k].get(); // // Serial.print(" ");
sample_r = stream[k].get(); // // Serial.println(r);
l += sample_l >> 2;
r += sample_r >> 2;
}
i2s.write16(l, r);
if(streams_loaded) { if(streams_loaded) {
for(int i = 0; i < NSTREAMS; i++){ for(int i = 0; i < NSTREAMS; i++){
@ -721,6 +766,10 @@ void loop(){
} }
} }
if(i2s.getOverflow()) Serial.println("overflow");
if(i2s.getUnderflow()) Serial.println("underflow");
//sc++;
}
} }