1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

M5Stack Core2: stack overflow エラーの対策

Last updated at Posted at 2021-09-07

次の記事を参考にしました。
ESP32 ( ESP-WROOM-32 , M5Stack )自分的 トラブルシューティング まとめ

Arduino core for the ESP32のsetup関数やloop関数内のスタックサイズは8192byteと定義されていて、
それを超えて巨大な配列を定義してしまうことが原因です。

stack overflow を起こすプログラム

test_stack_ng/test_stack_ng.ino
// ---------------------------------------------------------------
/*
	test_stack_ng.ino

					Sep/07/2021
*/
// ---------------------------------------------------------------
#include <M5Core2.h>

// ---------------------------------------------------------------
void setup() {
	M5.begin();
	M5.Lcd.setBrightness(32);
	delay(500);
	Serial.println("*** setup *** aaa ***");
	delay(500);
	Serial.println("*** setup *** bbb ***");
	delay(500);
}						 

// ---------------------------------------------------------------
void loop()
{
	const int max_ary = 8192;

	Serial.println("*** loop ***");
	delay(1000);
    
	uint8_t arry[max_ary];

	for(int it = 0; it < max_ary; it++)
		{
		arry[it] = 123;
		}
  
	Serial.println(arry[max_ary - 1]);

	delay(2000);
}

// ---------------------------------------------------------------

実行結果

$ cu -s 115200 -l /dev/ttyACM0
Connected.
*** loop ***
***ERROR*** A stack overflow in task loopTask has been detected.
abort() was called at PC 0x4008a540 on core 1

ELF file SHA256: 0000000000000000

Backtrace: 0x4008a2b0:0x3ffafde0 0x4008a529:0x3ffafe00 0x4008a540:0x3ffafe20 0x4008c126:0x3ffafe40 0x4008dd14:0x3ffafe60 0x4008dcca:0xad91e006

Rebooting...

対策したプログラム

malloc free を使って領域を確保します。

text/test_stack/test_stack.ino
// ---------------------------------------------------------------
/*
	test_stack.ino

					Sep/07/2021
*/
// ---------------------------------------------------------------
#include <M5Core2.h>

// ---------------------------------------------------------------
void setup() {
	M5.begin();
	M5.Lcd.setBrightness(32);
	delay(500);
	Serial.println("*** setup *** aaa ***");
	delay(500);
	Serial.println("*** setup *** bbb ***");
	delay(500);
}						 

// ---------------------------------------------------------------
void loop()
{
	const int max_ary = 8192;

	Serial.println("*** loop ***");
	delay(1000);
    
//  uint8_t arry[max_ary];
	uint8_t *arry = (uint8_t *)malloc(max_ary);

	for(int it = 0; it < max_ary; it++)
		{
		arry[it] = 123;
		}
  
	Serial.println(arry[max_ary - 1]);

	free(arry);

	delay(2000);
}

// ---------------------------------------------------------------

実行結果

$ cu -s 115200 -l /dev/ttyACM0
Connected.
*** loop ***
123
*** loop ***
123
*** loop ***
123
*** loop ***
123
*** loop ***
123
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?