|
| 1 | +--- |
| 2 | +title: Creating a Project |
| 3 | +description: This tutorial will guide you through creating a new project with a simple hello world program. |
| 4 | +code_folder: examples/guides/creating_project/ |
| 5 | +--- |
| 6 | +import Contact from '@components/contact.astro'; |
| 7 | +import GitSetup from '@components/gitsetup.mdx'; |
| 8 | + |
| 9 | +Now that we have played around with the examples and have a basic understanding of how to use RIOT, |
| 10 | +let's create a new project from scratch. |
| 11 | +We will create a simple hello world program that will print "Hello World!" to the console. |
| 12 | + |
| 13 | +Lets start with the basic git setup, |
| 14 | +if you already have a git repository set up, |
| 15 | +you can skip to the next section. |
| 16 | + |
| 17 | +## Step 1: The Basics of Git and Submodules |
| 18 | + |
| 19 | +<GitSetup /> |
| 20 | + |
| 21 | +## Step 2: Creating our hello world program |
| 22 | + |
| 23 | +Now that we have added RIOT as a submodule to our project, |
| 24 | +we can start writing our hello world program. |
| 25 | +To do this, we create a new file called `main.c` in the `hello_world` directory. |
| 26 | +You can use any text editor to create this file. |
| 27 | +We will use Visual Studio Code in this example. |
| 28 | +To open Visual Studio Code in the directory, you can use the following command: |
| 29 | + |
| 30 | +```bash |
| 31 | +code . |
| 32 | +``` |
| 33 | + |
| 34 | +Now that Visual Studio Code is open, |
| 35 | +we create a new file called `main.c` and add the following code: |
| 36 | + |
| 37 | +```c title="hello_world/main.c" |
| 38 | +/* |
| 39 | + * For many printing related things, such as the puts function here |
| 40 | + * we import stdio, depending on your board, platform or form of output |
| 41 | + * it then includes the right definitions without the need to |
| 42 | + * worry about the specific details. |
| 43 | + */ |
| 44 | +#include <stdio.h> |
| 45 | + |
| 46 | +/* |
| 47 | + * This is the main function of the program. |
| 48 | + * It serves as the entry point for the program and gets called once your CPU is |
| 49 | + * initialized. |
| 50 | + * |
| 51 | + * The function returns an integer value, which is the exit status |
| 52 | + * of the program. A return value of 0 indicates that the program has finished |
| 53 | + * successfully. |
| 54 | + */ |
| 55 | +int main(void) { |
| 56 | + puts("Hello World!"); |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
| 60 | +``` |
| 61 | +
|
| 62 | + |
| 63 | +
|
| 64 | +This program will print "Hello World!" to the console when it is run. |
| 65 | +The `#include <stdio.h>` line includes the standard input/output library, |
| 66 | +which allows us to use the `puts` function to print to the console. |
| 67 | +
|
| 68 | +## Step 3: Creating the Makefile |
| 69 | +
|
| 70 | +Now that we have created our hello world program, |
| 71 | +we need to create a Makefile to build our program. |
| 72 | +The Makefile is a build automation tool that allows us to define how our program should be built. |
| 73 | +We create a new file called `Makefile` in the `hello_world` directory and add the following code: |
| 74 | +
|
| 75 | +```makefile title="hello_world/Makefile" |
| 76 | +# name of your application |
| 77 | +APPLICATION = hello-world |
| 78 | +
|
| 79 | +# Change this to your board if you want to build for a different board |
| 80 | +BOARD ?= native |
| 81 | +
|
| 82 | +# This has to be the absolute path to the RIOT base directory: |
| 83 | +RIOTBASE ?= $(CURDIR)/RIOT |
| 84 | +
|
| 85 | +# Comment this out to disable code in RIOT that does safety checking |
| 86 | +# which is not needed in a production environment but helps in the |
| 87 | +# development process: |
| 88 | +DEVELHELP ?= 1 |
| 89 | +
|
| 90 | +# Change this to 0 show compiler invocation lines by default: |
| 91 | +QUIET ?= 1 |
| 92 | +
|
| 93 | +include $(RIOTBASE)/Makefile.include |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +Congratulations! You have now created a new project with a simple hello world program. |
| 99 | +In the next step, we will build and run our program just like we did in the "Getting Started" guide. |
| 100 | + |
| 101 | +## Step 4: Building and running the program |
| 102 | + |
| 103 | +<Contact /> |
| 104 | + |
| 105 | +To build our program, we use the following command: |
| 106 | + |
| 107 | +```bash |
| 108 | +BUILD_IN_DOCKER=1 make all |
| 109 | +``` |
| 110 | + |
| 111 | +:::note |
| 112 | +The `BUILD_IN_DOCKER=1` flag tells the build system to use the docker image provided by RIOT |
| 113 | +to build our program. |
| 114 | +This ensures that we have all the necessary dependencies to build our program. |
| 115 | +If you have already built RIOT on your system, |
| 116 | +you can omit this flag and the build system will use the toolchain installed on your system. |
| 117 | + |
| 118 | +Do note that building in Docker will take quite a bit longer on the first run, |
| 119 | +as it needs to download a fairly large Docker image. |
| 120 | + |
| 121 | +Alteratively, you can simply run: |
| 122 | + |
| 123 | +```bash |
| 124 | +make all |
| 125 | +``` |
| 126 | +::: |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +After building the program, |
| 131 | +we can run it using the following command to start the RIOT shell: |
| 132 | + |
| 133 | +```bash |
| 134 | +make term |
| 135 | +``` |
| 136 | + |
| 137 | +:::note |
| 138 | +Depending on your board, the hello world might appear before the RIOT shell starts. |
| 139 | + |
| 140 | +If you want to see the output of the hello world program, |
| 141 | +you can run `make flash term` instead, |
| 142 | +which will flash the program to the board and then start the RIOT shell. |
| 143 | + |
| 144 | +This does not guarantee that the hello world program will run before the RIOT shell starts |
| 145 | +but should increase the chances of it appearing before the shell starts. |
| 146 | +::: |
| 147 | + |
| 148 | +If everything went well, |
| 149 | +you should see our hello world program printing "Hello World!" to the console after a few seconds. |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +Hooraay! You have successfully created a new project with a simple hello world program. |
| 154 | + |
| 155 | +:::tip |
| 156 | +Before you push your project to a git hosting service such as Github, |
| 157 | +make sure to add a `.gitignore` file to your project to exclude |
| 158 | +unnecessary files from being tracked by git. |
| 159 | + |
| 160 | +For this project, a `.gitignore` file could look like this: |
| 161 | + |
| 162 | +```bash title=".gitignore" |
| 163 | +# Ignore build artifacts |
| 164 | +bin/ |
| 165 | +*.bin |
| 166 | +*.elf |
| 167 | +*.hex |
| 168 | +*.map |
| 169 | +*.lst |
| 170 | +*.o |
| 171 | +*.d |
| 172 | +*.a |
| 173 | +*.out |
| 174 | +``` |
| 175 | +::: |
| 176 | + |
| 177 | +## Conclusion |
| 178 | + |
| 179 | +In this tutorial, we have created a new project with a simple hello world program. |
| 180 | +We have added RIOT as a submodule to our project, created a hello world program, |
| 181 | +and built and run the program using the RIOT build system. |
| 182 | +You can now start building your own applications using RIOT |
| 183 | +and explore the vast possibilities that RIOT has to offer. |
| 184 | + |
| 185 | +:::note |
| 186 | +The source code for this tutorial can be found |
| 187 | +[HERE](https://github.com/RIOT-OS/RIOT/tree/master/examples/guides/creating_project). |
| 188 | + |
| 189 | +If your project is not working as expected, |
| 190 | +you can compare your code with the code in this repository to see if you missed anything. |
| 191 | +::: |
0 commit comments