2
\$\begingroup\$

Just out here seeing the craziest programming behaviour I have ever seen, I was wondering if anyone can explain why is this happening?

I finally just finished rewriting how UART packets from an ESP32 is processed within the STM32H753ZI with an array that holds address of functions.

Depending on which packet arrives, for example 0x03 which is enable line-in input it will access that function within the function pointer array at the index of the packet, so in this case the functionPointerArray[3]

What's happening is that when I comment out this piece of code:

void I2S_MuteHandler() {

     /* SPI1 -> CR1 |= SPI_CR1_CSUSP;
      SPI3 -> CR1 |= SPI_CR1_CSUSP;
      while (((SPI1 -> CR1) & (SPI_CR1_CSTART)));
      while (((SPI3 -> CR1) & (SPI_CR1_CSTART)));
      DMA1_Stream0 -> CR &= ~DMA_SxCR_EN;
      DMA1_Stream1 -> CR &= ~DMA_SxCR_EN;
      DMA1_Stream3 -> CR &= ~DMA_SxCR_EN;
      SPI1 -> CR1 &= ~SPI_CR1_SPE;
      SPI3 -> CR1 &= ~SPI_CR1_SPE;*/
    }

The output wave of which processed through a DSP code and sent out via I2S3_TxBUFF of code:

void I2S_HALFCOMPLETE_CALLBACK() {

    int * I2S1_RxBUFF = getI2S1_RxBUFF();
    int * I2S1_TxBUFF = getI2S1_TxBUFF();
    int * I2S3_TxBUFF = getI2S3_TxBUFF();

    double INSAMPLE_I2S_L[1024];
    double INSAMPLE_I2S_R[1024];
    double INSAMPLE_I2S_MONO[2048];

    double OUTSAMPLE_I2S_L[1024];
    double OUTSAMPLE_I2S_R[1024];
    double OUTSAMPLE_I2S_MONO[2048];

    for (int i = 0; i < 2048; i += 2) {

        //EVEN
        INSAMPLE_I2S_L[(i >> 1)] = (double)I2S1_RxBUFF[i] * (double)ADC_COMPENSATOR * 0.01 * centers.volume * centers.inputCompensator;
        INSAMPLE_I2S_MONO[i] = (double)((I2S1_RxBUFF[i] + I2S1_RxBUFF[i+1]) >> 1) * (double)ADC_COMPENSATOR * 0.01 * subwoofers.volume;
        //ODD
        INSAMPLE_I2S_R[(i >> 1)] = (double)I2S1_RxBUFF[i+1] * (double)ADC_COMPENSATOR * 0.01 * centers.volume * centers.inputCompensator;
        INSAMPLE_I2S_MONO[i+1] = INSAMPLE_I2S_MONO[i];
    }

    arm_biquad_cascade_df2T_f64(&subwoofers.audioStream_MONO, INSAMPLE_I2S_MONO, OUTSAMPLE_I2S_MONO, 2048);
    arm_biquad_cascade_df2T_f64(&centers.audioStream_L, INSAMPLE_I2S_L, OUTSAMPLE_I2S_L, 1024);
    arm_biquad_cascade_df2T_f64(&centers.audioStream_R, INSAMPLE_I2S_R, OUTSAMPLE_I2S_R, 1024);

    for (int i = 0 ; i < 2048; i += 2){

        //EVEN
        I2S1_TxBUFF[i] = (int)OUTSAMPLE_I2S_L[i >> 1];
        I2S3_TxBUFF[i] = (int)OUTSAMPLE_I2S_MONO[i];
        //ODD
        I2S1_TxBUFF[i+1] = (int)OUTSAMPLE_I2S_R[i >> 1];
        I2S3_TxBUFF[i+1] = (int)OUTSAMPLE_I2S_MONO[i+1];
    }
}

