mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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