Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleCreating a Memory Partition
          OS_MEM       MyPartition;                                    (1) 
          CPU_INT08U   MyPartitionStorage[12][100];                    (2) 
           
           
          void  main (void)                                            (3) 
          {
              OS_ERR  err;
              :
              :
              OSInit(&err);
              :
              OSMemCreate((OS_MEM    *)&MyPartition,                   (4) 
                          (CPU_CHAR  *)"My Partition",                 (5) 
                          (void      *)&MyPartitionStorage[0][0],      (6) 
                          (OS_MEM_QTY ) 12,                            (7) 
                          (OS_MEM_SIZE)100,                            (8) 
                          (OS_ERR    *)&err);                          (9) 
              /* Check 'err' */
              :
              :
              OSStart(&err);
          }


Panel
borderColorbgColor#f0f0f0

(1)  An application needs to allocate storage for a memory partition control block (i.e.  OS_MEM  structure). This can be a static allocation as shown here or  malloc()  can be used in the code. However, the application code must not deallocate the memory control block.

(2) The application also needs to allocate storage for the memory that will be split into memory blocks. This can also be a static allocation or malloc() can be used. The same reasoning applies. Do not deallocate this storage since other tasks may rely on the existence of this storage.

(3) Memory partition must be created before allocating and deallocating blocks from the partition. One of the best places to create memory partitions is in main() prior to starting the multitasking process. Of course, an application can call a function from main() to do this instead of actually placing the code directly in main().

(4) You pass the address of the memory partition control block to OSMemCreate(). You should never reference any of the internal members of the OS_MEM data structure. Instead, you should always use µC/OS-III’s API.

(5) You can assign a name to the memory partition. There is no limit to the length of the ASCII string as µC/OS-III saves a pointer to the ASCII string in the partition control block and not the actual characters.

(6) You then need to pass the base address of the storage area reserved for the memory blocks.

(7) Here, you specify how many memory blocks are available from this memory partition. Hard coded numbers are used for the sake of the illustration but you should instead use #define constants.

(8) You need to specify the size of each memory block in the partition. Again, a hard coded value is used for illustration, which is not recommended in real code.

(9) As with most µC/OS-III services, OSMemCreate() returns an error code indicating the outcome of the service. The call is successful if “err” contains OS_ERR_NONE.

...