void I2S_TRANSFERCOMPLETE_CALLBACK() {

    int * I2S1_RxBUFF = getI2S1_RxBUFF();
    int * I2S1_TxBUFF = getI2S1_TxBUFF();
    int * I2S3_TxBUFF = getI2S3_TxBUFF();

    double INSAMPLE_I2S_L[1024];
    double INSAMPLE_I2S_R[1024];
    double INSAMPLE_I2S_MONO[2048];

    double OUTSAMPLE_I2S_L[1024];
    double OUTSAMPLE_I2S_R[1024];
    double OUTSAMPLE_I2S_MONO[2048];

    for (int i = 2048; i < 4096; i += 2) {

        //EVEN
        INSAMPLE_I2S_L[(i >> 1)-1024] = (double)I2S1_RxBUFF[i] * (double)ADC_COMPENSATOR * 0.01 * (double)centers.volume * centers.inputCompensator;
        INSAMPLE_I2S_MONO[i-2048] = (double)((I2S1_RxBUFF[i] + I2S1_RxBUFF[i+1]) >> 1) * (double)ADC_COMPENSATOR * 0.01 * subwoofers.volume;
        //ODD
        INSAMPLE_I2S_R[(i >> 1)-1024] = (double)I2S1_RxBUFF[i+1] * (double)ADC_COMPENSATOR * 0.01 * (double)centers.volume * centers.inputCompensator;
        INSAMPLE_I2S_MONO[(i+1)-2048] =  INSAMPLE_I2S_MONO[i-2048];
    }

        arm_biquad_cascade_df2T_f64(&subwoofers.audioStream_MONO, INSAMPLE_I2S_MONO, OUTSAMPLE_I2S_MONO, 2048);
        arm_biquad_cascade_df2T_f64(&centers.audioStream_L, INSAMPLE_I2S_L, OUTSAMPLE_I2S_L, 1024);
        arm_biquad_cascade_df2T_f64(&centers.audioStream_R, INSAMPLE_I2S_R, OUTSAMPLE_I2S_R, 1024);

    for (int i = 2048 ; i < 4096; i += 2){

        //EVEN
        I2S1_TxBUFF[i] = (int)OUTSAMPLE_I2S_L[(i >> 1)-1024];
        I2S3_TxBUFF[i] = (int)OUTSAMPLE_I2S_MONO[i-2048];
        //ODD
        I2S1_TxBUFF[i+1] = (int)OUTSAMPLE_I2S_R[(i >> 1)-1024];
        I2S3_TxBUFF[i+1] = (int)OUTSAMPLE_I2S_MONO[(i+1)-2048];
    }
}

makes the input sine wave look like square waves and the biquad filters stop working enter image description here

and when I uncomment the code that's causing the issue, the wave is working perfectly and the biquad filters work. enter image description here

For a better in-depth look I'll include how I populate the array of function pointers and how it executes.

The function pointer array gets populated during the initialization stage of the MCU along-side its peripherals CODE:

