[Telink] fix stack sizes to avoid 90% usage (#36485)

* [Telink] Add temporarry THREAD_ANALYZER to check stack sizes during crash

* [Telink] fix configs

* [Telink] Increase OpenThread stack (98% usage during OTA)

* [Telink] Set INFO as default MCUboot log level

* [Telink] define CHIP_TASK_STACK_SIZE from one place

* [Telink] Add thread-analyzer as target build

* [Telink] decrise BOOT_MAX_IMG_SECTORS

* [Telink] temporary enable THREAD_ANALYZER by default

* [Telink] Increase stack sizes to avoid 90+% usage

* [Telink] increase more

* [Telink] set THREAD_ANALYZER=n by default
diff --git a/config/telink/app/bootloader.conf b/config/telink/app/bootloader.conf
index 52937e1..0ffacc8 100644
--- a/config/telink/app/bootloader.conf
+++ b/config/telink/app/bootloader.conf
@@ -30,7 +30,8 @@
 CONFIG_BOOT_VALIDATE_SLOT0=y
 
 # Maximum number of image sectors supported by the bootloader.
-CONFIG_BOOT_MAX_IMG_SECTORS=4096
+# Maximum signed image size: 512 * 4096 = 2M Bytes
+CONFIG_BOOT_MAX_IMG_SECTORS=512
 
 # Sets log level for modules which don't specify it explicitly.
 # When set to 0 it means log will not be activated for those modules.
@@ -40,4 +41,4 @@
 #  - 2 WARNING, default to write LOG_LEVEL_WRN
 #  - 3 INFO, default to write LOG_LEVEL_INFO
 #  - 4 DEBUG, default to write LOG_LEVEL_DBG
-CONFIG_LOG_DEFAULT_LEVEL=1
+CONFIG_LOG_DEFAULT_LEVEL=3
diff --git a/config/telink/chip-module/Kconfig b/config/telink/chip-module/Kconfig
index ee87af2..b06c7c8 100644
--- a/config/telink/chip-module/Kconfig
+++ b/config/telink/chip-module/Kconfig
@@ -246,6 +246,7 @@
 
 config CHIP_TASK_STACK_SIZE
 	int
+	default 4864 if PM
 	default 8192
 
 config CHIP_USE_MARS_SENSOR
diff --git a/config/telink/chip-module/Kconfig.defaults b/config/telink/chip-module/Kconfig.defaults
index d28d77d..bb722e8 100644
--- a/config/telink/chip-module/Kconfig.defaults
+++ b/config/telink/chip-module/Kconfig.defaults
@@ -71,7 +71,7 @@
     default 512 if PM
 
 config ISR_STACK_SIZE
-    default 502 if PM
+    default 576 if PM
 
 config SYSTEM_WORKQUEUE_STACK_SIZE
     default 616 if PM
@@ -154,7 +154,7 @@
     default n
 
 config B9X_BLE_CTRL_THREAD_STACK_SIZE
-    default 576
+    default 648
 
 config B9X_BLE_CTRL_MASTER_MAX_NUM
     default 0
@@ -261,10 +261,10 @@
 	default y if CHIP_THREAD_DEVICE_ROLE_SLEEPY_END_DEVICE
 
 config OPENTHREAD_THREAD_STACK_SIZE
-    default 2178 if PM
+    default 2400 if PM
 
 config OPENTHREAD_RADIO_WORKQUEUE_STACK_SIZE
-    default 361 if PM
+    default 432 if PM
     default 608
 
 config OPENTHREAD_SLAAC
@@ -366,6 +366,9 @@
 	  a random jitter interval is added to it to avoid periodicity. The random jitter is selected
 	  within range [-JITTER; +JITTER].
 
+config NET_MGMT_EVENT_STACK_SIZE
+	default 1128
+
 endif # CHIP_WIFI
 
 config CHIP_ENABLE_PAIRING_AUTOSTART
@@ -448,7 +451,17 @@
 config OPENTHREAD_SHELL
     default n
 
-config CHIP_TASK_STACK_SIZE
-	default 4864 if PM
+# Thread analyzer
+config THREAD_ANALYZER
+    default n
+    select THREAD_ANALYZER_AUTO
+
+config THREAD_ANALYZER_AUTO_INTERVAL
+    default 5
+    depends on THREAD_ANALYZER_AUTO
+
+config THREAD_ANALYZER_LOG_LEVEL
+    default 3 # info
+    depends on THREAD_ANALYZER
 
 endif
diff --git a/scripts/build/build/targets.py b/scripts/build/build/targets.py
index b36824b..0f80756 100755
--- a/scripts/build/build/targets.py
+++ b/scripts/build/build/targets.py
@@ -801,6 +801,7 @@
     target.AppendModifier('mars', mars_board_config=True)
     target.AppendModifier('usb', usb_board_config=True)
     target.AppendModifier('compress-lzma', compress_lzma_config=True)
+    target.AppendModifier('thread-analyzer', thread_analyzer_config=True)
 
     return target
 
diff --git a/scripts/build/builders/telink.py b/scripts/build/builders/telink.py
index 644f4cd..a494300 100644
--- a/scripts/build/builders/telink.py
+++ b/scripts/build/builders/telink.py
@@ -155,6 +155,7 @@
                  mars_board_config: bool = False,
                  usb_board_config: bool = False,
                  compress_lzma_config: bool = False,
+                 thread_analyzer_config: bool = False,
                  ):
         super(TelinkBuilder, self).__init__(root, runner)
         self.app = app
