Clone of mbed rtos 2 for easy accessibility.
Fork of mbed-rtos by
Diff: rtx/TARGET_CORTEX_M/RTX_CM_lib.h
- Revision:
- 118:6635230e06ba
- Parent:
- 116:0788b1a76461
- Child:
- 119:19af2d39a542
--- a/rtx/TARGET_CORTEX_M/RTX_CM_lib.h Mon May 23 11:00:15 2016 +0100 +++ b/rtx/TARGET_CORTEX_M/RTX_CM_lib.h Mon Jul 25 14:12:24 2016 +0100 @@ -51,16 +51,16 @@ #define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3] #define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2] -#define OS_TCB_SIZE 52 +#define OS_TCB_SIZE 60 #define OS_TMR_SIZE 8 -#if defined (__CC_ARM) && !defined (__MICROLIB) - typedef void *OS_ID; typedef uint32_t OS_TID; typedef uint32_t OS_MUT[4]; typedef uint32_t OS_RESULT; +#if defined (__CC_ARM) && !defined (__MICROLIB) + #define runtask_id() rt_tsk_self() #define mutex_init(m) rt_mut_init(m) #define mutex_wait(m) os_mut_wait(m,0xFFFFU) @@ -122,7 +122,11 @@ uint16_t const os_tickus_i = OS_CLOCK/1000000; uint16_t const os_tickus_f = (((uint64_t)(OS_CLOCK-1000000*(OS_CLOCK/1000000)))<<16)/1000000; uint32_t const os_trv = OS_TRV; +#if defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED) +uint8_t const os_flags = 0; +#else /* defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED) */ uint8_t const os_flags = OS_RUNPRIV; +#endif /* defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED) */ /* Export following defines to uVision debugger. */ __USED uint32_t const CMSIS_RTOS_API_Version = osCMSIS; @@ -178,7 +182,7 @@ #endif /* Legacy RTX User Timers not used */ -uint32_t os_tmr = 0U; +uint32_t os_tmr = 0U; uint32_t const *m_tmr = NULL; uint16_t const mp_tmr_size = 0U; @@ -190,6 +194,77 @@ extern void *__libspace_start; #endif +#if defined (__ICCARM__) +static osMutexId std_mutex_id_sys[_MAX_LOCK] = {0}; +static OS_MUT std_mutex_sys[_MAX_LOCK] = {0}; +#define _FOPEN_MAX 10 +static osMutexId std_mutex_id_file[_FOPEN_MAX] = {0}; +static OS_MUT std_mutex_file[_FOPEN_MAX] = {0}; +void __iar_system_Mtxinit(__iar_Rmtx *mutex) /* Initialize a system lock */ +{ + osMutexDef_t def; + uint32_t index; + for (index = 0; index < _MAX_LOCK; index++) { + if (0 == std_mutex_id_sys[index]) { + def.mutex = &std_mutex_sys[index]; + std_mutex_id_sys[index] = osMutexCreate(&def); + *mutex = (__iar_Rmtx*)&std_mutex_id_sys[index]; + return; + } + } + // This should never happen + error("Not enough mutexes\n"); +} + +void __iar_system_Mtxdst(__iar_Rmtx *mutex)/*Destroy a system lock */ +{ + osMutexDelete(*(osMutexId*)*mutex); + *mutex = 0; +} + +void __iar_system_Mtxlock(__iar_Rmtx *mutex) /* Lock a system lock */ +{ + osMutexWait(*(osMutexId*)*mutex, osWaitForever); +} + +void __iar_system_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a system lock */ +{ + osMutexRelease(*(osMutexId*)*mutex); +} + +void __iar_file_Mtxinit(__iar_Rmtx *mutex)/*Initialize a file lock */ +{ + osMutexDef_t def; + uint32_t index; + for (index = 0; index < _FOPEN_MAX; index++) { + if (0 == std_mutex_id_file[index]) { + def.mutex = &std_mutex_file[index]; + std_mutex_id_file[index] = osMutexCreate(&def); + *mutex = (__iar_Rmtx*)&std_mutex_id_file[index]; + return; + } + } + // The variable _FOPEN_MAX needs to be increased + error("Not enough mutexes\n"); +} + +void __iar_file_Mtxdst(__iar_Rmtx *mutex) /* Destroy a file lock */ +{ + osMutexDelete(*(osMutexId*)*mutex); + *mutex = 0; +} + +void __iar_file_Mtxlock(__iar_Rmtx *mutex) /* Lock a file lock */ +{ + osMutexWait(*(osMutexId*)*mutex, osWaitForever); +} + +void __iar_file_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a file lock */ +{ + osMutexRelease(*(osMutexId*)*mutex); +} + +#endif /*---------------------------------------------------------------------------- * RTX Optimizations (empty functions) @@ -301,7 +376,12 @@ #define INITIAL_SP (0x20003000UL) #elif defined(TARGET_K64F) +#if defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED) +extern uint32_t __StackTop[]; +#define INITIAL_SP (__StackTop) +#else #define INITIAL_SP (0x20030000UL) +#endif #elif defined(TARGET_K22F) #define INITIAL_SP (0x20010000UL) @@ -402,12 +482,15 @@ #elif defined(TARGET_NZ32_SC151) #define INITIAL_SP (0x20008000UL) -#elif (defined(TARGET_STM32F446RE) || defined(TARGET_STM32F446VE)) +#elif defined(TARGET_STM32F446RE) || defined(TARGET_STM32F446VE) || defined(TARGET_STM32F446ZE) #define INITIAL_SP (0x20020000UL) #elif defined(TARGET_STM32F070RB) || defined(TARGET_STM32F030R8) #define INITIAL_SP (0x20002000UL) +#elif defined(TARGET_STM32L432KC) +#define INITIAL_SP (0x2000C000UL) + #elif defined(TARGET_STM32L476VG) #define INITIAL_SP (0x20018000UL) @@ -420,6 +503,23 @@ #elif defined(TARGET_STM32L152RC) #define INITIAL_SP (0x20008000UL) +#elif defined(TARGET_EFM32GG_STK3700) || defined(TARGET_BEETLE) +#define INITIAL_SP (0x20020000UL) + +#elif defined(TARGET_EFM32HG_STK3400) +#define INITIAL_SP (0x20002000UL) + +#elif defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401) +#define INITIAL_SP (0x20008000UL) + +#elif defined(TARGET_MCU_NORDIC_32K) +#define INITIAL_SP (0x20008000UL) + +#elif defined(TARGET_MCU_NORDIC_16K) +#define INITIAL_SP (0x20004000UL) + +#elif (defined(TARGET_STM32F767ZI)) +#define INITIAL_SP (0x20080000UL) #else #error "no target defined" @@ -434,15 +534,28 @@ #define HEAP_START (__end__) #elif defined(__ICCARM__) #pragma section="HEAP" -#define HEAP_START (void *)__section_begin("HEAP") +#define HEAP_END (void *)__section_end("HEAP") #endif void set_main_stack(void) { + uint32_t interrupt_stack_size = ((uint32_t)OS_MAINSTKSIZE * 4); +#if defined(__ICCARM__) + /* For IAR heap is defined .icf file */ + uint32_t main_stack_size = ((uint32_t)INITIAL_SP - (uint32_t)HEAP_END) - interrupt_stack_size; +#else + /* For ARM , uARM, or GCC_ARM , heap can grow and reach main stack */ + uint32_t heap_plus_stack_size = ((uint32_t)INITIAL_SP - (uint32_t)HEAP_START) - interrupt_stack_size; + // Main thread's stack is 1/4 of the heap + uint32_t main_stack_size = heap_plus_stack_size/4; +#endif + // The main thread must be 4 byte aligned + uint32_t main_stack_start = ((uint32_t)INITIAL_SP - interrupt_stack_size - main_stack_size) & ~0x7; + // That is the bottom of the main stack block: no collision detection - os_thread_def_main.stack_pointer = HEAP_START; + os_thread_def_main.stack_pointer = (uint32_t*)main_stack_start; // Leave OS_MAINSTKSIZE words for the scheduler and interrupts - os_thread_def_main.stacksize = (INITIAL_SP - (unsigned int)HEAP_START) - (OS_MAINSTKSIZE * 4); + os_thread_def_main.stacksize = main_stack_size; } #if defined (__CC_ARM) @@ -553,20 +666,25 @@ #elif defined (__GNUC__) +osMutexDef(malloc_mutex); +static osMutexId malloc_mutex_id; +osMutexDef(env_mutex); +static osMutexId env_mutex_id; + extern void __libc_fini_array(void); extern void __libc_init_array (void); extern int main(int argc, char **argv); void pre_main(void) { + malloc_mutex_id = osMutexCreate(osMutex(malloc_mutex)); + env_mutex_id = osMutexCreate(osMutex(env_mutex)); atexit(__libc_fini_array); __libc_init_array(); main(0, NULL); } -__attribute__((naked)) void software_init_hook (void) { +__attribute__((naked)) void software_init_hook_rtos (void) { __asm ( - ".syntax unified\n" - ".thumb\n" "bl osKernelInitialize\n" #ifdef __MBED_CMSIS_RTOS_CM "bl set_main_stack\n" @@ -580,6 +698,29 @@ ); } +// Opaque declaration of _reent structure +struct _reent; + +void __rtos_malloc_lock( struct _reent *_r ) +{ + osMutexWait(malloc_mutex_id, osWaitForever); +} + +void __rtos_malloc_unlock( struct _reent *_r ) +{ + osMutexRelease(malloc_mutex_id); +} + +void __rtos_env_lock( struct _reent *_r ) +{ + osMutexWait(env_mutex_id, osWaitForever); +} + +void __rtos_env_unlock( struct _reent *_r ) +{ + osMutexRelease(env_mutex_id); +} + #elif defined (__ICCARM__) extern void* __vector_table; @@ -589,6 +730,8 @@ extern __weak void __iar_init_vfp( void ); extern void __iar_dynamic_initialization(void); extern void mbed_sdk_init(void); +extern void mbed_main(void); +extern int main(void); extern void exit(int arg); static uint8_t low_level_init_needed; @@ -597,6 +740,7 @@ if (low_level_init_needed) { __iar_dynamic_initialization(); } + mbed_main(); main(); }