void load_functionHandlers() {

    messageReceived[0] = I2S_MuteHandler; // I believe the issue stems here in this function
    messageReceived[1] = I2S_UnmuteHandler;
//  messageReceived[2] = SPDIF_MuteHandler;
//  messageReceived[3] = SPDIF_UnmuteHandler;

    messageReceived[3] = audioSelectorInlineHandler;
//  messageReceived[5] = audioSelectorBluetoothHandler;
//  messageReceived[6] = audioSelectorSPDIFHandler;
//  messageReceived[7] = audioSelectorHDMIHandler;

//  messageReceived[8] = audioSelectorInternalHandler;
//  messageReceived[9] = audioSelectorExternalHandler;

//  messageReceived[10] = setupIRHandler;
//  messageReceived[11] = disableTouchHandler;
//  messageReceived[12] = enableTouchHandler;

//  messageReceived[13] = nightRiderLightsHandler;
//  messageReceived[14] = rainbowLightsHandler;

    messageReceived[15] = volumeIncreaseHandler;
    messageReceived[16] = volumeDecreaseHandler;
//
    messageReceived[17] = n_12dB_subBass_notch_EQHandler;
    messageReceived[18] = n_9dB_subBass_notch_EQHandler;
    messageReceived[19] = n_6dB_subBass_notch_EQHandler;
    messageReceived[20] = n_3dB_subBass_notch_EQHandler;
    messageReceived[21] = _0dB_subBass_notch_EQHandler;
    messageReceived[22] = p_3dB_subBass_notch_EQHandler;
    messageReceived[23] = p_6dB_subBass_notch_EQHandler;
    messageReceived[24] = p_9dB_subBass_notch_EQHandler;
    messageReceived[25] = p_12dB_subBass_notch_EQHandler;

    messageReceived[26] =   n_12dB_Bass_notch_EQHandler;
    messageReceived[27] =   n_9dB_Bass_notch_EQHandler;
    messageReceived[28] =   n_6dB_Bass_notch_EQHandler;
    messageReceived[29] =   n_3dB_Bass_notch_EQHandler;
    messageReceived[30] =   _0dB_Bass_notch_EQHandler;
    messageReceived[31] =   p_3dB_Bass_notch_EQHandler;
    messageReceived[32] =   p_6dB_Bass_notch_EQHandler;
    messageReceived[33] =   p_9dB_Bass_notch_EQHandler;
    messageReceived[34] =   p_12dB_Bass_notch_EQHandler;

    messageReceived[35] =   n_12dB_lower_midrange_notch_EQHandler;
    messageReceived[36] =   n_9dB_lower_midrange_notch_EQHandler;
    messageReceived[37] =   n_6dB_lower_midrange_notch_EQHandler;
    messageReceived[38] =   n_3dB_lower_midrange_notch_EQHandler;
    messageReceived[39] =   _0dB_lower_midrange_notch_EQHandler;
    messageReceived[40] =   p_3dB_lower_midrange_notch_EQHandler;
    messageReceived[41] =   p_6dB_lower_midrange_notch_EQHandler;
    messageReceived[42] =   p_9dB_lower_midrange_notch_EQHandler;
    messageReceived[43] =   p_12dB_lower_midrange_notch_EQHandler;

    messageReceived[44] =   n_12dB_midrange_notch_EQHandler;
    messageReceived[45] =   n_9dB_midrange_notch_EQHandler;
    messageReceived[46] =   n_6dB_midrange_notch_EQHandler;
    messageReceived[47] =   n_3dB_midrange_notch_EQHandler;
    messageReceived[48] =   _0dB_midrange_notch_EQHandler;
    messageReceived[49] =   p_3dB_midrange_notch_EQHandler;
    messageReceived[50] =   p_6dB_midrange_notch_EQHandler;
    messageReceived[51] =   p_9dB_midrange_notch_EQHandler;
    messageReceived[52] =   p_12dB_midrange_notch_EQHandler;

    messageReceived[53] =   n_12dB_higher_midrange_notch_EQHandler;
    messageReceived[54] =   n_9dB_higher_midrange_notch_EQHandler;
    messageReceived[55] =   n_6dB_higher_midrange_notch_EQHandler;
    messageReceived[56] =   n_3dB_higher_midrange_notch_EQHandler;
    messageReceived[57] =   _0dB_higher_midrange_notch_EQHandler;
    messageReceived[58] =   p_3dB_higher_midrange_notch_EQHandler;
    messageReceived[59] =   p_6dB_higher_midrange_notch_EQHandler;
    messageReceived[60] =   p_9dB_higher_midrange_notch_EQHandler;
    messageReceived[61] =   p_12dB_higher_midrange_notch_EQHandler;

    messageReceived[62] =   n_12dB_presence_notch_EQHandler;
    messageReceived[63] =   n_9dB_presence_notch_EQHandler;
    messageReceived[64] =   n_6dB_presence_notch_EQHandler;
    messageReceived[65] =   n_3dB_presence_notch_EQHandler;
    messageReceived[66] =   _0dB_presence_notch_EQHandler;
    messageReceived[67] =   p_3dB_presence_notch_EQHandler;
    messageReceived[68] =   p_6dB_presence_notch_EQHandler;
    messageReceived[69] =   p_9dB_presence_notch_EQHandler;
    messageReceived[70] =   p_12dB_presence_notch_EQHandler;

    messageReceived[71] =   n_12dB_brilliance_notch_EQHandler;
    messageReceived[72] =   n_9dB_brilliance_notch_EQHandler;
    messageReceived[73] =   n_6dB_brilliance_notch_EQHandler;
    messageReceived[74] =   n_3dB_brilliance_notch_EQHandler;
    messageReceived[75] =   _0dB_brilliance_notch_EQHandler;
    messageReceived[76] =   p_3dB_brilliance_notch_EQHandler;
    messageReceived[77] =   p_6dB_brilliance_notch_EQHandler;
    messageReceived[78] =   p_9dB_brilliance_notch_EQHandler;
    messageReceived[79] =   p_12dB_brilliance_notch_EQHandler;

    messageReceived[80] =   n_12dB_Bass_lowShelf_EQHandler;
    messageReceived[81] =   n_9dB_Bass_lowShelf_EQHandler;
    messageReceived[82] =   n_6dB_Bass_lowShelf_EQHandler;
    messageReceived[83] =   n_3dB_Bass_lowShelf_EQHandler;
    messageReceived[84] =   _0dB_Bass_lowShelf_EQHandler;
    messageReceived[85] =   p_3dB_Bass_lowShelf_EQHandler;
    messageReceived[86] =   p_6dB_Bass_lowShelf_EQHandler;
    messageReceived[87] =   p_9dB_Bass_lowShelf_EQHandler;
    messageReceived[88] =   p_12dB_Bass_lowShelf_EQHandler;

    //messageReceived[89] = disableLightsHandler;
}

