Merge branch 'master' of https://github.com/texane/stlink
[fw/stlink] / doc / tutorial / tutorial.tex
index f0617368af6aa69c5954a93946c8929ba451916d..c3615df7818a1dad9f4d08a35ae8639217d9b42d 100644 (file)
@@ -24,7 +24,7 @@
 \section{Overview}
 \paragraph{}
 This guide details the use of STMicroelectronics STM32 discovery kits in
 \section{Overview}
 \paragraph{}
 This guide details the use of STMicroelectronics STM32 discovery kits in
-an opensource environment.
+an open source environment.
 
 
 \newpage
 
 
 \newpage
@@ -48,51 +48,81 @@ This documentation assumes the toolchains is installed in a \$TOOLCHAIN\_PATH.
 
 \section{Installing STLINK}
 \paragraph{}
 
 \section{Installing STLINK}
 \paragraph{}
-STLINK is an opensource software to program and debug the discovery kits. Those
+STLINK is open source software to program and debug ST's STM32 Discovery kits. Those
 kits have an onboard chip that translates USB commands sent by the host PC into
 kits have an onboard chip that translates USB commands sent by the host PC into
-JTAG commands. This chip is called STLINK, which is confusing since the software
-has the same name. It comes into 2 versions (STLINK v1 and v2). From a software
-point of view, those version differ only in the transport layer used to communicate
-(v1 uses SCSI passthru commands, while v2 uses raw USB).
+JTAG/SWD commands. This chip is called STLINK, (yes, isn't that confusing? suggest a better
+name!)  and comes in 2 versions (STLINK v1 and v2). From a software
+point of view, those versions differ only in the transport layer used to communicate
+(v1 uses SCSI passthru commands, while v2 uses raw USB).  From a user point of view, they 
+are identical. 
+
+\paragraph{}
+Before continuing, the following dependencies must be met:
+\begin{itemize}
+\item libusb-1.0
+\end{itemize}
+
+\paragraph{}
+STLINK should run on any system meeting the above constraints.
+
 \paragraph{}
 The STLINK software source code is retrieved using:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
 \paragraph{}
 The STLINK software source code is retrieved using:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
-git clone https://github.com/texane/stlink stlink.git
+$> git clone https://github.com/texane/stlink stlink.git
 \end{lstlisting}
 \end{small}
 
 \paragraph{}
 \end{lstlisting}
 \end{small}
 
 \paragraph{}
-The GDB server is called st-util and is built using:\\
+Everything can be built from the top directory:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
 \begin{small}
 \begin{lstlisting}[frame=tb]
-$> cd stlink.git; make ;
+$> cd stlink.git
+$> make 
 \end{lstlisting}
 \end{small}
 \end{lstlisting}
 \end{small}
+It includes:
+\begin{itemize}
+\item a communication library (stlink.git/libstlink.a),
+\item a GDB server (stlink.git/gdbserver/st-util),
+\item a flash manipulation tool (stlink.git/flash/flash).
+\end{itemize}
 
 
 \newpage
 
 
 \newpage
-
-\section{Building and running a program}
+\section{Building and running a program in SRAM}
+\paragraph{}
 A simple LED blinking example is provided in the example directory. It is built using:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
 A simple LED blinking example is provided in the example directory. It is built using:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
-cd stlink.git/example ;
-PATH=$TOOLCHAIN_PATH/bin:$PATH make ;
+cd stlink.git/example/blink ;
+PATH=$TOOLCHAIN_PATH/bin:$PATH make
 \end{lstlisting}
 \end{small}
 \end{lstlisting}
 \end{small}
