Difference between revisions of "STM32QuickStartScript"

From eLinux.org
Jump to: navigation, search
m
Line 1: Line 1:
<nowiki>
+
  #!/bin/bash
#!/bin/bash
+
 
# Set this to your chosen install destination
+
  # Set this to your chosen install destination
TOPDIR=""
+
  TOPDIR=""
# Set this to the location of your kernel source
+
  # Set this to the location of your kernel source
KERNELDIR=""
+
  KERNELDIR=""
# Either uncomment one of the boards below, or add your own"
+
  # Either uncomment one of the boards below, or add your own"
BOARD=""
+
  BOARD=""
#BOARD="stm32f429discovery"
+
  #BOARD="stm32f429discovery"
#BOARD="stm32f469discovery"
+
  #BOARD="stm32f469discovery"
 
+
 
OPENOCDDIR="/usr/local/share/openocd"
+
  # DEBUG DEBUG DEBUG
OPENOCDCFG=$OPENOCDDIR/scripts/board/$BOARD.cfg
+
  TOPDIR=$HOME/projects/stm32
 
+
  KERNELDIR=$HOME/projects/linaro/kernel
startupchecks()
+
  BOARD="stm32f469discovery"
{
+
  # DEBUG DEBUG DEBUG
    if [ ! -f /etc/lsb-release ]; then
+
 
echo "This does not appear to be a Debian machine"
+
  OPENOCDDIR="/usr/local/share/openocd"
echo "Please install the OSELAS toolchain manually"
+
  OPENOCDCFG=$OPENOCDDIR/scripts/board/$BOARD.cfg
exit
+
 
    fi
+
  startupchecks()
 
+
  {
    lsusb | grep STMicroelectronics
+
      if [ ! -f /etc/lsb-release ]; then
    if [ $? -ne 0 ]; then
+
  echo "This does not appear to be a Debian machine"
echo "Board not found. Is it plugged in?"
+
  echo "Please install the OSELAS toolchain manually"
exit
+
  exit
    fi
+
      fi
 
+
 
    if [ "$TOPDIR" == "" ]; then
+
      lsusb | grep STMicroelectronics
echo "Please set TOPDIR in $0"
+
      if [ $? -ne 0 ]; then
exit
+
  echo "Board not found. Is it plugged in?"
    fi
+
  exit
 
+
      fi
    if [ "$KERNELDIR" == "" ]; then
+
 
echo "Set the KERNELDIR in $0"
+
      if [ "$TOPDIR" == "" ]; then
exit
+
  echo "Please set TOPDIR in $0"
    fi
+
  exit
 
+
      fi
    if [ "$BOARD" == "" ]; then
+
 
echo "Uncomment or set BOARD in $0"
+
      if [ "$KERNELDIR" == "" ]; then
exit
+
  echo "Set the KERNELDIR in $0"
    fi
+
  exit
}
+
      fi
 
+
 
init()
+
      if [ "$BOARD" == "" ]; then
{
+
  echo "Uncomment or set BOARD in $0"
    if [ "$BOARD" == "stm32f429discovery" ]; then
+
  exit
BOARDAFBOOT=stm32f429i-disco
+
      fi
BOARDDTB=stm32f429-disco
+
  }
    elif [ "$BOARD" == "stm32f469discovery" ]; then
+
 
echo "###################################################"
+
  init()
echo "Don't forget to make the kernel changes detailed at"
+
  {
echo "  http://elinux.org/STM32#Mainline_Kernel"
+
      if [ "$BOARD" == "stm32f429discovery" ]; then
echo "###################################################"
+
  BOARDAFBOOT=stm32f429i-disco
echo "Hit return to acknowledge"
+
  BOARDDTB=stm32f429-disco
read
+
      elif [ "$BOARD" == "stm32f469discovery" ]; then
 
+
  echo "###################################################"
BOARDAFBOOT=stm32f469i-disco
+
  echo "Don't forget to make the kernel changes detailed at"
BOARDDTB=stm32f429-disco
+
  echo "  http://elinux.org/STM32#Mainline_Kernel"
#  BOARDDTB=stm32f469-disco  future
+
  echo "###################################################"
    else
+
  echo "Hit return to acknowledge"
echo "$BOARD is not supported by $0 - please add support"
+
  read
exit
+
 
    fi
+
  BOARDAFBOOT=stm32f469i-disco
 
+
  BOARDDTB=stm32f429-disco
    mkdir -p $TOPDIR
+
  #  BOARDDTB=stm32f469-disco  future
    cd $TOPDIR
+
      else
}
+
  echo "$BOARD is not supported by $0 - please add support"
 
+
  exit
openocd()
+
      fi
{
+
 
    if [ "$(which openocd)" == "" ] && [ ! -d openocd ]; then
+
      mkdir -p $TOPDIR
echo "Installing OpenOCD from source"
+
      cd $TOPDIR
git clone git://git.code.sf.net/p/openocd/code openocd
+
  }
cd openocd
+
 
./bootstrap && ./configure && make && sudo make install
+
  openocd()
cd ..
+
  {
    elif [ ! -f $OPENOCDCFG ]; then
+
      if [ "$(which openocd)" == "" ] && [ ! -d openocd ]; then
echo "$OPENOCDCFG not found. Perhaps installed OpenOCD is out of date"
+
  echo "Installing OpenOCD from source"
exit
+
  git clone git://git.code.sf.net/p/openocd/code openocd
    else
+
  cd openocd
echo "OpenOCD is already installed and appears to support your hardware"
+
  ./bootstrap && ./configure && make && sudo make install
    fi
+
  cd ..
}
+
      elif [ ! -f $OPENOCDCFG ]; then
 
+
  echo "$OPENOCDCFG not found. Perhaps installed OpenOCD is out of date"
stlink()
+
  exit
{
+
      else
    if [ "$(which st-flash)" == "" ] && [ ! -d openocd ]; then
+
  echo "OpenOCD is already installed and appears to support your hardware"
echo "Building STLink from source"
+
      fi
git clone https://github.com/texane/stlink.git stlink
+
  }
cd stlink
+
 
 
+
  stlink()
./autogen.sh
+
  {
./configure
+
      if [ "$(which st-flash)" == "" ] && [ ! -d openocd ]; then
make
+
  echo "Building STLink from source"
echo "Installing STLink"
+
  git clone https://github.com/texane/stlink.git stlink
make install  
+
  cd stlink
 
+
 
echo "Installing STLink udev rules"
+
  ./autogen.sh
sudo cp 49-stlinkv*.rules /etc/udev/rules.d
+
  ./configure
sudo udevadm control --reload-rules
+
  make
sudo udevadm trigger
+
  echo "Installing STLink"
cd ..
+
  make install  
    else
+
 
echo "STLink is already installed"
+
  echo "Installing STLink udev rules"
    fi
+
  sudo cp 49-stlinkv*.rules /etc/udev/rules.d
}
+
  sudo udevadm control --reload-rules
 
+
  sudo udevadm trigger
bmcompiler()
+
  cd ..
{
+
      else
    # Bare Metal compiler
+
  echo "STLink is already installed"
    if [ ! -d gcc-arm-none-eabi-4_9-2014q4 ]; then
+
      fi
echo "Installing the Bare Metal compliler [for building Bootloader and Kernel]"
+
  }
BAREMETALTAR=gcc-arm-none-eabi-4_9-2014q4-20141203-linux.tar.bz2
+
 
URL=https://launchpad.net/gcc-arm-embedded/4.9/4.9-2014-q4-major/+download/$BAREMETALTAR
+
  bmcompiler()
wget $URL
+
  {
tar -xf $BAREMETALTAR
+
      # Bare Metal compiler
rm $BAREMETALTAR
+
      if [ ! -d gcc-arm-none-eabi-4_9-2014q4 ]; then
PATH=$PATH:$PWD/gcc-arm-none-eabi-4_9-2014q4/bin
+
  echo "Installing the Bare Metal compliler [for building Bootloader and Kernel]"
    else
+
  BAREMETALTAR=gcc-arm-none-eabi-4_9-2014q4-20141203-linux.tar.bz2
echo "Bare metal complier already installed"
+
  URL=https://launchpad.net/gcc-arm-embedded/4.9/4.9-2014-q4-major/+download/$BAREMETALTAR
    fi
+
  wget $URL
}
+
  tar -xf $BAREMETALTAR
 
+
  rm $BAREMETALTAR
bootloader()
+
  PATH=$PATH:$PWD/gcc-arm-none-eabi-4_9-2014q4/bin
{
+
      else
    if [ ! -d afboot-stm32 ]; then
+
  echo "Bare metal complier already installed"
echo "Downloading bootloader"
+
      fi
git clone https://github.com/mcoquelin-stm32/afboot-stm32.git
+
  }
    else
+
 
echo "Bootloader already downloaded"
+
  bootloader()
    fi
+
  {
    echo "Building and flashing bootloader"
+
      if [ ! -d afboot-stm32 ]; then
    cd afboot-stm32
+
  echo "Downloading bootloader"
    make $BOARDAFBOOT
+
  git clone https://github.com/mcoquelin-stm32/afboot-stm32.git
    make flash_$BOARDAFBOOT
+
      else
    cd ..
+
  echo "Bootloader already downloaded"
}
+
      fi
 
+
      echo "Building and flashing bootloader"
cpio()
+
      cd afboot-stm32
{
+
      make $BOARDAFBOOT
    # Pre-built userspace
+
      make flash_$BOARDAFBOOT
    CPIO=$PWD/Stm32_mini_rootfs.cpio
+
      cd ..
    if [ ! -f Stm32_mini_rootfs.cpio ]; then
+
  }
echo "Downloading a pre-built userspace CPIO (RAMFS)"
+
 
wget http://elinux.org/images/5/51/$CPIO.bz2
+
  cpio()
bunzip2 $CPIO.bz2
+
  {
    else
+
      # Pre-built userspace
echo "Already have the desired CPIO"
+
      CPIO=$PWD/Stm32_mini_rootfs.cpio
    fi
+
      if [ ! -f Stm32_mini_rootfs.cpio ]; then
}
+
  echo "Downloading a pre-built userspace CPIO (RAMFS)"
 
+
  wget http://elinux.org/images/5/51/$CPIO.bz2
kernel()
+
  bunzip2 $CPIO.bz2
{
+
      else
    KERNELBUILDDIR=build-stm32
+
  echo "Already have the desired CPIO"
    echo "Building the kernel - output will be in $KERNELDIR/$KERNELBUILDDIR"
+
      fi
    cd $KERNELDIR
+
  }
    BRANCH=`git branch | grep "*" | sed 's/* //'`
+
 
    echo -e "\nIf $BRANCH is not the correct branch Ctrl+C now, else hit return"
+
  kernel()
    read
+
  {
    CFLAGS="ARCH=arm CROSS_COMPILE=arm-none-eabi- KBUILD_OUTPUT=$KERNELBUILDDIR"
+
      KERNELBUILDDIR=build-stm32
    make $CFLAGS stm32_defconfig
+
      echo "Building the kernel - output will be in $KERNELDIR/$KERNELBUILDDIR"
    yes "" | make $CFLAGS oldconfig
+
      cd $KERNELDIR
    ./scripts/config --file $KERNELBUILDDIR/.config \
+
      BRANCH=`git branch | grep "*" | sed 's/* //'`
--set-str INITRAMFS_ROOT_UID 0 \
+
      echo -e "\nIf $BRANCH is not the correct branch Ctrl+C now, else hit return"
--enable BLK_DEV_INITRD \
+
      read
--set-str INITRAMFS_SOURCE $CPIO \
+
      CFLAGS="ARCH=arm CROSS_COMPILE=arm-none-eabi- KBUILD_OUTPUT=$KERNELBUILDDIR"
--enable RD_GZIP \
+
      make $CFLAGS stm32_defconfig
--enable INITRAMFS_COMPRESSION_GZIP
+
      yes "" | make $CFLAGS oldconfig
    make $CFLAGS
+
      ./scripts/config --file $KERNELBUILDDIR/.config \
    cd ..
+
  --set-str INITRAMFS_ROOT_UID 0 \
}
+
  --enable BLK_DEV_INITRD \
 
+
  --set-str INITRAMFS_SOURCE $CPIO \
flash()
+
  --enable RD_GZIP \
{
+
  --enable INITRAMFS_COMPRESSION_GZIP
    DTB=$KERNELDIR/$KERNELBUILDDIR/arch/arm/boot/dts/$BOARDDTB.dtb
+
      make $CFLAGS
    echo "Flashing DTB ($DTB)"
+
      cd ..
    st-flash --reset write $KERNELDIR/$KERNELBUILDDIR/arch/arm/boot/dts/$BOARDDTB.dtb 0x08004000
+
  }
 
+
 
    KERNEL=$KERNELDIR/$KERNELBUILDDIR/arch/arm/boot/xipImage
+
  flash()
    echo "Flashing Kernel ($KERNEL)"
+
  {
    st-flash --reset write $KERNEL 0x08008000
+
      DTB=$KERNELDIR/$KERNELBUILDDIR/arch/arm/boot/dts/$BOARDDTB.dtb
}
+
      echo "Flashing DTB ($DTB)"
 
+
      st-flash --reset write $KERNELDIR/$KERNELBUILDDIR/arch/arm/boot/dts/$BOARDDTB.dtb 0x08004000
startupchecks
+
 
init
+
      KERNEL=$KERNELDIR/$KERNELBUILDDIR/arch/arm/boot/xipImage
openocd
+
      echo "Flashing Kernel ($KERNEL)"
stlink
+
      st-flash --reset write $KERNEL 0x08008000
bmcompiler
+
  }