How the function pointer gets executed CODE:

static void (*messageReceived[255])(void); // Global Variable 
    
    
void UART4_TRANSFERCOMPELTE_CALLBACK(uint8_t exitPacket) {
    //functions pointers
    if (exitPacket) {
        if ((exitPacket >= 0) && (exitPacket <= 88)) {
            (*messageReceived[exitPacket])();
        }
    } else {
        uint8_t packet = getUART4_RxBUFF()[0];

        if ((packet >= 0) && (packet <= 88)) {
            (*messageReceived[packet])();
        }
    }
}

I can't never predict the behaviour of this. I try commenting out random function pointers where the function pointers get populated into the array and it works, and vice versa.

I thought it was a memory address being overwritten somewhere, however I double checked the the two address of when the wave is square and when the wave is perfect. Its registers, address, anything you name it are identical. I am mind blown right now. If anyone can shine light on this would be amazing. It doesn't even enter the function that's in question at all.

I know it's an issue where populating the array is happening as when I do this test code to bypass the function pointer and comment out out the block of code it doesn't work still.

static void (*messageReceived[255])(void);

void UART4_TRANSFERCOMPELTE_CALLBACK(uint8_t exitPacket) {
    //functions pointers
    uint8_t * packet;

    if (exitPacket) {
        packet = malloc(sizeof(uint8_t));
        packet[0] = exitPacket;
    } else {
        packet = getUART4_RxBUFF();
    }

    //(*messageReceived[packet[0]])(); // By passing function pointer array
    audioSelectorInlineHandler();
}

UPDATE 1:

If I once again bypass the function pointer array and comment EVERYTHING in the function that populates the function pointer array. It works
CODE:

/*
 * UART_PACKET_PROCESSOR.c
 *
 *  Created on: May 13, 2021
 *      Author: Christopher
 */

#include <stdint.h>
#include <stdlib.h>
#include <constants.h>
#include <dspFactory.h>
#include <inputSourceFactory.h>
#include <opcode.h>
#include <packetProcessor.h>
#include <uartFactory.h>

static void (*messageReceived[255])(void);


void UART4_TRANSFERCOMPELTE_CALLBACK(uint8_t exitPacket) {
    //functions pointers
    uint8_t * packet = NULL;

    if (exitPacket) {
        //(*messageReceived[packet[exitPacket]])();
    } else {
        //(*messageReceived[getUART4_RxBUFF()[0]])();
    }

    audioSelectorInlineHandler();
}

