1 FreeRTOS Port for Xtensa Configurable and Diamond Processors 2 ============================================================ 3 4 FreeRTOS Version 8.2.0 5 6 7Introduction 8------------ 9 10This document describes the Xtensa port for FreeRTOS multitasking RTOS. 11For an introduction to FreeRTOS itself, please refer to FreeRTOS 12documentation. 13 14This port currently works with version 8.2.0. 15 16 17Xtensa Configuration Requirements and Restrictions 18-------------------------------------------------- 19 20The Xtensa configurable architecture supports a vast space of processor 21features. This port supports all of them, including custom processor 22extensions defined in the TIE language, with certain minimum 23requirements. You must use Xtensa Tools to compile and link FreeRTOS and 24your application for your Xtensa configuration. The port uses the Xtensa 25Hardware Abstraction Layer (HAL) to adapt to your Xtensa configuration. 26NOTE: It may be possible to build and run this with the open-source 27xtensa-linux tools provided you have the correct overlay for your Xtensa 28configuration. However, this has not been tested and is currently not 29supported by Cadence. 30 31This port includes optional reentrancy support for the 'newlib' C runtime 32library that is distributed with Xtensa Tools, providing thread-safety on 33a per task basis (for use in tasks only, not interrupt handlers). 34 35NOTE: At this time only the 'newlib' C library is supported for thread 36safety. The 'xclib' and 'uclibc' libraries are not reentrant and do not 37provide thread safety at this time. However, if you are not concerned 38with reentrancy then you can use any of these libraries. 39 40This port also includes a simple example application that may run on 41a supported board or the Xtensa instruction set simulator (ISS). There 42are also a couple of test programs used in maintaining the port, which 43serve as additional examples. 44 45FreeRTOS for Xtensa configurable processors requires the following minimum 46processor configuration options: 47- Timer interrupt option with at least one interruptible timer. 48- Interrupt option (implied by the timer interrupt option). 49- Exception Architecture 2 (XEA2). Please note that XEA1 is NOT supported. 50 All 'Diamond', 'Xtensa 6', 'Xtensa LX' and 'Xtensa LX2' processors and 51 most 'Xtensa T1050' processors are configured with XEA2. 52All Diamond processor cores meet these requirements and are supported. 53 54Minimal support for certain evaluation boards is provided via a board 55independent XTBSP API implemented by a board specific library distributed 56with the Xtensa Tools. This provides the board clock frequency and basic 57polled drivers for the display and console device. Note that XTBSP 58is not a tradtional RTOS "board support package" with RTOS specific 59interrupt-driven drivers - it is not specific to any RTOS. Note that 60FreeRTOS can run on any Xtensa or Diamond board without this board support 61(a "raw" platform), but you will have to provide the clock frequency 62and drivers for any on-board devices you want to use. 63 64 65Installation 66------------ 67 68This port is downloaded in a ZIP file from FreeRTOS Web site. You will 69also need to obtain the common FreeRTOS source package from FreeRTOS's 70website in order to build the OS and applications. The Xtensa port files 71are not currently included in the common package. 72 73All source is provided along with a Makefile that works for any host 74platform supported by Xtensa Tools (Windows, Linux). These instructions 75are written for Windows users, but can easily be understood and adapted 76to other host platforms. 77 78First install the FreeRTOS common package in a directory of your choosing. 79The structure of that package will look like this: 80 81FreeRTOS 82|-- Demo 83| |-- Common 84| | |-- Minimal 85| | `-- include 86| | 87| |-- Xtensa_XCC 88| `-- ParTest 89| 90`-- Source 91 |-- include 92 `-- portable 93 |-- MemMang 94 `-- XCC 95 `-- Xtensa 96 97Since the Xtensa specific files are currently unavailable in the common 98package, then you will need the separate zip file containing the Xtensa port 99files. Extract from that zip file the Source/portable/XCC and Demo/Xtensa_XCC 100folders and place them in the appropriate location under the common tree. 101 102The Xtensa Tools are available from Cadence as part of a processor 103license. Be sure you have installed the Xtensa Tools and your processor 104configuration. 105 106 107Building FreeRTOS for Xtensa 108---------------------------- 109 110The Makefiles are provided for your convenience and you do not need to 111use them. Essentially you need to compile all the FreeRTOS common files, 112the port files, and your application, and link them all. However all the 113build instructions in this note use the Makefiles. 114 115By default, you will build for the Xtensa instruction set simulator. If 116you have a supported emulation board, you can build to run on that. You 117can also build to run on a raw Xtensa core with no board support, a 118good starting point for supporting your own target platform. Cadence 119recommends doing functional development on the simulator because it 120is easier to debug with, then move to a board if/when you need to test 121hardware drivers or real-time performance. 122 123The provided Makefile simplifies building FreeRTOS and the example 124for your Xtensa configuration and platform (ISS, board, etc.). There 125are detailed instructions in the comment at the top of the Makefile. 126The test/Makefile provides similar support for building the tests, 127and may be invoked directly from the top level Makefile. 128 129The Makefiles work on any host platform and support incremental builds. 130The build for each Xtensa configuration and target platform is placed in 131a subdirectory so several core and platform builds can co-exist even with 132incremental rebuilds. You may specify the root of the build area (if tou 133want it to be elsewhere than under the source tree) by defining BLDROOT 134either in the make command or your shell environment. 135 136First, be sure you have installed Xtensa Tools and your processor 137configuration, and be sure that Xtensa Tools are in your search path. 138You can use xt-make, which comes with the Xtensa Tools, to run the 139makefiles. 140 141Change directories to the Xtensa port directory: 142 143> cd FreeRTOS/Source/portable/XCC/Xtensa 144 145Now build the FreeRTOS RTOS as a library (libfreertos.a) as follows: 146 147> xt-make 148 149which by default builds for the simulator (TARGET=sim), or: 150 151> xt-make TARGET=board 152 153which builds for a supported board. Note that the board type does not 154need to be specified when building the FreeRTOS library. 155 156If you are building for an Xtensa processor configuration that is not the 157default you selected when you installed Xtensa Tools, you need to define the 158environment variable XTENSA_CORE. If your configuration is not in the 159default registry you selected when you installed Xtensa Tools, you also 160need to define the environment variable XTENSA_SYSTEM. See tools manuals. 161You can avoid defining these in your environment if you pass the variables 162you need to redefine into xt-make as follows: 163 164> xt-make XTENSA_CORE=<your_config_name> XTENSA_SYSTEM=<your_registry> ... 165 166There are more details about build options in the comment in the Makefile. 167 168After the library has been built, you must link your application with this 169library in order to use FreeRTOS. 170 171 172Building a FreeRTOS Application 173------------------------------- 174 175The provided FreeRTOS example is designed to run on the Xtensa instruction 176set simulator (ISS) or a supported evaluation board programmed with your 177Xtensa processor configuration. 178 179To build the example for the default platform (simulator): 180 181> xt-make example 182 183which is the same as 184 185> xt-make example TARGET=sim 186 187The boards currently supported are the Xilinx ML605 and KC705 FPGA 188development boards. To target these boards, type 189 190> xt-make example TARGET=ml605 191 192or 193 194> xt-make example TARGET=kc705 195 196To build in a location other than the default, specify the new location 197using the BLDROOT variable. Note that this makefile will invoke the 198FreeRTOS library build makefile automatically, passing on the relevant 199parameters based on what you specified. 200 201You can override the default compilation options by specifying the new 202options via CFLAGS. For example: 203 204> xt-make example TARGET=sim CFLAGS="-O2 -Os -g" 205 206This compiles example.c and links it with the FreeRTOS library 207libfreertos.a and the appropriate linker-support package (LSP) for your 208target platform (you can override the LSP by adding LSP=<lsp> to the 209xt-make command line). The resulting file example.exe is an ELF binary 210file that can be downloaded and executed on the target. 211 212The example.exe binary appears in the platform specific subdirectory 213described earlier. For the following commands, change to that directory 214or prepend it as the path of example.exe. 215 216To build your application with thread-safe C library support using 217the open-source 'newlib' library provided with the Xtensa Tools, you 218need to make certain modifications to the application to plug in and 219invoke the newlib reentrancy support. This allows each task to use 220the library without interference with other tasks (it is not safe for 221interrupt handlers to call the C library). 222 223First, you must define 224 225 XT_USE_THREAD_SAFE_CLIB 226 227to a nonzero either in FreeRTOSConfig.h or in the compiler's command 228line. 229 230Then, you must also make sure to allocate extra space on the stack for 231each task that will use the C library reentrant functions. This extra 232space is to be allocated over and above the actual stack space required 233by the task itself. The define 234 235 XT_STACK_EXTRA_CLIB 236 237specifies the amount of extra space to be added on to the stack to allow 238saving the context for the C library as well as the coprocessors if any. 239E.g. if your task requires 2000 bytes of stack space, you must allocate 240(2000 + XT_STACK_EXTRA_CLIB) bytes for the stack. 241 242To build the example with thread-safe C library support: 243 244> xt-make CFLAGS="-O0 -DXT_USE_THREAD_SAFE_CLIB" example 245 246The "-O0" is necessary because you are overriding the default COPT=-O0. 247You can specify any optimization level you require (if none, the compiler 248defaults to -O2). 249 250 251Running or Debugging an Application 252----------------------------------- 253 254To execute the example application on the simulator: 255 256> xt-run [--turbo] example.exe 257 258The option --turbo provides much faster, but non-cycle-accurate simulation 259(the --turbo option is only available with Xtensa Tools version 7 or later). 260 261 262To execute on the simulator using the Xplorer GUI based debugger: 263 264> xplorer --debug example.exe 265 266 267To execute on a supported evaluation board, download example.exe per 268instructions in the tools manuals. Be sure the board has been programmed 269with the correct configuration and is set up to boot from RAM and debug 270a downloaded program! Optionally you may connect a terminal or terminal 271emulator to the serial port on the board with settings as described in 272the board user manual, and see the output of printf on the terminal. 273 274To obtain I/O on a "raw" platform such as an unsupported board, you need 275to provide low level I/O drivers (eg. inbyte() and outbyte() for character 276I/O if you want to use printf etc.). You can run "raw" executables on 277any Xtensa platform, including simulator and any board, but you will not 278see any behavior specific to the platform (eg. display, printed output, 279stopping simulation at end of program). You can, while debugging, use a 280debugger mechanism called GDBIO to obtain basic I/O. To use GDBIO, link 281with the gdbio LSP. Refer to Xtensa tools documentation for details. 282 283 284Task Stack Sizes 285---------------- 286 287The application must ensure that every task has enough space for its 288stack. Each task needs enough space for its own use, its own interrupt 289stack frame (defined in xtensa_context.h) and space to save coprocessor 290state, if any. Several factors influence the size of the stack required, 291including the compiler optimization level and the use of the C library. 292Calls to standard output functions such as printf() can use up a lot of 293stack space. The tool xt-stack-usage is helpful in determining safe stack 294sizes for your application. 295 296Some macros are provided in xtensa_config.h to help determine the stack 297size for tasks that do and do not use the C library. Use these as the 298basis for each task's stack size. They are minimum requirements taking 299into account your configuration and use of the C library. In particular, 300the define 301 302 XT_STACK_MIN_SIZE 303 304defines the minimum stack size for any task. Be very careful if you try 305to use a stack size smaller than this minimum. Stack overruns can cause 306all kinds of hard-to-debug errors. It is recommended that you enable the 307FreeRTOS stack checking features during development. 308 309WARNING: The newlib printf() function uses a lot of stack space. Be very 310careful in using it. Optionally you can use the 'libxtutil' library for 311output - it implements a subset of printf() that has smaller code size 312and uses far less stack space. More information about this library is in 313the Xtensa Tools documentation. 314 315 316Interrupt Stack 317--------------- 318 319Beginning with port version 1.2, the port uses a separate interrupt stack 320for handling interrupts. Thus, it is no longer necessary for each task to 321reserve space on its stack to handle interrupts. The size of the interrupt 322stack is controlled by the parameter "configISR_STACK_SIZE" defined in 323FreeRTOSConfig.h. Define this carefully to match your system requirements. 324 325 326Assembler / Compiler Switches 327----------------------------- 328 329The following are compiler switches are used by the provided 330Makefile in building the FreeRTOS library and example application. 331These can be modified by editing the Makefile or by overriding the 332CFLAGS variable in the make command line, for example: 333 334> xt-make CFLAGS="-O2 -DXT_USE_THREAD_SAFE_CLIB" 335 336 -g Specifies debug information. 337 -c Specifies object code generation. 338 -On Sets compiler optimization level n (default -O0). 339 -mlongcalls Allows assembler and linker to convert call 340 instructions to longer indirect call sequences 341 when target is out of range. 342 -x assembler-with-cpp Passes .s and .S files through C preprocessor. 343 -Dmacro Define a preprocessor macro with no value. 344 -Dmacro=value Define a preprocessor macro with a value. 345 346See the compiler / linker documentation for a full list of switches and 347their use. 348 349Many definitions can be provided at compile-time via the -D option 350without editing the source code. Here are some of the more useful ones: 351 352 XT_USE_THREAD_SAFE_CLIB Enable support for the reentrancy to provide 353 thread-safety in the GNU newlib supplied with 354 Xtensa Tools. Default off. 355 356 Note, the follwing defines are unique to the Xtensa port so have names 357 beginning with "XT_". 358 359 XT_SIMULATOR Set this if building to run on the simulator. 360 Takes advantage of certain simulator control 361 and reporting facilities, and adjusts timing 362 of periodic tick to provide a more acceptable 363 performance in simulation (see XT_CLOCK_FREQ). 364 Set by default unless PLATFORM is overridden. 365 366 XT_BOARD Set this if building for a supported board. 367 Be sure to specify the correct LSP for the 368 board. See the example makefile for usage. 369 370 XT_CLOCK_FREQ=freq Specifies the target processor's clock 371 frequency in Hz. Used primarily to set the 372 timer that generates the periodic interrupt. 373 Defaults are provided and may be edited in 374 xtensa_timer.h (see comments there also). 375 Default for simulator provides more acceptable 376 performance, but cannot provide real-time 377 performance due to variation in simulation 378 speed per host platform and insufficient 379 cycles between interrupts to process them. 380 Supported board platforms by default leave 381 this undefined and compute the clock frequency 382 at initialization unless this is explicitly 383 defined. 384 385 XT_TICK_PER_SEC=n Specifies the frequency of the periodic tick. 386 387 XT_TIMER_INDEX=n Specifies which timer to use for periodic tick. 388 Set this if your Xtensa processor configuration 389 provides more than one suitable timer and you 390 want to override the default. See xtensa_timer.h . 391 392 XT_INTEXC_HOOKS Enables hooks in interrupt vector handlers 393 to support dynamic installation of exception 394 and interrupt handlers. Disabled by default. 395 396 XT_USE_OVLY Enable code overlay support. It uses a mutex, 397 hence configUSE_MUTEX must be enabled. This 398 option is currently unsupported. 399 400 XT_USE_SWPRI Enable software prioritization of interrupts. 401 Enabling this will prioritize interrupts with 402 higher bit numbers over those with lower bit 403 numbers at the same level. This works only for 404 low and medium priority interrupts that can be 405 dispatched to C handlers. 406 407 408Register Usage and Stack Frames 409------------------------------- 410 411The Xtensa architecture specifies two ABIs that determine how the general 412purpose registers a0-a15 are used: the standard windowed ABI use with 413the Xtensa windowed register file architecture, and the optional and 414more conventional Call0 ABI (required for Xtensa configurations without 415a windowed register file). 416 417Xtensa processors may have other special registers (including co-processor 418registers and other TIE "states") that are independent of this choice 419of ABI. See Xtensa documentation for more details. 420 421In the windowed ABI the registers of the current window are used as follows: 422 a0 = return address 423 a1 = stack pointer (alias sp) 424 a2 = first argument and result of call (in simple cases) 425 a3-7 = second through sixth arguments of call (in simple cases). 426 Note that complex or large arguments are passed on the 427 stack. Details are in the Xtensa Tools manuals. 428 a8-a15 = available for use as temporaries. 429There are no callee-save registers. The windowed hardware automatically 430saves registers a0-a3 on a call4, a0-a8 on a call8, a0-a12 on a call12, 431by rotating the register window. Hardware triggers window overflow and 432underflow exceptions as necessary when registers outside the current 433window need to be spilled to preallocated space in the stack frame, or 434restored. Complete details are in the Xtensa manuals. The entire windowed 435register file is saved and restored on interrupt or task context switch. 436 437The Call0 ABI does not make use of register windows, relying instead 438on a fixed set of 16 registers without window rotation. 439The Call0 ABI is more conventional and uses registers as follows: 440 a0 = return address 441 a1 = stack pointer (alias sp) 442 a2 = first argument and result of call (in simple cases) 443 a3-7 = second through sixth arguments of call (in simple cases). 444 Note that complex or large arguments are passed on the 445 stack. Details are in the Xtensa Tools manuals. 446 a8-a11 = scratch. 447 a12-a15 = callee-save (a function must preserve these for its caller). 448On a FreeRTOS API call, callee-save registers are saved only when a task 449context switch occurs, and other registers are not saved at all (the caller 450does not expect them to be preserved). On an interrupt, callee-saved 451registers might only be saved and restored when a task context-switch 452occurs, but all other registers are always saved and restored. 453 454An Xtensa processor has other special registers independent of the ABI, 455depending on the configuration (including co-processor registers and other 456TIE state) that are part of the task context. FreeRTOS preserves all such 457registers over an unsolicited context-switch triggered by an interrupt. 458However it does NOT preserve these over a solicited context-switch during 459a FreeRTOS API call. This bears some explanation. These special registers 460are either ignored by the compiler or treated as caller-saved, meaning 461that if kept "live" over a function call (ie. need to be preserved) 462they must be saved and restored by the caller. Since solicited entry to 463FreeRTOS is always made by a function call, FreeRTOS assumes the caller 464has saved any of these registers that are "live". FreeRTOS avoids a lot 465of overhead by not having to save and restore every special register 466(there can be many) on every solicited context switch. 467 468As a consequence, the application developer should NOT assume that special 469registers are preserved over a FreeRTOS API call such as vTaskDelay(). 470If multiple tasks use a register, the caller must save and restore it. 471 472The saved context stack frames for context switches that occur as 473a result of interrupt handling (interrupt frame) or from task-level 474API calls (solicited frame) are described in human readable form in 475xtensa_context.h . All suspended tasks have one of these two types 476of stack frames. The top of the suspended task's stack is pointed to 477by pxCurrentTCB->pxTopOfStack. A special location common to both stack 478frames differentiates solicited and interrupt stack frames. 479 480 481Improving Performance, Footprint, or Ease of Debugging 482------------------------------------------------------ 483 484By default FreeRTOS for Xtensa is built with debug (-g) and without 485compiler optimizations (-O0). This makes debugging easier. Of course, 486-O0 costs performance and usually also increases stack usage. To make 487FreeRTOS run faster you can change the Makefile to enable the desired 488optimizations or set a predefined optimization level (-O<level>) . 489 490Maximum performance is achieved with -O3 -ipa, but that might increase 491the footprint substantially. A good compromise is -O2. See the compiler 492manual for details. 493 494Minimal footprint is achieved by optimizing for space with -Os, at the 495cost of some performance. See the compiler manual for details. 496 497The Xtensa architecture port-specific assembly files are coded with no 498file-scope labels inside functions (all labels inside functions begin with 499".L"). This allows a profiler to accurately associate an address with a 500function, and also allows the debugger's stack trace to show the correct 501function wherever the program counter is within that function. However 502there are some tradeoffs in debugging. Local (".L") labels are not 503visible to the debugger, so the following limitations may be observed 504during debugging: 505- You cannot set a breakpoint on a local label inside a function. 506- Disassembly will show the entire function, but will get out of sync and 507 show incorrect opcodes if it crosses any padding before an aligned local 508 branch target (".L" label, not ".Ln"). Restart disassembly specifying an 509 address range explicitly between points where there is padding. 510Since FreeRTOS is provided in source form, it is not difficult to remove 511the ".L" and ".Ln" prefixes from local labels if you want them visible. 512They can also be made visible by passing the '-L' option to the assembler 513and linker (see the assembler and linker manuals for details). 514 515 516Interrupt and Exception Handling 517-------------------------------- 518 519FreeRTOS provides a complete set of efficient exception and first-level 520interrupt handlers installed at the appropriate exception and interrupt 521vector locations. The Xtensa architecture supports several different 522classes of exceptions and interrupts. Being a configurable architecture, 523many of these are optional, and the vector locations are determined by 524your processor configuration. (Note that Diamond cores are pre-configured 525with specific vector locations.) The handlers provided use conditional 526compilation to adapt to your processor configuration and include only 527the code that is needed. 528 529Xtensa vector locations may reside almost anywhere, including in ROM. 530The amount of code space available at each of these locations is 531often very small (e.g. due to following vectors). A small stub of 532code installed at the vector jumps to the corresponding handler, 533usually in RAM. The exception and interrupt handlers are defined in 534xtensa_vectors.S. They are not specific to FreeRTOS, but call into 535FreeRTOS where appropriate via macros defined in xtensa_rtos.h . 536 537The handlers provided for low and medium priority interrupts are just 538dispatchers that save relevant state and call user-definable handlers. 539See the files xtensa_vectors.S and xtensa_api.h for more details of how 540to create and install application-specific user interrupt handlers. 541Similarly, user-defined handlers can be installed for exceptions (other 542than a few which are always handled by the OS). 543 544The high priority interrupt handlers provided may be considered templates 545into which the application adds code to service specific interrupts. 546The places where application handlers should be inserted are tagged with 547the comment "USER_EDIT" in xtensa_vectors.S. 548 549This FreeRTOS port supports strict priority-based nesting of interrupts. 550An interrupt may only nest on top of one of strictly lower priority. 551Equal priority interrupts concurrently pending are handled in an 552application-defined sequence before any lower priority interrupts 553are handled. During interrupt and exception handling, the processor's 554interrupt level (PS.INTLEVEL) is used to control the interrupt priority 555level that can be accepted; interrupt sources are not controlled 556individually by FreeRTOS (the application is free to access the INTENABLE 557register directly to enable/disable individual interrupts, eg. using 558Xtensa HAL services). This approach provides the most deterministic 559bounds on interrupt latency (for a given priority) and stack depth. 560 561Software prioritization of interrupts at the same priority is controlled 562by the definition of XT_USE_SWPRI. See above for a description of this 563parameter. 564 565The following subsections describe the handling of each class of exception 566and interrupt in more detail. Many have nothing to do with FreeRTOS but 567are mentioned because there is code to handle them in xtensa_vectors.S. 568 569User Exception and Interrupt Handler (Low/Medium Priority): 570 571 All Xtensa 'general exceptions' come to the user, kernel, or double 572 exception vector. The exception type is identified by the EXCCAUSE 573 special register (level 1 interrupts are one particular cause of a 574 general exception). This port sets up PS to direct all such exceptions 575 to the user vector. Exceptions taken at the other two vectors usually 576 indicate a kernel or application bug. 577 578 Level 1 interrupts are identified at the beginning of the handler 579 and are dispatched to a dedicated handler. Then, syscall and alloca 580 exceptions are identified and dispatched to special handlers described 581 below. After this, coprocessor exceptions are identified and dispatched 582 to the coprocessor handler. 583 584 Any remaining exceptions are processed as follows: 585 586 Having allocated the exception stack frame, the user exception handler 587 saves the current task state and sets up a C environment and enables 588 the high-priority class of interrupts (which do not interact with 589 FreeRTOS), then reads EXCCAUSE and uses the cause (number) to index 590 into a table of user-specified handlers. The correct handler is then 591 called. If the handler returns, the context is restored and control is 592 returned to the code that caused the exception. The user-defined handler 593 may alter the saved context, or any other system state, that allows the 594 faulting instruction to be retried. 595 596 If the cause is a level 1 (low-priority) or medium-priority interrupt, 597 the handler enables all interrupts above that priority level after 598 saving the task context. It then sets up the environment for C code 599 and then calls the handler (found in the handler table) for the 600 interrupt number. If the user has not specified a handler, then the 601 default handler will be called, which will terminate the program. 602 603 If the interrupt is for the system timer, it calls a special interrupt 604 handler for the system timer tick, which calls _frxt_timer_int then 605 clears its bit from the mask. This interrupt cannot be hooked by the 606 user-defined handler. 607 608 Finally, the handler calls _frxt_int_exit to allow FreeRTOS to perform 609 any scheduling necessary and return either to the interrupted task 610 or another. 611 612 If software prioritization is enabled, the handler will re-enable all 613 interrupts at the same level that are numerically higher than the current 614 one, before calling the user handler. This allows a higher priority 615 interrupt to pre-empt the lower priority handler. 616 617Medium Priority Interrupt Handlers: 618 619 Medium priority interrupts are those at levels 2 up to XCHAL_EXCM_LEVEL, 620 a configuration-specific maximum interrupt level affected by the global 621 'exception mode' bit in the processor status word (PS.EXCM). 622 Interrupt levels above XCHAL_EXCM_LEVEL are of the high-priority class. 623 The Xtensa hardware documentation considers medium priority interrupts 624 to be a special case of high-priority interrupts, but from a software 625 perspective they are very different. 626 627 Dispatch of medium-priority interrupts is discussed in the section 628 above. 629 630High Priority Interrupt Handlers: 631 632 High priority interrupts are those strictly above XCHAL_EXCM_LEVEL, 633 a configuration-specific maximum interrupt level affected by the 634 global 'exception mode' bit in the processor status word (PS.EXCM). 635 High priority handlers may not directly interact with FreeRTOS at all, 636 and are described here only for the sake of completeness. They must 637 be coded in assembler (may not be coded in C) and are intended to be 638 used for handling extremely high frequency hardware events that need 639 to be handled in only a few cycles. A high priority interrupt handler 640 may trigger a software interrupt at a medium or low priority level to 641 occasionally signal FreeRTOS. Please see Xtensa documentation. 642 643 There is a separate vector and a few special registers for each high 644 priority interrupt, providing for fast dispatch and efficient nesting 645 on top of lower priority interrupts. Handlers are templates included 646 only for the vectors that exist in your Xtensa processor configuration. 647 These templates are written for only one interrupt per high priority 648 level to minimize latency servicing very fast time-critical interrupts. 649 The vector code jumps to the corresponding first-level interrupt handler, 650 which then executes application-provided assembler code before returning 651 quickly to the interrupted task or lower priority handler. 652 653Kernel Exception Handler: 654 655 Kernel mode is not used in this port of FreeRTOS, and therefore kernel 656 exceptions should not happen. A stub is provided for the vector that 657 triggers the debugger (if connected) or calls _xt_panic to freeze the 658 processor should a kernel exception occur. 659 660Alloca Exception Handler: 661 662 Alloca exceptions are generated by the 'movsp' instruction, which 663 is used only in the windowed ABI. Its purpose is to allocate some 664 space on top of the stack. Because the window hardware may have 665 spilled some registers to the 16 byte "base save" area below the 666 stack pointer, it is necessary to protect those values. The alloca 667 handler accomplishes this quickly without setting up an interrupt 668 frame or entering FreeRTOS, by emulating a register underflow and 669 re-executing 'movsp'. 670 671Syscall Exception Handler: 672 673 Syscall exceptions are generated by a 'syscall' instruction. 674 The windowed ABI specifies that executing this instruction with 675 a value of zero in register a2 must spill any unsaved registers 676 in the windowed register file to their pre-determined locations 677 on the caller's stack. The handler does exactly that, and skips 678 over the 'syscall' instruction before returning to the caller. 679 If a2 is non-zero, the handler returns a2 == -1 to the caller. 680 681Co-Processor Exception Handler: 682 683 A co-processor exception is generated when a task accesses a 684 co-processor that it does not "own". Ownership represents which 685 task's state is currently in the co-processor. Co-processors are 686 context-switched "lazily" (on demand) only when a non-owning task 687 uses a co-processor instruction, otherwise a task retains ownership 688 even when it is preempted from the main processor. The co-processor 689 exception handler performs the context-switch and manages ownership. 690 691 Co-processors may not be used by any code outside the context of a 692 task. A co-processor exception triggered by code that is not part 693 of a running task is a fatal error and FreeRTOS for Xtensa will panic. 694 This restriction is intended to reduce the overhead of saving and 695 restoring co-processor state (which can be quite large) and in 696 particular remove that overhead from interrupt handlers. 697 698Debug Exception Handler: 699 700 A debug exception is caused as a result of running code, such as by 701 a 'break' instruction or hardware breakpoints and watchpoints, or 702 as a result of an external debug interrupt, such as from an OCD based 703 debugger or multiprocessor debug events ("breakin/breakout"). If the 704 processor is running in OCD mode under control of an OCD-based debugger, 705 the trigger event immediately halts the processor and gives control to 706 the OCD debugger. Otherwise control is transferred to the debug vector. 707 The debug vector handler calls the simulator if running on the ISS, 708 which then takes control and interacts with any attached debugger. 709 If running on hardware and not in OCD mode, debug exceptions are not 710 expected, so the debug handler calls _xt_panic to freeze the processor. 711 712Double Exception Handler: 713 714 A double exception is a general exception that happens while the 715 processor is in exception mode (PS.EXCM set), and thus indicates a 716 bug in kernel code. The double exception vector handler triggers 717 the debugger (if connected) or calls _xt_panic to freeze the 718 processor. 719 720Window Overflow and Underflow Exception Handlers: 721 722 Window overflow and underflow handlers are required for use of the 723 windowed ABI. Each has its own dedicated vector and highly optimized 724 code that is independent of OS. See Xtensa documentation for details. 725 726Hooks for Dynamic Installation of Handlers: 727 728 Optional hooks are provided in the user exception and low level 729 interrupt handler and all medium and high priority interrupt handlers, 730 to dynamically install a handler function (which may be coded in C, 731 unless in a high-priority interrupt handler). These hooks are enabled 732 and used by automatic regression tests, they are not part of a normal 733 FreeRTOS build. However an application is free to take advantage of 734 them. The interrupt/exception hooks are described in xtensa_rtos.h . 735 736 It is recommended that the application not make use of these hooks, but 737 rather use xt_set_interrupt_handler() and xt_set_exception_handler() 738 to install application-specific handlers. This method is more convenient 739 and allows arguments to be passed to the handlers. Software prioritization 740 of interrupts works only with this method. See xtensa_api.h for details. 741 742Overlay Support 743 744 Code overlays are currently not supported for FreeRTOS. This will be 745 supported in a future release. Make sure that the option XT_USE_OVLY is 746 never defined when building. 747 748 749Note on Porting Methodology and Copyrights 750------------------------------------------ 751 752This port is based on a well-tested Cadence RTOS porting layer 753that abstracts out Xtensa-generic aspects from the RTOS specifics 754in the same way most RTOSes abstract out their generic code from the 755architecture specific port code. This code is common to several RTOSes 756ported by Cadence, and deals with highly Xtensa specific aspects such 757as context save/restore, interrupt and exception dispatch, and handling 758of co-processor, alloc and window over/underflow exceptions. 759 760The porting layer files are prefixed "xtensa_" and belong to Cadence 761(they are provided freely as part of this port but are not subject to 762FreeRTOS's copyright). The rest of the port is FreeRTOS specific and 763are under FreeRTOS's copyright. 764 765The Makefiles are also provided by Cadence so have much in common with 766other RTOS Makefiles provided by Cadence. 767 768-End- 769