bootloader
+
 
cpio
+
  startupchecks
kernel
+
  init
flash
+
  openocd
 
+
  stlink
echo "################################################################################"
+
  bmcompiler
echo "Install OSELAS toolchain if you wish to build your own userspace rootfs/apps"
+
  bootloader
echo "  echo \"deb http://debian.pengutronix.de/debian/ sid main contrib non-free\" | \\"
+
  cpio
echo "    sudo tee /etc/apt/sources.list.d/pengutronix.list"
+
  kernel
echo "  sudo apt-get update"
+
  flash
echo "  sudo apt-get install \\"
+
 
echo "    oselas.toolchain-2012.12.1-arm-cortexm3-uclinuxeabi-\\"
+
  echo "################################################################################"
echo "    gcc-4.7.2-uclibc-0.9.33.2-binutils-2.22-kernel-3.6-sanitized"
+
  echo "Install OSELAS toolchain if you wish to build your own userspace rootfs/apps"
echo "################################################################################"
+
  echo "  echo \"deb http://debian.pengutronix.de/debian/ sid main contrib non-free\" | \\"
echo "Add the following to your ~/.bashrc file to run manually"
+
  echo "    sudo tee /etc/apt/sources.list.d/pengutronix.list"
echo "  export PATH=\$PATH:$PWD/gcc-arm-none-eabi-4_9-2014q4/bin"
+
  echo "  sudo apt-get update"
