esp32: Fix thread stack limit margin, change to new cstack API.

This change moves that complexity out into the stack checker and fixes the
bug where stack margin wasn't set correctly by ESP32-C3 threads.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h
index 3349e56..40b3f11 100644
--- a/ports/esp32/mpconfigport.h
+++ b/ports/esp32/mpconfigport.h
@@ -62,6 +62,11 @@
 // Python internal features
 #define MICROPY_READER_VFS                  (1)
 #define MICROPY_ENABLE_GC                   (1)
+#if CONFIG_IDF_TARGET_ARCH_RISCV                   // RISC-V SoCs use more stack than Xtensa
+#define MICROPY_STACK_CHECK_MARGIN          (2048) // This may be unnecessarily conservative
+#else
+#define MICROPY_STACK_CHECK_MARGIN          (1024)
+#endif
 #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
 #define MICROPY_LONGINT_IMPL                (MICROPY_LONGINT_IMPL_MPZ)
 #define MICROPY_ERROR_REPORTING             (MICROPY_ERROR_REPORTING_NORMAL)