void load_functionHandlers() {

//  for (int i = 0; i < 255; i++) {
//      messageReceived[i] = 0;
//  }
//
//  messageReceived[0] = &I2S_MuteHandler;
//  messageReceived[1] = &I2S_UnmuteHandler;
////    messageReceived[2] = SPDIF_MuteHandler;
////    messageReceived[3] = SPDIF_UnmuteHandler;
//
//  messageReceived[3] = &audioSelectorInlineHandler;
////    messageReceived[5] = audioSelectorBluetoothHandler;
////    messageReceived[6] = audioSelectorSPDIFHandler;
////    messageReceived[7] = audioSelectorHDMIHandler;
//
////    messageReceived[8] = audioSelectorInternalHandler;
////    messageReceived[9] = audioSelectorExternalHandler;
//
////    messageReceived[10] = setupIRHandler;
////    messageReceived[11] = disableTouchHandler;
////    messageReceived[12] = enableTouchHandler;
//
////    messageReceived[13] = nightRiderLightsHandler;
////    messageReceived[14] = rainbowLightsHandler;
//
//  messageReceived[15] = &volumeIncreaseHandler;
//  messageReceived[16] = &volumeDecreaseHandler;
////
//  messageReceived[17] = &n_12dB_subBass_notch_EQHandler;
//  messageReceived[18] = &n_9dB_subBass_notch_EQHandler;
//  messageReceived[19] = &n_6dB_subBass_notch_EQHandler;
//  messageReceived[20] = &n_3dB_subBass_notch_EQHandler;
//  messageReceived[21] = &_0dB_subBass_notch_EQHandler;
//  messageReceived[22] = &p_3dB_subBass_notch_EQHandler;
//  messageReceived[23] = &p_6dB_subBass_notch_EQHandler;
//  messageReceived[24] = &p_9dB_subBass_notch_EQHandler;
//  messageReceived[25] = &p_12dB_subBass_notch_EQHandler;
//
//  messageReceived[26] =   &n_12dB_Bass_notch_EQHandler;
//    messageReceived[27] = &n_9dB_Bass_notch_EQHandler;
//    messageReceived[28] = &n_6dB_Bass_notch_EQHandler;
//  messageReceived[29] =   &n_3dB_Bass_notch_EQHandler;
//  messageReceived[30] =   &_0dB_Bass_notch_EQHandler;
//  messageReceived[31] =   &p_3dB_Bass_notch_EQHandler;
//  messageReceived[32] =   &p_6dB_Bass_notch_EQHandler;
//  messageReceived[33] =   &p_9dB_Bass_notch_EQHandler;
//  messageReceived[34] =   &p_12dB_Bass_notch_EQHandler;
//
//  messageReceived[35] =   &n_12dB_lower_midrange_notch_EQHandler;
//  messageReceived[36] =   &n_9dB_lower_midrange_notch_EQHandler;
//    messageReceived[37] = &n_6dB_lower_midrange_notch_EQHandler;
//  messageReceived[38] =   &n_3dB_lower_midrange_notch_EQHandler;
//  messageReceived[39] =   &_0dB_lower_midrange_notch_EQHandler;
//  messageReceived[40] =   &p_3dB_lower_midrange_notch_EQHandler;
//  messageReceived[41] =   &p_6dB_lower_midrange_notch_EQHandler;
//  messageReceived[42] =   &p_9dB_lower_midrange_notch_EQHandler;
//  messageReceived[43] =   &p_12dB_lower_midrange_notch_EQHandler;
//
//  messageReceived[44] =   &n_12dB_midrange_notch_EQHandler;
//  messageReceived[45] =   &n_9dB_midrange_notch_EQHandler;
//    messageReceived[46] = &n_6dB_midrange_notch_EQHandler;
//  messageReceived[47] =   &n_3dB_midrange_notch_EQHandler;
//  messageReceived[48] =   &_0dB_midrange_notch_EQHandler;
//  messageReceived[49] =   &p_3dB_midrange_notch_EQHandler;
//  messageReceived[50] =   &p_6dB_midrange_notch_EQHandler;
//  messageReceived[51] =   &p_9dB_midrange_notch_EQHandler;
//  messageReceived[52] =   &p_12dB_midrange_notch_EQHandler;
//
//  messageReceived[53] =   &n_12dB_higher_midrange_notch_EQHandler;
//  messageReceived[54] =   &n_9dB_higher_midrange_notch_EQHandler;
//    messageReceived[55] = &n_6dB_higher_midrange_notch_EQHandler;
//  messageReceived[56] =   &n_3dB_higher_midrange_notch_EQHandler;
//  messageReceived[57] =   &_0dB_higher_midrange_notch_EQHandler;
//  messageReceived[58] =   &p_3dB_higher_midrange_notch_EQHandler;
//  messageReceived[59] =   &p_6dB_higher_midrange_notch_EQHandler;
//  messageReceived[60] =   &p_9dB_higher_midrange_notch_EQHandler;
//  messageReceived[61] =   &p_12dB_higher_midrange_notch_EQHandler;
//
//  messageReceived[62] =   &n_12dB_presence_notch_EQHandler;
//  messageReceived[63] =   &n_9dB_presence_notch_EQHandler;
//  messageReceived[64] =   &n_6dB_presence_notch_EQHandler;
//  messageReceived[65] =   &n_3dB_presence_notch_EQHandler;
//  messageReceived[66] =   &_0dB_presence_notch_EQHandler;
//  messageReceived[67] =   &p_3dB_presence_notch_EQHandler;
//  messageReceived[68] =   &p_6dB_presence_notch_EQHandler;
//  messageReceived[69] =   &p_9dB_presence_notch_EQHandler;
//  messageReceived[70] =   &p_12dB_presence_notch_EQHandler;
//
//  messageReceived[71] =   &n_12dB_brilliance_notch_EQHandler;
//  messageReceived[72] =   &n_9dB_brilliance_notch_EQHandler;
//  messageReceived[73] =   &n_6dB_brilliance_notch_EQHandler;
//  messageReceived[74] =   &n_3dB_brilliance_notch_EQHandler;
//  messageReceived[75] =   &_0dB_brilliance_notch_EQHandler;
//  messageReceived[76] =   &p_3dB_brilliance_notch_EQHandler;
//  messageReceived[77] =   &p_6dB_brilliance_notch_EQHandler;
//  messageReceived[78] =   &p_9dB_brilliance_notch_EQHandler;
//  messageReceived[79] =   &p_12dB_brilliance_notch_EQHandler;
//
//  messageReceived[80] =   &n_12dB_Bass_lowShelf_EQHandler;
//  messageReceived[81] =   &n_9dB_Bass_lowShelf_EQHandler;
//  messageReceived[82] =   &n_6dB_Bass_lowShelf_EQHandler;
//  messageReceived[83] =   &n_3dB_Bass_lowShelf_EQHandler;
//  messageReceived[84] =   &_0dB_Bass_lowShelf_EQHandler;
//  messageReceived[85] =   &p_3dB_Bass_lowShelf_EQHandler;
//  messageReceived[86] =   &p_6dB_Bass_lowShelf_EQHandler;
//  messageReceived[87] =   &p_9dB_Bass_lowShelf_EQHandler;
//  messageReceived[88] =   &p_12dB_Bass_lowShelf_EQHandler;

    //messageReceived[89] = disableLightsHandler;
}

