hjr-Embedded: Ucos-ii semaphore, mutex semaphore, message mailbox

Source: Internet
Author: User
Tags data structures inheritance mutex semaphore usleep

Because Ucos-ii is a multitasking-based operating system

This section is about three concepts that are important in the mission.

Before the first science several important functions:

(1) Osstart ();: Operating system multi-task startup

(2) Osinit () L: Operating system global variables, data structure initialization, establishment of idle, statistical, scheduled tasks

(1) Create several user tasks, priority

Ostaskcreate (the first address pointer of the task handler, task handler function parameter, task assignment stack top pointer, task priority number);

There are a few sentences that the program has several tasks

The priority number represents the priority of the task, starting with 0, the lower the priority, the higher the ucos-ii always runs into the ready state with the highest priority task

OSTIMEOLG (1)//delay a clock beat

OSTIMEDLYHMSM (0,0,1,0)//time delay, minutes, seconds, milliseconds

The following officially begins

1, the use of signal volume method

(1) Define an event pointer, such as "Os_event *ledsem"; here Ledsem an event pointer

(2) Call ossemcreate (); function Create semaphore

For example "Ledsem = ossemcreate (0);", at this point Ledsem is loaded with a semaphore, the initial calculated value is 0.

(4) Request the semaphore in another task, e.g. "Ossedpend (Ledsem,0,&err)"

#include <stdio.h> #include "includes.h" #include "system.h"//Macro definition pio_led_base #include "altera_avalon_pio_
Regs.h "OS_STK task1_stk[task_stacksize];//for the task definition stack to be created OS_STK task2_stk[task_stacksize]; #define Task1_priority 1//Assign a priority number to the task created #define Task1_priority 2 #define task_stacksize 2048//define 2048KB os_event *
randomsem;//defines a pointer to the semaphore Randomsem (defines the event pointer) int8u err; void Task1 (void* pdata)//define User Task 1{while (1)//Must Die Loop {ossempend (RANDOMSEM,0,&AMP;ERR);//request semaphore [OS_SEM.C] Iowr_altera_ Avalon_pio_data (pio_led_base,0x00);//#define Pio_led_base 0x4001000 usleep (500000);//The task hangs 500000 microseconds, or 0.5 seconds ossepost ( RANDOMSEM);//periodically releasing semaphores in a task ostimedly (1);//The task that called the function is delayed by a clock beat ([in os_time.c])}} void Task2 (void* pdata)//define User Task 2{W Hile (1)//Must Die Loop {ossempend (RANDOMSEM,0,&AMP;ERR);//request semaphore [OS_SEM.C] Iowr_altera_avalon_pio_data (pio_led_base,0x0f );//#define Pio_led_base 0x4001000 usleep (500000);//The task hangs 500000 microseconds, or 0.5 seconds ossepost (RANDOMSEM);//periodically releases the semaphore in a task Ostime
Dly (1);//The task that invokes the function is delayed by a clock beat ([in os_time.c])	}} int main (void) {osinit ();//for UCOS-II operating system initialization (initialization of global variables or data structures, establishment of system services such as idle, statistical, and Scheduled Tasks)//Call the function before calling the Osstart function (located in the Os_
	CORE.C) Iowr_altera_avalon_pio_data (pio_led_base,0x00); Randomsem = ossemcreate (1);//Call Ossemcreate (); Create semaphore, semaphore initial value set to 1//create user task [LOS_TASK.C] Ostaskcrateext (task1,//function name NULL, function parameter (void *) &task1_stk[task_stacksize-1],//stack top pointer task1_priority,//priority number task1_priority,//task ID number ta
	sk1_stk,//stack bottom pointer ask_stacksize,//stack size null,//user-defined spatial pointer 0//task option parameter); Ostaskcrateext (task1,//function name null,//functional parameter (void *) &task2_stk[task_stacksize-1],//stack top pointer task2_priority,//priority number T
	ask2_priority,//task ID number task2_stk,//stack bottom pointer task_stacksize,//stack size null,//user defined space pointer 0//task option parameter); Osstart ()//Start ucos-ii operating system multitasking scheduling [must be used (in os_core.c) after calling the Osinit function]}

2. How to use the mutex signal volume

(1) Define the event control module pointer, e.g. "Os_event *ledmutex;"

(2) Specify a priority number Mx_prio as the priority inheritance priority (PIP)

The value of the PIP should be less than the priority number of all tasks requesting the shared resource

(3) Call Osmutexcreate (); The function creates a mutually exclusive semaphore, such as "Ledmutex = Osmutexcreate (Mx_prio,&err);"

(4) If a task is to use a shared resource, the Osmutexpend () should be called first, the function requests the mutex semaphore, and after the request of the mutex, the shared resource is used, and then the Osmutexpost () is called when the function releases the mutex signal.

