Feb 28, 2014

Bash script for compile programs to ARM CORTEX M4F with StellarisWare

Target: LM4F120 Series of ARM Cortex-M4 (Texas Instruments)
OS Platform tested at the moment: Linux Ubuntu 13.04


Hi, in my last post i presented a script to install all tools to start developing in ARM on linux. Now i would like to present a script compiler that helped me when the makefiles didn't work well. I did this to copy the files required to compile from the stellaris folder and after compile all .c files from the target folder pointed by the user. The script asks for the source folder of your project (ex. ~/test_arm), the stellarisware folder library and the name of the main file (test.c). If all occur ok, you will see threes "Complete!", if some "Failed!" appear you will see a file named log_compiler.txt created. In this log_compiler.txt you can see what happened with the compilation. If you have some problem to use, just leave a comment and a i will be greatful to answer you.


Step 1. Create the file:

Just open any text editor, like gedit and paste the script below, after save the file with "name.sh"
SCRIPT:
#!/bin/bash
#Ânderson Ignácio da Silva -2014/1 - www.esplatforms.blogpsot.com.br
#to run just type in terminal sudo chmod  +x compilator.sh, add your password and execute (./compilator.sh)
sudo clear
echo -e "\e[4m\e[1m\e[34m\tCompiler for ARM Cortex M4F - STELLARIS LM4F120 Ver. 1.0"
echo -e "\e[31m\e[1m\e[24m\nAuthor:\e[21m\e[97m Ânderson Ignácio da Silva - http://esplatforms.blogspot.com.br/"
echo -e "\e[31m\e[1m\e[24mOS Tested:\e[21m\e[97m Ubuntu 13.04 or higher"
echo -e "\e[31m\e[1m\e[24mFree source code for everyone. :)\e[21m\e[97m"

echo -n "Your current directory:"
pwd 
read -e -p "Please type the directory folder of the project:" srcDir 
if [[ $srcDir == *~* ]]
then
  srcDir=$(sed -e 's/~//' <<<$srcDir)
  srcDir=$HOME$srcDir
fi
echo $srcDir
read -e -p "Please type the directory folder of the stellaris library:" srcLib 
if [[ $srcLib == *~* ]]
then
  srcLib=$(sed -e 's/~//' <<<$srcLib)
  srcLib=$HOME$srcLib
fi
cd "$srcDir"

echo $srcLib
read -e -p "Please type the name of the main file project(*.c):" srcMainFile

echo "LOG - USER:$USER" &>  log_compiler.txt
echo "      DATE:$(date)" &>> log_compiler.txt

mainC=`echo $srcMainFile | cut -f1 -d'.'`
cp "$srcLib"boards/ek-lm4f120xl/blinky/blinky.ld loader.ld
cp "$srcLib"boards/ek-lm4f120xl/blinky/startup_gcc.c .
files=$(ls *.c | tr '\n' ' ' | tr '\r' ' ' )

echo -ne "Compiling"
for i in {1..10}
do
 sleep .1s
 echo -ne "."
done

arm-none-eabi-gcc $files -g -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 \
-mfloat-abi=softfp -Os -ffunction-sections -fdata-sections -MD -std=c99 \
-Wall -pedantic -DPART_LM4F120H5QR -c -I$srcLib -DTARGET_IS_BLIZZARD_RA1 &>> log_compiler.txt

sleep .1s

if  grep -qs "Failed" log_compiler.txt | grep -qs "undefined" log_compiler.txt; then 
 echo -e "\e[31mFailed!\e[97m"
 exit 0
else
 echo -e "\e[34mComplete!\e[97m"
fi

echo -ne "Generating output"
for i in {1..10}
do
 sleep .1s
 echo -ne "."
done

object=$(ls *.o | tr '\n' ' ' | tr '\r' ' ')
arm-none-eabi-ld -T loader.ld --entry ResetISR -o a.out $object --gc-sections &>> log_compiler.txt

sleep .1s

if  grep -qs "Failed" log_compiler.txt | grep -qs "undefined" log_compiler.txt; then 
 echo -e "\e[31mFailed!\e[97m"
 exit 0
else
 echo -e "\e[34mComplete!\e[97m"
fi

echo -ne "Generating bin"
for i in {1..4}
do
 sleep .1s
 echo -ne "."
done

arm-none-eabi-objcopy -O binary a.out "$mainC".bin &>> log_compiler.txt

sleep .1s

if  grep -qs "Failed" log_compiler.txt | grep -qs "undefined" log_compiler.txt; then 
 echo -e "\e[31mFailed!\e[97m"
 exit 0
else
 echo -e "\e[34mComplete!\e[97m"
fi

echo -ne "Cleaning"
for i in {1..4}
do
 sleep .1s
 echo -ne "."
done

rm -rf "$mainC".o "$mainC".d a.out startup_gcc* loader.ld

sleep .1s

if  grep -qs "Failed" log_compiler.txt | grep -qs "undefined" log_compiler.txt; then 
 echo -e "\e[31mFailed!\e[97m"
 exit 0
