XC-Tutorial
39 pages
English

XC-Tutorial

-

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
39 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description



XC Tutorial
The Language of SDS









XMOS Semiconductor
Bristol • United Kingdom XC Tutorial - 080731: Contents Page www.xmos.com


2 of 39 © 2008 XMOS Semiconductor
XC Tutorial - 080731: Contents Page www.xmos.com
Contents Page
… HOW TO FIND YOUR WAY AROUND
1 | ABOUT THIS TUTORIAL ..................................................................................................................4 
2 | PORTS AND FIRST XC PROGRAM.................................................................................................5 
3 | PREDICATED INPUT (FROM PORTS).............................8 
4 | TIMERS............................................................................................................................................10 
5 | PORTS, TIMERS AND FLASHING LEDS......................11 
5.1 | Flashing an LED using a timer...12 
6 | TIMESTAMPS AND TIMED I/O.......................................................................................................14 
6.1 | Timed Input and Output.............14 
7 | PARALLELISM AND THREADS....15 
7.1 | The par Keyword .......................................................................................................................15 
8 | COMMUNICATION AND CHANNELS............................18 
8.1 | Declaring and using channels....................................................................................................18 
8.2 | Pass the LED............................ ...

Sujets

Informations

Publié par
Nombre de lectures 473
Langue English
Poids de l'ouvrage 2 Mo

Extrait

