- May 11, 2008
- 23,331
- 1,575
- 126
I have this code as seen below.
This code will hang because the pointer is forgotten it seems because of the switch case construction.
In the for loop, it will just write anywhere and will never return.
But when i do it like this, disabling the switch construction and load the pointer directly, the pointer is not forgotten and the function works.
The whole idea is that i have 4 raw value buffers that are constantly updated into a circular fashion :
What am i doing wrong ? Why is the compiler forgetting the pointer in the for loop ?
This code will hang because the pointer is forgotten it seems because of the switch case construction.
In the for loop, it will just write anywhere and will never return.
Code:
uint32_t ReadArrayAmg8831(amg8831_t *ps_ir)
{
uint32_t r;
uint32_t x;
uint32_t index;
uint32_t j;
uint8_t array[128];
volatile uint16_t *pointer=0;
[COLOR="Red"]removed code here that reads sensor data into array.[/COLOR]
switch(ps_ir->buffer_cntr)
{
case 0:
pointer = ps_ir->traw_0;
break;
case 1:
pointer = ps_ir->traw_1;
break;
case 2:
pointer = ps_ir->traw_2;
break;
case 3:
pointer = ps_ir->traw_3;
break;
}
ps_ir->buffer_cntr++;
ps_ir->buffer_cntr &= 0x03; //Make circular buffer.
index=0;
for(j=0;j<64;j++)
{
r = array[index++];
x = (array[index++] & 0xF);
r = r | (x << 8);
*pointer = (uint16_t)r;
pointer++;
}
return 0;
}
But when i do it like this, disabling the switch construction and load the pointer directly, the pointer is not forgotten and the function works.
Code:
uint32_t ReadArrayAmg8831(amg8831_t *ps_ir)
{
uint32_t r;
uint32_t x;
uint32_t index;
uint32_t j;
uint8_t array[128];
volatile uint16_t *pointer=0;
[COLOR="Red"]removed code here that reads sensor data into array.[/COLOR]
/*
switch(ps_ir->buffer_cntr)
{
case 0:
pointer = ps_ir->traw_0;
break;
case 1:
pointer = ps_ir->traw_1;
break;
case 2:
pointer = ps_ir->traw_2;
break;
case 3:
pointer = ps_ir->traw_3;
break;
}
*/
ps_ir->buffer_cntr++;
ps_ir->buffer_cntr &= 0x03; //Make circular buffer.
pointer = ps_ir->traw_0;
index=0;
for(j=0;j<64;j++)
{
r = array[index++];
x = (array[index++] & 0xF);
r = r | (x << 8);
*pointer = (uint16_t)r;
pointer++;
}
return 0;
The whole idea is that i have 4 raw value buffers that are constantly updated into a circular fashion :
Code:
// AMG8831 struct.
//
typedef struct s_amg8831
{ // 4 buffers for averaging the temperature or for detection.
uint16_t traw_0[64]; // Contains raw sensor data.
uint16_t traw_1[64]; // Contains raw sensor data.
uint16_t traw_2[64]; // Contains raw sensor data.
uint16_t traw_3[64]; // Contains raw sensor data.
uint16_t temperature[64]; // COntains temperature in celcius.
uint8_t buffer_cntr; // This counter counts from 0 up to 3. Is used by read array to select the right raw value buffer.
}amg8831_t;
What am i doing wrong ? Why is the compiler forgetting the pointer in the for loop ?
