joepasquariello
Well-known member
I'm using Zilch, and I have a suggestion for the CPP file. task_create() currently has a different signature for KINETISK/L. Both can use the same signature as shown below, which allows much of task_create() to be common. I have tested for KINETISK on Teensy 3.2 and 3.5. I can test on Teensy LC tomorrow, probably.
(1) In the extern "C" block, eliminate the KINETISK/L differences and have one declaration for task_create()
(2) This eliminates KINETISK/L differences in Zilch::create()
(3) And allows much of task_create() to be common for KINETISK/L:
Also, should the setting of frame->address be using "|=", or should it be like this?
(1) In the extern "C" block, eliminate the KINETISK/L differences and have one declaration for task_create()
Code:
TaskState task_create ( volatile stack_frame_t *frame, task_func_t func, size_t stack_size, void *arg );
(2) This eliminates KINETISK/L differences in Zilch::create()
Code:
TaskState Zilch::create( task_func_t task, size_t stack_size, void *arg ) {
// Round stack size to a word multiple
int s_size = ( stack_size + sizeof ( uint32_t ) - 1 ) / sizeof ( uint32_t ) * sizeof ( uint32_t );
if ( num_task+1 >= MAX_TASKS ) return TaskInvalid;
TaskState p = task_create( &process_tasks[++num_task], task, s_size, arg );
return p;
}
(3) And allows much of task_create() to be common for KINETISK/L:
Code:
TaskState task_create( volatile stack_frame_t *frame, task_func_t func, size_t stack_size, void *arg ) {
#if defined(KINETISL)
asm volatile("push {r0,r1,r2,r3}\n");
asm volatile("stmia r0!,{r4,r5,r6,r7}\n"); // Save lower regs
asm volatile("mov r1,r8\n");
asm volatile("mov r2,r9\n");
asm volatile("mov r3,sl\n");
asm volatile("stmia r0!,{r1,r2,r3}\n"); // Save r8,r9 & sl
asm volatile("mov r1,fp\n");
asm volatile("mov r3,lr\n");
asm volatile("stmia r0!,{r1,r2,r3}\n"); // Save fp,(placeholder for sp) & lr
asm volatile("pop {r0,r1,r2,r3}\n"); // Restore regs
frame->r7 = (uint32_t)frame; // Overwrite r12 with fiber ptr
#elif defined(KINETISK)
asm volatile( "STMEA %0,{r1-r11}\n" : "+r" ( frame ) :: "memory" );// Save r1-r11 to task struct
frame->r12 = (uint32_t)frame; // r12 points to struct
#endif
frame->stack_size = stack_size; // Save task size
frame->func_ptr = func; // Save task function
frame->arg = arg; // Save task arg
frame->sp = stackroot; // Save as tasks's sp
frame->lr = (void*)task_start; // Task startup code
frame->state = TaskCreated; // Set state of this task
frame->initial_sp = stackroot; // Save sp for restart()
stackroot -= stack_size; // This is the new root of the stack
int address = (1 << num_task); // get task address
frame->address |= address; // set task address
task_mask |= (1 << num_task); // task swap mask
init_mask = task_mask; // num of tasks
return TaskCreated;
}
Also, should the setting of frame->address be using "|=", or should it be like this?
Code:
int address = (1 << num_task); // task address
frame->address = address; // save address in frame
task_mask |= address; // add address to task mask