#include #include #include #include #include /*******************/ /*** Threads ***/ /*******************/ #define MAIN_THREAD_STACK_SIZE CYGNUM_HAL_STACK_SIZE_MINIMUM*3 cyg_uint8 main_thread_stack[MAIN_THREAD_STACK_SIZE]; cyg_handle_t main_thread_handle; cyg_thread main_thread_obj; void show_thread(cyg_handle_t thread) { cyg_uint16 id=cyg_thread_get_id(thread); cyg_thread_info info; if( !cyg_thread_get_info( thread, id, &info ) ) { diag_printf("Unable to get thread info\n"); return; } diag_write_dec(info.stack_used); diag_printf(" Bytes used\n"); } void stacktest3(void) { char test2[1512]="In this test we allocate (1512 Bytes), and we print out the stack usage\n\0"; diag_printf(test2); show_thread(main_thread_handle); } void stacktest2(void) { char test2[512]="Test data2 allocated (512 Bytes) in the called function, on the stack\n\0"; diag_printf(test2); } void stacktest(void) { char test[1024]="Test data allocated (1024 Bytes) in the called function, on the stack\n\0"; diag_printf(test); } static void main_thread(cyg_addrword_t index) { show_thread(main_thread_handle); show_thread(main_thread_handle); diag_printf("Stack usage should be the same, but it is not.\n"); show_thread(main_thread_handle); diag_printf("Calling the test function\n"); stacktest(); diag_printf("function returned, let's see the stack size, it should be the same as before\n"); show_thread(main_thread_handle); diag_printf("now we call it again\n"); stacktest(); diag_printf("function returned, let's see the stack size, it should be the same as before\n"); show_thread(main_thread_handle); diag_printf("now we the second test function\n"); stacktest2(); diag_printf("function returned, let's see the stack size, it should be the same as before\n"); show_thread(main_thread_handle); diag_printf("now we the third test function\n"); stacktest3(); diag_printf("function returned, let's see the stack size, it should be the same as before call\n"); show_thread(main_thread_handle); while(1) { diag_printf("main loop\n"); cyg_thread_delay(1000); show_thread(main_thread_handle); } } /****************/ /*** Init ***/ /****************/ void cyg_user_start(void) { diag_init(); diag_printf("\n"); diag_printf("---- User-start ---\n"); cyg_thread_create( 10, // priority main_thread, // thread function 0, // thread parameter NULL, // thread name &main_thread_stack, // stack base addr MAIN_THREAD_STACK_SIZE, // stack size &main_thread_handle, // thread handle &main_thread_obj); // thread object cyg_thread_resume(main_thread_handle); }