Statement: This article is original works, copyright belongs to the author of this blog, If You Need To reprint, please indicate the source of http://www.cnblogs.com/kingst/
In this chapter, we will briefly study the development process of the uC/OS system based on niosii. There are three tasks in the experiment: the first task is used to drive the DS1302 real-time clock, the second task is used to flash the LED lamp, and the third task is used to display the 654321 data through the serial port of the PC, if you are interested, you can add other functions on the Development Board.
1,UC/OSIIIntroduction
U C/o s is a real-time operating system with free open source code, small structure, and real-time kernel deprivation.
The predecessor of μC/OS-II is μC/OS, which was first originated from 1992, American embedded system expert Jean J. labrosse serialized articles published in May and June of embedded system programming, and published the source code of μC/OS in B of the magazine.
μC/OS and μC/OS-II are specially designed for embedded applications of computer, most of the Code is written in C language. CPU Hardware-related components are compiled in assembly language, with a total of about 200 lines of assembly language is compressed to a minimum, in order to facilitate transplantation to any other CPU. Users as long as there is a standard ansi c cross compiler, assembler, connectors and other software tools, you can embed the μC/OS-II into the development of the product. μC/OS-II has the characteristics of high execution efficiency, small space occupation, excellent real-time performance and strong scalability, the minimum kernel can be translated to 2 kb. μC/OS-II has been transplanted to almost all well-known CPUs.
Strictly speaking, uC/OS-II is just a real-time operating system kernel, which only contains basic functions such as task scheduling, task management, time management, memory management and communication and synchronization between tasks. No additional services are provided, such as input/output management, file system, and network. However, due to the good scalability and open source code of uC/OS-II, these non-essential functions can be fully implemented by the user as needed.
UC/OS-II aims to achieve a preemptible Real-Time Kernel Based on priority scheduling, and provide the most basic system services, such as semaphores, mailboxes, message queues, memory management, interrupt management.
1.1Task Management
UC/OS-II can support up to 64 tasks, corresponding to priority 0 ~ 63, where 0 is the highest priority. 63 is the lowest level. The system retains four tasks with the highest priority and four tasks with the lowest priority. All users can use 56 tasks.
UC/OS-II provides various function calls for task management, including creating tasks, deleting tasks, changing task priorities, task suspension and recovery.
Two tasks are automatically generated during system initialization: one is an idle task with the lowest priority, and the other is a system task, it has a low priority and is responsible for calculating the current cpu utilization.
1.2Time management
UC/OS-II time management is achieved through scheduled interruption, the scheduled interruption is generally 10 ms or 100 ms occurs once, the time frequency depends on the user's hardware system timer programming to achieve. The interval between interruptions is fixed, and the interruption also becomes a clock cycle.
UC/OS-II requires the user to call the system functions related to the clock cycle in the scheduled interrupt service program, such as the interrupt-level task switching function, the system time function.
1.3Memory Management
In ansi c, the malloc and free functions are used to dynamically allocate and release memory. However, in an embedded real-time system, multiple such operations may cause memory fragmentation, and the execution time of malloc and free is also unknown due to memory management algorithms.
Manage contiguous blocks of memory by partition in uC/OS-II. Each partition contains an integer with the same size as the memory block, but the memory size between different partitions can be different. When you need to dynamically allocate memory, the system selects an appropriate partition and allocates memory by block. When the memory is released, the block is put back to the partition it previously belongs to. This can effectively solve the fragmentation problem and the execution time is fixed.
1.4Inter-task communication and Synchronization
For a multi-task operating system, communication and synchronization between tasks are essential. UC/OS-II provides 4 synchronization objects, namely semaphores, mailboxes, message queues, and events. All these synchronization objects are created, waited, sent, and queried interfaces for communication and synchronization between processes.
1.5Task Scheduling
The uC/OS-II uses a real-time, multi-task kernel that is denied. The deprived real-time kernel is running at any time with the highest priority.
The scheduling of uC/OS-II tasks is a fully preemptible Scheduling Based on the task priority, that is, once the task with the highest priority is in the ready state, immediately preemptible the CPU resources of running low-priority tasks. To simplify the system design, uC/OS-II requires that all tasks have different priorities, because the priority of the task also uniquely identifies the task itself.
UC/OS-II detailed usage can refer to the relevant information.
2,FPGAUnderUC/OS-II
The following describes how to conduct a uC/oⅱ experiment on the EP2C208 black gold Development Board.
Step 1: Add a timer timer_ucos for the system clock cycle. The timer time is 100 ms (depending on the task ).
Step 2: Set the related options under the Nios. See the following procedure.
Open the Quart II project. Take the EP2C208 project of the black gold Development Board as an example. Go to the system Builder interface.
In System Contents on the left, click Peripherais on the left. In the pop-up menu, click "" on the left of Microcotroller Peripherais, as shown in.
Find "Interval Timer" and double-click it. In the pop-up window, click Set and click Finish.
Name it timer_ucos, for example.
The timer has been added. Click Generate to Generate the system.
Next, compile the Quart II project and connect the ". pof" interface to the EPC through the AS interface. Now the Quart II project has been completed ..
Click it to set up the Nias project. If the System Generation interface is not closed, you can click the "System Generation" Nios ii IDE button to perform the NIO project on the premise that the System has installed the niosii software.
Create a New project. Click "File" under "New", as shown in figure "Nio ii c/C ++ Application.
After entering, as shown in, and set according to the parameters in the figure (note ucoⅱ for the project name, ep2c8q for the system, Micro uC/OS-II tutorial for the uCOS-II template ).
Click the Next button, press set, and then click Finish.
Next, perform basic settings for the project. Right-click uCOSII and select "System Library Properties" in the pop-up menu. The following page is displayed and the settings are as follows. Click OK.
Replace the file content in ucosii_tutorial.c with the following code.
/** =================================================== ========================================================== =========** Filename: ds1302.c ** Description: DS1302 driver ** Version: 1.0.0 * Created: 2010.4.16 * Revision: none * Compiler: niosii 9.0 IDE ** Author: Mary * Email: ** =================================================== ========================================================== =========*/# include <stdio. h> # include <unistd. H> # include <string. h> # include "includes. h "# include" Stacks "# include" ds1302.h "# include" Stacks "# include" altera_avalon_timer_regs.h "# include" alt_types.h "# include" sys/alt_irq.h "// define the spine stack # define TASK_STACKSIZE 2048OS_STK initialize_task_stk [TASK_STACKSIZE]; OS _STK ds1302_task_stk [TASK_STACKSIZE]; OS _STK led_task_stk [TASK_STACKSIZE]; OS _STK seg_task_stk [TASK_STAC KSIZE]; // defines the priority # define INITIALIZE_TASK_PRIORITY 6 # define LED_TASK_PRIORITY 10 # define DS1302_TASK_PRIORITY 11 # define SEG_TASK_PRIORITY 12 // The format is: second minute hour day month week Year unsigned char time [7] = {0x00,0x19,0x14,0x17,0x03,0x17,0x10 }; unsigned char ti [] [7] = {"1", "2", "3", "4", "5", "6", "day "}; alt_u8 segtab [10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,0x92,0x82, 0xf8, 0x80, 0x90 }; unsigned char bittab [6] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf}; unsi Gned char led_buffer [8] = {1, 2, 4, 5, 6, 7, 8}; static unsigned char cnt = 0; void seg_handler (void ); /** = FUNCTION ================================================== ============================================================ * Name: ds1302_task * Description: task 1 calls the ds1302 driver and displays data through the serial port * ========================== ========================================================== ===================== */void ds1302_task (void * pdata) {INT8U return_code = OS _NO_ERR; ds1302. Set_time (time); printf ("Hello from NiO II! \ N "); while (1) {printf (" Hello from NiO II! \ R \ n "); ds1302.get _ time (time); printf (" % 02d-% 02d-% 02d % 02d: % 02d: % 02d week % s \ r \ n ", time [6], time [4], time [3], time [2], time [1], time [0], ti [time [5]-1]); OSTimeDlyHMSM (0, 0, 1, 0 );}} /** = FUNCTION ================================================== ============================================================ * Name: led_task * Description: task 2 call the LED driver and display the data through the serial port * ==================================== ========================================================== ==================== = */Void led_task (void * pdata) {INT8U return_code = OS _NO_ERR; unsigned int num = 0; LED-> DATA = 0 xffffffff; while (1) {printf ("led is running! \ R \ n "); if (num % 2 = 0) LED-> DATA = 0 xffffffff; else LED-> DATA = 0; num ++; OSTimeDlyHMSM (0, 0, 2, 0 );}} /** = FUNCTION ================================================== ============================================================ * Name: seg_task * Description: task 3: Call the digital tube driver and display data through the serial port * ============================== ========================================================== ===================== */void seg_task (void * pdata) {INT8U return_code = OS _NO_ERR; unsigned int numseg = 0; while (1) {numseg ++; seg_handler (); OSTimeDlyHMSM (0, 0, 0,300 );}} /** = FUNCTION ================================================== ============================================================ * Name: seg_handler * Description: * ===================================================== ========================================================== ======= */void seg_handler (void) {values (SEG_SEL_BASE, 0xff); IOWR_ALTERA_AVALON_PIO_DATA (SEG_SEL_BASE, bittab [cnt]); values (SEG_DAT_BASE, segtab [led_buffer [cnt]); cnt ++; if (cnt = 6) cnt = 0 ;} /** = FUNCTION ================================================== ============================================================ * Name: main * Description: * ===================================================== ========================================================== ======= */void initialize_task (void * pdata) {INT8U return_code = OS _NO_ERR; initCreateTasks (); return_code = OSTaskDel (OS _PRIO_SELF); while (1 );} /** = FUNCTION ================================================== ============================================================ * Name: main * Description: * ===================================================== ========================================================== ======= */int main (int argc, char * argv [], char * envp []) {INT8U return_code = OS _NO_ERR; return_code = OSTaskCreateExt (initialize_task, NULL, (void *) & signature [TASK_STACKSIZE], comment, initialize_task_stk, TASK_STACKSIZE, NULL, 0); OSStart (); return 0 ;} /** = FUNCTION ================================================== ============================================================ * Name: initCreateTasks * Description: * ===================================================== ========================================================== ======= */int initCreateTasks (void) {INT8U return_code = OS _NO_ERR; return_code = OSTaskCreateExt (ds1302_task, NULL, (void *) & empty [TASK_STACKSIZE], empty, TASK_STACKSIZE, NULL, 0 ); return_code = OSTaskCreateExt (led_task, NULL, (void *) & values [TASK_STACKSIZE], values, TASK_STACKSIZE, NULL, 0); return_code = OSTaskCreateExt (seg_task, NULL, (void *) & seg_task_stk [TASK_STACKSIZE], SEG_TASK_PRIORITY, SEG_TASK_PRIORITY, seg_task_stk, TASK_STACKSIZE, NULL, 0); return 0 ;}
Compile the niosii project and download the project to the new source. The method for downloading the project is described in the previous chapter. The following figure shows the data on the serial port debugging tool on the PC. On the Development Board, you can also see four LED lights flashing and digital display 654321.