
Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.
Dependencies: C12832 MQTT LM75B MMA7660
Dependents: MFT_IoT_demo_USB400 IBM_RFID
mbed-rtos/rtx/RTX_CM_lib.h@6:37b6d0d56190, 2014-08-20 (annotated)
- Committer:
- samdanbury
- Date:
- Wed Aug 20 12:45:14 2014 +0000
- Revision:
- 6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 6:37b6d0d56190 | 1 | /*---------------------------------------------------------------------------- |
samdanbury | 6:37b6d0d56190 | 2 | * RL-ARM - RTX |
samdanbury | 6:37b6d0d56190 | 3 | *---------------------------------------------------------------------------- |
samdanbury | 6:37b6d0d56190 | 4 | * Name: RTX_CM_LIB.H |
samdanbury | 6:37b6d0d56190 | 5 | * Purpose: RTX Kernel System Configuration |
samdanbury | 6:37b6d0d56190 | 6 | * Rev.: V4.60 |
samdanbury | 6:37b6d0d56190 | 7 | *---------------------------------------------------------------------------- |
samdanbury | 6:37b6d0d56190 | 8 | * |
samdanbury | 6:37b6d0d56190 | 9 | * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH |
samdanbury | 6:37b6d0d56190 | 10 | * All rights reserved. |
samdanbury | 6:37b6d0d56190 | 11 | * Redistribution and use in source and binary forms, with or without |
samdanbury | 6:37b6d0d56190 | 12 | * modification, are permitted provided that the following conditions are met: |
samdanbury | 6:37b6d0d56190 | 13 | * - Redistributions of source code must retain the above copyright |
samdanbury | 6:37b6d0d56190 | 14 | * notice, this list of conditions and the following disclaimer. |
samdanbury | 6:37b6d0d56190 | 15 | * - Redistributions in binary form must reproduce the above copyright |
samdanbury | 6:37b6d0d56190 | 16 | * notice, this list of conditions and the following disclaimer in the |
samdanbury | 6:37b6d0d56190 | 17 | * documentation and/or other materials provided with the distribution. |
samdanbury | 6:37b6d0d56190 | 18 | * - Neither the name of ARM nor the names of its contributors may be used |
samdanbury | 6:37b6d0d56190 | 19 | * to endorse or promote products derived from this software without |
samdanbury | 6:37b6d0d56190 | 20 | * specific prior written permission. |
samdanbury | 6:37b6d0d56190 | 21 | * |
samdanbury | 6:37b6d0d56190 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
samdanbury | 6:37b6d0d56190 | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
samdanbury | 6:37b6d0d56190 | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
samdanbury | 6:37b6d0d56190 | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
samdanbury | 6:37b6d0d56190 | 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
samdanbury | 6:37b6d0d56190 | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
samdanbury | 6:37b6d0d56190 | 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
samdanbury | 6:37b6d0d56190 | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
samdanbury | 6:37b6d0d56190 | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
samdanbury | 6:37b6d0d56190 | 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
samdanbury | 6:37b6d0d56190 | 32 | * POSSIBILITY OF SUCH DAMAGE. |
samdanbury | 6:37b6d0d56190 | 33 | *---------------------------------------------------------------------------*/ |
samdanbury | 6:37b6d0d56190 | 34 | #include "error.h" |
samdanbury | 6:37b6d0d56190 | 35 | |
samdanbury | 6:37b6d0d56190 | 36 | #if defined (__CC_ARM) |
samdanbury | 6:37b6d0d56190 | 37 | #pragma O3 |
samdanbury | 6:37b6d0d56190 | 38 | #define __USED __attribute__((used)) |
samdanbury | 6:37b6d0d56190 | 39 | #elif defined (__GNUC__) |
samdanbury | 6:37b6d0d56190 | 40 | #pragma GCC optimize ("O3") |
samdanbury | 6:37b6d0d56190 | 41 | #define __USED __attribute__((used)) |
samdanbury | 6:37b6d0d56190 | 42 | #elif defined (__ICCARM__) |
samdanbury | 6:37b6d0d56190 | 43 | #define __USED __root |
samdanbury | 6:37b6d0d56190 | 44 | #endif |
samdanbury | 6:37b6d0d56190 | 45 | |
samdanbury | 6:37b6d0d56190 | 46 | |
samdanbury | 6:37b6d0d56190 | 47 | /*---------------------------------------------------------------------------- |
samdanbury | 6:37b6d0d56190 | 48 | * Definitions |
samdanbury | 6:37b6d0d56190 | 49 | *---------------------------------------------------------------------------*/ |
samdanbury | 6:37b6d0d56190 | 50 | |
samdanbury | 6:37b6d0d56190 | 51 | #define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3] |
samdanbury | 6:37b6d0d56190 | 52 | #define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2] |
samdanbury | 6:37b6d0d56190 | 53 | |
samdanbury | 6:37b6d0d56190 | 54 | #define OS_TCB_SIZE 48 |
samdanbury | 6:37b6d0d56190 | 55 | #define OS_TMR_SIZE 8 |
samdanbury | 6:37b6d0d56190 | 56 | |
samdanbury | 6:37b6d0d56190 | 57 | #if defined (__CC_ARM) && !defined (__MICROLIB) |
samdanbury | 6:37b6d0d56190 | 58 | |
samdanbury | 6:37b6d0d56190 | 59 | typedef void *OS_ID; |
samdanbury | 6:37b6d0d56190 | 60 | typedef uint32_t OS_TID; |
samdanbury | 6:37b6d0d56190 | 61 | typedef uint32_t OS_MUT[3]; |
samdanbury | 6:37b6d0d56190 | 62 | typedef uint32_t OS_RESULT; |
samdanbury | 6:37b6d0d56190 | 63 | |
samdanbury | 6:37b6d0d56190 | 64 | #define runtask_id() rt_tsk_self() |
samdanbury | 6:37b6d0d56190 | 65 | #define mutex_init(m) rt_mut_init(m) |
samdanbury | 6:37b6d0d56190 | 66 | #define mutex_wait(m) os_mut_wait(m,0xFFFF) |
samdanbury | 6:37b6d0d56190 | 67 | #define mutex_rel(m) os_mut_release(m) |
samdanbury | 6:37b6d0d56190 | 68 | |
samdanbury | 6:37b6d0d56190 | 69 | extern OS_TID rt_tsk_self (void); |
samdanbury | 6:37b6d0d56190 | 70 | extern void rt_mut_init (OS_ID mutex); |
samdanbury | 6:37b6d0d56190 | 71 | extern OS_RESULT rt_mut_release (OS_ID mutex); |
samdanbury | 6:37b6d0d56190 | 72 | extern OS_RESULT rt_mut_wait (OS_ID mutex, uint16_t timeout); |
samdanbury | 6:37b6d0d56190 | 73 | |
samdanbury | 6:37b6d0d56190 | 74 | #define os_mut_wait(mutex,timeout) _os_mut_wait((uint32_t)rt_mut_wait,mutex,timeout) |
samdanbury | 6:37b6d0d56190 | 75 | #define os_mut_release(mutex) _os_mut_release((uint32_t)rt_mut_release,mutex) |
samdanbury | 6:37b6d0d56190 | 76 | |
samdanbury | 6:37b6d0d56190 | 77 | OS_RESULT _os_mut_release (uint32_t p, OS_ID mutex) __svc_indirect(0); |
samdanbury | 6:37b6d0d56190 | 78 | OS_RESULT _os_mut_wait (uint32_t p, OS_ID mutex, uint16_t timeout) __svc_indirect(0); |
samdanbury | 6:37b6d0d56190 | 79 | |
samdanbury | 6:37b6d0d56190 | 80 | #endif |
samdanbury | 6:37b6d0d56190 | 81 | |
samdanbury | 6:37b6d0d56190 | 82 | |
samdanbury | 6:37b6d0d56190 | 83 | /*---------------------------------------------------------------------------- |
samdanbury | 6:37b6d0d56190 | 84 | * Global Variables |
samdanbury | 6:37b6d0d56190 | 85 | *---------------------------------------------------------------------------*/ |
samdanbury | 6:37b6d0d56190 | 86 | |
samdanbury | 6:37b6d0d56190 | 87 | #if (OS_TIMERS != 0) |
samdanbury | 6:37b6d0d56190 | 88 | #define OS_TASK_CNT (OS_TASKCNT + 1) |
samdanbury | 6:37b6d0d56190 | 89 | #else |
samdanbury | 6:37b6d0d56190 | 90 | #define OS_TASK_CNT OS_TASKCNT |
samdanbury | 6:37b6d0d56190 | 91 | #endif |
samdanbury | 6:37b6d0d56190 | 92 | |
samdanbury | 6:37b6d0d56190 | 93 | uint16_t const os_maxtaskrun = OS_TASK_CNT; |
samdanbury | 6:37b6d0d56190 | 94 | uint32_t const os_rrobin = (OS_ROBIN << 16) | OS_ROBINTOUT; |
samdanbury | 6:37b6d0d56190 | 95 | uint32_t const os_trv = OS_TRV; |
samdanbury | 6:37b6d0d56190 | 96 | uint8_t const os_flags = OS_RUNPRIV; |
samdanbury | 6:37b6d0d56190 | 97 | |
samdanbury | 6:37b6d0d56190 | 98 | /* Export following defines to uVision debugger. */ |
samdanbury | 6:37b6d0d56190 | 99 | __USED uint32_t const os_clockrate = OS_TICK; |
samdanbury | 6:37b6d0d56190 | 100 | __USED uint32_t const os_timernum = 0; |
samdanbury | 6:37b6d0d56190 | 101 | |
samdanbury | 6:37b6d0d56190 | 102 | /* Stack for the os_idle_demon */ |
samdanbury | 6:37b6d0d56190 | 103 | unsigned int idle_task_stack[OS_IDLESTKSIZE]; |
samdanbury | 6:37b6d0d56190 | 104 | unsigned short const idle_task_stack_size = OS_IDLESTKSIZE; |
samdanbury | 6:37b6d0d56190 | 105 | |
samdanbury | 6:37b6d0d56190 | 106 | #ifndef OS_FIFOSZ |
samdanbury | 6:37b6d0d56190 | 107 | #define OS_FIFOSZ 16 |
samdanbury | 6:37b6d0d56190 | 108 | #endif |
samdanbury | 6:37b6d0d56190 | 109 | |
samdanbury | 6:37b6d0d56190 | 110 | /* Fifo Queue buffer for ISR requests.*/ |
samdanbury | 6:37b6d0d56190 | 111 | uint32_t os_fifo[OS_FIFOSZ*2+1]; |
samdanbury | 6:37b6d0d56190 | 112 | uint8_t const os_fifo_size = OS_FIFOSZ; |
samdanbury | 6:37b6d0d56190 | 113 | |
samdanbury | 6:37b6d0d56190 | 114 | /* An array of Active task pointers. */ |
samdanbury | 6:37b6d0d56190 | 115 | void *os_active_TCB[OS_TASK_CNT]; |
samdanbury | 6:37b6d0d56190 | 116 | |
samdanbury | 6:37b6d0d56190 | 117 | /* User Timers Resources */ |
samdanbury | 6:37b6d0d56190 | 118 | #if (OS_TIMERS != 0) |
samdanbury | 6:37b6d0d56190 | 119 | extern void osTimerThread (void const *argument); |
samdanbury | 6:37b6d0d56190 | 120 | osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 4*OS_TIMERSTKSZ); |
samdanbury | 6:37b6d0d56190 | 121 | osThreadId osThreadId_osTimerThread; |
samdanbury | 6:37b6d0d56190 | 122 | osMessageQDef(osTimerMessageQ, OS_TIMERCBQS, void *); |
samdanbury | 6:37b6d0d56190 | 123 | osMessageQId osMessageQId_osTimerMessageQ; |
samdanbury | 6:37b6d0d56190 | 124 | #else |
samdanbury | 6:37b6d0d56190 | 125 | osThreadDef_t os_thread_def_osTimerThread = { NULL }; |
samdanbury | 6:37b6d0d56190 | 126 | osThreadId osThreadId_osTimerThread; |
samdanbury | 6:37b6d0d56190 | 127 | osMessageQDef(osTimerMessageQ, 0, void *); |
samdanbury | 6:37b6d0d56190 | 128 | osMessageQId osMessageQId_osTimerMessageQ; |
samdanbury | 6:37b6d0d56190 | 129 | #endif |
samdanbury | 6:37b6d0d56190 | 130 | |
samdanbury | 6:37b6d0d56190 | 131 | |
samdanbury | 6:37b6d0d56190 | 132 | /*---------------------------------------------------------------------------- |
samdanbury | 6:37b6d0d56190 | 133 | * RTX Optimizations (empty functions) |
samdanbury | 6:37b6d0d56190 | 134 | *---------------------------------------------------------------------------*/ |
samdanbury | 6:37b6d0d56190 | 135 | |
samdanbury | 6:37b6d0d56190 | 136 | #if OS_ROBIN == 0 |
samdanbury | 6:37b6d0d56190 | 137 | void rt_init_robin (void) {;} |
samdanbury | 6:37b6d0d56190 | 138 | void rt_chk_robin (void) {;} |
samdanbury | 6:37b6d0d56190 | 139 | #endif |
samdanbury | 6:37b6d0d56190 | 140 | |
samdanbury | 6:37b6d0d56190 | 141 | #if OS_STKCHECK == 0 |
samdanbury | 6:37b6d0d56190 | 142 | void rt_stk_check (void) {;} |
samdanbury | 6:37b6d0d56190 | 143 | #endif |
samdanbury | 6:37b6d0d56190 | 144 | |
samdanbury | 6:37b6d0d56190 | 145 | |
samdanbury | 6:37b6d0d56190 | 146 | /*---------------------------------------------------------------------------- |
samdanbury | 6:37b6d0d56190 | 147 | * Standard Library multithreading interface |
samdanbury | 6:37b6d0d56190 | 148 | *---------------------------------------------------------------------------*/ |
samdanbury | 6:37b6d0d56190 | 149 | |
samdanbury | 6:37b6d0d56190 | 150 | #if defined (__CC_ARM) && !defined (__MICROLIB) |
samdanbury | 6:37b6d0d56190 | 151 | static OS_MUT std_libmutex[OS_MUTEXCNT]; |
samdanbury | 6:37b6d0d56190 | 152 | static uint32_t nr_mutex; |
samdanbury | 6:37b6d0d56190 | 153 | |
samdanbury | 6:37b6d0d56190 | 154 | /*--------------------------- _mutex_initialize -----------------------------*/ |
samdanbury | 6:37b6d0d56190 | 155 | |
samdanbury | 6:37b6d0d56190 | 156 | int _mutex_initialize (OS_ID *mutex) { |
samdanbury | 6:37b6d0d56190 | 157 | /* Allocate and initialize a system mutex. */ |
samdanbury | 6:37b6d0d56190 | 158 | |
samdanbury | 6:37b6d0d56190 | 159 | if (nr_mutex >= OS_MUTEXCNT) { |
samdanbury | 6:37b6d0d56190 | 160 | /* If you are here, you need to increase the number OS_MUTEXCNT. */ |
samdanbury | 6:37b6d0d56190 | 161 | error("Not enough stdlib mutexes\n"); |
samdanbury | 6:37b6d0d56190 | 162 | } |
samdanbury | 6:37b6d0d56190 | 163 | *mutex = &std_libmutex[nr_mutex++]; |
samdanbury | 6:37b6d0d56190 | 164 | mutex_init (*mutex); |
samdanbury | 6:37b6d0d56190 | 165 | return (1); |
samdanbury | 6:37b6d0d56190 | 166 | } |
samdanbury | 6:37b6d0d56190 | 167 | |
samdanbury | 6:37b6d0d56190 | 168 | |
samdanbury | 6:37b6d0d56190 | 169 | /*--------------------------- _mutex_acquire --------------------------------*/ |
samdanbury | 6:37b6d0d56190 | 170 | |
samdanbury | 6:37b6d0d56190 | 171 | __attribute__((used)) void _mutex_acquire (OS_ID *mutex) { |
samdanbury | 6:37b6d0d56190 | 172 | /* Acquire a system mutex, lock stdlib resources. */ |
samdanbury | 6:37b6d0d56190 | 173 | if (runtask_id ()) { |
samdanbury | 6:37b6d0d56190 | 174 | /* RTX running, acquire a mutex. */ |
samdanbury | 6:37b6d0d56190 | 175 | mutex_wait (*mutex); |
samdanbury | 6:37b6d0d56190 | 176 | } |
samdanbury | 6:37b6d0d56190 | 177 | } |
samdanbury | 6:37b6d0d56190 | 178 | |
samdanbury | 6:37b6d0d56190 | 179 | |
samdanbury | 6:37b6d0d56190 | 180 | /*--------------------------- _mutex_release --------------------------------*/ |
samdanbury | 6:37b6d0d56190 | 181 | |
samdanbury | 6:37b6d0d56190 | 182 | __attribute__((used)) void _mutex_release (OS_ID *mutex) { |
samdanbury | 6:37b6d0d56190 | 183 | /* Release a system mutex, unlock stdlib resources. */ |
samdanbury | 6:37b6d0d56190 | 184 | if (runtask_id ()) { |
samdanbury | 6:37b6d0d56190 | 185 | /* RTX running, release a mutex. */ |
samdanbury | 6:37b6d0d56190 | 186 | mutex_rel (*mutex); |
samdanbury | 6:37b6d0d56190 | 187 | } |
samdanbury | 6:37b6d0d56190 | 188 | } |
samdanbury | 6:37b6d0d56190 | 189 | |
samdanbury | 6:37b6d0d56190 | 190 | #endif |
samdanbury | 6:37b6d0d56190 | 191 | |
samdanbury | 6:37b6d0d56190 | 192 | |
samdanbury | 6:37b6d0d56190 | 193 | /*---------------------------------------------------------------------------- |
samdanbury | 6:37b6d0d56190 | 194 | * RTX Startup |
samdanbury | 6:37b6d0d56190 | 195 | *---------------------------------------------------------------------------*/ |
samdanbury | 6:37b6d0d56190 | 196 | |
samdanbury | 6:37b6d0d56190 | 197 | /* Main Thread definition */ |
samdanbury | 6:37b6d0d56190 | 198 | extern int main (void); |
samdanbury | 6:37b6d0d56190 | 199 | osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 0, NULL}; |
samdanbury | 6:37b6d0d56190 | 200 | |
samdanbury | 6:37b6d0d56190 | 201 | // This define should be probably moved to the CMSIS layer |
samdanbury | 6:37b6d0d56190 | 202 | #if defined(TARGET_LPC1768) |
samdanbury | 6:37b6d0d56190 | 203 | #define INITIAL_SP (0x10008000UL) |
samdanbury | 6:37b6d0d56190 | 204 | |
samdanbury | 6:37b6d0d56190 | 205 | #elif defined(TARGET_LPC11U24) |
samdanbury | 6:37b6d0d56190 | 206 | #define INITIAL_SP (0x10002000UL) |
samdanbury | 6:37b6d0d56190 | 207 | |
samdanbury | 6:37b6d0d56190 | 208 | #elif defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501) |
samdanbury | 6:37b6d0d56190 | 209 | #define INITIAL_SP (0x10002000UL) |
samdanbury | 6:37b6d0d56190 | 210 | |
samdanbury | 6:37b6d0d56190 | 211 | #elif defined(TARGET_LPC1114) |
samdanbury | 6:37b6d0d56190 | 212 | #define INITIAL_SP (0x10001000UL) |
samdanbury | 6:37b6d0d56190 | 213 | |
samdanbury | 6:37b6d0d56190 | 214 | #elif defined(TARGET_LPC812) |
samdanbury | 6:37b6d0d56190 | 215 | #define INITIAL_SP (0x10001000UL) |
samdanbury | 6:37b6d0d56190 | 216 | |
samdanbury | 6:37b6d0d56190 | 217 | #elif defined(TARGET_KL25Z) |
samdanbury | 6:37b6d0d56190 | 218 | #define INITIAL_SP (0x20003000UL) |
samdanbury | 6:37b6d0d56190 | 219 | |
samdanbury | 6:37b6d0d56190 | 220 | #elif defined(TARGET_K64F) |
samdanbury | 6:37b6d0d56190 | 221 | #define INITIAL_SP (0x20030000UL) |
samdanbury | 6:37b6d0d56190 | 222 | |
samdanbury | 6:37b6d0d56190 | 223 | #elif defined(TARGET_KL46Z) |
samdanbury | 6:37b6d0d56190 | 224 | #define INITIAL_SP (0x20006000UL) |
samdanbury | 6:37b6d0d56190 | 225 | |
samdanbury | 6:37b6d0d56190 | 226 | #elif defined(TARGET_LPC4088) |
samdanbury | 6:37b6d0d56190 | 227 | #define INITIAL_SP (0x10010000UL) |
samdanbury | 6:37b6d0d56190 | 228 | |
samdanbury | 6:37b6d0d56190 | 229 | #elif defined(TARGET_LPC1347) |
samdanbury | 6:37b6d0d56190 | 230 | #define INITIAL_SP (0x10002000UL) |
samdanbury | 6:37b6d0d56190 | 231 | |
samdanbury | 6:37b6d0d56190 | 232 | #elif defined(TARGET_STM32F100RB) || defined(TARGET_STM32F051R8) |
samdanbury | 6:37b6d0d56190 | 233 | #define INITIAL_SP (0x20002000UL) |
samdanbury | 6:37b6d0d56190 | 234 | |
samdanbury | 6:37b6d0d56190 | 235 | #elif defined(TARGET_DISCO_F303VC) |
samdanbury | 6:37b6d0d56190 | 236 | #define INITIAL_SP (0x2000A000UL) |
samdanbury | 6:37b6d0d56190 | 237 | |
samdanbury | 6:37b6d0d56190 | 238 | #elif defined(TARGET_STM32F407) || defined(TARGET_F407VG) |
samdanbury | 6:37b6d0d56190 | 239 | #define INITIAL_SP (0x20020000UL) |
samdanbury | 6:37b6d0d56190 | 240 | |
samdanbury | 6:37b6d0d56190 | 241 | #elif defined(TARGET_LPC1549) |
samdanbury | 6:37b6d0d56190 | 242 | #define INITIAL_SP (0x02009000UL) |
samdanbury | 6:37b6d0d56190 | 243 | |
samdanbury | 6:37b6d0d56190 | 244 | #elif defined(TARGET_LPC11U68) |
samdanbury | 6:37b6d0d56190 | 245 | #define INITIAL_SP (0x10004000UL) |
samdanbury | 6:37b6d0d56190 | 246 | |
samdanbury | 6:37b6d0d56190 | 247 | #else |
samdanbury | 6:37b6d0d56190 | 248 | #error "no target defined" |
samdanbury | 6:37b6d0d56190 | 249 | |
samdanbury | 6:37b6d0d56190 | 250 | #endif |
samdanbury | 6:37b6d0d56190 | 251 | |
samdanbury | 6:37b6d0d56190 | 252 | #ifdef __CC_ARM |
samdanbury | 6:37b6d0d56190 | 253 | extern unsigned char Image$$RW_IRAM1$$ZI$$Limit[]; |
samdanbury | 6:37b6d0d56190 | 254 | #define HEAP_START (Image$$RW_IRAM1$$ZI$$Limit) |
samdanbury | 6:37b6d0d56190 | 255 | #elif defined(__GNUC__) |
samdanbury | 6:37b6d0d56190 | 256 | extern unsigned char __end__[]; |
samdanbury | 6:37b6d0d56190 | 257 | #define HEAP_START (__end__) |
samdanbury | 6:37b6d0d56190 | 258 | #endif |
samdanbury | 6:37b6d0d56190 | 259 | |
samdanbury | 6:37b6d0d56190 | 260 | void set_main_stack(void) { |
samdanbury | 6:37b6d0d56190 | 261 | // That is the bottom of the main stack block: no collision detection |
samdanbury | 6:37b6d0d56190 | 262 | os_thread_def_main.stack_pointer = HEAP_START; |
samdanbury | 6:37b6d0d56190 | 263 | |
samdanbury | 6:37b6d0d56190 | 264 | // Leave OS_SCHEDULERSTKSIZE words for the scheduler and interrupts |
samdanbury | 6:37b6d0d56190 | 265 | os_thread_def_main.stacksize = (INITIAL_SP - (unsigned int)HEAP_START) - (OS_SCHEDULERSTKSIZE * 4); |
samdanbury | 6:37b6d0d56190 | 266 | } |
samdanbury | 6:37b6d0d56190 | 267 | |
samdanbury | 6:37b6d0d56190 | 268 | #if defined (__CC_ARM) |
samdanbury | 6:37b6d0d56190 | 269 | #ifdef __MICROLIB |
samdanbury | 6:37b6d0d56190 | 270 | void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF"))); |
samdanbury | 6:37b6d0d56190 | 271 | void _main_init (void) { |
samdanbury | 6:37b6d0d56190 | 272 | osKernelInitialize(); |
samdanbury | 6:37b6d0d56190 | 273 | set_main_stack(); |
samdanbury | 6:37b6d0d56190 | 274 | osThreadCreate(&os_thread_def_main, NULL); |
samdanbury | 6:37b6d0d56190 | 275 | osKernelStart(); |
samdanbury | 6:37b6d0d56190 | 276 | for (;;); |
samdanbury | 6:37b6d0d56190 | 277 | } |
samdanbury | 6:37b6d0d56190 | 278 | #else |
samdanbury | 6:37b6d0d56190 | 279 | |
samdanbury | 6:37b6d0d56190 | 280 | /* The single memory model is checking for stack collision at run time, verifing |
samdanbury | 6:37b6d0d56190 | 281 | that the heap pointer is underneath the stack pointer. |
samdanbury | 6:37b6d0d56190 | 282 | |
samdanbury | 6:37b6d0d56190 | 283 | With the RTOS there is not only one stack above the heap, there are multiple |
samdanbury | 6:37b6d0d56190 | 284 | stacks and some of them are underneath the heap pointer. |
samdanbury | 6:37b6d0d56190 | 285 | */ |
samdanbury | 6:37b6d0d56190 | 286 | #pragma import(__use_two_region_memory) |
samdanbury | 6:37b6d0d56190 | 287 | |
samdanbury | 6:37b6d0d56190 | 288 | __asm void __rt_entry (void) { |
samdanbury | 6:37b6d0d56190 | 289 | |
samdanbury | 6:37b6d0d56190 | 290 | IMPORT __user_setup_stackheap |
samdanbury | 6:37b6d0d56190 | 291 | IMPORT __rt_lib_init |
samdanbury | 6:37b6d0d56190 | 292 | IMPORT os_thread_def_main |
samdanbury | 6:37b6d0d56190 | 293 | IMPORT osKernelInitialize |
samdanbury | 6:37b6d0d56190 | 294 | IMPORT set_main_stack |
samdanbury | 6:37b6d0d56190 | 295 | IMPORT osKernelStart |
samdanbury | 6:37b6d0d56190 | 296 | IMPORT osThreadCreate |
samdanbury | 6:37b6d0d56190 | 297 | IMPORT exit |
samdanbury | 6:37b6d0d56190 | 298 | |
samdanbury | 6:37b6d0d56190 | 299 | BL __user_setup_stackheap |
samdanbury | 6:37b6d0d56190 | 300 | MOV R1,R2 |
samdanbury | 6:37b6d0d56190 | 301 | BL __rt_lib_init |
samdanbury | 6:37b6d0d56190 | 302 | BL osKernelInitialize |
samdanbury | 6:37b6d0d56190 | 303 | BL set_main_stack |
samdanbury | 6:37b6d0d56190 | 304 | LDR R0,=os_thread_def_main |
samdanbury | 6:37b6d0d56190 | 305 | MOVS R1,#0 |
samdanbury | 6:37b6d0d56190 | 306 | BL osThreadCreate |
samdanbury | 6:37b6d0d56190 | 307 | BL osKernelStart |
samdanbury | 6:37b6d0d56190 | 308 | BL exit |
samdanbury | 6:37b6d0d56190 | 309 | |
samdanbury | 6:37b6d0d56190 | 310 | ALIGN |
samdanbury | 6:37b6d0d56190 | 311 | } |
samdanbury | 6:37b6d0d56190 | 312 | #endif |
samdanbury | 6:37b6d0d56190 | 313 | |
samdanbury | 6:37b6d0d56190 | 314 | #elif defined (__GNUC__) |
samdanbury | 6:37b6d0d56190 | 315 | |
samdanbury | 6:37b6d0d56190 | 316 | #ifdef __CS3__ |
samdanbury | 6:37b6d0d56190 | 317 | |
samdanbury | 6:37b6d0d56190 | 318 | /* CS3 start_c routine. |
samdanbury | 6:37b6d0d56190 | 319 | * |
samdanbury | 6:37b6d0d56190 | 320 | * Copyright (c) 2006, 2007 CodeSourcery Inc |
samdanbury | 6:37b6d0d56190 | 321 | * |
samdanbury | 6:37b6d0d56190 | 322 | * The authors hereby grant permission to use, copy, modify, distribute, |
samdanbury | 6:37b6d0d56190 | 323 | * and license this software and its documentation for any purpose, provided |
samdanbury | 6:37b6d0d56190 | 324 | * that existing copyright notices are retained in all copies and that this |
samdanbury | 6:37b6d0d56190 | 325 | * notice is included verbatim in any distributions. No written agreement, |
samdanbury | 6:37b6d0d56190 | 326 | * license, or royalty fee is required for any of the authorized uses. |
samdanbury | 6:37b6d0d56190 | 327 | * Modifications to this software may be copyrighted by their authors |
samdanbury | 6:37b6d0d56190 | 328 | * and need not follow the licensing terms described here, provided that |
samdanbury | 6:37b6d0d56190 | 329 | * the new terms are clearly indicated on the first page of each file where |
samdanbury | 6:37b6d0d56190 | 330 | * they apply. |
samdanbury | 6:37b6d0d56190 | 331 | */ |
samdanbury | 6:37b6d0d56190 | 332 | |
samdanbury | 6:37b6d0d56190 | 333 | #include "cs3.h" |
samdanbury | 6:37b6d0d56190 | 334 | |
samdanbury | 6:37b6d0d56190 | 335 | extern void __libc_init_array (void); |
samdanbury | 6:37b6d0d56190 | 336 | |
samdanbury | 6:37b6d0d56190 | 337 | __attribute ((noreturn)) void __cs3_start_c (void){ |
samdanbury | 6:37b6d0d56190 | 338 | unsigned regions = __cs3_region_num; |
samdanbury | 6:37b6d0d56190 | 339 | const struct __cs3_region *rptr = __cs3_regions; |
samdanbury | 6:37b6d0d56190 | 340 | |
samdanbury | 6:37b6d0d56190 | 341 | /* Initialize memory */ |
samdanbury | 6:37b6d0d56190 | 342 | for (regions = __cs3_region_num, rptr = __cs3_regions; regions--; rptr++) { |
samdanbury | 6:37b6d0d56190 | 343 | long long *src = (long long *)rptr->init; |
samdanbury | 6:37b6d0d56190 | 344 | long long *dst = (long long *)rptr->data; |
samdanbury | 6:37b6d0d56190 | 345 | unsigned limit = rptr->init_size; |
samdanbury | 6:37b6d0d56190 | 346 | unsigned count; |
samdanbury | 6:37b6d0d56190 | 347 | |
samdanbury | 6:37b6d0d56190 | 348 | if (src != dst) |
samdanbury | 6:37b6d0d56190 | 349 | for (count = 0; count != limit; count += sizeof (long long)) |
samdanbury | 6:37b6d0d56190 | 350 | *dst++ = *src++; |
samdanbury | 6:37b6d0d56190 | 351 | else |
samdanbury | 6:37b6d0d56190 | 352 | dst = (long long *)((char *)dst + limit); |
samdanbury | 6:37b6d0d56190 | 353 | limit = rptr->zero_size; |
samdanbury | 6:37b6d0d56190 | 354 | for (count = 0; count != limit; count += sizeof (long long)) |
samdanbury | 6:37b6d0d56190 | 355 | *dst++ = 0; |
samdanbury | 6:37b6d0d56190 | 356 | } |
samdanbury | 6:37b6d0d56190 | 357 | |
samdanbury | 6:37b6d0d56190 | 358 | /* Run initializers. */ |
samdanbury | 6:37b6d0d56190 | 359 | __libc_init_array (); |
samdanbury | 6:37b6d0d56190 | 360 | |
samdanbury | 6:37b6d0d56190 | 361 | osKernelInitialize(); |
samdanbury | 6:37b6d0d56190 | 362 | set_main_stack(); |
samdanbury | 6:37b6d0d56190 | 363 | osThreadCreate(&os_thread_def_main, NULL); |
samdanbury | 6:37b6d0d56190 | 364 | osKernelStart(); |
samdanbury | 6:37b6d0d56190 | 365 | for (;;); |
samdanbury | 6:37b6d0d56190 | 366 | } |
samdanbury | 6:37b6d0d56190 | 367 | |
samdanbury | 6:37b6d0d56190 | 368 | #else |
samdanbury | 6:37b6d0d56190 | 369 | |
samdanbury | 6:37b6d0d56190 | 370 | __attribute__((naked)) void software_init_hook (void) { |
samdanbury | 6:37b6d0d56190 | 371 | __asm ( |
samdanbury | 6:37b6d0d56190 | 372 | ".syntax unified\n" |
samdanbury | 6:37b6d0d56190 | 373 | ".thumb\n" |
samdanbury | 6:37b6d0d56190 | 374 | "movs r0,#0\n" |
samdanbury | 6:37b6d0d56190 | 375 | "movs r1,#0\n" |
samdanbury | 6:37b6d0d56190 | 376 | "mov r4,r0\n" |
samdanbury | 6:37b6d0d56190 | 377 | "mov r5,r1\n" |
samdanbury | 6:37b6d0d56190 | 378 | "ldr r0,= __libc_fini_array\n" |
samdanbury | 6:37b6d0d56190 | 379 | "bl atexit\n" |
samdanbury | 6:37b6d0d56190 | 380 | "bl __libc_init_array\n" |
samdanbury | 6:37b6d0d56190 | 381 | "mov r0,r4\n" |
samdanbury | 6:37b6d0d56190 | 382 | "mov r1,r5\n" |
samdanbury | 6:37b6d0d56190 | 383 | "bl osKernelInitialize\n" |
samdanbury | 6:37b6d0d56190 | 384 | "bl set_main_stack\n" |
samdanbury | 6:37b6d0d56190 | 385 | "ldr r0,=os_thread_def_main\n" |
samdanbury | 6:37b6d0d56190 | 386 | "movs r1,#0\n" |
samdanbury | 6:37b6d0d56190 | 387 | "bl osThreadCreate\n" |
samdanbury | 6:37b6d0d56190 | 388 | "bl osKernelStart\n" |
samdanbury | 6:37b6d0d56190 | 389 | "bl exit\n" |
samdanbury | 6:37b6d0d56190 | 390 | ); |
samdanbury | 6:37b6d0d56190 | 391 | } |
samdanbury | 6:37b6d0d56190 | 392 | |
samdanbury | 6:37b6d0d56190 | 393 | #endif |
samdanbury | 6:37b6d0d56190 | 394 | |
samdanbury | 6:37b6d0d56190 | 395 | #elif defined (__ICCARM__) |
samdanbury | 6:37b6d0d56190 | 396 | |
samdanbury | 6:37b6d0d56190 | 397 | extern int __low_level_init(void); |
samdanbury | 6:37b6d0d56190 | 398 | extern void __iar_data_init3(void); |
samdanbury | 6:37b6d0d56190 | 399 | extern void exit(int arg); |
samdanbury | 6:37b6d0d56190 | 400 | |
samdanbury | 6:37b6d0d56190 | 401 | __noreturn __stackless void __cmain(void) { |
samdanbury | 6:37b6d0d56190 | 402 | int a; |
samdanbury | 6:37b6d0d56190 | 403 | |
samdanbury | 6:37b6d0d56190 | 404 | if (__low_level_init() != 0) { |
samdanbury | 6:37b6d0d56190 | 405 | __iar_data_init3(); |
samdanbury | 6:37b6d0d56190 | 406 | } |
samdanbury | 6:37b6d0d56190 | 407 | osKernelInitialize(); |
samdanbury | 6:37b6d0d56190 | 408 | osThreadCreate(&os_thread_def_main, NULL); |
samdanbury | 6:37b6d0d56190 | 409 | a = osKernelStart(); |
samdanbury | 6:37b6d0d56190 | 410 | exit(a); |
samdanbury | 6:37b6d0d56190 | 411 | } |
samdanbury | 6:37b6d0d56190 | 412 | |
samdanbury | 6:37b6d0d56190 | 413 | #endif |
samdanbury | 6:37b6d0d56190 | 414 | |
samdanbury | 6:37b6d0d56190 | 415 | |
samdanbury | 6:37b6d0d56190 | 416 | /*---------------------------------------------------------------------------- |
samdanbury | 6:37b6d0d56190 | 417 | * end of file |
samdanbury | 6:37b6d0d56190 | 418 | *---------------------------------------------------------------------------*/ |
samdanbury | 6:37b6d0d56190 | 419 |