快 速 porting μc/os-ii 及 driver 解 說 沈 智 明 晶 心 科 技 公 司 資 深 經 理 Email: jimmy@andestech.com WWW.ANDESTECH.COM
Outline Application Building Blocks μc/os-ii/rtos Introduction μc/os-ii & FreeRTOS Merge μc/os-ii kernel into Andes Demo Program Driver Introduction & Demo Analysis 2
Application Building Blocks Fundamental All-C Embedded Programming environment Optimized MCUlib, newlib, glibc, and uclibc Concise linker script Linker Scattering-and-Gathering (LdSaG), and its tool Sample programs to demo AndesCore s features Real-Time Operating System FreeRTOS, ecos, and contiki (open source) μc/os-ii, μc/os-iii and middleware (from Micriμm) ThreadX and middleware (from Express Logic) Nucleus kernel (from Mentor Graphics) μc3 micro c cube (μitron from eforce) Linux, Middleware and SW Framework U-Boot (mainlined), OpenWrt OProfile, gdbserver, strace GTK, Qt, and rdesktop Linux kernel 2.6.32 & 3.4.x and device drivers 3
Multi-tasking 4
Mutual Exclusion Concepts Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections. Critical Section of Code: Also called critical region, is code that needs to be treated indivisibly. Once the section of code starts executing, it must not be interrupted. Resource: A resource is any entity used by a task, it can thus be an I/O device, such as a printer, a variable or an array. Shared Resource: A shared resource is that can be used by more than one task. Each task should gain exclusive access to the shared resource to prevent data corruption. This is called mutual exclusion. http://zh.wikipedia.org/wiki/%e4%ba%92%e6%96% A5%E9%94%81 5
Reentrancy Function A reentrant function can be used by more than one task without fear of data corruption. It can be interrupted at any time and resumed at a later time without lost of data. It either use local variables or protect data when global variables are used. Example void strcpy(char *dest, char *src) { while (*dest++ = *src++) { ; } *dest = NUL; } 6
Non-Reentrant function Example : int Temp; void swap(int *x, int *y) { Temp = *x; *x = *y; *y = Temp; } 7
μc/os II Feature Open source code Memory footprint is about 20K for a fully functional kernel. Code size: 5KB ~ 24KB Preemptive priority-driven real-time scheduling Portable: Support many platforms:andes,x86,arm,mips ROMable: Was designed for embedded applications. It s easy as part of a product Scalable: you can use only the services that you need in your application. Multi-tasking: can manage up to 64 tasks, however, the current version of the software reserves eight (8) of these tasks for system use. Deterministic: Execution time of all μc/os-ii functions and services are deterministic. Task stacks: Each task requires its own stack, however, μc/os-ii allows each task to have a different stack size. Services: provides a number of system services such as mailboxes, queues, semaphores, fixed-sized memory partitions, time related functions, etc. Interrupt Management: Interrupts can be nested up to 255 levels deep. Robust and reliable: 8
Task states OSTaskDel OSTaskCreate OSTaskCreateExt Waiting Dormant OSIntExit Ready Running ISR OSTaskDel Task State Diagram OSMBoxPost OSQPost OSQPostFront OSSemPost OSTaskResume OSTimeDlyResume OSTimeTick OSTaskDel OSStart OS_TASK_SW Task is Preempted OSMBoxPend OSQPend OSSemPend OSTaskSuspend OSTimeDly Interrupt OSIntExit 9
uc/os-ii & FreeRTOS Kernel ROM Footprint range uc/os-ii FreeRTOS 6 ~ 26 KB 4 ~ 9 KB Tasks counts support 255 > 255 Multitasking model Code Execution Entities Synchronization Mechanisms License Extra Features Preemptive with unique priority tasks Tasks, ISR Queque Semaphores Mutex Flag MailBox commercial (45-day trial) TCPIP USB host/device/otg FAT CAN GUI Bluetooth Preemptive with round robin for same priority tasks Tasks, Coroutine, ISR Queque Semaphores Mutex Flag GPLv3 with exception TCPIP FAT (possibility of migration to OpenRTOS and SafeRTOS for commercial supporting) 10
Merge μc/os-ii kernel into Andes Demo Program Access website http://www.micrium.com/page/products/rtos/os-ii Download Micrium-uCOS-II-V291.ZIP kernel source code Merge μc/os II and Andes Demo Program tar zxvf ucos-ii_withoutkernel.tgz unzip Micrium-uCOS-II-V291.ZIP cp Micrium/Software/uCOS-II/Source/* rtos-ucos-ii/ucosii/source/ tar zcvf ucos-ii.tgz rtos-comm/ rtos-ucos-ii/ 11
Build&Run Demo Program How to build demo program Source Andes cross-complier Untar ucos-ii source code to workspace #tar zxvf ucos-ii.tgz Build individual APP by makefile #cd rtos-ucos-ii #make APP=BH AE210P=1 DEBUG=1 all After a while. Delivered image by AICE #ddd -debugger nds32le-elf-gdb build/bh.elf Delivered image by SD card # cp bin/bh.bin to SD card 12
For Example -> Timer What is our purpose? Setup timer interval. Hookup ISR Driver Introduction Start.S -> BSP_Init ->main /* Init all timer device */ hal_timer_init(0); /* System tick init */ BSP_Tmr_TickInit(0x1, (MB_PCLK / TICK_HZ), IRQ_SYS_TICK_VECTOR, tick_isr_fp); 13
Demo Analysis Why RTOS? The difference between RTOS & Non-OS How Threads/Tasks work within RTOS? Semaphore, event, queue.. Kernel scheduler Task Yield: task A-> scheduler -> task B. While(1){ Event_wait.... } While(1){ semaphore.... } Interrupt Yield: task A ->interrupt -> scheduler -> task B 14
Bottom half Analysis How Threads/Tasks work within RTOS? bottom half Thread + Semaphore Kernel scheduler BH While(1){ getchar() post_semaph ore... } While(1){ pend_semap hore.... } 15
Andes e-service is a web-based support ticket system. It is a convenient way to get quick response of your question. How to apply for a new account? Please send your information to es.admin@andestech.com Including your name, e-mail, company name and telephone number 16
How to Use E-service? E-service website: http://es.andestech.com/ilogin.php You can track your ticket in e-service system 17