In my program, I created a double buffer for a given area.
This buffer displays colored lines based on the position of an MPU6050 sensor, but every time, sooner or later, it crashes the board.
I've been trying to figure out why for two weeks without finding an answer.
I don't have any pixels outside the buffer.
I've tried lots of different things, but the Teensy 4.0 still crashes.
I'll spare you the semantics I've done on my code.
teensy 4.0
ILI9341_t3n
cpu speed 816
Wire.setClock(528000L); even when setting it to 400000, the effect is the same.
Memory Usage on Teensy 4.0:
FLASH: code:80048, data:26208, headers:8428 free for files:1916932
RAM1: variables:53792, code:76152, padding:22152 free for local variables:372192
RAM2: variables:12416 free for malloc/new:511872
Buffer definitions:
Same result.
Functions for loading it:
How to use it:
The function that uses the buffer and is called at each loop iteration.
void My function() {
Calculations based on the sensor
then
Two loops with a maximum of 210 iterations
then
End of function.
I don't know what else to do to make everything work properly. Thank you to anyone who takes the time to respond and help me with my problem.
I also have a second, smaller double buffer. This one does not cause any problems.
I also have a normal display, so the resources also use the sensor data, no problem.
This buffer displays colored lines based on the position of an MPU6050 sensor, but every time, sooner or later, it crashes the board.
I've been trying to figure out why for two weeks without finding an answer.
I don't have any pixels outside the buffer.
I've tried lots of different things, but the Teensy 4.0 still crashes.
I'll spare you the semantics I've done on my code.
teensy 4.0
ILI9341_t3n
cpu speed 816
Wire.setClock(528000L); even when setting it to 400000, the effect is the same.
Memory Usage on Teensy 4.0:
FLASH: code:80048, data:26208, headers:8428 free for files:1916932
RAM1: variables:53792, code:76152, padding:22152 free for local variables:372192
RAM2: variables:12416 free for malloc/new:511872
Buffer definitions:
C++:
// === Définition buffer A ===
#define BUF_W 111 // largeur de la zone
#define BUF_H 138 // hauteur de la zone
#define OFFSET_X 105 // position X sur l'écran
#define OFFSET_Y 32 // position Y sur l'écran
uint16_t buffer1[BUF_W * BUF_H];
uint16_t buffer2[BUF_W * BUF_H];
or:
//DMAMEM uint16_t buffer1[BUF_W * BUF_H];
//DMAMEM uint16_t buffer2[BUF_W * BUF_H];
Functions for loading it:
C++:
void drawPixelToBuffer(int x, int y, uint16_t color) {
if (!bufferReady) return;
bufferReady = false;
int bx = x - OFFSET_X;
int by = y - OFFSET_Y;
/* // Filtre pour éviter tout hors-buffer
if (bx < 0 || bx >= BUF_W || by < 0 || by >= BUF_H) {
// Optionnel : log pour debug
Serial.print(F("Pixel hors zone: ("));
Serial.print(x);
Serial.print(F(","));
Serial.print(y);
Serial.println(F(")"));
return; // on sort sans écrire
}
*/
activeBuffer[by * BUF_W + bx] = color;
bufferReady = true; // buffer prêt après écriture
}
void drawLineToBuffer(int x0, int y0, int x1, int y1, uint16_t color) {
//Serial.printf("drawLineToBuffer: (%d,%d) -> (%d,%d)\n", x0, y0, x1, y1);
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = dx + dy, e2;
while (true) {
drawPixelToBuffer(x0, y0, color);
if (x0 == x1 && y0 == y1) break;
e2 = 2 * err;
if (e2 >= dy) {
err += dy;
x0 += sx;
}
if (e2 <= dx) {
err += dx;
y0 += sy;
}
}
}
How to use it:
The function that uses the buffer and is called at each loop iteration.
void My function() {
Calculations based on the sensor
then
Two loops with a maximum of 210 iterations
C++:
drawLineToBuffer(x1,y1,x2,y2, RED);
then
C++:
tft.writeRect(OFFSET_X, OFFSET_Y, BUF_W, BUF_H, activeBuffer);
memset(activeBuffer, 0, sizeof(uint16_t) * BUF_W * BUF_H);
uint16_t* temp = activeBuffer;
activeBuffer = displayBuffer;
displayBuffer = temp;
swapBuffers();
bufferReady = true;
End of function.
I don't know what else to do to make everything work properly. Thank you to anyone who takes the time to respond and help me with my problem.
I also have a second, smaller double buffer. This one does not cause any problems.
I also have a normal display, so the resources also use the sensor data, no problem.
Last edited: