So there is code that is supposed to poll a GPIO read pin at a certain rate and put the SET/RESET result to a doubly linked list of 16 elements, deleting the first added (firstIn) element. formArray() was added for debugging options; it just intializes an array from a linked list of read GPIO results. The confirmHandshake() then checks if the input data is corresponding to a control array of 16 bits. On paper (ran on pc) this program works fine. But ran on a microcontroller, such put-delete cycle runs exactly 933 times and then calls a HardFault(). By smaller amount of experience in debugging, I found out that seemingly the code exceeds controller's stack memory, although the stack is not supposed to grow. It is probably one just one silly mistake that causes that to happen. But still please take a look at the code.
#include <stdbool.h>
#include <stdlib.h>
typedef struct boolNode{
int data;
struct boolNode * ahead;
struct boolNode * before;
}node;
int controlPattern[16] = {0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0};
int howMuch = 16;
node *firstIn=NULL;
node *lastIn=NULL;
int values[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int value;
int countHF=0;
int l,i,readDelayTicks=210;
int flag;
int d;
int * initBoolQueue(int howMuch, node **firstIn, node **lastIn){
node * arr[howMuch];
for(int i=0; i<howMuch; i++){
arr[i] = (node*)malloc(sizeof(node));}
for (int i = 0; i<howMuch; i++){
if(i==0){
arr[0] -> ahead = (*firstIn); // If the node is the 1st
(*firstIn) = arr[0];
arr[0] -> data = 0;
arr[0] -> before = arr[i+1];
(*lastIn) = arr[0];
values[i]=arr[0]->data;
}
else {
if (i==howMuch-1){ //If the node is the last
arr[i] -> data = 0;
arr[i] -> ahead = arr[i-1];
arr[i] -> before = NULL;
(*lastIn) = arr[i];
values[i]=arr[i]->data;
}
else { // All others
arr[i] -> data = 0;
arr[i] -> ahead = arr[i-1];
arr[i] -> before = arr[i+1];
values[i]=arr[i]->data;}
}
}
return values;
}
void pushNode(int data, node ** firstIn, node ** lastIn){ //pushing data to the
//end and deleting
//the first node of a list
node * newgpio = (node*)malloc(sizeof(node));
newgpio -> data = data;
newgpio -> ahead = (*lastIn);
newgpio -> before = NULL;
(*lastIn) -> before = newgpio;
(*lastIn) = newgpio;
node * temp;
temp = (*firstIn);
(*firstIn) = (*firstIn) -> before;
free(temp);
}
void formArray(int howMuch, node ** firstIn, node ** lastIn){
node * temp;
temp = (node*)malloc(sizeof(node));
temp = (*firstIn);
for (int i=0; i<howMuch; i++){
values[i]=temp->data;
if(i!=howMuch-1)
temp=temp->before;
}
}
int confirmHandshake(int howMuch){
flag =1;
for(int i=0; i<howMuch; i++){
if (values[i] != controlPattern[i]) flag =0;
}
if (flag == 1)
{HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);
HAL_Delay(15);
return 1;}
else return 0;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
initBoolQueue(howMuch, &firstIn, &lastIn);
HAL_Delay(5);
formArray(howMuch, &firstIn, &lastIn);
//timer functions are set for poll rate correction in the future
while (1)
{
HAL_TIM_Base_Start(&htim2);
value = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_9);
for(i=0; i<readDelayTicks;i++){}
pushNode(value, &firstIn, &lastIn);
formArray(howMuch, &firstIn, &lastIn);
confirmHandshake(16);
d=htim2.Instance->CNT;
HAL_TIM_Base_Stop(&htim2);
htim2.Instance->CNT=0;
}
}
Currently learning how to properly debug such low level data. Sorry if on this site it is incompetent to pin a output data like this, but here's the call stack along with disassembly tab call stack. Hope this helps. Thank you