Basic Thread code in STM32F7
Hi,
I am implementing basic multi-thread code where in a character is printed in each thread. I took example code from crossworks which had one thread and added one more thread to it. The edited code is as below. One weird thing that I am facing is that the print in each thread works only when the other thread has print too. If I keep only v++ in any one thread and do debug print in other, it does not work. I checked thread window in debug mode, 2nd task is always in runnable state and first is always in executing state. Both tasks have same priority. I am trying this out to test my understanding of threads in crossworks. This is pretty basic code and I think I am doing something wrong here. It will be great if someone and point me to right direction.
#include <string.h>
#include <stdio.h>
#include <ctl_api.h>
#include <debugio.h>
CTL_TASK_t main_task, new_task,new_task1;
#define CALLSTACKSIZE 16 // this is only required for AVR builds
#define STACKSIZE 64
static unsigned new_task_stack[1+STACKSIZE+1];
static unsigned new_task1_stack[1+STACKSIZE+1];
void hardware_init(void)
{
//RCC_OscInitTypeDef RCC_OscInitStruct;
}
void new_task_code(void *p)
{
unsigned int v=0;
debug_printf("task1\n");
while (1)
{
// task logic goes here
ctl_sleep(100);
debug_printf("1");
v++;
}
}
void new_task_code_2(void *p)
{
unsigned int v=0;
debug_printf("task2\n");
while (1)
{
// task logic goes here
//ctl_sleep(100);
// debug_printf("2");
v++;
}
}
void
ctl_handle_error(CTL_ERROR_CODE_t e)
{
while (1);
}
int main(void)
{
unsigned int v=0;
ctl_task_init(&main_task, 255, "main"); // create subsequent tasks whilst running at the highest priority.
ctl_start_timer(ctl_increment_tick_from_isr); // start the timer
debug_printf("init\n");
hardware_init();
memset(new_task_stack, 0xcd, sizeof(new_task_stack)); // write known values into the stack
new_task_stack[0]=new_task_stack[1+STACKSIZE]=0xfacefeed; // put marker values at the words before/after the stack
memset(new_task1_stack, 0xcd, sizeof(new_task1_stack)); // write known values into the stack
new_task1_stack[0]=new_task1_stack[1+STACKSIZE]=0xfacefeed;
ctl_task_run(&new_task, 1, new_task_code, 0, "new_task", STACKSIZE, new_task_stack+1, CALLSTACKSIZE);
ctl_task_run(&new_task1, 1, new_task_code_2, 0, "new_task1", STACKSIZE, new_task1_stack+1, CALLSTACKSIZE);
ctl_task_set_priority(&main_task, 0); // drop to lowest priority to start created tasks running.ctl_task_set_priority(&main_task, 0); // drop to lowest priority to start created tasks running. ctl_task_set_priority(&main_task, 0); // drop to lowest priority to start created tasks running.ctl_task_set_priority(&main_task, 0); // drop to lowest priority to start created tasks running.
while (1)
{
// power down can go here if supported
v++;
}
return 0;
}
Please sign in to leave a comment.
Comments
0 comments