XC Tutorial The Language of SDS XMOS Semiconductor Bristol • United Kingdom XC Tutorial - 080731: Contents Page www.xmos.com 2 of 39 © 2008 XMOS Semiconductor XC Tutorial - 080731: Contents Page www.xmos.com Contents Page … HOW TO FIND YOUR WAY AROUND 1 | ABOUT THIS TUTORIAL ..................................................................................................................4  2 | PORTS AND FIRST XC PROGRAM.................................................................................................5  3 | PREDICATED INPUT (FROM PORTS).............................8  4 | TIMERS............................................................................................................................................10  5 | PORTS, TIMERS AND FLASHING LEDS......................11  5.1 | Flashing an LED using a timer...12  6 | TIMESTAMPS AND TIMED I/O.......................................................................................................14  6.1 | Timed Input and Output.............14  7 | PARALLELISM AND THREADS....15  7.1 | The par Keyword .......................................................................................................................15  8 | COMMUNICATION AND CHANNELS............................18  8.1 | Declaring and using channels....................................................................................................18  8.2 | Pass the LED.............................................................19  8.3 | Binary Count..............................20  9 | HANDLING MULTIPLE EVENTS....22  10 | CLOCKS AND CLOCKED I/O.......................................................................................................26  10.1 | Clocked Output Example.........27  11 | INTEGRATING A SOFTWARE COMPONENT (RGB LEDS).......................................................31  12 | AUTHORING A SOFTWARE COMPONENT (BUTTON DRIVER)...............35  13 | APPENDIX A .................................................................................................................................37  13.1 | RGB driver XC source code....37  13.2 | XDK XCore signals..................................................................................................................37  3 of 39 © 2008 XMOS Semiconductor XC Tutorial - 080731: About This Tutorial www.xmos.com 1 | About This Tutorial WELCOME … The XDK tutorial is for developers who want to develop Software Defined Silicon solutions using the XC language, and introduces some of the key concepts of the language. XC is a high-level C-based programming language that includes intuitive and naturally readable extensions for concurrency, communications, input/output operations and real-time behaviour. XC libraries share a common ABI with standard C libraries so you can create applications that use both languages, although they cannot be mixed within a single file. This tutorial focuses on the features of XC and does not discuss integration with C programs. However, it assumes some familiarity with high-level programming languages, such as C. Knowledge of hardware description languages is neither assumed nor required. The tutorial uses a set of example files that you can run in the online WDE tools; click on the examples where indicated to load an example in the toolkit. When you finish this tutorial we recommend that you look at some of the other examples on: http://www.xmoslabs.com/ 4 of 39 © 2008 XMOS Semiconductor XC Tutorial - 080731: Ports and first XC program www.xmos.com 2 | Ports and first XC program XC directly expresses input and output operations on the XCore processor pins using ports. A port may be associated with a single pin or a group of pins on the device. On the XDK these ports are attached to external resource such as LEDs, buttons, and LCD Screen—see Appendix 13.2 | for a layout of the ports and devices. Data can be written and read directly to/from ports using built in I/O statements. Ports are declared using the port keyword: port port_a; Ports declared in this format are bidirectional and can be used for input and output, although not at the same time. Ports can be further qualified as an output or input port using the in and out keywords as follows: out port port_b; in port port_c; The port type cannot be changed after it has been declared. Data can be output from a port using the output operator <:, and input to a port using the input operator :>. You can also wait until input data meets a specified condition before it is read. The following example shows a simple program written in XC, which demonstrates the use of an output port: 5 of 39 © 2008 XMOS Semiconductor XC Tutorial - 080731: Ports and first XC program www.xmos.com #include // Declare port globally out port port1 = XS1_PORT_1E; // Main function int main () { port1 <: 1; return 0; } The syntax in should be familiar to most C programmers. The xs1_reva.h header file must be included in all projects. It provides an abstraction layer between hardware/software and allows the programmer to produce portable code across future XMOS devices. The XS1_PORT_1E definition originates from the xs1_reva.h file, and must be declared globally so that it can be allocated correctly by the linker/mapper. If you try and declare a port locally, the port status cannot be correctly verified during compilation, and the declaration is flagged as an error. As shown, comments are of the “C++ style” format. The multiline “C- style” /* comment */ format is also supported. Like C, program execution beings at the main()function. The main function in this example declares a port (1E) and outputs a value to it. The program is intended to illuminate an LED connected to port 1E. The LEDs on the XDK are active low. Create a new blank project in the WDE, rename the source file led.xc and then copy this code above into the file. Compile the file and then load the file onto your XDK. (For further details on loading programs onto your XDK see the XDK Quick Start Guide provided with the XDK or available on www.xmoslabs.com) The LED connected to porrt 1E is illuminated for a brief moment (too fast for the human eye to observe) prior to the program terminating and the port being disabled. 6 of 39 © 2008 XMOS Semiconductor XC Tutorial - 080731: Ports and first XC program www.xmos.com To keep the LED illuminated, modify the program to prevent it from terminating. This can be acheived by adding a loop, as shown below: #include out port led_port = XS1_PORT_1E; single_led // Main method int main () { led_port <: 1; while(1) { // Do nothing } return 0; } Try to extend the program to illuminate all 3 LEDs connected to core 0. Hint: They are connected to ports 1F and 1G. 7 of 39 © 2008 XMOS Semiconductor XC Tutorial - 080731: Predicated input (from ports) www.xmos.com 3 | Predicated input (from ports) In XC, you can specify a condition that must be evaluated as true before an input operation is performed. Execution pauses until the condition is evaluated as true, at which point the input completes and execution continues. In XC predicated input is done using the when keyword and a condition function. XC supports the following conditions that you can use to predicate input from ports: Syntax Meaning Condition is true when value on pinseq(int) pins is equal to the specified value Condition is true when value on pinsneq(int) pins is not equal to the specified value For example the following code can be used to delay the program execution until the value in the input port is equal to logic 1 (and the input value is guaranteed to be 1): in port p; int data; p when pinseq(1) :> data; All of the condition function prototypes are included in the xs1_reva.h header file, which must be included before use. Using predicated input from a port, you can make the LED illuminate once the button connected to port 1J is pressed: 8 of 39 © 2008 XMOS Semiconductor XC Tutorial - 080731: Predicated input (from ports) www.xmos.com #include out port led_port = XS1_PORT_1E; predicated in port button_port = XS1_PORT_1J; int main () { button_port when pinseq(1):> unsigned tmp; led_port <: 1; while(1) { // Do nothing } Return 0 } 9 of 39 © 2008 XMOS Semiconductor XC Tutorial - 080731: Timers www.xmos.com 4 | Timers XC directly expresses timing operations so that you can track time and control timed execution. The time reference is through a continually running reference clock that ticks at a frequency of 100MHz. A timer is declared using the timer keyword as follows: timer t; The current time can be read by using an input statement, for example, to input the current time into a variable time use: int time; timer t; t :> time; Output operations are not permitted on timers. Timers are free running and automatically reset if the device is restarted, so the program should use a timer to store the current time and delay operations relative to this time: int main() { timer t; unsigned time; t :> time; // Use current time... } 10 of 39 © 2008 XMOS Semiconductor
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents