[vc_row][vc_column][vc_column_text]μC/OS-II is a portable, ROMable, scalable, preemptive, real-time deterministic multitasking kernel for microprocessors, microcontrollers and DSPs. In this post, we will look at how to use pcDuino to learn μC/OS II.
In this post, the per-cursor of the following are used to indicate where the commands are typed:
- $: it means that the command is typed on pcDuino
- #: it means that the command is typed on a x86 linux machine.
- >: it means that the command is typed under u-boot status.
[/vc_column_text][vc_tour][vc_tab title=”Parts List” tab_id=”1392833824-1-34″][vc_column_text]In this post, we are going to need the following parts:
- 1 x pcDuino v2
- 1 x Linker Base Shield
- 1 x LED
- Several jumper wires
[/vc_column_text][/vc_tab][vc_tab title=”Download and Install μC/OS II” tab_id=”1392833824-2-81″][vc_column_text]1. Our μC/OS II code is hosted at github. If we haven’t got the chance to install git, we can use the following command to install git:
$sudo apt-get install git git-core
2. Navigate to the working directory /home/ubuntu, and run the following commands:
$git clone https://github.com/linksprite/ucos-ii-for-pcDuino
3. let’s look at what’s inside the directory:
ubuntu@ubuntu:~/ucos_linux_2.86$ ls Makefile app arduino build config.mk ucos ucos_2.86_original
Directory app is used to store the source files of the ucos applications.
Directory arduino is used to hold the API of arduino.
Build is the directory where the generated files will be stored during building process.
ucos is the directory that holds the source files of ucos system.
The documents of ucdos are stored at ucos_2.86_original.
4. Enter into directory arduino:
$cd arduino
5. Build the library libarduoino.so for arduino style programming:
$make
The files under arduino will look like:
ubuntu@ubuntu:~/ucos_linux_2.86/arduino$ ls Makefile hardware libarduino.a libarduino.so libraries output sample
6. Return to the ucos root directory:
$cd ..
7. Build the whole system:
$make
8. After it finishes, it will generate an app named ucos_sample.
ubuntu@ubuntu:~/ucos_linux_2.86$ make make[1]: Entering directory `/home/ubuntu/ucos_linux_2.86/ucos’ make[2]: Entering directory `/home/ubuntu/ucos_linux_2.86/ucos/port’ [CC] /home/ubuntu/ucos_linux_2.86/build/os_cpu_c.o [LD] /home/ubuntu/ucos_linux_2.86/build/port.o make[2]: Leaving directory `/home/ubuntu/ucos_linux_2.86/ucos/port’ [CC] /home/ubuntu/ucos_linux_2.86/build/os_core.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_dbg_r.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_flag.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_mbox.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_mem.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_mutex.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_q.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_sem.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_task.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_time.o [CC] /home/ubuntu/ucos_linux_2.86/build/os_tmr.o [LD] /home/ubuntu/ucos_linux_2.86/build/ucos.o make[1]: Leaving directory `/home/ubuntu/ucos_linux_2.86/ucos’ make[1]: Entering directory `/home/ubuntu/ucos_linux_2.86/app’ sample.c: In function ‘MyTask’: sample.c:31:9: warning: type defaults to ‘int’ in declaration of ‘flag1’ [-Wimplicit-int] sample.c: In function ‘main’: sample.c:79:7: warning: unused variable ‘sTask4’ [-Wunused-variable] sample.c:78:7: warning: unused variable ‘sTask3’ [-Wunused-variable] sample.c:77:7: warning: unused variable ‘sTask2’ [-Wunused-variable] sample.c:74:8: warning: unused variable ‘Stk5’ [-Wunused-variable] sample.c:73:8: warning: unused variable ‘Stk4’ [-Wunused-variable] sample.c:72:8: warning: unused variable ‘Stk3’ [-Wunused-variable] sample.c:71:8: warning: unused variable ‘Stk2’ [-Wunused-variable] [CC] /home/ubuntu/ucos_linux_2.86/build/sample.o [LD] /home/ubuntu/ucos_linux_2.86/build/app.o make[1]: Leaving directory `/home/ubuntu/ucos_linux_2.86/app’ [LD] ucos_sample -lpthread -larduino -L./arduino ubuntu@ubuntu:~/ucos_linux_2.86$ ls Makefile app arduino build config.mk ucos ucos_2.86_original ucos_sample
[/vc_column_text][/vc_tab][vc_tab title=”Wiring Diagram” tab_id=”1392834793147-2-6″][vc_column_text][/vc_column_text][/vc_tab][vc_tab title=”Sample Program” tab_id=”1392834889974-3-6″][vc_column_text]The test code is located under app.
$nano app/sample.c
The following is the test code:
/* ********************************************************************************************************* * sample.c * * Description: This sample program uses the ucos linux port to start 5 simple tasks. * * Author: Philip Mitchell * ********************************************************************************************************* */ #include <stdio.h> #include <stdlib.h> #include "ucos_ii.h" #include <core.h> #include <string.h> int led_pin = 1; int btn_pin = 5; void hardware_init() { pinMode(led_pin, OUTPUT); } /* Function common to all tasks */ void MyTask( void *p_arg ) { char* sTaskName = (char*)p_arg; static flag1 = 1; #if OS_CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr = 0; #endif while(1) { /* printf uses mutex to get terminal access, therefore must enter critical section */ OS_ENTER_CRITICAL(); printf( "Name: %s\n", sTaskName ); if(!strcmp(sTaskName,"Task 1")) { if(flag1 == 1) { flag1 = 0; printf("HIGH\n"); digitalWrite(led_pin, HIGH); } else { flag1 = 1; printf("LOW\n"); digitalWrite(led_pin, LOW); } } OS_EXIT_CRITICAL(); /* Delay so other tasks may execute. */ OSTimeDly(50); }/* while */ } int main (void) { /* pthreads allocates its own memory for task stacks. This UCOS linux port needs a minimum stack size in order to pass the function information within the port. */ hardware_init(); INT8U Stk1[ OSMinStkSize() ]; INT8U Stk2[ OSMinStkSize() ]; INT8U Stk3[ OSMinStkSize() ]; INT8U Stk4[ OSMinStkSize() ]; INT8U Stk5[ OSMinStkSize() ]; char sTask1[] = "Task 1"; char sTask2[] = "Task 2"; char sTask3[] = "Task 3"; char sTask4[] = "Task 4"; // char sTask5[] = "Task 5"; OSInit(); OSTaskCreate( MyTask, sTask1, (void*)Stk1, 4 ); // OSTaskCreate( MyTask, sTask2, (void*)Stk2, 5 ); // OSTaskCreate( MyTask, sTask3, (void*)Stk3, 6 ); // OSTaskCreate( MyTask, sTask4, (void*)Stk4, 7 ); // OSTaskCreate( MyTask, sTask5, (void*)Stk5, 8 ); OSStart(); return 0; }
Connect LED to pin 1 as shown in the section “wiring diagram”, and run the executed program:
ubuntu@ubuntu:~/ucos-ii-for-pcDuino$ ./ucos_sample 1Name: Task 1 HIGH Name: Task 1 LOW Name: Task 1 HIGH Name: Task 1 LOW Name: Task 1
We will observe that the LED is blinking.[/vc_column_text][/vc_tab][vc_tab title=”Summary” tab_id=”1392863489299-4-3″][vc_column_text]From the above sample, we can see that it is very convenient to arduino style API under ucos. The original code in setup can be implemented in hardware_init, and the code used to be included in loop, can be written in task.[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.