@@ -168,6 +169,7 @@
         self.mars_board_config = mars_board_config
         self.usb_board_config = usb_board_config
         self.compress_lzma_config = compress_lzma_config
+        self.thread_analyzer_config = thread_analyzer_config
 
     def get_cmd_prefixes(self):
         if not self._runner.dry_run:
@@ -215,6 +217,9 @@
         if self.compress_lzma_config:
             flags.append("-DCONFIG_COMPRESS_LZMA=y")
 
+        if self.thread_analyzer_config:
+            flags.append("-DCONFIG_THREAD_ANALYZER=y")
+
         if self.options.pregen_dir:
             flags.append(f"-DCHIP_CODEGEN_PREGEN_DIR={shlex.quote(self.options.pregen_dir)}")
 
diff --git a/scripts/build/testdata/all_targets_linux_x64.txt b/scripts/build/testdata/all_targets_linux_x64.txt
index f34f42b..c473de1 100644
--- a/scripts/build/testdata/all_targets_linux_x64.txt
+++ b/scripts/build/testdata/all_targets_linux_x64.txt
@@ -22,5 +22,5 @@
 qpg-qpg6105-{lock,light,shell,persistent-storage,light-switch,thermostat}[-updateimage]
 stm32-stm32wb5mm-dk-light
 tizen-arm-{all-clusters,chip-tool,light,tests}[-no-ble][-no-thread][-no-wifi][-asan][-ubsan][-coverage][-with-ui]
-telink-{tlsr9118bdk40d,tlsr9518adk80d,tlsr9528a,tlsr9528a_retention,tlsr9258a,tlsr9258a_retention}-{air-quality-sensor,all-clusters,all-clusters-minimal,bridge,contact-sensor,light,light-switch,lock,ota-requestor,pump,pump-controller,shell,smoke-co-alarm,temperature-measurement,thermostat,window-covering}[-ota][-dfu][-shell][-rpc][-factory-data][-4mb][-mars][-usb][-compress-lzma]
+telink-{tlsr9118bdk40d,tlsr9518adk80d,tlsr9528a,tlsr9528a_retention,tlsr9258a,tlsr9258a_retention}-{air-quality-sensor,all-clusters,all-clusters-minimal,bridge,contact-sensor,light,light-switch,lock,ota-requestor,pump,pump-controller,shell,smoke-co-alarm,temperature-measurement,thermostat,window-covering}[-ota][-dfu][-shell][-rpc][-factory-data][-4mb][-mars][-usb][-compress-lzma][-thread-analyzer]
 openiotsdk-{shell,lock}[-mbedtls][-psa]