]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/loadopenocd/loadopenocd.sh
f1678ce7a0b24f226c93b53db2f8c05a9ead6911
[pes-rpp/rpp-simulink.git] / rpp / loadopenocd / loadopenocd.sh
1 #!/bin/bash
2
3 # Copyright (C) 2013-2014 Czech Technical University in Prague
4 #
5 # Authors:
6 #     - Michal Horn <hornmich@fel.cvut.cz>
7 #
8 # This document contains proprietary information belonging to Czech
9 # Technical University in Prague. Passing on and copying of this
10 # document, and communication of its contents is not permitted
11 # without prior written authorization.
12 #
13 # File : loadopenocd.sh.py
14 # Abstract:
15 #       Wrapper script for downloading binary code via OpenOCD 
16 #
17 # Refs:
18 #       http://rtime.felk.cvut.cz/hw/index.php/TMS570LS3137#OpenOCD_setup_and_Flashing
19
20 function show_help {
21         echo "TMS570LS3137 loader script v0.1.0-dev (2014-09-02)"
22         echo "Licensed under GNU GPL v2"
23         echo "Downloads a compiled binary file to the Texas Instruments TMS570LS3137 MCU."
24         echo "-h    display this help"
25         echo "-d    destination selector <sdram|flash>"
26         echo "-b    halt after download completed"
27         echo "-s    path to the binary file to be downloaded to the MCU"
28         echo "----"
29         echo "example: ./load.sh -d flash -b -s ./bin/foo.out"
30 }
31
32 HALT=0
33 SOURCE="0"
34 DESTINATION="0"
35 LOAD_PATH=$(dirname $(which $0))
36 FLASH_MEMORY_SIZE=3145728 #3MB
37 SDRAM_MEMORY_SIZE=8388608 #8MB
38 MEMORY_SIZE=$FLASH_MEMORY_SIZE
39
40 while getopts  "hd:bs:" flag
41 do
42         case "$flag" in
43                 h) show_help; exit 0;;
44                 d)  case "$OPTARG" in
45                                 sdram) DESTINATION="SDRAM";;
46                                 flash) DESTINATION="FLASH";;
47                                 *) echo "unknown destination"; exit 1;;
48                         esac ;;
49                 b) HALT=1;;
50                 s) SOURCE="$OPTARG";;
51                 *) exit 1;;
52         esac
53 done
54
55 if [ $DESTINATION == "0" ]
56 then
57         echo "No destination specified."
58         exit 1
59 elif [ $DESTINATION == "SDRAM" ]
60 then
61         MEMORY_SIZE=$SDRAM_MEMORY_SIZE
62 fi
63
64 if [ $SOURCE == "0" ]
65 then
66         echo "No source binary file specified."
67         exit 1
68 fi
69 if [ ! -f $SOURCE ]
70 then
71         echo "Source binary file does not exists."
72         exit 1
73 fi
74
75 SIZE=`size -d $SOURCE | awk 'NR==2 {print $1}'`
76
77 echo >&2 "Checking binary file size..."
78 if [ "$SIZE" -ge "$MEMORY_SIZE" ]
79 then
80         echo "The output binary file si bigger than the capacity of the memory."
81         echo >&2 "    [failed] - binary size: $SIZE, memory cize: $MEMORY_SIZE"
82         exit 1
83 fi
84
85 echo >&2 "    [OK] - binary size: $SIZE"
86
87 if [ $HALT -eq "0" ]
88 then
89         CFG_FILE=$LOAD_PATH/openocd.cfg
90 else
91         CFG_FILE=$LOAD_PATH/openocd-halt-immediatelly.cfg
92 fi
93
94 echo >&2 "Checking existence of $CFG_FILE file..."
95 if [ ! -f $CFG_FILE ]
96 then
97         echo "OpenOCD configuration file not found."
98         echo >&2 "    [failed] - file $CFG_FILE not found."
99         exit 2
100 fi
101
102 echo >&2 "Looking for OpenOCD executable..."
103 OPENOCD_PATH=`command -v openocd || { echo "OpenOCD executable not found."; echo >&2 "    [failed] - Could not find OpenOCD."; exit 3; }`
104 echo >&2 "    [OK] - $OPENOCD_PATH"
105
106 if [ $DESTINATION == "FLASH" ]
107 then 
108         echo "Downloading binary file..."
109         echo >&2 "Downloading binary file..."
110         CMD="openocd -f $CFG_FILE --command \"program $SOURCE\" --command \"exit\""     
111         echo >&2 "    $CMD"
112         openocd -f $CFG_FILE --command "program $SOURCE" --command "exit"
113 else
114         echo "SDRAM download not yet implemented."
115         exit 1;
116 fi
117 exit 0;