0, some transplant, system-related
Os_cfg_app. H
/*---------------------Miscellaneous------------------*/#d Efine os_cfg_msg_pool_size 100u/* Message pool Size */#define OS_CF G_isr_stk_size 128u/* Stack SIZE of ISR stack (number of CPU_STK elements) */#define Os_cfg_ Task_stk_limit_pct_empty 10u/* Stack LIMIT position in percentage to EMPTY */ /*----------------------IDLE TASK---------------------*/#define Os_cfg_idle _task_stk_size 128u/* Idle task stack space size (usually not modified) */ /*------------------ISR HANDLER TASK------------------*/#define Os_cfg_int_q_size 10u/* Interrupt Service Queue Size */#define OS_CFG_INT_Q_TASK_STK_SIZE 128u /* Interrupt Service queue stack space size (one*/*-------------------statistic TAS K-------------------*/#define OS_CFG_STAT_TASK_PRIO (OS_CFG_PRIO_MAX-2U)/*/Statistics task priority (generally not modified) */#define OS_CFG_STAT_TASK_RATE_HZ 10u/* Statistical task frequency (1 to ten HZ) */#de Fine os_cfg_stat_task_stk_size 128u/* Statistics task stack space size (usually not modified) */ /*------------------------TICKS-----------------------*/#define Os_cfg_tick_r Ate_hz 200u/* clock beat frequency 200HZ = 5ms (ten to HZ) */#define OS_CFG_TICK_TASK_PR IO 1u/* clock beat priority, generally set a relatively high priority */#define OS_CFG_TICK_TASK_STK_SIZE 128u /* clock beat stack space size (usually not modified) */#define OS_CFG_TICK_WHEEL_SIZE 17u/* Numbe R of ' spokes ' in tick WHeel should be prime */*-----------------------timers------ -----------------*/#define Os_cfg_tmr_task_prio 2u/* Software Timer priority */#define OS_CFG_TMR _task_rate_hz 100u/* Software timer frequency 100HZ = 10ms, cannot be less than heartbeat clock beat */#define Os_cfg_tmr_task_stk_size 128U/* Software timer stack space size (usually not modified) */#define OS_CFG_TMR_WHEEL_SIZE 17u /* Number of ' spokes ' in timer wheel; should be prime */
Os_cfg. H: Functional cropping
Os_app_hooks. C: Hook function
OS_CPU_A.ASM:PendSV Interrupt, Task switch
Os_cpu_c.c:ostaskstkinit function, task creation, stack initialization, register address to reference manual
1. Frame writing (personal habits related)
1-1. Create a start task in the main function
int main (void) {Os_err ERR; Cpu_sr_alloc (); Initialize peripheral osinit (&ERR); Initialize Ucosiiios_critical_enter (); Enter the critical section ostaskcreate (); Create start Task Os_critical_exit (); Exit Critical Zone Osstart (&ERR); Open UCOSIII while (1);}
1-2. Start a task and create multiple tasks that we want to run
void Start_task (void *p_arg) {Os_err ERR; Cpu_sr_alloc ();p _arg = P_arg; Cpu_init (); #if os_cfg_stat_task_en > 0u //Statistical task Osstattaskcpuusageinit (&err); #endif #ifdef cpu_cfg_int_dis_meas_en//measurement interrupt off time cpu_intdismeasmaxcurreset (); #endif #ifos_cfg_sched_round_ Robin_en //Time slice rotary osschedroundrobincfg (def_enabled,1,&err); The time slice length is 1 system clock beats, both 1*5=5ms#endifos_critical_enter (); Enter the critical section ostaskcreate (); Create Task 1OSTaskCreate (); Create Task 2 ostaskcreate (); Create Task 3 os_critical_exit (); Enter the critical section Ostaskdel ((os_tcb*) 0,&err); Delete Start_task task itself}
2. Task creation, suspend, delete
2-1. Task Creation
================== task create macro definition, easy to modify ================== #define START_TASK_PRIO3//Task Priority # define Start_stk_size 128// Task stack size OS_TCB starttasktcb;//task control block CPU_STK Start_task_stk[start_stk_size]; Task stack void start_task (void *p_arg);//Task function//================== Task creation function ==================ostaskcreate ((OS_TCB *) & starttasktcb,//Task control block (cpu_char*) "Start Task",//Task name (os_task_ptr ) Start_task,//Task function (void*) 0,//Task task function parameters (Os_prio ) Start_task_prio, //Task priority (CPU_STK *) &start_task_stk[0],//task stack base address (cpu_stk_size) start_stk_size/10,// Task stack depth limit (cpu_stk_size) start_stk_size,//task stack size (os_msg_qty ) 0,//The maximum number of messages that can be received by the internal message queue for a task, which is 0 o'clock forbidden to receive messages (Os_tick) 0,//any Service use time slice rotation, time slice length, 0 is the default length, (void *) 0,//task User Supplementary storage area (os_opt ) os_opt_task_stk_chk| OS_OPT_TASK_STK_CLR,//Task options (OS_ERR *) &err);//Task Creation success or not
2-2. Task hangs
Ostasksuspend ((os_tcb*) &task1tasktcb,&err);//Suspend start task
2-3, the task of the solution hanging
Ostaskresume ((os_tcb*) &task1tasktcb,&err); Task unpacking
2-4. Task deletion
Ostaskdel ((os_tcb*) 0,&err);//delete Start_task task itself
3. Time Slice Rotation
3-1, two task priority equals
#define TASK0_TASK_PRIO4 //Priority 4#define TASK0_STK_SIZE128OS_TCB TASK0TASKTCB; CPU_STK task0_task_stk[task0_stk_size];void task0task (void *p_arg); #define Task1_task_prio4
3-2, enable rotation scheduling, call API, set unit time
#ifOS_CFG_SCHED_ROUND_ROBIN_EN //Using time slice rotary osschedroundrobincfg (def_enabled,1,&err); Time slice length is 1 system clock beats, both 1*5=5ms#endif
3-3. When creating a task, set the time slice size of the task
Ostaskcreate ((OS_TCB *) &TASK1TASKTCB, (cpu_char*) "Task1 task", (os_task_ptr) Task1task, (void*) 0, (Os_ PRIO ) Task1_task_prio, (CPU_STK *) &task1_task_stk[0], (cpu_stk_size) TASK1_STK_SIZE/10, (cpu_stk_ SIZE) Task1_stk_size, (Os_msg_qty ) 0, (Os_tick ) 2,//time slice length is 2*5=10ms (void *) 0, (os_opt ) os_opt_task _stk_chk| OS_OPT_TASK_STK_CLR, (Os_err *) &err);
4, hook function. function pointers, Ucos do not want us to modify his files, get a hook function, such as App_os_idletaskhook, in the idle run this function, the function to fill out their own.
4-1, enable hook function function, and set all function pointers
#if os_cfg_app_hooks_en > 0u//Enable hook function app_os_setallhooks (); #endif
4-2, in the OS_APP_HOOKS.C in the App_os_*hook, fill in the function you want.
void App_os_idletaskhook (void) {static int num; num++; Calculate how many idle tasks have been run}
5. Software Timer
5-1. Software Timer creation
============== timer struct, function declaration ==============os_tmr timer0;void timer0callback (void *p_tmr, void *p_arg);//============ = = Timer created ==============//Create timer 0 Task Ostmrcreate ((os_tmr*) &timer0,//timer structure (cpu_char*) "TIME0",//Timer name (Os_tick) 10, Timer first time delay beat 10*10 = 100ms (os_tick) 100,//timer after delay beat 100*10 = 1000ms = 1s (os_opt) os_opt_tmr_periodic,//timer option: Single or cycle. (OS_TMR_CALLBACK_PTR) timer0callback,//Timer callback function, pseudo interrupt service function (void*) 0,//Timer Interrupt Service function parameters (os_err*) &err);//Timer creation succeeded or not
5-2. Timer "Interrupt Service function", callback function
void Timer0callback (void *p_tmr, void *p_arg) {//do something}
6. Signal Volume
6-1. Signal Volume creation
Ossemcreate ( (Os_sem *) &mysem,//semaphore structure (Cpu_char *) "semtest",//Semaphore name (os_sem_ctr ) 1,//Semaphore initial value , (Os_err *) &err);//signal volume creation successful whether
6-2, the semaphore waits
Ossempend ((Os_sem *) &mysem,//semaphore structure (Os_tick ) 0,//semaphore wait Time-out (os_opt ) os_opt_pend_blocking,//semaphore Block or do not block (cpu_ts *) 0,//semaphore timestamp (os_err *) &err);//Semaphore wait Error
6-3, signal volume transmission
Ossempost ((Os_sem *) &mysem,//semaphore structure (os_opt ) os_opt_post_1,//semaphore to Ready highest priority (Os_err *) &err) ;//Semaphore wait Error
Purpose: 1, access to shared resources.
2, interrupt sends the signal, lets the processing in the task.
7, the task built-in signal volume
7-1, waiting for its own signal volume
Ostasksempend ((Os_tick ) 0,//built-in semaphore timeout (os_opt ) os_opt_pend_blocking,//built-in semaphore blocked or not blocked (cpu_ts *) 0,// Built-in Semaphore timestamp (os_err *) &err);//built-in Semaphore wait error
7-2, other tasks, to wait for the built-in signal volume of the task to send a semaphore
Ostasksempost ((OS_TCB *) &task0tasktcb,//built-in semaphore waiting task (os_opt ) os_opt_post_none,//built-in semaphore dispatch or no dispatch (Os_err *) &err);//built-in Semaphore wait error
8. Mutual exclusion Signal Volume
8-1. Mutual exclusion Semaphore creation
Osmutexcreate ((Os_mutex *) &mymutex,//mutually exclusive semaphore structure (cpu_char*) "mutextest",//Mutex semaphore name (os_err *) ERR);// Mutex Semaphore creation Error
8-2. Mutually exclusive semaphore waits
Osmutexpend ((os_mutex*) &mymutex,//mutex semaphore structure (Os_tick ) 0,//Mutex semaphore wait Time Out (os_opt ) os_opt_pend_blocking, Mutex semaphore blocking or not blocking (cpu_ts *) 0,//Mutex semaphore timestamp (os_err *) &err);//Mutex semaphore wait error
8-3. Mutually exclusive semaphore send
Osmutexpost ((Os_mutex *) &mymutex,//Mutex semaphore structure (os_opt ) os_opt_post_none,//Mutex dispatch or no dispatch (Os_err *) &ERR);//Mutex semaphore send error
Purpose: Prevent priority reversal, such as: 2 tasks share a resource, and the priority of the two is separated by multiple priorities (tasks). High priority waits for a low priority release, while the low priority is then interrupted by a medium priority, which becomes the medium priority of the high priority level.
With a mutex, the lower priority is temporarily raised to the high priority level of the shared resource. is not intended to be medium priority. After processing, the lower priority is lowered, and the high priority is then accessed. Medium-priority.
Found: 1, high priority waiting, low priority with ossched (); The expected effect is achieved at this point and will not be preempted by a medium priority.
2, high priority waiting, low priority with OSTIMEDLYHMSM (), delay instruction, at this time, will be preempted by the medium priority.
9. Message Queuing
9-1. Message Queuing creation
================ message Queue macro definition ================os_q my_msg_q; #define MY_MSG_QTY (os_msg_qty) 5//================ message Queue creation = = ==============osqcreate ((os_q *) &my_msg_q,//Message queue struct (cpu_char *) "msg_q_test",//Message queue name (Os_msg_qty ) my_msg_qty,//Message Queue Size (os_err *) &err);//Message Queuing creation Error
9-2. Send Message
U8 *msg = (U8 *) "Testtet"; Osqpost ((os_q *) &my_msg_q,//message queue struct (void *) msg,//messages sent by Message Queuing (Os_msg_size 8,//Message Queue sent message size (os_opt ) os_opt_post_fifo,//Message Queuing, normal FIFO, emergency LIFO, and send to all wait for the message, send dispatch or not (Os_err *) & ERR);//Message Queue send Error
9-3. Receiving Messages
U8 *msg;u8 q_size; MSG = Osqpend ((os_q *) &my_msg_q,//message queue struct (Os_tick ) 0,//Message Queuing wait timeout (os_opt ) os_opt_pend_blocking,// Message Queuing is blocked or not blocked (os_msg_size *) &q_size,//Message Queuing received size (cpu_ts *) 0,//Message Queuing timestamp (os_err *) &err);//Message Queuing Receive error
10. Task built-in message queue
10-1, built-in message queue send
U8 *msg = (U8 *) "Testtet"; Ostaskqpost ((OS_TCB *) &MSG_Q_TASKTCB,//built-in Message queue received task (void *) MSG,//messages sent in built-in message queue (Os_msg_size ) 8,//built-in message queue send message size (os_opt ) Os_opt_post_fifo,//built-in Message queue send method, normal FIFO, emergency LIFO, and send to all wait for the message, send dispatch or not (os_err *) &ERR); Built-in Message Queuing send error
10-2, built-in message queue receive
U8 *msg;u8 q_size; MSG = Ostaskqpend ((os_tick) 0,//built-in message queue wait timeout (os_opt ) os_opt_pend_blocking,//built-in message queue blocked or not blocked (os_msg_size *) &q_size,//built-in message queue received size (cpu_ts *) 0,//built-in message queue timestamp (os_err *) &err);//built-in Message Queuing receive error
11. Mark Bit Group
11-1. Tag bit Group creation
Os_flag_grp My_flag, #define Flag_init0x00#define flag_bit00x01#define flag_bit10x02osflagcreate (OS_FLAG_GRP * ) &my_flag,//Tag bit group structure (Cpu_char *) "flag test",//Tag bit group name (os_flags ) flag_init,//tag bit group tag initial value (Os_err *) &ERR);//Mark Bit group creation succeeded or not
11-2, Mark Bit group send
Osflagpost ((os_flag_grp *) &my_flag,//tag bit group structure (os_flags ) flag_bit0,//marker bit group bit0 (os_opt ) os_opt_ post_flag_set,//tag bit set 1 (os_err *) &err);//Marker bit Group Bit0 1 success or not
11-3. Mark as group wait
Os_flags Index;index = Osflagpend ((os_flag_grp *) &my_flag,//Tag bit group structure (os_flags ) flag_bit0 | FLAG_BIT1,//token bit group wait bit bit (Os_tick ) 0,//Mark bit group wait Time out (os_opt ) os_opt_pend_flag_set_any | Os_opt_pend_flag_consume | Os_opt_pend_blocking,//Mark bit group any 1 bit set 1, receive after clearing 0, block (cpu_ts *) 0,//marker bit set timestamp (os_err *) &err); Flag bit Group Wait error
12. Multiple Kernel objects
12-1. Multiple Kernel object creation
================= multiple kernel object structure body =================os_sem My_sem1;os_sem my_sem2;os_q my_q1; #define My_q1_size5//== =============== multiple Kernel objects created =================ossemcreate ((Os_sem *) &my_sem1, (Cpu_char *) "Sem1 test" (Os_ Sem_ctr ) 0, (os_err *) &err) ossemcreate ((Os_sem *) &my_sem2, (Cpu_char *) "SEM2 test" (Os_ Sem_ctr ) 0, (os_err *) &err) osqcreate ((os_q *) &my_q1, (Cpu_char *) "my_q1", ( Os_msg_qty ) my_q1_size, (os_err *) &err);
12-2. Wait for multiple kernel objects
=============== multiple Kernel objects macro definition =============== #define MY_OBJ_NUM 3//multiple kernel objects waiting//=============== multiple kernel objects waiting =os_pend_data my_obj_data[my_obj_num];//multiple Kernel object arrays my_obj_data[0]. Pendobjptr = (os_pend_obj *) &my_sem1;//multiple Kernel object objects 0my_obj_data[1]. Pendobjptr = (os_pend_obj *) &my_sem2;//multiple Kernel object objects 1my_obj_data[2]. Pendobjptr = (os_pend_obj *) &my_q1;//multiple Kernel object Objects 2my_return_data = Ospendmulti ((os_pend_data *) My_obj_data,//multiple Kernel Object Object Array (Os_obj_qty ) My_obj_num,//number of Kernel object objects (Os_tick ) 0,//Multiple kernel object wait time-out (os_opt ) Os_opt_pend_blockin g,//multiple kernel objects blocking or not blocking (os_err *) &err);//Multiple Kernel objects waiting for errors
12-3. Send multiple Kernel objects
Any object post, will end waiting
13. Memory Management
13-1. Memory creation
================== memory macro definition ==================os_mem In_mem, #define in_mem_block5//must be greater than 2#define In_mem_zone25 * 4// Must be greater than 4 and be a multiple of 4, storing the next piece of address content, 4 bytes cpu_int08u in_mem_data[in_mem_block][in_mem_zone];//================== memory Creation ========== ========osmemcreate ( (os_mem *) &in_mem,//memory structure (Cpu_char *) "In_mem",//Memory name (void * ) &in_mem_data[0][0],//Memory Base Address (Os_mem_qty ) in_mem_block,//memory several blocks, one-dimensional array (os_mem_size ) in_mem_ zone,//memory per block size, two-dimensional array (os_err *) &error);//Memory creation succeeded or not
13-2. Memory Application
U8 *p;p = Osmemget ((os_mem *) &in_mem,//memory structure (os_err *) &err);//Memory Request success or not
13-3. Memory Release
Osmemput ((os_mem *) &in_mem,//Memory structure (void *) p,//memory to release address (os_err *) &err); Memory release succeeded or not
Note: Memory application, will be released, if the application, do not release, and then apply, the address before the application can not be found, because your pointer address has changed.
So, if you want to apply multiple times, 1, but also to get an array of pointers, 2, or an ordinary array to hold the address of the current application, 3, or know ucos memory management mechanism, directly to the memory array to find the address.
For the time being, check the changes again later, hand acid.
Embedded: The use of UCOSIII