μC/OS-II experiment: Experiment 5 Memory Management

Source: Internet
Author: User
Tutorial Description :... Involved μC/OS-II system function :... Experiment code: App. c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Static void showmeminfo (void)
{
OS _mem_data mem_data;
Osmemquery (pmem, & mem_data );

Printf ("begining address of memory: \ t % x \ r \ n", (INT) (mem_data.osaddr ));
Printf ("block size in the memory area: \ t % d \ r \ n", (INT) (mem_data.osblksize ));
Printf ("Free blocks in the memory area: \ t % d \ r \ n", (INT) (mem_data.osnfree ));
Printf ("used blocks in the memory area: \ t % d \ r \ n", (INT) mem_data.osnused );
Printf ("\ r \ n ");
Ostimedlyhmsm (0, 0, 2, 0 );
}
Static void getmem (int8u I)
{
Int8u err;

// Apply for memory space

Pmemmsg [I] = osmemget (pmem, & ERR );
If (pmemmsg [I]! = (Void *) null & err = OS _no_err)
{
Printf ("got memory _ % d. \ r \ n", I); // The memory block has been allocated
Showmeminfo ();
}
Else
Printf ("not got memory _ % d. \ r \ n", I );

}

Static void putmem (void * pmemmsg, int8u I)
{
Int8u err;
// Release memory space
Err = osmemput (pmem, pmemmsg );
If (ERR = OS _no_err)
{
Printf ("released memory _ % d. \ r \ n", I);/* release the memory block */
Showmeminfo ();
}
Else
Printf ("not exist memory _ % d. \ r \ n", I );
}

Void taskmem1 (void * pdata)
{
Int8u I;
I = 0;

While (1)
{
Getmem (I ++ % msgsize );
Ostimedlyhmsm (0, 0, 2, 0 );
}
}

Void taskmem2 (void * pdata)
{
Int8u I;
I = 0;

While (1)
{
Putmem (pmemmsg [I ++ % msgsize], I ++ % msgsize );
Ostimedlyhmsm (0, 0, 2, 0 );
}
}

App. h
1
2
3
4
5
# Define msgsize 3
Global OS _mem * pmem; // set the returned value storage for the osmemcreate Function
Global void * pmemmsg [3]; // store the returned values for the osmemget Function
Global int8u membuf [2] [128]; // initialize the memory allocated space
App_cfg.h
1
2
3
4
5
6
7
8
9
10
11
# Define mem1_stk_size 64
# Define mem2_stk_size 64

# Define mem1_task_prio 20
# Define mem2_task_prio 21

Global OS _stk task_mem1_stk [mem1_stk_size];
Global OS _stk task_mem2_stk [mem2_stk_size];

Void taskmem1 (void * pdata );
Void taskmem2 (void * pdata );

Main. C: taskstart
1
2
3
4
5
/* Experiment 5 memory management */
Pmem = osmemcreate (membuf, 2,128, & ERR );

Ostaskcreate (taskmem1, 0, (OS _stk *) & task_mem1_stk [mem1_stk_size-1], mem1_task_prio );
Ostaskcreate (taskmem2, 0, (OS _stk *) & task_mem2_stk [mem2_stk_size-1], mem2_task_prio );

Experiment results: taskmem1 applied for memory space and taskmem2 released the memory space. Note: If the memory is successfully obtained, the condition is (pmemmsg [I]! = (Void *) null & err = OS _no_err ). Reference materials and supporting code: uCOS-II experiment guide ucos_demo Note: It is correct to refer to this document, but there is a reason why I put it at the end, because there are problems with the concept or code of several experiments (almost every ⊙ ﹏ ⊙ B Khan, I have pointed out earlier. If you are not clear about it, let's make a comparison.

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.