UPDATE 2:

If I tried another function to comment out and it still does the same behaviour.

CODE:

void load_functionHandlers() {

    for (int i = 0; i < 255; i++) {
        messageReceived[i] = 0;
    }

    messageReceived[0] = &I2S_MuteHandler;
    messageReceived[1] = &I2S_UnmuteHandler; //Comment this shows same behavoir
//  messageReceived[2] = SPDIF_MuteHandler;
//  messageReceived[3] = SPDIF_UnmuteHandler;

    messageReceived[3] = &audioSelectorInlineHandler;
//  messageReceived[5] = audioSelectorBluetoothHandler;
//  messageReceived[6] = audioSelectorSPDIFHandler;
//  messageReceived[7] = audioSelectorHDMIHandler;

//  messageReceived[8] = audioSelectorInternalHandler;
//  messageReceived[9] = audioSelectorExternalHandler;

//  messageReceived[10] = setupIRHandler;
//  messageReceived[11] = disableTouchHandler;
//  messageReceived[12] = enableTouchHandler;

//  messageReceived[13] = nightRiderLightsHandler;
//  messageReceived[14] = rainbowLightsHandler;

    messageReceived[15] = &volumeIncreaseHandler; // no affect
    messageReceived[16] = &volumeDecreaseHandler; // no affect
//
    messageReceived[17] = &n_12dB_subBass_notch_EQHandler; ////////
    messageReceived[18] = &n_9dB_subBass_notch_EQHandler;
    messageReceived[19] = &n_6dB_subBass_notch_EQHandler;
    messageReceived[20] = &n_3dB_subBass_notch_EQHandler;
    messageReceived[21] = &_0dB_subBass_notch_EQHandler;
    messageReceived[22] = &p_3dB_subBass_notch_EQHandler; any of these shows same behavior
    messageReceived[23] = &p_6dB_subBass_notch_EQHandler;
    messageReceived[24] = &p_9dB_subBass_notch_EQHandler; 
    messageReceived[25] = &p_12dB_subBass_notch_EQHandler;

    messageReceived[26] =   &n_12dB_Bass_notch_EQHandler;
    messageReceived[27] =   &n_9dB_Bass_notch_EQHandler; 
    messageReceived[28] =   &n_6dB_Bass_notch_EQHandler;
    messageReceived[29] =   &n_3dB_Bass_notch_EQHandler;
    messageReceived[30] =   &_0dB_Bass_notch_EQHandler;
    messageReceived[31] =   &p_3dB_Bass_notch_EQHandler;
    messageReceived[32] =   &p_6dB_Bass_notch_EQHandler;
    messageReceived[33] =   &p_9dB_Bass_notch_EQHandler;
    messageReceived[34] =   &p_12dB_Bass_notch_EQHandler;    
    messageReceived[35] =   &n_12dB_lower_midrange_notch_EQHandler;
    messageReceived[36] =   &n_9dB_lower_midrange_notch_EQHandler;
    messageReceived[37] =   &n_6dB_lower_midrange_notch_EQHandler; 
    messageReceived[38] =   &n_3dB_lower_midrange_notch_EQHandler;
    messageReceived[39] =   &_0dB_lower_midrange_notch_EQHandler;
    messageReceived[40] =   &p_3dB_lower_midrange_notch_EQHandler;
    messageReceived[41] =   &p_6dB_lower_midrange_notch_EQHandler;
    messageReceived[42] =   &p_9dB_lower_midrange_notch_EQHandler;
    messageReceived[43] =   &p_12dB_lower_midrange_notch_EQHandler;

    messageReceived[44] =   &n_12dB_midrange_notch_EQHandler;
    messageReceived[45] =   &n_9dB_midrange_notch_EQHandler;
    messageReceived[46] =   &n_6dB_midrange_notch_EQHandler;
    messageReceived[47] =   &n_3dB_midrange_notch_EQHandler; any of these shows same behavior
    messageReceived[48] =   &_0dB_midrange_notch_EQHandler;
    messageReceived[49] =   &p_3dB_midrange_notch_EQHandler;
    messageReceived[50] =   &p_6dB_midrange_notch_EQHandler;
    messageReceived[51] =   &p_9dB_midrange_notch_EQHandler;
    messageReceived[52] =   &p_12dB_midrange_notch_EQHandler;

    messageReceived[53] =   &n_12dB_higher_midrange_notch_EQHandler;
    messageReceived[54] =   &n_9dB_higher_midrange_notch_EQHandler;
    messageReceived[55] =   &n_6dB_higher_midrange_notch_EQHandler;
    messageReceived[56] =   &n_3dB_higher_midrange_notch_EQHandler; any of these shows same behavior
    messageReceived[57] =   &_0dB_higher_midrange_notch_EQHandler;
    messageReceived[58] =   &p_3dB_higher_midrange_notch_EQHandler;
    messageReceived[59] =   &p_6dB_higher_midrange_notch_EQHandler;
    messageReceived[60] =   &p_9dB_higher_midrange_notch_EQHandler;
    messageReceived[61] =   &p_12dB_higher_midrange_notch_EQHandler;

    messageReceived[62] =   &n_12dB_presence_notch_EQHandler;
    messageReceived[63] =   &n_9dB_presence_notch_EQHandler;
    messageReceived[64] =   &n_6dB_presence_notch_EQHandler;
    messageReceived[65] =   &n_3dB_presence_notch_EQHandler; any of these shows same behavior
    messageReceived[66] =   &_0dB_presence_notch_EQHandler;
    messageReceived[67] =   &p_3dB_presence_notch_EQHandler;
    messageReceived[68] =   &p_6dB_presence_notch_EQHandler;
    messageReceived[69] =   &p_9dB_presence_notch_EQHandler;
    messageReceived[70] =   &p_12dB_presence_notch_EQHandler;

    messageReceived[71] =   &n_12dB_brilliance_notch_EQHandler;
    messageReceived[72] =   &n_9dB_brilliance_notch_EQHandler;
    messageReceived[73] =   &n_6dB_brilliance_notch_EQHandler;
    messageReceived[74] =   &n_3dB_brilliance_notch_EQHandler;
    messageReceived[75] =   &_0dB_brilliance_notch_EQHandler;
    messageReceived[76] =   &p_3dB_brilliance_notch_EQHandler; any of these shows same behavior
    messageReceived[77] =   &p_6dB_brilliance_notch_EQHandler;
    messageReceived[78] =   &p_9dB_brilliance_notch_EQHandler;
    messageReceived[79] =   &p_12dB_brilliance_notch_EQHandler;

    messageReceived[80] =   &n_12dB_Bass_lowShelf_EQHandler;
    messageReceived[81] =   &n_9dB_Bass_lowShelf_EQHandler;
    messageReceived[82] =   &n_6dB_Bass_lowShelf_EQHandler;
    messageReceived[83] =   &n_3dB_Bass_lowShelf_EQHandler;
    messageReceived[84] =   &_0dB_Bass_lowShelf_EQHandler; any of these shows same behavior
    messageReceived[85] =   &p_3dB_Bass_lowShelf_EQHandler;
    messageReceived[86] =   &p_6dB_Bass_lowShelf_EQHandler;
    messageReceived[87] =   &p_9dB_Bass_lowShelf_EQHandler;
    messageReceived[88] =   &p_12dB_Bass_lowShelf_EQHandler; ////////////

    //messageReceived[89] = disableLightsHandler;
}