else
 echo -e "\e[34mComplete!\e[97m"
fi

echo -e "\n\e[34m Compilation Finished!, for details see log_compiler.txt\e[97m"

exit 0



Step 2. Change the permission to execute:

In this step you just need to change the permission to execute the file into your system and for this open the terminal to put the command below:
sudo chmod +u name.sh


Step 3. Executing:

Before to execute i almost lost to advice, but you need to download the Stellaris Ware(SW-EK-LM4F120XL) from this link http://www.ti.com/tool/sw-ek-lm4f120xl and put them into your ~/Downloads directory. And for finish, to execute the script just call them with "./name.sh" like below:
./name.sh

Tks All.

Bash Script to install all tools for ARM - Cortex M4F with StellarisWare on Linux


Target: LM4F120 Series of ARM Cortex-M4 (Texas Instruments)
OS Platform tested at the moment: Linux Ubuntu 13.04


Hi, actually i have been busy with some projects and i don't have so much time to write posts, but don't change the page now because i will present the "magic" script that i have developed. Yeah, you could think that i'm stupid by saying that's a magic script but i would like to note that for new users like me, this can help a lot and with a little changes you can use in other kind of debian distributions systems like PureOS, Debian, Stormix, Commodore OS, Ångström, etc...I wrote this to help me install all tools required to build programs for ARM cortex M4 with Stellaris Ware. The script is pretty simple, it creates a folder to put all files from the repos and make them of recursive form, if you need some help with a problem or error or the script takes a long time to forward, comment and a i will be greatful to answer. This script runs in BASH and you just need to open the terminal (ctrl + alt + t) and change the permission to execute the file. I will show how to do this in the steps below:

Step 1. Create the file:

Just open any text editor, like gedit and paste the script below, after save the file with "name.sh"
SCRIPT:
#!/bin/bash
#Ânderson Ignácio da Silva -2014/1 - www.esplatforms.blogpsot.com.br
#to run just type in terminal sudo chmod  +x arm_installer_stellaris.sh, add your password and execute (./arm_ins....)
var=$USER 
sudo clear

AdjustSystem()
{
  #n-continue same line // e- special chars
  echo -ne "\nAdding repository source of gcc arm..." 
  # argument -y no asking // &-redirect without echo // > - copy return to this file replacing all // >> - > - copy return to this file without replace
  sudo add-apt-repository ppa:terry.guo/gcc-arm-embedded -y &> ~/log_report_installer_arm.txt 
  if  grep -qs "Failed" ~/log_report_installer_arm.txt; then 
    echo -e "\e[31mFailed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi

  echo -n "Updating source repositories..."
  sudo apt-get update -y &> /dev/null
  echo -e "\e[34mComplete!\e[97m"
 
  echo -n "Installing gcc for arm..."
  sudo apt-get install gcc-arm-none-eabi -y &> ~/log_report_installer_arm.txt
  if  grep -qs "E: Unable to locate package gcc-arm-none-eabi" ~/log_report_installer_arm.txt; then 
    #Fix the reference in terry.guo repo changing saucy by raring
    sudo sed -i 's/saucy/raring/g' /etc/apt/sources.list.d/terry_guo-gcc-arm-embedded-saucy.list
    sudo apt-get update -y &> /dev/null
    sudo apt-get install gcc-arm-none-eabi -y &> ~/log_report_installer_arm.txt
    echo -e "\e[34mRepo bug fixed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi
}

StellarisInstaller()
{
  echo -en "\nCreating the source folder..."
  mkdir -p $srcFolder/stellarisware &> ~/log_report_installer_arm.txt
  if  grep -qs "Failed" ~/log_report_installer_arm.txt; then 
    echo -e "\e[31mFailed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi

  echo -en "Download \e[31m\e[1mStellaris Library(http://www.ti.com/tool/sw-ek-lm4f120xl)\e[21m\e[97m..."
  while true
  do
    if [ -f ~/Downloads/SW-EK-LM4F120XL-9453.exe ]; then
      echo -e "\e[34mComplete!\e[97m"
      break
    fi
  done

  sudo apt-get install -y unzip &>> /dev/null
  unzip -u ~/Downloads/SW-EK-LM4F120XL-9453.exe -d ~/$srcFolder/stellarisware &>> /dev/null

  echo -n "Making the library..."
  cd ~/$srcFolder/stellarisware
  make &> ~/log_report_installer_arm.txt
  if  grep -qs "Failed" ~/log_report_installer_arm.txt; then 
    echo -e "\e[31mFailed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi
}

