1.. zephyr:board:: altera_max10
2
3Overview
4********
5
6The Zephyr kernel is supported on the Altera MAX10 Rev C development kit, using
7the Nios II Gen 2 soft CPU.
8
9Hardware
10********
11
12DIP Switch settings
13===================
14
15There are two sets of switches on the back of the board. Of particular
16importance is SW2:
17
18* Switch 2 (CONFIG_SEL) should be in the OFF (up) position so that the first
19  boot image is CFM0
20* Switch 3 (VTAP_BYPASS) needs to be in the ON (down) position or the flashing
21  scripts won't work
22* Switch 4 (HSMC_BYPASSN) should be OFF (up)
23
24.. image:: img/Altera_MAX10_switches.jpg
25   :align: center
26   :alt: Altera's MAX* 10 Switches
27
28Other switches are user switches, their position is application-specific.
29
30Necessary Software
31==================
32
33You will need the Altera Quartus SDK in order to work with this device. The
34`Altera Lite Distribution`_ of Quartus may be obtained without
35charge.
36
37For your convenience using the SDK tools (such as ``nios2-configure-sof``),
38you should put the binaries provided by the SDK
39in your path. Below is an example, adjust ALTERA_BASE to where you installed the
40SDK:
41
42.. code-block:: console
43
44   export ALTERA_BASE=/opt/altera_lite/16.0
45   export PATH=$PATH:$ALTERA_BASE/quartus/bin:$ALTERA_BASE/nios2eds/bin
46
47You may need to adjust your udev rules so that you can talk to the USB Blaster
48II peripheral, which is the built-in JTAG interface for this device.
49
50The following works for Fedora 23:
51
52.. code-block:: console
53
54   # For Altera USB-Blaster permissions.
55   SUBSYSTEM=="usb",\
56   ENV{DEVTYPE}=="usb_device",\
57   ATTR{idVendor}=="09fb",\
58   ATTR{idProduct}=="6010",\
59   MODE="0666",\
60   NAME="bus/usb/$env{BUSNUM}/$env{DEVNUM}",\
61   RUN+="/bin/chmod 0666 %c"
62   SUBSYSTEM=="usb",\
63   ENV{DEVTYPE}=="usb_device",\
64   ATTR{idVendor}=="09fb",\
65   ATTR{idProduct}=="6810",\
66   MODE="0666",\
67   NAME="bus/usb/$env{BUSNUM}/$env{DEVNUM}",\
68   RUN+="/bin/chmod 0666 %c"
69
70You can test connectivity with the SDK jtagconfig tool, you should see something
71like:
72
73.. code-block:: console
74
75   $ jtagconfig
76   1) USB-BlasterII [1-1.2]
77     031050DD   10M50DA(.|ES)/10M50DC
78     020D10DD   VTAP10
79
80
81Reference CPU
82=============
83
84A reference CPU design of a Nios II/f core is included in the Zephyr tree
85in the :zephyr_file:`soc/altr/zephyr_nios2f/cpu` directory.
86
87Flash this CPU using the ``nios2-configure-sof`` SDK tool with the FPGA
88configuration file
89:zephyr_file:`soc/altr/zephyr_nios2f/cpu/ghrd_10m50da.sof`:
90
91.. code-block:: console
92
93   $ nios2-configure-sof ghrd_10m50da.sof
94
95This CPU is a Nios II/F core with a 16550 UART, JTAG UART, and the Avalon Timer.
96For any Nios II SOC definition, you can find out more details about the CPU
97configuration by inspecting system.h in the SOC's include directory.
98
99Console Output
100==============
101
10216550 UART
103----------
104
105By default, the kernel is configured to send console output to the 16550 UART.
106You can monitor this on your workstation by connecting to the top right mini USB
107port on the board (it will show up in /dev as a ttyUSB node), and then running
108minicom with flow control disabled, 115200-8N1 settings.
109
110JTAG UART
111---------
112
113You can also have it send its console output to the JTAG UART.
114Enable ``jtag_uart`` node in :file:`altera_max10.dts` or overlay file:
115
116.. code-block:: devicetree
117
118   &jtag_uart {
119       status = "okay";
120       current-speed = <115200>;
121   };
122
123To view these messages on your local workstation, run the terminal application
124in the SDK:
125
126.. code-block:: console
127
128   $ nios2-terminal
129
130Programming and Debugging
131*************************
132
133Flashing
134========
135
136Flashing Kernel into UFM
137------------------------
138
139The usual ``flash`` target will work with the ``altera_max10`` board
140configuration. Here is an example for the :zephyr:code-sample:`hello_world`
141application.
142
143.. zephyr-app-commands::
144   :zephyr-app: samples/hello_world
145   :board: altera_max10
146   :goals: flash
147
148Refer to :ref:`build_an_application` and :ref:`application_run` for
149more details.
150
151This provisions the Zephyr kernel and the CPU configuration onto the board,
152using the scripts/support/quartus-flash.py script. After it completes the kernel
153will immediately boot.
154
155
156Flashing Kernel directly into RAM over JTAG
157-------------------------------------------
158
159The SDK included the nios2-download tool which will let you flash a kernel
160directly into RAM and then boot it from the __start symbol.
161
162In order for this to work, your entire kernel must be located in RAM. Make sure
163the following config options are disabled:
164
165.. code-block:: cfg
166
167   CONFIG_XIP=n
168   CONFIG_INCLUDE_RESET_VECTOR=n
169
170Then, after building your kernel, push it into device's RAM by running
171this from the build directory:
172
173.. code-block:: console
174
175   $ nios2-download --go zephyr/zephyr.elf
176
177If you have a console session running (either minicom or nios2-terminal) you
178should see the application's output. There are additional arguments you can pass
179to nios2-download so that it spawns a GDB server that you can connect to,
180although it's typically simpler to just use nios2-gdb-server as described below.
181
182Debugging
183=========
184
185The Altera SDK includes a GDB server which can be used to debug a MAX10 board.
186You can either debug a running image that was flashed onto the device in User
187Flash Memory (UFM), or load an image over the JTAG using GDB.
188
189Debugging With UFM Flashed Image
190--------------------------------
191
192You can debug an application in the usual way.  Here is an example.
193
194.. zephyr-app-commands::
195   :zephyr-app: samples/hello_world
196   :board: altera_max10
197   :goals: debug
198
199You will see output similar to the following:
200
201.. code-block:: console
202
203   Nios II GDB server running on port 3335
204   Ignoring --stop option because --tcpport also specified
205   GNU gdb (GDB) 7.11.0.20160511-git
206   Copyright (C) 2016 Free Software Foundation, Inc.
207   License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
208   This is free software: you are free to change and redistribute it.
209   There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
210   and "show warranty" for details.
211   This GDB was configured as "--host=x86_64-pokysdk-linux --target=nios2-zephyr-elf".
212   Type "show configuration" for configuration details.
213   For bug reporting instructions, please see:
214   <http://www.gnu.org/software/gdb/bugs/>.
215   Find the GDB manual and other documentation resources online at:
216   <http://www.gnu.org/software/gdb/documentation/>.
217   For help, type "help".
218   Type "apropos word" to search for commands related to "word"...
219   Reading symbols from /projects/zephyr/samples/hello_world/build/zephyr/zephyr.elf...done.
220   Remote debugging using :3335
221   Using cable "USB-BlasterII [3-1.3]", device 1, instance 0x00
222   Resetting and pausing target processor: OK
223   Listening on port 3335 for connection from GDB: accepted
224   isr_tables_syms () at /projects/zephyr/arch/common/isr_tables.c:63
225   63      GEN_ABSOLUTE_SYM(__ISR_LIST_SIZEOF, sizeof(struct _isr_list));
226   (gdb) b z_prep_c
227   Breakpoint 1 at 0xdf0: file /projects/zephyr/arch/nios2/core/prep_c.c, line 36.
228   (gdb) b z_cstart
229   Breakpoint 2 at 0x1254: file /projects/zephyr/kernel/init.c, line 348.
230   (gdb) c
231   Continuing.
232
233   Breakpoint 2, z_cstart () at /projects/zephyr/kernel/init.c:348
234   348     {
235   (gdb)
236
237To start debugging manually:
238
239
240.. code-block:: console
241
242   nios2-gdb-server --tcpport 1234 --stop --reset-target
243
244And then connect with GDB from the build directory:
245
246
247.. code-block:: console
248
249   nios2-poky-elf-gdb  zephyr/zephyr.elf -ex "target remote :1234"
250
251Debugging With JTAG Flashed Image
252---------------------------------
253
254In order for this to work, execute-in-place must be disabled, since the GDB
255'load' command can only put text and data in RAM. Ensure this is in your
256configuration:
257
258.. code-block:: cfg
259
260   CONFIG_XIP=n
261
262It is OK for this procedure to leave the reset vector enabled, unlike
263nios2-download (which errors out if it finds sections outside of SRAM) it will
264be ignored.
265
266In a terminal, launch the nios2 GDB server. It doesn't matter what kernel (if
267any) is on the device, but you should have at least flashed a CPU using
268nios2-configure-sof. You can leave this process running.
269
270.. code-block:: console
271
272   $ nios2-gdb-server --tcpport 1234 --tcppersist --init-cache --reset-target
273
274Build your Zephyr kernel, and load it into a GDB built for Nios II (included in
275the Zephyr SDK) from the build directory:
276
277.. code-block:: console
278
279   $ nios2-poky-elf-gdb zephyr/zephyr.elf
280
281Then connect to the GDB server:
282
283.. code-block:: console
284
285   (gdb) target remote :1234
286
287And then load the kernel image over the wire. The CPU will not start from the
288reset vector, instead it will boot from the __start symbol:
289
290
291.. code-block:: console
292
293   (gdb) load
294   Loading section reset, size 0xc lma 0x0
295   Loading section exceptions, size 0x1b0 lma 0x400020
296   Loading section text, size 0x8df0 lma 0x4001d0
297   Loading section devconfig, size 0x30 lma 0x408fc0
298   Loading section rodata, size 0x3f4 lma 0x408ff0
299   Loading section datas, size 0x888 lma 0x4093e4
300   Loading section initlevel, size 0x30 lma 0x409c6c
301   Loading section _k_task_list, size 0x58 lma 0x409c9c
302   Loading section _k_task_ptr, size 0x8 lma 0x409cf4
303   Loading section _k_event_list, size 0x10 lma 0x409cfc
304   Start address 0x408f54, load size 40184
305   Transfer rate: 417 KB/sec, 368 bytes/write.
306   After this is done you may set breakpoints and continue execution. If you ever want to reset the CPU, issue the 'load' command again.
307
308
309
310References
311**********
312
313* `CPU Documentation <https://www.altera.com/en_US/pdfs/literature/hb/nios2/n2cpu-nii5v1gen2.pdf>`_
314* `Nios II Processor Booting Methods in MAX 10 FPGA Devices <https://www.altera.com/en_US/pdfs/literature/an/an730.pdf>`_
315* `Embedded Peripherals IP User Guide <https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/ug/ug_embedded_ip.pdf>`_
316* `MAX 10 FPGA Configuration User Guide <https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/hb/max-10/ug_m10_config.pdf>`_
317* `MAX 10 FPGA Development Kit User Guide <https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/ug/ug-max10m50-fpga-dev-kit.pdf>`_
318* `Nios II Command-Line Tools <https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/hb/nios2/edh_ed51004.pdf>`_
319* `Quartus II Scripting Reference Manual <https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/manual/tclscriptrefmnl.pdf>`_
320
321
322.. _Altera Lite Distribution: https://www.intel.com/content/www/us/en/collections/products/fpga/software/downloads.html
323