| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2010, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bkni.h $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/1 $ |
|---|
| 12 | * $brcm_Date: 8/19/10 8:07a $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/basemodules/kni/ucos_ii/bkni.h $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/1 8/19/10 8:07a jfisher |
|---|
| 21 | * SW7572-49: Add nesting check. |
|---|
| 22 | * |
|---|
| 23 | * Hydra_Software_Devel/4 1/19/09 1:56p cnovak |
|---|
| 24 | * PR32280: Add BKNI_ASSERT_ISR_CONTEXT functionality, merged from other |
|---|
| 25 | * OS ports. |
|---|
| 26 | * |
|---|
| 27 | * Hydra_Software_Devel/3 6/19/08 4:32p cnovak |
|---|
| 28 | * PR43697: Clean up uCOS port and move back to Hyrdra branch. |
|---|
| 29 | * |
|---|
| 30 | * Hydra_Software_Devel/2 2/4/05 6:21p cnovak |
|---|
| 31 | * PR 12841: Add includes bkni_multi and bkni_ucos so modules don't have |
|---|
| 32 | * to. |
|---|
| 33 | * |
|---|
| 34 | * Hydra_Software_Devel/1 1/31/05 5:52p cnovak |
|---|
| 35 | * PR 12841: Copied from ../generic directory, revision 29. |
|---|
| 36 | * |
|---|
| 37 | ***************************************************************************/ |
|---|
| 38 | #ifndef BKNI_H__ |
|---|
| 39 | #define BKNI_H__ |
|---|
| 40 | |
|---|
| 41 | #include "bkni_print.h" |
|---|
| 42 | |
|---|
| 43 | #ifdef __cplusplus |
|---|
| 44 | extern "C" { |
|---|
| 45 | #endif |
|---|
| 46 | |
|---|
| 47 | /*====================== Module Overview =========================== |
|---|
| 48 | |
|---|
| 49 | ---++++ *Summary:* |
|---|
| 50 | |
|---|
| 51 | The Kernel Interface is the set of operating system and standard C library functions |
|---|
| 52 | required by the Magnum architecture which is portable across all software |
|---|
| 53 | and hardware platforms which Broadcom uses. |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | ---++++ *Requirements:* |
|---|
| 57 | |
|---|
| 58 | *Portable* - every function must work on every platform in the same well-defined way. |
|---|
| 59 | Some error conditions cannot or should not be standardized, therefore we also include |
|---|
| 60 | usage rules (see BKNI_EnterCriticalSection for an example.) |
|---|
| 61 | Every platform implementationmust be regularly checked with compliance tests. |
|---|
| 62 | |
|---|
| 63 | *Safe* - Because of the inherent dangers of multitasking, |
|---|
| 64 | each kernel interface implementation must ensure there are no race conditions within the |
|---|
| 65 | implementation. |
|---|
| 66 | The API must also be simple and explicit in order to help developers avoid race |
|---|
| 67 | conditions in their code. |
|---|
| 68 | |
|---|
| 69 | *Minimal* - The number of functions should be just enough to allow us to implement the Magnum |
|---|
| 70 | architecture. Platform-dependent code should make use of platform-specific functions. |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | ---++++ *Files:* |
|---|
| 74 | |
|---|
| 75 | The kernel interface is divided into three sections: |
|---|
| 76 | |
|---|
| 77 | *bkni.h* is the single-task kernel interface. It is used by code which follows the single-task |
|---|
| 78 | model of execution specified in ThreadSafe. |
|---|
| 79 | |
|---|
| 80 | *bkni_multi.h* is the multi-tasking kernel interface. It is used by code which follows |
|---|
| 81 | the multi-task model of execution specified in ThreadSafe. bkni_multi.h also includes |
|---|
| 82 | bkni.h. |
|---|
| 83 | |
|---|
| 84 | *bkni_metrics.h* is the metrics interface. It must not be called by Magnum code, but |
|---|
| 85 | provides system monitoring and debugging to platform-specific code. |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | <nopublish> |
|---|
| 89 | ---++++ *Summary of Changes:* |
|---|
| 90 | |
|---|
| 91 | New naming convention. |
|---|
| 92 | |
|---|
| 93 | Replaced structures with opaque handles. |
|---|
| 94 | |
|---|
| 95 | Init and Uninit functions. |
|---|
| 96 | |
|---|
| 97 | Replaced semaphores with mutexes.While a semaphore can be configured as a mutex, it can |
|---|
| 98 | easily be misconfigured and introduce race conditions. The combination of a mutex and an event |
|---|
| 99 | is simple and sufficient. |
|---|
| 100 | |
|---|
| 101 | Removed sprintf and vsprintf because they introduce possible buffer overruns. snprintf |
|---|
| 102 | and vsnprintf are sufficient. |
|---|
| 103 | |
|---|
| 104 | Memory map functionality (MmuMap and UnmmuMap) was moved to the MemoryManager. |
|---|
| 105 | |
|---|
| 106 | Removed debug macros (DBGMSG) and data types (WORD, HANDLE, etc.). |
|---|
| 107 | </nopublish> |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | ---++++ *Interrupt Safety:* |
|---|
| 111 | |
|---|
| 112 | The following functions can be called from an ISR or a critical section: |
|---|
| 113 | |
|---|
| 114 | * BKNI_Memset, BKNI_Memcpy, BKNI_Memcmp, BKNI_Memchr, BKNI_Memmove |
|---|
| 115 | * BKNI_Printf, BKNI_Vprintf |
|---|
| 116 | * BKNI_Delay |
|---|
| 117 | * BKNI_SetEvent |
|---|
| 118 | * BKNI_WaitForEvent, but only with a timeout of zero. |
|---|
| 119 | |
|---|
| 120 | All other functions are not callable from either an ISR or critical section. |
|---|
| 121 | *****************************************************************************/ |
|---|
| 122 | |
|---|
| 123 | /*************************************************************************** |
|---|
| 124 | Summary: |
|---|
| 125 | Event handle created by BKNI_CreateEvent. |
|---|
| 126 | ****************************************************************************/ |
|---|
| 127 | typedef struct BKNI_EventObj *BKNI_EventHandle; |
|---|
| 128 | |
|---|
| 129 | /*************************************************************************** |
|---|
| 130 | Summary: |
|---|
| 131 | Initialize the kernel interface before use. |
|---|
| 132 | |
|---|
| 133 | Description: |
|---|
| 134 | The system must call BKNI_Init() before making any other kernel interface call. |
|---|
| 135 | Magnum code cannot call BKNI_Init(). |
|---|
| 136 | |
|---|
| 137 | The BKNI_Init call should reset the metrics interface. See BKNI_Metrics_Reset. |
|---|
| 138 | |
|---|
| 139 | Returns: |
|---|
| 140 | BERR_SUCCESS - The kernel interface successfully initialized. |
|---|
| 141 | BERR_OS_ERROR - Initialization failed. |
|---|
| 142 | ****************************************************************************/ |
|---|
| 143 | BERR_Code BKNI_Init(void); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | /*************************************************************************** |
|---|
| 147 | Summary: |
|---|
| 148 | Uninitialize the kernel interface after use. |
|---|
| 149 | |
|---|
| 150 | Description: |
|---|
| 151 | Cleans up the kernel interface. No kernel interface calls can be made after this, |
|---|
| 152 | except BKNI_Init(). |
|---|
| 153 | Magnum code cannot call BKNI_Uninit(). |
|---|
| 154 | |
|---|
| 155 | Returns: |
|---|
| 156 | <none> |
|---|
| 157 | ****************************************************************************/ |
|---|
| 158 | void BKNI_Uninit(void); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | /*************************************************************************** |
|---|
| 162 | Summary: |
|---|
| 163 | Set byte array to a value. |
|---|
| 164 | |
|---|
| 165 | Description: |
|---|
| 166 | Copies the value of ch (converted to an unsigned char) into each of the first n |
|---|
| 167 | characters of the memory pointed to by mem. |
|---|
| 168 | |
|---|
| 169 | Can be called from an interrupt-context. |
|---|
| 170 | |
|---|
| 171 | Input: |
|---|
| 172 | mem - memory to be set |
|---|
| 173 | ch - 8 bit value to be copied into memory |
|---|
| 174 | n - number of bytes to be copied into memory |
|---|
| 175 | |
|---|
| 176 | Returns: |
|---|
| 177 | The value of mem |
|---|
| 178 | ****************************************************************************/ |
|---|
| 179 | void *BKNI_Memset(void *mem, int ch, size_t n); |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | /*************************************************************************** |
|---|
| 183 | Summary: |
|---|
| 184 | Copy non-overlapping memory. |
|---|
| 185 | |
|---|
| 186 | Description: |
|---|
| 187 | Copies n characters from the object pointed to by src into the object pointed |
|---|
| 188 | to by dest. |
|---|
| 189 | |
|---|
| 190 | If copying takes place between objects that overlap, the |
|---|
| 191 | behavior is undefined. Use BKNI_Memmove instead. |
|---|
| 192 | |
|---|
| 193 | Can be called from an interrupt-context. |
|---|
| 194 | |
|---|
| 195 | Input: |
|---|
| 196 | dest - the destination byte array |
|---|
| 197 | src - the source byte array |
|---|
| 198 | n - number of bytes to copy |
|---|
| 199 | |
|---|
| 200 | Returns: |
|---|
| 201 | The value of dest |
|---|
| 202 | ****************************************************************************/ |
|---|
| 203 | void *BKNI_Memcpy(void *dest, const void *src, size_t n); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | /*************************************************************************** |
|---|
| 207 | Summary: |
|---|
| 208 | Compare two blocks of memory. |
|---|
| 209 | |
|---|
| 210 | Description: |
|---|
| 211 | Compares the first n characters of the object pointed to by s1 to the first n |
|---|
| 212 | characters of the object pointed to by s2. |
|---|
| 213 | |
|---|
| 214 | Can be called from an interrupt-context. |
|---|
| 215 | |
|---|
| 216 | Returns: |
|---|
| 217 | An integer greater than, equal to, or less than zero, accordingly as the object |
|---|
| 218 | pointed to by s1 is greater than, equal to, or less than the object pointed to by s2. |
|---|
| 219 | ****************************************************************************/ |
|---|
| 220 | int BKNI_Memcmp( |
|---|
| 221 | const void *s1, /* byte array to be compared */ |
|---|
| 222 | const void *s2, /* byte array to be compared */ |
|---|
| 223 | size_t n /* maximum number of bytes to be compared */ |
|---|
| 224 | ); |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | /*************************************************************************** |
|---|
| 228 | Summary: |
|---|
| 229 | Find a byte in a block of memory. |
|---|
| 230 | |
|---|
| 231 | Description: |
|---|
| 232 | Locates the first occurrence of ch (converted to an unsigned char) in the initial n |
|---|
| 233 | characters (each interpreted as unsigned char) of the object pointed to by mem. |
|---|
| 234 | |
|---|
| 235 | Can be called from an interrupt-context. |
|---|
| 236 | |
|---|
| 237 | Input: |
|---|
| 238 | mem - byte array to be searched |
|---|
| 239 | ch - 8 bit value to be searched for |
|---|
| 240 | n - maximum number of bytes to be searched |
|---|
| 241 | |
|---|
| 242 | Returns: |
|---|
| 243 | A pointer to the located character, or a null pointer if the character does not |
|---|
| 244 | occur in the object. |
|---|
| 245 | ****************************************************************************/ |
|---|
| 246 | void *BKNI_Memchr(const void *mem, int ch, size_t n); |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | /*************************************************************************** |
|---|
| 250 | Summary: |
|---|
| 251 | Copy potentially overlapping memory. |
|---|
| 252 | |
|---|
| 253 | Description: |
|---|
| 254 | Copies n characters from the object pointed to by src into the object pointed |
|---|
| 255 | to by dest. Copying takes place as if the n characters from the object pointed |
|---|
| 256 | to by src are first copied into a temporary array of n characters that does |
|---|
| 257 | not overlap the objects pointed to by dest and src, and then the n characters |
|---|
| 258 | from the temporary array are copied into the object pointed to by dest. |
|---|
| 259 | |
|---|
| 260 | If the memory does not overlap, BKNI_Memcpy is preferred. |
|---|
| 261 | |
|---|
| 262 | Can be called from an interrupt-context. |
|---|
| 263 | |
|---|
| 264 | Returns: |
|---|
| 265 | The value of dest |
|---|
| 266 | ****************************************************************************/ |
|---|
| 267 | void *BKNI_Memmove( |
|---|
| 268 | void *dest, /* destination byte array */ |
|---|
| 269 | const void *src, /* source byte array */ |
|---|
| 270 | size_t n /* number of bytes to copy */ |
|---|
| 271 | ); |
|---|
| 272 | |
|---|
| 273 | #ifndef _BKNI_PRINT_H_ |
|---|
| 274 | /*************************************************************************** |
|---|
| 275 | Summary: |
|---|
| 276 | Print characters to the console. |
|---|
| 277 | |
|---|
| 278 | Description: |
|---|
| 279 | Although printing to the console is very important for development, it cannot |
|---|
| 280 | and should not be guaranteed to actually print in all contexts. |
|---|
| 281 | It is valid for the system developer to eliminate all BKNI_Printf output in |
|---|
| 282 | release builds or if the context demands it (e.g. interrupt context). |
|---|
| 283 | |
|---|
| 284 | You should use BKNI_Printf instead of |
|---|
| 285 | DebugInterface when you explicity want to print information to a console |
|---|
| 286 | regardless of debug state (e.g. BXPT_PrintStatus, BPSI_PrintPsiInformation). |
|---|
| 287 | BKNI_Printf is also used by the generic DebugInterface implementation. |
|---|
| 288 | |
|---|
| 289 | We only guarantee a subset of ANSI C format specifiers. These include: |
|---|
| 290 | |
|---|
| 291 | * %d - int in decimal form |
|---|
| 292 | * %u - unsigned int in decimal form |
|---|
| 293 | * %ld - long in decimal form |
|---|
| 294 | * %lu - unsigned long in decimal form |
|---|
| 295 | * %x - unsigned int in lowercase hex form |
|---|
| 296 | * %lx - unsigned long in lowercase hex form |
|---|
| 297 | * %X - unsigned int in uppercase hex form |
|---|
| 298 | * %lX - unsigned long in uppercase hex form |
|---|
| 299 | * %c - an int argument converted to unsigned char |
|---|
| 300 | * %s - string |
|---|
| 301 | * \n - newline |
|---|
| 302 | * \t - tab |
|---|
| 303 | * %% - % character |
|---|
| 304 | * %03d - Zero padding of integers, where '3' and 'd' are only examples. This can be applied to any of the preceding numeric format specifiers (not %c or %s). |
|---|
| 305 | * Pass-through of non-control characters. |
|---|
| 306 | |
|---|
| 307 | Beyond these, we do not guarantee the output format. |
|---|
| 308 | |
|---|
| 309 | For BKNI_Printf and BKNI_Vprintf, other ANSI C format specifiers |
|---|
| 310 | may be used, and platforms should try to make sure that any combination of formats |
|---|
| 311 | and parameters will not cause a system crash. |
|---|
| 312 | |
|---|
| 313 | When calling BKNI_Snprint and BKNI_Vsnprintf, Magnum code must only use the |
|---|
| 314 | guaranteed format specifiers if the results must always be the same on all platforms. |
|---|
| 315 | |
|---|
| 316 | BKNI_Printf can be called from an interrupt-context. |
|---|
| 317 | |
|---|
| 318 | Returns: |
|---|
| 319 | >=0 is success. It is the number of characters transmitted. |
|---|
| 320 | <0 is failure, either in encoding or in outputing. |
|---|
| 321 | ****************************************************************************/ |
|---|
| 322 | int BKNI_Printf( |
|---|
| 323 | const char *fmt, /* format string */ |
|---|
| 324 | ... /* variable arguments */ |
|---|
| 325 | ); |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | /*************************************************************************** |
|---|
| 330 | Summary: |
|---|
| 331 | Print characters to the console using a variable argument list. |
|---|
| 332 | |
|---|
| 333 | Description: |
|---|
| 334 | Equivalent to BKNI_Printf, with the variable argument list replaced by the va_list |
|---|
| 335 | parameter. va_list must initialized by the va_start macro (and possibly |
|---|
| 336 | subsequent va_arg calls). The BKNI_Vprintf function does not invoke the va_end macro. |
|---|
| 337 | |
|---|
| 338 | The value of the va_list parameter may be modified and so it is indeterminate upon return. |
|---|
| 339 | |
|---|
| 340 | See BKNI_Printf for a description of the format specifiers supported. |
|---|
| 341 | |
|---|
| 342 | Can be called from an interrupt-context. |
|---|
| 343 | |
|---|
| 344 | Input: |
|---|
| 345 | fmt - See BKNI_Printf |
|---|
| 346 | va_list - See StandardTypes and stdarg.h |
|---|
| 347 | |
|---|
| 348 | Returns: |
|---|
| 349 | >=0 is success. It is the number of characters transmitted. |
|---|
| 350 | <0 is failure, either in encoding or in outputing. |
|---|
| 351 | ****************************************************************************/ |
|---|
| 352 | int BKNI_Vprintf(const char *fmt, va_list ap); |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | /*************************************************************************** |
|---|
| 356 | Summary: |
|---|
| 357 | Print characters to a null-terminated string using a variable argument list. |
|---|
| 358 | |
|---|
| 359 | Description: |
|---|
| 360 | See BKNI_Printf for a description of the format specifiers supported. |
|---|
| 361 | See BKNI_Vprintf for a description of the va_list parameter. |
|---|
| 362 | |
|---|
| 363 | Can be called from an interrupt-context. |
|---|
| 364 | |
|---|
| 365 | Input: |
|---|
| 366 | s - memory to print into |
|---|
| 367 | n - size of memory that can be used. It should include space for the trailing null byte. |
|---|
| 368 | fmt - See BKNI_Printf |
|---|
| 369 | va_list - See StandardTypes and stdarg.h |
|---|
| 370 | |
|---|
| 371 | Returns: |
|---|
| 372 | If the output is not truncated, it returns the number of characters printed, not |
|---|
| 373 | including the trailing null byte. |
|---|
| 374 | |
|---|
| 375 | If the output is truncated, it should try to return the number |
|---|
| 376 | of characters that would have been printed had the size of memory been large |
|---|
| 377 | enough. However, this result is not required and no Magnum code should |
|---|
| 378 | depend on this result. |
|---|
| 379 | ****************************************************************************/ |
|---|
| 380 | int BKNI_Vsnprintf(char *s, size_t n, const char *fmt, va_list ap); |
|---|
| 381 | #else /* _BKNI_PRINT_H_ */ |
|---|
| 382 | #define BKNI_Printf BKNI_Print_Printf |
|---|
| 383 | #define BKNI_Vprintf BKNI_Print_Vprintf |
|---|
| 384 | #endif /* _BKNI_PRINT_H_ */ |
|---|
| 385 | |
|---|
| 386 | /*************************************************************************** |
|---|
| 387 | Summary: |
|---|
| 388 | Print characters to a null-terminated string. |
|---|
| 389 | |
|---|
| 390 | Description: |
|---|
| 391 | See BKNI_Printf for a description of the format specifiers supported. |
|---|
| 392 | |
|---|
| 393 | Can be called from an interrupt-context. |
|---|
| 394 | |
|---|
| 395 | Returns: |
|---|
| 396 | If the output is not truncated, it returns the number of characters printed, not |
|---|
| 397 | including the trailing null byte. |
|---|
| 398 | |
|---|
| 399 | If the output is truncated, it should try to return the number |
|---|
| 400 | of characters that would have been printed had the size of memory been large |
|---|
| 401 | enough. However, this result is not required and no Magnum code should |
|---|
| 402 | depend on this result. |
|---|
| 403 | ****************************************************************************/ |
|---|
| 404 | int BKNI_Snprintf( |
|---|
| 405 | char *s, /* destination string */ |
|---|
| 406 | size_t n, /* size of memory that can be used. It should include |
|---|
| 407 | space for the trailing null byte. */ |
|---|
| 408 | const char *fmt, /* format string */ |
|---|
| 409 | ... /* variable arguments */ |
|---|
| 410 | ); |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | /*************************************************************************** |
|---|
| 414 | Summary: |
|---|
| 415 | Busy sleep. |
|---|
| 416 | |
|---|
| 417 | Description: |
|---|
| 418 | BKNI_Delay is a busy sleep which guarantees you will delay for at least the specified |
|---|
| 419 | number of microseconds. It does not call the scheduler, therefore the Delay is able to be |
|---|
| 420 | less than the system clock time. This consumes CPU time, so it should be used for only |
|---|
| 421 | short sleeps and only when BKNI_Sleep cannot be used. |
|---|
| 422 | |
|---|
| 423 | Be aware that on a preemptive system, any task can be interrupted and the scheduler can |
|---|
| 424 | run, and so there is no guarantee of maximum delay time. If you have maximum time |
|---|
| 425 | constraints, you should be using an interrupt. |
|---|
| 426 | |
|---|
| 427 | Can be called from an interrupt-context. |
|---|
| 428 | |
|---|
| 429 | Input: |
|---|
| 430 | microsec - minimum number of microseconds to delay |
|---|
| 431 | |
|---|
| 432 | Returns: |
|---|
| 433 | <none> |
|---|
| 434 | ****************************************************************************/ |
|---|
| 435 | void BKNI_Delay(unsigned int microsec); |
|---|
| 436 | |
|---|
| 437 | /** |
|---|
| 438 | BKNI_TRACK_MALLOCS requires implementation of BKNI_GetMallocEntryInfo,etc. instead use tagged malloc |
|---|
| 439 | **/ |
|---|
| 440 | #define BKNI_TRACK_MALLOCS 0 |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | /*************************************************************************** |
|---|
| 444 | Summary: |
|---|
| 445 | Allocate system memory. |
|---|
| 446 | |
|---|
| 447 | Description: |
|---|
| 448 | Allocates space for an object whose size is specified by size and whose |
|---|
| 449 | value is indeterminate. |
|---|
| 450 | |
|---|
| 451 | System memory is usually managed by an operating system. It differs |
|---|
| 452 | from memory managed by a MemoryManager in that it is not |
|---|
| 453 | guaranteed to be physically continuous and you cannot request alignment. |
|---|
| 454 | |
|---|
| 455 | Passing a size of 0 is not allowed and leads to undefined behavior. |
|---|
| 456 | |
|---|
| 457 | The caller is responsible to also call BKNI_Free to free the memory |
|---|
| 458 | when done. Memory that is not explicitly freed |
|---|
| 459 | may or may not remain allocated beyond the life-cycle of a particular |
|---|
| 460 | application. |
|---|
| 461 | |
|---|
| 462 | Returns: |
|---|
| 463 | NULL - Memory could not be allocated |
|---|
| 464 | Non-NULL - Memory was allocated |
|---|
| 465 | ****************************************************************************/ |
|---|
| 466 | #if BDBG_DEBUG_BUILD |
|---|
| 467 | #define BKNI_Malloc(size) BKNI_Malloc_tagged(size, __FILE__, __LINE__) |
|---|
| 468 | |
|---|
| 469 | void *BKNI_Malloc_tagged( |
|---|
| 470 | size_t size, |
|---|
| 471 | const char *file, |
|---|
| 472 | int line |
|---|
| 473 | ); |
|---|
| 474 | #else |
|---|
| 475 | void *BKNI_Malloc( |
|---|
| 476 | size_t size /* Number of bytes to allocate */ |
|---|
| 477 | ); |
|---|
| 478 | #endif |
|---|
| 479 | |
|---|
| 480 | /*************************************************************************** |
|---|
| 481 | Summary: |
|---|
| 482 | Dellocate system memory. |
|---|
| 483 | |
|---|
| 484 | Description: |
|---|
| 485 | Causes the memory pointed to by mem to be deallocated, that is, made available for |
|---|
| 486 | further allocation. |
|---|
| 487 | |
|---|
| 488 | The following scenarios are not allowed and lead to undefined behavior: |
|---|
| 489 | |
|---|
| 490 | * Passing a pointer which was not returned by an earlier BKNI_Malloc call |
|---|
| 491 | * Passing a pointer which was already freed |
|---|
| 492 | * Passing NULL |
|---|
| 493 | |
|---|
| 494 | Returns: |
|---|
| 495 | <none> |
|---|
| 496 | ****************************************************************************/ |
|---|
| 497 | #if BDBG_DEBUG_BUILD |
|---|
| 498 | #define BKNI_Free(mem) BKNI_Free_tagged(mem, __FILE__, __LINE__) |
|---|
| 499 | |
|---|
| 500 | void BKNI_Free_tagged( |
|---|
| 501 | void *mem, /* Pointer to memory allocated by BKNI_Malloc */ |
|---|
| 502 | const char *file, |
|---|
| 503 | int line |
|---|
| 504 | ); |
|---|
| 505 | #else |
|---|
| 506 | void BKNI_Free( |
|---|
| 507 | void *mem /* Pointer to memory allocated by BKNI_Malloc */ |
|---|
| 508 | ); |
|---|
| 509 | #endif |
|---|
| 510 | |
|---|
| 511 | #if BDBG_DEBUG_BUILD |
|---|
| 512 | /*************************************************************************** |
|---|
| 513 | Summary: |
|---|
| 514 | Print all current mallocs |
|---|
| 515 | ****************************************************************************/ |
|---|
| 516 | void BKNI_DumpMallocs(void); |
|---|
| 517 | #endif |
|---|
| 518 | |
|---|
| 519 | /*************************************************************************** |
|---|
| 520 | Summary: |
|---|
| 521 | Yield the current task to the scheduler. |
|---|
| 522 | |
|---|
| 523 | Description: |
|---|
| 524 | BKNI_Sleep is a sheduler sleep which guarantees you will delay for at least the |
|---|
| 525 | specified number of milliseconds. It puts the process to sleep and allows the scheduler |
|---|
| 526 | to run. The minimum sleep time is dependent on the system clock time. If you need |
|---|
| 527 | a minimum time which is less that the system clock time, you'll need to use BKNI_Delay. |
|---|
| 528 | |
|---|
| 529 | Actual sleep time is dependent on the scheduler but will be at least as long as |
|---|
| 530 | the value specified by the millisec parameter. |
|---|
| 531 | |
|---|
| 532 | A sleep value of 0 should cause the scheduler to execute. This may or may not result in |
|---|
| 533 | any delay. |
|---|
| 534 | |
|---|
| 535 | BKNI_Sleep cannot be called from an interrupt context. Use BKNI_Delay instead. |
|---|
| 536 | |
|---|
| 537 | Returns: |
|---|
| 538 | BERR_SUCCESS - The system slept for at least the specified number of milliseconds. |
|---|
| 539 | BERR_OS_ERROR - The sleep was interrupted before the specified time. |
|---|
| 540 | ****************************************************************************/ |
|---|
| 541 | BERR_Code BKNI_Sleep( |
|---|
| 542 | unsigned int millisec /* minimum number of milliseconds to sleep */ |
|---|
| 543 | ); |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | /*************************************************************************** |
|---|
| 547 | Summary: |
|---|
| 548 | Create an event used by one task to signal another task. |
|---|
| 549 | |
|---|
| 550 | Description: |
|---|
| 551 | Note that there is no 'name' parameter in BKNI_CreateEvent. We do not support named |
|---|
| 552 | events because they are essentially global variables that can lead to unwanted behavior. |
|---|
| 553 | Passing in a name for debugging purposes might lead to someone to think we |
|---|
| 554 | support named events. |
|---|
| 555 | |
|---|
| 556 | The event is created in an unsignalled state. |
|---|
| 557 | |
|---|
| 558 | Input: |
|---|
| 559 | event - point to an event handle which will be initialized. |
|---|
| 560 | |
|---|
| 561 | Returns: |
|---|
| 562 | BERR_SUCCESS - The event is allocated and initialized. |
|---|
| 563 | BERR_OS_ERROR - The event could not be allocated or initialized. |
|---|
| 564 | ****************************************************************************/ |
|---|
| 565 | BERR_Code BKNI_CreateEvent(BKNI_EventHandle *event); |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | /*************************************************************************** |
|---|
| 569 | Summary: |
|---|
| 570 | Destroy an event. |
|---|
| 571 | |
|---|
| 572 | Description: |
|---|
| 573 | If any signal is pending, it is lost. |
|---|
| 574 | |
|---|
| 575 | Input: |
|---|
| 576 | event - event initialized by BKNI_CreateEvent |
|---|
| 577 | ****************************************************************************/ |
|---|
| 578 | void BKNI_DestroyEvent(BKNI_EventHandle event); |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | /*************************************************************************** |
|---|
| 582 | Summary: |
|---|
| 583 | Wait until an event is signalled by BKNI_SetEvent. |
|---|
| 584 | |
|---|
| 585 | Description: |
|---|
| 586 | Suspends execution of the current task until the event specified by the event parameter |
|---|
| 587 | becomes signalled. The task should be blocked by the scheduler, resulting in no wasted |
|---|
| 588 | CPU time. |
|---|
| 589 | |
|---|
| 590 | This function fails if the event does not become signalled within |
|---|
| 591 | the period specified by the timeoutMsec parameter. A timeout of zero instructs this |
|---|
| 592 | function to fail immediately if the event is not signalled. A timeout value of |
|---|
| 593 | BKNI_INFINTE (defined in bkni_multi.h) causes this function not to return until successful. |
|---|
| 594 | |
|---|
| 595 | BKNI_WaitForEvent can be called from a single-task module only if that module follows |
|---|
| 596 | the rules specified under PortingInterface interrupt handling. |
|---|
| 597 | For this reason, BKNI_INFINITE is defined in bkni_multi.h. An event |
|---|
| 598 | that blocks for a less-than-infinite time can be considered as an interruptible |
|---|
| 599 | sleep. See PortingInterface for a description of interrupt handling in a single-threaded |
|---|
| 600 | module. |
|---|
| 601 | |
|---|
| 602 | A single BKNI_WaitForEvent call will consume all pending signals. |
|---|
| 603 | |
|---|
| 604 | However, if multiple tasks call BKNI_WaitForEvent for the same event, it is only |
|---|
| 605 | guaranteed that at least one will be woken up with the next BKNI_SetEvent call. Some platforms |
|---|
| 606 | may wake up one, others may wake up all. To avoid problems, Magnum code |
|---|
| 607 | should only wait on a particular event from a single task. |
|---|
| 608 | |
|---|
| 609 | BKNI_WaitForEvent can be called from an interrupt context, but only if timeoutMsec |
|---|
| 610 | is 0. In this case, it checks if the event has been set, and does not block if it |
|---|
| 611 | has not. Calling BKNI_WaitForEvent from an interrupt context with a non-zero timeout |
|---|
| 612 | leads to undefined behavior. |
|---|
| 613 | |
|---|
| 614 | Returns: |
|---|
| 615 | BERR_SUCCESS - An event happened and was consumed. |
|---|
| 616 | BERR_TIMEOUT - Either the timeout was 0 and no event was already pending, or the timeout expired before an event happened. |
|---|
| 617 | BERR_OS_ERROR - The system interruped the wait and an event did not happen. |
|---|
| 618 | ****************************************************************************/ |
|---|
| 619 | BERR_Code BKNI_WaitForEvent(BKNI_EventHandle event, int timeoutMsec); |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | /*************************************************************************** |
|---|
| 623 | Summary: |
|---|
| 624 | Signal an event which causes BKNI_WaitForEvent to return. |
|---|
| 625 | |
|---|
| 626 | Description: |
|---|
| 627 | Causes the event specified by the event parameter to become signalled. At least one |
|---|
| 628 | task waiting on the event becomes unblocked. |
|---|
| 629 | |
|---|
| 630 | If no task is waiting on this event, the signal is remembered and the next call |
|---|
| 631 | to BKNI_WaitForEvent will succeed immediately. |
|---|
| 632 | |
|---|
| 633 | BKNI_SetEvent may be called from an interrupt context. |
|---|
| 634 | |
|---|
| 635 | See Also: |
|---|
| 636 | See BKNI_WaitForEvent for more details and usage rules. |
|---|
| 637 | ****************************************************************************/ |
|---|
| 638 | void BKNI_SetEvent(BKNI_EventHandle event); |
|---|
| 639 | |
|---|
| 640 | |
|---|
| 641 | /*************************************************************************** |
|---|
| 642 | Summary: |
|---|
| 643 | Reset an event so that there are no signals pending. |
|---|
| 644 | |
|---|
| 645 | Description: |
|---|
| 646 | In certain cases it is necessary to reset an event in order to avoid a race condition. |
|---|
| 647 | Consider the following code: |
|---|
| 648 | |
|---|
| 649 | <verbatim> |
|---|
| 650 | foo() { |
|---|
| 651 | SetHardware(); |
|---|
| 652 | err = BKNI_WaitForEvent(event, timeout); |
|---|
| 653 | if (err) |
|---|
| 654 | return err; |
|---|
| 655 | return AcquireData(); |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | interrupt() { |
|---|
| 659 | BKNI_SetEvent(event); |
|---|
| 660 | } |
|---|
| 661 | </verbatim> |
|---|
| 662 | |
|---|
| 663 | And now consider the following execution sequence: |
|---|
| 664 | |
|---|
| 665 | <verbatim> |
|---|
| 666 | foo() called |
|---|
| 667 | setHardware() called |
|---|
| 668 | BKNI_WaitForEvent() called |
|---|
| 669 | BKNI_WaitForEvent times out and foo returns an error. |
|---|
| 670 | |
|---|
| 671 | Interrupt happens and BKNI_SetEvent called |
|---|
| 672 | |
|---|
| 673 | foo() called again |
|---|
| 674 | setHardware() called |
|---|
| 675 | BKNI_WaitForEvent() called |
|---|
| 676 | --> BKNI_WaitForEvent returns BERR_SUCCESS from the old interrupt |
|---|
| 677 | AcquireData() is called before it should be. |
|---|
| 678 | </verbatim> |
|---|
| 679 | |
|---|
| 680 | In order to avoid this problem, BKNI_ResetEvent must be called before setting or |
|---|
| 681 | resetting hardware. Be careful that there is not a race condition between |
|---|
| 682 | BKNI_ResetEvent and the actual resetting of hardware. If you cannot disable |
|---|
| 683 | the particular interrupt, you need to use a critical section. |
|---|
| 684 | |
|---|
| 685 | <verbatim> |
|---|
| 686 | foo() { |
|---|
| 687 | BKNI_EnterCriticalSection(); |
|---|
| 688 | BKNI_ResetEvent(event); |
|---|
| 689 | SetHardware(); |
|---|
| 690 | BKNI_LeaveCriticalSection(); |
|---|
| 691 | err = BKNI_WaitForEvent(event, timeout); |
|---|
| 692 | if (err) |
|---|
| 693 | return err; |
|---|
| 694 | return AcquireData(); |
|---|
| 695 | } |
|---|
| 696 | </verbatim> |
|---|
| 697 | |
|---|
| 698 | A simple implementation of BKNI_ResetEvent is to call BKNI_WaitForEvent with a timeout |
|---|
| 699 | of 0. For some platforms, there may be a more optimal implementation. |
|---|
| 700 | |
|---|
| 701 | Returns: |
|---|
| 702 | <none> |
|---|
| 703 | ****************************************************************************/ |
|---|
| 704 | void BKNI_ResetEvent(BKNI_EventHandle event); |
|---|
| 705 | |
|---|
| 706 | |
|---|
| 707 | /*************************************************************************** |
|---|
| 708 | Summary: |
|---|
| 709 | Cause the application or system to abort immediately if possible. |
|---|
| 710 | On platforms that support it, the system state should be captured. |
|---|
| 711 | |
|---|
| 712 | Description: |
|---|
| 713 | This is called from a failed BDBG_ASSERT() (see DebugInterface). |
|---|
| 714 | |
|---|
| 715 | Can be called from an interrupt-context. |
|---|
| 716 | |
|---|
| 717 | There is no required behavior for this function. It can be completely |
|---|
| 718 | empty. There is no need to print an error message from inside BKNI_Fail |
|---|
| 719 | because the DebugInterface will have printed something before calling |
|---|
| 720 | it. |
|---|
| 721 | |
|---|
| 722 | See Also: |
|---|
| 723 | DebugInterface, BDBG_ASSERT |
|---|
| 724 | ****************************************************************************/ |
|---|
| 725 | void BKNI_Fail(void); |
|---|
| 726 | |
|---|
| 727 | |
|---|
| 728 | /*************************************************************************** |
|---|
| 729 | Summary: |
|---|
| 730 | Create a Magnum critical section which protects against concurrent execution by |
|---|
| 731 | another Magnum critical section or Magnum ISR (interrupt service routine). |
|---|
| 732 | |
|---|
| 733 | Description: |
|---|
| 734 | A Magnum critical section is defined as a block of code between a BKNI_EnterCriticalSection |
|---|
| 735 | call and a BKNI_LeaveCriticalSection call or any code inside a Magnum ISR (distinguished with an _isr suffix). |
|---|
| 736 | Be aware that a Magnum critical section may not mean the same thing as an operating system's critical |
|---|
| 737 | section. Typically, operating system critical sections mean that interrupts are disabled and |
|---|
| 738 | no context switch is possible. |
|---|
| 739 | A Magnum critical section may or may not mean this, depending on the implementation of KNI. |
|---|
| 740 | |
|---|
| 741 | Magnum critical sections cannot be preempted by other Magnum critical sections. |
|---|
| 742 | This includes both interrupts and context switching. But it only applies to Magnum code, not |
|---|
| 743 | to code outside of Magnum. While there are many ways these rules can be implemented in a system, the recommended |
|---|
| 744 | approach is as following: |
|---|
| 745 | * If your system executes Magnum-isr code in interrupt context, Magnum critical sections must be implemented by disabling interrupts. |
|---|
| 746 | * If your system executes Magnum-isr code in task context, Magnum critical sections must be implemented with a global mutex. |
|---|
| 747 | |
|---|
| 748 | Be aware that on task-context only systems, critical sections may not prevent the scheduler |
|---|
| 749 | from time-slicing and executing non-critical section code concurrently with the |
|---|
| 750 | critical section. |
|---|
| 751 | |
|---|
| 752 | Critical sections are also used to protect concurrent access to a register shared |
|---|
| 753 | with another module. Ideally, there are no registers shared between disconnected |
|---|
| 754 | software modules, but sometimes this is unavoidable. |
|---|
| 755 | |
|---|
| 756 | Magnum code cannot nest critical sections. Calling BKNI_EnterCriticalSection from |
|---|
| 757 | inside a critical section is not allowed and leads to undefined behavior. |
|---|
| 758 | Possible results include deadlock or undetected race conditions. |
|---|
| 759 | |
|---|
| 760 | A platform implementation might chose to allow BKNI_EnterCriticalSection to nest |
|---|
| 761 | because of platform-specific considerations. |
|---|
| 762 | |
|---|
| 763 | See Also: |
|---|
| 764 | BKNI_LeaveCriticalSection, Magnum InterruptSafe rules |
|---|
| 765 | ****************************************************************************/ |
|---|
| 766 | void BKNI_EnterCriticalSection(void); |
|---|
| 767 | |
|---|
| 768 | |
|---|
| 769 | /*************************************************************************** |
|---|
| 770 | Summary: |
|---|
| 771 | Leave a critical section. |
|---|
| 772 | |
|---|
| 773 | Description: |
|---|
| 774 | Calling BKNI_LeaveCriticalSection when you are not in a critical section |
|---|
| 775 | is not allowed and leads to undefined behavior. |
|---|
| 776 | |
|---|
| 777 | See Also: |
|---|
| 778 | BKNI_EnterCriticalSection, Magnum InterruptSafe rules |
|---|
| 779 | ****************************************************************************/ |
|---|
| 780 | void BKNI_LeaveCriticalSection(void); |
|---|
| 781 | |
|---|
| 782 | /*************************************************************************** |
|---|
| 783 | Summary: |
|---|
| 784 | Verifies a critical section. |
|---|
| 785 | |
|---|
| 786 | Description: |
|---|
| 787 | Assert that code is running in interrupt context, either from an ISR |
|---|
| 788 | or inside critical section |
|---|
| 789 | |
|---|
| 790 | See Also: |
|---|
| 791 | BKNI_EnterCriticalSection, Magnum InterruptSafe rules |
|---|
| 792 | ****************************************************************************/ |
|---|
| 793 | #ifdef BDBG_DEBUG_BUILD |
|---|
| 794 | #define BKNI_ASSERT_ISR_CONTEXT() BKNI_AssertIsrContext(__FILE__, __LINE__) |
|---|
| 795 | #else |
|---|
| 796 | #define BKNI_ASSERT_ISR_CONTEXT() (void)0 |
|---|
| 797 | #endif |
|---|
| 798 | void BKNI_AssertIsrContext(const char *filename, unsigned lineno); |
|---|
| 799 | |
|---|
| 800 | |
|---|
| 801 | /* lines below provides aliases for functions safe to call from the interrupt handler */ |
|---|
| 802 | #define BKNI_Memset_isr BKNI_Memset |
|---|
| 803 | #define BKNI_Memcpy_isr BKNI_Memcpy |
|---|
| 804 | #define BKNI_Memcmp_isr BKNI_Memcmp |
|---|
| 805 | #define BKNI_Memchr_isr BKNI_Memchr |
|---|
| 806 | #define BKNI_Memmove_isr BKNI_Memmove |
|---|
| 807 | #define BKNI_Delay_isr BKNI_Delay |
|---|
| 808 | #define BKNI_SetEvent_isr BKNI_SetEvent |
|---|
| 809 | |
|---|
| 810 | #ifdef __cplusplus |
|---|
| 811 | } |
|---|
| 812 | #endif |
|---|
| 813 | |
|---|
| 814 | #endif /* BKNI_H__ */ |
|---|