These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Committer:
frank26080115
Date:
Sun Mar 20 05:38:56 2011 +0000
Revision:
0:bf7b9fba3924

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frank26080115 0:bf7b9fba3924 1 /*
frank26080115 0:bf7b9fba3924 2 * Copyright (c) 2004-2005, Swedish Institute of Computer Science.
frank26080115 0:bf7b9fba3924 3 * All rights reserved.
frank26080115 0:bf7b9fba3924 4 *
frank26080115 0:bf7b9fba3924 5 * Redistribution and use in source and binary forms, with or without
frank26080115 0:bf7b9fba3924 6 * modification, are permitted provided that the following conditions
frank26080115 0:bf7b9fba3924 7 * are met:
frank26080115 0:bf7b9fba3924 8 * 1. Redistributions of source code must retain the above copyright
frank26080115 0:bf7b9fba3924 9 * notice, this list of conditions and the following disclaimer.
frank26080115 0:bf7b9fba3924 10 * 2. Redistributions in binary form must reproduce the above copyright
frank26080115 0:bf7b9fba3924 11 * notice, this list of conditions and the following disclaimer in the
frank26080115 0:bf7b9fba3924 12 * documentation and/or other materials provided with the distribution.
frank26080115 0:bf7b9fba3924 13 * 3. Neither the name of the Institute nor the names of its contributors
frank26080115 0:bf7b9fba3924 14 * may be used to endorse or promote products derived from this software
frank26080115 0:bf7b9fba3924 15 * without specific prior written permission.
frank26080115 0:bf7b9fba3924 16 *
frank26080115 0:bf7b9fba3924 17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
frank26080115 0:bf7b9fba3924 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
frank26080115 0:bf7b9fba3924 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
frank26080115 0:bf7b9fba3924 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
frank26080115 0:bf7b9fba3924 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
frank26080115 0:bf7b9fba3924 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
frank26080115 0:bf7b9fba3924 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
frank26080115 0:bf7b9fba3924 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
frank26080115 0:bf7b9fba3924 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
frank26080115 0:bf7b9fba3924 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
frank26080115 0:bf7b9fba3924 27 * SUCH DAMAGE.
frank26080115 0:bf7b9fba3924 28 *
frank26080115 0:bf7b9fba3924 29 * This file is part of the uIP TCP/IP stack
frank26080115 0:bf7b9fba3924 30 *
frank26080115 0:bf7b9fba3924 31 * Author: Adam Dunkels <adam@sics.se>
frank26080115 0:bf7b9fba3924 32 *
frank26080115 0:bf7b9fba3924 33 * $Id: pt.h,v 1.2 2006/06/12 08:00:30 adam Exp $
frank26080115 0:bf7b9fba3924 34 */
frank26080115 0:bf7b9fba3924 35
frank26080115 0:bf7b9fba3924 36 /**
frank26080115 0:bf7b9fba3924 37 * \addtogroup pt
frank26080115 0:bf7b9fba3924 38 * @{
frank26080115 0:bf7b9fba3924 39 */
frank26080115 0:bf7b9fba3924 40
frank26080115 0:bf7b9fba3924 41 /**
frank26080115 0:bf7b9fba3924 42 * \file
frank26080115 0:bf7b9fba3924 43 * Protothreads implementation.
frank26080115 0:bf7b9fba3924 44 * \author
frank26080115 0:bf7b9fba3924 45 * Adam Dunkels <adam@sics.se>
frank26080115 0:bf7b9fba3924 46 *
frank26080115 0:bf7b9fba3924 47 */
frank26080115 0:bf7b9fba3924 48
frank26080115 0:bf7b9fba3924 49 #ifndef __PT_H__
frank26080115 0:bf7b9fba3924 50 #define __PT_H__
frank26080115 0:bf7b9fba3924 51
frank26080115 0:bf7b9fba3924 52 #include "lc.h"
frank26080115 0:bf7b9fba3924 53
frank26080115 0:bf7b9fba3924 54 struct pt {
frank26080115 0:bf7b9fba3924 55 lc_t lc;
frank26080115 0:bf7b9fba3924 56 };
frank26080115 0:bf7b9fba3924 57
frank26080115 0:bf7b9fba3924 58 #define PT_WAITING 0
frank26080115 0:bf7b9fba3924 59 #define PT_EXITED 1
frank26080115 0:bf7b9fba3924 60 #define PT_ENDED 2
frank26080115 0:bf7b9fba3924 61 #define PT_YIELDED 3
frank26080115 0:bf7b9fba3924 62
frank26080115 0:bf7b9fba3924 63 /**
frank26080115 0:bf7b9fba3924 64 * \name Initialization
frank26080115 0:bf7b9fba3924 65 * @{
frank26080115 0:bf7b9fba3924 66 */
frank26080115 0:bf7b9fba3924 67
frank26080115 0:bf7b9fba3924 68 /**
frank26080115 0:bf7b9fba3924 69 * Initialize a protothread.
frank26080115 0:bf7b9fba3924 70 *
frank26080115 0:bf7b9fba3924 71 * Initializes a protothread. Initialization must be done prior to
frank26080115 0:bf7b9fba3924 72 * starting to execute the protothread.
frank26080115 0:bf7b9fba3924 73 *
frank26080115 0:bf7b9fba3924 74 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 75 *
frank26080115 0:bf7b9fba3924 76 * \sa PT_SPAWN()
frank26080115 0:bf7b9fba3924 77 *
frank26080115 0:bf7b9fba3924 78 * \hideinitializer
frank26080115 0:bf7b9fba3924 79 */
frank26080115 0:bf7b9fba3924 80 #define PT_INIT(pt) LC_INIT((pt)->lc)
frank26080115 0:bf7b9fba3924 81
frank26080115 0:bf7b9fba3924 82 /** @} */
frank26080115 0:bf7b9fba3924 83
frank26080115 0:bf7b9fba3924 84 /**
frank26080115 0:bf7b9fba3924 85 * \name Declaration and definition
frank26080115 0:bf7b9fba3924 86 * @{
frank26080115 0:bf7b9fba3924 87 */
frank26080115 0:bf7b9fba3924 88
frank26080115 0:bf7b9fba3924 89 /**
frank26080115 0:bf7b9fba3924 90 * Declaration of a protothread.
frank26080115 0:bf7b9fba3924 91 *
frank26080115 0:bf7b9fba3924 92 * This macro is used to declare a protothread. All protothreads must
frank26080115 0:bf7b9fba3924 93 * be declared with this macro.
frank26080115 0:bf7b9fba3924 94 *
frank26080115 0:bf7b9fba3924 95 * \param name_args The name and arguments of the C function
frank26080115 0:bf7b9fba3924 96 * implementing the protothread.
frank26080115 0:bf7b9fba3924 97 *
frank26080115 0:bf7b9fba3924 98 * \hideinitializer
frank26080115 0:bf7b9fba3924 99 */
frank26080115 0:bf7b9fba3924 100 #define PT_THREAD(name_args) char name_args
frank26080115 0:bf7b9fba3924 101
frank26080115 0:bf7b9fba3924 102 /**
frank26080115 0:bf7b9fba3924 103 * Declare the start of a protothread inside the C function
frank26080115 0:bf7b9fba3924 104 * implementing the protothread.
frank26080115 0:bf7b9fba3924 105 *
frank26080115 0:bf7b9fba3924 106 * This macro is used to declare the starting point of a
frank26080115 0:bf7b9fba3924 107 * protothread. It should be placed at the start of the function in
frank26080115 0:bf7b9fba3924 108 * which the protothread runs. All C statements above the PT_BEGIN()
frank26080115 0:bf7b9fba3924 109 * invokation will be executed each time the protothread is scheduled.
frank26080115 0:bf7b9fba3924 110 *
frank26080115 0:bf7b9fba3924 111 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 112 *
frank26080115 0:bf7b9fba3924 113 * \hideinitializer
frank26080115 0:bf7b9fba3924 114 */
frank26080115 0:bf7b9fba3924 115 #define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)
frank26080115 0:bf7b9fba3924 116
frank26080115 0:bf7b9fba3924 117 /**
frank26080115 0:bf7b9fba3924 118 * Declare the end of a protothread.
frank26080115 0:bf7b9fba3924 119 *
frank26080115 0:bf7b9fba3924 120 * This macro is used for declaring that a protothread ends. It must
frank26080115 0:bf7b9fba3924 121 * always be used together with a matching PT_BEGIN() macro.
frank26080115 0:bf7b9fba3924 122 *
frank26080115 0:bf7b9fba3924 123 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 124 *
frank26080115 0:bf7b9fba3924 125 * \hideinitializer
frank26080115 0:bf7b9fba3924 126 */
frank26080115 0:bf7b9fba3924 127 #define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \
frank26080115 0:bf7b9fba3924 128 PT_INIT(pt); return PT_ENDED; }
frank26080115 0:bf7b9fba3924 129
frank26080115 0:bf7b9fba3924 130 /** @} */
frank26080115 0:bf7b9fba3924 131
frank26080115 0:bf7b9fba3924 132 /**
frank26080115 0:bf7b9fba3924 133 * \name Blocked wait
frank26080115 0:bf7b9fba3924 134 * @{
frank26080115 0:bf7b9fba3924 135 */
frank26080115 0:bf7b9fba3924 136
frank26080115 0:bf7b9fba3924 137 /**
frank26080115 0:bf7b9fba3924 138 * Block and wait until condition is true.
frank26080115 0:bf7b9fba3924 139 *
frank26080115 0:bf7b9fba3924 140 * This macro blocks the protothread until the specified condition is
frank26080115 0:bf7b9fba3924 141 * true.
frank26080115 0:bf7b9fba3924 142 *
frank26080115 0:bf7b9fba3924 143 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 144 * \param condition The condition.
frank26080115 0:bf7b9fba3924 145 *
frank26080115 0:bf7b9fba3924 146 * \hideinitializer
frank26080115 0:bf7b9fba3924 147 */
frank26080115 0:bf7b9fba3924 148 #define PT_WAIT_UNTIL(pt, condition) \
frank26080115 0:bf7b9fba3924 149 do { \
frank26080115 0:bf7b9fba3924 150 LC_SET((pt)->lc); \
frank26080115 0:bf7b9fba3924 151 if(!(condition)) { \
frank26080115 0:bf7b9fba3924 152 return PT_WAITING; \
frank26080115 0:bf7b9fba3924 153 } \
frank26080115 0:bf7b9fba3924 154 } while(0)
frank26080115 0:bf7b9fba3924 155
frank26080115 0:bf7b9fba3924 156 /**
frank26080115 0:bf7b9fba3924 157 * Block and wait while condition is true.
frank26080115 0:bf7b9fba3924 158 *
frank26080115 0:bf7b9fba3924 159 * This function blocks and waits while condition is true. See
frank26080115 0:bf7b9fba3924 160 * PT_WAIT_UNTIL().
frank26080115 0:bf7b9fba3924 161 *
frank26080115 0:bf7b9fba3924 162 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 163 * \param cond The condition.
frank26080115 0:bf7b9fba3924 164 *
frank26080115 0:bf7b9fba3924 165 * \hideinitializer
frank26080115 0:bf7b9fba3924 166 */
frank26080115 0:bf7b9fba3924 167 #define PT_WAIT_WHILE(pt, cond) PT_WAIT_UNTIL((pt), !(cond))
frank26080115 0:bf7b9fba3924 168
frank26080115 0:bf7b9fba3924 169 /** @} */
frank26080115 0:bf7b9fba3924 170
frank26080115 0:bf7b9fba3924 171 /**
frank26080115 0:bf7b9fba3924 172 * \name Hierarchical protothreads
frank26080115 0:bf7b9fba3924 173 * @{
frank26080115 0:bf7b9fba3924 174 */
frank26080115 0:bf7b9fba3924 175
frank26080115 0:bf7b9fba3924 176 /**
frank26080115 0:bf7b9fba3924 177 * Block and wait until a child protothread completes.
frank26080115 0:bf7b9fba3924 178 *
frank26080115 0:bf7b9fba3924 179 * This macro schedules a child protothread. The current protothread
frank26080115 0:bf7b9fba3924 180 * will block until the child protothread completes.
frank26080115 0:bf7b9fba3924 181 *
frank26080115 0:bf7b9fba3924 182 * \note The child protothread must be manually initialized with the
frank26080115 0:bf7b9fba3924 183 * PT_INIT() function before this function is used.
frank26080115 0:bf7b9fba3924 184 *
frank26080115 0:bf7b9fba3924 185 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 186 * \param thread The child protothread with arguments
frank26080115 0:bf7b9fba3924 187 *
frank26080115 0:bf7b9fba3924 188 * \sa PT_SPAWN()
frank26080115 0:bf7b9fba3924 189 *
frank26080115 0:bf7b9fba3924 190 * \hideinitializer
frank26080115 0:bf7b9fba3924 191 */
frank26080115 0:bf7b9fba3924 192 #define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread))
frank26080115 0:bf7b9fba3924 193
frank26080115 0:bf7b9fba3924 194 /**
frank26080115 0:bf7b9fba3924 195 * Spawn a child protothread and wait until it exits.
frank26080115 0:bf7b9fba3924 196 *
frank26080115 0:bf7b9fba3924 197 * This macro spawns a child protothread and waits until it exits. The
frank26080115 0:bf7b9fba3924 198 * macro can only be used within a protothread.
frank26080115 0:bf7b9fba3924 199 *
frank26080115 0:bf7b9fba3924 200 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 201 * \param child A pointer to the child protothread's control structure.
frank26080115 0:bf7b9fba3924 202 * \param thread The child protothread with arguments
frank26080115 0:bf7b9fba3924 203 *
frank26080115 0:bf7b9fba3924 204 * \hideinitializer
frank26080115 0:bf7b9fba3924 205 */
frank26080115 0:bf7b9fba3924 206 #define PT_SPAWN(pt, child, thread) \
frank26080115 0:bf7b9fba3924 207 do { \
frank26080115 0:bf7b9fba3924 208 PT_INIT((child)); \
frank26080115 0:bf7b9fba3924 209 PT_WAIT_THREAD((pt), (thread)); \
frank26080115 0:bf7b9fba3924 210 } while(0)
frank26080115 0:bf7b9fba3924 211
frank26080115 0:bf7b9fba3924 212 /** @} */
frank26080115 0:bf7b9fba3924 213
frank26080115 0:bf7b9fba3924 214 /**
frank26080115 0:bf7b9fba3924 215 * \name Exiting and restarting
frank26080115 0:bf7b9fba3924 216 * @{
frank26080115 0:bf7b9fba3924 217 */
frank26080115 0:bf7b9fba3924 218
frank26080115 0:bf7b9fba3924 219 /**
frank26080115 0:bf7b9fba3924 220 * Restart the protothread.
frank26080115 0:bf7b9fba3924 221 *
frank26080115 0:bf7b9fba3924 222 * This macro will block and cause the running protothread to restart
frank26080115 0:bf7b9fba3924 223 * its execution at the place of the PT_BEGIN() call.
frank26080115 0:bf7b9fba3924 224 *
frank26080115 0:bf7b9fba3924 225 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 226 *
frank26080115 0:bf7b9fba3924 227 * \hideinitializer
frank26080115 0:bf7b9fba3924 228 */
frank26080115 0:bf7b9fba3924 229 #define PT_RESTART(pt) \
frank26080115 0:bf7b9fba3924 230 do { \
frank26080115 0:bf7b9fba3924 231 PT_INIT(pt); \
frank26080115 0:bf7b9fba3924 232 return PT_WAITING; \
frank26080115 0:bf7b9fba3924 233 } while(0)
frank26080115 0:bf7b9fba3924 234
frank26080115 0:bf7b9fba3924 235 /**
frank26080115 0:bf7b9fba3924 236 * Exit the protothread.
frank26080115 0:bf7b9fba3924 237 *
frank26080115 0:bf7b9fba3924 238 * This macro causes the protothread to exit. If the protothread was
frank26080115 0:bf7b9fba3924 239 * spawned by another protothread, the parent protothread will become
frank26080115 0:bf7b9fba3924 240 * unblocked and can continue to run.
frank26080115 0:bf7b9fba3924 241 *
frank26080115 0:bf7b9fba3924 242 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 243 *
frank26080115 0:bf7b9fba3924 244 * \hideinitializer
frank26080115 0:bf7b9fba3924 245 */
frank26080115 0:bf7b9fba3924 246 #define PT_EXIT(pt) \
frank26080115 0:bf7b9fba3924 247 do { \
frank26080115 0:bf7b9fba3924 248 PT_INIT(pt); \
frank26080115 0:bf7b9fba3924 249 return PT_EXITED; \
frank26080115 0:bf7b9fba3924 250 } while(0)
frank26080115 0:bf7b9fba3924 251
frank26080115 0:bf7b9fba3924 252 /** @} */
frank26080115 0:bf7b9fba3924 253
frank26080115 0:bf7b9fba3924 254 /**
frank26080115 0:bf7b9fba3924 255 * \name Calling a protothread
frank26080115 0:bf7b9fba3924 256 * @{
frank26080115 0:bf7b9fba3924 257 */
frank26080115 0:bf7b9fba3924 258
frank26080115 0:bf7b9fba3924 259 /**
frank26080115 0:bf7b9fba3924 260 * Schedule a protothread.
frank26080115 0:bf7b9fba3924 261 *
frank26080115 0:bf7b9fba3924 262 * This function shedules a protothread. The return value of the
frank26080115 0:bf7b9fba3924 263 * function is non-zero if the protothread is running or zero if the
frank26080115 0:bf7b9fba3924 264 * protothread has exited.
frank26080115 0:bf7b9fba3924 265 *
frank26080115 0:bf7b9fba3924 266 * \param f The call to the C function implementing the protothread to
frank26080115 0:bf7b9fba3924 267 * be scheduled
frank26080115 0:bf7b9fba3924 268 *
frank26080115 0:bf7b9fba3924 269 * \hideinitializer
frank26080115 0:bf7b9fba3924 270 */
frank26080115 0:bf7b9fba3924 271 #define PT_SCHEDULE(f) ((f) == PT_WAITING)
frank26080115 0:bf7b9fba3924 272
frank26080115 0:bf7b9fba3924 273 /** @} */
frank26080115 0:bf7b9fba3924 274
frank26080115 0:bf7b9fba3924 275 /**
frank26080115 0:bf7b9fba3924 276 * \name Yielding from a protothread
frank26080115 0:bf7b9fba3924 277 * @{
frank26080115 0:bf7b9fba3924 278 */
frank26080115 0:bf7b9fba3924 279
frank26080115 0:bf7b9fba3924 280 /**
frank26080115 0:bf7b9fba3924 281 * Yield from the current protothread.
frank26080115 0:bf7b9fba3924 282 *
frank26080115 0:bf7b9fba3924 283 * This function will yield the protothread, thereby allowing other
frank26080115 0:bf7b9fba3924 284 * processing to take place in the system.
frank26080115 0:bf7b9fba3924 285 *
frank26080115 0:bf7b9fba3924 286 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 287 *
frank26080115 0:bf7b9fba3924 288 * \hideinitializer
frank26080115 0:bf7b9fba3924 289 */
frank26080115 0:bf7b9fba3924 290 #define PT_YIELD(pt) \
frank26080115 0:bf7b9fba3924 291 do { \
frank26080115 0:bf7b9fba3924 292 PT_YIELD_FLAG = 0; \
frank26080115 0:bf7b9fba3924 293 LC_SET((pt)->lc); \
frank26080115 0:bf7b9fba3924 294 if(PT_YIELD_FLAG == 0) { \
frank26080115 0:bf7b9fba3924 295 return PT_YIELDED; \
frank26080115 0:bf7b9fba3924 296 } \
frank26080115 0:bf7b9fba3924 297 } while(0)
frank26080115 0:bf7b9fba3924 298
frank26080115 0:bf7b9fba3924 299 /**
frank26080115 0:bf7b9fba3924 300 * \brief Yield from the protothread until a condition occurs.
frank26080115 0:bf7b9fba3924 301 * \param pt A pointer to the protothread control structure.
frank26080115 0:bf7b9fba3924 302 * \param cond The condition.
frank26080115 0:bf7b9fba3924 303 *
frank26080115 0:bf7b9fba3924 304 * This function will yield the protothread, until the
frank26080115 0:bf7b9fba3924 305 * specified condition evaluates to true.
frank26080115 0:bf7b9fba3924 306 *
frank26080115 0:bf7b9fba3924 307 *
frank26080115 0:bf7b9fba3924 308 * \hideinitializer
frank26080115 0:bf7b9fba3924 309 */
frank26080115 0:bf7b9fba3924 310 #define PT_YIELD_UNTIL(pt, cond) \
frank26080115 0:bf7b9fba3924 311 do { \
frank26080115 0:bf7b9fba3924 312 PT_YIELD_FLAG = 0; \
frank26080115 0:bf7b9fba3924 313 LC_SET((pt)->lc); \
frank26080115 0:bf7b9fba3924 314 if((PT_YIELD_FLAG == 0) || !(cond)) { \
frank26080115 0:bf7b9fba3924 315 return PT_YIELDED; \
frank26080115 0:bf7b9fba3924 316 } \
frank26080115 0:bf7b9fba3924 317 } while(0)
frank26080115 0:bf7b9fba3924 318
frank26080115 0:bf7b9fba3924 319 /** @} */
frank26080115 0:bf7b9fba3924 320
frank26080115 0:bf7b9fba3924 321 #endif /* __PT_H__ */
frank26080115 0:bf7b9fba3924 322
frank26080115 0:bf7b9fba3924 323 /** @} */