diff --git a/py/mpconfig.h b/py/mpconfig.h
index 4a3288a3d10ccda32cf139794d18424af16022a0..d7504c1402338d8c2c33b32a334f5a67d6d202f3 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -157,6 +157,12 @@
 #define MICROPY_ENABLE_GC_FINALISER (0)
 #endif
 
+// Whether to check C stack usage. C stack used for calling Python functions,
+// etc. Not checking means segfault on overflow.
+#ifndef MICROPY_STACK_CHECK
+#define MICROPY_STACK_CHECK (1)
+#endif
+
 // Whether to include REPL helper function
 #ifndef MICROPY_HELPER_REPL
 #define MICROPY_HELPER_REPL (0)
diff --git a/py/objfun.c b/py/objfun.c
index 29363129b27a5b54c9cb9ad6e863968a735c4365..f75e9142a2bca5d1ea2a3d9239a7fb7c98bba3d0 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -39,6 +39,7 @@
 #include "runtime0.h"
 #include "runtime.h"
 #include "bc.h"
+#include "stackctrl.h"
 
 #if 0 // print debugging info
 #define DEBUG_PRINT (1)
@@ -204,6 +205,8 @@ STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, uint expected, ui
 // code_state should have ->ip filled in (pointing past code info block),
 // as well as ->n_state.
 void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
+    // This function is pretty complicated.  It's main aim is to be efficient in speed and RAM
+    // usage for the common case of positional only args.
     mp_obj_fun_bc_t *self = self_in;
     machine_uint_t n_state = code_state->n_state;
     const byte *ip = code_state->ip;
@@ -353,8 +356,7 @@ continue2:;
 
 
 STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
-    // This function is pretty complicated.  It's main aim is to be efficient in speed and RAM
-    // usage for the common case of positional only args.
+    STACK_CHECK();
 
     DEBUG_printf("Input n_args: %d, n_kw: %d\n", n_args, n_kw);
     DEBUG_printf("Input pos args: ");
diff --git a/py/py.mk b/py/py.mk
index 2b74f252cf5ba8c251d0fe46eca6e0ad25e3586a..374bb11d614a5c92715edeb20264e5d3e556cd12 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -43,6 +43,7 @@ PY_O_BASENAME = \
 	parsenum.o \
 	emitglue.o \
 	runtime.o \
+	stackctrl.o \
 	argcheck.o \
 	map.o \
 	obj.o \
diff --git a/py/stackctrl.c b/py/stackctrl.c
new file mode 100644
index 0000000000000000000000000000000000000000..aef9bad9e9360b7e5db395e1a9947fce40ad4b21
--- /dev/null
+++ b/py/stackctrl.c
@@ -0,0 +1,63 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Paul Sokolovsky
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "mpconfig.h"
+#include "misc.h"
+#include "nlr.h"
+#include "qstr.h"
+#include "obj.h"
+#include "runtime.h"
+#include "stackctrl.h"
+
+// Stack top at the start of program
+char *stack_top;
+
+void stack_ctrl_init() {
+    volatile int stack_dummy;
+    stack_top = (char*)&stack_dummy;
+}
+
+uint stack_usage() {
+    // Assumes descending stack
+    volatile int stack_dummy;
+    return stack_top - (char*)&stack_dummy;
+}
+
+#if MICROPY_STACK_CHECK
+
+uint stack_limit = 10240;
+
+void stack_set_limit(uint limit) {
+    stack_limit = limit;
+}
+
+void stack_check() {
+    if (stack_usage() >= stack_limit) {
+        nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "maximum recursion depth exceeded"));
+    }
+}
+
+#endif // MICROPY_STACK_CHECK
diff --git a/py/stackctrl.h b/py/stackctrl.h
new file mode 100644
index 0000000000000000000000000000000000000000..a9a8d2e2d895584e6a65f96130df256eba898dfc
--- /dev/null
+++ b/py/stackctrl.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Paul Sokolovsky
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+void stack_ctrl_init();
+uint stack_usage();
+
+#if MICROPY_STACK_CHECK
+
+void stack_set_limit(uint limit);
+void stack_check();
+#define STACK_CHECK() stack_check()
+
+#else
+
+#define stack_set_limit(limit)
+#define STACK_CHECK()
+
+#endif
diff --git a/unix/main.c b/unix/main.c
index 552df9b8047ac366edc6908bc520546eaf294c33..a08661339cdcf9d9139b54c2eea5b303e9d35ba9 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -51,6 +51,7 @@
 #include "gc.h"
 #include "genhdr/py-version.h"
 #include "input.h"
+#include "stackctrl.h"
 
 // Command line options, with their defaults
 bool compile_only = false;
@@ -63,9 +64,6 @@ uint mp_verbose_flag;
 long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
 #endif
 
-// Stack top at the start of program
-char *stack_top;
-
 void microsocket_init();
 void time_init();
 void ffi_init();
@@ -214,10 +212,9 @@ int usage(char **argv) {
 }
 
 mp_obj_t mem_info(void) {
-    volatile int stack_dummy;
     printf("mem: total=%d, current=%d, peak=%d\n",
         m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
-    printf("stack: " INT_FMT "\n", stack_top - (char*)&stack_dummy);
+    printf("stack: %u\n", stack_usage());
 #if MICROPY_ENABLE_GC
     gc_dump_info();
 #endif
@@ -268,8 +265,8 @@ void pre_process_options(int argc, char **argv) {
 #endif
 
 int main(int argc, char **argv) {
-    volatile int stack_dummy;
-    stack_top = (char*)&stack_dummy;
+    stack_ctrl_init();
+    stack_set_limit(32768);
 
     pre_process_options(argc, argv);