#include <stdio.h> #include "includes.h" #include "system.h"//Macro definition pio_led_base #include "altera_avalon_pio_
Regs.h "OS_STK task1_stk[task_stacksize];//for the task definition stack to be created OS_STK task2_stk[task_stacksize];
#define Task1_priority 1//assigns a priority number to the task created #define TASK1_PRIORITY 2 #define TASK_STACK_SIZE 512;
Os_event *randommutex;
int8u My_prio = 2;//my_pario is called Priority inheritance priority (PIP) [os_mutex.c] int8u my_err;
void Task1 (void* pdata);
void Task2 (void* pdata);
	void Main (void) {osinit ();
	Iowr_altera_avalon_pio_data (pio_led_base,0x00); Randommutex = Osmutexcreate (My_prio,&my_err);//Create and initialize a mutex semaphore for the task to gain exclusive access to the shared resource//must ensure my_ The value of PRIO is less than all of the task priority ostaskcreate that may request a mutex semaphore (task1,//function name null,//functions parameter &task1_stk[task_stack_size-1],//stack top pointer Task1_prio
	Priority number);
	Ostaskcreate (task2,//function name null,//functions parameter &task2_stk[task_stack_size-1],//stack top pointer task2_prio//priority number);
Osstart (); } void Task1 (void* pdata) {while (1) {osmutexpend (RANDOMMUTEX,0,&AMP;MY_ERR);//Request Mutex semaphore [OS_MUTEX.C] Iowr_altera_avalo N_pio_data (pio_led_base,0xFF);
		OSTIMEDLYHMSM (0,0,1,0); Osmutexpost (Randommutex);//Release Mutex semaphore}} void Task1 (void* pdata) {while (1) {osmutexpend (RANDOMMUTEX,0,&AMP;MY_ERR);//Request
		Mutex semaphore [os_mutex.c] Iowr_altera_avalon_pio_data (pio_led_base,0x00);
		OSTIMEDLYHMSM (0,0,1,0); Osmutexpost (Randommutex);//Release Mutex semaphore}}

3. How to use the message mailbox

(1) Define event pointers, such as "Os_event *ledmbox;", where Ledmbox is an event pointer

(2) Before using the message mailbox, you must call Osmboxcreate (); function Create message mailbox

For example, "Ledmbox = Osmboxcreate (NULL);", which means creating an empty message mailbox or assigning an initial value to the message when the message mailbox is created.

(3) First define a one-dimensional array in a task, such as "ini8u mymessage[80", and then the message to be delivered is stored in the mymessage array, for example "strcpy ((char *) mymessage, ' Led 1 Light ')" , and then call the function Osmboxpost periodically to release the message. For example "Osmboxpost (Ledmbox, (void *) &mymessage[0]);"

(4) First define a local one-dimensional pointer variable in another task to hold the received message, such as "void *pmsg;", and then call the function osmboxpend the message in the mailbox of the request message.

For example "pmsg = Osmboxpend (Edmbox,os_ticks_per_sec,&err);", the message "Led 1 light." "is passed from another task to the task.

#include <stdio.h> #include "includes.h" #include "system.h"//Macro definition pio_led_base #include "altera_avalon_pio_
Regs.h "OS_STK task1_stk[task_stacksize];//for the task definition stack to be created OS_STK task2_stk[task_stacksize];
#define Task1_priority 1//Assign a priority number to the task created #define TASK1_PRIORITY 2 #define TASK_STACKSIZE 2048 os_event *randomsem;
Int8u My_err;
	Os_event *msgmbox;//defines the message mailbox pointer void Task1 (void* pdata) {int txmsg = 0;
		while (1) {osmboxpost (msgmbox,&txmsg);//Incoming message to mailbox (send message to TASK2) [os_mbox.c] txmsg++;
		if (txmsg = = 5) txmsg = 0;
	OSTIMEDLYHMSM (0,0,1,0);
	}} void Task2 (void* pdata) {int * rxmsg = 0; while (1) {rxmsg = (int *) osmboxpend (MSGMBOX,0,&AMP;MY_ERR);//Request message to Mailbox [OS_MBOX.C] (return message pointer, which can get the message) Iowr_altera_
		Avalon_pio_data (PIO_LED_BASE,*RXMSG);
	OSTIMEDLYHMSM (0,0,1,0);
	}} int main (void) {osinit ();
	Iowr_altera_avalon_pio_data (pio_led_base,0x00); MsgBox = Osmboxcreate ((void *) 0);//Create and initialize a message mailbox (if the parameter is not empty, the new mailbox will contain a message) [OS_MBOX.C] Ostaskcreate (task1,//set up the task null, &task1_stk[task_stacksize-1],task1_priopity);
	Ostaskcreate (task2,//set up task null,&task2_stk[task_stacksize-1],task2_priopity);
	Osstart ();
return 0; }





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.