]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/loadopenocd/loadopenocd.sh
Change license to MIT
[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 # Permission is hereby granted, free of charge, to any person
9 # obtaining a copy of this software and associated documentation
10 # files (the "Software"), to deal in the Software without
11 # restriction, including without limitation the rights to use,
12 # copy, modify, merge, publish, distribute, sublicense, and/or sell
13 # copies of the Software, and to permit persons to whom the
14 # Software is furnished to do so, subject to the following
15 # conditions:
16
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
19
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 # OTHER DEALINGS IN THE SOFTWARE.
28 #
29 # File : loadopenocd.sh.py
30 # Abstract:
31 #       Wrapper script for downloading binary code via OpenOCD 
32 #
33 # Refs:
34 #       http://rtime.felk.cvut.cz/hw/index.php/TMS570LS3137#OpenOCD_setup_and_Flashing
35
36 function show_help {
37         echo "TMS570LS3137 loader script v0.1.0-dev (2014-09-02)"
38         echo "Licensed under GNU GPL v2"
39         echo "Downloads a compiled binary file to the Texas Instruments TMS570LS3137 MCU."
40         echo "-h    display this help"
41         echo "-d    destination selector <sdram|flash>"
42         echo "-b    halt after download completed"
43         echo "-s    path to the binary file to be downloaded to the MCU"
44         echo "----"
45         echo "example: ./load.sh -d flash -b -s ./bin/foo.out"
46 }
47
48 HALT=0
49 SOURCE="0"
50 DESTINATION="0"
51 LOAD_PATH=$(dirname $(which $0))
52 FLASH_MEMORY_SIZE=3145728 #3MB
53 SDRAM_MEMORY_SIZE=8388608 #8MB
54 MEMORY_SIZE=$FLASH_MEMORY_SIZE
55
56 while getopts  "hd:bs:" flag
57 do
58         case "$flag" in
59                 h) show_help; exit 0;;
60                 d)  case "$OPTARG" in
61                                 sdram) DESTINATION="SDRAM";;
62                                 flash) DESTINATION="FLASH";;
63                                 *) echo "unknown destination"; exit 1;;
64                         esac ;;
65                 b) HALT=1;;
66                 s) SOURCE="$OPTARG";;
67                 *) exit 1;;
68         esac
69 done
70
71 if [ $DESTINATION == "0" ]
72 then
73         echo "No destination specified."
74         exit 1
75 elif [ $DESTINATION == "SDRAM" ]
76 then
77         MEMORY_SIZE=$SDRAM_MEMORY_SIZE
78 fi
79
80 if [ $SOURCE == "0" ]
81 then
82         echo "No source binary file specified."
83         exit 1
84 fi
85 if [ ! -f $SOURCE ]
86 then
87         echo "Source binary file does not exists."
88         exit 1
89 fi
90
91 SIZE=`size -d $SOURCE | awk 'NR==2 {print $1}'`
92
93 echo >&2 "Checking binary file size..."
94 if [ "$SIZE" -ge "$MEMORY_SIZE" ]
95 then
96         echo "The output binary file si bigger than the capacity of the memory."
97         echo >&2 "    [failed] - binary size: $SIZE, memory cize: $MEMORY_SIZE"
98         exit 1
99 fi
100
101 echo >&2 "    [OK] - binary size: $SIZE"
102
103 if [ $HALT -eq "0" ]
104 then
105         CFG_FILE=$LOAD_PATH/openocd.cfg
106 else
107         CFG_FILE=$LOAD_PATH/openocd-halt-immediatelly.cfg
108 fi
109
110 echo >&2 "Checking existence of $CFG_FILE file..."
111 if [ ! -f $CFG_FILE ]
112 then
113         echo "OpenOCD configuration file not found."
114         echo >&2 "    [failed] - file $CFG_FILE not found."
115         exit 2
116 fi
117
118 echo >&2 "Looking for OpenOCD executable..."
119 OPENOCD_PATH=`command -v openocd || { echo "OpenOCD executable not found."; echo >&2 "    [failed] - Could not find OpenOCD."; exit 3; }`
120 echo >&2 "    [OK] - $OPENOCD_PATH"
121
122 if [ $DESTINATION == "FLASH" ]
123 then 
124         echo "Downloading binary file..."
125         echo >&2 "Downloading binary file..."
126         CMD="openocd -f $CFG_FILE --command \"program $SOURCE\" --command \"exit\""     
127         echo >&2 "    $CMD"
128         openocd -f $CFG_FILE --command "program $SOURCE" --command "exit"
129 else
130         echo "SDRAM download not yet implemented."
131         exit 1;
132 fi
133 exit 0;