UPDATE 3:

I strongly believe it's a memory issue. The code below is essentially a smaller version of what's above. I commented all the other handlers() expect for the one that's in testing "audioSelectorInlineHandler()" and changed the allocation of the function pointer array from [255] -> [4] and it works.

UPDATE 4: Took out the function pointers and noticed it did the same thing. I am going to assume this is caused by using SPI3. Not sure why

\$\endgroup\$
2
  • \$\begingroup\$ The long comment chain has exceeded what is reasonable for comments. Therefore it has been moved to chat and should be continued there (link below). --- As this bulk moving of comments to chat can only be done once, any further comments posted here to discuss the question, might be deleted without notice. Keep it in chat, please! When someone has got enough information from the chat to post an answer, then please do that. Any updates to the question which are decided during the chat, should be made via an edit to the question. Thanks. \$\endgroup\$
    – SamGibson
    Commented Jun 18, 2021 at 16:37
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – SamGibson
    Commented Jun 18, 2021 at 16:38

1 Answer 1

1
\$\begingroup\$

Not really an answer, but the malloc could be the problem, that would be much cleaner:

void UART4_TRANSFERCOMPELTE_CALLBACK(uint8_t exitPacket) {

    if (exitPacket) {
     ProcessCB(exitPacket);
    } else {
    ProcessCB(getUART4_RxBUFF()[0]);
    }
}

