dce 2010 ChibiOS/RT A Real-time OS BK TP.HCM www.chibios.org by Hoàng Ngọc Tuấn Anh 50700041 and Nguyễn Quang Vinh 50702974 dce 2010 Overview – Authur : Giovanni Di Sirio. – ChibiOS/RT (ちびOS/RT) means small Real Time Operating System. – Designed for realtime applications. – Preemptive scheduling. – 128 priority levels. Multiple threads at the same priority level are allowed. – Round Robin scheduling for threads at the same priority level. Embedded system 2 dce 2010 Overview • An embedded real time OS which is: – High portability, compact size, efficient context switching – Static architecture – Dynamic extensions, dynamic object – Many objects : threads, semaphores, mutexes… – Support many hardware abstract drivers – Support architecture: ARM7, ARM Cortex-M0, CortexM3, AVR, MSP430, SMT8… Embedded system 3 dce 2010 Overview – Programming language : C – Code size ( compiled) about 5kB ~ 10kB • Minimal build can reduce code size to about 1,5kB – Context switch time: 2µS ~ 10µS – Extensive test suite with benchmarks Embedded system 4 dce 2010 License: • Multiple licensing options exist for ChibiOS/RT: – Pure GPL3 license. • For unstable (alpha, beta…) release – GPL3 license with Linking Exception. • For stable release, with exception for closed source app – Commercial license. • Features and price see at webpage • http://chibios.org/dokuwiki/doku.php?id=chibios:commerc ial Embedded system 5 dce 2010 System state: Embedded system 6 dce 2010 Kernel subsystem • • • • • • • • • • Version numbers and Indentification Configuration Types Base kernel services Synchroniztion Memory Management I/O support Registry Debug Internals Embedded system 7 dce 2010 Version and Indentification • Define kernel relate info – – – – – – – – – – #define _CHIBIOS_RT_ ChibiOS/RT identification macro. #define CH_KERNEL_VERSION "2.3.2unstable" Kernel version string. #define CH_KERNEL_MAJOR 2 Kernel version major number. #define CH_KERNEL_MINOR 3 Kernel version minor number. #define CH_KERNEL_PATCH 2 Kernel version patch number. Embedded system 8 dce 2010 Configuration • Define kernel settings and hooks Embedded system 9 dce 2010 Types • Define system types and macros Embedded system 10 dce 2010 Base kernel services Embedded system 11 dce 2010 Base kernel services • System Management chSysInit (void) chSysTimerHandlerI (void) WORKING_AREA (_idle_thread_wa, IDLE_THREAD_STACK_SIZE) _idle_thread (void *p) Embedded system 12 dce 2010 Base kernel services • Scheduler _scheduler_init (void) chSchReadyI (Thread *tp) chSchGoSleepTimeoutS (tstate_t newstate, systime_t time) chSchWakeupS (Thread *ntp, msg_t msg) chSchDoRescheduleI (void) chSchRescheduleS (void) chSchIsRescRequiredExI (void) Embedded system 13 dce 2010 Thread state: Embedded system 14 dce 2010 Base kernel services • Threads chThdCreateStatic (void *wsp, size_t size, tprio_t prio, tfunc_t pf, void *arg) chThdSetPriority (tprio_t newprio) chThdResume (Thread *tp) chThdTerminate (Thread *tp) chThdSleep (systime_t time) chThdExit (msg_t msg) chThdWait (Thread *tp) Embedded system 15 dce 2010 Base kernel services • Time and Virtual timers chVTSetI (VirtualTimer *vtp, systime_t time, vtfunc_t vtfunc, void *par) chVTResetI (VirtualTimer *vtp) bool_t chTimeIsWithin (systime_t start, systime_t end) Embedded system 16 dce 2010 Synchronization Embedded system 17 dce 2010 Counting Semaphores • Signal: The semaphore counter is increased and if the result is non-positive then a waiting thread is removed from the semaphore queue and made ready for execution. • Wait: The semaphore counter is decreased and if the result becomes negative the thread is queued in the semaphore and suspended. • Reset: The semaphore counter is reset to a non-negative value and all the threads in the queue are released. Embedded system 18 dce 2010 Counting Semaphores • • • • • void void void msg_t msg_t • void • void Embedded system chSemInit (Semaphore *sp, cnt_t n) chSemReset (Semaphore *sp, cnt_t n) chSemResetI (Semaphore *sp, cnt_t n) chSemWait (Semaphore *sp) chSemWaitTimeout (Semaphore *sp, systime_t time) chSemSignal (Semaphore *sp) chSemSignalI (Semaphore *sp) 19 dce 2010 Counting Semaphores • msg_t : – RDY_OK – RDY_RESET – RDY_TIMEOUT • systime_t : – TIME_IMMEDIATE – TIME_INFINITE Embedded system : immediate timeout : no timeout 20 dce 2010 Binary Semaphores • Use the existing counting semaphores • Counter of binary semaphores is not allowed to grow above the value 1 • Not implement the priority protocol. • Have only two defined states : – Taken – Not taken Embedded system 21 dce 2010 Mutexs Semaphores • Same as Tkernel • Defined states : – Lock – Unlock Embedded system 22 dce 2010 Events Flag • Each thread has a mask of pending event flags inside • Operations : – Wait – Clear – Signal – Broadcast – Dispatch Embedded system 23 dce 2010 Events Flag • void chEvtRegisterMask (EventSource *esp, EventListener *elp, eventmask_t mask) • void chEvtUnregister (EventSource *esp, EventListener *elp) • eventmask_t chEvtClearFlags (eventmask_t mask) • void chEvtSignalFlags (Thread *tp, eventmask_t mask) • void chEvtBroadcastFlags (EventSource *esp, eventmask_t mask) • eventmask_t chEvtWaitOne (eventmask_t mask) Embedded system 24 dce 2010 Memory Management • • • • Core Memory Manager Heaps Memory pools Dynamic threads Embedded system 25 dce 2010 Core Memory Manager • The core memory manager is a simplified allocator that only allows to allocate memory blocks without the possibility to free themMemory pools Embedded system 26 dce 2010 Heap • malloc() • free() Embedded system 27 dce 2010 Memory Pools • allocate/free fixed size objects in constant time and reliably without memory fragmentation problems Embedded system 28 dce 2010 I/O Management • Abstract Sequential Streams – Can be used as base class for high level object types such as files, sockets, serial ports, pipes etc. • Abstract File Streams • Abstract I/O channels • I/O queues – Mostly used in serial-like device drivers Embedded system 29 dce 2010 Registry • The Threads Registry is a double linked list that holds all the active threads in the system. • Thread * chRegFirstThread (void) • Thread * chRegNextThread (Thread * tp) • #define REG_REMOVE (tp) • #define REG_INSERT (tp) Embedded system 30 dce 2010 Debug • Trace buffer. • Parameters check. • Kernel assertions. Embedded system 31 dce 2010 Internals • Functions manage ThreadsQueue and ThreadsList • Are not OS APIs • Should not be directly used in the user applications code Embedded system 32