Hello,
I have a struct defined in a header file as follows:
typedef struct CommutationStep { uint16_t index; uint16_t hall_val; struct CommutationStep *next_step; struct CommutationStep *prev_step; } CommutationStep;
In the file containing the main(), I have an other struct:
typedef struct MotorInfo { CommutationStep *comm_table; uint16_t hall_raw;
uint16_t prev_hall;
bool debounced; // other unused fields } MotorInfo;
In my main I initialize a MotorInfo struct called motor succesfully. *comm_table also points correctly to the 1st element of a 6 element circular array. (debugger verified, the circular list is fine up to this point)
Next, there is the infinite loop of my main:
while(1) { if (!motor.debounced) { set_commutation_step(motor.hall_raw); } motor.hall_raw = get_hall_val(); debounce_hall(&motor); }
and the debounce_hall function:
void debounce_hall(MotorInfo *motor) { // Check if debounce is disabled if (DEBOUNCE_LEVEL < 1 || DEBOUNCE_LEVEL > 5) // (bug here) { motor->debounced = false; return; } if (DEBOUNCE_LEVEL == 1) { if (motor->hall_raw != motor->prev_hall) { // Point the comm_table to the step matching the raw hall val while(motor->comm_table->next_step->hall_val != motor->hall_raw) { motor->comm_table = motor->comm_table->next_step; } motor->comm_table = motor->comm_table->next_step; motor->prev_hall = motor->hall_raw; motor->debounced = false; } else { motor->debounced = true; } return; } else { // other code, not of interest } }
Now to my problem: comm_table points to one of the six addresses 0x00000410-420-430-450-460. MotorInfo motor address is 0x00000404. I am debugging manually step by step, setting the hall_raw to the next step each time. By the time comm_table points to the sixth step, the address is correctly 0x00000460, the next_step is 410, and the previous 450. Checking the next_step at the debugger, it still correctly points to the next and prev addresses. Now, I am at the start of the 2nd revolution, and I set the hall_raw correctly to match the 1st step. debounce_hall(&motor) is called, and still every single address points to the expected step. At the 1st line of the function (comment // bug here), suddenly the struct at address 0x00000410 (1st step) gets a field modified: next_step now instead of pointing to 0x00000420 is pointing to 0x00000404. (motor struct address).
What may be the cause of it?
Thank you in advance!