echo "################################################################################"
+
  echo "  sudo apt-get install \\"
</nowiki>
+
  echo "    oselas.toolchain-2012.12.1-arm-cortexm3-uclinuxeabi-\\"
 +
  echo "    gcc-4.7.2-uclibc-0.9.33.2-binutils-2.22-kernel-3.6-sanitized"
 +
  echo "################################################################################"
 +
  echo "Add the following to your ~/.bashrc file to run manually"
 +
  echo "  export PATH=\$PATH:$PWD/gcc-arm-none-eabi-4_9-2014q4/bin"
 +
  echo "################################################################################"

Revision as of 10:43, 15 February 2016

  #!/bin/bash
 
 # Set this to your chosen install destination
 TOPDIR=""
 # Set this to the location of your kernel source
 KERNELDIR=""
 # Either uncomment one of the boards below, or add your own"
 BOARD=""
 #BOARD="stm32f429discovery"
 #BOARD="stm32f469discovery"
 
 # DEBUG DEBUG DEBUG
 TOPDIR=$HOME/projects/stm32
 KERNELDIR=$HOME/projects/linaro/kernel
 BOARD="stm32f469discovery"
 # DEBUG DEBUG DEBUG
 
 OPENOCDDIR="/usr/local/share/openocd"
 OPENOCDCFG=$OPENOCDDIR/scripts/board/$BOARD.cfg
 
 startupchecks()
 {
     if [ ! -f /etc/lsb-release ]; then
 	echo "This does not appear to be a Debian machine"
 	echo "Please install the OSELAS toolchain manually"
 	exit
     fi
 
     lsusb | grep STMicroelectronics
     if [ $? -ne 0 ]; then
 	echo "Board not found. Is it plugged in?"
 	exit
     fi
 
     if [ "$TOPDIR" == "" ]; then
 	echo "Please set TOPDIR in $0"
 	exit
     fi
 
     if [ "$KERNELDIR" == "" ]; then
 	echo "Set the KERNELDIR in $0"
 	exit
     fi
 
     if [ "$BOARD" == "" ]; then
 	echo "Uncomment or set BOARD in $0"
 	exit
     fi
 }
 
 init()
 {
     if [ "$BOARD" == "stm32f429discovery" ]; then
 	BOARDAFBOOT=stm32f429i-disco
 	BOARDDTB=stm32f429-disco
     elif [ "$BOARD" == "stm32f469discovery" ]; then
 	echo "###################################################"
 	echo "Don't forget to make the kernel changes detailed at"
 	echo "  http://elinux.org/STM32#Mainline_Kernel"
 	echo "###################################################"
 	echo "Hit return to acknowledge"
 	read
 
 	BOARDAFBOOT=stm32f469i-disco
 	BOARDDTB=stm32f429-disco
 	#   BOARDDTB=stm32f469-disco  future
     else
 	echo "$BOARD is not supported by $0 - please add support"
 	exit
     fi
 
     mkdir -p $TOPDIR
     cd $TOPDIR
 }
 
 openocd()
 {
     if [ "$(which openocd)" == "" ] && [ ! -d openocd ]; then
 	echo "Installing OpenOCD from source"
 	git clone git://git.code.sf.net/p/openocd/code openocd
 	cd openocd
 	./bootstrap && ./configure && make && sudo make install
 	cd ..
     elif [ ! -f $OPENOCDCFG ]; then
 	echo "$OPENOCDCFG not found. Perhaps installed OpenOCD is out of date"
 	exit
     else
 	echo "OpenOCD is already installed and appears to support your hardware"
     fi
 }
 
 stlink()
 {
     if [ "$(which st-flash)" == "" ] && [ ! -d openocd ]; then
 	echo "Building STLink from source"
 	git clone https://github.com/texane/stlink.git stlink
 	cd stlink
 
 	./autogen.sh
 	./configure
 	make
 	echo "Installing STLink"
 	make install 
 
 	echo "Installing STLink udev rules"
 	sudo cp 49-stlinkv*.rules /etc/udev/rules.d
 	sudo udevadm control --reload-rules
 	sudo udevadm trigger
 	cd ..
     else
 	echo "STLink is already installed"
     fi
 }
 
 bmcompiler()
 {
     # Bare Metal compiler
     if [ ! -d gcc-arm-none-eabi-4_9-2014q4 ]; then
 	echo "Installing the Bare Metal compliler [for building Bootloader and Kernel]"
 	BAREMETALTAR=gcc-arm-none-eabi-4_9-2014q4-20141203-linux.tar.bz2
 	URL=https://launchpad.net/gcc-arm-embedded/4.9/4.9-2014-q4-major/+download/$BAREMETALTAR
 	wget $URL
 	tar -xf $BAREMETALTAR
 	rm $BAREMETALTAR
 	PATH=$PATH:$PWD/gcc-arm-none-eabi-4_9-2014q4/bin
     else
 	echo "Bare metal complier already installed"
     fi
 }
 
 bootloader()
 {
     if [ ! -d afboot-stm32 ]; then
 	echo "Downloading bootloader"
 	git clone https://github.com/mcoquelin-stm32/afboot-stm32.git
     else
 	echo "Bootloader already downloaded"
     fi
     echo "Building and flashing bootloader"
     cd afboot-stm32
     make $BOARDAFBOOT
     make flash_$BOARDAFBOOT
     cd ..
 }
 
 cpio()
 {
     # Pre-built userspace
     CPIO=$PWD/Stm32_mini_rootfs.cpio
     if [ ! -f Stm32_mini_rootfs.cpio ]; then
 	echo "Downloading a pre-built userspace CPIO (RAMFS)"
 	wget http://elinux.org/images/5/51/$CPIO.bz2
 	bunzip2 $CPIO.bz2
     else
 	echo "Already have the desired CPIO"
     fi
 }
 
 kernel()
 {
     KERNELBUILDDIR=build-stm32
     echo "Building the kernel - output will be in $KERNELDIR/$KERNELBUILDDIR"
     cd $KERNELDIR
     BRANCH=`git branch | grep "*" | sed 's/* //'`
     echo -e "\nIf $BRANCH is not the correct branch Ctrl+C now, else hit return"
     read
     CFLAGS="ARCH=arm CROSS_COMPILE=arm-none-eabi- KBUILD_OUTPUT=$KERNELBUILDDIR"
     make $CFLAGS stm32_defconfig
     yes "" | make $CFLAGS oldconfig
     ./scripts/config --file $KERNELBUILDDIR/.config \
 	--set-str INITRAMFS_ROOT_UID 0 \
 	--enable BLK_DEV_INITRD \
 	--set-str INITRAMFS_SOURCE $CPIO \
 	--enable RD_GZIP \
 	--enable INITRAMFS_COMPRESSION_GZIP
     make $CFLAGS
     cd ..
 }
 
 flash()
 {
     DTB=$KERNELDIR/$KERNELBUILDDIR/arch/arm/boot/dts/$BOARDDTB.dtb
     echo "Flashing DTB ($DTB)"
     st-flash --reset write $KERNELDIR/$KERNELBUILDDIR/arch/arm/boot/dts/$BOARDDTB.dtb 0x08004000
 
     KERNEL=$KERNELDIR/$KERNELBUILDDIR/arch/arm/boot/xipImage
     echo "Flashing Kernel ($KERNEL)"
     st-flash --reset write $KERNEL 0x08008000
 }
 
 startupchecks
 init
 openocd
 stlink
 bmcompiler
 bootloader
 cpio
 kernel
 flash
 
 echo "################################################################################"
 echo "Install OSELAS toolchain if you wish to build your own userspace rootfs/apps"
 echo "  echo \"deb http://debian.pengutronix.de/debian/ sid main contrib non-free\" | \\"
 echo "    sudo tee /etc/apt/sources.list.d/pengutronix.list"
 echo "  sudo apt-get update"
 echo "  sudo apt-get install \\"
 echo "    oselas.toolchain-2012.12.1-arm-cortexm3-uclinuxeabi-\\"
 echo "    gcc-4.7.2-uclibc-0.9.33.2-binutils-2.22-kernel-3.6-sanitized"
 echo "################################################################################"
 echo "Add the following to your ~/.bashrc file to run manually"
 echo "  export PATH=\$PATH:$PWD/gcc-arm-none-eabi-4_9-2014q4/bin"
 echo "################################################################################"