Donatien Garnier / rtos_net

Dependents:   EthernetHTTPClientTest

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RTX_CM_lib.h Source File

RTX_CM_lib.h

00001 /*----------------------------------------------------------------------------
00002  *      RL-ARM - RTX
00003  *----------------------------------------------------------------------------
00004  *      Name:    RTX_CM_LIB.H
00005  *      Purpose: RTX Kernel System Configuration
00006  *      Rev.:    V4.21
00007  *----------------------------------------------------------------------------
00008  *
00009  * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
00010  * All rights reserved.
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are met:
00013  *  - Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  *  - Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *  - Neither the name of ARM  nor the names of its contributors may be used 
00019  *    to endorse or promote products derived from this software without 
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
00026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  *---------------------------------------------------------------------------*/
00034 
00035 #if   defined (__CC_ARM)
00036 #pragma O3
00037 #define __USED __attribute__((used))
00038 #elif defined (__GNUC__)
00039 #pragma GCC optimize ("O3")
00040 #define __USED __attribute__((used))
00041 #elif defined (__ICCARM__)
00042 #define __USED __root
00043 #endif
00044 
00045 
00046 /*----------------------------------------------------------------------------
00047  *      Definitions
00048  *---------------------------------------------------------------------------*/
00049 
00050 #define _declare_box(pool,size,cnt)  uint32_t pool[(((size)+3)/4)*(cnt) + 3]
00051 #define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2]
00052 
00053 #define OS_TCB_SIZE     48
00054 #define OS_TMR_SIZE     8
00055 
00056 #if defined (__CC_ARM) && !defined (__MICROLIB)
00057 
00058 typedef void    *OS_ID;
00059 typedef uint32_t OS_TID;
00060 typedef uint32_t OS_MUT[3];
00061 typedef uint32_t OS_RESULT;
00062 
00063 #define runtask_id()    rt_tsk_self()
00064 #define mutex_init(m)   rt_mut_init(m)
00065 #define mutex_wait(m)   os_mut_wait(m,0xFFFF)
00066 #define mutex_rel(m)    os_mut_release(m)
00067 
00068 extern OS_TID    rt_tsk_self    (void);
00069 extern void      rt_mut_init    (OS_ID mutex);
00070 extern OS_RESULT rt_mut_release (OS_ID mutex);
00071 extern OS_RESULT rt_mut_wait    (OS_ID mutex, uint16_t timeout);
00072 
00073 #define os_mut_wait(mutex,timeout) _os_mut_wait((uint32_t)rt_mut_wait,mutex,timeout)
00074 #define os_mut_release(mutex)      _os_mut_release((uint32_t)rt_mut_release,mutex)
00075 
00076 OS_RESULT _os_mut_release (uint32_t p, OS_ID mutex)                   __svc_indirect(0);
00077 OS_RESULT _os_mut_wait    (uint32_t p, OS_ID mutex, uint16_t timeout) __svc_indirect(0);
00078 
00079 #endif
00080 
00081 
00082 /*----------------------------------------------------------------------------
00083  *      Global Variables
00084  *---------------------------------------------------------------------------*/
00085 
00086 #if (OS_TIMERS != 0)
00087 #define OS_TASK_CNT (OS_TASKCNT + 1)
00088 #define OS_PRIV_CNT (OS_PRIVCNT + 2)
00089 #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE+OS_TIMERSTKSZ))
00090 #else
00091 #define OS_TASK_CNT  OS_TASKCNT
00092 #define OS_PRIV_CNT (OS_PRIVCNT + 1)
00093 #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE))
00094 #endif
00095 
00096 extern uint16_t const os_maxtaskrun = OS_TASK_CNT;
00097 extern uint32_t const os_stackinfo  = (OS_STKCHECK<<24)| (OS_PRIV_CNT<<16) | (OS_STKSIZE*4);
00098 extern uint32_t const os_rrobin     = (OS_ROBIN << 16) | OS_ROBINTOUT;
00099 extern uint32_t const os_trv        = OS_TRV;
00100 extern uint8_t  const os_flags      = OS_RUNPRIV;
00101 
00102 /* Export following defines to uVision debugger. */
00103 extern __USED uint32_t const os_clockrate = OS_TICK;
00104 extern __USED uint32_t const os_timernum  = 0;
00105 
00106 /* Memory pool for TCB allocation    */
00107 _declare_box  (mp_tcb, OS_TCB_SIZE, OS_TASK_CNT);
00108 extern uint16_t const mp_tcb_size = sizeof(mp_tcb);
00109 
00110 /* Memory pool for System stack allocation (+os_idle_demon). */
00111 _declare_box8 (mp_stk, OS_STKSIZE*4, OS_TASK_CNT-OS_PRIV_CNT+1);
00112 extern uint32_t const mp_stk_size = sizeof(mp_stk);
00113 
00114 /* Memory pool for user specified stack allocation (+main, +timer) */
00115 uint64_t       os_stack_mem[2+OS_PRIV_CNT+(OS_STACK_SZ/8)];
00116 extern uint32_t const os_stack_sz = sizeof(os_stack_mem);
00117 
00118 #ifndef OS_FIFOSZ
00119  #define OS_FIFOSZ      16
00120 #endif
00121 
00122 /* Fifo Queue buffer for ISR requests.*/
00123 uint32_t       os_fifo[OS_FIFOSZ*2+1];
00124 extern uint8_t  const os_fifo_size = OS_FIFOSZ;
00125 
00126 /* An array of Active task pointers. */
00127 void *os_active_TCB[OS_TASK_CNT];
00128 
00129 /* User Timers Resources */
00130 #if (OS_TIMERS != 0)
00131 extern void osTimerThread (void const *argument);
00132 osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 1, 4*OS_TIMERSTKSZ);
00133 osThreadId osThreadId_osTimerThread;
00134 osMessageQDef(osTimerMessageQ, OS_TIMERCBQS, void *);
00135 osMessageQId osMessageQId_osTimerMessageQ;
00136 #else
00137 osThreadDef_t os_thread_def_osTimerThread = { NULL };
00138 osThreadId osThreadId_osTimerThread;
00139 osMessageQDef(osTimerMessageQ, 0, void *);
00140 osMessageQId osMessageQId_osTimerMessageQ;
00141 #endif
00142 
00143 /* Legacy RTX User Timers not used */
00144 uint32_t       os_tmr = 0; 
00145 extern uint32_t const *m_tmr = NULL;
00146 extern uint16_t const mp_tmr_size = 0;
00147 
00148 #if defined (__CC_ARM) && !defined (__MICROLIB)
00149  /* A memory space for arm standard library. */
00150  static uint32_t std_libspace[OS_TASK_CNT][96/4];
00151  static OS_MUT   std_libmutex[OS_MUTEXCNT];
00152  static uint32_t nr_mutex;
00153  extern void  *__libspace_start;
00154 #endif
00155 
00156 
00157 /*----------------------------------------------------------------------------
00158  *      RTX Optimizations (empty functions)
00159  *---------------------------------------------------------------------------*/
00160 
00161 #if OS_ROBIN == 0
00162  void rt_init_robin (void) {;}
00163  void rt_chk_robin  (void) {;}
00164 #endif
00165 
00166 #if OS_STKCHECK == 0
00167  void rt_stk_check  (void) {;}
00168 #endif
00169 
00170 
00171 /*----------------------------------------------------------------------------
00172  *      Standard Library multithreading interface
00173  *---------------------------------------------------------------------------*/
00174 
00175 #if defined (__CC_ARM) && !defined (__MICROLIB)
00176 
00177 /*--------------------------- __user_perthread_libspace ---------------------*/
00178 
00179 void *__user_perthread_libspace (void) {
00180   /* Provide a separate libspace for each task. */
00181   uint32_t idx;
00182 
00183   idx = runtask_id ();
00184   if (idx == 0) {
00185     /* RTX not running yet. */
00186     return (&__libspace_start);
00187   }
00188   return ((void *)&std_libspace[idx-1]);
00189 }
00190 
00191 /*--------------------------- _mutex_initialize -----------------------------*/
00192 
00193 int _mutex_initialize (OS_ID *mutex) {
00194   /* Allocate and initialize a system mutex. */
00195 
00196   if (nr_mutex >= OS_MUTEXCNT) {
00197     /* If you are here, you need to increase the number OS_MUTEXCNT. */
00198     for (;;);
00199   }
00200   *mutex = &std_libmutex[nr_mutex++];
00201   mutex_init (*mutex);
00202   return (1);
00203 }
00204 
00205 
00206 /*--------------------------- _mutex_acquire --------------------------------*/
00207 
00208 __attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
00209   /* Acquire a system mutex, lock stdlib resources. */
00210   if (runtask_id ()) {
00211     /* RTX running, acquire a mutex. */
00212     mutex_wait (*mutex);
00213   }
00214 }
00215 
00216 
00217 /*--------------------------- _mutex_release --------------------------------*/
00218 
00219 __attribute__((used)) void _mutex_release (OS_ID *mutex) {
00220   /* Release a system mutex, unlock stdlib resources. */
00221   if (runtask_id ()) {
00222     /* RTX runnning, release a mutex. */
00223     mutex_rel (*mutex);
00224   }
00225 }
00226 
00227 #endif
00228 
00229 
00230 /*----------------------------------------------------------------------------
00231  *      RTX Startup
00232  *---------------------------------------------------------------------------*/
00233 
00234 /* Main Thread definition */
00235 extern int main (void);
00236 osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 1, 4*OS_MAINSTKSIZE };
00237 
00238 
00239 #if defined (__CC_ARM)
00240 
00241 typedef void PROC();
00242 
00243 extern const size_t SHT$$INIT_ARRAY$$Base[];
00244 extern const size_t SHT$$INIT_ARRAY$$Limit[];
00245 
00246 void cpp_init(void) {
00247     const size_t *base = SHT$$INIT_ARRAY$$Base;
00248     const size_t *lim  = SHT$$INIT_ARRAY$$Limit;
00249     
00250     for (; base != lim; ++base) {
00251         PROC *proc = (PROC*)((const char*)base + *base);
00252         (*proc)();
00253     }
00254 }
00255 
00256 // void $Super$$__cpp_initialize__aeabi_(void);
00257 void $Sub$$__cpp_initialize__aeabi_(void) {}
00258 
00259 void $Super$$main(void);
00260 void $Sub$$main(void) {
00261     //  $Super$$__cpp_initialize__aeabi_();
00262     cpp_init();
00263     
00264     $Super$$main();
00265 }
00266 
00267 #ifdef __MICROLIB
00268 void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
00269 void _main_init (void) {
00270   osKernelStart(&os_thread_def_main, NULL);
00271   for (;;);
00272 }
00273 #else
00274 __asm void __rt_entry (void) {
00275 
00276   IMPORT  __user_setup_stackheap
00277   IMPORT  __rt_lib_init
00278   IMPORT  os_thread_def_main
00279   IMPORT  osKernelStart
00280   IMPORT  exit
00281 
00282   BL      __user_setup_stackheap
00283   MOV     R1,R2
00284   BL      __rt_lib_init
00285   LDR     R0,=os_thread_def_main
00286   MOVS    R1,#0
00287   BL      osKernelStart
00288   BL      exit
00289 
00290   ALIGN
00291 }
00292 #endif
00293 
00294 #elif defined (__GNUC__)
00295 
00296 #ifdef __CS3__
00297 
00298 /* CS3 start_c routine.
00299  *
00300  * Copyright (c) 2006, 2007 CodeSourcery Inc
00301  *
00302  * The authors hereby grant permission to use, copy, modify, distribute,
00303  * and license this software and its documentation for any purpose, provided
00304  * that existing copyright notices are retained in all copies and that this
00305  * notice is included verbatim in any distributions. No written agreement,
00306  * license, or royalty fee is required for any of the authorized uses.
00307  * Modifications to this software may be copyrighted by their authors
00308  * and need not follow the licensing terms described here, provided that
00309  * the new terms are clearly indicated on the first page of each file where
00310  * they apply.
00311  */
00312 
00313 #include "cs3.h"
00314 
00315 extern void __libc_init_array (void);
00316 
00317 __attribute ((noreturn)) void __cs3_start_c (void){
00318   unsigned regions = __cs3_region_num;
00319   const struct __cs3_region *rptr = __cs3_regions;
00320 
00321   /* Initialize memory */
00322   for (regions = __cs3_region_num, rptr = __cs3_regions; regions--; rptr++) {
00323     long long *src = (long long *)rptr->init;
00324     long long *dst = (long long *)rptr->data;
00325     unsigned limit = rptr->init_size;
00326     unsigned count;
00327 
00328     if (src != dst)
00329         for (count = 0; count != limit; count += sizeof (long long))
00330         *dst++ = *src++;
00331     else 
00332         dst = (long long *)((char *)dst + limit);
00333     limit = rptr->zero_size;
00334     for (count = 0; count != limit; count += sizeof (long long))
00335         *dst++ = 0;
00336   }
00337 
00338   /* Run initializers.  */
00339   __libc_init_array ();
00340 
00341   osKernelStart(&os_thread_def_main, NULL);
00342   for (;;);
00343 }
00344 
00345 #else
00346 
00347 __attribute__((naked)) void software_init_hook (void) {
00348   __asm (
00349     ".syntax unified\n"
00350     ".thumb\n"
00351     "movs r0,#0\n"
00352     "movs r1,#0\n"
00353     "mov  r4,r0\n"
00354     "mov  r5,r1\n"
00355     "ldr  r0,= __libc_fini_array\n"
00356     "bl   atexit\n"
00357     "bl   __libc_init_array\n"
00358     "mov  r0,r4\n"
00359     "mov  r1,r5\n"
00360     "ldr  r0,=os_thread_def_main\n"
00361     "movs r1,#0\n"
00362     "bl   osKernelStart\n"
00363     "bl   exit\n"
00364   );
00365 }
00366 
00367 #endif
00368 
00369 #elif defined (__ICCARM__)
00370 
00371 extern int  __low_level_init(void);
00372 extern void __iar_data_init3(void);
00373 extern void exit(int arg);
00374 
00375 __noreturn __stackless void __cmain(void) {
00376   int a;
00377   
00378   if (__low_level_init() != 0) {
00379     __iar_data_init3();
00380   }
00381   a = osKernelStart(&os_thread_def_main, NULL);
00382   exit(a);
00383 }
00384 
00385 #endif
00386 
00387 
00388 /*----------------------------------------------------------------------------
00389  * end of file
00390  *---------------------------------------------------------------------------*/
00391