+This builds three files, one for each of the Discovery boards currently
+available, linked to run from SRAM. (So no risk of overwriting anything you didn't mean to) 
+These blink examples can safely be used to verify that:
+
+\begin{itemize}
+\item Your installed toolchain is capable of compiling for cortex M3/M4 targets
+\item stlink is functional
+\item Your arm-none-eabi-gdb is functional
+\item Your board is functional
+\end{itemize}
 
 \paragraph{}
 
 \paragraph{}
-A GDB server must be start to interact with the STM32.
-Depending on the discovery kit you are using, you must
-run one of the 2 commands:\\
+A GDB server must be started to interact with the STM32. Depending on the discovery kit you
+are using, you must run one of the 2 commands:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
 \begin{small}
 \begin{lstlisting}[frame=tb]
-# STM32VL discovery kit
-$> sudo ./st-util /dev/sg2
+# STM32VL discovery kit (onboard ST-link)
+$> ./st-util --stlinkv1
+
+# STM32L or STM32F4 discovery kit (onboard ST-link/V2)
+$> ./st-util 
 
 
-# STM32L discovery kit
-$> sudo ./st-util
+# Full help for other options (listen port, version)
+$> ./st-util --help
 \end{lstlisting}
 \end{small}
 
 \end{lstlisting}
 \end{small}
 
@@ -113,18 +143,134 @@ $> target extended localhost:4242
 \end{small}
 
 \paragraph{}
 \end{small}
 
 \paragraph{}
-To load the program, use:\\
+By default, the program was linked such that the base address is 0x20000000. From the architecture
+memory map, GDB knows this address belongs to SRAM. To load the program in SRAM, simply use:\\
+\begin{small}
+\begin{lstlisting}[frame=tb]
+$> # Choose one as appropriate for your Discovery kit
+$> load blink_32L.elf | load blink_32VL.elf | load blink_F4.elf
+\end{lstlisting}
+\end{small}
+
+\paragraph{}
+GDB automatically set the PC register to the correct value, 0x20000000 in this case. Then, you
+can run the program using:\\
+\begin{small}
+\begin{lstlisting}[frame=tb]
+$> continue
+\end{lstlisting}
+\end{small}
+
+\paragraph{}
+All the LEDs on the board should now be blinking in time (those leds are near the user and reset buttons).
+
+\newpage
+\section{Building and flashing a program}
+\paragraph{}
+FLASH memory reading and writing is done by a separate tool, as shown below:\\
+\begin{small}
+\begin{lstlisting}[frame=tb]
+# change to the flash tool directory
+$> cd stlink.git/flash ;
+
+# stlinkv1 command to read 4096 from flash into out.bin
+$> ./flash read v1 out.bin 0x8000000 4096
+
+# stlinkv2 command
+$> ./flash read out.bin 0x8000000 4096
+
+# stlinkv1 command to write the file in.bin into flash
+$> ./flash write v1 in.bin 0x8000000
+
+# stlinkv2 command
+$> ./flash write in.bin 0x8000000
+\end{lstlisting}
+\end{small}
+
+\paragraph{}
+A LED blinking example is provided:\\
+\begin{small}
+\begin{lstlisting}[frame=tb]
+# build the example, resulting in blink.bin
+$> cd stlink.git/example/blink_flash
+$> PATH=$TOOLCHAIN_PATH:$PATH make CONFIG_STM32L_DISCOVERY=1
+
+# write blink.bin into FLASH
+$> sudo ./flash write blink.bin 0x08000000
+\end{lstlisting}
+\end{small}
+
+\paragraph{}
+Upon reset, the board LEDs should be blinking.
+
+\newpage
+\section{Building and installing the CHIBIOS kernel}
+\paragraph{}
+CHIBIOS is an open source RTOS. More information can be found on the project website:
+\begin{center}
+http://www.chibios.org/dokuwiki/doku.php
+\end{center}
+
+\paragraph{}
+It supports several boards, including the STM32L DISCOVERY kit:
+\begin{center}
+http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:stm32l\_discovery
+\end{center}
+
+\paragraph{}
+The installation procedure is detailed below:\\
+\begin{small}
+\begin{lstlisting}[frame=tb]
+# checkout and build CHIBIOS for STM32L DISCOVERY kits
+svn checkout https://chibios.svn.sourceforge.net/svnroot/chibios/trunk
+cd chibios/trunk/demos/ARMCM3-STM32L152-DISCOVERY
+PATH=$TOOLCHAIN_PATH:$PATH make
+
+# flash the image into STM32L
+sudo ./flash write build/ch.bin 0x08000000
+\end{lstlisting}
+\end{small}
+
+\newpage
+\section{Notes}
+
+\subsection{Disassembling THUMB code in GDB}
+\paragraph{}
+By default, the disassemble command in GDB operates in ARM mode. The programs running on CORTEX-M3
+are compiled in THUMB mode. To correctly disassemble them under GDB, uses an odd address. For instance,
+if you want to disassemble the code at 0x20000000, use:\\
+\begin{small}
+\begin{lstlisting}[frame=tb]
+$> disassemble 0x20000001
+\end{lstlisting}
+\end{small}
+
+
+\subsection{libstm32l\_discovery}
+\paragraph{}
+The repository includes the STM32L discovery library source code from ST original firmware packages,
+available here:\\
+\begin{small}
+\begin{lstlisting}[frame=tb]
+http://www.st.com/internet/evalboard/product/250990.jsp#FIRMWARE
+\end{lstlisting}
+\end{small}
+
+\paragraph{}
+It is built using:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
 \begin{small}
 \begin{lstlisting}[frame=tb]
-$> load blinky.elf
+$> cd stlink.git/example/libstm32l_discovery/build
+$> make
 \end{lstlisting}
 \end{small}
 
 \paragraph{}
 \end{lstlisting}
 \end{small}
 
 \paragraph{}
-Then, you can run the program using:\\
+An example using the library can be built using:\\
 \begin{small}
 \begin{lstlisting}[frame=tb]
 \begin{small}
 \begin{lstlisting}[frame=tb]
-$> run
+$> cd stlink.git/example/lcd
+$> make
 \end{lstlisting}
 \end{small}
 
 \end{lstlisting}
 \end{small}