lm4Install()
{
  echo -ne "\nInstalling some required tools..."
  sudo apt-get install libusb-1.0-0-dev -y &> ~/log_report_installer_arm.txt
  if  grep -qs "Failed" ~/log_report_installer_arm.txt; then 
    echo -e "\e[31mFailed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi

  echo -ne "Installing git tools..."
  sudo apt-get install git -y &> ~/log_report_installer_arm.txt
  if  grep -qs "Failed" ~/log_report_installer_arm.txt; then 
    echo -e "\e[31mFailed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi
  
  echo -ne "Clonning gitrepo of lm4tools..."
  cd ~/$srcFolder
  git clone https://github.com/utzig/lm4tools.git &> ~/log_report_installer_arm.txt
  if  grep -qs "Failed" ~/log_report_installer_arm.txt; then 
    echo -e "\e[31mFailed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi

  cd ~/$srcFolder/lm4tools/lm4flash
  echo -ne "Making tools..."
  make &> ~/log_report_installer_arm.txt
  if  grep -qs "Failed" ~/log_report_installer_arm.txt; then 
    echo -e "\e[31mFailed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi
}

autoInstall()
{
  echo -ne "Adding lm4flash to usr profile..."
  sudo echo "export PATH=$PATH:$HOME/$srcFolder/lm4tools/lm4flash" &>> ~/.profile 
  echo -e "\e[34mComplete!\e[97m"
 
  echo 'ATTRS{idVendor}=="1cbe", ATTRS{idProduct}=="00fd", GROUP="users", MODE="0660"' | \
  sudo tee /etc/udev/rules.d/99-stellaris-launchpad.rules &>> /dev/null
  sudo usermod -aG users users $var &>> /dev/null
}

openInstall()
{
  mkdir -p ~/$srcFolder/
  cd ~/$srcFolder/
  echo -ne "Cloning openOCD repositories..."
  git clone http://git.code.sf.net/p/openocd/code &> ~/log_report_installer_arm.txt
  if  grep -qs "Failed" ~/log_report_installer_arm.txt; then 
    echo -e "\e[31mFailed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi

  mv -f code openocd &>> /dev/null
  cd ~/$srcFolder/openocd
  echo -ne "Updating git repo and changing the branch..."
  git fetch http://openocd.zylin.com/openocd refs/changes/22/922/15 &>> /dev/null
  git checkout FETCH_HEAD &>> /dev/null
  echo -e "\e[34mComplete!\e[97m"

  echo -ne "Setting the module..."
  ./bootstrap &>> /dev/null
  ./configure --enable-maintainer-mode --enable-ti-icdi &>> /dev/null
  make &>> /dev/null
  echo -e "\e[34mComplete!\e[97m"

  echo -ne "Installing the openOCD..."
  sudo make install &> ~/log_report_installer_arm.txt
  if  grep -qs "Failed" ~/log_report_installer_arm.txt; then 
    echo -e "\e[31mFailed!\e[97m"
  else
    echo -e "\e[34mComplete!\e[97m"
  fi
}

echo -e "\e[4m\e[1m\e[34m\tInstaller for ARM Cortex M4F - STELLARIS LM4F120 Ver. 1.0"
echo -e "\e[31m\e[1m\e[24m\nAuthor:\e[21m\e[97m Ânderson Ignácio da Silva - http://esplatforms.blogspot.com.br/"
echo -e "\e[31m\e[1m\e[24mOS Tested:\e[21m\e[97m Ubuntu 13.04 or higher"
echo -e "\e[31m\e[1m\e[24mWARNING 1:\e[21m\e[97mRemember to put the stellaris library file (SW-EK-LM4F120XL-9453.exe)\ninto ~/Downloads!"
echo -e "\e[31m\e[1m\e[24mWARNING 2:\e[21m\e[97mVisit http://openocd.zylin.com/#/c/922/ to check the last updates and\nthen update the script of openOCD"
echo -e "\e[31m\e[1m\e[24mFree source code for everyone. :)\e[21m\e[97m"
read -e -p "Type folder name to install:" srcFolder

if [[ $srcFolder == *~* ]]
then
  srcFolder=$(sed -e 's/~//' <<<$srcFolder)
  srcFolder=$HOME$srcFolder
fi

echo -e "\e[31m\e[1m\nStep 1 - Installing GCC-ARM: \e[21m\e[97m"
AdjustSystem;
echo -e "\e[31m\e[1m\nStep 2 - Installing the Stellaris Library: \e[21m\e[97m"
StellarisInstaller $srcFolder
echo -e "\e[31m\e[1m\nStep 3 - Installing lm4tools: \e[21m\e[97m"
lm4Install $srcFolder
echo -e "\e[31m\e[1m\nStep 4 - Configuring auto-tools: \e[21m\e[97m\n"
autoInstall $srcFolder
echo -e "\e[31m\e[1m\nStep 5 - Instaling OpenOCD: \e[21m\e[97m\n"
openInstall $srcFolder

sudo rm -rf ~/log_report_installer_arm.txt &>> /dev/null

echo -e "\e[34m------------------Success!------------------\e[97m"

echo -e "\e[0m"
exit 0


Step 2. Change the permission to execute:

In this step you just need to change the permission to execute the file into your system and for this open the terminal to put the command below:
sudo chmod +x name.sh


Step 3. Executing:

Before to execute i almost lost to advice, but you need to download the Stellaris Ware(SW-EK-LM4F120XL) from this link and put them into your ~/Downloads directory. And for finish, to execute the script just call them with "./name.sh" like below:
./name.sh

Tks All.