2022-08-03 01:00 PM
I am trying to implement a function that extracts mfcc of the retrieved sound. I am using Hanning window. I actually am applying dynamic memory allocation twice. First allocation works successfully. Second allocation however somehow takes me to hard fault handler function.
float number_of_freq_bins = ceilf(hz_BW/(float)(SAMPLE_RATE/(FFTSIZE>>1)));
if (((int)number_of_freq_bins % 2) == 0) number_of_freq_bins++;
size_t len = (size_t)number_of_freq_bins; // just for having appropriate datatype for allocation
hanning(offset, number_of_freq_bins);
float* filter = malloc(sizeof(float) * len); // yields hard fault exception
In line 6, hanning is a function that creates hanning window, and seems as below:
void hanning(int offset, int fftsize){
//float *mul = malloc(sizeof(float)*fftsize);
float mul[fftsize];
for (int i = 0; i<fftsize; i++){
mul[i] = 0.5 * (1 - cos(2*PI*i/(fftsize-1)));
}
// AUDIO_HANN_ADDRESS 0xC0400000
memcpy((uint32_t*)(AUDIO_HANN_ADDRES + offset), mul, fftsize);
}
Edit: I changed the statement that contains malloc to normal array.
This time, since the statement is inside a loop, it works fine until the loop is terminated. But when the loop, and hence the function is terminated, it takes me to hard_fault handler function. And SCB->SHCSR yields sometimes 1 and sometimes 65535.
2022-08-03 01:12 PM
Are the stack or heap adequate?
The second here uses the stack.
Does the malloc() return a viable address?
Any useful information from the Hard Fault Handler on the register content or the exact code that's fault.
A while(1) is going to tell you next to nothing
2022-08-04 01:32 AM
I've tried to check if malloc allocates space correctly, by using:
if(filter != NULL) {
...
}
However, the program couldn't reach this statement.
I am not sure stack or heap are adequate. The min heap size is adjusted as 0x200 while min_stack_size is 0x400. I've tried to change it but it didn't work. This values are taken in STM32F746NGHx_FLASH.ld file, since I am using BSP project released by ST.