summaryrefslogtreecommitdiff
path: root/cmake-nRF5x/readme.md
blob: 1a556b12d2733b71ccffdc912cf1c413a17f4bbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# cmake-nRF5x

Cmake script for projects targeting Nordic Semiconductor nRF5x series devices using the GCC toolchain from ARM.

# Dependencies

The script makes use of the following tools:

- nRF5x SDK by Nordic Semiconductor - SoC specific drivers and libraries (also includes a lot of examples)
- JLink by Segger - interface software for the JLink family of programmers
- nrfjprog by Nordic Semiconductor - Wrapper utility around JLink
- arm-non-eabi-gcc by ARM and the GCC Team - compiler toolchain for embedded (= bare metal) ARM chips

# Setup

1. Download this repo (or add as submodule) to the directory `cmake-nRF5x` in your project

1. Search the SDK `example` directory for a `sdk_config.h`, `main.c` and a linker script (normally named `<project_name>_gcc_<chip family>.ld`) that fits your chip and project needs.

1. Copy the `sdk_config.h` and the project `main.c` into a new directory `src`. Modify them as required for your project.

1. Copy the linker script into the root of your project. Rename it to just `gcc_<chip family>.ld` For example:
	
	```
	gcc_nrf51.ld
	gcc_nrf52.ld
	```

1. Create a new `CMakeLists.txt` file at the same level. Add the project standard cmake project header

	A typical file may look like this:

	```
	cmake_minimum_required(VERSION 3.6)

	set(NRF_TARGET "nrf52")
	
	# optional, won't be used if passing toolchain on command line (see below)
	if (NOT DEFINED ARM_NONE_EABI_TOOLCHAIN_PATH)
		set(ARM_NONE_EABI_TOOLCHAIN_PATH "/usr/local/bin")
	endif ()
	
	set(NRF5_SDK_PATH "${CMAKE_SOURCE_DIR}/toolchains/nRF5/nRF5_SDK")
	set(NRFJPROG "${CMAKE_SOURCE_DIR}/toolchains/nRF5/nrfjprog/nrfjprog")

	include("cmake-nRF5x/CMake_nRF5x.cmake")

	# must be called before first project call or add_subdirectory unless passing on command line
	nRF5x_toolchainSetup()

	project(YourProjectName C ASM)
	
	nRF5x_setup()

	nRF5x_addAppScheduler()
	nRF5x_addAppFIFO()
	nRF5x_addAppTimer()
	nRF5x_addAppUART()
	nRF5x_addAppButton()
	nRF5x_addBSP(TRUE FALSE FALSE)
	nRF5x_addBLEGATT()


	nRF5x_addBLEService(ble_bas)

	add_definitions(-DCONFIG_GPIO_AS_PINRESET)
			
	include_directories("./src")
	list(APPEND SOURCE_FILES "./src/main.c")

	nRF5x_addExecutable(${PROJECT_NAME} "${SOURCE_FILES}")
	```

	Adjust as needed for your project.

	_Note_: you can add `CXX` between `C ASM` to add c++ support
	
1. Optionally add additional libraries:

	Only the most common drivers and libraries are wrapped with cmake macros.

	To include BLE services, use `nRF5x_addBLEService(<service name>)`.

	For other SDK libraries you can use `include_directories` and `list(APPEND SDK_SOURCE_FILES ...)` to add them. For example:

	```cmake
	include_directories(
	        "${NRF5_SDK_PATH}/<library header directory path>"
	)
		
	list(APPEND SDK_SOURCE_FILES
	        "${NRF5_SDK_PATH}/<library source file path>"
	)
	```
	

# Build

After setup you can use cmake as usual:

1. Generate the actual build files (out-of-source builds are strongly recommended):

	```commandline
	cmake -H. -B"cmake-build" -G "Unix Makefiles"
	```
	You can optionally pass the toolchain to `cmake` when configuring:
    ```
    -DCMAKE_TOOLCHAIN_PATH=cmake-nRF5x/arm-gcc-toolchain.cmake
    ```
    but if you do so you must ensure the toolchain binaries are available in your environment PATH (i.e. work on the command line without specifying absolute path)

2. Build your app:

	```commandline
	cmake --build "cmake-build" --target <your project name>
	```

# Flash

In addition to the build target (named like your project) the script adds some support targets:

`FLASH_SOFTDEVICE` To flash a nRF softdevice to the SoC (typically done only once for each SoC)

```commandline
cmake --build "cmake-build" --target FLASH_SOFTDEVICE
```

`FLASH_<your project name>` To flash your application (this will also rebuild your App first)

```commandline
cmake --build "cmake-build" --target FLASH_<your project name>
```

`FLASH_ERASE` To completely erase the SoC flash

```commandline
cmake --build "cmake-build" --target FLASH_ERASE
```

# JLink Applications

To start the gdb server and RTT terminal, build the target `START_JLINK`:

```commandline
cmake --build "cmake-build" --target START_JLINK
```

# License

MIT for the `CMake_nRF5x.cmake` file. 

Please note that the nRF5x SDK by Nordic Semiconductor is covered by it's own license and shouldn't be re-distributed.