Also, init your messageReceived array to 0 and then before calling:

void ProcessCB(unsigned char packet)
{
    if ((*messageReceived[packet]) == 0) {
        // error
    } else {

        (*messageReceived[packet])();
    }
}

Put these big array on memory that is defined at compile time instead of the dynamic stack, which can be limited, which means as global or static, on both methods

static double INSAMPLE_I2S_L[1024];
static double INSAMPLE_I2S_R[1024];
static double INSAMPLE_I2S_MONO[2048];

static double OUTSAMPLE_I2S_L[1024];
static double OUTSAMPLE_I2S_R[1024];
static double OUTSAMPLE_I2S_MONO[2048];

That 500kbit, Don't know if the MCU can handle that in the stack.

People tend to be cautious with a large stack, because it grows backwards in RAM and overwrites values of variables, leading to unexplainable behavior. It gets even worse, because you need to know the lowest possible stack pointer address and subtract the size to allocate when entering the routine.

This is all a job for the hardware memory management (should generate traps or faults when a stack overflow occurs) or for the compiler, given it has features for this kind of analysis.

https://softwareengineering.stackexchange.com/questions/333344/is-there-a-drawback-at-allocating-a-huge-amount-of-the-stack-for-a-single-array

\$\endgroup\$
2
  • \$\begingroup\$ The long comment chain for this answer has exceeded what is reasonable for comments. Therefore it has been moved to chat and should be continued there (link below). --- As this bulk moving of comments to chat can only be done once, any further comments posted here to discuss the answer, might be deleted without notice. Keep it in chat, please! Any factual updates to the answer which are decided during the chat, should be made via an edit to the answer. Thanks! \$\endgroup\$
    – SamGibson
    Commented Jun 18, 2021 at 16:45
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – SamGibson
    Commented Jun 18, 2021 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.